diff --git a/src/coolant/condenser_secondary.hpp b/src/coolant/condenser_secondary.hpp index 7023517..1c2a6f1 100644 --- a/src/coolant/condenser_secondary.hpp +++ b/src/coolant/condenser_secondary.hpp @@ -18,8 +18,8 @@ public: condenser_secondary(condenser* primary, evaporator* source, double volume); virtual double add_heat(double m, double t) { return source->add_heat(m, t); } - virtual void add_gas(double steam, double gas, double t, double e) { return source->add_gas(steam, gas, t, e); } virtual double extract_fluid(double amount) { return source->extract_fluid(amount); } + virtual void add_gas(double steam, double gas, double t) { return source->add_gas(steam, gas, t); } virtual double add_fluid(double amount, double heat); diff --git a/src/coolant/fluid_holder.cpp b/src/coolant/fluid_holder.cpp index 1a06809..79cc2a7 100644 --- a/src/coolant/fluid_holder.cpp +++ b/src/coolant/fluid_holder.cpp @@ -77,7 +77,7 @@ double fluid_holder::extract_fluid(double amount) return amount; } -void fluid_holder::add_gas(double m_s2, double m_a2, double t_2, double e_2) +void fluid_holder::add_gas(double m_s2, double m_a2, double t_2) { double m_2 = m_a2 + m_s2; double m_1 = get_thermal_mass(); diff --git a/src/coolant/fluid_holder.hpp b/src/coolant/fluid_holder.hpp index 5f1cd4d..e2f093b 100644 --- a/src/coolant/fluid_holder.hpp +++ b/src/coolant/fluid_holder.hpp @@ -31,7 +31,7 @@ public: virtual double extract_fluid(double amount); virtual double add_fluid(double amount, double heat); - virtual void add_gas(double steam, double air, double heat, double energy); + virtual void add_gas(double steam, double air, double heat); virtual double get_volume() const { return volume; } // litres virtual double get_level() const { return level; } // litres diff --git a/src/coolant/pump.cpp b/src/coolant/pump.cpp index a04a3ec..9e87aec 100644 --- a/src/coolant/pump.cpp +++ b/src/coolant/pump.cpp @@ -1,5 +1,6 @@ #include "pump.hpp" +#include "../util/math.hpp" #include #include @@ -53,18 +54,6 @@ const char* pump::get_state_string() return "On"; } -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) { if(powered) @@ -82,7 +71,7 @@ void pump::update(double dt) break; } - velocity += calc_work(dt * power * max_power, mass); + velocity += util::calc_work(dt * power * max_power, mass); } else @@ -99,9 +88,9 @@ void pump::update(double dt) double p_diff_2 = dst->get_pressure() - src->get_pressure(); double p_diff = (p_diff_1 + p_diff_2) / 2; - double work = p_diff * dst_volume * 0.001 + get_rpm() * 60 * dt * friction; + double work = p_diff * dst_volume * 0.001 + get_rpm() / 60 * dt * friction; - velocity = std::max(velocity - calc_work(work, mass), 0.0); + velocity = std::max(velocity - util::calc_work(work, mass), 0.0); flow = dst_volume / dt; } diff --git a/src/coolant/valve.cpp b/src/coolant/valve.cpp index 53a2d1b..18d7b1a 100644 --- a/src/coolant/valve.cpp +++ b/src/coolant/valve.cpp @@ -75,8 +75,8 @@ void valve::update(double dt) double heat1 = src->get_heat(); // C double heat2 = dst->get_heat(); - src->add_gas(-mass_s, mass_a, heat2, 0); - dst->add_gas(mass_s, mass_a, heat1, 0); + src->add_gas(-mass_s, mass_a, heat2); + dst->add_gas(mass_s, mass_a, heat1); this->flow = (mass_s + mass_a) / dt; } diff --git a/src/electric/turbine.cpp b/src/electric/turbine.cpp index 45f2afc..fb3d6dd 100644 --- a/src/electric/turbine.cpp +++ b/src/electric/turbine.cpp @@ -1,6 +1,7 @@ #include "turbine.hpp" #include "../system.hpp" +#include "../util/math.hpp" #include #include @@ -25,6 +26,7 @@ turbine::turbine(const Json::Value& node, coolant::condenser* condenser) : condenser(condenser), length(node["length"].asDouble()), diameter(node["diameter"].asDouble()), + friction(node["friction"].asDouble()), sim::coolant::fluid_holder(node) { velocity = node["velocity"].asDouble(); @@ -32,12 +34,20 @@ turbine::turbine(const Json::Value& node, coolant::condenser* condenser) : void turbine::update(double dt) { - + double work = get_rpm() / 60 * dt * friction; + velocity = std::max(velocity - work, 0.0); } -void turbine::add_gas(double steam, double air, double t, double e) +double turbine::get_rpm() const { - condenser->add_gas(steam, air, t, e); + return velocity / (M_PI * extra_mass * 0.001 * diameter * diameter * 0.25); +} + +void turbine::add_gas(double steam, double air, double t) +{ + double joules = (steam + air) * fluid.jPg; + velocity = std::max(velocity + util::calc_work(joules, extra_mass), 0.0); + condenser->add_gas(steam, air, t); } turbine::operator Json::Value() const @@ -47,6 +57,7 @@ turbine::operator Json::Value() const node["length"] = length; node["diameter"] = diameter; node["velocity"] = velocity; + node["friction"] = friction; return node; } diff --git a/src/electric/turbine.hpp b/src/electric/turbine.hpp index 975e091..eb6683c 100644 --- a/src/electric/turbine.hpp +++ b/src/electric/turbine.hpp @@ -13,6 +13,7 @@ class turbine : public sim::coolant::fluid_holder const double length; const double diameter; + const double friction = 1; double velocity = 0; // m/s @@ -22,11 +23,13 @@ public: turbine(const Json::Value& node, coolant::condenser* condenser); void update(double dt); + double get_rpm() const; virtual double add_heat(double m, double t) { return condenser->add_heat(m, t); } virtual double extract_fluid(double amount) { return condenser->extract_fluid(amount); } virtual double add_fluid(double amount, double heat) { return condenser->add_fluid(amount, heat); } - virtual void add_gas(double steam, double gas, double t, double e); + + virtual void add_gas(double steam, double gas, double t); virtual double get_volume() const { return condenser->get_volume(); } virtual double get_level() const { return condenser->get_level(); } diff --git a/src/graphics/input/focus.cpp b/src/graphics/input/focus.cpp index 459a6ca..2653560 100644 --- a/src/graphics/input/focus.cpp +++ b/src/graphics/input/focus.cpp @@ -136,6 +136,22 @@ void focus::update(double dt) } } +void focus::render_ui() +{ + if(state) + { + state->render_ui(); + } +} + +void focus::render() +{ + if(state) + { + state->render(); + } +} + bool focus::is_focused() { return (state != nullptr); diff --git a/src/graphics/input/focus.hpp b/src/graphics/input/focus.hpp index ad8f5cb..5569506 100644 --- a/src/graphics/input/focus.hpp +++ b/src/graphics/input/focus.hpp @@ -17,6 +17,8 @@ struct focus_t virtual void on_cursor_pos(double x, double y) { } virtual void on_charcode(unsigned int c) { } virtual void update(double dt) { } + virtual void render_ui() { } + virtual void render() { } }; bool is_focused(); @@ -32,6 +34,8 @@ void on_mouse_button(int button, int action, int mods); void on_cursor_pos(double x, double y); void on_charcode(unsigned int c); void update(double dt); +void render_ui(); +void render(); }; diff --git a/src/graphics/mesh/font.cpp b/src/graphics/mesh/font.cpp index 0db843a..e791621 100644 --- a/src/graphics/mesh/font.cpp +++ b/src/graphics/mesh/font.cpp @@ -101,6 +101,13 @@ void mesh::load_text(const char* text, double size) float x = 0, y = size; unsigned int at = 0; + if(text[0] == '\0') + { + this->vertices.clear(); + this->indices.clear(); + return; + } + for(unsigned int i = 0; text[i] != '\0'; i++) { char c = text[i]; @@ -139,7 +146,7 @@ void mesh::load_text(const char* text, double size) x += ch.advance * size; } - set_vertices(&vertices[0], vertices.size()); - set_indices(&indices[0], indices.size()); + this->vertices = std::move(vertices); + this->indices = std::move(indices); } diff --git a/src/graphics/monitor/turbine.cpp b/src/graphics/monitor/turbine.cpp new file mode 100644 index 0000000..0876031 --- /dev/null +++ b/src/graphics/monitor/turbine.cpp @@ -0,0 +1,78 @@ + +#include +#include + +#include "helpers.hpp" +#include "turbine.hpp" +#include "../locations.hpp" +#include "../../system.hpp" +#include "../../coolant/valve.hpp" +#include "../input/focus.hpp" + +#include +#include + +using namespace sim::graphics; +using namespace sim::graphics::monitor; + +turbine::turbine() +{ + +} + +void turbine::init() +{ + mesh1.model_matrix = locations::monitors[4]; + mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0)); + + mesh1.colour_matrix = mesh2.colour_matrix = { + 1, 1, 1, 1, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 + }; + + std::stringstream ss; + sim::graphics::mesh rmesh; + + ss << "Turbine\n\n"; + ss << "Heat\nPressure\nSpeed\n\n"; + + rmesh.load_text(ss.str().c_str(), 0.04); + mesh1.bind(); + mesh1.set(rmesh, GL_STATIC_DRAW); +} + +void turbine::update(double dt) +{ + system& sys = sim::system::active; + clock_now += dt; + + if(clock_at + 1.0/30.0 < clock_now) + { + std::stringstream ss; + sim::graphics::mesh rmesh; + clock_at += 1.0/30.0; + + ss << "\n\n"; + ss << show( sys.turbine->get_heat() ) << " C\n"; + ss << show( sys.turbine->get_pressure() / 1000 ) << " kPa\n"; + ss << show( sys.turbine->get_rpm() ) << " r/min\n"; + + rmesh.load_text(ss.str().c_str(), 0.04); + mesh2.bind(); + mesh2.set(rmesh, GL_DYNAMIC_DRAW); + } +} + +void turbine::render() +{ + mesh1.bind(); + mesh1.uniform(); + mesh1.render(); + + mesh2.bind(); + mesh2.uniform(); + mesh2.render(); +} + diff --git a/src/graphics/monitor/turbine.hpp b/src/graphics/monitor/turbine.hpp new file mode 100644 index 0000000..a6a3c60 --- /dev/null +++ b/src/graphics/monitor/turbine.hpp @@ -0,0 +1,23 @@ + +#pragma once + +#include "../mesh/glmesh.hpp" + +namespace sim::graphics::monitor +{ + +class turbine +{ + sim::graphics::glmesh mesh1, mesh2; + double clock_at = 0, clock_now = 0; + +public: + + turbine(); + void init(); + void update(double dt); + void render(); +}; + +}; + diff --git a/src/graphics/widget/clock.cpp b/src/graphics/widget/clock.cpp index 9e84866..c2d9436 100644 --- a/src/graphics/widget/clock.cpp +++ b/src/graphics/widget/clock.cpp @@ -21,7 +21,7 @@ using namespace sim::graphics::widget; void clock::update(double dt) { mesh m; - double at = 3600 * 12 + system::active.clock; + double at = system::active.clock; glm::vec2 wsize(resize::get_size() / 2); std::stringstream ss; diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index ee7d21f..8fa6e3b 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -12,6 +12,7 @@ #include "mesh/arrays.hpp" #include "input/keyboard.hpp" #include "input/mouse.hpp" +#include "input/focus.hpp" #include "camera.hpp" #include "resize.hpp" #include "window.hpp" @@ -22,6 +23,7 @@ #include "monitor/core.hpp" #include "monitor/primary_loop.hpp" #include "monitor/secondary_loop.hpp" +#include "monitor/turbine.hpp" #include "mesh/texture.hpp" #include "ui.hpp" @@ -35,6 +37,7 @@ static monitor::vessel monitor_vessel; static monitor::core monitor_core; static monitor::primary_loop monitor_primary_loop; static monitor::secondary_loop monitor_secondary_loop; +static monitor::turbine monitor_turbine; glm::mat4 window::projection_matrix; @@ -116,6 +119,7 @@ void window::create() monitor_vessel.init(); monitor_primary_loop.init(); monitor_secondary_loop.init(); + monitor_turbine.init(); glfwShowWindow(win); glViewport(0, 0, 800, 600); @@ -129,6 +133,7 @@ void window::update(double dt) monitor_vessel.update(dt); monitor_primary_loop.update(dt); monitor_secondary_loop.update(dt); + monitor_turbine.update(dt); ui::update(dt); } @@ -152,8 +157,11 @@ void window::render() monitor_vessel.render(); monitor_primary_loop.render(); monitor_secondary_loop.render(); + monitor_turbine.render(); + focus::render(); ui::render(); + focus::render_ui(); glfwSwapBuffers(win); } diff --git a/src/system.hpp b/src/system.hpp index ccb0492..6156bc0 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -38,7 +38,7 @@ struct system std::unique_ptr turbine_inlet_valve; double speed = 1; - double clock = 0; + double clock = 3600 * 12; system(); system(const Json::Value& node); diff --git a/src/util/math.hpp b/src/util/math.hpp index 53cb189..131656e 100644 --- a/src/util/math.hpp +++ b/src/util/math.hpp @@ -3,6 +3,7 @@ #include #include +#include template std::ostream& operator<<(std::ostream& o, const glm::vec& v) @@ -18,3 +19,20 @@ std::ostream& operator<<(std::ostream& o, const glm::vec& v) return o; } +namespace sim::util +{ + +constexpr double calc_work(double j, double mass) +{ + double m = 1; + + if(j < 0) + { + m = -1; + } + + return m * std::sqrt(m * j / (mass * 0.001)); +} + +}; +