forked from martinjrobins/SPH-DEM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sph.h
executable file
·1842 lines (1656 loc) · 60.5 KB
/
sph.h
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
#ifndef SPH_H
#define SPH_H
#ifndef PRICE2
const double PRICE2_ALPHA = 1.5;
const double PRICE2_BETA = 0.7;
#endif
const double PRICE2_A = 4.0*(25.0-pow(PRICE2_BETA,2))/(pow(PRICE2_ALPHA,2)*(pow(PRICE2_ALPHA,2)-pow(PRICE2_BETA,2)));
const double PRICE2_B = (16 + PRICE2_A*pow(PRICE2_ALPHA,4))/pow(PRICE2_BETA,4);
#ifndef MY_CUBIC
const double MY_CUBIC_ALPHA = 1.0;
#endif
const double MY_CUBIC_A = -4.0/pow(MY_CUBIC_ALPHA,2);
#ifdef _1D_
const double WCON = 1;
const double WCON_HANN = 0.25;
const double WCON_PRICE2 = 6.0/(2.0*(PRICE2_A*pow(PRICE2_ALPHA,6)+PRICE2_B*pow(PRICE2_BETA,6)+pow(2.0,6)));
#endif
#ifdef _2D_
const double WCON = 15.0/(7.0*PI);
const double WCON_QUINTIC = 7.0/(478.0*PI);
const double WCON_HANN = 1.0/(4.0*PI-16.0/PI);
const double WCON_PRICE2 = 21.0/(PI*(PRICE2_A*pow(PRICE2_ALPHA,7)+PRICE2_B*pow(PRICE2_BETA,7)+pow(2.0,7)));
const double WCON_MY_CUBIC = 10.0/(PI*(32.0+MY_CUBIC_A*pow(MY_CUBIC_ALPHA,5)));
const double WCON_WENDLAND = 7.0/(64.0*PI);
#endif
#ifdef _3D_
const double WCON = 3.0/(2.0*PI);
const double WCON_WENDLAND = 21.0/(256.0*PI);
#endif
#include "misc.h"
const double BETA_MAX = 1.5;
const double QIN = 2.0/3.0;
const double W1 = 15.0*(2.0/3.0 - 1.0/pow(HFAC,2) + 0.5/pow(HFAC,3))/(7.0*PI);
const int NEPS = 4;
const double ALPHA = Nmisc::viscToAlpha(VISCOSITY,H,SPSOUND);
#include "demConstants.h"
#include "vect.h"
#include "particle.h"
#include "customConstants.h"
#include "globalVars.h"
#include <gsl/gsl_multifit.h>
#include <gsl/gsl_linalg.h>
#include <blitz/array.h>
namespace Nsph {
#ifdef MY_VAR_RES
//inline vect varResDx(vect newDr,vect gradV[NDIM]) {
// vect newDx;
//TODO:this is 2d!
// newDx[0] = newDr[0]*(gradV[0][0]*PSEP/MY_VAR_RES_DV+1) + newDr[1]*gradV[0][1]*PSEP/MY_VAR_RES_DV;
// newDx[1] = newDr[1]*(gradV[1][1]*PSEP/MY_VAR_RES_DV+1) + newDr[0]*gradV[1][0]*PSEP/MY_VAR_RES_DV;
// return newDx;
//}
inline vect varResDx(vect dx,vect dv,vect gradV[NDIM]) {
vect newDx;
for (int i=0;i<NDIM;i++) {
newDx[i] = dx[i] + dot(dv,gradV[i])*pow(PSEP/MY_VAR_RES_DV,2);
}
return newDx;
}
#endif
inline double F(const double q,const double h) {
#ifdef WENDLAND
if (q<=2.0) {
return (1/pow(h,NDIM+2))*WCON_WENDLAND*(-4*pow(2-q,3)*(1+2*q) + 2*pow(2-q,4))/q;
} else {
return 0.0;
}
#else
#ifdef MY_CUBIC
if (q<=MY_CUBIC_ALPHA) {
return -(1/pow(h,NDIM+2))*WCON_MY_CUBIC*3*(pow(2-q,2) + MY_CUBIC_A*pow(MY_CUBIC_ALPHA-q,2))/q;
} else if (q<=2.0) {
return -(1/pow(h,NDIM+2))*WCON_MY_CUBIC*3*pow(2-q,2)/q;
} else {
return 0.0;
}
#else
#ifdef PRICE2
if (q<=PRICE2_BETA) {
return -(1/pow(h,NDIM+2))*WCON_PRICE2*5*(pow(2-q,4) + PRICE2_A*pow(PRICE2_ALPHA-q,4) + PRICE2_B*pow(PRICE2_BETA-q,4))/q;
} else if (q<=PRICE2_ALPHA) {
return -(1/pow(h,NDIM+2))*WCON_PRICE2*5*(pow(2-q,4) + PRICE2_A*pow(PRICE2_ALPHA-q,4))/q;
} else if (q<=2.0) {
return -(1/pow(h,NDIM+2))*WCON_PRICE2*5*pow(2-q,4)/q;
} else {
return 0.0;
}
#else
#ifdef QUINTIC
if (q<=1.0) {
return -(1/pow(h,NDIM+2))*WCON_QUINTIC*5*(pow(3-q,4) - 6*pow(2-q,4) + 15*pow(1-q,4))/q;
} else if (q<=2.0) {
return -(1/pow(h,NDIM+2))*WCON_QUINTIC*5*(pow(3-q,4) - 6*pow(2-q,4))/q;
} else if (q<=3.0) {
return -(1/pow(h,NDIM+2))*WCON_QUINTIC*5*pow(3-q,4)/q;
} else {
return 0.0;
}
#else
#ifdef HANN
if (q<=2.0) {
return (1/pow(h,NDIM+2))*WCON_HANN*0.5*PI*sin(0.5*PI*(q-2.0))/q;
} else {
return 0.0;
}
#else
if (q<=1.0) {
return (1/pow(h,NDIM+2))*WCON*(-2.0+ 1.5*q);
}
else if (q<=2.0) {
return -(1/pow(h,NDIM+2))*3.0*(WCON/6.0)*pow(2.0-q,2)/q;
}
else {
return 0.0;
}
#endif
#endif
#endif
#endif
#endif
}
inline double dKdq(const double q,const double h) {
#ifdef WENDLAND
if (q<=2.0) {
return WCON_WENDLAND*(-4*pow(2-q,3)*(1+2*q) + 2*pow(2-q,4));
} else {
return 0.0;
}
#else
#ifdef MY_CUBIC
if (q<=MY_CUBIC_ALPHA) {
return -WCON_MY_CUBIC*3*(pow(2-q,2) + MY_CUBIC_A*pow(MY_CUBIC_ALPHA-q,2));
} else if (q<=2.0) {
return -WCON_MY_CUBIC*3*pow(2-q,2);
} else {
return 0.0;
}
#else
#ifdef PRICE2
if (q<=PRICE2_BETA) {
return -WCON_PRICE2*5*(pow(2-q,4) + PRICE2_A*pow(PRICE2_ALPHA-q,4) + PRICE2_B*pow(PRICE2_BETA-q,4));
} else if (q<=PRICE2_ALPHA) {
return -WCON_PRICE2*5*(pow(2-q,4) + PRICE2_A*pow(PRICE2_ALPHA-q,4));
} else if (q<=2.0) {
return -WCON_PRICE2*5*pow(2-q,4);
} else {
return 0.0;
}
#else
#ifdef QUINTIC
if (q<=1.0) {
return -WCON_QUINTIC*5*(pow(3-q,4) - 6*pow(2-q,4) + 15*pow(1-q,4));
} else if (q<=2.0) {
return -WCON_QUINTIC*5*(pow(3-q,4) - 6*pow(2-q,4));
} else if (q<=3.0) {
return -WCON_QUINTIC*5*pow(3-q,4);
} else {
return 0.0;
}
#else
#ifdef HANN
if (q<=2.0) {
return WCON_HANN*0.5*PI*sin(0.5*PI*(q-2.0));
} else {
return 0.0;
}
#else
if (q<=1.0) {
double q2 = pow(q,2);
return WCON*(-2.0*q + 1.5*q2);
}
else if (q<=2.0) {
return -3.0*(WCON/6.0)*pow(2.0-q,2);
}
else {
return 0.0;
}
#endif
#endif
#endif
#endif
#endif
}
inline double K(const double q,const double h) {
#ifdef WENDLAND
if (q<=2.0) {
return WCON_WENDLAND*pow(2.0-q,4)*(1.0+2.0*q);
} else {
return 0.0;
}
#else
#ifdef MY_CUBIC
if (q<=MY_CUBIC_ALPHA) {
return WCON_MY_CUBIC*(pow(2-q,3) + MY_CUBIC_A*pow(MY_CUBIC_ALPHA-q,3));
} else if (q<=2.0) {
return WCON_MY_CUBIC*pow(2-q,3);
} else {
return 0.0;
}
#else
#ifdef PRICE2
if (q<=PRICE2_BETA) {
return WCON_PRICE2*(pow(2-q,5) + PRICE2_A*pow(PRICE2_ALPHA-q,5) + PRICE2_B*pow(PRICE2_BETA-q,5));
} else if (q<=PRICE2_ALPHA) {
return WCON_PRICE2*(pow(2-q,5) + PRICE2_A*pow(PRICE2_ALPHA-q,5));
} else if (q<=2.0) {
return WCON_PRICE2*pow(2-q,5);
} else {
return 0.0;
}
#else
#ifdef QUINTIC
if (q<=1.0) {
return WCON_QUINTIC*(pow(3-q,5) - 6*pow(2-q,5) + 15*pow(1-q,5));
} else if (q<=2.0) {
return WCON_QUINTIC*(pow(3-q,5) - 6*pow(2-q,5));
} else if (q<=3.0) {
return WCON_QUINTIC*pow(3-q,5);
} else {
return 0.0;
}
#else
#ifdef HANN
if (q<=2.0) {
return WCON_HANN*(1.0-cos(0.5*PI*(q-2.0)));
} else {
return 0.0;
}
#else
if (q<=1.0) {
double q2 = pow(q,2);
double q3 = q*q2;
return WCON*(2.0/3.0 - q2 + 0.5*q3);
}
else if (q<=2.0) {
return (WCON/6.0)*pow(2.0-q,3);
}
else {
return 0.0;
}
#endif
#endif
#endif
#endif
#endif
}
inline double K3d(const double q,const double h) {
#ifdef WENDLAND
if (q<=2.0) {
return (21.0/(256.0*PI))*pow(2-q,4)*(1+2*q);
} else {
return 0.0;
}
#else
if (q<=1.0) {
double q2 = pow(q,2);
double q3 = q*q2;
return 3.0*(2.0/3.0 - q2 + 0.5*q3)/(2.0*PI);
}
else if (q<=2.0) {
return (1.0/(4.0*PI))*pow(2.0-q,3);
}
else {
return 0.0;
}
#endif
}
inline double pdKh(const double q,const double h) {
#ifdef WENDLAND
if (q<=2.0) {
return -WCON_WENDLAND*q*(-4*pow(2-q,3)*(1+2*q) + 2*pow(2-q,4))/h;
} else {
return 0.0;
}
#else
if (q<=1.0) {
return -WCON*(-2.0 + 1.5*q)*pow(q,2)/h;
}
else if (q<=2.0) {
return 3.0*(WCON/6.0)*pow(2.0-q,2)*q/h;
}
else {
return 0.0;
}
#endif
}
inline double W(const double q,const double h) {
return (1/pow(h,NDIM))*K(q,h);
}
inline double pdWh(const double q,const double h) {
return (1/pow(h,NDIM))*pdKh(q,h)-NDIM/pow(h,NDIM+1)*K(q,h);
}
inline double W3d(const double q,const double h) {
return (1/pow(h,3))*K3d(q,h);
}
inline double W_MLS(const vect dx,const double Wab,const double b0,const vect bRest) {
return (b0 + dot(bRest,dx))*Wab;
}
inline vect gradW(const Cparticle &pa, const Cparticle &pb, const vect dr, const double q, const double h) {
vect drF = 0.0;
if (q!=0) drF = dr*F(q,h);
#ifdef CORRECTED_GRADIENT
const vect ret = product(pa.invM,(drF-pa.sumGrad/pa.sum)/pa.sum);
#else
//if (q==0) cout<<"q=0, r = "<<pa.r<<" iam = "<<pa.iam<<" iam = "<<pb.iam<<" tag = "<<pa.tag<<" tag = "<<pb.tag<<endl;
const vect ret = drF;
#endif
#ifdef VAR_H_CORRECTION
return ret/pa.omega;
#else
#ifdef VAR_H_CORRECTION2
//return 0.5*(1.0+pdWh(q,h)*pb.mass*pb.h/pb.dens)*ret;
//cout << ret << pdWh(q,h)*pa.gradH<<endl;
return (ret + pdWh(q,h)*pa.gradH);
#else
return ret;
#endif
#endif
}
inline void calcB_MLS_iteration(const vect &dx, const double &vWab, gsl_matrix *matA, gsl_vector *dxExt) {
gsl_vector_set(dxExt,0,1.0);
for (int j=0;j<NDIM;j++) {
gsl_vector_set(dxExt,j+1,dx[j]);
}
for (int j=0;j<NDIM+1;j++) {
for (int k=0;k<NDIM+1;k++) {
gsl_matrix_set(matA,j,k,gsl_matrix_get(matA,j,k)+gsl_vector_get(dxExt,j)*gsl_vector_get(dxExt,k)*vWab);
}
}
}
inline void calcB_MLS(Cparticle &p,vector<Cparticle *> &neighbrs,double &b0,vect &bRest,vector<double> &vWab) {
int n = neighbrs.size();
vWab.resize(n+1);
gsl_matrix *matA = gsl_matrix_alloc(NDIM+1,NDIM+1);
gsl_matrix_set_all(matA,0.0);
gsl_vector *dxExt = gsl_vector_alloc(NDIM+1);
vWab[0] = W(0.0,p.h);
vect dx = 0.0;
calcB_MLS_iteration(dx,p.mass*vWab[0]/p.dens,matA,dxExt);
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
vect dx = p.r-pn->r;
//double hav = 0.5*(p.h+pn->h);
//vWab[i] = W(len(dx)/hav,hav);
vWab[i+1] = W(len(dx)/p.h,p.h);
calcB_MLS_iteration(dx,pn->mass*vWab[i+1]/pn->dens,matA,dxExt);
}
gsl_matrix *invA = gsl_matrix_alloc(NDIM+1,NDIM+1);
gsl_permutation *perm = gsl_permutation_alloc(NDIM+1);
int tmp;
gsl_linalg_LU_decomp(matA,perm,&tmp);
gsl_linalg_LU_invert(matA,perm,invA);
b0 = gsl_matrix_get(invA,0,0);
for (int i=0;i<NDIM;i++) {
bRest[i] = gsl_matrix_get(invA,i+1,0);
}
gsl_matrix_free(matA);
gsl_matrix_free(invA);
gsl_permutation_free(perm);
gsl_vector_free(dxExt);
}
#ifdef CORRECTED_GRADIENT
inline void calcInvM(Cparticle &p,vector<Cparticle *> &neighbrs,CglobalVars &g) {
p.invM = 0.0;
p.sum = (p.mass/p.dens)*W(0,p.h);
p.sumGrad = 0.0;
int n = neighbrs.size();
if (n == 0) {
for (int i=0;i<NDIM;i++) p.invM[NDIM*i+i] = 1.0;
return;
}
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
if ((pn->iam==sph)||(pn->iam==sphBoundary)) {
vect dr = p.r-pn->r;
double r = len(dr);
const double dvol = pn->mass/pn->dens;
double hav = 0.5*(p.h+pn->h);
const double scaleR = r/hav;
const double Fa = F(scaleR,hav);
const double Wab = W(scaleR,hav);
p.sum += dvol*Wab;
p.sumGrad += dvol*dr*Fa;
}
}
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
if ((pn->iam==sph)||(pn->iam==sphBoundary)) {
vect dr = p.r-pn->r;
double r = len(dr);
const double dvol = pn->mass/pn->dens;
double hav = 0.5*(p.h+pn->h);
const double scaleR = r/hav;
const double Fa = F(scaleR,hav);
const vect gradW = (dr*Fa-p.sumGrad/p.sum)/p.sum;
p.invM -= (pn->mass/pn->dens)*outerProduct(gradW,dr);
}
}
//if (p.tag ==2101) cout<<"M = "<<p.invM<<endl;
inverseSym(p.invM);
//if (p.tag ==2101) cout<<"M-1 = "<<p.invM<<endl;
// cout <<sum<<' '<<p.invM<<endl;
}
#endif
#ifdef VAR_H_CORRECTION
inline void calcOmega(Cparticle &p,vector<Cparticle *> &neighbrs,CglobalVars &g) {
p.omega = p.mass*pdWh(0,p.h);
int n = neighbrs.size();
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
if ((pn->iam==sph)||(pn->iam==sphBoundary)) {
vect dr = p.r-pn->r;
double r = len(dr);
//double hav = 0.5*(p.h+pn->h);
p.omega += pn->mass*pdWh(r/p.h,p.h);
}
}
p.omega = 1+p.h*p.omega/(p.dens*NDIM);
}
#endif
#ifdef VAR_H_CORRECTION2
#ifdef LIQ_DEM
inline void initGradH(Cparticle &p,CglobalVars &g) {
p.gradH = 0.0;
}
inline void calcGradPorosity(Cparticle &p,vector<Cparticle *> &neighbrs,CglobalVars &g) {
if (p.shepSum < 0.0) return;
int n = neighbrs.size();
#ifdef _2D_
#ifdef _2D_DEM
const double vol = PI*pow(DEM_RADIUS,2);
#else
const double vol = (4.0/3.0)*PI*pow(DEM_RADIUS,3)/(2.0*DEM_RADIUS);
#endif
#else
const double vol = (4.0/3.0)*PI*pow(DEM_RADIUS,3);
#endif
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
if (pn->iam==dem) cout <<"dem dem dem!!!!!!!!!!!!!!!!!!!!!"<<endl;
const vect dr = pn->r-p.r;
double r = len(dr);
pn->gradH -= vol*gradW(p,*pn,dr,2.0*r/LIQ_DEM_COUPLING_RADIUS,LIQ_DEM_COUPLING_RADIUS/2.0);
}
}
inline void finaliseGradH(Cparticle &p,CglobalVars &g) {
p.gradH *= -p.h/(NDIM*p.porosity);
//p.f -= p.vhat*(p.dporositydt/p.porosity + p.dddt/p.dens);
//p.ff -= p.vhat*(p.dporositydt/p.porosity + p.dddt/p.dens);
}
inline void calcGradPorosity2(Cparticle &p,vector<Cparticle *> &neighbrs,CglobalVars &g) {
int n = neighbrs.size();
#ifdef _2D_
#ifdef _2D_DEM
const double vol = PI*pow(DEM_RADIUS,2);
#else
const double vol = (4.0/3.0)*PI*pow(DEM_RADIUS,3)/(2.0*DEM_RADIUS);
#endif
#else
const double vol = (4.0/3.0)*PI*pow(DEM_RADIUS,3);
#endif
p.gradH = 0.0;
for (int i=0;i<n;i++) {
Cparticle *pn = neighbrs[i];
const vect dr = p.r-pn->r;
double r = len(dr);
if (pn->shepSum < 0.0) continue;
p.gradH -= vol*gradW(*pn,p,dr,2.0*r/LIQ_DEM_COUPLING_RADIUS,LIQ_DEM_COUPLING_RADIUS/2.0);
}
}
#endif
#endif
/*
inline void calc_aom(Cparticle &pa,Cparticle &pb,CglobalVars &g) {
double qa = len(pa.r-pb.r)/pa.h;
pa.aom -= (1.0/(NDIM*pow(HFAC,NDIM)*pa.mass))*pb.mass*(dKdq(qa,pa.h)*qa+NDIM*K(qa,pa.h));
//cout << "aom increment = "<<(1.0/(NDIM*pow(HFAC,NDIM)*pa.mass))*pb.mass*(Csph::dKdq(qa,pa.h)*qa+NDIM*Csph::K(qa,pa.h))<<endl;
//cout << "right most bit = "<<(Csph::dKdq(qa,pa.h)*qa+NDIM*Csph::K(qa,pa.h))<<endl;
//pa.aom -= (1/NDIM)*(1/pow(HFAC,NDIM))*(pb.mass/pa.mass)*(Csph::dKdq(qa,pa.h)*qa+NDIM*Csph::K(qa,pa.h));
//pa.aom = 1;
}
*/
/*
inline void init_aom(Cparticle &p,CglobalVars &g) {
p.aom= 1;
calc_aom(p,p,g);
}
*/
/*
inline void calc_dhdt_and_dalphdt(Cparticle &p,CglobalVars &g) {
double graddotv = -p.dddt/p.dens;
p.dhdt = 0.5*p.h*graddotv;
double tou = p.h/(0.1*p.maxvsig);
double s = max(-graddotv,0.0)*(2.0-p.alpha);
p.dalphdt = -(p.alpha-MIN_ALPHA)/tou + s;
}
*/
inline double courantCondition(const double h, const double vsig) {
//return 0.8*h/vsig;
#ifdef HALF_COURANT
return 0.3*h/vsig;
#else
return 0.6*h/vsig;
#endif
}
inline double viscDiffusionCondition(const double h, const double viscosity) {
#ifdef VISC_MONAGHAN
return 0.06*pow(h,2)/viscosity;
#else
return 0.125*pow(h,2)/viscosity;
#endif
}
inline double accelCondition(const double h, const double accel) {
return 0.3*sqrt(h/accel);
}
inline void accelCondition(Cparticle &pa, CglobalVars &g) {
g.newDt = min(g.newDt,0.25*sqrt(pa.h/len(pa.f)));
}
inline void calcViscEnergy(Cparticle &pa, Cparticle &pb, CglobalVars &g, const vect fv) {
if (pb.iam == sphBoundary) {
pa.deViscBdt -= dot(pa.v,fv);
} else {
pa.deViscFdt -= dot(pa.v,fv);
}
}
inline void calcViscForce(Cparticle &pa,Cparticle &pb,CglobalVars &g,const vect gradWa, const vect dv,const double r) {
vect dx = pa.r-pb.r;
double vdr = dot(dx,dv);
double hav = 0.5*(pa.h+pb.h);
vect fv = 0.0;
double vsig = 0.0;
#ifdef VISC_MORRIS
double visc = VISCOSITY*(pa.dens+pb.dens)/(pa.dens*pb.dens);
#ifdef SLK
vsig = 2.0*SPSOUND*r/len(pa.currR-pb.currR);
#else
vsig = 2.0*SPSOUND;
#endif
#ifdef LIQ_DEM
visc = 2.0*VISCOSITY*DENS/(pa.dens*pb.dens);
//vsig *= 0.5/sqrt(pa.porosity)+0.5/sqrt(pb.porosity);
#endif
fv = dv*pb.mass*visc*dot(dx,gradWa)/(pow(r,2)+0.01*pow(hav,2));
#endif
#ifdef VISC_CLEARY
double visc = 19.8*VISCOSITY*vdr/((pa.dens+pb.dens)*(pow(r,2)+0.01*pow(hav,2)));
vsig = 2.0*SPSOUND;
#ifdef LIQ_DEM
//vsig *= 0.5/sqrt(pa.porosity)+0.5/sqrt(pb.porosity);
#endif
fv = pb.mass*visc*gradWa;
#endif
#ifdef VISC_MONAGHAN
#ifdef SLK
double dr = 0.0;
vect newDx = pa.currR-pb.currR;
double newVdr = dot(newDx,dv);
double newR = len(newDx);
if (newR!=0.0) dr = 1/newR;
double viss = newVdr*dr;
vsig = 2.0*(SPSOUND + abs(viss))*r/len(pa.currR-pb.currR);
#else
double dr = 0.0;
if (r!=0.0) dr = 1/r;
double viss = vdr*dr;
#ifdef LIQ_DEM
//double vsig = SPSOUND*(1.0/sqrt(pa.porosity)+1.0/sqrt(pb.porosity)) + 2.0*abs(viss);
#else
vsig = 2.0*SPSOUND + 2.0*abs(viss);
#endif
#endif
//cout << "abs(viss) = "<<abs(viss)<<" avspsound = "<<0.5*(pa.spsound+pb.spsound)<<endl;
//double vsig = 2.0*SPSOUND;
//if (vsig > pa.maxvsig) pa.maxvsig = vsig;
double visc = viss*vsig*ALPHA/(pa.dens+pb.dens);
fv = pb.mass*visc*gradWa;
#endif
#ifdef VISC_ARTIFICIAL
double artificial_vsig = 0;
double artificial_visc = 0.0;
if (vdr<0) {
double dr = 0.0;
if (r!=0.0) dr = 1/r;
double viss = vdr*dr;
// vsig = SPSOUND*(1.0/sqrt(pa.porosity)+1.0/sqrt(pb.porosity) - 4.0*viss);
artificial_vsig = 2.0*SPSOUND - 4.0*viss;
artificial_visc = viss*artificial_vsig*ALPHA_ARTIFICIAL/(pa.dens+pb.dens);
}
pa.f += pb.mass*artificial_visc*gradWa;
vsig = max(vsig,artificial_vsig);
#endif
g.newDt = min(g.newDt,courantCondition(hav,vsig));
//g.newDt = min(g.newDt,courantCondition(hav,2*SPSOUND));
g.newDt = min(g.newDt,viscDiffusionCondition(hav,VISCOSITY));
pa.fv += fv;
#ifdef LIQ_DEM
#ifdef LIQ_DEM_SEPARATE_DRAGS
fv *= pa.porosity;
#endif
#endif
pa.f += fv;
//calcViscEnergy(pa,pb,g,fv);
}
inline void calcElasticEnergy(Cparticle &pa, Cparticle &pb, CglobalVars &g, const double Fa, const double antic) {
double vdr = dot(pa.v-pb.v,pa.r-pb.r);
double ufac = vdr*Fa;
double dudtinc = pb.mass*(pa.pdr2+0.5*antic)*ufac;
pa.dudt += dudtinc;
//if (pb.iam == sphBoundary) {
// dudtinc = pa.mass*(pb.pdr2+0.5*antic)*ufac;
// pb.dudt += dudtinc;
//}
}
inline void calcBoundaryEnergy(Cparticle &pa, Cparticle &pb, CglobalVars &g, const vect fb) {
pa.deBForcedt -= dot(pa.v,fb);
}
#ifdef BACKGROUND_PRESSURE_FORCE
inline void addBackgroundPressure(Cparticle &pa,CglobalVars &g) {
for (int i=0;i<NDIM;i++) {
pa.fp[i] += BGP_ACCEL[i];
pa.f[i] += BGP_ACCEL[i];
}
}
#endif
inline void calcPressForce(Cparticle &pa, Cparticle &pb,CglobalVars &g, const vect gradWa, const vect gradWb,const double kdwPowNeps) {
vect dx = pa.r-pb.r;
#ifdef DDENS_VARIANT
vect prfac = 2.0*sqrt(pa.press*pb.press)/(pa.dens*pb.dens)*gradWa;
#else
vect prfac = (pa.pdr2*gradWa + pb.pdr2*gradWb);
#endif
vect antic = 0.01*abs(pa.pdr2+pb.pdr2)*kdwPowNeps;
#ifdef DIRECT_SMOOTHING
prfac = prfac + antic - (EPSILON/2.0)*len2(pa.v-pb.v)/DENS;
#else
prfac = prfac + antic;
#endif
//if ((pa.tag == 1)&&(pb.tag == 11)) cout << "prfac = "<<prfac<<" antic = "<<antic<<" dens = "<<pa.dens<<" press = "<<pa.press<<" dens b = "<<pb.dens<<" press b = "<<pb.press<<endl;
//if ((pa.tag == 1)&&(pb.tag == 11)) cout << "pb.mass = "<<pb.mass<<" gradWa = "<<gradWa <<endl;
vect fp = -pb.mass*prfac;
//if ((pa.tag == 1)&&(pb.tag == 11)) {
// cout << "fp = "<<fp<<" pa.pdr2 = "<<pa.pdr2<<" pb.pdr2 = "<<pb.pdr2<<" pb.iam = "<<pb.iam<<" p.y = "<<pb.r[1]<<" b.tag == "<<pb.tag<<"pb.r = "<<pb.r<<" pa.r = "<<pa.r<<endl;
//}
#ifdef DENS_DIFFUSE_PRESS
double r = len(dx);
vect dv = pa.v-pb.v;
double vdr = dot(dv,dx);
double dr = 0.0;
if (r!=0.0) dr = 1/r;
double viss = vdr*dr;
//cout << "abs(viss) = "<<abs(viss)<<" avspsound = "<<0.5*(pa.spsound+pb.spsound)<<endl;
double vsig = 2.0*SPSOUND + 2.0*abs(viss);
//double vsig = 2.0*SPSOUND;
//if (vsig > pa.maxvsig) pa.maxvsig = vsig;
double pressDiff = 2.0*ALPHA*vsig*(pa.dens-pb.dens)*(pa.pdr2-pb.pdr2)/(pa.dens+pb.dens);
fp -= pb.mass*pressDiff*gradWa;
#endif
pa.fp += fp;
#ifdef LIQ_DEM
#ifdef LIQ_DEM_SEPARATE_DRAGS
fp *= pa.porosity;
#endif
#endif
pa.f += fp;
}
inline void calcNormal(Cparticle &pa, Cparticle &pb,const vect &dx,const double &r,vect &normp,vect &normt) {
if (all(pb.norm2==0)) {
normp = pb.norm1;
#ifdef _3D_
normt = pb.norm3;
#endif
return;
}
float rdot1 = dot(dx,pb.norm1);
float rdot2 = dot(dx,pb.norm2);
float rntest = r*r*dot(pb.norm1,pb.norm2) - rdot1*rdot2;
if (rntest <= 0) {
//use radial vector
normp = dx/r;
//This assumes that pb.norm3 is parallel to the edge
//normt = cross(normp,pb.norm3);
#ifdef _3D_
normt = pb.norm3;
#endif
return;
} else {
//use either normal
int con;
if (pb.concave) {
con = 1;
} else {
con = -1;
}
if (con*(rdot1-rdot2) > 0) {
normp = pb.norm1;
#ifdef _3D_
normt = pb.norm3;
#endif
return;
} else {
normp = pb.norm2;
#ifdef _3D_
normt = pb.norm3;
#endif
return;
}
}
}
inline void calcRadialBoundaryForces2D(Cparticle &p, Cparticle &bp,CglobalVars &g) {
const double bforce = 1;
vect fb = 0.02*pow(SPSOUND,2)*bforce/(p.mass+bp.mass);
p.f += fb;
p.fb += fb;
}
inline void calcBoundaryForces(Cparticle &p, Cparticle &bp,CglobalVars &g,const vect normp, const vect normt,const double kdwPowNeps) {
vect dx = p.r-bp.r;
vect dv = p.vhat-bp.vhat;
double rperp = dot(normp,dx);
vect rtang = dx - rperp*normp;
double ptang[2];
double pdist[2];
const double rDelP = 1.0/(BFAC*PSEP);
#ifdef _3D_
ptang[0] = abs(dot(rtang,normt));
ptang[1] = len(cross(rtang,normt));
#else
ptang[0] = len(rtang);
ptang[1] = 0;
#endif
pdist[0] = ptang[0]*rDelP;
pdist[1] = ptang[1]*rDelP;
if ((pdist[0] <= 1.0)&&(pdist[1] <= 1.0)) {
const double pesky = 0.25*(1+cos(PI*pdist[0]))*(1+cos(PI*pdist[1]));
double hav = 0.5*(p.h+bp.h);
#ifdef MY_VAR_RES
vect gradV[NDIM];
for (int i=0;i<NDIM;i++) {
gradV[i]=0.5*(p.gradV[i]+bp.gradV[i]);
}
//TODO:This is 2D
vect newDv;
newDv[0] = dx[0]*gradV[0][0] + dx[1]*gradV[1][0];
newDv[1] = dx[0]*gradV[0][1] + dx[1]*gradV[1][1];
double q = (abs(rperp)+len(newDv))/hav;
#else
//double q = abs(rperp)/hav;
#endif
/*
double bfact = 1.0/abs(rperp);
double bforce = 0.0;
if (q <= QIN) {
bforce = bfact*QIN;
} else if (q <= 1.0) {
bforce = bfact*(2.0*q - 1.5*q*q);
} else if (q <= 2.0) {
bforce = bfact*0.5*pow(2.0-q,2);
}
int srperp = 1;
if (rperp < 0) srperp = -1;
double coef = 0.01*pow(SPSOUND,2)*(1.0+kdwPowNeps);
double amassrat = 2.0*p.mass/(p.mass+bp.mass);
double bff = coef*bforce*pesky*amassrat;
vect fb = srperp*bff*norm;
*/
//double q = abs(rperp)/(2.0*hav);
double q = abs(rperp)/(1.0*PSEP);
vect fb = 0.0;
if (q < 2.0) {
double epsilon = 1;
#ifdef MODIFY_BFORCE_WITH_STILL_LEVEL
epsilon = 0;
#ifdef _3D_
double vert = bp.r[2]-STILL_LEVEL;
#else
double vert = bp.r[1]-STILL_LEVEL;
#endif
if (vert > 0) {
epsilon = 0.02;
} else if (vert>-STILL_LEVEL) {
epsilon = abs(vert/STILL_LEVEL) + 0.02;
} else {
epsilon = 1.0;
}
double normVel = dot(dv,normp);
if (normVel > 0) {
epsilon += 0;
} else if (normVel > -SPSOUND/20.0) {
epsilon += -20.0*normVel/SPSOUND;
} else {
epsilon += 1.0;
}
const double bigA = 0.01*pow(SPSOUND,2);
#else
int beta = 0;
if (dot(dx,dv) < 0) beta = 1;
const double bigA = 0.01*pow(SPSOUND,2) - beta*SPSOUND*dot(dv,normp);
#endif
double bfact = 1.0/abs(rperp);
//double bfact = 1.0/hav;
double bforce = 0.0;
if (q <= QIN) {
bforce = bfact*QIN;
} else if (q <= 1.0) {
bforce = bfact*(2.0*q - 1.5*q*q);
} else if (q <= 2.0) {
bforce = bfact*0.5*pow(2.0-q,2);
}
const double bigR = bigA*bforce;
//const double bigR = bigA*(1-0.5*q)/(hav*sqrt(0.5*q));
const double amassrat = 2.0*p.mass/(p.mass+bp.mass);
int sign = 1;
if (rperp<0) {
sign = -1;
}
fb = sign*amassrat*normp*bigR*pesky*epsilon;
//if (len(fb)>4000) {
// cout <<"fb = "<<fb<<"pb.r = "<<bp.r<<"bp.norm1 = "<<bp.norm1<<"pb.norm2 = "<<bp.norm2<<"bp.norm3 = "<<bp.norm3<<" normp = "<<normp<<" normt = "<<normt<<endl;
//}
}
p.fb += fb;
#ifdef LIQ_DEM
#ifdef LIQ_DEM_SEPARATE_DRAGS
fb *= p.porosity;
#endif
#endif
p.f += fb;
//p.debfor += -bff*(bp.norm[0]*p.v[0] + bp.norm[1]*p.v[1]);
//p.minDt = min(p.minDt,0.5*abs(rperp)/sqrt(coef));
//g.newDt = min(g.newDt,0.5*abs(rperp)/sqrt(coef));
//g.newDt = min(g.newDt,0.5*abs(rperp)/sqrt(bigA));
//if (p.tag==2204) cout << "norm = "<<norm<<" pdist = "<<pdist<<" fb = "<<fb<<" p.r = "<<p.r<<" bp.r = "<<bp.r<<endl;
}
}
inline void calcBoundaryForces2D(Cparticle &p, Cparticle &bp,CglobalVars &g,const vect norm, const double kdwPowNeps) {
vect dx = p.r-bp.r;
double rperp = norm[0]*dx[0] + norm[1]*dx[1];
double rtang = norm[0]*dx[1] - norm[1]*dx[0];
double pdist = abs(rtang/BFAC*PSEP);
if (pdist <= 1.0) {
double pesky = 1.0-pdist;
double hav = 0.5*(p.h+bp.h);
#ifdef MY_VAR_RES
vect gradV[NDIM];
for (int i=0;i<NDIM;i++) {
gradV[i]=0.5*(p.gradV[i]+bp.gradV[i]);
}
//TODO:This is 2D
vect newDv;
newDv[0] = dx[0]*gradV[0][0] + dx[1]*gradV[1][0];
newDv[1] = dx[0]*gradV[0][1] + dx[1]*gradV[1][1];
double q = (abs(rperp)+len(newDv))/hav;
#else
double q = abs(rperp)/hav;
#endif
double bfact = 1.0/abs(rperp);
double bforce = 0.0;
if (q <= QIN) {
bforce = bfact*QIN;
} else if (q <= 1.0) {
bforce = bfact*(2.0*q - 1.5*q*q);
} else if (q <= 2.0) {
bforce = bfact*0.5*pow(2.0-q,2);
}
int srperp = 1;
if (rperp < 0) srperp = -1;
double coef = 0.01*pow(SPSOUND,2)*(1.0+kdwPowNeps);
double amassrat = 2.0*bp.mass/(bp.mass+p.mass);
double bff = coef*bforce*pesky*amassrat;
vect fb = srperp*bff*norm;
p.f += fb;
p.fb += fb;
//p.debfor += -bff*(bp.norm[0]*p.v[0] + bp.norm[1]*p.v[1]);
//p.minDt = min(p.minDt,0.5*abs(rperp)/sqrt(coef));
g.newDt = min(g.newDt,0.5*abs(rperp)/sqrt(coef));
//if (p.tag==2204) cout << "norm = "<<norm<<" pdist = "<<pdist<<" fb = "<<fb<<" p.r = "<<p.r<<" bp.r = "<<bp.r<<endl;
}
}
inline void calcVortSPHSum(Cparticle &p, vector<Cparticle *> &neighbrs,CglobalVars &g) {
//TODO: currently only supports 2D, make it more general
double correctionTerm = 0;
p.vort = 0;
for (int i=0;i<neighbrs.size();i++) {
if (neighbrs[i]->iam == sph) {
Cparticle *pn = neighbrs[i];
vect dr = p.r-pn->r;
vect dv = p.v-pn->v;
double r2 = len2(dr);
double r = sqrt(r2);
double hav = 0.5*(p.h+pn->h);
double Fa = F(r/hav,hav);
//Here is the 2D specific line
p.vort += pn->mass*Fa*(dv[0]*dr[1]-dv[1]*dr[0])/pn->dens;
correctionTerm -= 0.5*pn->mass*r2*Fa/pn->dens;
}
}
if (correctionTerm != 0) p.vort /= correctionTerm;
}
#ifdef SLK
inline void calcGLeastSquares(Cparticle &p, vector<Cparticle *> &neighbrs,CglobalVars &g) {
double chisq;
int n = neighbrs.size();
gsl_matrix *X = gsl_matrix_alloc(n,NDIM);
gsl_vector *y = gsl_vector_alloc(n);