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 .idea/caches/build_file_checksums.ser
/run /run
*.csv *.jl
*.jl

View File

@ -35,14 +35,12 @@ void DLASystem::clearParticles() {
// note the position has the initial particle at (0,0) // note the position has the initial particle at (0,0)
// 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; *index_grid(pos) = 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; return *index_grid(pos);
return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)];
} }
// add a particle to the system at a specific position // add a particle to the system at a specific position
@ -169,6 +167,7 @@ int DLASystem::checkStick() {
// constructor // constructor
DLASystem::DLASystem(Config config) DLASystem::DLASystem(Config config)
: stickProbability(config.stickProbability), : stickProbability(config.stickProbability),
grid(gridSize * gridSize),
csv_out(std::move(config.csv)), csv_out(std::move(config.csv)),
endNum(config.maxParticles), endNum(config.maxParticles),
frame(0), frame(0),
@ -181,17 +180,12 @@ DLASystem::DLASystem(Config config)
* Handle grid data structure. * Handle grid data structure.
* */ * */
grid = new int *[gridSize]; // // set the grid to zero
for (int i = 0; i < gridSize; i++) { // for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize]; // 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 // Add particle to the centre of the grid to start
double pos[] = {0.0, 0.0}; double pos[] = {0.0, 0.0};
@ -214,10 +208,6 @@ DLASystem::DLASystem(Config config)
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// delete the particles // delete the particles
clearParticles(); clearParticles();
// delete the grid
for (int i = 0; i < gridSize; i++)
delete[] grid[i];
delete[] grid;
if (csv_out.is_open()) { if (csv_out.is_open()) {
csv_out.flush(); csv_out.flush();

View File

@ -21,6 +21,7 @@ public:
double stickProbability; double stickProbability;
std::ofstream csv; std::ofstream csv;
int maxParticles; int maxParticles;
Config(int argc, char **argv); Config(int argc, char **argv);
}; };
@ -43,7 +44,7 @@ private:
// 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 std::vector<int> grid;
// random number generator, class name is rnd, instance is rgen // random number generator, class name is rnd, instance is rgen
rnd rgen; rnd rgen;
@ -61,6 +62,14 @@ private:
int frame; 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: public:
// these are public variables and functions // 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