Compare commits

...
19 Commits
Author SHA1 Message Date
joshuacoles 56d8007d46 Make clang-tidy a little happier 2023-02-20 20:56:10 +00:00
joshuacoles 9f34807269 God I hate CPP 2023-02-20 20:48:16 +00:00
joshuacoles 7d37960439 God I hate CPP 2023-02-20 20:46:41 +00:00
joshuacoles 6afaf0c82d Move from C array style grid to vector 2023-02-20 20:06:44 +00:00
joshuacoles 345da9aa86 Rename update 2023-02-20 19:53:53 +00:00
joshuacoles 39231f0d92 Some code cleanup 2023-02-20 18:03:15 +00:00
joshuacoles 5f0f5c3820 Fix harness 2023-02-20 17:32:53 +00:00
joshuacoles f7bfd64616 Add run and csv to ignore 2023-02-20 17:32:47 +00:00
joshuacoles 820323b53d Add run and csv to ignore 2023-02-20 17:32:35 +00:00
joshuacoles 7041c6d485 Increase range of stickProbabilities 2023-02-20 10:00:16 +00:00
joshuacoles 3f6a8f0282 Swap out Deno harness for gnu parallel harness as we were not running to completion 2023-02-20 09:56:58 +00:00
joshuacoles c33a2a207f Add call harness to generate data 2023-02-20 09:39:20 +00:00
joshuacoles e47864752a A config object 2023-02-20 09:36:47 +00:00
joshuacoles f323248610 Add missing initial particle 2023-02-19 19:26:40 +00:00
joshuacoles 54d02253ef Add parametrisation for testing 2023-02-19 19:16:53 +00:00
joshuacoles dacfff3e96 Minor changes 2023-02-19 18:59:53 +00:00
joshuacoles c1b4b6bab1 Add stick probability 2023-02-19 18:22:12 +00:00
joshuacoles 863fd61d28 Add CSV output 2023-02-19 18:17:22 +00:00
joshuacoles d69402e32e Remove unneeded properties 2023-02-19 18:00:30 +00:00
8 changed files with 1162 additions and 222 deletions
+3
View File
@@ -110,3 +110,6 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
/run
*.jl
+71 -137
View File
@@ -4,13 +4,18 @@
#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 (numParticles < endNum) { } else if (this->particleList.size() < (size_t) endNum) {
addParticleOnAddCircle(); addParticleOnAddCircle();
setParticleActive(); setParticleActive();
} else { } else {
@@ -18,76 +23,24 @@ void DLASystem::Update() {
} }
} }
void DLASystem::clearParticles() {
// delete particles and the particle list
for (int i = 0; i < numParticles; i++) {
delete particleList[i];
}
particleList.clear();
numParticles = 0;
}
// remove any existing particles and setup initial condition
void DLASystem::Reset() {
// stop running
running = 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 view
int InitialViewSize = 40;
setViewSize(InitialViewSize);
}
// 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 ]
void DLASystem::setGrid(double pos[], int val) { void DLASystem::setGrid(std::array<double, 2> pos, int val) {
int halfGrid = gridSize / 2; *index_grid(pos) = val;
grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)] = val;
} }
// read the grid cell for a given position // read the grid cell for a given position
int DLASystem::readGrid(double pos[]) { int DLASystem::readGrid(std::array<double, 2> pos) {
int halfGrid = gridSize / 2; return *index_grid(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 // add a particle to the system at a specific position
void DLASystem::addParticle(double pos[]) { void DLASystem::addParticle(std::array<double, 2> pos) {
// create a new particle // create a new particle
Particle *p = new Particle(pos); auto *p = new Particle(pos);
// push_back means "add this to the end of the list" // push_back means "add this to the end of the list"
particleList.push_back(p); particleList.push_back(p);
numParticles++;
// pos coordinates should be -gridSize/2 < x < gridSize/2 // pos coordinates should be -gridSize/2 < x < gridSize/2
setGrid(pos, 1); setGrid(pos, 1);
@@ -97,20 +50,19 @@ void DLASystem::addParticle(double pos[]) {
// if we hit an occupied site then we do nothing except print a message // if we hit an occupied site then we do nothing except print a message
// (this should never happen) // (this should never happen)
void DLASystem::addParticleOnAddCircle() { void DLASystem::addParticleOnAddCircle() {
double pos[2];
double theta = rgen.random01() * 2 * M_PI; double theta = rgen.random01() * 2 * M_PI;
pos[0] = ceil(addCircle * cos(theta)); std::array<double, 2> pos{ceil(addCircle * cos(theta)), 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,
// this has to be done separately... // this has to be done separately...
void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) { void DLASystem::setPosNeighbour(std::array<double, 2> &setpos, const std::array<double, 2> pos, int val) {
switch (val) { switch (val) {
case 0: case 0:
setpos[0] = pos[0] + 1.0; setpos[0] = pos[0] + 1.0;
@@ -131,92 +83,68 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
} }
} }
// if the view is smaller than the kill circle then increase the view area (zoom out)
void DLASystem::updateViewSize() {
double mult = 1.2;
if (viewSize < 2.0 * killCircle) {
setViewSize(viewSize * mult);
}
}
// set the view to be the size of the add circle (ie zoom in on the cluster)
void DLASystem::viewAddCircle() {
setViewSize(2.0 * addCircle); // factor of 2 is to go from radius to diameter
}
// 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(std::array<double, 2> pos) {
double newRadius = distanceFromOrigin(pos);
double rr = distanceFromOrigin(pos); if (newRadius > clusterRadius) {
if (rr > clusterRadius) { clusterRadius = newRadius;
clusterRadius = rr;
// 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;
updateViewSize();
} }
checkStop();
if (killCircle + 2 >= gridSize / 2) {
std::cerr << "Early Exit" << endl;
this->running = false;
}
} }
} }
// make a random move of the last particle in the particleList // make a random move of the last particle in the particleList
void DLASystem::moveLastParticle() { void DLASystem::moveLastParticle() {
int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop? int direction = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
double newpos[2]; std::array<double, 2> newpos{};
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[this->particleList.size() - 1];
setPosNeighbour(newpos, lastP->pos, rr); setPosNeighbour(newpos, lastP->pos, direction);
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
numParticles--;
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[numParticles - 1]->pos[0] = newpos[0]; particleList[this->particleList.size() - 1]->pos[0] = newpos[0];
particleList[numParticles - 1]->pos[1] = newpos[1]; particleList[this->particleList.size() - 1]->pos[1] = newpos[1];
setGrid(lastP->pos, 1); // set the new grid site to be occupied setGrid(lastP->pos, 1); // set the new grid site to be occupied
// check if we stick // check if we stick
if (checkStick()) { if (checkStick() && this->rgen.random01() <= this->stickProbability) {
//cout << "stick" << endl; this->csv_out << frame << "," << lastP->pos[0] << "," << lastP->pos[1] << 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 (numParticles % 100 == 0 && logfile.is_open()) {
logfile << numParticles << " " << 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();
} }
} }
// check if the last particle should stick (to a neighbour) // check if the last particle should stick (to a neighbour)
int DLASystem::checkStick() { int DLASystem::checkStick() {
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[this->particleList.size() - 1];
int result = 0; int result = 0;
// loop over neighbours // loop over neighbours
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
double checkpos[2]; std::array<double, 2> checkpos{};
setPosNeighbour(checkpos, lastP->pos, i); setPosNeighbour(checkpos, lastP->pos, i);
// if the neighbour is occupied... // if the neighbour is occupied...
if (readGrid(checkpos) == 1) if (readGrid(checkpos) == 1)
@@ -227,38 +155,44 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem() { DLASystem::DLASystem(Config config)
cout << "creating system, gridSize " << gridSize << endl; : stickProbability(config.stickProbability),
numParticles = 0; grid(gridSize * gridSize),
endNum = 1000; csv_out(std::move(config.csv)),
endNum(config.maxParticles),
frame(0),
running(false),
lastParticleIsActive(false) {
cout << "GridSize: " << gridSize << endl;
rgen.setSeed(config.seed);
// allocate memory for the grid, remember to free the memory in destructor /*
grid = new int *[gridSize]; * Handle grid data structure.
for (int i = 0; i < gridSize; i++) { * */
grid[i] = new int[gridSize]; // Add particle to the centre of the grid to start
} std::array<double, 2> pos{0.0, 0.0};
slowNotFast = 1; addParticle(pos);
// reset initial parameters
Reset();
// Make sure to include the central location in our csv.
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;
// this opens a logfile, if we want to... killCircle = 2.0 * addCircle;
//logfile.open("opfile.txt"); clusterRadius = 0.0;
} }
// 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(); particleList.clear();
// delete the grid
for (int i = 0; i < gridSize; i++)
delete[] grid[i];
delete[] grid;
if (logfile.is_open()) if (csv_out.is_open()) {
logfile.close(); csv_out.flush();
csv_out.close();
}
} }
+35 -56
View File
@@ -2,12 +2,12 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <stdio.h> #include <cstdio>
#include <vector> #include <vector>
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h> #include <cmath>
#include <random> #include <random>
#include <string> #include <string>
#include <sstream> #include <sstream>
@@ -15,18 +15,21 @@
#include "Particle.h" #include "Particle.h"
#include "rnd.h" #include "rnd.h"
using namespace std; class Config {
public:
int seed;
double stickProbability;
std::ofstream csv;
int maxParticles;
Config(int argc, char **argv);
};
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;
int numParticles;
// delete particles and clear the particle list
void clearParticles();
// size of cluster // size of cluster
double clusterRadius; double clusterRadius;
@@ -34,19 +37,17 @@ private:
double addCircle; double addCircle;
double killCircle; double killCircle;
double stickProbability;
// size of grid // size of grid
static const int gridSize = 1600; static const int gridSize = 1600;
int **grid; // this will be a 2d array that stores whether each site is occupied std::vector<int> grid;
// the window draws only part of the grid, viewSize controls how much...
double viewSize;
double drawScale;
// random number generator, class name is rnd, instance is rgen // random number generator, class name is rnd, instance is rgen
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)
@@ -56,74 +57,52 @@ 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;
int *index_grid(std::array<double, 2> pos) {
int halfGrid = gridSize / 2;
int i = (int) (pos[0] + halfGrid);
int j = (int) (pos[1] + halfGrid);
int ij = i * gridSize + j;
return &grid[ij];
}
public: public:
// these are public variables and functions // these are public variables and functions
// update the system: if there is an active particle then move it, // update the system: if there is an active particle then move it,
// else create a new particle (on the adding circle) // else create a new particle (on the adding circle)
void Update(); void update();
// is the simulation running (1) or paused (0) ? // is the simulation running (1) or paused (0) ?
int running; bool running;
// slowNotFast is +1 for slow running, 0 for fast
int slowNotFast;
// lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0 // lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
DLASystem(); explicit DLASystem(Config config);
// 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); }
// check whether we should stop (eg the cluster has reached the edge of the grid) void setRunning() { running = true; }
int checkStop();
// stop/start the algorithm void pauseRunning() { running = false; }
void setRunning() { if (checkStop() == true) running = 1; }
void pauseRunning() { running = 0; }
// set whether it runs fast or slow
void setSlow() { slowNotFast = 1; }
void setFast() { slowNotFast = 0; }
void setSuperFast() { slowNotFast = -1; }
// set which part of the grid is visible on the screen
// basically the screen shows co-ordinates -vv < x < vv
// where vv is the input value
void setViewSize(double vv) {
viewSize = vv;
drawScale = 2.0 / viewSize;
}
// if the killcircle is almost as big as the view then increase the view
void updateViewSize();
// set the view to be the approx size of the addCircle
void viewAddCircle();
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos. // if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
void updateClusterRadius(double pos[]); void updateClusterRadius(std::array<double, 2> pos);
// set and read grid entries associated with a given position // set and read grid entries associated with a given position
void setGrid(double pos[], int val); void setGrid(std::array<double, 2> pos, int val);
int readGrid(double pos[]); int readGrid(std::array<double, 2> pos);
// return the distance of a given point from the origin // return the distance of a given point from the origin
double distanceFromOrigin(double pos[]) { double distanceFromOrigin(std::array<double, 2> pos) {
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]); return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
} }
@@ -133,14 +112,14 @@ public:
void setParticleInactive() { lastParticleIsActive = 0; } void setParticleInactive() { lastParticleIsActive = 0; }
// add a particle at pos // add a particle at pos
void addParticle(double pos[]); void addParticle(std::array<double, 2> pos);
// add a particle at a random point on the addCircle // add a particle at a random point on the addCircle
void addParticleOnAddCircle(); void addParticleOnAddCircle();
// assign setpos to the position of a neighbour of pos // assign setpos to the position of a neighbour of pos
// which neighbour we look at is determined by val (=0,1,2,3) // which neighbour we look at is determined by val (=0,1,2,3)
void setPosNeighbour(double setpos[], double pos[], int val); void setPosNeighbour(std::array<double, 2> &setpos, std::array<double, 2> pos, int val);
// this attempts to move the last particle in the List to a random neighbour // this attempts to move the last particle in the List to a random neighbour
// if the neighbour is occupied then nothing happens // if the neighbour is occupied then nothing happens
+2 -2
View File
@@ -10,11 +10,11 @@
CXX = clang++ CXX = clang++
CXXFLAGS = -Wall -Wextra -g -O0 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:
+6 -14
View File
@@ -1,22 +1,14 @@
#pragma once #pragma once
#include <array>
class Particle { class Particle {
public: public:
static const int dim = 2; // we are in two dimensions static const int dim = 2; // we are in two dimensions
double *pos; // pointer to an array of size dim, to store the position std::array<double, dim> pos{};
// default constructor // constructor, with a specified initial pos
Particle() { explicit Particle(std::array<double, dim> pos) {
pos = new double[dim]; this->pos[0] = pos[0];
this->pos[1] = pos[1];
} }
// constructor, with a specified initial position
Particle(double set_pos[]) {
pos = new double[dim];
for (int d = 0; d < dim; d++)
pos[d] = set_pos[d];
}
// destructor
~Particle() { delete[] pos; }
}; };
Executable
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
parallel "../run {1} {2} {3}" ::: $(seq 0 10) ::: $(seq 0 0.05 1) ::: $(seq 2000 1000 10000)
+41 -13
View File
@@ -1,29 +1,57 @@
#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"
// this is a global pointer, which is how we access the system itself using std::cout;
DLASystem *sys; using std::endl;
/*
* 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) {
if (argc != 4) {
exit(1);
} else {
this->seed = std::stoi(argv[1]);
this->stickProbability = std::stod(argv[2]);
this->maxParticles = std::stoi(argv[3]);
if (stickProbability <= 0 || stickProbability > 1) {
exit(1);
}
std::stringstream str;
// Ensure the output file name contains all information required to replicate data
str << "./out-" << seed << '-' << stickProbability << "-" << maxParticles << ".csv";
std::ofstream csv_out(str.str());
this->csv = std::move(csv_out);
// Add headers to csv output
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) {
// create the system Config config(argc, argv);
sys = new DLASystem();
// this is the seed for the random numbers // Create the system
int seed = 6; auto *sys = new DLASystem(std::move(config));
cout << "setting seed " << seed << endl; sys->setRunning();
sys->setSeed(seed);
sys->running = true;
/* /*
* 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();
} }
return 0; return 0;
+1001
View File
File diff suppressed because it is too large Load Diff