From ebb4959d825441c6c52d8438586813629da03c12 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Wed, 1 Mar 2023 21:19:47 +0000 Subject: [PATCH] Add CSV export basic csv export code --- DLASystem.cpp | 38 +++++++++++++++++++++++++------------- DLASystem.h | 4 +++- Makefile | 2 +- mainDLA.cpp | 6 ++++-- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/DLASystem.cpp b/DLASystem.cpp index 3d22052..e006c10 100644 --- a/DLASystem.cpp +++ b/DLASystem.cpp @@ -72,7 +72,7 @@ int DLASystem::readGrid(double pos[]) { int DLASystem::checkStop() { if (killCircle + 2 >= gridSize / 2) { this->running = false; - cout << "STOP" << endl; + cerr << "STOP" << endl; return 1; } else return 0; } @@ -100,7 +100,7 @@ void DLASystem::addParticleOnAddCircle() { if (readGrid(pos) == 0) addParticle(pos); else - cout << "FAIL " << pos[0] << " " << pos[1] << endl; + cerr << "FAIL " << pos[0] << " " << pos[1] << endl; } // send back the position of a neighbour of a given grid cell @@ -158,7 +158,7 @@ void DLASystem::moveLastParticle() { setPosNeighbour(newpos, lastP->pos, rr); if (distanceFromOrigin(newpos) > killCircle) { - //cout << "#deleting particle" << endl; + //cerr << "#deleting particle" << endl; setGrid(lastP->pos, 0); particleList.pop_back(); // remove particle from particleList numParticles--; @@ -174,16 +174,16 @@ void DLASystem::moveLastParticle() { // check if we stick if (checkStick()) { - //cout << "stick" << endl; + //cerr << "stick" << endl; setParticleInactive(); // make the particle inactive (stuck) updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc. } } 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; + cerr << "reject " << rr << endl; + cerr << lastP->pos[0] << " " << lastP->pos[1] << endl; + //cerr << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl; //printOccupied(); } } @@ -205,10 +205,10 @@ int DLASystem::checkStick() { // constructor -DLASystem::DLASystem() { - cout << "creating system, gridSize " << gridSize << endl; +DLASystem::DLASystem(int maxParticles, string csvPath) { + cerr << "creating system, gridSize " << gridSize << endl; numParticles = 0; - endNum = 1000; + endNum = maxParticles; // allocate memory for the grid, remember to free the memory in destructor grid = new int *[gridSize]; @@ -222,18 +222,30 @@ DLASystem::DLASystem() { 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"); + csv.open(csvPath); } // destructor DLASystem::~DLASystem() { // strictly we should not print inside the destructor but never mind... - cout << "deleting system" << endl; + cerr << "deleting system" << endl; // delete the particles clearParticles(); // delete the grid for (int i = 0; i < gridSize; i++) delete[] grid[i]; delete[] grid; + + if (csv.is_open()) { + csv.flush(); + csv.close(); + } +} + +void DLASystem::exportData() { + csv << "x,y" << endl; + + for (auto particle: particleList) { + csv << particle->pos[0] << "," << particle->pos[1] << endl; + } } diff --git a/DLASystem.h b/DLASystem.h index 773a3e1..b305c92 100644 --- a/DLASystem.h +++ b/DLASystem.h @@ -66,7 +66,7 @@ class DLASystem { int lastParticleIsActive; // constructor - explicit DLASystem(); + explicit DLASystem(int maxParticles, string csvPath); // destructor ~DLASystem(); @@ -112,4 +112,6 @@ class DLASystem { // check whether the last particle should stick // currently it sticks whenever it touches another particle int checkStick(); + + void exportData(); }; diff --git a/Makefile b/Makefile index abcd365..49e5df1 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CXX = clang++ -CXXFLAGS = -Wall -Wextra -g -O0 +CXXFLAGS = -Wall -Wextra -g -O0 --std=c++20 IFLAGS = -I/usr/local/include -I/usr/include diff --git a/mainDLA.cpp b/mainDLA.cpp index 5dc34b1..2171f4a 100644 --- a/mainDLA.cpp +++ b/mainDLA.cpp @@ -4,12 +4,12 @@ int main(int argc, char **argv) { // create the system - DLASystem* sys = new DLASystem(); + auto *sys = new DLASystem(1000, std::string("./out.csv")); // this is the seed for the random numbers int seed = 6; - std::cout << "Setting seed " << seed << endl; + std::cerr << "Setting seed " << seed << endl; sys->setSeed(seed); sys->running = true; @@ -17,5 +17,7 @@ int main(int argc, char **argv) { sys->update(); } + sys->exportData(); + return 0; }