fast-nuclear-sim/src/coolant/valve.cpp

96 lines
1.9 KiB
C++
Raw Normal View History

#include "valve.hpp"
2024-02-05 18:33:31 +11:00
#include "../conversions/temperature.hpp"
#include "../util/constants.hpp"
2024-02-04 23:22:15 +11:00
#include <cmath>
#include <iostream>
using namespace sim::coolant;
2024-02-05 18:33:31 +11:00
valve::valve(fluid_holder* src, fluid_holder* dst, double state, double max) : src(src), dst(dst), max(max)
{
2024-02-05 18:33:31 +11:00
this->state = state;
}
void valve::add_open_speed(double v)
{
2024-02-04 23:22:15 +11:00
speed += v;
}
void valve::clear_open_speed()
{
2024-02-04 23:22:15 +11:00
speed = 0;
}
void valve::update(double dt)
{
2024-02-04 23:22:15 +11:00
state += speed * dt;
if(state > 1) state = 1;
if(state < 0) state = 0;
2024-02-04 23:22:15 +11:00
2024-02-13 17:05:57 +11:00
if(src->get_gas_volume() == 0 || dst->get_gas_volume() == 0 || (src->get_gas() == 0 && dst->get_gas() == 0))
2024-02-05 18:33:31 +11:00
{
flow = 0;
return;
}
double pressure1 = src->get_pressure(); // Pa
2024-02-04 23:22:15 +11:00
double pressure2 = dst->get_pressure();
2024-02-05 18:33:31 +11:00
2024-02-07 16:04:22 +11:00
double m = max * state;
double diff = (pressure1 - pressure2);
double remove = diff - diff * std::pow(1 - m, dt);
2024-02-13 17:05:57 +11:00
double ratio_a, ratio_s;
double mass_a, mass_s;
double mol;
if(remove < 0)
{
2024-02-13 17:05:57 +11:00
ratio_a = src->get_air() / src->get_gas();
ratio_s = src->get_steam() / src->get_gas();
mol = fluid_holder::calc_pressure_mol(src->get_heat_k(), src->get_gas_volume(), pressure1 - remove);
mass_a = src->get_air() - mol / constants::M_air;
mass_s = src->get_steam() - src->fluid.mol_to_g(mol);
}
else
{
2024-02-13 17:05:57 +11:00
ratio_a = dst->get_air() / dst->get_gas();
ratio_s = dst->get_steam() / dst->get_gas();
mol = fluid_holder::calc_pressure_mol(dst->get_heat_k(), dst->get_gas_volume(), pressure2 - remove);
mass_a = dst->get_air() - mol / constants::M_air;
mass_s = dst->get_steam() - dst->fluid.mol_to_g(mol);
}
2024-02-13 17:05:57 +11:00
mass_a *= ratio_a;
mass_s *= ratio_s;
2024-02-07 16:04:22 +11:00
double heat1 = src->get_heat(); // C
double heat2 = dst->get_heat();
2024-02-04 23:22:15 +11:00
2024-02-13 22:33:55 +11:00
src->add_gas(-mass_s, mass_a, heat2, 0);
dst->add_gas(mass_s, mass_a, heat1, 0);
2024-02-04 23:22:15 +11:00
2024-02-13 17:05:57 +11:00
this->flow = (mass_s + mass_a) / dt;
}
2024-02-13 22:33:55 +11:00
valve::operator Json::Value() const
{
Json::Value node;
node["max"] = max;
node["speed"] = speed;
node["state"] = state;
node["flow"] = flow;
return node;
}