Compare commits
No commits in common. "4649658a9b46cf6cb56d10b0dcee5ec4d69c429a" and "629989f1967306246b6a2559ac16c60fabaae713" have entirely different histories.
4649658a9b
...
629989f196
101
DLASystem.cpp
101
DLASystem.cpp
@ -40,9 +40,7 @@ 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++) {
|
||||||
for (int k = 0; k < gridSize; ++k) {
|
grid[i][j] = 0;
|
||||||
grid[i][j][k] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +49,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, 0.0};
|
double pos[] = {0.0, 0.0};
|
||||||
addParticle(pos);
|
addParticle(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,13 +58,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)][(int) (pos[2] + halfGrid)] = 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(double pos[]) {
|
||||||
int halfGrid = gridSize / 2;
|
int halfGrid = gridSize / 2;
|
||||||
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)][(int) (pos[2] + halfGrid)];
|
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)];
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the cluster is big enough and we should stop:
|
// check if the cluster is big enough and we should stop:
|
||||||
@ -95,58 +93,38 @@ 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[3];
|
double pos[2];
|
||||||
double theta = rgen.random01() * M_PI;
|
double theta = rgen.random01() * 2 * M_PI;
|
||||||
double phi = rgen.random01() * 2 * M_PI;
|
pos[0] = ceil(addCircle * cos(theta));
|
||||||
|
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
|
||||||
cerr << "FAIL " << pos[0] << " " << pos[1] << endl;
|
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
|
// 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(double setpos[], double pos[], int val) {
|
||||||
const static double offsets[NEIGHBOURS][3] = {
|
switch (val) {
|
||||||
{-1, -1, -1},
|
case 0:
|
||||||
{-1, -1, 0},
|
setpos[0] = pos[0] + 1.0;
|
||||||
{-1, -1, 1},
|
setpos[1] = pos[1];
|
||||||
{-1, 0, -1},
|
break;
|
||||||
{-1, 0, 0},
|
case 1:
|
||||||
{-1, 0, 1},
|
setpos[0] = pos[0] - 1.0;
|
||||||
{-1, 1, -1},
|
setpos[1] = pos[1];
|
||||||
{-1, 1, 0},
|
break;
|
||||||
{-1, 1, 1},
|
case 2:
|
||||||
{0, -1, -1},
|
setpos[0] = pos[0];
|
||||||
{0, -1, 0},
|
setpos[1] = pos[1] + 1.0;
|
||||||
{0, -1, 1},
|
break;
|
||||||
{0, 0, -1},
|
case 3:
|
||||||
{0, 0, 1},
|
setpos[0] = pos[0];
|
||||||
{0, 1, -1},
|
setpos[1] = pos[1] - 1.0;
|
||||||
{0, 1, 0},
|
break;
|
||||||
{0, 1, 1},
|
}
|
||||||
{1, -1, -1},
|
|
||||||
{1, -1, 0},
|
|
||||||
{1, -1, 1},
|
|
||||||
{1, 0, -1},
|
|
||||||
{1, 0, 0},
|
|
||||||
{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
|
// when we add a particle to the cluster, we should update the cluster radius
|
||||||
@ -172,8 +150,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(NEIGHBOURS); // pick a random number in the range 0-3, which direction do we hop?
|
int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
|
||||||
double newpos[3];
|
double newpos[2];
|
||||||
|
|
||||||
Particle *lastP = particleList[numParticles - 1];
|
Particle *lastP = particleList[numParticles - 1];
|
||||||
|
|
||||||
@ -190,7 +168,6 @@ 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
|
||||||
@ -206,8 +183,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 < 6; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
double checkpos[3];
|
double checkpos[2];
|
||||||
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) {
|
||||||
@ -227,13 +204,9 @@ 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
|
||||||
@ -252,12 +225,8 @@ 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()) {
|
||||||
@ -267,9 +236,9 @@ DLASystem::~DLASystem() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DLASystem::exportData() {
|
void DLASystem::exportData() {
|
||||||
csv << "x,y,z" << endl;
|
csv << "x,y" << endl;
|
||||||
|
|
||||||
for (auto particle: particleList) {
|
for (auto particle: particleList) {
|
||||||
csv << particle->pos[0] << "," << particle->pos[1] << "," << particle->pos[2] << endl;
|
csv << particle->pos[0] << "," << particle->pos[1] << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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] + pos[2]*pos[2] );
|
return sqrt( pos[0]*pos[0] + pos[1]*pos[1] );
|
||||||
}
|
}
|
||||||
|
|
||||||
// set whether there is an active particle in the system or not
|
// set whether there is an active particle in the system or not
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
class Particle {
|
class Particle {
|
||||||
public:
|
public:
|
||||||
static const int dim = 3; // 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
|
double *pos; // pointer to an array of size dim, to store the position
|
||||||
|
|
||||||
// default constructor
|
// default constructor
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user