add reactor

This commit is contained in:
Jay Robson 2024-01-13 17:33:53 +11:00
parent 0dead64fe9
commit 451fc9e647
6 changed files with 145 additions and 7 deletions

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.25)
project(FastNuclearSim VERSION 1.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-O3 -lncurses")
set(CMAKE_CXX_FLAGS "-g -lncurses")
file(GLOB_RECURSE SOURCES src/*.cpp)

View File

@ -36,6 +36,11 @@ double fuel_rod::extract_heat(double k, double o)
return v;
}
void fuel_rod::add_heat(double amount)
{
temperature += amount;
}
void fuel_rod::set_reactivity(double v)
{
reactivity = v;

View File

@ -20,9 +20,14 @@ class fuel_rod
public:
fuel_rod(double fuel, double mass);
fuel_rod() : fuel_rod(0, 0) {};
void update(double secs);
void set_reactivity(double amount);
void add_heat(double amount);
constexpr double get_temperature() const { return temperature; }
constexpr double get_reactivity() const { return reactivity; }
double extract_free_neutrons();
double extract_heat(double k, double o);

View File

@ -1,5 +1,5 @@
#include "fuel_rod.hpp"
#include "reactor.hpp"
#include <iostream>
@ -7,18 +7,18 @@
int main()
{
sim::fuel_rod fr(100, 200);
sim::reactor reactor(5, {100, 200});
fr.set_reactivity(0);
// react.set_reactivity(0);
for(;;)
{
for(int i = 0; i < 1e4; i++)
for(int i = 0; i < 1e3; i++)
{
fr.update(1e-4);
reactor.update(1e-3);
}
std::cout << "\nFuel Rod:\n\n" << fr << "\n\n";
std::cout << "\n\nShow:\n\n" << reactor << "\n";
}
return 0;

85
src/reactor.cpp Normal file
View File

@ -0,0 +1,85 @@
#include "reactor.hpp"
#include <iostream>
using namespace sim;
reactor::reactor(int size, fuel_rod fr)
{
this->size = size;
for(int y = 0; y < size; y++)
for(int x = 0; x < size; x++)
{
rods.push_back({fr, x, y});
}
}
static const double HEAT_K = 1.0/2.0;
void reactor::update(double secs)
{
update_count++;
static const int C[8][2] = {
{0, -1},
{0, 1},
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
{-1, 0},
{1, 0}
};
const int C_OFF = update_count % 4;
double heat_add[rods.size()] = {0};
for(rod_t& r : rods)
{
r.fr.update(secs);
for(int i = 0; i < 4; i++)
{
const auto [cx, cy] = C[C_OFF + i];
int id_o = get_id(r.x + cx, r.y + cy);
if(id_o == -1) continue;
rod_t& r_o = rods[id_o];
heat_add[id_o] += r.fr.extract_heat(HEAT_K, r_o.fr.get_temperature());
}
}
for(int i = 0; i < rods.size(); i++)
{
rods[i].fr.add_heat(heat_add[i]);
}
}
int reactor::get_id(int x, int y) const
{
if(x < 0 || y < 0 || x >= size || y >= size) return -1;
return y * size + x;
}
void reactor::display(std::ostream& o) const
{
for(int y = -1; y <= size; y++)
{
for(int x = -1; x <= size; x++)
{
int id = get_id(x, y);
o << "\t";
if(id == -1) continue;
o << rods[id].fr.get_temperature();
}
o << "\n";
}
}

43
src/reactor.hpp Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include "fuel_rod.hpp"
#include <vector>
namespace sim
{
class reactor
{
struct rod_t
{
fuel_rod fr;
int x, y;
rod_t(fuel_rod fr, int x, int y) : fr(fr) {}
};
std::vector<rod_t> rods;
int size;
long update_count = 0;
int get_id(int x, int y) const;
void display(std::ostream& o) const;
public:
reactor(int radius, fuel_rod fr);
void update(double secs);
friend std::ostream& operator<<(std::ostream& o, reactor& r)
{
r.display(o);
return o;
}
};
}