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

69 lines
1.3 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-05 18:33:31 +11:00
if(src->get_steam_volume() == 0 || dst->get_steam_volume() == 0)
{
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);
double mol, mass;
if(remove < 0)
{
mol = fluid_holder::calc_pressure_mol(src->get_heat_k(), src->get_steam_volume(), pressure1 - remove);
mass = src->get_steam() - src->fluid.mol_to_g(mol);
}
else
{
mol = fluid_holder::calc_pressure_mol(dst->get_heat_k(), dst->get_steam_volume(), pressure2 - remove);
mass = dst->get_steam() - dst->fluid.mol_to_g(mol);
}
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-07 16:04:22 +11:00
src->add_steam(-mass, heat2);
dst->add_steam(mass, heat1);
2024-02-04 23:22:15 +11:00
this->flow = mass / dt;
}