From 863fd61d289ad5a9f7686f4008317d6e69fd943e Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sun, 19 Feb 2023 18:17:22 +0000 Subject: [PATCH] Add CSV output --- DLASystem.cpp | 60 ++++++++++++++++++++++----------------------------- DLASystem.h | 14 +++++------- mainDLA.cpp | 7 +++++- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/DLASystem.cpp b/DLASystem.cpp index 0557310..490927f 100644 --- a/DLASystem.cpp +++ b/DLASystem.cpp @@ -4,10 +4,15 @@ #include "DLASystem.h" +using std::cout; +using std::endl; + // this function gets called every step, // if there is an active particle then it gets moved, // if not then add a particle void DLASystem::Update() { + this->frame++; + if (lastParticleIsActive == 1) { moveLastParticle(); } else if (this->particleList.size() < (size_t) endNum) { @@ -30,6 +35,7 @@ void DLASystem::clearParticles() { void DLASystem::Reset() { // stop running running = false; + frame = 0; clearParticles(); @@ -65,16 +71,6 @@ int DLASystem::readGrid(double pos[]) { return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)]; } -// check if the cluster is big enough and we should stop: -// to be safe, we need the killCircle to be at least 2 less than the edge of the grid -int DLASystem::checkStop() { - if (killCircle + 2 >= gridSize / 2) { - pauseRunning(); - cout << "STOP" << endl; - return 1; - } else return 0; -} - // add a particle to the system at a specific position void DLASystem::addParticle(double pos[]) { // create a new particle @@ -94,10 +90,11 @@ void DLASystem::addParticleOnAddCircle() { double theta = rgen.random01() * 2 * M_PI; pos[0] = ceil(addCircle * cos(theta)); pos[1] = ceil(addCircle * sin(theta)); - if (readGrid(pos) == 0) + if (readGrid(pos) == 0) { addParticle(pos); - else + } else { cout << "FAIL " << pos[0] << " " << pos[1] << endl; + } } // send back the position of a neighbour of a given grid cell @@ -127,20 +124,25 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) { // when we add a particle to the cluster, we should update the cluster radius // and the sizes of the addCircle and the killCircle void DLASystem::updateClusterRadius(double pos[]) { - double rr = distanceFromOrigin(pos); - if (rr > clusterRadius) { - clusterRadius = rr; + double newRadius = distanceFromOrigin(pos); + if (newRadius > clusterRadius) { + clusterRadius = newRadius; // this is how big addCircle is supposed to be: // either 20% more than cluster radius, or at least 5 bigger. double check = clusterRadius * addRatio; - if (check < clusterRadius + 5) + if (check < clusterRadius + 5) { check = clusterRadius + 5; + } + // if it is smaller then update everything... if (addCircle < check) { addCircle = check; killCircle = killRatio * addCircle; } - checkStop(); + + if (killCircle + 2 >= gridSize / 2) { + this->running = false; + } } } @@ -154,13 +156,10 @@ void DLASystem::moveLastParticle() { setPosNeighbour(newpos, lastP->pos, rr); if (distanceFromOrigin(newpos) > killCircle) { - //cout << "#deleting particle" << endl; setGrid(lastP->pos, 0); particleList.pop_back(); // remove particle from particleList setParticleInactive(); - } - // check if destination is empty - else if (readGrid(newpos) == 0) { + } else if (readGrid(newpos) == 0) { setGrid(lastP->pos, 0); // set the old grid site to empty // update the position particleList[this->particleList.size() - 1]->pos[0] = newpos[0]; @@ -169,22 +168,15 @@ void DLASystem::moveLastParticle() { // check if we stick if (checkStick()) { - cout << lastP->pos[0] << "," << lastP->pos[1] << endl; - //cout << "stick" << endl; + this->csv_out << frame << "," << lastP->pos[0] << "," << lastP->pos[1] << endl; setParticleInactive(); // make the particle inactive (stuck) updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc. - - if (this->particleList.size() % 100 == 0 && logfile.is_open()) { - logfile << this->particleList.size() << " " << clusterRadius << endl; - } } } else { // if we get to here then we are trying to move to an occupied site // (this should never happen as long as the sticking probability is 1.0) cout << "reject " << rr << endl; cout << lastP->pos[0] << " " << lastP->pos[1] << endl; - //cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl; - //printOccupied(); } } @@ -205,9 +197,10 @@ int DLASystem::checkStick() { // constructor -DLASystem::DLASystem() { +DLASystem::DLASystem(std::ofstream csv_out) { cout << "creating system, gridSize " << gridSize << endl; endNum = 1000; + this->csv_out = std::move(csv_out); // allocate memory for the grid, remember to free the memory in destructor grid = new int *[gridSize]; @@ -226,8 +219,6 @@ DLASystem::DLASystem() { // destructor DLASystem::~DLASystem() { - // strictly we should not print inside the destructor but never mind... - cout << "deleting system" << endl; // delete the particles clearParticles(); // delete the grid @@ -235,6 +226,7 @@ DLASystem::~DLASystem() { delete[] grid[i]; delete[] grid; - if (logfile.is_open()) - logfile.close(); + if (csv_out.is_open()) { + csv_out.close(); + } } diff --git a/DLASystem.h b/DLASystem.h index 9a3d57c..3fa3d56 100644 --- a/DLASystem.h +++ b/DLASystem.h @@ -15,14 +15,11 @@ #include "Particle.h" #include "rnd.h" -using namespace std; - - class DLASystem { private: // these are private variables and functions that the user will not see // list of particles - vector particleList; + std::vector particleList; // delete particles and clear the particle list void clearParticles(); @@ -41,7 +38,7 @@ private: rnd rgen; // output file (not used at the moment) - ofstream logfile; + std::ofstream csv_out; // number of particles at which the simulation will stop // (the value is set in constructor) @@ -51,6 +48,8 @@ private: double addRatio; // how much bigger the addCircle should be, compared to cluster radius double killRatio; // how much bigger is the killCircle, compared to the addCircle + int frame; + public: // these are public variables and functions @@ -65,7 +64,7 @@ public: int lastParticleIsActive; // constructor - DLASystem(); + DLASystem(std::ofstream csv_out); // destructor ~DLASystem(); @@ -76,9 +75,6 @@ public: // this sets the seed for the random numbers void setSeed(int s) { rgen.setSeed(s); } - // check whether we should stop (eg the cluster has reached the edge of the grid) - int checkStop(); - void setRunning() { running = true; } void pauseRunning() { running = false; } diff --git a/mainDLA.cpp b/mainDLA.cpp index 106b701..740580d 100644 --- a/mainDLA.cpp +++ b/mainDLA.cpp @@ -6,12 +6,17 @@ #include "DLASystem.h" +using std::cout; +using std::endl; + // this is a global pointer, which is how we access the system itself DLASystem *sys; int main(int argc, char **argv) { + std::ofstream csv_out("./out.csv"); + // create the system - sys = new DLASystem(); + sys = new DLASystem(std::move(csv_out)); // this is the seed for the random numbers int seed = 6;