From 451fc9e647ff28e131a78254fd7b9151e1de62d1 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Sat, 13 Jan 2024 17:33:53 +1100 Subject: [PATCH] add reactor --- CMakeLists.txt | 2 +- src/fuel_rod.cpp | 5 +++ src/fuel_rod.hpp | 5 +++ src/main.cpp | 12 +++---- src/reactor.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ src/reactor.hpp | 43 ++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 src/reactor.cpp create mode 100644 src/reactor.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f726ec5..f1fad78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/fuel_rod.cpp b/src/fuel_rod.cpp index db259a7..b1ed5be 100644 --- a/src/fuel_rod.cpp +++ b/src/fuel_rod.cpp @@ -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; diff --git a/src/fuel_rod.hpp b/src/fuel_rod.hpp index e48a853..e70ecf6 100644 --- a/src/fuel_rod.hpp +++ b/src/fuel_rod.hpp @@ -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); diff --git a/src/main.cpp b/src/main.cpp index adefa5e..0c49a35 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ -#include "fuel_rod.hpp" +#include "reactor.hpp" #include @@ -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; diff --git a/src/reactor.cpp b/src/reactor.cpp new file mode 100644 index 0000000..defff28 --- /dev/null +++ b/src/reactor.cpp @@ -0,0 +1,85 @@ + +#include "reactor.hpp" + +#include + +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"; + } +} + diff --git a/src/reactor.hpp b/src/reactor.hpp new file mode 100644 index 0000000..a1998e6 --- /dev/null +++ b/src/reactor.hpp @@ -0,0 +1,43 @@ + +#pragma once + +#include "fuel_rod.hpp" + +#include + +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 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; + } +}; + +} +