Add CSV export basic csv export code

This commit is contained in:
Joshua Coles 2023-03-01 21:19:47 +00:00
parent 9cd43ad5b6
commit ebb4959d82
4 changed files with 33 additions and 17 deletions

View File

@ -72,7 +72,7 @@ int DLASystem::readGrid(double pos[]) {
int DLASystem::checkStop() { int DLASystem::checkStop() {
if (killCircle + 2 >= gridSize / 2) { if (killCircle + 2 >= gridSize / 2) {
this->running = false; this->running = false;
cout << "STOP" << endl; cerr << "STOP" << endl;
return 1; return 1;
} else return 0; } else return 0;
} }
@ -100,7 +100,7 @@ void DLASystem::addParticleOnAddCircle() {
if (readGrid(pos) == 0) if (readGrid(pos) == 0)
addParticle(pos); addParticle(pos);
else 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 // send back the position of a neighbour of a given grid cell
@ -158,7 +158,7 @@ void DLASystem::moveLastParticle() {
setPosNeighbour(newpos, lastP->pos, rr); setPosNeighbour(newpos, lastP->pos, rr);
if (distanceFromOrigin(newpos) > killCircle) { if (distanceFromOrigin(newpos) > killCircle) {
//cout << "#deleting particle" << endl; //cerr << "#deleting particle" << endl;
setGrid(lastP->pos, 0); setGrid(lastP->pos, 0);
particleList.pop_back(); // remove particle from particleList particleList.pop_back(); // remove particle from particleList
numParticles--; numParticles--;
@ -174,16 +174,16 @@ void DLASystem::moveLastParticle() {
// check if we stick // check if we stick
if (checkStick()) { if (checkStick()) {
//cout << "stick" << endl; //cerr << "stick" << endl;
setParticleInactive(); // make the particle inactive (stuck) setParticleInactive(); // make the particle inactive (stuck)
updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc. updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc.
} }
} else { } else {
// if we get to here then we are trying to move to an occupied site // 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) // (this should never happen as long as the sticking probability is 1.0)
cout << "reject " << rr << endl; cerr << "reject " << rr << endl;
cout << lastP->pos[0] << " " << lastP->pos[1] << endl; cerr << lastP->pos[0] << " " << lastP->pos[1] << endl;
//cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl; //cerr << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl;
//printOccupied(); //printOccupied();
} }
} }
@ -205,10 +205,10 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem() { DLASystem::DLASystem(int maxParticles, string csvPath) {
cout << "creating system, gridSize " << gridSize << endl; cerr << "creating system, gridSize " << gridSize << endl;
numParticles = 0; numParticles = 0;
endNum = 1000; endNum = maxParticles;
// allocate memory for the grid, remember to free the memory in destructor // allocate memory for the grid, remember to free the memory in destructor
grid = new int *[gridSize]; grid = new int *[gridSize];
@ -222,18 +222,30 @@ DLASystem::DLASystem() {
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
// this opens a logfile, if we want to... csv.open(csvPath);
//logfile.open("opfile.txt");
} }
// destructor // destructor
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// strictly we should not print inside the destructor but never mind... // strictly we should not print inside the destructor but never mind...
cout << "deleting system" << endl; cerr << "deleting system" << endl;
// delete the particles // delete the particles
clearParticles(); clearParticles();
// delete the grid // delete the grid
for (int i = 0; i < gridSize; i++) for (int i = 0; i < gridSize; i++)
delete[] grid[i]; delete[] grid[i];
delete[] grid; 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;
}
} }

View File

@ -66,7 +66,7 @@ class DLASystem {
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
explicit DLASystem(); explicit DLASystem(int maxParticles, string csvPath);
// destructor // destructor
~DLASystem(); ~DLASystem();
@ -112,4 +112,6 @@ class DLASystem {
// check whether the last particle should stick // check whether the last particle should stick
// currently it sticks whenever it touches another particle // currently it sticks whenever it touches another particle
int checkStick(); int checkStick();
void exportData();
}; };

View File

@ -10,7 +10,7 @@
CXX = clang++ CXX = clang++
CXXFLAGS = -Wall -Wextra -g -O0 CXXFLAGS = -Wall -Wextra -g -O0 --std=c++20
IFLAGS = -I/usr/local/include -I/usr/include IFLAGS = -I/usr/local/include -I/usr/include

View File

@ -4,12 +4,12 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
// create the system // create the system
DLASystem* sys = new DLASystem(); auto *sys = new DLASystem(1000, std::string("./out.csv"));
// this is the seed for the random numbers // this is the seed for the random numbers
int seed = 6; int seed = 6;
std::cout << "Setting seed " << seed << endl; std::cerr << "Setting seed " << seed << endl;
sys->setSeed(seed); sys->setSeed(seed);
sys->running = true; sys->running = true;
@ -17,5 +17,7 @@ int main(int argc, char **argv) {
sys->update(); sys->update();
} }
sys->exportData();
return 0; return 0;
} }