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

3
.gitignore vendored
View File

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

View File

@ -10,7 +10,7 @@ using std::endl;
// this function gets called every step,
// if there is an active particle then it gets moved,
// if not then add a particle
void DLASystem::Update() {
void DLASystem::update() {
this->frame++;
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
// note the position has the initial particle at (0,0)
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
void DLASystem::setGrid(double pos[], int val) {
int halfGrid = gridSize / 2;
grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)] = val;
void DLASystem::setGrid(std::array<double, 2> pos, int val) {
*index_grid(pos) = val;
}
// read the grid cell for a given position
int DLASystem::readGrid(double pos[]) {
int halfGrid = gridSize / 2;
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)];
int DLASystem::readGrid(std::array<double, 2> pos) {
return *index_grid(pos);
}
// 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
Particle *p = new Particle(pos);
auto *p = new Particle(pos);
// push_back means "add this to the end of the list"
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
// (this should never happen)
void DLASystem::addParticleOnAddCircle() {
double pos[2];
double theta = rgen.random01() * 2 * M_PI;
pos[0] = ceil(addCircle * cos(theta));
pos[1] = ceil(addCircle * sin(theta));
std::array<double, 2> pos{ceil(addCircle * cos(theta)), ceil(addCircle * sin(theta))};
if (readGrid(pos) == 0) {
addParticle(pos);
} else {
@ -74,7 +62,7 @@ void DLASystem::addParticleOnAddCircle() {
// send back the position of a neighbour of a given grid cell
// NOTE: there is no check that the neighbour is inside the grid,
// 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) {
case 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
// 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);
if (newRadius > clusterRadius) {
clusterRadius = newRadius;
@ -124,7 +112,7 @@ void DLASystem::updateClusterRadius(double pos[]) {
// make a random move of the last particle in the particleList
void DLASystem::moveLastParticle() {
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];
@ -156,7 +144,7 @@ int DLASystem::checkStick() {
int result = 0;
// loop over neighbours
for (int i = 0; i < 4; i++) {
double checkpos[2];
std::array<double, 2> checkpos{};
setPosNeighbour(checkpos, lastP->pos, i);
// if the neighbour is occupied...
if (readGrid(checkpos) == 1)
@ -169,6 +157,7 @@ int DLASystem::checkStick() {
// constructor
DLASystem::DLASystem(Config config)
: stickProbability(config.stickProbability),
grid(gridSize * gridSize),
csv_out(std::move(config.csv)),
endNum(config.maxParticles),
frame(0),
@ -180,21 +169,8 @@ DLASystem::DLASystem(Config config)
/*
* 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
double pos[] = {0.0, 0.0};
std::array<double, 2> pos{0.0, 0.0};
addParticle(pos);
// Make sure to include the central location in our csv.
@ -213,11 +189,7 @@ DLASystem::DLASystem(Config config)
// destructor
DLASystem::~DLASystem() {
// delete the particles
clearParticles();
// delete the grid
for (int i = 0; i < gridSize; i++)
delete[] grid[i];
delete[] grid;
particleList.clear();
if (csv_out.is_open()) {
csv_out.flush();

View File

@ -2,12 +2,12 @@
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdio>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>
#include <random>
#include <string>
#include <sstream>
@ -21,6 +21,7 @@ public:
double stickProbability;
std::ofstream csv;
int maxParticles;
Config(int argc, char **argv);
};
@ -30,9 +31,6 @@ private:
// list of particles
std::vector<Particle *> particleList;
// delete particles and clear the particle list
void clearParticles();
// size of cluster
double clusterRadius;
// these are related to the DLA algorithm
@ -43,7 +41,7 @@ private:
// size of grid
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
rnd rgen;
@ -61,12 +59,20 @@ private:
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:
// these are public variables and functions
// update the system: if there is an active particle then move it,
// else create a new particle (on the adding circle)
void Update();
void update();
// is the simulation running (1) or paused (0) ?
bool running;
@ -75,7 +81,7 @@ public:
int lastParticleIsActive;
// constructor
DLASystem(Config config);
explicit DLASystem(Config config);
// destructor
~DLASystem();
@ -88,15 +94,15 @@ public:
void pauseRunning() { running = false; }
// 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
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
double distanceFromOrigin(double pos[]) {
double distanceFromOrigin(std::array<double, 2> pos) {
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
}
@ -106,14 +112,14 @@ public:
void setParticleInactive() { lastParticleIsActive = 0; }
// 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
void addParticleOnAddCircle();
// assign setpos to the position of a neighbour of pos
// 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
// if the neighbour is occupied then nothing happens

View File

@ -1,22 +1,14 @@
#pragma once
#include <array>
class Particle {
public:
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
Particle() {
pos = new double[dim];
// constructor, with a specified initial pos
explicit Particle(std::array<double, dim> pos) {
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.
* */
while (sys->running) {
sys->Update();
sys->update();
}
return 0;

1001
out-2-0.5-1000.csv Normal file

File diff suppressed because it is too large Load Diff