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

136 lines
2.1 KiB
C++
Raw Normal View History

#include "fluid_holder.hpp"
#include "../util/constants.hpp"
#include "../conversions/temperature.hpp"
#include "../reactor/fuel/half_life.hpp"
#include <cmath>
2024-02-03 23:35:59 +11:00
#include <iostream>
using namespace sim::coolant;
2024-02-05 18:33:31 +11:00
fluid_holder::fluid_holder(fluid_t fluid, double volume, double extra_mass) : fluid(fluid), volume(volume), extra_mass(extra_mass)
{
}
double fluid_holder::add_heat(double m1, double t1)
{
double t2 = get_heat();
double t = t1 - t2;
2024-02-05 18:33:31 +11:00
double m2 = get_thermal_mass();
double m = m1 + m2;
2024-02-03 23:35:59 +11:00
if(m1 == 0 || m2 == 0)
return t1;
heat = t1 - t * m2 / m;
2024-02-03 23:35:59 +11:00
return heat;
}
double fluid_holder::add_fluid(double v2, double t2)
{
if(level + v2 > volume)
{
v2 = volume - level;
}
2024-02-05 18:33:31 +11:00
int m1 = get_thermal_mass();
int m2 = fluid.l_to_g(v2);
double t1 = get_heat();
double t = t1 - t2;
heat = t1 - t * m2 / (m1 + m2);
level += v2;
return v2;
}
double fluid_holder::extract_fluid(double amount)
{
if(amount < level)
{
level -= amount;
}
else
{
amount = level;
level = 0;
}
return amount;
}
2024-02-04 23:22:15 +11:00
void fluid_holder::add_steam(double m2, double t2)
{
2024-02-05 18:33:31 +11:00
double m1 = get_thermal_mass();
2024-02-04 23:22:15 +11:00
double t1 = heat;
double m = m1 + m2;
2024-02-04 23:22:15 +11:00
if(m > 0)
{
2024-02-05 18:33:31 +11:00
heat = t1 - (t1 - t2) * m2 / (m1 + m2);
}
2024-02-05 18:33:31 +11:00
steam += m2;
}
double fluid_holder::get_pressure() const
{
double T = conversions::temperature::c_to_k(heat);
2024-02-04 23:22:15 +11:00
double V = get_steam_volume() * 0.001;
double n = fluid.g_to_mol(steam);
2024-02-05 18:33:31 +11:00
if(V == 0)
{
return 0;
2024-02-05 18:33:31 +11:00
}
return (n * T * constants::R) / V;
}
2024-02-05 18:33:31 +11:00
double fluid_holder::get_steam_density() const
{
double v = get_steam_volume();
return v > 0 ? steam / v : 0;
2024-02-05 18:33:31 +11:00
}
void fluid_holder::update(double secs)
{
2024-02-05 18:33:31 +11:00
double mass = get_thermal_mass();
2024-02-04 23:22:15 +11:00
if(mass > 0)
{
2024-02-04 23:22:15 +11:00
double V = get_steam_volume() * 0.001;
double P = fluid.vapor_pressure.calc_p(heat);
double T = conversions::temperature::c_to_k(heat);
double n = fluid.mol_to_g((V * P) / (T * constants::R)) - steam;
double s = steam + n;
double l = fluid.l_to_g(level) - n;
double v = fluid.l_to_g(volume);
if(l < 0)
{
s += l;
l = 0;
}
if(l > v)
{
l = v;
s = 0;
}
double diff = s - steam;
steam = s;
level = fluid.g_to_l(l);
2024-02-05 18:33:31 +11:00
heat -= diff * fluid.jPg / mass;
}
}