Add CSV output

This commit is contained in:
Joshua Coles 2023-02-19 18:17:22 +00:00
parent d69402e32e
commit 863fd61d28
3 changed files with 37 additions and 44 deletions

View File

@ -4,10 +4,15 @@
#include "DLASystem.h" #include "DLASystem.h"
using std::cout;
using std::endl;
// this function gets called every step, // this function gets called every step,
// if there is an active particle then it gets moved, // if there is an active particle then it gets moved,
// if not then add a particle // if not then add a particle
void DLASystem::Update() { void DLASystem::Update() {
this->frame++;
if (lastParticleIsActive == 1) { if (lastParticleIsActive == 1) {
moveLastParticle(); moveLastParticle();
} else if (this->particleList.size() < (size_t) endNum) { } else if (this->particleList.size() < (size_t) endNum) {
@ -30,6 +35,7 @@ void DLASystem::clearParticles() {
void DLASystem::Reset() { void DLASystem::Reset() {
// stop running // stop running
running = false; running = false;
frame = 0;
clearParticles(); clearParticles();
@ -65,16 +71,6 @@ int DLASystem::readGrid(double pos[]) {
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)]; 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 // add a particle to the system at a specific position
void DLASystem::addParticle(double pos[]) { void DLASystem::addParticle(double pos[]) {
// create a new particle // create a new particle
@ -94,11 +90,12 @@ void DLASystem::addParticleOnAddCircle() {
double theta = rgen.random01() * 2 * M_PI; double theta = rgen.random01() * 2 * M_PI;
pos[0] = ceil(addCircle * cos(theta)); pos[0] = ceil(addCircle * cos(theta));
pos[1] = ceil(addCircle * sin(theta)); pos[1] = ceil(addCircle * sin(theta));
if (readGrid(pos) == 0) if (readGrid(pos) == 0) {
addParticle(pos); addParticle(pos);
else } else {
cout << "FAIL " << pos[0] << " " << pos[1] << endl; cout << "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
// NOTE: there is no check that the neighbour is inside the grid, // NOTE: there is no check that the neighbour is inside the grid,
@ -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 // when we add a particle to the cluster, we should update the cluster radius
// and the sizes of the addCircle and the killCircle // and the sizes of the addCircle and the killCircle
void DLASystem::updateClusterRadius(double pos[]) { void DLASystem::updateClusterRadius(double pos[]) {
double rr = distanceFromOrigin(pos); double newRadius = distanceFromOrigin(pos);
if (rr > clusterRadius) { if (newRadius > clusterRadius) {
clusterRadius = rr; clusterRadius = newRadius;
// this is how big addCircle is supposed to be: // this is how big addCircle is supposed to be:
// either 20% more than cluster radius, or at least 5 bigger. // either 20% more than cluster radius, or at least 5 bigger.
double check = clusterRadius * addRatio; double check = clusterRadius * addRatio;
if (check < clusterRadius + 5) if (check < clusterRadius + 5) {
check = clusterRadius + 5; check = clusterRadius + 5;
}
// if it is smaller then update everything... // if it is smaller then update everything...
if (addCircle < check) { if (addCircle < check) {
addCircle = check; addCircle = check;
killCircle = killRatio * addCircle; killCircle = killRatio * addCircle;
} }
checkStop();
if (killCircle + 2 >= gridSize / 2) {
this->running = false;
}
} }
} }
@ -154,13 +156,10 @@ 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;
setGrid(lastP->pos, 0); setGrid(lastP->pos, 0);
particleList.pop_back(); // remove particle from particleList particleList.pop_back(); // remove particle from particleList
setParticleInactive(); setParticleInactive();
} } else if (readGrid(newpos) == 0) {
// check if destination is empty
else if (readGrid(newpos) == 0) {
setGrid(lastP->pos, 0); // set the old grid site to empty setGrid(lastP->pos, 0); // set the old grid site to empty
// update the position // update the position
particleList[this->particleList.size() - 1]->pos[0] = newpos[0]; particleList[this->particleList.size() - 1]->pos[0] = newpos[0];
@ -169,22 +168,15 @@ void DLASystem::moveLastParticle() {
// check if we stick // check if we stick
if (checkStick()) { if (checkStick()) {
cout << lastP->pos[0] << "," << lastP->pos[1] << endl; this->csv_out << frame << "," << lastP->pos[0] << "," << lastP->pos[1] << endl;
//cout << "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.
if (this->particleList.size() % 100 == 0 && logfile.is_open()) {
logfile << this->particleList.size() << " " << clusterRadius << endl;
}
} }
} 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; cout << "reject " << rr << endl;
cout << lastP->pos[0] << " " << lastP->pos[1] << 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 // constructor
DLASystem::DLASystem() { DLASystem::DLASystem(std::ofstream csv_out) {
cout << "creating system, gridSize " << gridSize << endl; cout << "creating system, gridSize " << gridSize << endl;
endNum = 1000; endNum = 1000;
this->csv_out = std::move(csv_out);
// 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];
@ -226,8 +219,6 @@ DLASystem::DLASystem() {
// destructor // destructor
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// strictly we should not print inside the destructor but never mind...
cout << "deleting system" << endl;
// delete the particles // delete the particles
clearParticles(); clearParticles();
// delete the grid // delete the grid
@ -235,6 +226,7 @@ DLASystem::~DLASystem() {
delete[] grid[i]; delete[] grid[i];
delete[] grid; delete[] grid;
if (logfile.is_open()) if (csv_out.is_open()) {
logfile.close(); csv_out.close();
}
} }

View File

@ -15,14 +15,11 @@
#include "Particle.h" #include "Particle.h"
#include "rnd.h" #include "rnd.h"
using namespace std;
class DLASystem { class DLASystem {
private: private:
// these are private variables and functions that the user will not see // these are private variables and functions that the user will not see
// list of particles // list of particles
vector<Particle *> particleList; std::vector<Particle *> particleList;
// delete particles and clear the particle list // delete particles and clear the particle list
void clearParticles(); void clearParticles();
@ -41,7 +38,7 @@ private:
rnd rgen; rnd rgen;
// output file (not used at the moment) // output file (not used at the moment)
ofstream logfile; std::ofstream csv_out;
// number of particles at which the simulation will stop // number of particles at which the simulation will stop
// (the value is set in constructor) // (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 addRatio; // how much bigger the addCircle should be, compared to cluster radius
double killRatio; // how much bigger is the killCircle, compared to the addCircle double killRatio; // how much bigger is the killCircle, compared to the addCircle
int frame;
public: public:
// these are public variables and functions // these are public variables and functions
@ -65,7 +64,7 @@ public:
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
DLASystem(); DLASystem(std::ofstream csv_out);
// destructor // destructor
~DLASystem(); ~DLASystem();
@ -76,9 +75,6 @@ public:
// 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); }
// check whether we should stop (eg the cluster has reached the edge of the grid)
int checkStop();
void setRunning() { running = true; } void setRunning() { running = true; }
void pauseRunning() { running = false; } void pauseRunning() { running = false; }

View File

@ -6,12 +6,17 @@
#include "DLASystem.h" #include "DLASystem.h"
using std::cout;
using std::endl;
// this is a global pointer, which is how we access the system itself // this is a global pointer, which is how we access the system itself
DLASystem *sys; DLASystem *sys;
int main(int argc, char **argv) { int main(int argc, char **argv) {
std::ofstream csv_out("./out.csv");
// create the system // create the system
sys = new DLASystem(); sys = new DLASystem(std::move(csv_out));
// this is the seed for the random numbers // this is the seed for the random numbers
int seed = 6; int seed = 6;