2024-02-02 22:03:47 +11:00
|
|
|
|
|
|
|
#include "pump.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace sim::coolant;
|
|
|
|
|
2024-02-06 01:08:44 +11:00
|
|
|
pump::pump(fluid_holder* src, fluid_holder* dst, double mass, double radius, double power, double l_per_rev, double friction) :
|
2024-02-06 17:57:59 +11:00
|
|
|
src(src),
|
|
|
|
dst(dst),
|
|
|
|
mass(mass),
|
|
|
|
radius(radius),
|
|
|
|
l_per_rev(l_per_rev),
|
|
|
|
friction(friction),
|
|
|
|
max_power(power)
|
2024-02-02 22:03:47 +11:00
|
|
|
{
|
2024-02-06 17:57:59 +11:00
|
|
|
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|
2024-02-06 17:57:59 +11:00
|
|
|
double pump::get_flow_target() const
|
2024-02-02 22:03:47 +11:00
|
|
|
{
|
2024-02-06 01:08:44 +11:00
|
|
|
return l_per_rev * get_rpm() * 60;
|
|
|
|
}
|
|
|
|
|
2024-02-06 17:57:59 +11:00
|
|
|
double pump::get_flow() const
|
|
|
|
{
|
|
|
|
return flow;
|
|
|
|
}
|
|
|
|
|
2024-02-06 01:08:44 +11:00
|
|
|
double pump::get_flow_mass() const
|
|
|
|
{
|
2024-02-06 17:57:59 +11:00
|
|
|
return src->fluid.l_to_g(flow);
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
double pump::get_rpm() const
|
|
|
|
{
|
|
|
|
return velocity / (M_PI * mass * 0.001 * radius * radius);
|
|
|
|
}
|
|
|
|
|
2024-02-06 17:57:59 +11:00
|
|
|
double pump::get_power() const
|
2024-02-05 18:33:31 +11:00
|
|
|
{
|
2024-02-06 17:57:59 +11:00
|
|
|
return power;
|
|
|
|
}
|
2024-02-05 18:33:31 +11:00
|
|
|
|
2024-02-06 17:57:59 +11:00
|
|
|
const char* pump::get_state_string()
|
|
|
|
{
|
|
|
|
if(!powered) return "Off";
|
|
|
|
if(power == 0) return "Idle";
|
|
|
|
return "On";
|
2024-02-05 18:33:31 +11:00
|
|
|
}
|
|
|
|
|
2024-02-02 22:03:47 +11:00
|
|
|
static double calc_work(double j, double mass)
|
|
|
|
{
|
|
|
|
double m = 1;
|
|
|
|
|
|
|
|
if(j < 0)
|
|
|
|
{
|
|
|
|
m = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m * std::sqrt(m * j / (mass * 0.001));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pump::update(double dt)
|
|
|
|
{
|
2024-02-06 17:57:59 +11:00
|
|
|
if(powered)
|
2024-02-05 18:33:31 +11:00
|
|
|
{
|
2024-02-06 17:57:59 +11:00
|
|
|
power = pid.calculate(dt, src->get_volume() / 2, src->get_steam_volume());
|
|
|
|
velocity += calc_work(dt * power * max_power, mass);
|
2024-02-05 18:33:31 +11:00
|
|
|
}
|
2024-02-02 22:03:47 +11:00
|
|
|
|
2024-02-06 01:08:44 +11:00
|
|
|
fluid_holder fh_src(*src);
|
|
|
|
fluid_holder fh_dst(*dst);
|
|
|
|
|
2024-02-02 22:03:47 +11:00
|
|
|
double src_heat = src->get_heat();
|
|
|
|
double p_diff_1 = dst->get_pressure() - src->get_pressure();
|
2024-02-06 17:57:59 +11:00
|
|
|
double src_volume = fh_src.extract_fluid(get_flow_target() * dt);
|
2024-02-06 01:08:44 +11:00
|
|
|
double dst_volume = fh_dst.add_fluid(src_volume, src_heat);
|
2024-02-02 22:03:47 +11:00
|
|
|
|
2024-02-06 01:08:44 +11:00
|
|
|
src->extract_fluid(dst_volume);
|
|
|
|
dst->add_fluid(dst_volume, src_heat);
|
2024-02-02 22:03:47 +11:00
|
|
|
|
|
|
|
double p_diff_2 = dst->get_pressure() - src->get_pressure();
|
|
|
|
double p_diff = (p_diff_1 + p_diff_2) / 2;
|
2024-02-06 01:08:44 +11:00
|
|
|
double work = p_diff * dst_volume * 0.001 + get_rpm() * 60 * dt * friction;
|
2024-02-02 22:03:47 +11:00
|
|
|
|
|
|
|
velocity -= calc_work(work, mass);
|
2024-02-06 17:57:59 +11:00
|
|
|
flow = dst_volume / dt;
|
2024-02-02 22:03:47 +11:00
|
|
|
}
|
|
|
|
|