comp-b-cw1/DLASystem.h

133 lines
3.6 KiB
C++

#pragma once
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <random>
#include <string>
#include <sstream>
#include "Particle.h"
#include "rnd.h"
class Config {
public:
int seed;
double stickProbability;
std::ofstream csv;
int maxParticles;
Config(int argc, char **argv);
};
class DLASystem {
private:
// these are private variables and functions that the user will not see
// list of particles
std::vector<Particle *> particleList;
// size of cluster
double clusterRadius;
// these are related to the DLA algorithm
double addCircle;
double killCircle;
double stickProbability;
// size of grid
static const int gridSize = 1600;
std::vector<int> grid;
// random number generator, class name is rnd, instance is rgen
rnd rgen;
// output file (not used at the moment)
std::ofstream csv_out;
// number of particles at which the simulation will stop
// (the value is set in constructor)
int endNum;
// the values of these variables are set in the constructor
double addRatio; // how much bigger the addCircle should be, compared to cluster radius
double killRatio; // how much bigger is the killCircle, compared to the addCircle
int frame;
int *index_grid(std::array<double, 2> 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
// update the system: if there is an active particle then move it,
// else create a new particle (on the adding circle)
void update();
// is the simulation running (1) or paused (0) ?
bool running;
// lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0
int lastParticleIsActive;
// constructor
explicit DLASystem(Config config);
// destructor
~DLASystem();
// this sets the seed for the random numbers
void setSeed(int s) { rgen.setSeed(s); }
void setRunning() { running = true; }
void pauseRunning() { running = false; }
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
void updateClusterRadius(std::array<double, 2> pos);
// set and read grid entries associated with a given position
void setGrid(std::array<double, 2> pos, int val);
int readGrid(std::array<double, 2> pos);
// return the distance of a given point from the origin
double distanceFromOrigin(std::array<double, 2> pos) {
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
}
// set whether there is an active particle in the system or not
void setParticleActive() { lastParticleIsActive = 1; }
void setParticleInactive() { lastParticleIsActive = 0; }
// add a particle at pos
void addParticle(std::array<double, 2> pos);
// add a particle at a random point on the addCircle
void addParticleOnAddCircle();
// assign setpos to the position of a neighbour of pos
// which neighbour we look at is determined by val (=0,1,2,3)
void setPosNeighbour(std::array<double, 2> &setpos, std::array<double, 2> pos, int val);
// this attempts to move the last particle in the List to a random neighbour
// if the neighbour is occupied then nothing happens
// the function also checks if the moving particle should stick.
void moveLastParticle();
// check whether the last particle should stick
// currently it sticks whenever it touches another particle
int checkStick();
};