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

88 lines
1.5 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
int overshoots = 0;
double m = max * state * dt;
2024-02-04 23:22:15 +11:00
double temp, mass;
2024-02-05 18:33:31 +11:00
for(;;)
2024-02-04 23:22:15 +11:00
{
2024-02-05 18:33:31 +11:00
double diff = (pressure1 - pressure2) * m; // L
if(diff > 0)
{
temp = src->get_heat();
mass = std::min(diff * src->get_steam_density(), src->get_steam());
}
else
{
temp = dst->get_heat();
mass = std::min(diff * dst->get_steam_density(), dst->get_steam());
}
fluid_holder fh_src(*src);
fluid_holder fh_dst(*dst);
fh_src.add_steam(-mass, temp);
fh_dst.add_steam(mass, temp);
// if((pressure1 > fh_dst.get_pressure()) == (pressure2 < fh_src.get_pressure()))
{
break;
}
overshoots += 1;
m *= 0.5;
2024-02-04 23:22:15 +11:00
}
2024-02-05 18:33:31 +11:00
if(overshoots > 0)
2024-02-04 23:22:15 +11:00
{
2024-02-05 18:33:31 +11:00
std::cout << "Warning: overshot " << overshoots << " times\n";
2024-02-04 23:22:15 +11:00
}
src->add_steam(-mass, temp);
dst->add_steam(mass, temp);
this->flow = mass / dt;
}