added reactor builder

This commit is contained in:
Jay Robson 2024-01-14 17:50:43 +11:00
parent d45bfe310a
commit be0d6cfacb
11 changed files with 81 additions and 45 deletions

View File

@ -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::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 "
});
((sim::reactor::control::control_rod*)reactor.rods[0][1])->set_reactivity(0.99);
for(;;)
{
for(int i = 0; i < 1e3; i++)
{
reactor.update(1e-3);
}
reactor.update(1);
erase();
display::draw_text(1, 0, "Reactor Core:");

46
src/reactor/builder.hpp Normal file
View File

@ -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 <int W, int H>
reactor<W, H> builder(fuel::fuel_rod fr, control::control_rod cr, coolant::pipe p, std::array<const char*, H> lines)
{
std::array<rod*, W * H> 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<W, H>(arr);
}
};

View File

@ -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";

View File

@ -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);

View File

@ -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)

View File

@ -8,9 +8,11 @@ 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:
virtual void update(double secs);

View File

@ -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)

View File

@ -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);

View File

@ -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

View File

@ -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
{
}

View File

@ -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();
};