-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
297 lines (241 loc) · 8.6 KB
/
simulator.cpp
File metadata and controls
297 lines (241 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "simulator.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cmath>
#define PI 3.14159265359
Simulator::Simulator(int numPoints, double lambda, double sigma, int _seed){
radius = 0.0;
seed = _seed;
startTime = std::chrono::system_clock::now();
// initialize the randomizers
mt_rand.seed(seed);
expDist = std::exponential_distribution<double>(lambda);
normDist = std::normal_distribution<double>(0,sigma);
uniformDist = std::uniform_real_distribution<double>(0.,1.);
intDist = std::uniform_int_distribution<int>(0,numPoints-1);
for(int i = 0; i < numPoints; i++){
#ifdef sphere
points.emplace_back(2*PI*uniformDist(mt_rand), acos(2*uniformDist(mt_rand)-1));
#endif
#ifdef torus
//http://math.stackexchange.com/questions/2017079/uniform-random-points-on-a-torus
double theta = 2*PI*uniformDist(mt_rand);
double phi = 2*PI*uniformDist(mt_rand);
if(3+cos(theta)/4>=uniformDist(mt_rand))
points.emplace_back(theta, phi);
else
i--;
#endif
}
}
bool Simulator::movePoint(){
double dPhi, dTheta;
int rand = intDist(mt_rand);
double oPhi = points[rand].sph.phi;
double oTheta = points[rand].sph.theta;
dPhi = normDist(mt_rand);
dTheta = normDist(mt_rand);
points[rand].move(dPhi,dTheta);
if(!hasCollisionSingle(&points[rand])){
#ifdef sphere
double probability = std::abs(sin(points[rand].sph.theta)/sin(oTheta));
#endif
#ifdef torus
double probability = std::abs((3+cos(points[rand].sph.theta))/(3+cos(oTheta)));
#endif
if(probability>uniformDist(mt_rand))
return true;
}
points[rand].sph.phi = oPhi;
points[rand].sph.theta = oTheta;
points[rand].transformCoordinates();
return false;
}
bool Simulator::increaseRadius(){
double oldRadius = radius;
radius += expDist(mt_rand);
if(this->hasCollision()){
radius = oldRadius;
return false;
}
return true;
}
bool Simulator::hasCollision(){
for(int i = 0; i < points.size(); i++){
for(int j = i + 1; j < points.size(); j++){
if(points[i].collidesWith(&points[j], radius)) return true;
}
}
return false;
}
void Simulator::saveCoordsToFile(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"coordinatesRAW.dat", std::ios::trunc);
myfile << "#Phi\tTheta\tx\ty\tz\tr\n";
for(auto &point: points){
myfile << std::fixed << std::setprecision(5) << point.sph.phi<< "\t" << point.sph.theta << "\t" << point.cart.x << "\t" << point.cart.y << "\t" << point.cart.z << "\t" << radius << "\n";
}
myfile.close();
}
void Simulator::saveCoordsToFileOpengl(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"coordinatesOGL.dat", std::ios::trunc);
myfile << points.size() << std::endl;
#ifdef sphere
myfile << "-2 2" << std::endl;
myfile << "-2 2" << std::endl;
myfile << "-2 2" << std::endl;
#endif
#ifdef torus
myfile << "-4 4" << std::endl;
myfile << "-4 4" << std::endl;
myfile << "-4 4" << std::endl;
#endif
for(auto &point: points){
myfile << std::fixed << std::setprecision(7) << point.cart.x << " " << point.cart.y << " " << point.cart.z
<< " " << 2*radius << "\n";
}
myfile.close();
}
void Simulator::saveCoordsToFileOpenglColourTouch(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"coordinatesOGLColor.dat", std::ios::trunc);
myfile << points.size() << std::endl;
#ifdef sphere
myfile << "-2 2" << std::endl;
myfile << "-2 2" << std::endl;
myfile << "-2 2" << std::endl;
#endif
#ifdef torus
myfile << "-4 4" << std::endl;
myfile << "-4 4" << std::endl;
myfile << "-4 4" << std::endl;
#endif
for(auto &point: points){
myfile << std::fixed << std::setprecision(7) << point.cart.x << " " << point.cart.y << " " << point.cart.z
<< " " << 2*radius << " " << countNeighbours(&point) << "\n";
}
myfile.close();
}
void Simulator::saveCoordsToFileQhull(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"coordinatesQhull.dat", std::ios::trunc);
myfile << 3 << "Diameter=" << 2*radius << std::endl;
//add artificial point at 0; torus needs more additional points or a different method
myfile << points.size()+1 << std::endl;
myfile << std::fixed << std::setprecision(7) << 0.0 << " " << 0.0 << " " << 0.0 << "\n";
for(auto &point: points){
myfile << std::fixed << std::setprecision(7) << point.cart.x << " " << point.cart.y << " " << point.cart.z << "\n";
}
myfile.close();
}
void Simulator::saveObservablesToFile(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"Observables.dat", std::ios::trunc);
double exactPackingDensity = 0;
#ifdef sphere
exactPackingDensity = points.size()*2*PI*(1-radius*radius)*(1-sqrt(1-radius*radius))/(4*PI*(1-radius*radius));
#endif
#ifdef sphere
myfile << "type=sphere" << std::endl;
#endif
#ifdef torus
myfile << "type=torus" << std::endl;
#endif
myfile << std::fixed << std::setprecision(19) << "seed=" << seed << std::endl;
myfile << std::fixed << std::setprecision(19) << "numParticles=" << points.size() << std::endl;
myfile << std::fixed << std::setprecision(19) << "Radius=" << 2*radius << std::endl;
myfile << std::fixed << std::setprecision(19) << "discPackingDensity=" << discPackingDensity() << std::endl;
myfile << std::fixed << std::setprecision(19) << "exactPackingDensity=" << exactPackingDensity << std::endl;
myfile << std::fixed << std::setprecision(19) << "MCPackingDensity=" << MCpackingDensity(10000000) << std::endl;
myfile.close();
}
void Simulator::saveTimestamp(std::string folderPath){
std::ofstream myfile;
myfile.open (folderPath+"timestamp.dat", std::ios::trunc);
myfile << std::fixed << std::setprecision(19) << elapsedTime() << std::endl;
myfile.close();
}
bool Simulator::hasCollisionSingle(Point * refPoint){
for(auto &point: points){
if(point.collidesWith(refPoint, radius)) return true;
}
return false;
}
int Simulator::countNeighbours(Point * refPoint){
int counter = -1; //distance to self is 0
double epsilon = 2.01 * radius;
for(auto &point: points){
if(point.getDistance(refPoint) < epsilon) counter++;
}
return counter;
}
double Simulator::discPackingDensity(){
#ifdef sphere
double totalArea = 4*PI;
#endif
#ifdef torus
double totalArea = 4*PI*PI*3;
#endif
double particleArea = radius*radius*PI*points.size();
return particleArea/totalArea;
}
double Simulator::MCpackingDensity(long totalSamples){
long particleSamples = 0;
for(long i = 0; i < totalSamples; i++){
#ifdef sphere
double phi = 2*PI*uniformDist(mt_rand);
double theta = acos(2*uniformDist(mt_rand)-1);
#endif
#ifdef torus
//http://math.stackexchange.com/questions/2017079/uniform-random-points-on-a-torus
double theta, phi = 2*PI*uniformDist(mt_rand);
do {
theta = 2*PI*uniformDist(mt_rand);
} while(3+cos(theta)/4<uniformDist(mt_rand));
#endif
Point * samplePoint = new Point(phi,theta);
for(auto &point: points){
if(point.collidesWith(samplePoint, radius/2)){
particleSamples++; break;
}
}
delete samplePoint;
}
return (double)particleSamples/(double)totalSamples;
}
void Simulator::printReport(){
double mcDensity = MCpackingDensity(10000000);
std::cerr << points.size() << " simple disc packing density: " << std::fixed << std::setprecision(19) << discPackingDensity() << std::endl;
std::cerr << points.size() << " MC packing density: " << std::fixed << std::setprecision(19) << mcDensity << std::endl;
#ifdef sphere
double exactPackingDensity = points.size()*2*PI*(1-radius*radius)*(1-sqrt(1-radius*radius))/(4*PI*(1-radius*radius));
std::cerr << std::fixed << std::setprecision(19) << "Packing density with correction factor: " << exactPackingDensity << std::endl;
#endif
std::cerr << std::fixed << std::setprecision(19) << "Final Radius for " << points.size() << " Particles: " << radius << " Diameter: " << 2*radius << std::endl;
}
double Simulator::elapsedTime(){
elapsed_seconds = std::chrono::system_clock::now()-startTime;
return elapsed_seconds.count();
}
void Simulator::saveFiles(long int i){
std::stringstream folderPathSS;
#ifdef sphere
folderPathSS << "data/sphere/numParticles:" << points.size() << "/seed:" << seed << "/iteration:" << std::setw(10) << std::setfill('0') << i << "/";
#endif
#ifdef torus
folderPathSS << "data/torus/numParticles:" << points.size() << "/seed:" << seed << "/iteration:" << std::setw(10) << std::setfill('0') << i << "/";
#endif
std::string folderPath = folderPathSS.str();
std::stringstream mkdirSS;
mkdirSS << "mkdir -p " << folderPath;
int dummy = system(mkdirSS.str().c_str());
saveCoordsToFile(folderPath);
saveCoordsToFileOpengl(folderPath);
saveCoordsToFileOpenglColourTouch(folderPath);
saveCoordsToFileQhull(folderPath);
saveObservablesToFile(folderPath);
saveTimestamp(folderPath);
}