2024-02-02 22:03:47 +11:00
|
|
|
|
|
|
|
#include "valve.hpp"
|
2024-02-05 18:33:31 +11:00
|
|
|
#include "../conversions/temperature.hpp"
|
2024-02-06 01:08:44 +11:00
|
|
|
#include "../util/constants.hpp"
|
2024-02-02 22:03:47 +11:00
|
|
|
|
2024-02-04 23:22:15 +11:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
|
2024-02-02 22:03:47 +11:00
|
|
|
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-02 22:03:47 +11:00
|
|
|
{
|
2024-02-05 18:33:31 +11:00
|
|
|
this->state = state;
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|
2024-02-03 18:12:18 +11:00
|
|
|
void valve::add_open_speed(double v)
|
2024-02-02 22:03:47 +11:00
|
|
|
{
|
2024-02-04 23:22:15 +11:00
|
|
|
speed += v;
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|
2024-02-03 18:12:18 +11:00
|
|
|
void valve::clear_open_speed()
|
2024-02-02 22:03:47 +11:00
|
|
|
{
|
2024-02-04 23:22:15 +11:00
|
|
|
speed = 0;
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
void valve::update(double dt)
|
|
|
|
{
|
2024-02-04 23:22:15 +11:00
|
|
|
state += speed * dt;
|
|
|
|
|
2024-02-03 18:12:18 +11:00
|
|
|
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);
|
|
|
|
|
2024-02-09 21:31:36 +11:00
|
|
|
double mol, mass;
|
|
|
|
|
|
|
|
if(remove < 0)
|
|
|
|
{
|
2024-02-13 15:28:39 +11:00
|
|
|
mol = fluid_holder::calc_pressure_mol(src->get_heat_k(), src->get_steam_volume(), pressure1 - remove);
|
2024-02-09 21:31:36 +11:00
|
|
|
mass = src->get_steam() - src->fluid.mol_to_g(mol);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2024-02-13 15:28:39 +11:00
|
|
|
mol = fluid_holder::calc_pressure_mol(dst->get_heat_k(), dst->get_steam_volume(), pressure2 - remove);
|
2024-02-09 21:31:36 +11:00
|
|
|
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;
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|