Compare commits
3 Commits
main
...
3d-off-axi
| Author | SHA1 | Date | |
|---|---|---|---|
| 40cc47e07a | |||
| 4649658a9b | |||
| 3c6e6947fb |
104
DLASystem.cpp
104
DLASystem.cpp
@ -40,7 +40,9 @@ void DLASystem::Reset() {
|
||||
// set the grid to zero
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
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;
|
||||
clusterRadius = 0.0;
|
||||
// add a single particle at the origin
|
||||
double pos[] = {0.0, 0.0};
|
||||
double pos[] = {0.0, 0.0, 0.0};
|
||||
addParticle(pos);
|
||||
}
|
||||
|
||||
@ -58,13 +60,13 @@ void DLASystem::Reset() {
|
||||
// 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;
|
||||
grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)][(int) (pos[2] + halfGrid)] = 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)];
|
||||
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:
|
||||
@ -93,38 +95,61 @@ 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));
|
||||
double pos[3];
|
||||
double theta = rgen.random01() * M_PI;
|
||||
double phi = rgen.random01() * 2 * M_PI;
|
||||
|
||||
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)
|
||||
addParticle(pos);
|
||||
else
|
||||
cerr << "FAIL " << pos[0] << " " << pos[1] << endl;
|
||||
}
|
||||
|
||||
const static size_t NEIGHBOURS = 26;
|
||||
|
||||
// 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) {
|
||||
switch (val) {
|
||||
case 0:
|
||||
setpos[0] = pos[0] + 1.0;
|
||||
setpos[1] = pos[1];
|
||||
break;
|
||||
case 1:
|
||||
setpos[0] = pos[0] - 1.0;
|
||||
setpos[1] = pos[1];
|
||||
break;
|
||||
case 2:
|
||||
setpos[0] = pos[0];
|
||||
setpos[1] = pos[1] + 1.0;
|
||||
break;
|
||||
case 3:
|
||||
setpos[0] = pos[0];
|
||||
setpos[1] = pos[1] - 1.0;
|
||||
break;
|
||||
}
|
||||
const static double offsets[NEIGHBOURS][3] = {
|
||||
// These 6 are first to represent the 6 immediate neighbours of the cell when checking sticking
|
||||
{1, 0, 0},
|
||||
{-1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, -1, 0},
|
||||
{0, 0, 1},
|
||||
{0, 0, -1},
|
||||
|
||||
// The off-axis neighbours
|
||||
{-1, -1, -1},
|
||||
{-1, -1, 0},
|
||||
{-1, -1, 1},
|
||||
{-1, 0, -1},
|
||||
{-1, 0, 1},
|
||||
{-1, 1, -1},
|
||||
{-1, 1, 0},
|
||||
{-1, 1, 1},
|
||||
{0, -1, -1},
|
||||
{0, -1, 1},
|
||||
{0, 1, -1},
|
||||
{0, 1, 1},
|
||||
{1, -1, -1},
|
||||
{1, -1, 0},
|
||||
{1, -1, 1},
|
||||
{1, 0, -1},
|
||||
{1, 0, 1},
|
||||
{1, 1, -1},
|
||||
{1, 1, 0},
|
||||
{1, 1, 1}
|
||||
};
|
||||
|
||||
setpos[0] = pos[0] + offsets[val][0];
|
||||
setpos[1] = pos[1] + offsets[val][1];
|
||||
setpos[2] = pos[2] + offsets[val][2];
|
||||
}
|
||||
|
||||
// when we add a particle to the cluster, we should update the cluster radius
|
||||
@ -150,8 +175,8 @@ void DLASystem::updateClusterRadius(double pos[]) {
|
||||
|
||||
// make a random move of the last particle in the particleList
|
||||
void DLASystem::moveLastParticle() {
|
||||
int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
|
||||
double newpos[2];
|
||||
int rr = rgen.randomInt(NEIGHBOURS); // pick a random number in the range 0-3, which direction do we hop?
|
||||
double newpos[3];
|
||||
|
||||
Particle *lastP = particleList[numParticles - 1];
|
||||
|
||||
@ -168,6 +193,7 @@ void DLASystem::moveLastParticle() {
|
||||
// update the position
|
||||
particleList[numParticles - 1]->pos[0] = newpos[0];
|
||||
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
|
||||
|
||||
// check if we stick
|
||||
@ -183,8 +209,8 @@ void DLASystem::moveLastParticle() {
|
||||
int DLASystem::checkStick() {
|
||||
Particle *lastP = particleList[numParticles - 1];
|
||||
// loop over neighbours
|
||||
for (int i = 0; i < 4; i++) {
|
||||
double checkpos[2];
|
||||
for (int i = 0; i < 6; i++) {
|
||||
double checkpos[3];
|
||||
setPosNeighbour(checkpos, lastP->pos, i);
|
||||
// if the neighbour is occupied and the particle will stick probabilistically.
|
||||
if (readGrid(checkpos) == 1 && rgen.random01() < stickProbability) {
|
||||
@ -204,9 +230,13 @@ DLASystem::DLASystem(const int maxParticles, const string &csvPath, const double
|
||||
this->stickProbability = stickProbability;
|
||||
|
||||
// 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++) {
|
||||
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
|
||||
@ -225,8 +255,12 @@ DLASystem::~DLASystem() {
|
||||
// delete the particles
|
||||
clearParticles();
|
||||
// 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;
|
||||
|
||||
if (csv.is_open()) {
|
||||
@ -236,9 +270,9 @@ DLASystem::~DLASystem() {
|
||||
}
|
||||
|
||||
void DLASystem::exportData() {
|
||||
csv << "x,y" << endl;
|
||||
csv << "x,y,z" << endl;
|
||||
|
||||
for (auto particle: particleList) {
|
||||
csv << particle->pos[0] << "," << particle->pos[1] << endl;
|
||||
csv << particle->pos[0] << "," << particle->pos[1] << "," << particle->pos[2] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ class DLASystem {
|
||||
|
||||
// size of grid
|
||||
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
|
||||
rnd rgen;
|
||||
@ -93,7 +93,7 @@ class DLASystem {
|
||||
|
||||
// return the distance of a given point from the origin
|
||||
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
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
class Particle {
|
||||
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
|
||||
|
||||
// default constructor
|
||||
|
||||
Loading…
Reference in New Issue
Block a user