Some code cleanup

This commit is contained in:
Joshua Coles 2023-02-20 18:03:15 +00:00
parent 5f0f5c3820
commit 39231f0d92
4 changed files with 44 additions and 48 deletions

View File

@ -31,32 +31,6 @@ void DLASystem::clearParticles() {
particleList.clear(); particleList.clear();
} }
// remove any existing particles and setup initial condition
void DLASystem::Reset() {
// stop running
running = false;
frame = 0;
clearParticles();
lastParticleIsActive = 0;
// set the grid to zero
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
grid[i][j] = 0;
}
}
// setup initial condition and parameters
addCircle = 10;
killCircle = 2.0 * addCircle;
clusterRadius = 0.0;
// add a single particle at the origin
double pos[] = {0.0, 0.0};
addParticle(pos);
}
// set the value of a grid cell for a particular position // set the value of a grid cell for a particular position
// note the position has the initial particle at (0,0) // note the position has the initial particle at (0,0)
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ] // but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
@ -194,23 +168,46 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem(Config config) DLASystem::DLASystem(Config config)
: stickProbability(config.stickProbability), csv_out(std::move(config.csv)), endNum(config.maxParticles) { : stickProbability(config.stickProbability),
cout << "creating system, gridSize " << gridSize << endl; csv_out(std::move(config.csv)),
endNum(config.maxParticles),
frame(0),
running(false),
lastParticleIsActive(false) {
cout << "GridSize: " << gridSize << endl;
rgen.setSeed(config.seed); rgen.setSeed(config.seed);
// allocate memory for the grid, remember to free the memory in destructor /*
* Handle grid data structure.
* */
grid = new int *[gridSize]; grid = new int *[gridSize];
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize]; grid[i] = new int[gridSize];
} }
// reset initial parameters // set the grid to zero
Reset(); for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
grid[i][j] = 0;
}
}
// Add particle to the centre of the grid to start
double pos[] = {0.0, 0.0};
addParticle(pos);
// Make sure to include the central location in our csv.
this->csv_out << 0 << "," << 0 << "," << 0 << std::endl; this->csv_out << 0 << "," << 0 << "," << 0 << std::endl;
/*
* System behaviour parameters
* */
addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius 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 killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle
addCircle = 10;
killCircle = 2.0 * addCircle;
clusterRadius = 0.0;
} }
// destructor // destructor
@ -222,9 +219,8 @@ DLASystem::~DLASystem() {
delete[] grid[i]; delete[] grid[i];
delete[] grid; delete[] grid;
csv_out.flush();
if (csv_out.is_open()) { if (csv_out.is_open()) {
csv_out.flush();
csv_out.close(); csv_out.close();
} }
} }

View File

@ -80,9 +80,6 @@ public:
// destructor // destructor
~DLASystem(); ~DLASystem();
// delete all particles and reset
void Reset();
// this sets the seed for the random numbers // this sets the seed for the random numbers
void setSeed(int s) { rgen.setSeed(s); } void setSeed(int s) { rgen.setSeed(s); }

View File

@ -14,7 +14,7 @@ CXXFLAGS = -Wall -Wextra -g -O0 -std=c++20 -stdlib=libc++
IFLAGS = -I/usr/local/include -I/usr/include IFLAGS = -I/usr/local/include -I/usr/include
LFLAGS = -L/usr/local/lib -lm -framework OpenGL -framework GLUT LFLAGS = -L/usr/local/lib -lm
# ------------------------------------------ # ------------------------------------------
# FOR GENERIC MAKEFILE: # FOR GENERIC MAKEFILE:

View File

@ -1,7 +1,6 @@
#include <iostream> #include <iostream>
#include <stdio.h>
#include <vector> #include <vector>
#include <math.h> #include <cmath>
#include <string> #include <string>
#include "DLASystem.h" #include "DLASystem.h"
@ -9,9 +8,9 @@
using std::cout; using std::cout;
using std::endl; using std::endl;
// this is a global pointer, which is how we access the system itself /*
DLASystem *sys; * In a proper project I would write a better argument parser, don't care here, just exit with an error if it is wrong.
* */
Config::Config(int argc, char **argv) { Config::Config(int argc, char **argv) {
if (argc != 4) { if (argc != 4) {
exit(1); exit(1);
@ -30,26 +29,30 @@ Config::Config(int argc, char **argv) {
std::ofstream csv_out(str.str()); std::ofstream csv_out(str.str());
this->csv = std::move(csv_out); this->csv = std::move(csv_out);
// Add headers to csv output
this->csv << "frame" << "," << "x" << "," << "y" << std::endl; this->csv << "frame" << "," << "x" << "," << "y" << std::endl;
cout <<
"seed: " << seed << ", " <<
"stickProbability: " << stickProbability << ", " <<
"maxParticles: " << maxParticles <<
endl;
} }
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
Config config(argc, argv); Config config(argc, argv);
cout << "Seed: " << config.seed << ", Stick P: " << config.stickProbability << endl;
// create the system // Create the system
sys = new DLASystem(std::move(config)); auto *sys = new DLASystem(std::move(config));
sys->setRunning(); sys->setRunning();
/* /*
* NOTE: We run at max speed as rendering is handled by a different engine so we simply want to hjand * NOTE: We run at max speed as rendering is handled by a different engine so we don't need to care.
* */ * */
while (sys->running) { while (sys->running) {
sys->Update(); sys->Update();
} }
cout << "hey" << endl;
return 0; return 0;
} }