From e47864752ac38971684136046faf2858744e695d Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Mon, 20 Feb 2023 09:36:47 +0000 Subject: [PATCH] A config object --- DLASystem.cpp | 9 +++------ DLASystem.h | 11 ++++++++++- mainDLA.cpp | 36 +++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/DLASystem.cpp b/DLASystem.cpp index df1d482..41a90a0 100644 --- a/DLASystem.cpp +++ b/DLASystem.cpp @@ -192,10 +192,10 @@ int DLASystem::checkStick() { // constructor -DLASystem::DLASystem(std::ofstream csv_out, double stickProbability) - : stickProbability(stickProbability), csv_out(std::move(csv_out)) { +DLASystem::DLASystem(Config config) + : stickProbability(config.stickProbability), csv_out(std::move(config.csv)), endNum(config.maxParticles) { cout << "creating system, gridSize " << gridSize << endl; - endNum = 1000; + rgen.setSeed(config.seed); // allocate memory for the grid, remember to free the memory in destructor grid = new int *[gridSize]; @@ -210,9 +210,6 @@ DLASystem::DLASystem(std::ofstream csv_out, double stickProbability) addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle - - // this opens a logfile, if we want to... - //logfile.open("opfile.txt"); } // destructor diff --git a/DLASystem.h b/DLASystem.h index 22b1421..e73f8d6 100644 --- a/DLASystem.h +++ b/DLASystem.h @@ -15,6 +15,15 @@ #include "Particle.h" #include "rnd.h" +class Config { +public: + int seed; + double stickProbability; + std::ofstream csv; + int maxParticles; + Config(int argc, char **argv); +}; + class DLASystem { private: // these are private variables and functions that the user will not see @@ -66,7 +75,7 @@ public: int lastParticleIsActive; // constructor - DLASystem(std::ofstream csv_out, double stickProbability); + DLASystem(Config config); // destructor ~DLASystem(); diff --git a/mainDLA.cpp b/mainDLA.cpp index 19d8d4a..aaedfa2 100644 --- a/mainDLA.cpp +++ b/mainDLA.cpp @@ -12,26 +12,32 @@ using std::endl; // this is a global pointer, which is how we access the system itself DLASystem *sys; -int main(int argc, char **argv) { - int seed; - double stickProbability = 0.5; - - if (argc != 3) { - return 1; +Config::Config(int argc, char **argv) { + if (argc != 4) { + exit(1); } else { - seed = std::stoi(argv[1]); - stickProbability = std::stod(argv[2]); + this->seed = std::stoi(argv[1]); + this->stickProbability = std::stod(argv[2]); + this->maxParticles = std::stoi(argv[3]); + + if (stickProbability <= 0 || stickProbability > 1) { + exit(1); + } + + std::stringstream str; + // Ensure the output file name contains all information required to replicate data + str << "./out-" << seed << '-' << stickProbability << "-" << maxParticles << ".csv"; + std::ofstream csv_out(str.str()); + this->csv = std::move(csv_out); } +} - cout << "Seed: " << seed << ", Stick P: " << stickProbability << endl; - - std::stringstream str; - str << "./out-" << seed << '-' << stickProbability << ".csv"; - std::ofstream csv_out(str.str()); +int main(int argc, char **argv) { + Config config(argc, argv); + cout << "Seed: " << config.seed << ", Stick P: " << config.stickProbability << endl; // create the system - sys = new DLASystem(std::move(csv_out), stickProbability); - sys->setSeed(seed); + sys = new DLASystem(std::move(config)); sys->setRunning(); /*