-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheulerian.cpp
More file actions
2834 lines (2299 loc) · 103 KB
/
eulerian.cpp
File metadata and controls
2834 lines (2299 loc) · 103 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cmath>
#include <utility>
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <ctime>
#include <string>
#include <iterator>
#include <random>
#include <chrono>
#include <thread>
#include <fstream>
#include <sstream>
#include <filesystem>
//#include <minwindef.h>
//#include "window.h"
//#include <glm/glm.hpp>
//#include <glm/gtc/matrix_transform.hpp>
//#include <glm/gtc/type_ptr.hpp>
#include "dependencies/glad/glad.h"
#include "dependencies/GLFW/include/GLFW/glfw3.h"
#include <algorithm>
#include <optional>
#undef max
using namespace std;
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
//typedef void (*grav_pointer) (State*, fluid_type, fluid_type, int, int);
typedef float fluid_type;
bool sample_log(int expected_passes) {
bool ret = false;
int straw = (int)(rand() % expected_passes);
//printf("\n\tstraw:%i", straw);
if(straw < log10((double)expected_passes)) {
ret = true;
}
return ret;
}
#define U_FIELD 0
#define V_FIELD 1
#define S_FIELD 2
#define T_FIELD 3
#define RENDER_MODE_PRESSURE 0
#define RENDER_MODE_VELOCITY 1
#define RENDER_MODE_TEMPERATURE 2
#define SCENE_TYPE_TANK 0
#define SCENE_TYPE_WIND_TUNNEL 1
#define SCENE_TYPE_PAINT 2
#define S_FIELD_FLUID 1.0
#define S_FIELD_SOLID 0.0
#define MIN_TIME_TO_LOG 3.0
#define DONT_AUTO_PAUSE -1.0
#define PAUSE_AFTER 10.0
#define WHITE_OUT_UNTOUCHED_CELLS FALSE
#define DEFAULT_DT 1.0 / 60.0
#define DEFAULT_VISCOSITY 0.0
#define DEFAULT_RESOLUTION 1000
#define DEFAULT_SCENARIO SCENE_TYPE_WIND_TUNNEL
#define DEFAULT_DENSITY 1000.0
#define DEFAULT_OVER_RELAXATION 1.6
#define DEFAULT_WIND_TUNNEL_IN_VEL 2.0
#define DEFAULT_DIVERGENCE_ITERATIONS 50
#define DEFAULT_DIVERGENCE_ITERATIONS_WIND_TUNNEL 100
#define DEFAULT_COARSE_GRID_BUDGET_DIVISOR 2
#define DEFAULT_COARSE_GRID_SIDE_LENGTH_POWER 3
#define DEFAULT_MINIMUM_POW_2_CELL_DIVISIBILITY 4
#define STARTING_DT DEFAULT_DT
#define STARTING_VISCOSITY DEFAULT_VISCOSITY
#define STARTING_RESOLUTION 800
#define STARTING_SCENARIO DEFAULT_SCENARIO
#define STARTING_DENSITY DEFAULT_DENSITY
#define STARTING_OVER_RELAXATION 1.9
#define STARTING_WIND_TUNNEL_IN_VEL DEFAULT_WIND_TUNNEL_IN_VEL
#define STARTING_DIVERGENCE_ITERATIONS DEFAULT_DIVERGENCE_ITERATIONS
#define STARTING_DIVERGENCE_ITERATIONS_WIND_TUNNEL 400
#define STARTING_COARSE_GRID_BUDGET_DIVISOR 3
#define STARTING_COARSE_GRID_SIDE_LENGTH_POWER 1
#define STARTING_MINIMUM_POW_2_CELL_DIVISIBILITY 5
#define INITIAL_WINDOW_WIDTH 1920
#define INITIAL_WINDOW_HEIGHT 1080
bool print_perf = true;
int render_mode = RENDER_MODE_PRESSURE;
bool request_show_pressure = false;
bool request_show_smoke = false;
bool request_show_velocity = false;
bool request_show_streamlines = false;
bool request_show_temperature = false;
bool show_temperature_setting = false;
bool show_pressure_setting = false;
/// set by the glfw callback
bool change_mode_active = false;
string change_mode_buffer;
string viscosity_command = "VISCOSITY";
string divergence_iters_command = "DIVERGENCE";
string pulse_command = "PULSE"; //creates a pulse of pressure along the left side of the world with specified x vel
string grav_power_command = "GRAV";
#define COMMAND_VISCOSITY "VISCOSITY"
#define COMMAND_DIVERGENCE "DIVERGENCE"
#define COMMAND_PULSE "PULSE"
#define COMMAND_GRAV "GRAV"
#define COMMAND_PRINT_PERF "PRINT PERF"
vector<string> commands{COMMAND_VISCOSITY, COMMAND_DIVERGENCE, COMMAND_PULSE, COMMAND_GRAV, COMMAND_PRINT_PERF};
bool request_clear_pressure = false;
int request_delta_pressure_iterations = 0;
int request_scene_type = SCENE_TYPE_TANK;
bool paused = false;
struct Object {
fluid_type x;
fluid_type y;
fluid_type radius;
};
struct State {
fluid_type density;
int* cells;
int dimensions;
int num_cells;
/// cell side length
fluid_type h;
fluid_type* u_current; //u[our index] = velocity at our leftmost edge
fluid_type* v_current; //v[our index] = velocity at our down edge
fluid_type* u_next; //whats actually operated on in sim_step(), switch with current at the end
fluid_type* v_next;
fluid_type* pressure; //helps with enforcing 0 divergence
fluid_type* solids; //the walls and the circle
fluid_type* u_source;
fluid_type* v_source;
fluid_type* m_current; //smoke
fluid_type* m_next;
fluid_type* temperature_current; //independent from velocity
fluid_type* temperature_next;
int coarse_cells_x;
int coarse_cells_y;
int coarse_cells;
int fine_cells_per_coarse;
int coarse_grid_side_length_mult;
fluid_type coarse_h;
fluid_type* u_coarse;
fluid_type* v_coarse;
fluid_type* u_residuals;
fluid_type* v_residuals;
fluid_type* solids_coarse;
/// reset to 0 every sim step, set to not 0 to force that pixel to a color
int* debug_touch_buffer;
/// idk what this is for
fluid_type* debug_data_buffer;
fluid_type viscosity;
fluid_type over_relaxation;
int num_divergence_iterations;
fluid_type g_strength;
void (*g_pointer) (State* s, fluid_type strength, fluid_type dt, int x, int y);
int objects_len;
int objects_capacity;
Object* objects;
bool clear_pressure;
};
void set_obstacle(State* s, fluid_type dt, int index, fluid_type x, fluid_type y, bool reset = false) {
if(index >= s->objects_len) {
return;
}
Object* object = &s->objects[index];
fluid_type vx = 0.0;
fluid_type vy = 0.0;
if(!reset) {
vx = (x - object->x) / dt;
vy = (y - object->y) / dt;
}
object->x = x;
object->y = y;
int n = s->cells[0];
fluid_type cd = sqrt(2) * s->h;
fluid_type r = object->radius;
for(int j = 1; j < s->cells[1] - 2; j++) {
for(int i = 1; i < s->cells[0] - 2; i++) {
s->solids[i + n * j] = S_FIELD_FLUID;
fluid_type d = sqrt(pow(x - i, 2.0) + pow(y - j, 2.0));
//if(dx * dx + dy * dy < r * r) {
if(d < r) {
//printf("\n%g, %g outside of object with radius %g lhs: %g rhs: %g", dx, dy, r, dx * dx + dy + dy, r * r);
//int x = 1 / 0;
s->solids[j * n + i] = S_FIELD_SOLID;
s->m_current[j * n + i] = 1.0;
s->u_current[j * n + i] = vx;
s->u_current[j * n + i + 1] = vx;
s->v_current[j * n + i] = vy;
s->v_current[(j + 1) * n + i] = vy;
}
}
}
}
void add_object(State* s, fluid_type x, fluid_type y, fluid_type radius) {
if(s->objects_capacity == s->objects_len) {
s->objects_capacity *= 2;
Object* new_objects = new Object[s->objects_capacity];
copy(s->objects, s->objects + s->objects_len - 1, new_objects);
delete [] s->objects;
s->objects = new_objects;
}
Object* object = &s->objects[s->objects_len];
object->radius = radius;
object->x = x;
object->y = y;
s->objects_len++;
set_obstacle(s, 0.0, s->objects_len - 1, x, y, true);
}
template<typename T>
void zero_out(T* to_zero, int len) {
for(int i = 0; i < len; i++) {
to_zero[i] = (T)0.0;
}
}
void vary_averaged_quantities(State* s, int x, int y) {
double lower_bound = -1.0;
double upper_bound = 1.0;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
uniform_real_distribution<double> unif(lower_bound, upper_bound);
default_random_engine re(seed);
double periods_per_meter[2] = {20.0, 10.0};
double mean_temp = 350.0;
const double pi = 3.14159265;
double sin_x = 5.0 * sin(2 * pi * periods_per_meter[0] * (x * s->h));
double sin_y = 3.0 * sin(2 * pi * periods_per_meter[1] * (y * s->h));
s->temperature_current[x + y * s->cells[0]] = mean_temp + sin_x + sin_y + unif(re);
s->temperature_next[x + y * s->cells[0]] = s->temperature_current[x + y * s->cells[0]];
}
State* new_state(fluid_type density, fluid_type viscosity, int num_x, int num_y, fluid_type h, fluid_type over_relaxation, fluid_type g_strength, void (*g_pointer) (State*, fluid_type, fluid_type, int, int)) {
State* s = new State();
s->density = density;
s->viscosity = viscosity;
num_x += 2;
num_y += 2;
//each dimension must be divisible by this
int modulo = (int)pow(2, STARTING_MINIMUM_POW_2_CELL_DIVISIBILITY);
while(num_x % modulo != 0) {
num_x++;
}
while(num_y % modulo != 0) {
num_y++;
}
s->cells = new int[2] {num_x, num_y};
s->dimensions = 2;
s->h = h;
s->over_relaxation = over_relaxation;
s->num_cells = s->cells[0] * s->cells[1];
printf("\nnum_cells: %i, x: %i, y: %i", s->num_cells, s->cells[0], s->cells[1]);
s->u_current = new fluid_type[s->num_cells];
s->u_source = new fluid_type[s->num_cells];
s->v_current = new fluid_type[s->num_cells];
s->v_source = new fluid_type[s->num_cells];
s->u_next = new fluid_type[s->num_cells];
s->v_next = new fluid_type[s->num_cells];
s->pressure = new fluid_type[s->num_cells];
s->solids = new fluid_type[s->num_cells];
s->m_current = new fluid_type[s->num_cells];
s->m_next = new fluid_type[s->num_cells];
s->temperature_current = new fluid_type[s->num_cells];
s->temperature_next = new fluid_type[s->num_cells];
int coarse_grid_side_length_power = STARTING_COARSE_GRID_SIDE_LENGTH_POWER;
int coarse_grid_side_length_mult = (int)pow(2, coarse_grid_side_length_power);
s->coarse_grid_side_length_mult = coarse_grid_side_length_mult;
s->coarse_cells_x = num_x / coarse_grid_side_length_mult;
s->coarse_cells_y = num_y / coarse_grid_side_length_mult;
s->coarse_cells = s->coarse_cells_x * s->coarse_cells_y;
s->fine_cells_per_coarse = (int)pow(coarse_grid_side_length_mult, 2);
s->coarse_h = h * coarse_grid_side_length_mult;
s->u_coarse = new fluid_type[s->coarse_cells];
s->v_coarse = new fluid_type[s->coarse_cells];
s->u_residuals = new fluid_type[s->coarse_cells];
s->v_residuals = new fluid_type[s->coarse_cells];
s->solids_coarse = new fluid_type[s->coarse_cells];
s->debug_touch_buffer = new int[s->num_cells];
s->debug_data_buffer = new fluid_type[s->num_cells];
s->num_divergence_iterations = 100;
s->g_strength = g_strength;
s->g_pointer = g_pointer;
s->objects = new Object[5];
s->objects_capacity = 5;
s->objects_len = 0;
s->clear_pressure = false;
int num = s->num_cells;
zero_out(s->u_current, num);
zero_out(s->u_source, num);
zero_out(s->v_current, num);
zero_out(s->v_source, num);
zero_out(s->u_next, num);
zero_out(s->v_next, num);
zero_out(s->pressure, num);
zero_out(s->solids, num);
zero_out(s->m_next, num);
zero_out(s->debug_touch_buffer, num);
zero_out(s->debug_data_buffer, num);
zero_out(s->u_coarse, s->coarse_cells);
zero_out(s->v_coarse, s->coarse_cells);
zero_out(s->u_residuals, s->coarse_cells);
zero_out(s->v_residuals, s->coarse_cells);
zero_out(s->solids_coarse, s->coarse_cells);
for(int y = 0; y < s->cells[1]; y++) {
for(int x = 0; x < s->cells[0]; x++) {
vary_averaged_quantities(s, x, y);
}
}
for(int i = 0; i < num; i++){
s->m_current[i] = 1.0;
}
return s;
}
void g_standard(State* s, fluid_type strength, fluid_type dt, int x, int y) {
//*v += -strength * dt;
s->v_current[y * s->cells[0] + x] += strength * dt;
}
void g_right(State* s, fluid_type strength, fluid_type dt, int x, int y) {
if(x < s->cells[0] / 2) {
s->v_current[y * s->cells[0] + x] += (strength / 1.0) * dt;
} else {
s->v_current[x + s->cells[0] * y] += -(strength / 1.0) * dt;
}
//*v += -strength * dt;
//s->u_current[x + s->cells[0] * y] += -strength * dt;
}
void g_center(State* s, fluid_type strength, fluid_type dt, int x, int y) {
fluid_type theta = atan2(y - s->cells[1] / 2, x - s->cells[0] / 2);
fluid_type magnitude = -strength / pow(max(sqrt(pow(x - s->cells[0] / 2, 2) + pow(y - s->cells[1] / 2, 2)), 0.05), 2);
fluid_type x_mag = magnitude * cos(theta);
fluid_type y_mag = magnitude * sin(theta);
//printf("\n\t\t\tx, y: %i, %i x force: %g y force: %g", x, y, x_mag, y_mag);
//fluid_type magnitude = -strength / max((int)pow(x - s->cells[0] / 2, 2) + (int)pow(y - s->cells[1] / 2,2), 1);
s->u_current[x + s->cells[0] * y] += x_mag * dt;
s->v_current[x + s->cells[0] * y] += y_mag * dt;
}
struct Scene {
State* s;
int scene_type;
bool show_smoke;
bool show_pressure;
bool show_streamlines;
bool show_velocity;
bool show_temperature;
public:
Scene() {
//State* s = new_state(1000.0, 1.0, 500, 500, 0.05, 1.9);
int res = STARTING_RESOLUTION;
s = new_state(STARTING_DENSITY, STARTING_VISCOSITY, res, res, 1.0 / res, STARTING_OVER_RELAXATION, 9.81, g_standard);
scene_type = STARTING_SCENARIO;
show_smoke = false;
show_pressure = true;
show_streamlines = false;
show_velocity = false;
}
void set_request_globals() {
request_scene_type = scene_type;
request_show_pressure = show_pressure;
request_show_smoke = show_smoke;
request_show_streamlines = show_streamlines;
request_show_velocity = show_velocity;
request_show_temperature = show_temperature;
show_pressure_setting = show_pressure;
show_temperature_setting = show_temperature;
request_clear_pressure = s->clear_pressure;
}
void setup_scene(int scene_type) {
this->scene_type = scene_type;
s->objects_len = 0;
s->clear_pressure = true;
s->num_divergence_iterations = STARTING_DIVERGENCE_ITERATIONS;
for(int y = 0; y < s->cells[1]; y++) {
for(int x = 0; x < s->cells[0]; x++) {
int index = x + s->cells[0] * y;
s->solids[index] = S_FIELD_FLUID;
s->pressure[index] = 0.0;
s->m_current[index] = 1.0;
s->m_next[index] = 0.0;
s->u_current[index] = 0.0;
s->u_next[index] = 0.0;
s->v_current[index] = 0.0;
s->v_next[index] = 0.0;
vary_averaged_quantities(s, x, y);
}
}
if(scene_type == SCENE_TYPE_TANK) {
for(int y = 0; y < s->cells[1]; y++) {
for(int x = 0; x < s->cells[0]; x++) {
fluid_type sol = S_FIELD_FLUID;
if(x == 0 || x == s->cells[0] - 1 || y == 0) {
sol = S_FIELD_SOLID;
}
s->solids[x + s->cells[0] * y] = sol;
}
}
s->num_divergence_iterations = 50;
s->g_strength = -9.81;
show_pressure = true;
show_pressure_setting = true;
show_smoke = false;
show_streamlines = false;
show_velocity = false;
show_temperature = false;
show_temperature_setting = false;
}
else if(scene_type == SCENE_TYPE_WIND_TUNNEL) {
fluid_type in_velocity = STARTING_WIND_TUNNEL_IN_VEL;
for(int y = 0; y < s->cells[1]; y++) {
for(int x = 0; x < s->cells[0]; x++) {
fluid_type sol = S_FIELD_FLUID;
if(x == 0 || y == s->cells[1] - 1 || y == 0) {
sol = S_FIELD_SOLID;
}
s->solids[x + s->cells[0] * y] = sol;
if(x == 1) {
s->u_current[x + s->cells[0] * y] = in_velocity;
//s->v_current[x + s->cells[0] * y] = in_velocity / 0.50;
//s->u_source[x + s->cells[0] * y] = in_velocity;
}
//if(x == (s->cells[0] - 3)) {
// s->u_current[x + s->cells[0] * y] = 1.0 * in_velocity;
//}
//if(x == s->cells[0] - 1) {
// s->u_current[x + s->cells[0] * y] = -in_velocity;
//}
}
}
s->num_divergence_iterations = STARTING_DIVERGENCE_ITERATIONS_WIND_TUNNEL;
fluid_type pipe_h = 0.1 * s->cells[1];
int min_j = floor(0.5 * s->cells[1] - 0.5 * pipe_h);
int max_j = floor(0.5 * s->cells[1] + 0.5 * pipe_h);
for(int y = min_j; y < max_j; y++) {
//for(int x = 0; x < s->cells[0]; x++) {
s->m_current[s->cells[0] * y] = 0.0;
s->m_current[s->cells[0] * y + s->cells[0] - 2] = 0.0;
//}
}
s->g_strength = 0.0;
show_pressure = true;
show_smoke = true;
show_streamlines = false;
show_velocity = false;
}
else if(scene_type == SCENE_TYPE_PAINT) {
s->g_strength = 0.0;
s->over_relaxation = 1.0;
show_pressure = false;
show_smoke = true;
show_streamlines = false;
show_velocity = false;
}
printf("\n x at %i y at %i", (int)(s->cells[0] / 2.0), (int)(s->cells[1] / 2.0));
add_object(s, (int)(s->cells[0] * 0.4), (int)(s->cells[1] / 2.0), s->cells[1] * 0.15);
set_request_globals();
zero_out(s->solids_coarse, s->coarse_cells);
int n = s->cells[0];
int coarse_cells_x = s->coarse_cells_x;
int coarse_cells_y = s->coarse_cells_y;
int coarse_side_multiplier = s->coarse_grid_side_length_mult;
fluid_type* coarse_solids = s->solids_coarse;
fluid_type* solids_buffer = s->solids;
int cn = s->coarse_cells_y;
for(int cy = 0; cy < coarse_cells_y; cy++) {
for(int cx = 0; cx < coarse_cells_x; cx++) {
fluid_type u_residual = 0.0; //the sum of the divergence of each of the cells we're downsampled from
fluid_type v_residual = 0.0;
int cus = cx + coarse_cells_y * cy;
int cright = cus + 1;
int cleft = cus - 1;
int cup = cus + coarse_cells_y;
int cdown = cus - coarse_cells_y;
for(int fy = 0; fy < coarse_side_multiplier; fy++) {
for(int fx = 0; fx < coarse_side_multiplier; fx++) {
int y = coarse_side_multiplier * cy + fy;
int x = coarse_side_multiplier * cx + fx;
if(y == 0 || x == 0 || y == s->cells[1] - 1 || x == s->cells[0] - 1) {
continue;
}
int us = y * n + x;
int right = us + 1;
int left = us - 1;
int up = us + n;
int down = us - n;
coarse_solids[cus] += solids_buffer[us] / (fluid_type)pow(coarse_side_multiplier, 2);
}
}
}
}
}
};
Scene* create_scene(int scene_type = STARTING_SCENARIO) {
Scene* scene = new Scene();
scene->setup_scene(scene_type);
return scene;
}
struct LogRow {
public:
optional<int> iteration;
optional<fluid_type> time;
optional<fluid_type> avg_pressure;
optional<fluid_type> min_pressure;
optional<fluid_type> max_pressure;
optional<fluid_type> avg_u_velocity;
optional<fluid_type> avg_v_velocity;
optional<fluid_type> avg_energy;
bool ready() {
return iteration.has_value() && time.has_value() && avg_pressure.has_value() && min_pressure.has_value() && max_pressure.has_value() && avg_u_velocity.has_value() && avg_energy.has_value();
}
void write(ofstream &log) {
log << (iteration.has_value() ? to_string(iteration.value()) : "NULL");
log << ",";
log << (time.has_value() ? to_string(time.value()) : "NULL");
log << ",";
log << (avg_pressure.has_value() ? to_string(avg_pressure.value()) : "NULL");
log << ",";
log << (min_pressure.has_value() ? to_string(min_pressure.value()) : "NULL");
log << ",";
log << (max_pressure.has_value() ? to_string(max_pressure.value()) : "NULL");
log << ",";
log << (avg_u_velocity.has_value() ? to_string(avg_u_velocity.value()) : "NULL");
log << ",";
log << (avg_v_velocity.has_value() ? to_string(avg_v_velocity.value()) : "NULL");
log << ",";
log << (avg_energy.has_value() ? to_string(avg_energy.value()) : "NULL");
log << "\n";
}
};
/// adjust the vertical velocity of every cell according to a gravity function
void integrate(State* s, fluid_type dt) {
fluid_type g_strength = s->g_strength;
void (*g_pointer)(State*, fluid_type, fluid_type, int, int) = s->g_pointer;
if(g_strength == 0.0) {
return;
}
int n = s->cells[0];
for(int y = 1; y < s->cells[1]-1; y++) {
for(int x = 1; x < s->cells[0]-1; x++) {
if(s->solids[x + n * y] != 0.0 && s->solids[x + n * (y - 1)] != 0.0) {
//s->v_current[i*n + j] += g * dt;
g_pointer(s, g_strength, dt, x, y);//(State* s, fluid_type strength, fluid_type dt, int x, int y)
}
}
}
}
struct AABB {
int x0;
int y0;
int x1;
int y1;
};
///gauss-seidel iteration on an axis aligned bounding box
void solve_AABB_buffers(AABB aabb, unsigned int full_width, int* test_buffer, fluid_type* u_buffer, fluid_type* v_buffer, fluid_type* u_residuals, fluid_type* v_residuals, fluid_type* pressure, fluid_type* solids_buffer, fluid_type over_relax, fluid_type viscosity, fluid_type cp) {
int n = full_width;
for(int y = aabb.y0; y <= aabb.y1; y++) {
for(int x = aabb.x0; x <= aabb.x1; x++) {
if(test_buffer != nullptr) {
test_buffer[y * n + x] = 0;
}
if(solids_buffer[y * n + x] == S_FIELD_SOLID) {
continue;
}
unsigned int us = y * n + x;
unsigned int left = us - 1;
unsigned int right = us + 1;
unsigned int up = (y + 1) * n + x;
unsigned int down = (y - 1) * n + x;
fluid_type sol = solids_buffer[us];
fluid_type sx0 = solids_buffer[left];
fluid_type sx1 = solids_buffer[right];
fluid_type sy0 = solids_buffer[down];
fluid_type sy1 = solids_buffer[up];
sol = sx0 + sx1 + sy0 + sy1;
if(sol == S_FIELD_SOLID) {
continue;
}
fluid_type u_r0 = u_residuals == nullptr ? 0.0 : u_residuals[us];
fluid_type v_r0 = v_residuals == nullptr ? 0.0 : v_residuals[us];
fluid_type u_r1 = u_residuals == nullptr ? 0.0 : u_residuals[right];
fluid_type v_r1 = v_residuals == nullptr ? 0.0 : v_residuals[up];
fluid_type residual_divergence = u_r1 - u_r0 + v_r1 - v_r0;
//every iteration the velocity in each edge decreases in magnitude by an amount proportional to
// the total divergence of both cells its attached to
//Ax = (b = 0)
fluid_type divergence = u_buffer[right] - u_buffer[us] + v_buffer[up] - v_buffer[us];
fluid_type p = (residual_divergence - divergence) / sol;
p *= over_relax;
if(pressure != nullptr) {
pressure[us] += cp * p;
}
u_buffer[us] += sx0 * (0 - p);
u_buffer[right] -= sx1 * (0 - p);
v_buffer[us] += sy0 * (0 - p);
v_buffer[up] -= sy1 * (0 - p);
if(isnan(u_buffer[us]) || isnan(u_buffer[right]) || isnan(v_buffer[us]) || isnan(v_buffer[up])) {
printf("\n\tsolve_AABB_buffers just set a nan! x %i y %i u us %g u right %g v us %g v up %g u_r %g v_r %g sol %g", x, y, u_buffer[us], u_buffer[right], v_buffer[us], v_buffer[up], u_r0, v_r0, sol);
paused = true;
abort();
}
}
}
}
// fine -> coarse
void restriction(State* s, int coarse_cells_x, int coarse_cells_y, int coarse_side_multiplier, fluid_type* u_buffer, fluid_type* v_buffer, fluid_type* u_residuals, fluid_type* v_residuals, fluid_type* solids_buffer, fluid_type* coarse_solids) {
int n = s->cells[0];
int cn = coarse_cells_y;
for(int cy = 0; cy < coarse_cells_y; cy++) {
for(int cx = 0; cx < coarse_cells_x; cx++) {
fluid_type u_residual = 0.0; //the sum of the divergence of each of the cells we're downsampled from
fluid_type v_residual = 0.0;
int cus = cx + coarse_cells_y * cy;
int cright = cus + 1;
int cleft = cus - 1;
int cup = cus + coarse_cells_y;
int cdown = cus - coarse_cells_y;
for(int fy = 0; fy < coarse_side_multiplier; fy++) {
for(int fx = 0; fx < coarse_side_multiplier; fx++) {
int y = coarse_side_multiplier * cy + fy;
int x = coarse_side_multiplier * cx + fx;
if(y == 0 || x == 0 || y == s->cells[1] - 1 || x == s->cells[0] - 1) {
continue;
}
int us = y * n + x;
int right = us + 1;
int left = us - 1;
int up = us + n;
int down = us - n;
//coarse_solids[cx + coarse_cells_y * cy] += solids_buffer[us] / (fluid_type)pow(coarse_side_multiplier, 2);
//if(isnan(coarse_solids[cx + coarse_cells_y * cy])) {
// printf("\n\trestriction had a nan in coarse_solids already!: cx %i cy %i fx %i fy %i x %i y %i solids[us] %g, pow(coarse_side_mult, 2) %g",
// cx,cy,fx,fy,x,y,solids_buffer[us],(fluid_type)pow(coarse_side_multiplier, 2));
// abort();
//}
//coarse_solids[cx + coarse_cells_y * cy] += solids_buffer[us] / (fluid_type)pow(coarse_side_multiplier, 2);
//if(isnan(coarse_solids[cx + coarse_cells_y * cy])) {
// printf("\n\trestriction just set a nan in coarse_solids: cx %i cy %i fx %i fy %i x %i y %i solids[us] %g, pow(coarse_side_mult, 2) %g",
// cx,cy,fx,fy,x,y,solids_buffer[us],(fluid_type)pow(coarse_side_multiplier, 2));
// abort();
//}
if(solids_buffer[us] == S_FIELD_SOLID) {
continue;
}
fluid_type sol = solids_buffer[us];
fluid_type sx0 = solids_buffer[left];
fluid_type sx1 = solids_buffer[right];
fluid_type sy0 = solids_buffer[down];
fluid_type sy1 = solids_buffer[up];
sol = sx0 + sx1 + sy0 + sy1;
//Ax = b -> r = b - Ax
fluid_type divergence = u_buffer[right] - u_buffer[us] + v_buffer[up] - v_buffer[us];
fluid_type p = divergence / sol;
//u_residual -= sx0 * p;
//v_residual -= sy0 * p;
u_residuals[cx + coarse_cells_y * cy] -= sx0 * p;
if(cx + 1 + coarse_cells_y * cy < coarse_cells_x * coarse_cells_y) {
u_residuals[cx + 1 + coarse_cells_y * cy] += sx1 * p;
}
v_residuals[cx + coarse_cells_y * cy] -= sy0 * p;
if(cx + coarse_cells_y * (cy + 1) < coarse_cells_x * coarse_cells_y) {
v_residuals[cx + coarse_cells_y * (cy + 1)] += sy1 * p;
}
//if(isnan(u_residuals[cx + coarse_cells_y * cy]) || isnan(u_residuals[cx + 1 + coarse_cells_y * cy]) ||
// isnan(v_residuals[cx + coarse_cells_y & cy] || isnan(v_residuals[cx + coarse_cells_y * (cy + 1)]))) {
// printf("\n\trestriction just set a nan! cx %i cy %i u residuals[us] %g u residuals[right] %g v residuals[us] %g v residuals[up]", cx, cy,
// u_residuals[cx + coarse_cells_y * cy], u_residuals[cx + 1 + coarse_cells_y * cy], v_residuals[cx + coarse_cells_y & cy], v_residuals[cx + coarse_cells_y * (cy + 1)]);
// paused = true;
// abort();
//}
}
}
//u_residuals[cx + coarse_cells_y * cy] = u_residual;
//v_residuals[cx + coarse_cells_y * cy] = v_residual;
//if(isnan(u_residual) || isnan(v_residual)) {
// printf("\n\trestriction just set a nan! cx %i cy %i u residual %g v residual %g", cx, cy, u_residual, v_residual);
// paused = true;
//}
}
}
}
//coarse -> fine via interpolation of the delta u and v solved using the coarse grid,
// added onto the total u and v vels we iterated on before restriction
void prolongation(State* s, int coarse_cells_x, int coarse_cells_y, int coarse_side_multiplier, fluid_type* u_buffer, fluid_type* v_buffer, fluid_type* u_error, fluid_type* v_error, fluid_type* pressure_buffer, fluid_type cp, fluid_type* solids_buffer, fluid_type* coarse_solids) {
int n = s->cells[0];
int coarse_len = coarse_cells_x * coarse_cells_y;
for(int y = 1; y < s->cells[1] - 1; y++) {
for(int x = 1; x < s->cells[0] - 1; x++) {
int us = x + y * n;
int left = us - 1;
int right = us + 1;
int up = (y + 1) * n + x;
int down = (y - 1) * n + x;
if(solids_buffer[us] == S_FIELD_SOLID) {
continue;
}
fluid_type raw_cy = (fluid_type)y / (fluid_type)coarse_side_multiplier;
fluid_type raw_cx = (fluid_type)x / (fluid_type)coarse_side_multiplier;
int cy = (int)floor(raw_cy);
int cx = (int)floor(raw_cx);
int cy_ahead = (int)ceil(raw_cy);
int cx_ahead = (int)ceil(raw_cx);
int cus = cx + coarse_cells_y * cy;
int cright = cus + 1;
int cleft = cus - 1;
int cup = cus + coarse_cells_y;
int cdown = cus - coarse_cells_y;
//printf("\n\t\t--------PROLONG BEFORE C WEIGHTS");
fluid_type cs_down = cy > 0 ? coarse_solids[cdown] : 1.0;
fluid_type cs_up = cy < coarse_cells_y - 1 ? coarse_solids[cup] : 1.0;
fluid_type cs_left = cx > 0 ? coarse_solids[cleft] : 1.0;
fluid_type cs_right = cx < coarse_cells_x - 1 ? coarse_solids[cright] : 1.0;
fluid_type cy_behind_weight = (cy_ahead - raw_cy) * solids_buffer[down];
fluid_type cy_ahead_weight = (raw_cy - cy) * solids_buffer[up];
fluid_type cx_behind_weight = (cx_ahead - raw_cx) * solids_buffer[left];
fluid_type cx_ahead_weight = (raw_cx - cx) * solids_buffer[right];
//printf("\n\tprolong x %i y %i cx %i cy %i raw cx %g raw cy %g cy behind %g ahead %g cx behind %g ahead %g",
// x,y,cx,cy,raw_cx,raw_cy, cy_behind_weight, cy_ahead_weight, cx_behind_weight, cx_ahead_weight);
//printf("\n\t\t--------PROLONG BEFORE BUFFER FILLS");
//pressure_buffer[us] += u_error[cus] + v_error[cus];
fluid_type u_correction = (cx_behind_weight * u_error[cus] + cx_ahead_weight * (cright < coarse_cells_x ? u_error[cright] : u_error[cus]));
fluid_type v_correction = (cy_behind_weight * v_error[cus] + cy_ahead_weight * (cup < coarse_cells_y ? v_error[cup] : v_error[cus]));
//fluid_type divergence0 = u_buffer[right] - u_buffer[us] + v_buffer[up] - v_buffer[us];
//fluid_type divergence1 = u_buffer[right] - u_buffer[us] + v_buffer[up] - v_buffer[us];
//printf("\n\tprolong x %i y %i u %g -> %g, v %g -> %g", x,y,u_buffer[us], u_buffer[us] + u_correction, v_buffer[us], v_buffer[us] + v_correction);
u_buffer[us] += u_correction;
v_buffer[us] += v_correction;
//if(isnan(u_buffer[us]) || isnan(v_buffer[us])) {
// printf("\n\tprolongation just set a nan! x %i y %i u us %g v us %g u error %g v error %g", x, y, u_buffer[us], v_buffer[us], u_error[us], v_error[us]);
// paused = true;
//}
}
}
}
void fit_AABBs(AABB* AABBs, int rects, int rect_levels, int width, int height) {
int rects_per_side = 2 * rect_levels;
int rect_width = width / rects_per_side;
int rect_height = height / rects_per_side;
int* x_limits = new int[2 * rects_per_side];
int* y_limits = new int[2 * rects_per_side];
int px = 1;
int py = 1;
int x_rect_i = 0;
int y_rect_i = 0;
for(x_rect_i; x_rect_i < rects_per_side; x_rect_i++) {
x_limits[2 * x_rect_i] = px;
px += rect_width;
x_limits[2 * x_rect_i + 1] = px - 1;
}
x_limits[2 * (rects_per_side - 1) + 1] = width - 2;
for(int ry = 0; ry < rects_per_side; ry++) {
y_limits[2 * ry] = py;
py += rect_height;
y_limits[2 * ry + 1] = py - 1;
}
y_limits[2 * (rects_per_side - 1) + 1] = height - 2;
for(int ry = 0; ry < rects_per_side; ry++) {
for(int rx = 0; rx < rects_per_side; rx++) {
AABBs[rx + ry * rects_per_side].x0 = x_limits[2 * rx];
AABBs[rx + ry * rects_per_side].x1 = x_limits[2 * rx + 1];
AABBs[rx + ry * rects_per_side].y0 = y_limits[2 * ry];
AABBs[rx + ry * rects_per_side].y1 = y_limits[2 * ry + 1];
//printf("\n\t(%i, %i): (%i-%i, %i-%i)", rx, ry, AABBs[rx + ry * rects_per_side].x0, AABBs[rx + ry * rects_per_side].x1, AABBs[rx + ry * rects_per_side].y0, AABBs[rx + ry * rects_per_side].y1);
}
}
delete [] x_limits;
delete [] y_limits;
}
fluid_type calc_avg_divergence(State* s) {
int n = s->cells[0];
fluid_type* u_buffer = s->u_current;
fluid_type* v_buffer = s->v_current;
fluid_type* solids_buffer = s->solids;
fluid_type avg_divergence = 0.0;
for(int y = 1; y < s->cells[1] - 1; y++) {
for(int x = 1; x < s->cells[0] - 1; x++) {
unsigned int us = y * n + x;
unsigned int left = us - 1;
unsigned int right = us + 1;
unsigned int up = (y + 1) * n + x;
unsigned int down = (y - 1) * n + x;
fluid_type sol = solids_buffer[us];
fluid_type sx0 = solids_buffer[left];
fluid_type sx1 = solids_buffer[right];
fluid_type sy0 = solids_buffer[down];
fluid_type sy1 = solids_buffer[up];
sol = sx0 + sx1 + sy0 + sy1;
if(sol == S_FIELD_SOLID) {
continue;
}
avg_divergence -= (sx1*u_buffer[right] - sx0*u_buffer[us] + sy1*v_buffer[up] - sy0*v_buffer[us]) / sol;
}
}
avg_divergence /= s->num_cells;
return abs(avg_divergence);
}
void solve_incompressability_multithread_multigrid(State* s, int num_iterations, fluid_type dt) {
int n = s->cells[0];
fluid_type cp = s->density * s->h / dt;
int rect_levels = 2; //0 = no recursion, 1 = world rect is split into 4 subrects, 2 = 1 and each subrect is split into 4 subsubrects. every rect gets a thread
int rects = (int)pow(4, rect_levels);
AABB* AABBs = new AABB[rects];
//vector<thread> threads;
thread** threads = new thread*[rects];
fit_AABBs(AABBs, rects, rect_levels, s->cells[0], s->cells[1]);
if(STARTING_COARSE_GRID_BUDGET_DIVISOR == 0 || STARTING_COARSE_GRID_SIDE_LENGTH_POWER == 0) {
unsigned int full_width = s->cells[0];
int* test_buffer = s->debug_touch_buffer;
for(int i = 0; i < s->num_cells; i++) {
test_buffer[i] = 1;
}
printf("\n-------BEFORE FIRST FINE ITER");
fluid_type avg_divergence = calc_avg_divergence(s);
printf("\n\tbefore iter, avg divergence is %g", avg_divergence);
//first fine iteration portion
for(int iter = 0; iter < num_iterations; iter++) {
for(int i = 0; i < rects; i++) {
threads[i] = new thread(solve_AABB_buffers, AABBs[i], full_width, test_buffer, s->u_current, s->v_current, nullptr, nullptr, s->pressure, s->solids, s->over_relaxation, s->viscosity, cp);
}
for(int i = 0; i < rects; i++) {
threads[i]->join();
delete threads[i];
}
}
fluid_type avg_divergence2 = calc_avg_divergence(s);
printf("\n\tafter iter, avg divergence is %g (%g)", avg_divergence2, avg_divergence2 - avg_divergence);
delete [] AABBs;
delete [] threads;
return;
}
//we split our iterations into 3 regions: the first fine iterations, the coarse iterations, and the second fine iterations
//(1/n) of num_iterations is in the coarse region, the remaining is evenly split between both fine regions