-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.cpp
More file actions
1495 lines (1215 loc) · 45.7 KB
/
main.cpp
File metadata and controls
1495 lines (1215 loc) · 45.7 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
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balacing
*
*
* [TOC]
*
*
* # SPH with Dynamic load Balancing # {#SPH_dlb}
*
*
* This example show the classical SPH Dam break simulation with Load Balancing and Dynamic load balancing. With
* Load balancing and Dynamic load balancing we indicate the possibility of the system to re-adapt the domain
* decomposition to keep all the processor load and reduce idle time.
*
* \htmlonly
* <a href="#" onclick="hide_show('vector-video-3')" >Simulation video 1</a><br>
* <div style="display:none" id="vector-video-3">
* <video id="vid3" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_speed.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-4')" >Simulation video 2</a><br>
* <div style="display:none" id="vector-video-4">
* <video id="vid4" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_speed2.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-15')" >Simulation dynamic load balancing video 1</a><br>
* <div style="display:none" id="vector-video-15">
* <video id="vid15" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_dlb.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-16')" >Simulation dynamic load balancing video 2</a><br>
* <div style="display:none" id="vector-video-16">
* <video id="vid16" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_dlb2.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-17')" >Simulation countour prospective 1</a><br>
* <div style="display:none" id="vector-video-17">
* <video id="vid17" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_zoom.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-18')" >Simulation countour prospective 2</a><br>
* <div style="display:none" id="vector-video-18">
* <video id="vid18" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_back.mp4" type="video/mp4"></video>
* </div>
* <a href="#" onclick="hide_show('vector-video-19')" >Simulation countour prospective 3</a><br>
* <div style="display:none" id="vector-video-19">
* <video id="vid19" width="1200" height="576" controls> <source src="http://openfpm.mpi-cbg.de/web/images/examples/7_SPH_dlb/sph_all.mp4" type="video/mp4"></video>
* </div>
* \endhtmlonly
*
* \htmlonly
* <img src="http://ppmcore.mpi-cbg.de/web/images/examples/7_SPH_dlb/dam_break_all.jpg"/>
* \endhtmlonly
*
* ## Inclusion ## {#e7_sph_inclusion}
*
* In order to use distributed vectors in our code we have to include the file Vector/vector_dist.hpp
* we also include DrawParticles that has nice utilities to draw particles in parallel accordingly
* to simple shapes
*
* \snippet Vector/7_SPH_dlb/main.cpp inclusion
*
*/
//#define SE_CLASS1
//#define STOP_ON_ERROR
//! \cond [inclusion] \endcond
#include "Vector/vector_dist.hpp"
#include <math.h>
#include "Draw/DrawParticles.hpp"
//! \cond [inclusion] \endcond
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balacing
*
* ## SPH simulation {#e7_sph_parameters}
*
* The SPH formulation used in this example code follow these equations
*
* \f$\frac{dv_a}{dt} = - \sum_{b = NN(a) } m_b \left(\frac{P_a + P_b}{\rho_a \rho_b} + \Pi_{ab} \right) \nabla_{a} W_{ab} + g \tag{1} \f$
*
* \f$\frac{d\rho_a}{dt} = \sum_{b = NN(a) } m_b v_{ab} \cdot \nabla_{a} W_{ab} \tag{2} \f$
*
* \f$ P_a = b \left[ \left( \frac{\rho_a}{\rho_{0}} \right)^{\gamma} - 1 \right] \tag{3} \f$
*
* with
*
* \f$ \Pi_{ab} = \begin{cases} - \frac {\alpha \bar{c_{ab}} \mu_{ab} }{\bar{\rho_{ab}} } & v_{ab} \cdot r_{ab} > 0 \\ 0 & v_{ab} \cdot r_{ab} < 0 \end{cases} \tag{4}\f$
*
* and the constants defined as
*
* \f$ b = \frac{c_{s}^{2} \rho_0}{\gamma} \tag{5} \f$
*
* \f$ c_s = \sqrt{g \cdot h_{swl}} \tag{6} \f$
*
* While the particle kernel support is given by
*
* \f$ H = \sqrt{3 \cdot dp} \tag{7} \f$
*
* Explain the equations is out of the context of this tutorial. An introduction
* can be found regarding SPH in general in the original Monghagan SPH paper.
* In this example we use the sligtly modified version
* used by Dual-SPH (http://www.dual.sphysics.org/). A summary of the equation and constants can be founded in
* their User Manual and the XML user Manual.
*
* ### Parameters {#e7_sph_parameters}
*
* Based on the equation
* reported before several constants must be defined.
*
* \snippet Vector/7_SPH_dlb/main.cpp sim parameters
*
*/
/*! \cond [sim parameters] \endcond */
// A constant to indicate boundary particles
#define BOUNDARY 0
// A constant to indicate fluid particles
#define FLUID 1
// initial spacing between particles dp in the formulas
const double dp = 0.0085;
// Maximum height of the fluid water
// is going to be calculated and filled later on
double h_swl = 0.0;
// c_s in the formulas (constant used to calculate the sound speed)
const double coeff_sound = 20.0;
// gamma in the formulas
const double gamma_ = 7.0;
// sqrt(3.0*dp*dp) support of the kernel
const double H = 0.0147224318643;
// Eta in the formulas
const double Eta2 = 0.01 * H*H;
// alpha in the formula
const double visco = 0.1;
// cbar in the formula (calculated later)
double cbar = 0.0;
// Mass of the fluid particles
const double MassFluid = 0.000614125;
// Mass of the boundary particles
const double MassBound = 0.000614125;
// End simulation time
#ifdef TEST_RUN
const double t_end = 0.001;
#else
const double t_end = 1.5;
#endif
// Gravity acceleration
const double gravity = 9.81;
// Reference densitu 1000Kg/m^3
const double rho_zero = 1000.0;
// Filled later require h_swl, it is b in the formulas
double B = 0.0;
// Constant used to define time integration
const double CFLnumber = 0.2;
// Minimum T
const double DtMin = 0.00001;
// Minimum Rho allowed
const double RhoMin = 700.0;
// Maximum Rho allowed
const double RhoMax = 1300.0;
// Filled in initialization
double max_fluid_height = 0.0;
// Properties
// FLUID or BOUNDARY
const size_t type = 0;
// Density
const int rho = 1;
// Density at step n-1
const int rho_prev = 2;
// Pressure
const int Pressure = 3;
// Delta rho calculated in the force calculation
const int drho = 4;
// calculated force
const int force = 5;
// velocity
const int velocity = 6;
// velocity at previous step
const int velocity_prev = 7;
/*! \cond [sim parameters] \endcond */
/*! \cond [vector_dist_def] \endcond */
// Type of the vector containing particles
typedef vector_dist<3,double,aggregate<size_t,double, double, double, double, double[3], double[3], double[3]>> particles;
// | | | | | | | |
// | | | | | | | |
// type density density Pressure delta force velocity velocity
// at n-1 density at n - 1
/*! \cond [vector_dist_def] \endcond */
/*! \cond [model custom] \endcond */
struct ModelCustom
{
template<typename Decomposition, typename vector> inline void addComputation(Decomposition & dec,
vector & vd,
size_t v,
size_t p)
{
if (vd.template getProp<type>(p) == FLUID)
dec.addComputationCost(v,4);
else
dec.addComputationCost(v,3);
}
template<typename Decomposition> inline void applyModel(Decomposition & dec, size_t v)
{
dec.setSubSubDomainComputationCost(v, dec.getSubSubDomainComputationCost(v) * dec.getSubSubDomainComputationCost(v));
}
double distributionTol()
{
return 1.01;
}
};
/*! \cond [model custom] \endcond */
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balacing
*
* ### Equation of state {#e7_sph_equation_state}
*
* This function implement the formula 3 in the set of equations. It calculate the
* pressure of each particle based on the local density of each particle.
*
* \snippet Vector/7_SPH_dlb/main.cpp eq_state_and_ker
*
*/
/*! \cond [eq_state_and_ker] \endcond */
inline void EqState(particles & vd)
{
auto it = vd.getDomainIterator();
while (it.isNext())
{
auto a = it.get();
double rho_a = vd.template getProp<rho>(a);
double rho_frac = rho_a / rho_zero;
vd.template getProp<Pressure>(a) = B*( rho_frac*rho_frac*rho_frac*rho_frac*rho_frac*rho_frac*rho_frac - 1.0);
++it;
}
}
/*! \cond [eq_state_and_ker] \endcond */
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Cubic SPH kernel and derivatives {#e7_sph_kernel}
*
* This function define the Cubic kernel or \f$ W_{ab} \f$. The cubic kernel is
* defined as
*
* \f$ \begin{cases} 1.0 - \frac{3}{2} q^2 + \frac{3}{4} q^3 & 0 < q < 1 \\ (2 - q)^3 & 1 < q < 2 \\ 0 & q > 2 \end{cases} \f$
*
* \snippet Vector/7_SPH_dlb/main.cpp kernel_sph
*
*/
/*! \cond [kernel_sph] \endcond */
const double a2 = 1.0/M_PI/H/H/H;
inline double Wab(double r)
{
r /= H;
if (r < 1.0)
return (1.0 - 3.0/2.0*r*r + 3.0/4.0*r*r*r)*a2;
else if (r < 2.0)
return (1.0/4.0*(2.0 - r)*(2.0 - r)*(2.0 - r))*a2;
else
return 0.0;
}
/*! \cond [kernel_sph] \endcond */
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* This function define the gradient of the Cubic kernel function \f$ W_{ab} \f$.
*
* \f$ \nabla W_{ab} = \beta (x,y,z) \f$
*
* \f$ \beta = \begin{cases} (c_1 q + d_1 q^2) & 0 < q < 1 \\ c_2 (2 - q)^2 & 1 < q < 2 \end{cases} \f$
*
* \snippet Vector/7_SPH_dlb/main.cpp kernel_sph_der
*
*/
/*! \cond [kernel_sph_der] \endcond */
const double c1 = -3.0/M_PI/H/H/H/H;
const double d1 = 9.0/4.0/M_PI/H/H/H/H;
const double c2 = -3.0/4.0/M_PI/H/H/H/H;
const double a2_4 = 0.25*a2;
// Filled later
double W_dap = 0.0;
inline void DWab(Point<3,double> & dx, Point<3,double> & DW, double r, bool print)
{
const double qq=r/H;
double qq2 = qq * qq;
double fac1 = (c1*qq + d1*qq2)/r;
double b1 = (qq < 1.0)?1.0f:0.0f;
double wqq = (2.0 - qq);
double fac2 = c2 * wqq * wqq / r;
double b2 = (qq >= 1.0 && qq < 2.0)?1.0f:0.0f;
double factor = (b1*fac1 + b2*fac2);
DW.get(0) = factor * dx.get(0);
DW.get(1) = factor * dx.get(1);
DW.get(2) = factor * dx.get(2);
}
/*! \cond [kernel_sph_der] \endcond */
/*!
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Tensile correction {#e7_sph_tensile}
*
* This function define the Tensile term. An explanation of the Tensile term is out of the
* context of this tutorial, but in brief is an additional repulsive term that avoid the particles
* to get too near. Can be considered at small scale like a repulsive force that avoid
* particles to get too close like the Lennard-Jhonned potential at atomistic level. A good
* reference is the Monaghan paper "SPH without a Tensile Instability"
*
* \snippet Vector/7_SPH_dlb/main.cpp tensile_term
*
*
*/
/*! \cond [tensile_term] \endcond */
// Tensile correction
inline double Tensile(double r, double rhoa, double rhob, double prs1, double prs2)
{
const double qq=r/H;
//-Cubic Spline kernel
double wab;
if(r>H)
{
double wqq1=2.0f-qq;
double wqq2=wqq1*wqq1;
wab=a2_4*(wqq2*wqq1);
}
else
{
double wqq2=qq*qq;
double wqq3=wqq2*qq;
wab=a2*(1.0f-1.5f*wqq2+0.75f*wqq3);
}
//-Tensile correction.
double fab=wab*W_dap;
fab*=fab; fab*=fab; //fab=fab^4
const double tensilp1=(prs1/(rhoa*rhoa))*(prs1>0? 0.01: -0.2);
const double tensilp2=(prs2/(rhob*rhob))*(prs2>0? 0.01: -0.2);
return (fab*(tensilp1+tensilp2));
}
/*! \cond [tensile_term] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Viscous term {#e7_sph_viscous}
*
* This function implement the viscous term \f$ \Pi_{ab} \f$
*
* \snippet Vector/7_SPH_dlb/main.cpp viscous_term
*
*
*/
/*! \cond [viscous_term] \endcond */
inline double Pi(const Point<3,double> & dr, double rr2, Point<3,double> & dv, double rhoa, double rhob, double massb, double & visc)
{
const double dot = dr.get(0)*dv.get(0) + dr.get(1)*dv.get(1) + dr.get(2)*dv.get(2);
const double dot_rr2 = dot/(rr2+Eta2);
visc=std::max(dot_rr2,visc);
if(dot < 0)
{
const float amubar=H*dot_rr2;
const float robar=(rhoa+rhob)*0.5f;
const float pi_visc=(-visco*cbar*amubar/robar);
return pi_visc;
}
else
return 0.0;
}
/*! \cond [viscous_term] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Force calculation {#e7_force_calc}
*
* Calculate forces. It calculate equation 1 and 2 in the set of formulas
*
* \snippet Vector/7_SPH_dlb/main.cpp calc_forces
*
*
*/
/*! \cond [calc_forces] \endcond */
template<typename CellList> inline void calc_forces(particles & vd, CellList & NN, double & max_visc)
{
auto part = vd.getDomainIterator();
// Update the cell-list
vd.updateCellList(NN);
// For each particle ...
while (part.isNext())
{
// ... a
auto a = part.get();
// Get the position xp of the particle
Point<3,double> xa = vd.getPos(a);
// Take the mass of the particle dependently if it is FLUID or BOUNDARY
double massa = (vd.getProp<type>(a) == FLUID)?MassFluid:MassBound;
// Get the density of the of the particle a
double rhoa = vd.getProp<rho>(a);
// Get the pressure of the particle a
double Pa = vd.getProp<Pressure>(a);
// Get the Velocity of the particle a
Point<3,double> va = vd.getProp<velocity>(a);
// Reset the force counter (- gravity on zeta direction)
vd.template getProp<force>(a)[0] = 0.0;
vd.template getProp<force>(a)[1] = 0.0;
vd.template getProp<force>(a)[2] = -gravity;
vd.template getProp<drho>(a) = 0.0;
// We threat FLUID particle differently from BOUNDARY PARTICLES ...
if (vd.getProp<type>(a) != FLUID)
{
// If it is a boundary particle calculate the delta rho based on equation 2
// This require to run across the neighborhoods particles of a
auto Np = NN.getNNIteratorBox(NN.getCell(vd.getPos(a)));
// For each neighborhood particle
while (Np.isNext() == true)
{
// ... q
auto b = Np.get();
// Get the position xp of the particle
Point<3,double> xb = vd.getPos(b);
// if (p == q) skip this particle
if (a.getKey() == b) {++Np; continue;};
// get the mass of the particle
double massb = (vd.getProp<type>(b) == FLUID)?MassFluid:MassBound;
// Get the velocity of the particle b
Point<3,double> vb = vd.getProp<velocity>(b);
// Get the pressure and density of particle b
double Pb = vd.getProp<Pressure>(b);
double rhob = vd.getProp<rho>(b);
// Get the distance between p and q
Point<3,double> dr = xa - xb;
// take the norm of this vector
double r2 = norm2(dr);
// If the particles interact ...
if (r2 < 4.0*H*H)
{
// ... calculate delta rho
double r = sqrt(r2);
Point<3,double> dv = va - vb;
Point<3,double> DW;
DWab(dr,DW,r,false);
const double dot = dr.get(0)*dv.get(0) + dr.get(1)*dv.get(1) + dr.get(2)*dv.get(2);
const double dot_rr2 = dot/(r2+Eta2);
max_visc=std::max(dot_rr2,max_visc);
vd.getProp<drho>(a) += massb*(dv.get(0)*DW.get(0)+dv.get(1)*DW.get(1)+dv.get(2)*DW.get(2));
}
++Np;
}
}
else
{
// If it is a fluid particle calculate based on equation 1 and 2
// Get an iterator over the neighborhood particles of p
auto Np = NN.getNNIteratorBox(NN.getCell(vd.getPos(a)));
// For each neighborhood particle
while (Np.isNext() == true)
{
// ... q
auto b = Np.get();
// Get the position xp of the particle
Point<3,double> xb = vd.getPos(b);
// if (p == q) skip this particle
if (a.getKey() == b) {++Np; continue;};
double massb = (vd.getProp<type>(b) == FLUID)?MassFluid:MassBound;
Point<3,double> vb = vd.getProp<velocity>(b);
double Pb = vd.getProp<Pressure>(b);
double rhob = vd.getProp<rho>(b);
// Get the distance between p and q
Point<3,double> dr = xa - xb;
// take the norm of this vector
double r2 = norm2(dr);
// if they interact
if (r2 < 4.0*H*H)
{
double r = sqrt(r2);
Point<3,double> v_rel = va - vb;
Point<3,double> DW;
DWab(dr,DW,r,false);
double factor = - massb*((vd.getProp<Pressure>(a) + vd.getProp<Pressure>(b)) / (rhoa * rhob) + Tensile(r,rhoa,rhob,Pa,Pb) + Pi(dr,r2,v_rel,rhoa,rhob,massb,max_visc));
vd.getProp<force>(a)[0] += factor * DW.get(0);
vd.getProp<force>(a)[1] += factor * DW.get(1);
vd.getProp<force>(a)[2] += factor * DW.get(2);
vd.getProp<drho>(a) += massb*(v_rel.get(0)*DW.get(0)+v_rel.get(1)*DW.get(1)+v_rel.get(2)*DW.get(2));
}
++Np;
}
}
++part;
}
}
/*! \cond [calc_forces] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Integration and dynamic time integration {#e7_delta_time_t}
*
* This function calculate the Maximum acceleration and velocity across the particles.
* It is used to calculate a dynamic time-stepping.
*
* \snippet Vector/7_SPH_dlb/main.cpp max_acc_vel
*
*
*/
/*! \cond [max_acc_vel] \endcond */
void max_acceleration_and_velocity(particles & vd, double & max_acc, double & max_vel)
{
// Calculate the maximum acceleration
auto part = vd.getDomainIterator();
while (part.isNext())
{
auto a = part.get();
Point<3,double> acc(vd.getProp<force>(a));
double acc2 = norm2(acc);
Point<3,double> vel(vd.getProp<velocity>(a));
double vel2 = norm2(vel);
if (vel2 >= max_vel)
max_vel = vel2;
if (acc2 >= max_acc)
max_acc = acc2;
++part;
}
max_acc = sqrt(max_acc);
max_vel = sqrt(max_vel);
Vcluster<> & v_cl = create_vcluster();
v_cl.max(max_acc);
v_cl.max(max_vel);
v_cl.execute();
}
/*! \cond [max_acc_vel] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* In this example we are using Dynamic time-stepping. The Dynamic time stepping is
* calculated with the Courant-Friedrich-Lewy condition. See Monaghan 1992 "Smoothed Particle Hydrodynamic"
*
* \f$ \delta t = CFL \cdot min(t_f,t_{cv}) \f$
*
* where
*
* \f$ \delta t_f = min \sqrt{h/f_a}\f$
*
* \f$ \delta t_{cv} = min \frac{h}{c_s + max \left| \frac{hv_{ab} \cdot r_{ab}}{r_{ab}^2} \right|} \f$
*
*
* \snippet Vector/7_SPH_dlb/main.cpp dyn_stepping
*
*
*/
/*! \cond [dyn_stepping] \endcond */
double calc_deltaT(particles & vd, double ViscDtMax)
{
double Maxacc = 0.0;
double Maxvel = 0.0;
max_acceleration_and_velocity(vd,Maxacc,Maxvel);
//-dt1 depends on force per unit mass.
const double dt_f = (Maxacc)?sqrt(H/Maxacc):std::numeric_limits<int>::max();
//-dt2 combines the Courant and the viscous time-step controls.
const double dt_cv = H/(std::max(cbar,Maxvel*10.) + H*ViscDtMax);
//-dt new value of time step.
double dt=double(CFLnumber)*std::min(dt_f,dt_cv);
if(dt<double(DtMin))
dt=double(DtMin);
return dt;
}
/*! \cond [dyn_stepping] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* This function perform verlet integration accordingly to the Verlet time stepping scheme
*
* \f$ v_a^{n+1} = v_a^{n-1} + 2 \delta t F_a^{n} \f$
*
* \f$ r_a^{n+1} = \delta t V_a^n + 0.5 \delta t^2 F_a^n \f$
*
* \f$ \rho_a^{n+1} = \rho_a^{n-1} + 2 \delta t D_a^n \f$
*
* Every N Verlet steps the euler stepping scheme is choosen to avoid instabilities
*
* \f$ v_a^{n+1} = v_a^{n} + \delta t F_a^n \f$
*
* \f$ r_a^{n+1} = r_a^{n} + \delta t V_a^n + \frac{1}{2} \delta t^2 F_a^n \f$
*
* \f$ \rho_a^{n+1} = \rho_a^n + \delta t D_a^n \f$
*
* This function also check that no particles go outside the simulation
* domain or their density go dangerously out of range. If a particle go out of range is removed
* from the simulation
*
* \snippet Vector/7_SPH_dlb/main.cpp verlet_int
*
*
*/
/*! \cond [verlet_int] \endcond */
openfpm::vector<size_t> to_remove;
size_t cnt = 0;
void verlet_int(particles & vd, double dt)
{
// list of the particle to remove
to_remove.clear();
// particle iterator
auto part = vd.getDomainIterator();
double dt205 = dt*dt*0.5;
double dt2 = dt*2.0;
// For each particle ...
while (part.isNext())
{
// ... a
auto a = part.get();
// if the particle is boundary
if (vd.template getProp<type>(a) == BOUNDARY)
{
// Update rho
double rhop = vd.template getProp<rho>(a);
// Update only the density
vd.template getProp<velocity>(a)[0] = 0.0;
vd.template getProp<velocity>(a)[1] = 0.0;
vd.template getProp<velocity>(a)[2] = 0.0;
double rhonew = vd.template getProp<rho_prev>(a) + dt2*vd.template getProp<drho>(a);
vd.template getProp<rho>(a) = (rhonew < rho_zero)?rho_zero:rhonew;
vd.template getProp<rho_prev>(a) = rhop;
++part;
continue;
}
//-Calculate displacement and update position / Calcula desplazamiento y actualiza posicion.
double dx = vd.template getProp<velocity>(a)[0]*dt + vd.template getProp<force>(a)[0]*dt205;
double dy = vd.template getProp<velocity>(a)[1]*dt + vd.template getProp<force>(a)[1]*dt205;
double dz = vd.template getProp<velocity>(a)[2]*dt + vd.template getProp<force>(a)[2]*dt205;
vd.getPos(a)[0] += dx;
vd.getPos(a)[1] += dy;
vd.getPos(a)[2] += dz;
double velX = vd.template getProp<velocity>(a)[0];
double velY = vd.template getProp<velocity>(a)[1];
double velZ = vd.template getProp<velocity>(a)[2];
double rhop = vd.template getProp<rho>(a);
vd.template getProp<velocity>(a)[0] = vd.template getProp<velocity_prev>(a)[0] + vd.template getProp<force>(a)[0]*dt2;
vd.template getProp<velocity>(a)[1] = vd.template getProp<velocity_prev>(a)[1] + vd.template getProp<force>(a)[1]*dt2;
vd.template getProp<velocity>(a)[2] = vd.template getProp<velocity_prev>(a)[2] + vd.template getProp<force>(a)[2]*dt2;
vd.template getProp<rho>(a) = vd.template getProp<rho_prev>(a) + dt2*vd.template getProp<drho>(a);
// Check if the particle go out of range in space and in density
if (vd.getPos(a)[0] < 0.000263878 || vd.getPos(a)[1] < 0.000263878 || vd.getPos(a)[2] < 0.000263878 ||
vd.getPos(a)[0] > 0.000263878+1.59947 || vd.getPos(a)[1] > 0.000263878+0.672972 || vd.getPos(a)[2] > 0.000263878+0.903944 ||
vd.template getProp<rho>(a) < RhoMin || vd.template getProp<rho>(a) > RhoMax)
{
to_remove.add(a.getKey());
}
vd.template getProp<velocity_prev>(a)[0] = velX;
vd.template getProp<velocity_prev>(a)[1] = velY;
vd.template getProp<velocity_prev>(a)[2] = velZ;
vd.template getProp<rho_prev>(a) = rhop;
++part;
}
// remove the particles
vd.remove(to_remove,0);
// increment the iteration counter
cnt++;
}
void euler_int(particles & vd, double dt)
{
// list of the particle to remove
to_remove.clear();
// particle iterator
auto part = vd.getDomainIterator();
double dt205 = dt*dt*0.5;
double dt2 = dt*2.0;
// For each particle ...
while (part.isNext())
{
// ... a
auto a = part.get();
// if the particle is boundary
if (vd.template getProp<type>(a) == BOUNDARY)
{
// Update rho
double rhop = vd.template getProp<rho>(a);
// Update only the density
vd.template getProp<velocity>(a)[0] = 0.0;
vd.template getProp<velocity>(a)[1] = 0.0;
vd.template getProp<velocity>(a)[2] = 0.0;
double rhonew = vd.template getProp<rho>(a) + dt*vd.template getProp<drho>(a);
vd.template getProp<rho>(a) = (rhonew < rho_zero)?rho_zero:rhonew;
vd.template getProp<rho_prev>(a) = rhop;
++part;
continue;
}
//-Calculate displacement and update position / Calcula desplazamiento y actualiza posicion.
double dx = vd.template getProp<velocity>(a)[0]*dt + vd.template getProp<force>(a)[0]*dt205;
double dy = vd.template getProp<velocity>(a)[1]*dt + vd.template getProp<force>(a)[1]*dt205;
double dz = vd.template getProp<velocity>(a)[2]*dt + vd.template getProp<force>(a)[2]*dt205;
vd.getPos(a)[0] += dx;
vd.getPos(a)[1] += dy;
vd.getPos(a)[2] += dz;
double velX = vd.template getProp<velocity>(a)[0];
double velY = vd.template getProp<velocity>(a)[1];
double velZ = vd.template getProp<velocity>(a)[2];
double rhop = vd.template getProp<rho>(a);
vd.template getProp<velocity>(a)[0] = vd.template getProp<velocity>(a)[0] + vd.template getProp<force>(a)[0]*dt;
vd.template getProp<velocity>(a)[1] = vd.template getProp<velocity>(a)[1] + vd.template getProp<force>(a)[1]*dt;
vd.template getProp<velocity>(a)[2] = vd.template getProp<velocity>(a)[2] + vd.template getProp<force>(a)[2]*dt;
vd.template getProp<rho>(a) = vd.template getProp<rho>(a) + dt*vd.template getProp<drho>(a);
// Check if the particle go out of range in space and in density
if (vd.getPos(a)[0] < 0.000263878 || vd.getPos(a)[1] < 0.000263878 || vd.getPos(a)[2] < 0.000263878 ||
vd.getPos(a)[0] > 0.000263878+1.59947 || vd.getPos(a)[1] > 0.000263878+0.672972 || vd.getPos(a)[2] > 0.000263878+0.903944 ||
vd.template getProp<rho>(a) < RhoMin || vd.template getProp<rho>(a) > RhoMax)
{
to_remove.add(a.getKey());
}
vd.template getProp<velocity_prev>(a)[0] = velX;
vd.template getProp<velocity_prev>(a)[1] = velY;
vd.template getProp<velocity_prev>(a)[2] = velZ;
vd.template getProp<rho_prev>(a) = rhop;
++part;
}
// remove the particles
vd.remove(to_remove,0);
// increment the iteration counter
cnt++;
}
/*! \cond [verlet_int] \endcond */
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ### Probes/sensors {#e7_sph_prob_sens}
*
* This function show how to create a pressure sensor/probe on a set of specified points. To do this
* from the cell-list we just get an iterator across the neighborhood points of the sensors and we
* calculate the pressure profile. On the other hand because the sensor is in the processor domain
* of only one processor, only one processor must do this calculation. We will use the function isLocal
* to determine which processor contain the probe and only such processor will do the calculation.
*
* \warning This type of calculation is suitable if the number of probes is small (like 10) and pressure is not
* calculated every time step. In case the number of
* probes is comparable to the number of particles or the pressure is calculated every time-step than we suggest
* to create a set of "probe" particles
*
*
* \snippet Vector/7_SPH_dlb/main.cpp sens_press
*
*
*/
/*! \cond [sens_press] \endcond */
template<typename Vector, typename CellList>
inline void sensor_pressure(Vector & vd,
CellList & NN,
openfpm::vector<openfpm::vector<double>> & press_t,
openfpm::vector<Point<3,double>> & probes)
{
Vcluster<> & v_cl = create_vcluster();
press_t.add();
for (size_t i = 0 ; i < probes.size() ; i++)
{
float press_tmp = 0.0f;
float tot_ker = 0.0;
// if the probe is inside the processor domain
if (vd.getDecomposition().isLocal(probes.get(i)) == true)
{
// Get the position of the probe i
Point<3,double> xp = probes.get(i);
// get the iterator over the neighbohood particles of the probes position
auto itg = NN.getNNIteratorBox(NN.getCell(probes.get(i)));
while (itg.isNext())
{
auto q = itg.get();
// Only the fluid particles are importants
if (vd.template getProp<type>(q) != FLUID)
{
++itg;
continue;
}
// Get the position of the neighborhood particle q
Point<3,double> xq = vd.getPos(q);
// Calculate the contribution of the particle to the pressure
// of the probe
double r = sqrt(norm2(xp - xq));
double ker = Wab(r) * (MassFluid / rho_zero);
// Also keep track of the calculation of the summed
// kernel
tot_ker += ker;
// Add the total pressure contribution
press_tmp += vd.template getProp<Pressure>(q) * ker;
// next neighborhood particle
++itg;
}
// We calculate the pressure normalizing the
// sum over all kernels
if (tot_ker == 0.0)
press_tmp = 0.0;
else
press_tmp = 1.0 / tot_ker * press_tmp;
}
// This is not necessary in principle, but if you
// want to make all processor aware of the history of the calculated
// pressure we have to execute this
v_cl.sum(press_tmp);
v_cl.execute();
// We add the calculated pressure into the history
press_t.last().add(press_tmp);
}
}
/*! \cond [sens_press] \endcond */
int main(int argc, char* argv[])
{
/*!
*
* \page Vector_7_sph_dlb Vector 7 SPH Dam break simulation with Dynamic load balancing
*
* ## Main function {#e7_sph_main}
*