3D conversion

This commit is contained in:
Joshua Coles 2023-03-14 14:46:03 +00:00
parent 629989f196
commit 3c6e6947fb
3 changed files with 49 additions and 20 deletions

View File

@ -40,7 +40,9 @@ void DLASystem::Reset() {
// set the grid to zero // set the grid to zero
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) { for (int j = 0; j < gridSize; j++) {
grid[i][j] = 0; for (int k = 0; k < gridSize; ++k) {
grid[i][j][k] = 0;
}
} }
} }
@ -49,7 +51,7 @@ void DLASystem::Reset() {
killCircle = 2.0 * addCircle; killCircle = 2.0 * addCircle;
clusterRadius = 0.0; clusterRadius = 0.0;
// add a single particle at the origin // add a single particle at the origin
double pos[] = {0.0, 0.0}; double pos[] = {0.0, 0.0, 0.0};
addParticle(pos); addParticle(pos);
} }
@ -58,13 +60,13 @@ void DLASystem::Reset() {
// 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(double pos[], int val) {
int halfGrid = gridSize / 2; int halfGrid = gridSize / 2;
grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)] = val; grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)][(int) (pos[2] + 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(double pos[]) {
int halfGrid = gridSize / 2; int halfGrid = gridSize / 2;
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)]; return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)][(int) (pos[2] + halfGrid)];
} }
// check if the cluster is big enough and we should stop: // check if the cluster is big enough and we should stop:
@ -93,10 +95,14 @@ 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 pos[3];
double theta = rgen.random01() * 2 * M_PI; double theta = rgen.random01() * M_PI;
pos[0] = ceil(addCircle * cos(theta)); double phi = rgen.random01() * 2 * M_PI;
pos[1] = ceil(addCircle * sin(theta));
pos[0] = ceil(addCircle * sin(theta) * cos(phi));
pos[1] = ceil(addCircle * sin(theta) * sin(phi));
pos[2] = ceil(addCircle * cos(theta));
if (readGrid(pos) == 0) if (readGrid(pos) == 0)
addParticle(pos); addParticle(pos);
else else
@ -111,18 +117,32 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
case 0: case 0:
setpos[0] = pos[0] + 1.0; setpos[0] = pos[0] + 1.0;
setpos[1] = pos[1]; setpos[1] = pos[1];
setpos[2] = pos[2];
break; break;
case 1: case 1:
setpos[0] = pos[0] - 1.0; setpos[0] = pos[0] - 1.0;
setpos[1] = pos[1]; setpos[1] = pos[1];
setpos[2] = pos[2];
break; break;
case 2: case 2:
setpos[0] = pos[0]; setpos[0] = pos[0];
setpos[1] = pos[1] + 1.0; setpos[1] = pos[1] + 1.0;
setpos[2] = pos[2];
break; break;
case 3: case 3:
setpos[0] = pos[0]; setpos[0] = pos[0];
setpos[1] = pos[1] - 1.0; setpos[1] = pos[1] - 1.0;
setpos[2] = pos[2];
break;
case 4:
setpos[0] = pos[0];
setpos[1] = pos[1];
setpos[2] = pos[2] + 1.0;
break;
case 5:
setpos[0] = pos[0];
setpos[1] = pos[1];
setpos[2] = pos[2] - 1.0;
break; break;
} }
} }
@ -150,8 +170,8 @@ 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 rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop? int rr = rgen.randomInt(6); // pick a random number in the range 0-3, which direction do we hop?
double newpos[2]; double newpos[3];
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[numParticles - 1];
@ -168,6 +188,7 @@ void DLASystem::moveLastParticle() {
// update the position // update the position
particleList[numParticles - 1]->pos[0] = newpos[0]; particleList[numParticles - 1]->pos[0] = newpos[0];
particleList[numParticles - 1]->pos[1] = newpos[1]; particleList[numParticles - 1]->pos[1] = newpos[1];
particleList[numParticles - 1]->pos[2] = newpos[2];
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
@ -183,8 +204,8 @@ void DLASystem::moveLastParticle() {
int DLASystem::checkStick() { int DLASystem::checkStick() {
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[numParticles - 1];
// loop over neighbours // loop over neighbours
for (int i = 0; i < 4; i++) { for (int i = 0; i < 6; i++) {
double checkpos[2]; double checkpos[3];
setPosNeighbour(checkpos, lastP->pos, i); setPosNeighbour(checkpos, lastP->pos, i);
// if the neighbour is occupied and the particle will stick probabilistically. // if the neighbour is occupied and the particle will stick probabilistically.
if (readGrid(checkpos) == 1 && rgen.random01() < stickProbability) { if (readGrid(checkpos) == 1 && rgen.random01() < stickProbability) {
@ -204,9 +225,13 @@ DLASystem::DLASystem(const int maxParticles, const string &csvPath, const double
this->stickProbability = stickProbability; this->stickProbability = stickProbability;
// allocate memory for the grid, remember to free the memory in destructor // allocate memory for the grid, remember to free the memory in destructor
grid = new int *[gridSize]; grid = new int **[gridSize];
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize]; grid[i] = new int*[gridSize];
for (int j = 0; j < gridSize; ++j) {
grid[i][j] = new int[gridSize];
}
} }
// reset initial parameters // reset initial parameters
@ -225,8 +250,12 @@ DLASystem::~DLASystem() {
// delete the particles // delete the particles
clearParticles(); clearParticles();
// delete the grid // delete the grid
for (int i = 0; i < gridSize; i++) for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; ++j) {
delete[] grid[i][j];
}
delete[] grid[i]; delete[] grid[i];
}
delete[] grid; delete[] grid;
if (csv.is_open()) { if (csv.is_open()) {
@ -236,9 +265,9 @@ DLASystem::~DLASystem() {
} }
void DLASystem::exportData() { void DLASystem::exportData() {
csv << "x,y" << endl; csv << "x,y,z" << endl;
for (auto particle: particleList) { for (auto particle: particleList) {
csv << particle->pos[0] << "," << particle->pos[1] << endl; csv << particle->pos[0] << "," << particle->pos[1] << "," << particle->pos[2] << endl;
} }
} }

View File

@ -40,7 +40,7 @@ class DLASystem {
// 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 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;
@ -93,7 +93,7 @@ class DLASystem {
// 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(double pos[]) {
return sqrt( pos[0]*pos[0] + pos[1]*pos[1] ); return sqrt( pos[0]*pos[0] + pos[1]*pos[1] + pos[2]*pos[2] );
} }
// set whether there is an active particle in the system or not // set whether there is an active particle in the system or not

View File

@ -2,7 +2,7 @@
class Particle { class Particle {
public: public:
static const int dim = 2; // we are in two dimensions static const int dim = 3; // we are in two dimensions
double *pos; // pointer to an array of size dim, to store the position double *pos; // pointer to an array of size dim, to store the position
// default constructor // default constructor