-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
413 lines (369 loc) · 10.9 KB
/
main.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "main.h"
#define M_Pi 3.14159265358f
void CalculateStep(cplx_t* wf, const flt_t* v, const bool* mask, const flt_t dt, const flt_t dx, const flt_t& bec_as);
cplx_t* k1 = new cplx_t[w * h]; //runge kutta steps
cplx_t* k2 = new cplx_t[w * h];
cplx_t* k3 = new cplx_t[w * h];
cplx_t* k4 = new cplx_t[w * h];
cplx_t* kSub = new cplx_t[w * h]; //runge kutta substep
const flt_t potentialScaling = 100;
int main()
{
bool render_frames = true;
int frame = 0;
int old_frame = -1;
sf::RenderWindow window(sf::VideoMode(w, h), "Qm Sim");
reload: //sue me...
std::ios_base::sync_with_stdio(false);
cplx_t* wf = new cplx_t[w * h]; //wave function
flt_t* v = new flt_t[w * h]; //potential
bool* mask = new bool[w * h]; //dirichlet condition mask. false=infinity
constexpr flt_t dx = 0.1;
constexpr flt_t dt = dx * dx / 2.0;
flt_t bec_as = 1.0;
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
{
wf[y * w + x] = cplx_t(static_cast<flt_t>(0.0), static_cast<flt_t>(0.0));
k1[y * w + x] = cplx_t();
k2[y * w + x] = cplx_t();
k3[y * w + x] = cplx_t();
k4[y * w + x] = cplx_t();
kSub[y * w + x] = cplx_t();
}
flt_t* p_x;
flt_t* p_y;
int p_num = 0;
flt_t p_m = 40.0;
if (!LoadWFScenario(wf, &p_x, &p_y, &p_num, &p_m, &bec_as))
{
std::cout << "Error while reading wf scenario file" << std::endl;
std::system("pause");
return 0;
}
std::vector<uint8_t> imBuffer, image; //for image loadings
std::string maskFile = "mask.png";
std::string potentialFile = "potential.png";
unsigned long pngW, pngH;
//load dirichlet image
//loadFile2(imBuffer, maskFile);
imBuffer = load_file(maskFile);
int error = decodePNG(image, pngW, pngH, imBuffer.empty() ? 0 : &imBuffer[0], (unsigned long)imBuffer.size());
if(error != 0 || pngW != w || pngH != h)
{
std::cout << "error: " << error << std::endl;
std::system("pause");
return error;
}
for (int i = 0; i < w * h * 4; i += 4)
{
mask[i / 4] = (static_cast<unsigned int>(image[i])
+ static_cast<unsigned int>(image[i + 1])
+ static_cast<unsigned int>(image[i + 2])) == 0;
if (mask[i / 4])
wf[i / 4] = 0;
}
//load potential image
//loadFile2(imBuffer, potentialFile);
imBuffer = load_file(potentialFile);
error = decodePNG(image, pngW, pngH, imBuffer.empty() ? 0 : &imBuffer[0], (unsigned long)imBuffer.size());
if(error != 0 || pngW != w || pngH != h)
{
std::cout << "error: " << error << std::endl;
std::system("pause");
return error;
}
for (int i = 0; i < w * h * 4; i += 4)
v[i / 4] = (static_cast<flt_t>(static_cast<unsigned int>(image[i])
+ static_cast<unsigned int>(image[i + 1])
+ static_cast<unsigned int>(image[i + 2])) / static_cast<flt_t>(255.0 * 3.0)) * potentialScaling;
sf::Texture texture;
texture.create(w, h);
texture.setSmooth(false);
texture.setRepeated(false);
uint8_t* buffer = new uint8_t[w * h * 4];
for (int y = 0; y < h; ++y)
for (int x = 3; x < w * 4; x += 4)
buffer[y * w * 4 + x] = 255;
sf::Sprite sprite;
CalculateStep(wf, v, mask, dt, dx, bec_as);
bool inCalculation = false;
while (window.isOpen())
{
sf::Event event;
bool windowWasClosed = false;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
windowWasClosed = true;
break;
}
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
windowWasClosed = true;
break;
}
else if (event.key.code == sf::Keyboard::Delete)
{
inCalculation = false;
goto reload; //I SAID SUE ME!!
}
inCalculation = !inCalculation;
}
}
if (windowWasClosed)
break;
if (inCalculation)
{
for (int st = 0; st < imidiateSteps; ++st)
{
CalculateStep(wf, v, mask, dt, dx, bec_as);
for (int p = 0; p < p_num; ++p)
{
flt_t temp_px = p_x[p];
flt_t temp_py = p_y[p];
Calculate_Particle_Step(wf, &temp_px, &temp_py, dt, dx, p_m);
p_x[p] = temp_px;
p_y[p] = temp_py;
}
}
++frame;
/*int mX = 1, mY = 1;
FindModes(wf, mX, mY);*/
}
flt_t maxReFactor = static_cast<flt_t>(0.0), maxImFactor = static_cast<flt_t>(0.0), maxNormFactor = static_cast<flt_t>(0.0);
for (int y = 0; y < h; ++y)
{
int offset = y * w;
for (int x = 0; x < w; ++x)
{
if (std::abs(wf[offset + x].real()) > maxReFactor)
maxReFactor = std::abs(wf[offset + x].real());
if (std::abs(wf[offset + x].imag()) > maxImFactor)
maxImFactor = std::abs(wf[offset + x].imag());
if (std::norm(wf[offset + x]) > maxNormFactor)
maxNormFactor = std::norm(wf[offset + x]);
}
}
maxReFactor = static_cast<flt_t>(255.0) / maxReFactor;
maxImFactor = static_cast<flt_t>(255.0) / maxImFactor;
maxNormFactor = static_cast<flt_t>(255.0) / maxNormFactor;
#pragma omp parallel for
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
buffer[(y * w + x) * 4 ] = static_cast<uint8_t>(std::abs(wf[y * w + x].real()) * maxReFactor);
buffer[(y * w + x) * 4 + 1] = static_cast<uint8_t>(std::norm(wf[y * w + x]) * maxNormFactor);
buffer[(y * w + x) * 4 + 2] = static_cast<uint8_t>(std::abs(wf[y * w + x].imag()) * maxImFactor);
}
}
for (int p = 0; p < p_num; ++p)
{
const int x = static_cast<int>((w - 1) * p_x[p]);
const int y = static_cast<int>((h - 1) * p_y[p]);
buffer[(y * w + x) * 4 ] = 255;
buffer[(y * w + x) * 4 + 1] = 255;
buffer[(y * w + x) * 4 + 2] = 255;
}
texture.update(buffer);
sprite.setTexture(texture);
window.draw(sprite);
window.display();
if (render_frames && inCalculation && frame != old_frame)
{
old_frame = frame;
SaveBuffer(buffer);
}
}
delete[] buffer;
delete[] wf;
delete[] k1;
delete[] k2;
delete[] k3;
delete[] k4;
delete[] kSub;
delete[] v;
delete[] mask;
}
void inline KStep(cplx_t* dst, const cplx_t* src, const flt_t* v, const bool* mask, const flt_t dx, const flt_t& as)
{
flt_t spaceDerivativeQuotient = static_cast<flt_t>(1.0) / (dx * dx * static_cast<flt_t>(2.0));
#pragma omp parallel for
for (int y = 1; y < h - 1; ++y)
{
for (int x = 1; x < w - 1; ++x)
{
dst[y * w + x] = li * (
((src[y * w + x + 1] + src[y * w + x - 1] - static_cast<flt_t>(2.0) * src[y * w + x])
+ (src[(y + 1) * w + x] + src[(y - 1) * w + x] - static_cast<flt_t>(2.0) * src[y * w + x])) * spaceDerivativeQuotient
- v[y * w + x] * src[y * w + x]
- std::norm(src[y * w + x]) * src[y * w + x] * static_cast<flt_t>(2.0 * M_PI * as)
);
}
}
#pragma omp parallel for
for (int y = 1; y < h - 1; ++y)
for (int x = 1; x < w - 1; ++x)
if (mask[y * w + x])
dst[y * w + x] = 0;
}
void inline CalculateStep(cplx_t* wf, const flt_t* v, const bool* mask, const flt_t dt, const flt_t dx, const flt_t& bec_as)
{
KStep(k1, wf, v, mask, dx, bec_as);
#pragma omp parallel for
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
kSub[y * w + x] = (dt / static_cast<flt_t>(2.0)) * k1[y * w + x] + wf[y * w + x];
KStep(k2, kSub, v, mask, dx, bec_as);
#pragma omp parallel for
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
kSub[y * w + x] = (dt / static_cast<flt_t>(2.0)) * k2[y * w + x] + wf[y * w + x];
KStep(k3, kSub, v, mask, dx, bec_as);
#pragma omp parallel for
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
kSub[y * w + x] = dt * k3[y * w + x] + wf[y * w + x];
KStep(k4, kSub, v, mask, dx, bec_as);
#pragma omp parallel for
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
wf[y * w + x] += (dt / static_cast<flt_t>(6.0))
* (k1[y * w + x]
+ static_cast<flt_t>(2.0) * k2[y * w + x]
+ static_cast<flt_t>(2.0) * k3[y * w + x]
+ k4[y * w + x]);
}
void inline Calculate_Particle_Step(cplx_t* wf, flt_t* x, flt_t* y, const flt_t& dt, const flt_t& dx, const flt_t& m)
{
const int x_idx = static_cast<int>((w - 1) * (*x));
const int y_idx = static_cast<int>((h - 1) * (*y));
if (isnan(*x) || isnan(*y))
return;
//std::cout << (*x) << " " << (*y) << std::endl;
cplx_t der;
flt_t acc;
if (x_idx == 0)
der = -li * (wf[y_idx * w + x_idx + 1] - wf[y_idx * w + x_idx]) / cplx_t{dx, 0};
else if (x_idx == (w - 1))
der = -li * (wf[y_idx * w + x_idx] - wf[y_idx * w + x_idx - 1]) / cplx_t{dx, 0};
else
der = -li * (wf[y_idx * w + x_idx + 1] - wf[y_idx * w + x_idx - 1]) / cplx_t{2 * dx, 0};
acc = (der / wf[y_idx * w + x_idx]).real() * dt / m;
//std::cout << "acc " << acc << " isnan = " << (acc != acc) << std::endl;
if (!isnan(acc))
(*x) = (*x) + acc;
if (y_idx == 0)
der = -li * (wf[(y_idx + 1) * w + x_idx] - wf[y_idx * w + x_idx]) / cplx_t{dx, 0};
else if (y_idx == (h - 1))
der = -li * (wf[y_idx * w + x_idx] - wf[(y_idx - 1) * w + x_idx]) / cplx_t{dx, 0};
else
der = -li * (wf[(y_idx + 1) * w + x_idx] - wf[(y_idx - 1) * w + x_idx]) / cplx_t{2 * dx, 0};
acc = (der / wf[y_idx * w + x_idx]).real() * dt / m;
if (!isnan(acc))
(*y) += acc;
if ((*x) > 0.9999)
(*x) = 0.9999;
else if ((*x) < 0.0001)
(*x) = 0.0001;
if ((*y) > 0.9999)
(*y) = 0.9999;
else if ((*y) < 0.0001)
(*y) = 0.0001;
//std::cout << (*x) << " " << (*y) << std::endl;
}
void SaveBuffer(const uint8_t* frame_buffer)
{
static std::ofstream frameFile;
frameFile.open("frameFile.bin", std::ios_base::app | std::ios_base::binary);
frameFile.write((char*)frame_buffer, w * h * 4);
//for (int i = 0; i < w * h * 4; ++i)
// frameFile << frame_buffer[i];
frameFile.flush();
frameFile.close();
}
void inline FindModes(const cplx_t* wf, int& modesX, int& modesY)
{
static flt_t* xVals = new flt_t[w];
static flt_t* yVals = new flt_t[h];
static int lastXMode = -1;
static int lastYMode = -1;
static int lastUpdatedStep = 0;
static std::ofstream modeFile;
if (!modeFile.is_open())
modeFile.open("modefile.csv", std::ios_base::app);
flt_t xMax = 0;
flt_t yMax = 0;
for (int x = 0; x < w; ++x)
{
xVals[x] = 0;
for(int y = 0; y < h; ++y)
xVals[x] += std::abs(wf[y * w + x]);
if (xVals[x] > xMax)
xMax = xVals[x];
}
for (int y = 0; y < h; ++y)
{
yVals[y] = 0;
for(int x = 0; x < w; ++x)
yVals[y] += std::abs(wf[y * w + x]);
if (yVals[y] > yMax)
yMax = yVals[y];
}
const flt_t xUpperLimit = 0.9 * xMax;
const flt_t xLowerLimit = 0.7 * xMax;
const flt_t yUpperLimit = 0.9 * yMax;
const flt_t yLowerLimit = 0.7 * yMax;
modesX = 0;
modesY = 0;
bool gotXStreak = false, gotYStreak = false;
for (int i = 0; i < w; ++i)
{
if (gotXStreak)
{
if (xVals[i] < xLowerLimit)
gotXStreak = false;
}
else
{
if (xVals[i] > xUpperLimit)
{
gotXStreak = true;
modesX += 1;
}
}
}
for (int i = 0; i < h; ++i)
{
if (gotYStreak)
{
if (yVals[i] < yLowerLimit)
gotYStreak = false;
}
else
{
if (yVals[i] > yUpperLimit)
{
gotYStreak = true;
++modesY;
}
}
}
if (lastXMode != modesX || lastYMode != modesY)
{
modeFile << modesX << ',' << modesY << ',' << lastUpdatedStep << '\n';
lastXMode = modesX;
lastYMode = modesY;
lastUpdatedStep = 0;
}
else
{
++lastUpdatedStep;
}
}