-
Notifications
You must be signed in to change notification settings - Fork 336
/
setup.cpp
1336 lines (1222 loc) · 87.3 KB
/
setup.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
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 "setup.hpp"
#ifdef BENCHMARK
#include "info.hpp"
void main_setup() { // benchmark; required extensions in defines.hpp: BENCHMARK, optionally FP16S or FP16C
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
uint mlups = 0u; {
//LBM lbm( 32u, 32u, 32u, 1.0f);
//LBM lbm( 64u, 64u, 64u, 1.0f);
//LBM lbm(128u, 128u, 128u, 1.0f);
LBM lbm(256u, 256u, 256u, 1.0f); // default
//LBM lbm(384u, 384u, 384u, 1.0f);
//LBM lbm(512u, 512u, 512u, 1.0f);
//const uint memory = 1488u; // memory occupation in MB (for multi-GPU benchmarks: make this close to as large as the GPU's VRAM capacity)
//const uint3 lbm_N = resolution(float3(1.0f, 1.0f, 1.0f), memory); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
//LBM lbm(1u*lbm_N.x, 1u*lbm_N.y, 1u*lbm_N.z, 1u, 1u, 1u, 1.0f); // 1 GPU
//LBM lbm(2u*lbm_N.x, 1u*lbm_N.y, 1u*lbm_N.z, 2u, 1u, 1u, 1.0f); // 2 GPUs
//LBM lbm(2u*lbm_N.x, 2u*lbm_N.y, 1u*lbm_N.z, 2u, 2u, 1u, 1.0f); // 4 GPUs
//LBM lbm(2u*lbm_N.x, 2u*lbm_N.y, 2u*lbm_N.z, 2u, 2u, 2u, 1.0f); // 8 GPUs
// #########################################################################################################################################################################################
for(uint i=0u; i<1000u; i++) {
lbm.run(10u, 1000u*10u);
mlups = max(mlups, to_uint((double)lbm.get_N()*1E-6/info.runtime_lbm_timestep_smooth));
}
} // make lbm object go out of scope to free its memory
print_info("Peak MLUPs/s = "+to_string(mlups));
#if defined(_WIN32)
wait();
#endif // Windows
} /**/
#endif // BENCHMARK
/*void main_setup() { // 3D Taylor-Green vortices; required extensions in defines.hpp: INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
LBM lbm(128u, 128u, 128u, 1u, 1u, 1u, 0.01f);
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
const float A = 0.25f;
const uint periodicity = 1u;
const float a=(float)Nx/(float)periodicity, b=(float)Ny/(float)periodicity, c=(float)Nz/(float)periodicity;
const float fx = (float)x+0.5f-0.5f*(float)Nx;
const float fy = (float)y+0.5f-0.5f*(float)Ny;
const float fz = (float)z+0.5f-0.5f*(float)Nz;
lbm.u.x[n] = A*cosf(2.0f*pif*fx/a)*sinf(2.0f*pif*fy/b)*sinf(2.0f*pif*fz/c);
lbm.u.y[n] = -A*sinf(2.0f*pif*fx/a)*cosf(2.0f*pif*fy/b)*sinf(2.0f*pif*fz/c);
lbm.u.z[n] = A*sinf(2.0f*pif*fx/a)*sinf(2.0f*pif*fy/b)*cosf(2.0f*pif*fz/c);
lbm.rho[n] = 1.0f-sq(A)*3.0f/4.0f*(cosf(4.0f*pif*fx/a)+cosf(4.0f*pif*fy/b));
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_STREAMLINES;
lbm.run();
//lbm.run(1000u); lbm.u.read_from_device(); println(lbm.u.x[lbm.index(Nx/2u, Ny/2u, Nz/2u)]); wait(); // test for binary identity
} /**/
/*void main_setup() { // 2D Taylor-Green vortices (use D2Q9); required extensions in defines.hpp: INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
LBM lbm(1024u, 1024u, 1u, 0.02f);
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
const float A = 0.2f;
const uint periodicity = 5u;
const float a=(float)Nx/(float)periodicity, b=(float)Ny/(float)periodicity;
const float fx = (float)x+0.5f-0.5f*(float)Nx;
const float fy = (float)y+0.5f-0.5f*(float)Ny;
lbm.u.x[n] = A*cosf(2.0f*pif*fx/a)*sinf(2.0f*pif*fy/b);
lbm.u.y[n] = -A*sinf(2.0f*pif*fx/a)*cosf(2.0f*pif*fy/b);
lbm.rho[n] = 1.0f-sq(A)*3.0f/4.0f*(cosf(4.0f*pif*fx/a)+cosf(4.0f*pif*fy/b));
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FIELD;
lbm.graphics.slice_mode = 3;
lbm.run();
} /**/
/*void main_setup() { // Poiseuille flow validation; required extensions in defines.hpp: VOLUME_FORCE
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint R = 63u; // channel radius (default: 63)
const float umax = 0.1f; // maximum velocity in channel center (must be < 0.57735027f)
const float tau = 1.0f; // relaxation time (must be > 0.5f), tau = nu*3+0.5
const float nu = units.nu_from_tau(tau); // nu = (tau-0.5)/3
const uint H = 2u*(R+1u);
#ifndef D2Q9
LBM lbm(H, lcm(sq(H), WORKGROUP_SIZE)/sq(H), H, nu, 0.0f, units.f_from_u_Poiseuille_3D(umax, 1.0f, nu, R), 0.0f); // 3D
#else // D2Q9
LBM lbm(lcm(H, WORKGROUP_SIZE)/H, H, 1u, nu, units.f_from_u_Poiseuille_2D(umax, 1.0f, nu, R), 0.0f, 0.0f); // 2D
#endif // D2Q9
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
#ifndef D2Q9
if(!cylinder(x, y, z, lbm.center(), float3(0u, Ny, 0u), 0.5f*(float)min(Nx, Nz)-1.0f)) lbm.flags[n] = TYPE_S; // 3D
#else // D2Q9
if(y==0u||y==Ny-1u) lbm.flags[n] = TYPE_S; // 2D
#endif // D2Q9
}); // ####################################################################### run simulation, export images and data ##########################################################################
double error_min = max_double;
while(true) { // main simulation loop
lbm.run(1000u);
lbm.u.read_from_device();
double error_dif=0.0, error_sum=0.0;
#ifndef D2Q9
for(uint x=0u; x<Nx; x++) {
for(uint y=Ny/2u; y<Ny/2u+1u; y++) {
for(uint z=0; z<Nz; z++) {
const uint n = x+(y+z*Ny)*Nx;
const double r = (double)sqrt(sq(x+0.5f-0.5f*(float)Nx)+sq(z+0.5f-0.5f*(float)Nz)); // radius from channel center
if(r<R) {
const double unum = (double)sqrt(sq(lbm.u.x[n])+sq(lbm.u.y[n])+sq(lbm.u.z[n])); // numerical velocity
const double uref = umax*(sq(R)-sq(r))/sq(R); // theoretical velocity profile u = G*(R^2-r^2)
error_dif += sq(unum-uref); // L2 error (Krüger p. 138)
error_sum += sq(uref);
}
}
}
}
#else // D2Q9
for(uint x=Nx/2u; x<Nx/2u+1u; x++) {
for(uint y=1u; y<Ny-1u; y++) {
const uint n = x+(y+0u*Ny)*Nx;
const double r = (double)(y+0.5f-0.5f*(float)Ny); // radius from channel center
const double unum = (double)sqrt(sq(lbm.u.x[n])+sq(lbm.u.y[n])); // numerical velocity
const double uref = umax*(sq(R)-sq(r))/sq(R); // theoretical velocity profile u = G*(R^2-r^2)
error_dif += sq(unum-uref); // L2 error (Krüger p. 138)
error_sum += sq(uref);
}
}
#endif // D2Q9
if(sqrt(error_dif/error_sum)>=error_min) { // stop when error has converged
print_info("Poiseuille flow error converged after "+to_string(lbm.get_t())+" steps to "+to_string(100.0*error_min, 3u)+"%"); // typical expected L2 errors: 2-5% (Krüger p. 256)
wait();
exit(0);
}
error_min = fmin(error_min, sqrt(error_dif/error_sum));
print_info("Poiseuille flow error after t="+to_string(lbm.get_t())+" is "+to_string(100.0*error_min, 3u)+"%"); // typical expected L2 errors: 2-5% (Krüger p. 256)
}
} /**/
/*void main_setup() { // Stokes drag validation; required extensions in defines.hpp: FORCE_FIELD, EQUILIBRIUM_BOUNDARIES
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const ulong dt = 100ull; // check error every dt time steps
const float R = 32.0f; // sphere radius
const float Re = 0.01f; // Reynolds number
const float nu = 1.0f; // kinematic shear viscosity
const float rho = 1.0f; // density
const uint L = to_uint(8.0f*R); // simulation box size
const float u = units.u_from_Re(Re, 2.0f*R, nu); // velocity
LBM lbm(L, L, L, nu); // flow driven by equilibrium boundaries
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E;
if(sphere(x, y, z, lbm.center(), R)) {
lbm.flags[n] = TYPE_S|TYPE_X; // flag boundary cells for force summation additionally with TYPE_X
} else {
lbm.rho[n] = units.rho_Stokes(lbm.position(x, y, z), float3(-u, 0.0f, 0.0f), R, rho, nu);
const float3 un = units.u_Stokes(lbm.position(x, y, z), float3(-u, 0.0f, 0.0f), R);
lbm.u.x[n] = un.x;
lbm.u.y[n] = un.y;
lbm.u.z[n] = un.z;
}
}); // ####################################################################### run simulation, export images and data ##########################################################################
double E1=1000.0, E2=1000.0;
while(true) { // main simulation loop
lbm.run(dt);
lbm.calculate_force_on_boundaries();
lbm.F.read_from_device();
const float3 force = lbm.calculate_force_on_object(TYPE_S|TYPE_X);
const double F_theo = units.F_Stokes(rho, u, nu, R);
const double F_sim = (double)length(force);
const double E0 = fabs(F_sim-F_theo)/F_theo;
print_info(to_string(lbm.get_t())+", expected: "+to_string(F_theo, 6u)+", measured: "+to_string(F_sim, 6u)+", error = "+to_string((float)(100.0*E0), 1u)+"%");
if(converged(E2, E1, E0, 1E-4)) { // stop when error has sufficiently converged
print_info("Error converged after "+to_string(lbm.get_t())+" steps to "+to_string(100.0*E0, 1u)+"%");
wait();
break;
}
E2 = E1;
E1 = E0;
}
} /**/
/*void main_setup() { // cylinder in rectangular duct; required extensions in defines.hpp: VOLUME_FORCE, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const float Re = 25000.0f;
const float D = 64.0f;
const float u = rsqrt(3.0f);
const float w=D, l=12.0f*D, h=3.0f*D;
const float nu = units.nu_from_Re(Re, D, u);
const float f = units.f_from_u_rectangular_duct(w, D, 1.0f, nu, u);
LBM lbm(to_uint(w), to_uint(l), to_uint(h), nu, 0.0f, f, 0.0f);
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
lbm.u.y[n] = 0.1f*u;
if(cylinder(x, y, z, float3(lbm.center().x, 2.0f*D, lbm.center().z), float3(Nx, 0u, 0u), 0.5f*D)) lbm.flags[n] = TYPE_S;
if(x==0u||x==Nx-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_S; // x and z non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_Q_CRITERION;
lbm.run();
} /**/
/*void main_setup() { // Taylor-Couette flow; required extensions in defines.hpp: MOVING_BOUNDARIES, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
LBM lbm(96u, 96u, 192u, 1u, 1u, 1u, 0.04f);
// ###################################################################################### define geometry ######################################################################################
const uint threads = (uint)thread::hardware_concurrency();
vector<uint> seed(threads);
for(uint t=0u; t<threads; t++) seed[t] = 42u+t;
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), threads, [&](ulong n, uint t) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(!cylinder(x, y, z, lbm.center(), float3(0u, 0u, Nz), (float)(Nx/2u-1u))) lbm.flags[n] = TYPE_S;
if( cylinder(x, y, z, lbm.center(), float3(0u, 0u, Nz), (float)(Nx/4u ))) {
const float3 relative_position = lbm.relative_position(n);
lbm.u.x[n] = relative_position.y;
lbm.u.y[n] = -relative_position.x;
lbm.u.z[n] = (1.0f-random(seed[t], 2.0f))*0.001f;
lbm.flags[n] = TYPE_S;
}
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_STREAMLINES;
lbm.run();
//lbm.run(4000u); lbm.u.read_from_device(); println(lbm.u.x[lbm.index(Nx/4u, Ny/4u, Nz/2u)]); wait(); // test for binary identity
} /**/
/*void main_setup() { // lid-driven cavity; required extensions in defines.hpp: MOVING_BOUNDARIES, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint L = 128u;
const float Re = 1000.0f;
const float u = 0.1f;
LBM lbm(L, L, L, units.nu_from_Re(Re, (float)(L-2u), u));
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(z==Nz-1) lbm.u.y[n] = u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_S; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_STREAMLINES;
lbm.run();
} /**/
/*void main_setup() { // 2D Karman vortex street; required extensions in defines.hpp: D2Q9, FP16S, EQUILIBRIUM_BOUNDARIES, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint R = 16u;
const float Re = 250.0f;
const float u = 0.10f;
LBM lbm(16u*R, 32u*R, 1u, units.nu_from_Re(Re, 2.0f*(float)R, u));
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(cylinder(x, y, z, float3(Nx/2u, Ny/4u, Nz/2u), float3(0u, 0u, Nz), (float)R)) lbm.flags[n] = TYPE_S;
else lbm.u.y[n] = u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_FIELD;
lbm.graphics.slice_mode = 3;
lbm.run();
} /**/
/*void main_setup() { // particle test; required extensions in defines.hpp: VOLUME_FORCE, FORCE_FIELD, MOVING_BOUNDARIES, PARTICLES, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint L = 128u;
const float Re = 1000.0f;
const float u = 0.1f;
LBM lbm(L, L, L, units.nu_from_Re(Re, (float)(L-2u), u), 0.0f, 0.0f, -0.00001f, cb(L/4u), 2.0f);
// ###################################################################################### define geometry ######################################################################################
uint seed = 42u;
for(ulong n=0ull; n<lbm.particles->length(); n++) {
lbm.particles->x[n] = random_symmetric(seed, 0.5f*lbm.size().x/4.0f);
lbm.particles->y[n] = random_symmetric(seed, 0.5f*lbm.size().y/4.0f);
lbm.particles->z[n] = random_symmetric(seed, 0.5f*lbm.size().z/4.0f);
}
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(z==Nz-1) lbm.u.y[n] = u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_S; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_STREAMLINES|VIS_PARTICLES;
lbm.run();
} /**/
/*void main_setup() { // delta wing; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint L = 128u;
const float Re = 100000.0f;
const float u = 0.075f;
LBM lbm(L, 4u*L, L, units.nu_from_Re(Re, (float)L, u));
// ###################################################################################### define geometry ######################################################################################
const float3 offset = float3(lbm.center().x, 0.0f, lbm.center().z);
const float3 p0 = offset+float3( 0*(int)L/64, 5*(int)L/64, 20*(int)L/64);
const float3 p1 = offset+float3(-20*(int)L/64, 90*(int)L/64, -10*(int)L/64);
const float3 p2 = offset+float3(+20*(int)L/64, 90*(int)L/64, -10*(int)L/64);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(triangle(x, y, z, p0, p1, p2)) lbm.flags[n] = TYPE_S;
else lbm.u.y[n] = u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run();
} /**/
/*void main_setup() { // NASA Common Research Model; required extensions in defines.hpp: FP16C, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 1.5f, 1.0f/3.0f), 2000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float Re = 10000000.0f;
const float u = 0.075f;
LBM lbm(lbm_N, units.nu_from_Re(Re, (float)lbm_N.x, u));
// ###################################################################################### define geometry ######################################################################################
// model: https://commonresearchmodel.larc.nasa.gov/high-lift-crm/high-lift-crm-geometry/assembled-geometry/, .stp file converted to .stl with https://imagetostl.com/convert/file/stp/to/stl
Mesh* half = read_stl(get_exe_path()+"../stl/crm-hl_reference_ldg.stl", lbm.size(), float3(0.0f), float3x3(float3(0, 0, 1), radians(90.0f)), 1.0f*lbm_N.x);
half->translate(float3(-0.5f*(half->pmax.x-half->pmin.x), 0.0f, 0.0f));
Mesh* mesh = new Mesh(2u*half->triangle_number, float3(0.0f));
for(uint i=0u; i<half->triangle_number; i++) {
mesh->p0[i] = half->p0[i];
mesh->p1[i] = half->p1[i];
mesh->p2[i] = half->p2[i];
}
half->rotate(float3x3(float3(1, 0, 0), radians(180.0f))); // mirror-copy half
for(uint i=0u; i<half->triangle_number; i++) {
mesh->p0[half->triangle_number+i] = -half->p0[i];
mesh->p1[half->triangle_number+i] = -half->p1[i];
mesh->p2[half->triangle_number+i] = -half->p2[i];
}
delete half;
mesh->find_bounds();
mesh->rotate(float3x3(float3(1, 0, 0), radians(-10.0f)));
mesh->translate(float3(0.0f, 0.0f, -0.5f*(mesh->pmin.z+mesh->pmax.z)));
mesh->translate(float3(0.0f, -0.5f*lbm.size().y+mesh->pmax.y+0.5f*(lbm.size().x-(mesh->pmax.x-mesh->pmin.x)), 0.0f));
mesh->translate(lbm.center());
lbm.voxelize_mesh_on_device(mesh);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run();
} /**/
/*void main_setup() { // Concorde; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 3.0f, 0.5f), 2084u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float si_u = 300.0f/3.6f;
const float si_length=62.0f, si_width=26.0f;
const float si_T = 1.0f;
const float si_nu=1.48E-5f, si_rho=1.225f;
const float lbm_length = 0.56f*(float)lbm_N.y;
const float lbm_u = 0.075f;
units.set_m_kg_s(lbm_length, lbm_u, 1.0f, si_length, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
print_info("Re = "+to_string(to_uint(units.si_Re(si_width, si_u, si_nu))));
LBM lbm(lbm_N, 1u, 1u, 1u, lbm_nu);
// ###################################################################################### define geometry ######################################################################################
const float3 center = float3(lbm.center().x, 0.52f*lbm_length, lbm.center().z+0.03f*lbm_length);
const float3x3 rotation = float3x3(float3(1, 0, 0), radians(-10.0f))*float3x3(float3(0, 0, 1), radians(90.0f))*float3x3(float3(1, 0, 0), radians(90.0f));
lbm.voxelize_stl(get_exe_path()+"../stl/concord_cut_large.stl", center, rotation, lbm_length); // https://www.thingiverse.com/thing:1176931/files
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
lbm.write_status();
while(lbm.get_t()<=lbm_T) { // main simulation loop
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 10.0f)) {
lbm.graphics.set_camera_free(float3(0.491343f*(float)Nx, -0.882147f*(float)Ny, 0.564339f*(float)Nz), -78.0f, 6.0f, 22.0f);
lbm.graphics.write_frame(get_exe_path()+"export/front/");
lbm.graphics.set_camera_free(float3(1.133361f*(float)Nx, 1.407077f*(float)Ny, 1.684411f*(float)Nz), 72.0f, 12.0f, 20.0f);
lbm.graphics.write_frame(get_exe_path()+"export/back/");
lbm.graphics.set_camera_centered(0.0f, 0.0f, 25.0f, 1.648722f);
lbm.graphics.write_frame(get_exe_path()+"export/side/");
lbm.graphics.set_camera_centered(0.0f, 90.0f, 25.0f, 1.648722f);
lbm.graphics.write_frame(get_exe_path()+"export/top/");
lbm.graphics.set_camera_free(float3(0.269361f*(float)Nx, -0.179720f*(float)Ny, 0.304988f*(float)Nz), -56.0f, 31.6f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/wing/");
lbm.graphics.set_camera_free(float3(0.204399f*(float)Nx, 0.340055f*(float)Ny, 1.620902f*(float)Nz), 80.0f, 35.6f, 34.0f);
lbm.graphics.write_frame(get_exe_path()+"export/follow/");
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run(1u, lbm_T); // run dt time steps
}
lbm.write_status();
} /**/
/*void main_setup() { // Boeing 747; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 0.5f), 880u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 1000000.0f;
const float lbm_u = 0.075f;
const ulong lbm_T = 10000ull;
LBM lbm(lbm_N, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float size = 1.0f*lbm.size().x;
const float3 center = float3(lbm.center().x, 0.55f*size, lbm.center().z);
const float3x3 rotation = float3x3(float3(1, 0, 0), radians(-15.0f));
lbm.voxelize_stl(get_exe_path()+"../stl/techtris_airplane.stl", center, rotation, size); // https://www.thingiverse.com/thing:2772812/files
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.graphics.set_camera_free(float3(1.0f*(float)Nx, -0.4f*(float)Ny, 2.0f*(float)Nz), -33.0f, 42.0f, 68.0f);
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 10.0f)) lbm.graphics.write_frame(); // render enough frames 10 seconds of 60fps video
lbm.run(1u, lbm_T);
}
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // Star Wars X-wing; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 0.5f), 880u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 100000.0f;
const float lbm_u = 0.075f;
const ulong lbm_T = 50000ull;
LBM lbm(lbm_N, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float size = 1.0f*lbm.size().x;
const float3 center = float3(lbm.center().x, 0.55f*size, lbm.center().z);
const float3x3 rotation = float3x3(float3(0, 0, 1), radians(180.0f));
lbm.voxelize_stl(get_exe_path()+"../stl/X-Wing.stl", center, rotation, size); // https://www.thingiverse.com/thing:353276/files
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_free(float3(1.0f*(float)Nx, -0.4f*(float)Ny, 2.0f*(float)Nz), -33.0f, 42.0f, 68.0f);
lbm.graphics.write_frame(get_exe_path()+"export/t/");
lbm.graphics.set_camera_free(float3(0.5f*(float)Nx, -0.35f*(float)Ny, -0.7f*(float)Nz), -33.0f, -40.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/b/");
lbm.graphics.set_camera_free(float3(0.0f*(float)Nx, 0.51f*(float)Ny, 0.75f*(float)Nz), 90.0f, 28.0f, 80.0f);
lbm.graphics.write_frame(get_exe_path()+"export/f/");
lbm.graphics.set_camera_free(float3(0.7f*(float)Nx, -0.15f*(float)Ny, 0.06f*(float)Nz), 0.0f, 0.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/s/");
}
lbm.run(1u, lbm_T);
}
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // Star Wars TIE fighter; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 1.0f), 1760u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 100000.0f;
const float lbm_u = 0.075f;
const ulong lbm_T = 50000ull;
const ulong lbm_dt = 28ull;
LBM lbm(lbm_N, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float size = 0.65f*lbm.size().x;
const float3 center = float3(lbm.center().x, 0.6f*size, lbm.center().z);
const float3x3 rotation = float3x3(float3(1, 0, 0), radians(90.0f));
Mesh* mesh = read_stl(get_exe_path()+"../stl/DWG_Tie_Fighter_Assembled_02.stl", lbm.size(), center, rotation, size); // https://www.thingiverse.com/thing:2919109/files
lbm.voxelize_mesh_on_device(mesh);
lbm.flags.read_from_device();
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<lbm_T) { // main simulation loop
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_free(float3(1.0f*(float)Nx, -0.4f*(float)Ny, 0.63f*(float)Nz), -33.0f, 33.0f, 80.0f);
lbm.graphics.write_frame(get_exe_path()+"export/t/");
lbm.graphics.set_camera_free(float3(0.3f*(float)Nx, -1.5f*(float)Ny, -0.45f*(float)Nz), -83.0f, -10.0f, 40.0f);
lbm.graphics.write_frame(get_exe_path()+"export/b/");
lbm.graphics.set_camera_free(float3(0.0f*(float)Nx, 0.57f*(float)Ny, 0.7f*(float)Nz), 90.0f, 29.5f, 80.0f);
lbm.graphics.write_frame(get_exe_path()+"export/f/");
lbm.graphics.set_camera_free(float3(2.5f*(float)Nx, 0.0f*(float)Ny, 0.0f*(float)Nz), 0.0f, 0.0f, 50.0f);
lbm.graphics.write_frame(get_exe_path()+"export/s/");
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run(lbm_dt, lbm_T);
const float3x3 rotation = float3x3(float3(0.2f, 1.0f, 0.1f), radians(0.4032f)); // create rotation matrix to rotate mesh
lbm.unvoxelize_mesh_on_device(mesh);
mesh->rotate(rotation); // rotate mesh
lbm.voxelize_mesh_on_device(mesh);
}
} /**/
/*void main_setup() { // radial fan; required extensions in defines.hpp: FP16S, MOVING_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(3.0f, 3.0f, 1.0f), 181u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 100000.0f;
const float lbm_u = 0.1f;
const ulong lbm_T = 48000ull;
const ulong lbm_dt = 10ull;
LBM lbm(lbm_N, 1u, 1u, 1u, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float radius = 0.25f*(float)lbm_N.x;
const float3 center = float3(lbm.center().x, lbm.center().y, 0.36f*radius);
const float lbm_omega=lbm_u/radius, lbm_domega=lbm_omega*lbm_dt;
Mesh* mesh = read_stl(get_exe_path()+"../stl/FAN_Solid_Bottom.stl", lbm.size(), center, 2.0f*radius); // https://www.thingiverse.com/thing:6113/files
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u) lbm.flags[n] = TYPE_S; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<lbm_T) { // main simulation loop
lbm.voxelize_mesh_on_device(mesh, TYPE_S, center, float3(0.0f), float3(0.0f, 0.0f, lbm_omega));
lbm.run(lbm_dt, lbm_T);
mesh->rotate(float3x3(float3(0.0f, 0.0f, 1.0f), lbm_domega)); // rotate mesh
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_free(float3(0.353512f*(float)Nx, -0.150326f*(float)Ny, 1.643939f*(float)Nz), -25.0f, 61.0f, 100.0f);
lbm.graphics.write_frame();
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
}
} /**/
/*void main_setup() { // electric ducted fan (EDF); required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, MOVING_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 1.5f, 1.0f), 8000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 1000000.0f;
const float lbm_u = 0.1f;
const ulong lbm_T = 180000ull;
const ulong lbm_dt = 4ull;
LBM lbm(lbm_N, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float3 center = lbm.center();
const float3x3 rotation = float3x3(float3(0, 0, 1), radians(180.0f));
Mesh* stator = read_stl(get_exe_path()+"../stl/edf_v39.stl", 1.0f, rotation); // https://www.thingiverse.com/thing:3014759/files
Mesh* rotor = read_stl(get_exe_path()+"../stl/edf_v391.stl", 1.0f, rotation); // https://www.thingiverse.com/thing:3014759/files
const float scale = 0.98f*stator->get_scale_for_box_fit(lbm.size()); // scale stator and rotor to simulation box size
stator->scale(scale);
rotor->scale(scale);
stator->translate(lbm.center()-stator->get_bounding_box_center()-float3(0.0f, 0.2f*stator->get_max_size(), 0.0f)); // move stator and rotor to simulation box center
rotor->translate(lbm.center()-rotor->get_bounding_box_center()-float3(0.0f, 0.41f*stator->get_max_size(), 0.0f));
stator->set_center(stator->get_bounding_box_center()); // set center of meshes to their bounding box center
rotor->set_center(rotor->get_bounding_box_center());
const float lbm_radius=0.5f*rotor->get_max_size(), omega=lbm_u/lbm_radius, domega=omega*(float)lbm_dt;
lbm.voxelize_mesh_on_device(stator, TYPE_S, center);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]==0u) lbm.u.y[n] = 0.3f*lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<lbm_T) { // main simulation loop
lbm.voxelize_mesh_on_device(rotor, TYPE_S, center, float3(0.0f), float3(0.0f, omega, 0.0f));
lbm.run(lbm_dt, lbm_T);
rotor->rotate(float3x3(float3(0.0f, 1.0f, 0.0f), domega)); // rotate mesh
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_centered(-70.0f+100.0f*(float)lbm.get_t()/(float)lbm_T, 2.0f, 60.0f, 1.284025f);
lbm.graphics.write_frame();
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
}
} /**/
/*void main_setup() { // aerodynamics of a cow; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 1.0f), 1000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float si_u = 1.0f;
const float si_length = 2.4f;
const float si_T = 10.0f;
const float si_nu=1.48E-5f, si_rho=1.225f;
const float lbm_length = 0.65f*(float)lbm_N.y;
const float lbm_u = 0.075f;
units.set_m_kg_s(lbm_length, lbm_u, 1.0f, si_length, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
print_info("Re = "+to_string(to_uint(units.si_Re(si_length, si_u, si_nu))));
LBM lbm(lbm_N, lbm_nu);
// ###################################################################################### define geometry ######################################################################################
const float3x3 rotation = float3x3(float3(1, 0, 0), radians(180.0f))*float3x3(float3(0, 0, 1), radians(180.0f));
Mesh* mesh = read_stl(get_exe_path()+"../stl/Cow_t.stl", lbm.size(), lbm.center(), rotation, lbm_length); // https://www.thingiverse.com/thing:182114/files
mesh->translate(float3(0.0f, 1.0f-mesh->pmin.y+0.1f*lbm_length, 1.0f-mesh->pmin.z)); // move mesh forward a bit and to simulation box bottom, keep in mind 1 cell thick box boundaries
lbm.voxelize_mesh_on_device(mesh);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(z==0u) lbm.flags[n] = TYPE_S; // solid floor
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u; // initialize y-velocity everywhere except in solid cells
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all other simulation box boundaries are inflow/outflow
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.graphics.set_camera_centered(-40.0f, 20.0f, 78.0f, 1.25f);
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 10.0f)) lbm.graphics.write_frame();
lbm.run(1u, lbm_T);
}
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // Space Shuttle; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 4.0f, 0.8f), 1000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 10000000.0f;
const float lbm_u = 0.075f;
const ulong lbm_T = 108000ull;
LBM lbm(lbm_N, 2u, 4u, 1u, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u)); // run on 2x4x1 = 8 GPUs
// ###################################################################################### define geometry ######################################################################################
const float size = 1.25f*lbm.size().x;
const float3 center = float3(lbm.center().x, 0.55f*size, lbm.center().z+0.05f*size);
const float3x3 rotation = float3x3(float3(1, 0, 0), radians(-20.0f))*float3x3(float3(0, 0, 1), radians(270.0f));
Clock clock;
lbm.voxelize_stl(get_exe_path()+"../stl/Full_Shuttle.stl", center, rotation, size); // https://www.thingiverse.com/thing:4975964/files
println(print_time(clock.stop()));
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.write_status();
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_free(float3(-1.435962f*(float)Nx, 0.364331f*(float)Ny, 1.344426f*(float)Nz), -205.0f, 36.0f, 74.0f); // top
lbm.graphics.write_frame(get_exe_path()+"export/top/");
lbm.graphics.set_camera_free(float3(-1.021207f*(float)Nx, -0.518006f*(float)Ny, 0.0f*(float)Nz), -137.0f, 0.0f, 74.0f); // bottom
lbm.graphics.write_frame(get_exe_path()+"export/bottom/");
}
lbm.run(1u, lbm_T);
}
lbm.write_status();
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // Starship; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 2.0f), 1000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_Re = 10000000.0f;
const float lbm_u = 0.05f;
const ulong lbm_T = 108000ull;
LBM lbm(lbm_N, 1u, 1u, 1u, units.nu_from_Re(lbm_Re, (float)lbm_N.x, lbm_u));
// ###################################################################################### define geometry ######################################################################################
const float size = 1.6f*lbm.size().x;
const float3 center = float3(lbm.center().x, lbm.center().y+0.05f*size, 0.18f*size);
lbm.voxelize_stl(get_exe_path()+"../stl/StarShipV2.stl", center, size); // https://www.thingiverse.com/thing:4912729/files
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.z[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_LATTICE|VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.write_status();
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 20.0f)) {
lbm.graphics.set_camera_free(float3(2.116744f*(float)Nx, -0.775261f*(float)Ny, 1.026577f*(float)Nz), -38.0f, 37.0f, 60.0f); // top
lbm.graphics.write_frame(get_exe_path()+"export/top/");
lbm.graphics.set_camera_free(float3(0.718942f*(float)Nx, 0.311263f*(float)Ny, -0.498366f*(float)Nz), 32.0f, -40.0f, 104.0f); // bottom
lbm.graphics.write_frame(get_exe_path()+"export/bottom/");
lbm.graphics.set_camera_free(float3(1.748119f*(float)Nx, 0.442782f*(float)Ny, 0.087945f*(float)Nz), 24.0f, 2.0f, 92.0f); // side
lbm.graphics.write_frame(get_exe_path()+"export/side/");
}
lbm.run(1u, lbm_T);
}
lbm.write_status();
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // Ahmed body; required extensions in defines.hpp: FP16C, FORCE_FIELD, EQUILIBRIUM_BOUNDARIES, SUBGRID, optionally INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint memory = 10000u; // available VRAM of GPU(s) in MB
const float lbm_u = 0.05f;
const float box_scale = 6.0f;
const float si_u = 60.0f;
const float si_nu=1.48E-5f, si_rho=1.225f;
const float si_width=0.389f, si_height=0.288f, si_length=1.044f;
const float si_A = si_width*si_height+2.0f*0.05f*0.03f;
const float si_T = 0.25f;
const float si_Lx = units.x(box_scale*si_width);
const float si_Ly = units.x(box_scale*si_length);
const float si_Lz = units.x(0.5f*(box_scale-1.0f)*si_width+si_height);
const uint3 lbm_N = resolution(float3(si_Lx, si_Ly, si_Lz), memory); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
units.set_m_kg_s((float)lbm_N.y, lbm_u, 1.0f, box_scale*si_length, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
const float lbm_length = units.x(si_length);
print_info("Re = "+to_string(to_uint(units.si_Re(si_width, si_u, si_nu))));
LBM lbm(lbm_N, lbm_nu);
// ###################################################################################### define geometry ######################################################################################
Mesh* mesh = read_stl(get_exe_path()+"../stl/ahmed_25deg_m.stl", lbm.size(), lbm.center(), float3x3(float3(0, 0, 1), radians(90.0f)), lbm_length);
mesh->translate(float3(0.0f, units.x(0.5f*(0.5f*box_scale*si_length-si_width))-mesh->pmin.y, 1.0f-mesh->pmin.z));
lbm.voxelize_mesh_on_device(mesh, TYPE_S|TYPE_X); // https://github.com/nathanrooy/ahmed-bluff-body-cfd/blob/master/geometry/ahmed_25deg_m.stl converted to binary
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(z==0u) lbm.flags[n] = TYPE_S;
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==Nz-1u) lbm.flags[n] = TYPE_E;
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.graphics.set_camera_centered(20.0f, 30.0f, 10.0f, 1.648722f);
lbm.run(0u, lbm_T); // initialize simulation
#if defined(FP16S)
const string path = get_exe_path()+"FP16S/"+to_string(memory)+"MB/";
#elif defined(FP16C)
const string path = get_exe_path()+"FP16C/"+to_string(memory)+"MB/";
#else // FP32
const string path = get_exe_path()+"FP32/"+to_string(memory)+"MB/";
#endif // FP32
//lbm.write_status(path);
//write_file(path+"Cd.dat", "# t\tCd\n");
while(lbm.get_t()<=lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 5.0f)) {
Clock clock;
lbm.calculate_force_on_boundaries();
lbm.F.read_from_device();
const float3 lbm_force = lbm.calculate_force_on_object(TYPE_S|TYPE_X);
const float Cd = units.si_F(lbm_force.y)/(0.5f*si_rho*sq(si_u)*si_A); // expect Cd to be too large by a factor 1.3-2.0x; need wall model
println("\r"+to_string(Cd, 3u)+" "+to_string(clock.stop(), 3u)+" ");
//write_line(path+"Cd.dat", to_string(lbm.get_t())+"\t"+to_string(Cd, 3u)+"\n");
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
//lbm.graphics.write_frame(path+"images/");
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
}
lbm.run(1u, lbm_T);
}
//lbm.write_status(path);
} /**/
/*void main_setup() { // Cessna 172 propeller aircraft; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, MOVING_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 0.8f, 0.25f), 8000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_u = 0.075f;
const float lbm_width = 0.95f*(float)lbm_N.x;
const ulong lbm_dt = 4ull; // revoxelize rotor every dt time steps
const float si_T = 1.0f;
const float si_width = 11.0f;
const float si_u = 226.0f/3.6f;
const float si_nu=1.48E-5f, si_rho=1.225f;
units.set_m_kg_s(lbm_width, lbm_u, 1.0f, si_width, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
print_info("Re = "+to_string(to_uint(units.si_Re(si_width, si_u, si_nu))));
print_info(to_string(si_T, 3u)+" seconds = "+to_string(lbm_T)+" time steps");
LBM lbm(lbm_N, units.nu(si_nu));
// ###################################################################################### define geometry ######################################################################################
Mesh* plane = read_stl(get_exe_path()+"../stl/Cessna-172-Skyhawk-body.stl"); // https://www.thingiverse.com/thing:814319/files
Mesh* rotor = read_stl(get_exe_path()+"../stl/Cessna-172-Skyhawk-rotor.stl"); // plane and rotor separated with Microsoft 3D Builder
const float scale = lbm_width/plane->get_bounding_box_size().x; // scale plane and rotor to simulation box size
plane->scale(scale);
rotor->scale(scale);
const float3 offset = lbm.center()-plane->get_bounding_box_center(); // move plane and rotor to simulation box center
plane->translate(offset);
rotor->translate(offset);
plane->set_center(plane->get_bounding_box_center()); // set center of meshes to their bounding box center
rotor->set_center(rotor->get_bounding_box_center());
const float lbm_radius=0.5f*rotor->get_max_size(), omega=-lbm_u/lbm_radius, domega=omega*(float)lbm_dt;
lbm.voxelize_mesh_on_device(plane);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
lbm.voxelize_mesh_on_device(rotor, TYPE_S, rotor->get_center(), float3(0.0f), float3(0.0f, omega, 0.0f)); // revoxelize mesh on GPU
lbm.run(lbm_dt, lbm_T); // run dt time steps
rotor->rotate(float3x3(float3(0.0f, 1.0f, 0.0f), domega)); // rotate mesh
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 5.0f)) {
lbm.graphics.set_camera_free(float3(0.192778f*(float)Nx, -0.669183f*(float)Ny, 0.657584f*(float)Nz), -77.0f, 27.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/a/");
lbm.graphics.set_camera_free(float3(0.224926f*(float)Nx, -0.594332f*(float)Ny, -0.277894f*(float)Nz), -65.0f, -14.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/b/");
lbm.graphics.set_camera_free(float3(-0.000000f*(float)Nx, 0.650189f*(float)Ny, 1.461048f*(float)Nz), 90.0f, 40.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/c/");
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
}
} /**/
/*void main_setup() { // Bell 222 helicopter; required extensions in defines.hpp: FP16C, EQUILIBRIUM_BOUNDARIES, MOVING_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 1.2f, 0.3f), 8000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_u = 0.16f;
const float lbm_length = 0.8f*(float)lbm_N.x;
const float si_T = 0.34483f; // 2 revolutions of the main rotor
const ulong lbm_dt = 4ull; // revoxelize rotor every dt time steps
const float si_length=12.85f, si_d=12.12f, si_rpm=348.0f;
const float si_u = si_rpm/60.0f*si_d*pif;
const float si_nu=1.48E-5f, si_rho=1.225f;
units.set_m_kg_s(lbm_length, lbm_u, 1.0f, si_length, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
LBM lbm(lbm_N, 1u, 1u, 1u, lbm_nu);
// ###################################################################################### define geometry ######################################################################################
Mesh* body = read_stl(get_exe_path()+"../stl/Bell-222-body.stl"); // https://www.thingiverse.com/thing:1625155/files
Mesh* main = read_stl(get_exe_path()+"../stl/Bell-222-main.stl"); // body and rotors separated with Microsoft 3D Builder
Mesh* back = read_stl(get_exe_path()+"../stl/Bell-222-back.stl");
const float scale = lbm_length/body->get_bounding_box_size().y; // scale body and rotors to simulation box size
body->scale(scale);
main->scale(scale);
back->scale(scale);
const float3 offset = lbm.center()-body->get_bounding_box_center(); // move body and rotors to simulation box center
body->translate(offset);
main->translate(offset);
back->translate(offset);
body->set_center(body->get_bounding_box_center()); // set center of meshes to their bounding box center
main->set_center(main->get_bounding_box_center());
back->set_center(back->get_bounding_box_center());
const float main_radius=0.5f*main->get_max_size(), main_omega=lbm_u/main_radius, main_domega=main_omega*(float)lbm_dt;
const float back_radius=0.5f*back->get_max_size(), back_omega=-lbm_u/back_radius, back_domega=back_omega*(float)lbm_dt;
lbm.voxelize_mesh_on_device(body);
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = 0.2f*lbm_u;
if(lbm.flags[n]!=TYPE_S) lbm.u.z[n] = -0.1f*lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_E; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
lbm.voxelize_mesh_on_device(main, TYPE_S, main->get_center(), float3(0.0f), float3(0.0f, 0.0f, main_omega)); // revoxelize mesh on GPU
lbm.voxelize_mesh_on_device(back, TYPE_S, back->get_center(), float3(0.0f), float3(back_omega, 0.0f, 0.0f)); // revoxelize mesh on GPU
lbm.run(lbm_dt, lbm_T); // run dt time steps
main->rotate(float3x3(float3(0.0f, 0.0f, 1.0f), main_domega)); // rotate mesh
back->rotate(float3x3(float3(1.0f, 0.0f, 0.0f), back_domega)); // rotate mesh
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
if(lbm.graphics.next_frame(lbm_T, 10.0f)) {
lbm.graphics.set_camera_free(float3(0.528513f*(float)Nx, 0.102095f*(float)Ny, 1.302283f*(float)Nz), 16.0f, 47.0f, 96.0f);
lbm.graphics.write_frame(get_exe_path()+"export/a/");
lbm.graphics.set_camera_free(float3(0.0f*(float)Nx, -0.114244f*(float)Ny, 0.543265f*(float)Nz), 90.0f+degrees((float)lbm.get_t()/(float)lbm_dt*main_domega), 36.0f, 120.0f);
lbm.graphics.write_frame(get_exe_path()+"export/b/");
lbm.graphics.set_camera_free(float3(0.557719f*(float)Nx, -0.503388f*(float)Ny, -0.591976f*(float)Nz), -43.0f, -21.0f, 75.0f);
lbm.graphics.write_frame(get_exe_path()+"export/c/");
lbm.graphics.set_camera_centered(58.0f, 9.0f, 88.0f, 1.648722f);
lbm.graphics.write_frame(get_exe_path()+"export/d/");
lbm.graphics.set_camera_centered(0.0f, 90.0f, 100.0f, 1.100000f);
lbm.graphics.write_frame(get_exe_path()+"export/e/");
lbm.graphics.set_camera_free(float3(0.001612f*(float)Nx, 0.523852f*(float)Ny, 0.992613f*(float)Nz), 90.0f, 37.0f, 94.0f);
lbm.graphics.write_frame(get_exe_path()+"export/f/");
}
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
}
} /**/
/*void main_setup() { // Mercedes F1 W14 car; required extensions in defines.hpp: FP16S, EQUILIBRIUM_BOUNDARIES, MOVING_BOUNDARIES, SUBGRID, INTERACTIVE_GRAPHICS or GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint3 lbm_N = resolution(float3(1.0f, 2.0f, 0.5f), 4000u); // input: simulation box aspect ratio and VRAM occupation in MB, output: grid resolution
const float lbm_u = 0.075f;
const float lbm_length = 0.8f*(float)lbm_N.y;
const float si_T = 0.25f;
const float si_u = 100.0f/3.6f;
const float si_length=5.5f, si_width=2.0f;
const float si_nu=1.48E-5f, si_rho=1.225f;
units.set_m_kg_s(lbm_length, lbm_u, 1.0f, si_length, si_u, si_rho);
const float lbm_nu = units.nu(si_nu);
const ulong lbm_T = units.t(si_T);
print_info("Re = "+to_string(to_uint(units.si_Re(si_width, si_u, si_nu))));
LBM lbm(lbm_N, 1u, 1u, 1u, lbm_nu);
// ###################################################################################### define geometry ######################################################################################
Mesh* body = read_stl(get_exe_path()+"../stl/mercedesf1-body.stl"); // https://downloadfree3d.com/3d-models/vehicles/sports-car/mercedes-f1-w14/
Mesh* front_wheels = read_stl(get_exe_path()+"../stl/mercedesf1-front-wheels.stl"); // wheels separated, decals removed and converted to .stl in Microsoft 3D Builder
Mesh* back_wheels = read_stl(get_exe_path()+"../stl/mercedesf1-back-wheels.stl"); // to avoid instability from too small gaps: remove front wheel fenders and move out right back wheel a bit
const float scale = lbm_length/body->get_bounding_box_size().y; // scale parts
body->scale(scale);
front_wheels->scale(scale);
back_wheels->scale(scale);
const float3 offset = float3(lbm.center().x-body->get_bounding_box_center().x, 1.0f-body->pmin.y+0.25f*back_wheels->get_min_size(), 4.0f-back_wheels->pmin.z);
body->translate(offset);
front_wheels->translate(offset);
back_wheels->translate(offset);
body->set_center(body->get_bounding_box_center()); // set center of meshes to their bounding box center
front_wheels->set_center(front_wheels->get_bounding_box_center());
back_wheels->set_center(back_wheels->get_bounding_box_center());
const float lbm_radius=0.5f*back_wheels->get_min_size(), omega=lbm_u/lbm_radius;
lbm.voxelize_mesh_on_device(body);
lbm.voxelize_mesh_on_device(front_wheels, TYPE_S, front_wheels->get_center(), float3(0.0f), float3(omega, 0.0f, 0.0f)); // make wheels rotating
lbm.voxelize_mesh_on_device(back_wheels, TYPE_S, back_wheels->get_center(), float3(0.0f), float3(omega, 0.0f, 0.0f)); // make wheels rotating
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(lbm.flags[n]!=TYPE_S) lbm.u.y[n] = lbm_u;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==Nz-1u) lbm.flags[n] = TYPE_E;
if(z==0u) lbm.flags[n] = TYPE_S;
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = VIS_FLAG_SURFACE|VIS_Q_CRITERION;
#if defined(GRAPHICS) && !defined(INTERACTIVE_GRAPHICS)
lbm.run(0u, lbm_T); // initialize simulation
while(lbm.get_t()<=lbm_T) { // main simulation loop
if(lbm.graphics.next_frame(lbm_T, 30.0f)) {
lbm.graphics.set_camera_free(float3(0.779346f*(float)Nx, -0.315650f*(float)Ny, 0.329444f*(float)Nz), -27.0f, 19.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/a/");
lbm.graphics.set_camera_free(float3(0.556877f*(float)Nx, 0.228191f*(float)Ny, 1.159613f*(float)Nz), 19.0f, 53.0f, 100.0f);
lbm.graphics.write_frame(get_exe_path()+"export/b/");
lbm.graphics.set_camera_free(float3(0.220650f*(float)Nx, -0.589529f*(float)Ny, 0.085407f*(float)Nz), -72.0f, 16.0f, 86.0f);
lbm.graphics.write_frame(get_exe_path()+"export/c/");
const float progress = (float)lbm.get_t()/(float)lbm_T;
const float A = 75.0f, B = -160.0f;
lbm.graphics.set_camera_centered(A+progress*(B-A), -5.0f, 100.0f, 1.648721f);
lbm.graphics.write_frame(get_exe_path()+"export/d/");
}
lbm.run(1u, lbm_T);
}
#else // GRAPHICS && !INTERACTIVE_GRAPHICS
lbm.run();
#endif // GRAPHICS && !INTERACTIVE_GRAPHICS
} /**/
/*void main_setup() { // hydraulic jump; required extensions in defines.hpp: FP16S, VOLUME_FORCE, EQUILIBRIUM_BOUNDARIES, SURFACE, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
LBM lbm(96u, 352u, 96u, 1u, 1u, 1u, 0.007f, 0.0f, 0.0f, -0.0005f);
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
const uint H1=Nz*2u/5u, H2=Nz*3u/5u, P1=Ny*1u/20u, P3=Ny*3u/20u;
if(z<H2) lbm.flags[n] = TYPE_F;
if(y<P3&&z< H1) lbm.flags[n] = TYPE_S;
if(y<P1&&z>=H2) lbm.flags[n] = TYPE_S;
if(y==1u&&z>=H1&&z<H2) {
lbm.flags[n] = TYPE_E;
lbm.rho[n] = 1.55f;
}
if(y==Ny-2u) {
lbm.flags[n] = TYPE_E;
lbm.u.y[n] = 0.2f/5.0f;
lbm.rho[n] = 0.99f;
}
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_S; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = lbm.get_D()==1u ? VIS_PHI_RAYTRACE : VIS_PHI_RASTERIZE;
lbm.run();
//lbm.run(1000u); lbm.u.read_from_device(); println(lbm.u.x[lbm.index(Nx/2u, Ny/4u, Nz/4u)]); wait(); // test for binary identity
} /**/
/*void main_setup() { // dam break; required extensions in defines.hpp: FP16S, VOLUME_FORCE, SURFACE, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
LBM lbm(128u, 256u, 256u, 0.005f, 0.0f, 0.0f, -0.0002f, 0.0001f);
// ###################################################################################### define geometry ######################################################################################
const uint Nx=lbm.get_Nx(), Ny=lbm.get_Ny(), Nz=lbm.get_Nz(); parallel_for(lbm.get_N(), [&](ulong n) { uint x=0u, y=0u, z=0u; lbm.coordinates(n, x, y, z);
if(z<Nz*6u/8u && y<Ny/8u) lbm.flags[n] = TYPE_F;
if(x==0u||x==Nx-1u||y==0u||y==Ny-1u||z==0u||z==Nz-1u) lbm.flags[n] = TYPE_S; // all non periodic
}); // ####################################################################### run simulation, export images and data ##########################################################################
lbm.graphics.visualization_modes = lbm.get_D()==1u ? VIS_PHI_RAYTRACE : VIS_PHI_RASTERIZE;
lbm.run();
} /**/
/*void main_setup() { // liquid metal on a speaker; required extensions in defines.hpp: FP16S, VOLUME_FORCE, MOVING_BOUNDARIES, SURFACE, INTERACTIVE_GRAPHICS
// ################################################################## define simulation box size, viscosity and volume force ###################################################################
const uint L = 128u;