Compare commits

..

No commits in common. "56d8007d46af8bdff82d05049593816424516eb0" and "39231f0d923bc842ec74b27c5123157c494e84c2" have entirely different histories.

6 changed files with 74 additions and 1044 deletions

3
.gitignore vendored
View File

@ -112,4 +112,5 @@ fabric.properties
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
/run /run
*.jl *.csv
*.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,22 +23,32 @@ 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(std::array<double, 2> pos, int val) { void DLASystem::setGrid(double pos[], int val) {
*index_grid(pos) = val; int halfGrid = gridSize / 2;
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(std::array<double, 2> pos) { int DLASystem::readGrid(double pos[]) {
return *index_grid(pos); int halfGrid = gridSize / 2;
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(std::array<double, 2> pos) { void DLASystem::addParticle(double pos[]) {
// create a new particle // create a new particle
auto *p = new Particle(pos); Particle *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);
@ -50,8 +60,10 @@ void DLASystem::addParticle(std::array<double, 2> 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;
std::array<double, 2> pos{ceil(addCircle * cos(theta)), ceil(addCircle * sin(theta))}; pos[0] = ceil(addCircle * cos(theta));
pos[1] = ceil(addCircle * sin(theta));
if (readGrid(pos) == 0) { if (readGrid(pos) == 0) {
addParticle(pos); addParticle(pos);
} else { } else {
@ -62,7 +74,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(std::array<double, 2> &setpos, const std::array<double, 2> pos, int val) { void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
switch (val) { switch (val) {
case 0: case 0:
setpos[0] = pos[0] + 1.0; setpos[0] = pos[0] + 1.0;
@ -85,7 +97,7 @@ void DLASystem::setPosNeighbour(std::array<double, 2> &setpos, const std::array<
// 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(std::array<double, 2> pos) { void DLASystem::updateClusterRadius(double pos[]) {
double newRadius = distanceFromOrigin(pos); double newRadius = distanceFromOrigin(pos);
if (newRadius > clusterRadius) { if (newRadius > clusterRadius) {
clusterRadius = newRadius; clusterRadius = newRadius;
@ -112,7 +124,7 @@ void DLASystem::updateClusterRadius(std::array<double, 2> 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?
std::array<double, 2> newpos{}; double newpos[2];
Particle *lastP = particleList[this->particleList.size() - 1]; Particle *lastP = particleList[this->particleList.size() - 1];
@ -144,7 +156,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++) {
std::array<double, 2> checkpos{}; double checkpos[2];
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)
@ -157,7 +169,6 @@ 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),
@ -169,8 +180,21 @@ 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
std::array<double, 2> pos{0.0, 0.0}; double 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.
@ -189,7 +213,11 @@ DLASystem::DLASystem(Config config)
// destructor // destructor
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// delete the particles // delete the particles
particleList.clear(); clearParticles();
// 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 <cstdio> #include <stdio.h>
#include <vector> #include <vector>
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <cmath> #include <math.h>
#include <random> #include <random>
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -21,7 +21,6 @@ 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);
}; };
@ -31,6 +30,9 @@ 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
@ -41,7 +43,7 @@ private:
// size of grid // size of grid
static const int gridSize = 1600; static const int gridSize = 1600;
std::vector<int> grid; int **grid; // this will be a 2d array that stores whether each site is occupied
// random number generator, class name is rnd, instance is rgen // random number generator, class name is rnd, instance is rgen
rnd rgen; rnd rgen;
@ -59,20 +61,12 @@ 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;
@ -81,7 +75,7 @@ public:
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
explicit DLASystem(Config config); DLASystem(Config config);
// destructor // destructor
~DLASystem(); ~DLASystem();
@ -94,15 +88,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(std::array<double, 2> pos); void updateClusterRadius(double pos[]);
// set and read grid entries associated with a given position // set and read grid entries associated with a given position
void setGrid(std::array<double, 2> pos, int val); void setGrid(double pos[], int val);
int readGrid(std::array<double, 2> pos); int readGrid(double pos[]);
// return the distance of a given point from the origin // return the distance of a given point from the origin
double distanceFromOrigin(std::array<double, 2> pos) { double distanceFromOrigin(double pos[]) {
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]); return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
} }
@ -112,14 +106,14 @@ public:
void setParticleInactive() { lastParticleIsActive = 0; } void setParticleInactive() { lastParticleIsActive = 0; }
// add a particle at pos // add a particle at pos
void addParticle(std::array<double, 2> pos); void addParticle(double 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(std::array<double, 2> &setpos, std::array<double, 2> pos, int val); void setPosNeighbour(double setpos[], double 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,14 +1,22 @@
#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
std::array<double, dim> pos{}; double *pos; // pointer to an array of size dim, to store the position
// constructor, with a specified initial pos // default constructor
explicit Particle(std::array<double, dim> pos) { Particle() {
this->pos[0] = pos[0]; pos = new double[dim];
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;

File diff suppressed because it is too large Load Diff