diff --git a/src/coolant/condenser.cpp b/src/coolant/condenser.cpp index 06b08f0..cb6c1dd 100644 --- a/src/coolant/condenser.cpp +++ b/src/coolant/condenser.cpp @@ -19,6 +19,14 @@ condenser::condenser(fluid_t type, double height, double diameter, double mass, this->level = level; } +condenser::condenser(const Json::Value& node) : + height(node["height"].asDouble()), + diameter(node["diameter"].asDouble()), + fluid_holder(node) +{ + +} + void condenser::update(double secs) { update_base(secs); diff --git a/src/coolant/condenser.hpp b/src/coolant/condenser.hpp index 6ab2372..9f34855 100644 --- a/src/coolant/condenser.hpp +++ b/src/coolant/condenser.hpp @@ -14,10 +14,11 @@ class condenser : public fluid_holder public: condenser(fluid_t type, double height, double diameter, double mass, double level); + condenser(const Json::Value& node); void update(double dt); - virtual operator Json::Value() const; + operator Json::Value() const; }; }; diff --git a/src/coolant/evaporator.cpp b/src/coolant/evaporator.cpp index c81aa43..1db4307 100644 --- a/src/coolant/evaporator.cpp +++ b/src/coolant/evaporator.cpp @@ -22,6 +22,14 @@ evaporator::evaporator(fluid_t type, double height, double diameter, double mass this->level = level; } +evaporator::evaporator(const Json::Value& node) : + height(node["height"].asDouble()), + diameter(node["diameter"].asDouble()), + fluid_holder(node) +{ + steam_output = node["steam_output"].asDouble(); +} + double evaporator::get_steam_output() { return steam_output; diff --git a/src/coolant/evaporator.hpp b/src/coolant/evaporator.hpp index fbe52aa..5e09b07 100644 --- a/src/coolant/evaporator.hpp +++ b/src/coolant/evaporator.hpp @@ -16,8 +16,9 @@ class evaporator : public fluid_holder public: evaporator(fluid_t type, double height, double diameter, double mass, double level); + evaporator(const Json::Value& node); - virtual operator Json::Value() const; + operator Json::Value() const; double get_steam_output(); void update(double dt); diff --git a/src/coolant/fluid_holder.cpp b/src/coolant/fluid_holder.cpp index 5be021c..b33f609 100644 --- a/src/coolant/fluid_holder.cpp +++ b/src/coolant/fluid_holder.cpp @@ -14,6 +14,14 @@ fluid_holder::fluid_holder(fluid_t fluid, double volume, double extra_mass) : fl } +fluid_holder::fluid_holder(const Json::Value& node) : fluid(node["fluid"]), volume(node["volume"].asDouble()), extra_mass(node["extra_mass"].asDouble()) +{ + level = node["level"].asDouble(); + steam = node["steam"].asDouble(); + air = node["air"].asDouble(); + heat = node["heat"].asDouble(); +} + double fluid_holder::add_heat(double m1, double t1) { double t2 = get_heat(); diff --git a/src/coolant/fluid_holder.hpp b/src/coolant/fluid_holder.hpp index 94d04bd..5f1cd4d 100644 --- a/src/coolant/fluid_holder.hpp +++ b/src/coolant/fluid_holder.hpp @@ -21,6 +21,7 @@ class fluid_holder public: fluid_holder(fluid_t fluid, double volume, double extra_mass); + fluid_holder(const Json::Value& node); const fluid_t fluid; const double volume; // litres @@ -45,7 +46,7 @@ public: virtual double get_pressure() const; // pascals virtual double get_gas_density() const; // g/L - virtual operator Json::Value() const; + operator Json::Value() const; static double calc_pressure(double heat, double pressure, double mol); static double calc_pressure_mol(double heat, double pressure, double volume); diff --git a/src/coolant/fluid_t.hpp b/src/coolant/fluid_t.hpp index d1c5f4a..6bffc30 100644 --- a/src/coolant/fluid_t.hpp +++ b/src/coolant/fluid_t.hpp @@ -23,6 +23,15 @@ struct fluid_t } + fluid_t(const Json::Value& node) : + gPl(node["gPl"].asDouble()), + gPmol(node["gPmol"].asDouble()), + jPg(node["jPg"].asDouble()), + vapor_pressure(node["vapor_pressure"]) + { + + } + operator Json::Value() const { Json::Value node; diff --git a/src/coolant/pump.cpp b/src/coolant/pump.cpp index 7a67b21..a04a3ec 100644 --- a/src/coolant/pump.cpp +++ b/src/coolant/pump.cpp @@ -3,6 +3,7 @@ #include #include +#include using namespace sim::coolant; @@ -104,6 +105,34 @@ void pump::update(double dt) flow = dst_volume / dt; } +static pump::mode_t get_mode(std::string mode) +{ + if(mode == "SRC") + return pump::mode_t::SRC; + if(mode == "DST") + return pump::mode_t::DST; + + return pump::mode_t::NONE; +} + +pump::pump(const Json::Value& node, fluid_holder* src, fluid_holder* dst) : + mode(get_mode(node["mode"].asString())), + mass(node["mass"].asDouble()), + radius(node["radius"].asDouble()), + l_per_rev(node["l_per_rev"].asDouble()), + friction(node["friction"].asDouble()), + max_power(node["max_power"].asDouble()), + target(node["target"].asDouble()), + pid(node["pid"]), + src(src), + dst(dst) +{ + flow = node["flow"].asDouble(); + velocity = node["velocity"].asDouble(); + power = node["power"].asDouble(); + powered = node["powered"].asBool(); +} + pump::operator Json::Value() const { Json::Value node; diff --git a/src/coolant/pump.hpp b/src/coolant/pump.hpp index e117fdc..032aef3 100644 --- a/src/coolant/pump.hpp +++ b/src/coolant/pump.hpp @@ -39,6 +39,7 @@ public: bool powered = false; pump(fluid_holder* src, fluid_holder* dst, double mass, double radius, double power, double l_per_rev, double friction, mode_t mode, double target); + pump(const Json::Value& node, fluid_holder* src, fluid_holder* dst); double get_flow() const; // L/s double get_flow_target() const; // L/s diff --git a/src/coolant/valve.cpp b/src/coolant/valve.cpp index 40cf58b..3ad2fdf 100644 --- a/src/coolant/valve.cpp +++ b/src/coolant/valve.cpp @@ -81,6 +81,13 @@ void valve::update(double dt) this->flow = (mass_s + mass_a) / dt; } +valve::valve(const Json::Value& node, fluid_holder* src, fluid_holder* dst) : src(src), dst(dst), max(node["max"].asDouble()) +{ + speed = node["speed"].asDouble(); + state = node["state"].asDouble(); + flow = node["flow"].asDouble(); +} + valve::operator Json::Value() const { Json::Value node; diff --git a/src/coolant/valve.hpp b/src/coolant/valve.hpp index fe9c0a7..ab78698 100644 --- a/src/coolant/valve.hpp +++ b/src/coolant/valve.hpp @@ -20,6 +20,7 @@ class valve public: valve(fluid_holder* src, fluid_holder* dst, double state, double max); + valve(const Json::Value& node, fluid_holder* src, fluid_holder* dst); void update(double secs); void add_open_speed(double v); diff --git a/src/coolant/vapor_pressure.cpp b/src/coolant/vapor_pressure.cpp index 67862f2..3ddb4b6 100644 --- a/src/coolant/vapor_pressure.cpp +++ b/src/coolant/vapor_pressure.cpp @@ -15,6 +15,14 @@ double vapor_pressure::calc_t(double p) const return B / (A - std::log(p) / std::log(10)) - C; } +vapor_pressure::vapor_pressure(const Json::Value& node) : + A(node["A"].asDouble()), + B(node["B"].asDouble()), + C(node["C"].asDouble()) +{ + +} + vapor_pressure::operator Json::Value() const { Json::Value node; diff --git a/src/coolant/vapor_pressure.hpp b/src/coolant/vapor_pressure.hpp index 0131f8f..2b7dc78 100644 --- a/src/coolant/vapor_pressure.hpp +++ b/src/coolant/vapor_pressure.hpp @@ -12,6 +12,7 @@ struct vapor_pressure constexpr vapor_pressure(double A, double B, double C) : A(A), B(B), C(C) { } + vapor_pressure(const Json::Value& node); operator Json::Value() const; double calc_p(double t) const; diff --git a/src/electric/turbine.cpp b/src/electric/turbine.cpp index 7a15543..45f2afc 100644 --- a/src/electric/turbine.cpp +++ b/src/electric/turbine.cpp @@ -21,6 +21,15 @@ turbine::turbine(coolant::fluid_t type, coolant::condenser* condenser, double le } +turbine::turbine(const Json::Value& node, coolant::condenser* condenser) : + condenser(condenser), + length(node["length"].asDouble()), + diameter(node["diameter"].asDouble()), + sim::coolant::fluid_holder(node) +{ + velocity = node["velocity"].asDouble(); +} + void turbine::update(double dt) { diff --git a/src/electric/turbine.hpp b/src/electric/turbine.hpp index 0edc48d..975e091 100644 --- a/src/electric/turbine.hpp +++ b/src/electric/turbine.hpp @@ -19,6 +19,7 @@ class turbine : public sim::coolant::fluid_holder public: turbine(coolant::fluid_t type, coolant::condenser* condenser, double length, double diameter, double mass); + turbine(const Json::Value& node, coolant::condenser* condenser); void update(double dt); @@ -40,7 +41,7 @@ public: virtual double get_pressure() const { return condenser->get_pressure(); } // pascals virtual double get_gas_density() const { return condenser->get_gas_density(); } // g/L - virtual operator Json::Value() const; + operator Json::Value() const; }; }; diff --git a/src/graphics/input/keyboard.cpp b/src/graphics/input/keyboard.cpp index 00cf233..56512fd 100644 --- a/src/graphics/input/keyboard.cpp +++ b/src/graphics/input/keyboard.cpp @@ -47,7 +47,10 @@ static void cb_keypress(GLFWwindow* win, int key, int sc, int action, int mods) sim::system::active.speed = 43200; // 12 h/s break; case GLFW_KEY_O: - sim::system::active.save(); + sim::system::save(); + break; + case GLFW_KEY_L: + sim::system::load(); break; } } diff --git a/src/graphics/widget/clock.cpp b/src/graphics/widget/clock.cpp index 4ba09ae..9e84866 100644 --- a/src/graphics/widget/clock.cpp +++ b/src/graphics/widget/clock.cpp @@ -21,6 +21,7 @@ using namespace sim::graphics::widget; void clock::update(double dt) { mesh m; + double at = 3600 * 12 + system::active.clock; glm::vec2 wsize(resize::get_size() / 2); std::stringstream ss; @@ -32,7 +33,6 @@ void clock::update(double dt) ss << std::setfill('0') << std::setw(2) << t_m << ":"; ss << std::setfill('0') << std::setw(2) << t_s << "\n"; ss << "Day: " << std::floor(at / (3600 * 24)) << "\n"; - at += dt * sim::system::active.speed; m.load_text(ss.str().c_str(), 20); diff --git a/src/graphics/widget/clock.hpp b/src/graphics/widget/clock.hpp index 5228949..52a502a 100644 --- a/src/graphics/widget/clock.hpp +++ b/src/graphics/widget/clock.hpp @@ -8,7 +8,6 @@ namespace sim::graphics::widget struct clock { - double at = 3600 * 12; glmesh data; void update(double dt); diff --git a/src/main.cpp b/src/main.cpp index 5ca443c..26feabd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,14 +37,11 @@ int main() // tests::run(); // return 0; - std::ofstream log("log.csv"); graphics::window::create(); long clock = get_now(); double at = 0; - log << R"("clock","level","steam","heat","pressure")" << "\n"; - while(!graphics::window::should_close()) { long now = get_now(); @@ -59,12 +56,6 @@ int main() graphics::window::update(dt); graphics::focus::update(dt); graphics::window::render(); - - log << at << ","; - log << sim::system::active.condenser->get_level() << ","; - log << sim::system::active.condenser->get_steam() << ","; - log << sim::system::active.condenser->get_heat() << ","; - log << sim::system::active.condenser->get_pressure() << "\n"; } graphics::window::destroy(); diff --git a/src/reactor/builder.cpp b/src/reactor/builder.cpp index 3f30e58..82fa227 100644 --- a/src/reactor/builder.cpp +++ b/src/reactor/builder.cpp @@ -1,5 +1,9 @@ #include "builder.hpp" +#include "control/boron_rod.hpp" +#include "control/graphite_rod.hpp" +#include "coolant/pipe.hpp" +#include "coolant/heater.hpp" #include #include @@ -45,3 +49,24 @@ sim::reactor::reactor sim::reactor::builder(const int W, const int H, const doub return reactor(&arr[0], W, H, CW, CH); } +std::unique_ptr sim::reactor::load_rod(const Json::Value& node, coolant::vessel* v) +{ + int id = node["id"].asInt(); + + switch(id) + { + case 1: + return std::make_unique(node); + case 2: + return std::make_unique(node, v); + case 3: + return std::make_unique(node); + case 4: + return std::make_unique(node); + case 5: + return std::make_unique(node, v); + } + + return std::make_unique(); +} + diff --git a/src/reactor/builder.hpp b/src/reactor/builder.hpp index 1064be0..807eb08 100644 --- a/src/reactor/builder.hpp +++ b/src/reactor/builder.hpp @@ -3,17 +3,16 @@ #include "rod.hpp" #include "fuel/fuel_rod.hpp" -#include "control/boron_rod.hpp" -#include "control/graphite_rod.hpp" -#include "coolant/pipe.hpp" -#include "coolant/heater.hpp" #include "coolant/vessel.hpp" #include "reactor.hpp" +#include + namespace sim::reactor { reactor builder(const int W, const int H, const double CW, const double CH, fuel::fuel_rod fr, coolant::vessel* v, const char** lines); +std::unique_ptr load_rod(const Json::Value& node, coolant::vessel* v); }; diff --git a/src/reactor/control/boron_rod.cpp b/src/reactor/control/boron_rod.cpp index 1830fc8..46a94e5 100644 --- a/src/reactor/control/boron_rod.cpp +++ b/src/reactor/control/boron_rod.cpp @@ -9,6 +9,20 @@ constexpr double boron_density = 2340000; // g/m^3 constexpr double boron_molar_mass = 10; // g/mol constexpr double boron_molar_density = boron_density / boron_molar_mass; // mol/m^3 +boron_rod::boron_rod(const Json::Value& node, coolant::vessel* v) : coolant::pipe(node, v) +{ + inserted = node["inserted"].asDouble(); + absorbed = node["absorbed"].asDouble(); +} + +Json::Value boron_rod::serialize() const +{ + Json::Value node(coolant::pipe::serialize()); + node["inserted"] = inserted; + node["absorbed"] = absorbed; + return node; +} + boron_rod::boron_rod(coolant::vessel* v) : coolant::pipe(v) { diff --git a/src/reactor/control/boron_rod.hpp b/src/reactor/control/boron_rod.hpp index 7652cf0..dead840 100644 --- a/src/reactor/control/boron_rod.hpp +++ b/src/reactor/control/boron_rod.hpp @@ -19,7 +19,9 @@ class boron_rod : public coolant::pipe public: boron_rod(coolant::vessel* v); + boron_rod(const Json::Value& node, coolant::vessel* v); + virtual Json::Value serialize() const; virtual void update(double secs); void set_reactivity(double a); double get_inserted() { return inserted; } diff --git a/src/reactor/control/graphite_rod.cpp b/src/reactor/control/graphite_rod.cpp index 7c87b9d..32a2237 100644 --- a/src/reactor/control/graphite_rod.cpp +++ b/src/reactor/control/graphite_rod.cpp @@ -5,6 +5,18 @@ using namespace sim::reactor::control; +graphite_rod::graphite_rod(const Json::Value& node) : rod(node) +{ + inserted = node["inserted"].asDouble(); +} + +Json::Value graphite_rod::serialize() const +{ + Json::Value node(rod::serialize()); + node["inserted"] = inserted; + return node; +} + void graphite_rod::display(std::ostream& o) const { o << "Inserted: " << (inserted * 100) << "%\n"; diff --git a/src/reactor/control/graphite_rod.hpp b/src/reactor/control/graphite_rod.hpp index f5b8316..ce9e1be 100644 --- a/src/reactor/control/graphite_rod.hpp +++ b/src/reactor/control/graphite_rod.hpp @@ -19,7 +19,12 @@ class graphite_rod : public sim::reactor::rod double get_inserted() { return inserted; } public: + + constexpr graphite_rod() {} + graphite_rod(const Json::Value& node); + + virtual Json::Value serialize() const; virtual void update(double secs); void set_reactivity(double a); diff --git a/src/reactor/coolant/heater.cpp b/src/reactor/coolant/heater.cpp index 1d3eecb..cc6b2e7 100644 --- a/src/reactor/coolant/heater.cpp +++ b/src/reactor/coolant/heater.cpp @@ -3,6 +3,18 @@ using namespace sim::reactor::coolant; +heater::heater(const Json::Value& node) : rod(node) +{ + rate = node["rate"].asDouble(); +} + +Json::Value heater::serialize() const +{ + Json::Value node(rod::serialize()); + node["rate"] = rate; + return node; +} + void heater::update(double secs) { update_rod(secs); diff --git a/src/reactor/coolant/heater.hpp b/src/reactor/coolant/heater.hpp index 75958f0..626ee05 100644 --- a/src/reactor/coolant/heater.hpp +++ b/src/reactor/coolant/heater.hpp @@ -18,7 +18,12 @@ class heater : public sim::reactor::rod virtual int get_id() const { return 3; } public: + + constexpr heater() {} + + heater(const Json::Value& node); + virtual Json::Value serialize() const; virtual void update(double secs); virtual bool should_display() const { return true; } virtual bool should_select() const { return true; } diff --git a/src/reactor/coolant/pipe.cpp b/src/reactor/coolant/pipe.cpp index bc06964..15e458d 100644 --- a/src/reactor/coolant/pipe.cpp +++ b/src/reactor/coolant/pipe.cpp @@ -10,6 +10,18 @@ pipe::pipe(coolant::vessel* v) this->steam = 0; } +pipe::pipe(const Json::Value& node, coolant::vessel* v) : vessel(v) +{ + steam = node["steam"].asDouble(); +} + +Json::Value pipe::serialize() const +{ + Json::Value node(rod::serialize()); + node["steam"] = steam; + return node; +} + double pipe::get_k(val_t type) const { return vessel->get_level() / vessel->get_volume() * 0.5; diff --git a/src/reactor/coolant/pipe.hpp b/src/reactor/coolant/pipe.hpp index 001e9c1..30a3236 100644 --- a/src/reactor/coolant/pipe.hpp +++ b/src/reactor/coolant/pipe.hpp @@ -23,7 +23,9 @@ protected: public: pipe(coolant::vessel* v); + pipe(const Json::Value& node, coolant::vessel* v); + virtual Json::Value serialize() const; virtual std::unique_ptr clone() const { return std::make_unique(*this); } virtual bool should_display() const { return true; } virtual void update(double secs); diff --git a/src/reactor/coolant/vessel.cpp b/src/reactor/coolant/vessel.cpp index 7059d84..6d0e5cf 100644 --- a/src/reactor/coolant/vessel.cpp +++ b/src/reactor/coolant/vessel.cpp @@ -23,6 +23,15 @@ vessel::vessel(sim::coolant::fluid_t fluid, double height, double diameter, doub this->level = level; } +vessel::vessel(const Json::Value& node) : + sim::coolant::fluid_holder(node), + height(node["height"].asDouble()), + diameter(node["diameter"].asDouble()), + bubble_hl(node["bubble_hl"].asDouble()) +{ + steam_suspended = node["steam_suspended"].asDouble(); +} + double vessel::get_steam_suspended() const { return steam_suspended; diff --git a/src/reactor/coolant/vessel.hpp b/src/reactor/coolant/vessel.hpp index 4c851a4..af1970a 100644 --- a/src/reactor/coolant/vessel.hpp +++ b/src/reactor/coolant/vessel.hpp @@ -19,13 +19,14 @@ public: double steam_suspended = 0; // grams vessel(sim::coolant::fluid_t fluid, double height, double diameter, double mass, double level, double bubble_hl); + vessel(const Json::Value& node); double get_steam_suspended() const; // grams double get_void_ratio() const; void update(double secs); - virtual operator Json::Value() const; + operator Json::Value() const; friend std::ostream& operator<<(std::ostream& o, const vessel& v) { diff --git a/src/reactor/fuel/fuel_rod.cpp b/src/reactor/fuel/fuel_rod.cpp index fe2c250..a029aa0 100644 --- a/src/reactor/fuel/fuel_rod.cpp +++ b/src/reactor/fuel/fuel_rod.cpp @@ -15,6 +15,18 @@ fuel_rod::fuel_rod(double fuel) : s(fuel) } +fuel_rod::fuel_rod(const Json::Value& node) : s(node["sample"]), rod(node) +{ + +} + +Json::Value fuel_rod::serialize() const +{ + Json::Value node(rod::serialize()); + node["sample"] = s; + return node; +} + void fuel_rod::display(std::ostream& o) const { double mol = fuel_molar_density * get_volume(); diff --git a/src/reactor/fuel/fuel_rod.hpp b/src/reactor/fuel/fuel_rod.hpp index ff4be62..291eb81 100644 --- a/src/reactor/fuel/fuel_rod.hpp +++ b/src/reactor/fuel/fuel_rod.hpp @@ -23,7 +23,9 @@ class fuel_rod : public sim::reactor::rod public: fuel_rod(double fuel); + fuel_rod(const Json::Value& node); + virtual Json::Value serialize() const; virtual std::unique_ptr clone() const { return std::make_unique(*this); } virtual bool should_display() const { return true; } virtual void update(double secs); diff --git a/src/reactor/fuel/sample.cpp b/src/reactor/fuel/sample.cpp index 8638ba2..a52d1ea 100644 --- a/src/reactor/fuel/sample.cpp +++ b/src/reactor/fuel/sample.cpp @@ -15,6 +15,41 @@ sample::sample(double fuel) this->mass = 1; } +sample::sample(const Json::Value& node) : waste(node["waste"]) +{ + fuel = node["fuel"].asDouble(); + i_135 = node["i_135"].asDouble(); + xe_135 = node["xe_135"].asDouble(); + te_135 = node["te_135"].asDouble(); + u_238 = node["u_238"].asDouble(); + mass = node["mass"].asDouble(); + + energy = node["energy"].asDouble(); + fast_neutrons = node["neutrons"]["fast"].asDouble(); + slow_neutrons = node["neutrons"]["slow"].asDouble(); + efficiency = node["efficiency"].asDouble(); +} + +sample::operator Json::Value() const +{ + Json::Value node; + + node["waste"] = waste; + node["fuel"] = fuel; + node["i_135"] = i_135; + node["xe_135"] = xe_135; + node["te_135"] = te_135; + node["u_238"] = u_238; + node["mass"] = mass; + + node["energy"] = energy; + node["neutrons"]["fast"] = fast_neutrons; + node["neutrons"]["slow"] = slow_neutrons; + node["efficiency"] = efficiency; + + return node; +} + void sample::update(double secs) { double m; diff --git a/src/reactor/fuel/sample.hpp b/src/reactor/fuel/sample.hpp index 777479c..c6a415c 100644 --- a/src/reactor/fuel/sample.hpp +++ b/src/reactor/fuel/sample.hpp @@ -33,7 +33,10 @@ class sample public: sample(double fuel); + sample(const Json::Value& node); + operator Json::Value() const; + void update(double secs); constexpr double get_fuel() const { return fuel; } diff --git a/src/reactor/fuel/waste.cpp b/src/reactor/fuel/waste.cpp index 291d516..63b6d22 100644 --- a/src/reactor/fuel/waste.cpp +++ b/src/reactor/fuel/waste.cpp @@ -4,6 +4,40 @@ using namespace sim::reactor::fuel; +waste::waste(const Json::Value& node) +{ + const Json::Value& j_ladder = node["ladder"]; + + for(int i = 0; i < N; i++) + { + high[i] = j_ladder[i][0].asDouble(); + low[i] = j_ladder[i][1].asDouble(); + } + + neutrons = node["neutrons"].asDouble(); + energy = node["energy"].asDouble(); +} + +waste::operator Json::Value() const +{ + Json::Value node; + Json::Value j_ladder; + + for(int i = 0; i < N; i++) + { + Json::Value j_step; + j_step.append(high[i]); + j_step.append(low[i]); + j_ladder.append(std::move(j_step)); + } + + node["ladder"] = std::move(j_ladder); + node["neutrons"] = neutrons; + node["energy"] = energy; + + return node; +} + void waste::update(double secs) { double next[waste::N - 1] = {0}; diff --git a/src/reactor/fuel/waste.hpp b/src/reactor/fuel/waste.hpp index 68c3590..2e0b661 100644 --- a/src/reactor/fuel/waste.hpp +++ b/src/reactor/fuel/waste.hpp @@ -1,6 +1,8 @@ #pragma once +#include + namespace sim::reactor::fuel { @@ -14,6 +16,12 @@ class waste double energy = 0; public: + + constexpr waste() { }; + + waste(const Json::Value& node); + + operator Json::Value() const; void update(double secs); void add_fissile(double amount); diff --git a/src/reactor/reactor.cpp b/src/reactor/reactor.cpp index 5fbbb5b..db80679 100644 --- a/src/reactor/reactor.cpp +++ b/src/reactor/reactor.cpp @@ -1,6 +1,7 @@ #include "reactor.hpp" #include "../util/random.hpp" +#include "builder.hpp" #include @@ -239,6 +240,24 @@ void reactor::get_stats(rod::val_t type, double& min, double& max) } } +reactor::reactor(const Json::Value& node, coolant::vessel* v) : + cell_width(node["cell_width"].asDouble()), + cell_height(node["cell_height"].asDouble()), + width(node["width"].asDouble()), + height(node["height"].asDouble()), + size(node["size"].asDouble()) +{ + const Json::Value& j_rods = node["rods"]; + + rod_speed = node["rod_speed"].asDouble(); + cursor = node["cursor"].asInt(); + + for(int i = 0; i < size; i++) + { + rods.push_back(load_rod(j_rods[i], v)); + } +} + reactor::operator Json::Value() const { Json::Value node; @@ -260,13 +279,16 @@ reactor::operator Json::Value() const if(rods[i]->get_id() == 0) { - continue; + Json::Value j_rod; + j_rod["id"] = 0; + j_rods.append(std::move(j_rod)); } - Json::Value j_rod(*rods[i]); - j_rod["pos"]["x"] = x; - j_rod["pos"]["y"] = y; - j_rods.append(std::move(j_rod)); + else + { + Json::Value j_rod(rods[i]->serialize()); + j_rods.append(std::move(j_rod)); + } } node["rods"] = std::move(j_rods); diff --git a/src/reactor/reactor.hpp b/src/reactor/reactor.hpp index 6d14d2e..c824b40 100644 --- a/src/reactor/reactor.hpp +++ b/src/reactor/reactor.hpp @@ -2,6 +2,7 @@ #pragma once #include "rod.hpp" +#include "coolant/vessel.hpp" #include #include @@ -24,6 +25,7 @@ struct reactor int cursor; reactor(std::unique_ptr* rods, int width, int height, double cell_width, double cell_height); + reactor(const Json::Value& node, coolant::vessel* v); reactor(const reactor& r); reactor(reactor&& r); diff --git a/src/reactor/rod.cpp b/src/reactor/rod.cpp index a7e2b0e..03e1923 100644 --- a/src/reactor/rod.cpp +++ b/src/reactor/rod.cpp @@ -82,7 +82,20 @@ void rod::update_rod(double secs) } } -rod::operator Json::Value() const +rod::rod(const Json::Value& node) +{ + const Json::Value& j_vals = node["vals"]; + + selected = node["selected"].asBool(); + + for(int i = 0; i < VAL_N; i++) + { + vals[i] = j_vals[i][0].asDouble(); + vals_n[i] = j_vals[i][1].asDouble(); + } +} + +Json::Value rod::serialize() const { Json::Value node; Json::Value j_vals; diff --git a/src/reactor/rod.hpp b/src/reactor/rod.hpp index 3da06cb..608420f 100644 --- a/src/reactor/rod.hpp +++ b/src/reactor/rod.hpp @@ -25,6 +25,9 @@ public: N_FAST = 2, }; + constexpr rod() {}; + rod(const Json::Value& node); + virtual ~rod() {}; virtual void interact(rod* o, double secs); virtual void update(double secs) { } @@ -62,7 +65,7 @@ public: return o; } - operator Json::Value() const; + virtual Json::Value serialize() const; protected: diff --git a/src/system.cpp b/src/system.cpp index 9aba2e2..02712c4 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -55,28 +55,31 @@ system::system() freight_pump = std::make_unique(sink.get(), evaporator.get(), 1e5, 1, 1e4, 0.1, 10, coolant::pump::mode_t::DST, 1e6); } -system::system(system&& o) +system::system(const Json::Value& node) { - vessel = std::move(o.vessel); - reactor = std::move(o.reactor); - condenser = std::move(o.condenser); - turbine = std::move(o.turbine); + clock = node["clock"].asDouble(); - sink = std::move(o.sink); - condenser_secondary = std::move(o.condenser_secondary); - evaporator = std::move(o.evaporator); + vessel = std::make_unique(node["vessel"]); + reactor = std::make_unique(node["reactor"], vessel.get()); + condenser = std::make_unique(node["condenser"]); + turbine = std::make_unique(node["turbine"], condenser.get()); - turbine_bypass_valve = std::move(o.turbine_bypass_valve); - turbine_inlet_valve = std::move(o.turbine_inlet_valve); + evaporator = std::make_unique(node["evaporator"]); + sink = std::make_unique(evaporator->fluid, 11, 0, 0); + condenser_secondary = std::make_unique(condenser.get(), evaporator.get(), 1000); - primary_pump = std::move(o.primary_pump); - secondary_pump = std::move(o.secondary_pump); - freight_pump = std::move(o.freight_pump); + turbine_inlet_valve = std::make_unique(node["valve"]["turbine"]["inlet"], vessel.get(), turbine.get()); + turbine_bypass_valve = std::make_unique(node["valve"]["turbine"]["bypass"], vessel.get(), condenser.get()); + + primary_pump = std::make_unique(node["pump"]["primary"], condenser.get(), vessel.get()); + secondary_pump = std::make_unique(node["pump"]["secondary"], evaporator.get(), condenser_secondary.get()); + freight_pump = std::make_unique(node["pump"]["freight"], sink.get(), evaporator.get()); } void system::update(double dt) { dt *= speed; + clock += dt; turbine_inlet_valve->update(dt); turbine_bypass_valve->update(dt); @@ -100,20 +103,21 @@ system::operator Json::Value() const node["turbine"] = *turbine; node["condenser"] = *condenser; node["evaporator"] = *evaporator; - node["primary_pump"] = *primary_pump; - node["secondary_pump"] = *secondary_pump; - node["freight_pump"] = *freight_pump; - node["turbine_inlet_valve"] = *turbine_inlet_valve; - node["turbine_bypass_valve"] = *turbine_bypass_valve; - node["camera"] = graphics::camera::serialize(); + node["pump"]["primary"] = *primary_pump; + node["pump"]["secondary"] = *secondary_pump; + node["pump"]["freight"] = *freight_pump; + node["valve"]["turbine"]["inlet"] = *turbine_inlet_valve; + node["valve"]["turbine"]["bypass"] = *turbine_bypass_valve; node["reactor"] = *reactor; + node["clock"] = clock; return node; } void system::save() { - Json::Value root(*this); + Json::Value root(active); + root["camera"] = graphics::camera::serialize(); Json::StreamWriterBuilder builder; builder["commentStyle"] = "None"; @@ -125,3 +129,15 @@ void system::save() savefile.close(); } +void system::load() +{ + Json::Value root; + std::ifstream savefile("savefile.json"); + savefile >> root; + savefile.close(); + + system sys(root); + graphics::camera::load(root["camera"]); + active = std::move(sys); +} + diff --git a/src/system.hpp b/src/system.hpp index 94be2e1..06421d3 100644 --- a/src/system.hpp +++ b/src/system.hpp @@ -40,13 +40,15 @@ struct system sim::graphics::mesh scene; double speed = 1; + double clock = 0; system(); - system(system&& o); - system(const system& o) = delete; + system(const Json::Value& node); void update(double dt); - void save(); + + static void save(); + static void load(); operator Json::Value() const; }; diff --git a/src/util/pid.cpp b/src/util/pid.cpp index 5cb6a4d..1f8adf6 100644 --- a/src/util/pid.cpp +++ b/src/util/pid.cpp @@ -27,6 +27,17 @@ using namespace std; using namespace sim::util; +PID::PID(const Json::Value& node) : + _max(node["max"].asDouble()), + _min(node["min"].asDouble()), + _Kp(node["Kp"].asDouble()), + _Kd(node["Kd"].asDouble()), + _Ki(node["Ki"].asDouble()), + _pre_error(node["pre_error"].asDouble()), + _integral(node["integral"].asDouble()) +{ + +} PID::operator Json::Value() const { diff --git a/src/util/pid.hpp b/src/util/pid.hpp index d7a405e..20ae70b 100644 --- a/src/util/pid.hpp +++ b/src/util/pid.hpp @@ -44,6 +44,7 @@ class PID // pv - current process value double calculate( double dt, double sp, double pv ); + PID(const Json::Value& node); operator Json::Value() const; private: