Compare commits

..
9 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
8 changed files with 1082 additions and 112 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
+35 -67
View File
@@ -10,7 +10,7 @@ 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++; this->frame++;
if (lastParticleIsActive == 1) { if (lastParticleIsActive == 1) {
@@ -23,58 +23,22 @@ void DLASystem::Update() {
} }
} }
void DLASystem::clearParticles() {
// delete particles and the particle list
for (size_t i = 0; i < this->particleList.size(); i++) {
delete particleList[i];
}
particleList.clear();
}
// remove any existing particles and setup initial condition
void DLASystem::Reset() {
// stop running
running = false;
frame = 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 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)];
} }
// 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);
@@ -86,10 +50,8 @@ 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 {
@@ -100,7 +62,7 @@ void DLASystem::addParticleOnAddCircle() {
// 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;
@@ -123,7 +85,7 @@ 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(std::array<double, 2> pos) {
double newRadius = distanceFromOrigin(pos); double newRadius = distanceFromOrigin(pos);
if (newRadius > clusterRadius) { if (newRadius > clusterRadius) {
clusterRadius = newRadius; clusterRadius = newRadius;
@@ -150,7 +112,7 @@ void DLASystem::updateClusterRadius(double pos[]) {
// 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 direction = 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[this->particleList.size() - 1]; Particle *lastP = particleList[this->particleList.size() - 1];
@@ -182,7 +144,7 @@ int DLASystem::checkStick() {
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)
@@ -194,37 +156,43 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem(Config config) DLASystem::DLASystem(Config config)
: stickProbability(config.stickProbability), csv_out(std::move(config.csv)), endNum(config.maxParticles) { : stickProbability(config.stickProbability),
cout << "creating system, gridSize " << gridSize << endl; grid(gridSize * gridSize),
csv_out(std::move(config.csv)),
endNum(config.maxParticles),
frame(0),
running(false),
lastParticleIsActive(false) {
cout << "GridSize: " << gridSize << endl;
rgen.setSeed(config.seed); 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};
addParticle(pos);
// reset initial parameters
Reset();
// Make sure to include the central location in our csv.
this->csv_out << 0 << "," << 0 << "," << 0 << std::endl; 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;
killCircle = 2.0 * addCircle;
clusterRadius = 0.0;
} }
// destructor // destructor
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// delete the particles // delete the particles
clearParticles(); particleList.clear();
// delete the grid
for (int i = 0; i < gridSize; i++)
delete[] grid[i];
delete[] grid;
csv_out.flush();
if (csv_out.is_open()) { if (csv_out.is_open()) {
csv_out.flush();
csv_out.close(); csv_out.close();
} }
} }
+20 -17
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>
@@ -21,6 +21,7 @@ public:
double stickProbability; double stickProbability;
std::ofstream csv; std::ofstream csv;
int maxParticles; int maxParticles;
Config(int argc, char **argv); Config(int argc, char **argv);
}; };
@@ -30,9 +31,6 @@ private:
// list of particles // list of particles
std::vector<Particle *> particleList; std::vector<Particle *> particleList;
// delete particles and clear the particle list
void clearParticles();
// size of cluster // size of cluster
double clusterRadius; double clusterRadius;
// these are related to the DLA algorithm // these are related to the DLA algorithm
@@ -43,7 +41,7 @@ private:
// 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;
// random number generator, class name is rnd, instance is rgen // random number generator, class name is rnd, instance is rgen
rnd rgen; rnd rgen;
@@ -61,12 +59,20 @@ private:
int frame; 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) ?
bool running; bool running;
@@ -75,14 +81,11 @@ public:
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
DLASystem(Config config); 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); }
@@ -91,15 +94,15 @@ public:
void pauseRunning() { running = false; } void pauseRunning() { running = false; }
// 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]);
} }
@@ -109,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
+1 -1
View File
@@ -14,7 +14,7 @@ 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; }
}; };
+1 -1
View File
@@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
parallel "../run {1} {2} {3}" ::: $(seq 0 10) ::: $(seq 0 0.05 1) ::: 1000 parallel "../run {1} {2} {3}" ::: $(seq 0 10) ::: $(seq 0 0.05 1) ::: $(seq 2000 1000 10000)
+15 -12
View File
@@ -1,7 +1,6 @@
#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"
@@ -9,9 +8,9 @@
using std::cout; using std::cout;
using std::endl; using std::endl;
// this is a global pointer, which is how we access the system itself /*
DLASystem *sys; * 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) { Config::Config(int argc, char **argv) {
if (argc != 4) { if (argc != 4) {
exit(1); exit(1);
@@ -30,26 +29,30 @@ Config::Config(int argc, char **argv) {
std::ofstream csv_out(str.str()); std::ofstream csv_out(str.str());
this->csv = std::move(csv_out); this->csv = std::move(csv_out);
// Add headers to csv output
this->csv << "frame" << "," << "x" << "," << "y" << std::endl; 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) {
Config config(argc, argv); Config config(argc, argv);
cout << "Seed: " << config.seed << ", Stick P: " << config.stickProbability << endl;
// create the system // Create the system
sys = new DLASystem(std::move(config)); auto *sys = new DLASystem(std::move(config));
sys->setRunning(); sys->setRunning();
/* /*
* 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();
} }
cout << "hey" << endl;
return 0; return 0;
} }
+1001
View File
File diff suppressed because it is too large Load Diff