Compare commits

..

5 Commits

Author SHA1 Message Date
56d8007d46 Make clang-tidy a little happier 2023-02-20 20:56:10 +00:00
9f34807269 God I hate CPP 2023-02-20 20:48:16 +00:00
7d37960439 God I hate CPP 2023-02-20 20:46:41 +00:00
6afaf0c82d Move from C array style grid to vector 2023-02-20 20:06:44 +00:00
345da9aa86 Rename update 2023-02-20 19:53:53 +00:00
6 changed files with 1044 additions and 74 deletions

1
.gitignore vendored
View File

@ -112,5 +112,4 @@ fabric.properties
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
/run /run
*.csv
*.jl *.jl

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,32 +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();
}
// 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);
@ -60,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 {
@ -74,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;
@ -97,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;
@ -124,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];
@ -156,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)
@ -169,6 +157,7 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem(Config config) DLASystem::DLASystem(Config config)
: stickProbability(config.stickProbability), : stickProbability(config.stickProbability),
grid(gridSize * gridSize),
csv_out(std::move(config.csv)), csv_out(std::move(config.csv)),
endNum(config.maxParticles), endNum(config.maxParticles),
frame(0), frame(0),
@ -180,21 +169,8 @@ DLASystem::DLASystem(Config config)
/* /*
* Handle grid data structure. * Handle grid data structure.
* */ * */
grid = new int *[gridSize];
for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize];
}
// set the grid to zero
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
grid[i][j] = 0;
}
}
// Add particle to the centre of the grid to start // Add particle to the centre of the grid to start
double pos[] = {0.0, 0.0}; std::array<double, 2> pos{0.0, 0.0};
addParticle(pos); addParticle(pos);
// Make sure to include the central location in our csv. // Make sure to include the central location in our csv.
@ -213,11 +189,7 @@ DLASystem::DLASystem(Config config)
// 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;
if (csv_out.is_open()) { if (csv_out.is_open()) {
csv_out.flush(); csv_out.flush();

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,7 +81,7 @@ public:
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
DLASystem(Config config); explicit DLASystem(Config config);
// destructor // destructor
~DLASystem(); ~DLASystem();
@ -88,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]);
} }
@ -106,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

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; }
}; };

View File

@ -51,7 +51,7 @@ int main(int argc, char **argv) {
* NOTE: We run at max speed as rendering is handled by a different engine so we don't need to care. * 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
out-2-0.5-1000.csv Normal file

File diff suppressed because it is too large Load Diff