Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 56d8007d46 | |||
| 9f34807269 | |||
| 7d37960439 | |||
| 6afaf0c82d | |||
| 345da9aa86 | |||
| 39231f0d92 | |||
| 5f0f5c3820 | |||
| f7bfd64616 | |||
| 820323b53d | |||
| 7041c6d485 | |||
| 3f6a8f0282 | |||
| c33a2a207f | |||
| e47864752a | |||
| f323248610 | |||
| 54d02253ef | |||
| dacfff3e96 | |||
| c1b4b6bab1 | |||
| 863fd61d28 | |||
| d69402e32e | |||
| 952e31449f | |||
| 4e497b8438 | |||
| ef89b285d1 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -110,3 +110,6 @@ fabric.properties
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
/run
|
||||
*.jl
|
||||
|
||||
262
DLASystem.cpp
262
DLASystem.cpp
@ -4,102 +4,43 @@
|
||||
|
||||
#include "DLASystem.h"
|
||||
|
||||
// colors
|
||||
namespace colours {
|
||||
GLfloat blue[] = {0.1, 0.3, 0.9, 1.0}; // blue
|
||||
GLfloat red[] = {1.0, 0.2, 0.1, 0.2}; // red
|
||||
GLfloat green[] = {0.3, 0.6, 0.3, 1.0}; // green
|
||||
GLfloat paleGrey[] = {0.7, 0.7, 0.7, 1.0}; // green
|
||||
GLfloat darkGrey[] = {0.2, 0.2, 0.2, 1.0}; // green
|
||||
}
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
// this function gets called every step,
|
||||
// if there is an active particle then it gets moved,
|
||||
// if not then add a particle
|
||||
void DLASystem::Update() {
|
||||
if (lastParticleIsActive == 1)
|
||||
void DLASystem::update() {
|
||||
this->frame++;
|
||||
|
||||
if (lastParticleIsActive == 1) {
|
||||
moveLastParticle();
|
||||
else if (numParticles < endNum) {
|
||||
} else if (this->particleList.size() < (size_t) endNum) {
|
||||
addParticleOnAddCircle();
|
||||
setParticleActive();
|
||||
} else {
|
||||
this->running = false;
|
||||
}
|
||||
if (lastParticleIsActive == 0 || slowNotFast == 1)
|
||||
glutPostRedisplay(); //Tell GLUT that the display has changed
|
||||
}
|
||||
|
||||
|
||||
void DLASystem::clearParticles() {
|
||||
// delete particles and the particle list
|
||||
for (int i = 0; i < numParticles; i++) {
|
||||
delete particleList[i];
|
||||
}
|
||||
particleList.clear();
|
||||
numParticles = 0;
|
||||
}
|
||||
|
||||
// remove any existing particles and setup initial condition
|
||||
void DLASystem::Reset() {
|
||||
// stop running
|
||||
running = 0;
|
||||
|
||||
clearParticles();
|
||||
|
||||
lastParticleIsActive = 0;
|
||||
|
||||
// set the grid to zero
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
for (int j = 0; j < gridSize; j++) {
|
||||
grid[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// setup initial condition and parameters
|
||||
addCircle = 10;
|
||||
killCircle = 2.0 * addCircle;
|
||||
clusterRadius = 0.0;
|
||||
// add a single particle at the origin
|
||||
double pos[] = {0.0, 0.0};
|
||||
addParticle(pos);
|
||||
|
||||
// set the view
|
||||
int InitialViewSize = 40;
|
||||
setViewSize(InitialViewSize);
|
||||
|
||||
}
|
||||
|
||||
// set the value of a grid cell for a particular position
|
||||
// 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;
|
||||
void DLASystem::setGrid(std::array<double, 2> pos, int 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)];
|
||||
}
|
||||
|
||||
// check if the cluster is big enough and we should stop:
|
||||
// to be safe, we need the killCircle to be at least 2 less than the edge of the grid
|
||||
int DLASystem::checkStop() {
|
||||
if (killCircle + 2 >= gridSize / 2) {
|
||||
pauseRunning();
|
||||
cout << "STOP" << endl;
|
||||
glutPostRedisplay(); // update display
|
||||
return 1;
|
||||
} else return 0;
|
||||
int DLASystem::readGrid(std::array<double, 2> pos) {
|
||||
return *index_grid(pos);
|
||||
}
|
||||
|
||||
// add a particle to the system at a specific position
|
||||
void DLASystem::addParticle(double pos[]) {
|
||||
void DLASystem::addParticle(std::array<double, 2> pos) {
|
||||
// create a new particle
|
||||
Particle *p = new Particle(pos);
|
||||
auto *p = new Particle(pos);
|
||||
// push_back means "add this to the end of the list"
|
||||
particleList.push_back(p);
|
||||
numParticles++;
|
||||
|
||||
// pos coordinates should be -gridSize/2 < x < gridSize/2
|
||||
setGrid(pos, 1);
|
||||
@ -109,20 +50,19 @@ 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));
|
||||
if (readGrid(pos) == 0)
|
||||
std::array<double, 2> pos{ceil(addCircle * cos(theta)), ceil(addCircle * sin(theta))};
|
||||
if (readGrid(pos) == 0) {
|
||||
addParticle(pos);
|
||||
else
|
||||
} else {
|
||||
cout << "FAIL " << pos[0] << " " << pos[1] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
void DLASystem::setPosNeighbour(std::array<double, 2> &setpos, const std::array<double, 2> pos, int val) {
|
||||
switch (val) {
|
||||
case 0:
|
||||
setpos[0] = pos[0] + 1.0;
|
||||
@ -143,92 +83,68 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
|
||||
}
|
||||
}
|
||||
|
||||
// if the view is smaller than the kill circle then increase the view area (zoom out)
|
||||
void DLASystem::updateViewSize() {
|
||||
double mult = 1.2;
|
||||
if (viewSize < 2.0 * killCircle) {
|
||||
setViewSize(viewSize * mult);
|
||||
}
|
||||
}
|
||||
|
||||
// set the view to be the size of the add circle (ie zoom in on the cluster)
|
||||
void DLASystem::viewAddCircle() {
|
||||
setViewSize(2.0 * addCircle); // factor of 2 is to go from radius to diameter
|
||||
}
|
||||
|
||||
// when we add a particle to the cluster, we should update the cluster radius
|
||||
// and the sizes of the addCircle and the killCircle
|
||||
void DLASystem::updateClusterRadius(double pos[]) {
|
||||
|
||||
double rr = distanceFromOrigin(pos);
|
||||
if (rr > clusterRadius) {
|
||||
clusterRadius = rr;
|
||||
void DLASystem::updateClusterRadius(std::array<double, 2> pos) {
|
||||
double newRadius = distanceFromOrigin(pos);
|
||||
if (newRadius > clusterRadius) {
|
||||
clusterRadius = newRadius;
|
||||
// this is how big addCircle is supposed to be:
|
||||
// either 20% more than cluster radius, or at least 5 bigger.
|
||||
double check = clusterRadius * addRatio;
|
||||
if (check < clusterRadius + 5)
|
||||
if (check < clusterRadius + 5) {
|
||||
check = clusterRadius + 5;
|
||||
}
|
||||
|
||||
// if it is smaller then update everything...
|
||||
if (addCircle < check) {
|
||||
addCircle = check;
|
||||
killCircle = killRatio * addCircle;
|
||||
updateViewSize();
|
||||
}
|
||||
checkStop();
|
||||
|
||||
if (killCircle + 2 >= gridSize / 2) {
|
||||
std::cerr << "Early Exit" << endl;
|
||||
this->running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 direction = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
|
||||
std::array<double, 2> newpos{};
|
||||
|
||||
Particle *lastP = particleList[numParticles - 1];
|
||||
Particle *lastP = particleList[this->particleList.size() - 1];
|
||||
|
||||
setPosNeighbour(newpos, lastP->pos, rr);
|
||||
setPosNeighbour(newpos, lastP->pos, direction);
|
||||
|
||||
if (distanceFromOrigin(newpos) > killCircle) {
|
||||
//cout << "#deleting particle" << endl;
|
||||
setGrid(lastP->pos, 0);
|
||||
particleList.pop_back(); // remove particle from particleList
|
||||
numParticles--;
|
||||
setParticleInactive();
|
||||
}
|
||||
// check if destination is empty
|
||||
else if (readGrid(newpos) == 0) {
|
||||
} else if (readGrid(newpos) == 0) {
|
||||
setGrid(lastP->pos, 0); // set the old grid site to empty
|
||||
// update the position
|
||||
particleList[numParticles - 1]->pos[0] = newpos[0];
|
||||
particleList[numParticles - 1]->pos[1] = newpos[1];
|
||||
particleList[this->particleList.size() - 1]->pos[0] = newpos[0];
|
||||
particleList[this->particleList.size() - 1]->pos[1] = newpos[1];
|
||||
setGrid(lastP->pos, 1); // set the new grid site to be occupied
|
||||
|
||||
// check if we stick
|
||||
if (checkStick()) {
|
||||
//cout << "stick" << endl;
|
||||
if (checkStick() && this->rgen.random01() <= this->stickProbability) {
|
||||
this->csv_out << frame << "," << lastP->pos[0] << "," << lastP->pos[1] << endl;
|
||||
setParticleInactive(); // make the particle inactive (stuck)
|
||||
updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc.
|
||||
|
||||
if (numParticles % 100 == 0 && logfile.is_open()) {
|
||||
logfile << numParticles << " " << clusterRadius << endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// if we get to here then we are trying to move to an occupied site
|
||||
// (this should never happen as long as the sticking probability is 1.0)
|
||||
cout << "reject " << rr << endl;
|
||||
cout << lastP->pos[0] << " " << lastP->pos[1] << endl;
|
||||
//cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl;
|
||||
//printOccupied();
|
||||
}
|
||||
}
|
||||
|
||||
// check if the last particle should stick (to a neighbour)
|
||||
int DLASystem::checkStick() {
|
||||
Particle *lastP = particleList[numParticles - 1];
|
||||
Particle *lastP = particleList[this->particleList.size() - 1];
|
||||
int result = 0;
|
||||
// loop over neighbours
|
||||
for (int i = 0; i < 4; i++) {
|
||||
double checkpos[2];
|
||||
std::array<double, 2> checkpos{};
|
||||
setPosNeighbour(checkpos, lastP->pos, i);
|
||||
// if the neighbour is occupied...
|
||||
if (readGrid(checkpos) == 1)
|
||||
@ -239,78 +155,44 @@ int DLASystem::checkStick() {
|
||||
|
||||
|
||||
// constructor
|
||||
DLASystem::DLASystem(Window *set_win) {
|
||||
cout << "creating system, gridSize " << gridSize << endl;
|
||||
win = set_win;
|
||||
numParticles = 0;
|
||||
endNum = 1000;
|
||||
DLASystem::DLASystem(Config config)
|
||||
: stickProbability(config.stickProbability),
|
||||
grid(gridSize * gridSize),
|
||||
csv_out(std::move(config.csv)),
|
||||
endNum(config.maxParticles),
|
||||
frame(0),
|
||||
running(false),
|
||||
lastParticleIsActive(false) {
|
||||
cout << "GridSize: " << gridSize << endl;
|
||||
rgen.setSeed(config.seed);
|
||||
|
||||
// allocate memory for the grid, remember to free the memory in destructor
|
||||
grid = new int *[gridSize];
|
||||
for (int i = 0; i < gridSize; i++) {
|
||||
grid[i] = new int[gridSize];
|
||||
}
|
||||
slowNotFast = 1;
|
||||
// reset initial parameters
|
||||
Reset();
|
||||
/*
|
||||
* Handle grid data structure.
|
||||
* */
|
||||
// Add particle to the centre of the grid to start
|
||||
std::array<double, 2> pos{0.0, 0.0};
|
||||
addParticle(pos);
|
||||
|
||||
// Make sure to include the central location in our csv.
|
||||
this->csv_out << 0 << "," << 0 << "," << 0 << std::endl;
|
||||
|
||||
/*
|
||||
* System behaviour parameters
|
||||
* */
|
||||
addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius
|
||||
killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle
|
||||
|
||||
// this opens a logfile, if we want to...
|
||||
//logfile.open("opfile.txt");
|
||||
addCircle = 10;
|
||||
killCircle = 2.0 * addCircle;
|
||||
clusterRadius = 0.0;
|
||||
}
|
||||
|
||||
// destructor
|
||||
DLASystem::~DLASystem() {
|
||||
// strictly we should not print inside the destructor but never mind...
|
||||
cout << "deleting system" << endl;
|
||||
// delete the particles
|
||||
clearParticles();
|
||||
// delete the grid
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
delete[] grid[i];
|
||||
delete[] grid;
|
||||
particleList.clear();
|
||||
|
||||
if (logfile.is_open())
|
||||
logfile.close();
|
||||
}
|
||||
|
||||
|
||||
// this draws the system
|
||||
void DLASystem::DrawSquares() {
|
||||
|
||||
// draw the particles
|
||||
double halfSize = 0.5;
|
||||
for (int p = 0; p < numParticles; p++) {
|
||||
double *vec = particleList[p]->pos;
|
||||
glPushMatrix();
|
||||
if (p == numParticles - 1 && lastParticleIsActive == 1)
|
||||
glColor4fv(colours::red);
|
||||
else if (p == 0)
|
||||
glColor4fv(colours::green);
|
||||
else
|
||||
glColor4fv(colours::blue);
|
||||
glRectd(drawScale * (vec[0] - halfSize),
|
||||
drawScale * (vec[1] - halfSize),
|
||||
drawScale * (vec[0] + halfSize),
|
||||
drawScale * (vec[1] + halfSize));
|
||||
glPopMatrix();
|
||||
if (csv_out.is_open()) {
|
||||
csv_out.flush();
|
||||
csv_out.close();
|
||||
}
|
||||
|
||||
// print some information (at top left)
|
||||
// this ostringstream is a way to create a string with numbers and words (similar to cout << ... )
|
||||
ostringstream str;
|
||||
str << "num " << numParticles << " size " << clusterRadius;
|
||||
|
||||
// print the string
|
||||
win->displayString(str, -0.9, 0.9, colours::red);
|
||||
|
||||
// if we are paused then print this (at bottom left)
|
||||
if (running == 0) {
|
||||
ostringstream pauseStr;
|
||||
pauseStr << "paused";
|
||||
win->displayString(pauseStr, -0.9, -0.9, colours::red);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
106
DLASystem.h
106
DLASystem.h
@ -1,37 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <GLUT/glut.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdio.h>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
#define _USE_MATH_DEFINES
|
||||
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "Window.h"
|
||||
#include "Particle.h"
|
||||
#include "rnd.h"
|
||||
|
||||
using namespace std;
|
||||
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
|
||||
|
||||
Window *win; // window in which the system is running
|
||||
|
||||
// list of particles
|
||||
vector<Particle *> particleList;
|
||||
int numParticles;
|
||||
|
||||
// delete particles and clear the particle list
|
||||
void clearParticles();
|
||||
std::vector<Particle *> particleList;
|
||||
|
||||
// size of cluster
|
||||
double clusterRadius;
|
||||
@ -39,19 +37,17 @@ private:
|
||||
double addCircle;
|
||||
double killCircle;
|
||||
|
||||
double stickProbability;
|
||||
|
||||
// size of grid
|
||||
static const int gridSize = 1600;
|
||||
int **grid; // this will be a 2d array that stores whether each site is occupied
|
||||
|
||||
// the window draws only part of the grid, viewSize controls how much...
|
||||
double viewSize;
|
||||
double drawScale;
|
||||
std::vector<int> grid;
|
||||
|
||||
// random number generator, class name is rnd, instance is rgen
|
||||
rnd rgen;
|
||||
|
||||
// output file (not used at the moment)
|
||||
ofstream logfile;
|
||||
std::ofstream csv_out;
|
||||
|
||||
// number of particles at which the simulation will stop
|
||||
// (the value is set in constructor)
|
||||
@ -61,77 +57,52 @@ private:
|
||||
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();
|
||||
|
||||
// draw particles as squares
|
||||
void DrawSquares();
|
||||
void update();
|
||||
|
||||
// is the simulation running (1) or paused (0) ?
|
||||
int running;
|
||||
|
||||
// slowNotFast is +1 for slow running, 0 for fast
|
||||
int slowNotFast;
|
||||
bool running;
|
||||
|
||||
// lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0
|
||||
int lastParticleIsActive;
|
||||
|
||||
// constructor
|
||||
DLASystem(Window *set_win);
|
||||
explicit DLASystem(Config config);
|
||||
|
||||
// destructor
|
||||
~DLASystem();
|
||||
|
||||
// delete all particles and reset
|
||||
void Reset();
|
||||
|
||||
// this sets the seed for the random numbers
|
||||
void setSeed(int s) { rgen.setSeed(s); }
|
||||
|
||||
// check whether we should stop (eg the cluster has reached the edge of the grid)
|
||||
int checkStop();
|
||||
void setRunning() { running = true; }
|
||||
|
||||
// stop/start the algorithm
|
||||
void setRunning() { if (checkStop() == 0) running = 1; }
|
||||
|
||||
void pauseRunning() { running = 0; }
|
||||
|
||||
// set whether it runs fast or slow
|
||||
void setSlow() { slowNotFast = 1; }
|
||||
|
||||
void setFast() { slowNotFast = 0; }
|
||||
|
||||
void setSuperFast() { slowNotFast = -1; }
|
||||
|
||||
// set which part of the grid is visible on the screen
|
||||
// basically the screen shows co-ordinates -vv < x < vv
|
||||
// where vv is the input value
|
||||
void setViewSize(double vv) {
|
||||
viewSize = vv;
|
||||
drawScale = 2.0 / viewSize;
|
||||
}
|
||||
|
||||
// if the killcircle is almost as big as the view then increase the view
|
||||
void updateViewSize();
|
||||
|
||||
// set the view to be the approx size of the addCircle
|
||||
void viewAddCircle();
|
||||
void pauseRunning() { running = false; }
|
||||
|
||||
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
|
||||
void updateClusterRadius(double pos[]);
|
||||
void updateClusterRadius(std::array<double, 2> pos);
|
||||
|
||||
// set and read grid entries associated with a given position
|
||||
void setGrid(double pos[], int val);
|
||||
void setGrid(std::array<double, 2> pos, int val);
|
||||
|
||||
int readGrid(double pos[]);
|
||||
int readGrid(std::array<double, 2> pos);
|
||||
|
||||
// return the distance of a given point from the origin
|
||||
double distanceFromOrigin(double pos[]) {
|
||||
double distanceFromOrigin(std::array<double, 2> pos) {
|
||||
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
|
||||
}
|
||||
|
||||
@ -141,14 +112,14 @@ public:
|
||||
void setParticleInactive() { lastParticleIsActive = 0; }
|
||||
|
||||
// add a particle at pos
|
||||
void addParticle(double 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(double setpos[], double pos[], int val);
|
||||
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
|
||||
@ -158,11 +129,4 @@ public:
|
||||
// check whether the last particle should stick
|
||||
// currently it sticks whenever it touches another particle
|
||||
int checkStick();
|
||||
|
||||
// set the background colour for the window
|
||||
// it would be better for an OOP philosophy to make these member functions for the Window class
|
||||
// but we are being a bit lazy here
|
||||
void setWinBackgroundWhite() { glClearColor(1.0, 1.0, 1.0, 1.0); }
|
||||
|
||||
void setWinBackgroundBlack() { glClearColor(0.0, 0.0, 0.0, 0.0); }
|
||||
};
|
||||
|
||||
4
Makefile
4
Makefile
@ -10,11 +10,11 @@
|
||||
|
||||
CXX = clang++
|
||||
|
||||
CXXFLAGS = -Wall -Wextra -g -O0
|
||||
CXXFLAGS = -Wall -Wextra -g -O0 -std=c++20 -stdlib=libc++
|
||||
|
||||
IFLAGS = -I/usr/local/include -I/usr/include
|
||||
|
||||
LFLAGS = -L/usr/local/lib -lm -framework OpenGL -framework GLUT
|
||||
LFLAGS = -L/usr/local/lib -lm
|
||||
|
||||
# ------------------------------------------
|
||||
# FOR GENERIC MAKEFILE:
|
||||
|
||||
20
Particle.h
20
Particle.h
@ -1,22 +1,14 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
|
||||
class Particle {
|
||||
public:
|
||||
static const int dim = 2; // we are in two dimensions
|
||||
double *pos; // pointer to an array of size dim, to store the position
|
||||
std::array<double, dim> pos{};
|
||||
|
||||
// default constructor
|
||||
Particle() {
|
||||
pos = new double[dim];
|
||||
// constructor, with a specified initial pos
|
||||
explicit Particle(std::array<double, dim> pos) {
|
||||
this->pos[0] = pos[0];
|
||||
this->pos[1] = pos[1];
|
||||
}
|
||||
|
||||
// constructor, with a specified initial position
|
||||
Particle(double set_pos[]) {
|
||||
pos = new double[dim];
|
||||
for (int d = 0; d < dim; d++)
|
||||
pos[d] = set_pos[d];
|
||||
}
|
||||
|
||||
// destructor
|
||||
~Particle() { delete[] pos; }
|
||||
};
|
||||
|
||||
27
Window.cpp
27
Window.cpp
@ -1,27 +0,0 @@
|
||||
#include "Window.h"
|
||||
|
||||
// constructor
|
||||
Window::Window(int set_size[], string &set_title) {
|
||||
size[0] = set_size[0];
|
||||
size[1] = set_size[1];
|
||||
title = set_title;
|
||||
|
||||
locateOnScreen();
|
||||
glutInitWindowSize(size[0], size[1]);
|
||||
glutInitWindowPosition(pos[0], pos[1]);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutCreateWindow(title.c_str());
|
||||
|
||||
// sets the background to black
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
// print a string at a given position, don't worry about how this works...
|
||||
void Window::displayString(ostringstream &str, double x, double y, GLfloat col[]) {
|
||||
string localString = str.str();
|
||||
glColor4fv(col);
|
||||
glRasterPos2d(x, y);
|
||||
for (int i = 0; i < localString.length(); i++) {
|
||||
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]);
|
||||
}
|
||||
}
|
||||
34
Window.h
34
Window.h
@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <GLUT/glut.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Window {
|
||||
public:
|
||||
string title;
|
||||
int size[2];
|
||||
int pos[2];
|
||||
|
||||
void locateOnScreen() {
|
||||
// the fx sets where on the screen the window will appear
|
||||
// (values should be between 0 and 1)
|
||||
double fx[] = {0.7, 0.5};
|
||||
pos[0] = (glutGet(GLUT_SCREEN_WIDTH) - size[0]) * fx[0];
|
||||
pos[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1];
|
||||
}
|
||||
|
||||
// constructor, size is in pixels
|
||||
Window(int set_size[], string &set_title);
|
||||
|
||||
// function which prints a string to the screen, at a given position, with a given color
|
||||
// note position is "absolute", not easy to get two strings spaced one above each other like this
|
||||
void displayString(ostringstream &str, double x, double y, GLfloat col[]);
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
3
harness.bash
Executable file
3
harness.bash
Executable file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
parallel "../run {1} {2} {3}" ::: $(seq 0 10) ::: $(seq 0 0.05 1) ::: $(seq 2000 1000 10000)
|
||||
194
mainDLA.cpp
194
mainDLA.cpp
@ -1,172 +1,58 @@
|
||||
#include <GLUT/glut.h>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
|
||||
#include "DLASystem.h"
|
||||
#include "Window.h"
|
||||
|
||||
using namespace std;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
// functions which are needed for openGL go into a namespace so that we can identify them
|
||||
namespace drawFuncs {
|
||||
void handleKeypress(unsigned char key, int x, int y);
|
||||
/*
|
||||
* In a proper project I would write a better argument parser, don't care here, just exit with an error if it is wrong.
|
||||
* */
|
||||
Config::Config(int argc, char **argv) {
|
||||
if (argc != 4) {
|
||||
exit(1);
|
||||
} else {
|
||||
this->seed = std::stoi(argv[1]);
|
||||
this->stickProbability = std::stod(argv[2]);
|
||||
this->maxParticles = std::stoi(argv[3]);
|
||||
|
||||
void display(void);
|
||||
if (stickProbability <= 0 || stickProbability > 1) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void update(int val);
|
||||
std::stringstream str;
|
||||
// Ensure the output file name contains all information required to replicate data
|
||||
str << "./out-" << seed << '-' << stickProbability << "-" << maxParticles << ".csv";
|
||||
std::ofstream csv_out(str.str());
|
||||
this->csv = std::move(csv_out);
|
||||
|
||||
void introMessage();
|
||||
// Add headers to csv output
|
||||
this->csv << "frame" << "," << "x" << "," << "y" << std::endl;
|
||||
|
||||
cout <<
|
||||
"seed: " << seed << ", " <<
|
||||
"stickProbability: " << stickProbability << ", " <<
|
||||
"maxParticles: " << maxParticles <<
|
||||
endl;
|
||||
}
|
||||
}
|
||||
|
||||
// this is a global pointer, which is how we access the system itself
|
||||
DLASystem *sys;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// turn on glut
|
||||
glutInit(&argc, argv);
|
||||
Config config(argc, argv);
|
||||
|
||||
int window_size[] = {480, 480};
|
||||
string window_title("simple DLA simulation");
|
||||
// Create the system
|
||||
auto *sys = new DLASystem(std::move(config));
|
||||
sys->setRunning();
|
||||
|
||||
// create a window
|
||||
Window *win = new Window(window_size, window_title);
|
||||
|
||||
// create the system
|
||||
sys = new DLASystem(win);
|
||||
|
||||
// this is the seed for the random numbers
|
||||
int seed = 6;
|
||||
cout << "setting seed " << seed << endl;
|
||||
sys->setSeed(seed);
|
||||
|
||||
// print the "help" message to the console
|
||||
drawFuncs::introMessage();
|
||||
|
||||
// tell openGL how to redraw the screen and respond to the keyboard
|
||||
glutDisplayFunc(drawFuncs::display);
|
||||
glutKeyboardFunc(drawFuncs::handleKeypress);
|
||||
|
||||
// tell openGL to do its first update after waiting 10ms
|
||||
int wait = 10;
|
||||
int val = 0;
|
||||
glutTimerFunc(wait, drawFuncs::update, val);
|
||||
|
||||
// start the openGL stuff
|
||||
glutMainLoop();
|
||||
/*
|
||||
* NOTE: We run at max speed as rendering is handled by a different engine so we don't need to care.
|
||||
* */
|
||||
while (sys->running) {
|
||||
sys->update();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// this is just a help message
|
||||
void drawFuncs::introMessage() {
|
||||
cout << "Keys (while in graphics window):" << endl << " q or e to quit (or exit)" << endl;
|
||||
cout << " h to print this message (help)" << endl;
|
||||
cout << " u for a single update" << endl;
|
||||
cout << " g to start running (go)" << endl;
|
||||
cout << " p to pause running" << endl;
|
||||
cout << " s to run in slow-mode" << endl;
|
||||
cout << " f to run in fast-mode" << endl;
|
||||
cout << " r to clear everything (reset)" << endl;
|
||||
cout << " z to pause and zoom in" << endl;
|
||||
cout << " w or b to change background colour to white or black" << endl;
|
||||
}
|
||||
|
||||
// openGL function deals with the keyboard
|
||||
void drawFuncs::handleKeypress(unsigned char key, int x, int y) {
|
||||
switch (key) {
|
||||
case 'h':
|
||||
drawFuncs::introMessage();
|
||||
break;
|
||||
case 'q':
|
||||
case 'e':
|
||||
cout << "Exiting..." << endl;
|
||||
// delete the system
|
||||
delete sys;
|
||||
exit(0);
|
||||
break;
|
||||
case 'p':
|
||||
cout << "pause" << endl;
|
||||
sys->pauseRunning();
|
||||
break;
|
||||
case 'g':
|
||||
cout << "go" << endl;
|
||||
sys->setRunning();
|
||||
glutTimerFunc(0, drawFuncs::update, 0);
|
||||
break;
|
||||
case 's':
|
||||
cout << "slow" << endl;
|
||||
sys->setSlow();
|
||||
break;
|
||||
case 'w':
|
||||
cout << "white" << endl;
|
||||
sys->setWinBackgroundWhite();
|
||||
break;
|
||||
case 'b':
|
||||
cout << "black" << endl;
|
||||
sys->setWinBackgroundBlack();
|
||||
break;
|
||||
case 'f':
|
||||
cout << "fast" << endl;
|
||||
sys->setFast();
|
||||
break;
|
||||
case 'r':
|
||||
cout << "reset" << endl;
|
||||
sys->Reset();
|
||||
break;
|
||||
case 'z':
|
||||
cout << "zoom" << endl;
|
||||
sys->pauseRunning();
|
||||
sys->viewAddCircle();
|
||||
break;
|
||||
case 'u':
|
||||
cout << "upd" << endl;
|
||||
sys->Update();
|
||||
break;
|
||||
}
|
||||
// tell openGL to redraw the window
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
// this function gets called whenever the algorithm should do its update
|
||||
void drawFuncs::update(int val) {
|
||||
int wait; // time to wait between updates (milliseconds)
|
||||
|
||||
if (sys->running) {
|
||||
if (sys->slowNotFast == 1)
|
||||
wait = 10;
|
||||
else
|
||||
wait = 0;
|
||||
|
||||
sys->Update();
|
||||
|
||||
// tell openGL to call this funtion again after "wait" milliseconds
|
||||
glutTimerFunc(wait, drawFuncs::update, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// this function redraws the window when necessary
|
||||
void drawFuncs::display() {
|
||||
// Clear the window or more specifically the frame buffer...
|
||||
// This happens by replacing all the contents of the frame
|
||||
// buffer by the clear color (black in our case)
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// this puts the camera at the origin (not sure why) with (I think) z axis out of page and y axis up
|
||||
// there is also the question of the GL perspective which is not set up in any clear way at the moment
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(0.0, 0.0, 1.0, /* camera position */
|
||||
0.0, 0.0, -1.0, /* point to look at */
|
||||
0.0, 1.0, 0.0); /* up direction */
|
||||
|
||||
//sys->DrawSpheres();
|
||||
sys->DrawSquares();
|
||||
|
||||
// Swap contents of backward and forward frame buffers
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
|
||||
1001
out-2-0.5-1000.csv
Normal file
1001
out-2-0.5-1000.csv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user