diff --git a/src/main.cpp b/src/main.cpp index bf38495..8a26f07 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ -#include "reactor/reactor.hpp" +#include "reactor/builder.hpp" #include "reactor/control/control_rod.hpp" #include "reactor/fuel/fuel_rod.hpp" #include "reactor/coolant/pipe.hpp" @@ -18,19 +18,20 @@ int main() nodelay(stdscr, TRUE); curs_set(0); - sim::reactor::reactor<2, 2> reactor({ - new sim::reactor::fuel::fuel_rod(100, 400), new sim::reactor::fuel::fuel_rod(100, 400), - new sim::reactor::control::control_rod(1000), new sim::reactor::coolant::pipe() - }); - - ((sim::reactor::control::control_rod*)reactor.rods[0][1])->set_reactivity(0.99); + sim::reactor::reactor<5, 5> reactor = sim::reactor::builder<5, 5>( + sim::reactor::fuel::fuel_rod(100, 400), + sim::reactor::control::control_rod(1000), + sim::reactor::coolant::pipe(), { + " P ", + " FCF ", + "PCPCP", + " FCF ", + " P " + }); for(;;) { - for(int i = 0; i < 1e3; i++) - { - reactor.update(1e-3); - } + reactor.update(1); erase(); display::draw_text(1, 0, "Reactor Core:"); diff --git a/src/reactor/builder.hpp b/src/reactor/builder.hpp new file mode 100644 index 0000000..64f1e6a --- /dev/null +++ b/src/reactor/builder.hpp @@ -0,0 +1,46 @@ + +#pragma once + +#include "rod.hpp" +#include "fuel/fuel_rod.hpp" +#include "control/control_rod.hpp" +#include "coolant/pipe.hpp" +#include "reactor.hpp" + +namespace sim::reactor +{ + +template +reactor builder(fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, std::array lines) +{ + std::array arr; + + for(int y = 0; y < H; y++) + for(int x = 0; x < W; x++) + { + char c = lines[y][x]; + rod* r; + + switch(c) + { + case 'F': + r = new fuel::fuel_rod(fr); + break; + case 'C': + r = new control::control_rod(cr); + break; + case 'P': + r = new coolant::pipe(p); + break; + default: + r = new rod(); + } + + arr[y * W + x] = r; + } + + return reactor(arr); +} + +}; + diff --git a/src/reactor/control/control_rod.cpp b/src/reactor/control/control_rod.cpp index 774944a..3562009 100644 --- a/src/reactor/control/control_rod.cpp +++ b/src/reactor/control/control_rod.cpp @@ -8,11 +8,6 @@ control_rod::control_rod(double limit) this->limit = limit; } -const char* control_rod::get_name() const -{ - return "Control Rod"; -} - void control_rod::display(std::ostream& o) const { o << "Inserted: " << (inserted * 100) << "%\n"; diff --git a/src/reactor/control/control_rod.hpp b/src/reactor/control/control_rod.hpp index b1b5c68..72f8d67 100644 --- a/src/reactor/control/control_rod.hpp +++ b/src/reactor/control/control_rod.hpp @@ -12,10 +12,12 @@ class control_rod : public sim::reactor::rod double absorbed = 0; double limit; - virtual const char* get_name() const; virtual double get_k(sim::reactor::rod::val_t type) const; virtual void display(std::ostream& o) const; + virtual const char* get_name() const { return "Control Rod"; } + virtual bool should_display() const { return true; } + public: control_rod(double limit); diff --git a/src/reactor/coolant/pipe.cpp b/src/reactor/coolant/pipe.cpp index 0e46ea5..49f5c55 100644 --- a/src/reactor/coolant/pipe.cpp +++ b/src/reactor/coolant/pipe.cpp @@ -3,11 +3,6 @@ using namespace sim::reactor::coolant; -const char* pipe::get_name() const -{ - return "Coolant Pipe"; -} - double pipe::get_k(val_t type) const { switch(type) diff --git a/src/reactor/coolant/pipe.hpp b/src/reactor/coolant/pipe.hpp index 6530366..13405a5 100644 --- a/src/reactor/coolant/pipe.hpp +++ b/src/reactor/coolant/pipe.hpp @@ -8,8 +8,10 @@ namespace sim::reactor::coolant class pipe : public sim::reactor::rod { - virtual const char* get_name() const; virtual double get_k(sim::reactor::rod::val_t type) const; + + virtual const char* get_name() const { return "Coolant Pipe"; } + virtual bool should_display() const { return true; } public: diff --git a/src/reactor/fuel/fuel_rod.cpp b/src/reactor/fuel/fuel_rod.cpp index e366526..b476fc8 100644 --- a/src/reactor/fuel/fuel_rod.cpp +++ b/src/reactor/fuel/fuel_rod.cpp @@ -14,11 +14,6 @@ void fuel_rod::display(std::ostream& o) const o << "Energy: " << s.get_energy() << "\n"; } -const char* fuel_rod::get_name() const -{ - return "Fuel"; -} - double fuel_rod::get_k(val_t type) const { switch(type) diff --git a/src/reactor/fuel/fuel_rod.hpp b/src/reactor/fuel/fuel_rod.hpp index f1b2677..5f1d5b3 100644 --- a/src/reactor/fuel/fuel_rod.hpp +++ b/src/reactor/fuel/fuel_rod.hpp @@ -11,10 +11,12 @@ class fuel_rod : public sim::reactor::rod { sample s; - virtual const char* get_name() const; virtual double get_k(sim::reactor::rod::val_t type) const; virtual void display(std::ostream& o) const; + virtual const char* get_name() const { return "Fuel"; } + virtual bool should_display() const { return true; } + public: fuel_rod(double fuel, double mass); diff --git a/src/reactor/reactor.hpp b/src/reactor/reactor.hpp index 52d585a..a5cafc0 100644 --- a/src/reactor/reactor.hpp +++ b/src/reactor/reactor.hpp @@ -30,20 +30,20 @@ struct reactor // do interactions for(int x = 1; x < W; x++) { - rods[x][0]->interact(rods[x - 1][0]); + rods[x][0]->interact(rods[x - 1][0], secs); } for(int y = 1; y < H; y++) { - rods[0][y]->interact(rods[0][y - 1]); + rods[0][y]->interact(rods[0][y - 1], secs); } for(int y = 1; y < H; y++) for(int x = 1; x < W; x++) { rod* r = rods[x][y]; - r->interact(rods[x - 1][y]); - r->interact(rods[x][y - 1]); + r->interact(rods[x - 1][y], secs); + r->interact(rods[x][y - 1], secs); } // do updates diff --git a/src/reactor/rod.cpp b/src/reactor/rod.cpp index a45bb7e..b8390f5 100644 --- a/src/reactor/rod.cpp +++ b/src/reactor/rod.cpp @@ -20,12 +20,12 @@ double rod::extract(val_t type, double k, double o) return v; } -void rod::interact(rod* o) +void rod::interact(rod* o, double secs) { for(int i = 0; i < rod::VAL_N; i++) { val_t v = (val_t)i; - add(v, o->extract(v, get_k(v), get(v))); + add(v, o->extract(v, secs * get_k(v), get(v))); } } @@ -39,8 +39,3 @@ void rod::update_rod() } } -void rod::display(std::ostream& o) const -{ - -} - diff --git a/src/reactor/rod.hpp b/src/reactor/rod.hpp index d45152f..9da2df4 100644 --- a/src/reactor/rod.hpp +++ b/src/reactor/rod.hpp @@ -19,14 +19,16 @@ public: N_FAST = 2 }; - virtual void interact(rod* o); - virtual void update(double secs) = 0; + virtual void interact(rod* o, double secs); + virtual void update(double secs) { }; virtual void add(val_t type, double v); virtual double extract(val_t type, double k, double o); virtual double get(val_t type) const; friend std::ostream& operator<<(std::ostream& o, const rod& r) { + if(!r.should_display()) return o; + o << "Name: " << r.get_name() << "\n"; r.display(o); o << "Heat: " << r.get(val_t::HEAT) << "\n"; @@ -41,9 +43,10 @@ protected: double vals_in[VAL_N] = {0}; double vals[VAL_N] = {0}; - virtual void display(std::ostream& o) const; - virtual double get_k(val_t type) const = 0; - virtual const char* get_name() const = 0; + virtual void display(std::ostream& o) const { }; + virtual double get_k(val_t type) const { return 0; } + virtual const char* get_name() const { return "Empty"; } + virtual bool should_display() const { return false; } void update_rod(); };