Move from C array style grid to vector

This commit is contained in:
Joshua Coles 2023-02-20 20:06:44 +00:00
parent 345da9aa86
commit 6afaf0c82d
4 changed files with 1021 additions and 22 deletions

3
.gitignore vendored
View File

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

View File

@ -35,14 +35,12 @@ void DLASystem::clearParticles() {
// 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;
*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)];
return *index_grid(pos);
}
// add a particle to the system at a specific position
@ -169,6 +167,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),
@ -181,17 +180,12 @@ 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;
}
}
// // 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};
@ -214,10 +208,6 @@ DLASystem::DLASystem(Config config)
DLASystem::~DLASystem() {
// delete the particles
clearParticles();
// delete the grid
for (int i = 0; i < gridSize; i++)
delete[] grid[i];
delete[] grid;
if (csv_out.is_open()) {
csv_out.flush();

View File

@ -21,6 +21,7 @@ public:
double stickProbability;
std::ofstream csv;
int maxParticles;
Config(int argc, char **argv);
};
@ -43,7 +44,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,6 +62,14 @@ private:
int frame;
int *index_grid(double 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

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

File diff suppressed because it is too large Load Diff