-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSolution.cpp
executable file
·1707 lines (1477 loc) · 48.9 KB
/
MSolution.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 "MSolution.h"
#include <math.h>
#include <mpi.h>
MSolution::MSolution(MSimulationData *simdata, MOutput *output)
{
m_simdata = simdata;
m_output = output;
}
MSolution::~MSolution()
{
HandleMpiMemory(false);
}
MSolution* MSolution::_instance=0;
MSolution *MSolution::Instance(MSimulationData *simdata, MOutput *output)
{
if (_instance == 0)
_instance = new MSolution(simdata, output);
return _instance;
}
void MSolution::DestroyInstance()
{
delete _instance;
_instance=0;
}
void MSolution::AViscosity(MParticle &part)
{
MConstitutive::AViscosity(part, m_simdata->mat[ part.mat ] );
}
int MSolution::CalcCriticalTimestep()
{
MSimulationData &sd = *m_simdata;
MParticleData &par = sd.par;
MGlobalVars &gv = sd.m_globvars;
MOptionVars &ov = sd.m_optvars;
int i;
real viscosity, surfaceTensionCoeff;
for (i=gv.sph_ssp, gv.sph_critts = 1.e20; i<=gv.sph_esp; i++)
{
MParticle &part = par[i];
switch (ov.sph_tcrit_opt)
{
// Option 0: DYNA 3D formula
case 0:
PrintScreenLog("ERROR: Critical time step option not implemented yet.\n");
return ERROR;
break;
// Option 1: Use h as length
case 1:
part.critts = part.h / (part.c + part.vabs);
if (gv.sph_timestep==1) part.critts = 1.e-09;
break;
// Option 2: Use minimum interparticle distance as length
case 2:
part.critts = part.mindist / (part.c + part.vabs);
if (gv.sph_timestep==1) part.critts = 1.e-09;
break;
// Option 3: Morris et al 1997. & 2000. (surface tension) time step calculation
case 3:
viscosity = sd.mat[part.mat].av_l;
surfaceTensionCoeff = sd.mat[part.mat].surfaceTensionCoeff;
part.critts = MIN( 0.25*part.h/part.c, 0.125*SQR(part.h)/viscosity );
part.CalculateAabs(gv.sph_ndim);
part.critts = MIN( part.critts, 0.25*sqrt(part.h/part.aabs) );
// Surface tension CFL condition, according to Morris 2000.
if (ov.sph_surface_tension)
part.critts = MIN( part.critts, 0.25*sqrt(part.rho*CUBE(part.h)/2/PI/surfaceTensionCoeff) );
break;
// Error message
default:
PrintScreenLog("ERROR: Wrong critical time step option.\n");
break;
}
// Check if particle timestep is less than current
if (part.active && !part.lennardJones && !InRigidBody(part) && part.critts<gv.sph_critts)
gv.sph_critts = part.critts;
}
// Gather minimal time step results for all processes
double minimum_timestep;
MPI_Allreduce(&gv.sph_critts, &minimum_timestep, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
//MPI_Reduce(&gv.sph_critts, &minimum_timestep, 1, MPI_DOUBLE, MPI_MIN, ROOT_PROCESS, MPI_COMM_WORLD);
//MPI_Bcast(&minimum_timestep, 1, MPI_DOUBLE, ROOT_PROCESS, MPI_COMM_WORLD);
gv.sph_critts = minimum_timestep;
return OK;
}
int MSolution::Neighbours()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MOptionVars &ov = sd.m_optvars;
// 0, 1, 2 = Conventional SPH
// 3 = Total Lagrangian
switch (gv.sph_disctype)
{
case 0: case 1: case 2: case 3:
if (SetupLinkedList()!=OK) return ERROR;
// calculate coordinates and values of ghost particles
//if (LinkedListNeighbours()!=OK) return ERROR;
break;
}
return OK;
}
int MSolution::SetupLinkedList()
{
MSimulationData &sd = *m_simdata;
MNeighbourVars &nv = sd.m_neighbourvars;
MGlobalVars &gv = sd.m_globvars;
MSymPerVars &spv = sd.m_sympervars;
MParticleData &par = sd.par;
IntVector boxcrd(3);
int i;
boxcrd.clear();
// Init underlying grid of size factor*h
nv.InitGrid(gv.sph_ndim, spv.sph_boundary_type, gv.sph_coord_minmax, spv.sph_boundary_x);
// Initialize MPI process grid values
if (InitializeMpiProcess()!=OK) return ERROR;
// Now set up list, particle loop puts particle to cells
for (i=1; i<=gv.sph_np; i++)
{
// Only consider active particles
if ( !par[i].ToCompute() ) { par[i].llpointer=LINKEDLISTEND; continue; }
nv.GetBoxCoords(par[i].x, boxcrd);
// Set inactive particles which are not interesting for this process
if ( gv.sph_timestep==1 && (boxcrd(0)<box_procmin(sd.my_id) || boxcrd(0)>box_procmax(sd.my_id)) )
{ par[i].active = false; continue; }
// Each particle llpointer references to previous particle in that box
par[i].llpointer = nv.GetGridValue( boxcrd(0), boxcrd(1), boxcrd(2) );
nv.SetGridValue( boxcrd(0), boxcrd(1), boxcrd(2), i);
}
return OK;
}
int MSolution::LinkedListNeighbours()
{
MSimulationData &sd = *m_simdata;
MNeighbourVars &nv = sd.m_neighbourvars;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MGhostParticleData &gpar = sd.gpar;
IntVector boxcrd;
IntMatrix looplim;
int id_j, r, s, t;
real h_avg, dist_squared;
par.InitNeighbours(gv.sph_maxnbr);
// Loop over all particles associated to corresponding process
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute()) continue;
MParticle &par_i = par[i];
par_i.mindist = 1.0e20;
// Only do search is particle is inside the sort domain, otherwise leave nnbr = 0
// continue to next particle
// Identify which cell the particle lies in, and prevent loop over grid
// cells which do not exist
nv.GetBoxCoords(par_i.x, boxcrd);
nv.GetLoopLimits(gv.sph_ndim, boxcrd, looplim);
for (r=looplim(0,0); r<=looplim(1,0); r++)
for (s=looplim(0,1); s<=looplim(1,1); s++)
for (t=looplim(0,2); t<=looplim(1,2); t++)
{
// first particle in grid cell
id_j = nv.GetGridValue(r, s, t);
while (id_j != LINKEDLISTEND)
{
MCommonParticle &par_j = (id_j>-1) ? *(MCommonParticle *) &par[id_j] :
*(MCommonParticle *) &gpar[-id_j];
// Do not count the particle itself
if (id_j==i) { id_j = par_j.llpointer; continue; }
dist_squared = par_i.DistanceSquared(par_j, gv.sph_ndim);
h_avg = nv.m_factor * AVG(par_i.h, par_j.h);
// Check if particle is within 2h domain around i and same material
// material comparison commented out for surface tension, July 2008.
if (dist_squared<SQR(h_avg) /*&& par_j.mat==par_i.mat*/)
{
par_i.mindist = MIN(par_i.mindist, sqrt(dist_squared));
if (par_i.nnbr < gv.sph_maxnbr)
par_i.AddNeighbour(id_j);
else {
PrintScreenLog("ERROR: Neighbour list exceeded maximum.");
return ERROR;
}
}
// Go further following llpointer
id_j = par_j.llpointer;
} // while loop end
} // 3 for loops end
} // upper for loop (i)
return OK;
}
void MSolution::PrintScreenLog(const std::string &msg)
{
printf("%s\n", msg.c_str());
fprintf(m_simdata->m_filevars.f_logfile, "%s\n", msg.c_str());
}
int MSolution::FindNewMaxNeighbour()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
// Find new maxnbr
gv.sph_maxnbr = 0;
for (int i=1; i<=gv.sph_np; i++)
gv.sph_maxnbr = MAX(gv.sph_maxnbr, par[i].nnbr);
return OK;
}
int MSolution::Solution()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MOptionVars &ov = sd.m_optvars;
MOutputVars &outv = sd.m_outvars;
bool iterate = true, bPlotWritten=false;
char msg[300];
// Reserve memory for MPI transfer table
HandleMpiMemory(true);
while (iterate)
{
// Calculate new neighbour set
if (Neighbours()!=OK) return ERROR;
TransferParticles(0);
TransferParticles(1);
ExchangeData(0);
ExchangeData(1);
// Setup ghost particles
if (sd.m_optvars.sph_boundary) GhostSetup();
// Linked list neighbours after exchanging data among processes
if (LinkedListNeighbours()!=OK) return ERROR;
// Rate of deformation
if (Strain()!=OK) return ERROR;
// Update smoothing length if necessary, to be implemented
if (ov.sph_h_opt>0) UpdateH();
// Calculate normals needed for surface tension effects
if (sd.m_optvars.sph_surface_tension /*||sd.m_optvars.sph_rigid_body*/) CalculateSurfaceNormals();
// Material modeling
if (Constitutive()!=OK) return ERROR;
// Update time and set new time step
if (CalcCriticalTimestep()!=OK) return ERROR;
// Time advance
gv.sph_dtold = gv.sph_dt;
gv.sph_dt = MIN(gv.sph_tssf * gv.sph_critts, 1.1*gv.sph_dtold);
gv.sph_ptime += gv.sph_dt;
// Solve momentum equation and calculate new acceleration
if (Momentum()!=OK) return ERROR;
// Impose accel. boundary conditions
if (BoundCond()!=OK) return ERROR;
// Calculate total energy, to be implemented
//CalcTotalE();
// Write to state plot files if necessary
if ( gv.sph_ptime>outv.sph_nextsttime || gv.sph_ptime>gv.sph_endtime )
{
TransferParticlesToRoot(false);
if (sd.my_id==ROOT_PROCESS)
{
m_output->StateOutput();
sprintf(msg, "\tState plot written at time %10.4e", gv.sph_ptime);
PrintScreenLog(msg);
PlotDroppingCylinder(sd.m_filevars.sph_filein+".drp");
}
outv.sph_nextsttime += outv.sph_stpltime;
}
// Time history state output, to be implemented
// Write problem status to log file
if (sd.my_id==ROOT_PROCESS && gv.sph_timestep%gv.sph_status_interval==0 )
{
sprintf(msg, "\tProblem status for time-step %6d\t Time: %10.4e\t dt: %10.4e",
gv.sph_timestep, gv.sph_ptime, gv.sph_dt);
PrintScreenLog(msg);
}
// Update particle velocities
if (UpdateVelocity()!=OK) return ERROR;
// Update particle positions
if (MoveParticles()!=OK) return ERROR;
// Write plot output in right time
if (gv.sph_ptime>=outv.sph_plottime && !bPlotWritten) {
TransferParticlesToRoot(false);
if ( sd.my_id==ROOT_PROCESS) PlotOutput("plot_out.dat");
bPlotWritten=true;
}
// Write CPU time and memory data, to be implemented
// Increment time step counter
gv.sph_timestep++;
// Check for run termination conditions
if (gv.sph_ptime-gv.sph_dt >= gv.sph_endtime) iterate=false;
// Exchange data at the end of the time step
}
return OK;
}
// Control function calling the right one
int MSolution::Strain()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
// Half step positions back in time to that positions and velocities are held at the same time
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones) continue;
par[i].x -= 0.5 * par[i].v * gv.sph_dt;
}
switch (gv.sph_disctype)
{
// Basic SPH, Morris 1997. equal strain calculation
case 0: case 2:
ClassicStrain();
break;
// Mixed correction, to be implemented
case 1:
//CorrectedStrain();
break;
}
// Half step return
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones) continue;
par[i].x += 0.5 * par[i].v * gv.sph_dt;
}
return OK;
}
int MSolution::ClassicStrain()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MGhostParticleData &gpar = sd.gpar;
int j, id_j;
real Volj, havg, dWdr;
MKernel *pkernel;
RealVector dWdx;
RealMatrix grad_v, trans_grad_v;
// Uses the 'standard' sph approach
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones /*|| InRigidBody(par[i])*/) continue;
MParticle &par_i = par[i];
par_i.rod.clear();
par_i.spin.clear();
grad_v.clear();
for (j=0; j<par_i.nnbr; j++)
{
id_j = par_i.m_nbrlist(j);
MCommonParticle &par_j = (id_j>-1) ? *(MCommonParticle *)&par[id_j] :
*(MCommonParticle *)&gpar[-id_j];
// do not consider Lennard-Jones neighbours when computing strain
if (par_j.lennardJones) continue;
Volj = par_j.mass / par_j.rho;
havg = AVG(par_i.h, par_j.h);
//pkernel = GenerateKernel(havg);
//pkernel->GradW(dWdx, dWdr, par_i.x, par_j.x);
//delete pkernel;
MKernelBSpline::GradW(dWdx, dWdr, par_i.x, par_j.x, gv.sph_ndim, havg);
// Calculate velocity gradient
for (int r=0; r<gv.sph_ndim; r++) for (int s=0; s<gv.sph_ndim; s++)
grad_v(r,s) += Volj * (par_j.v(r)-par_i.v(r))*dWdx(s);
}
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
trans_grad_v(i,j) = grad_v(j,i);
// Rate of deformation and spin from
par_i.rod = 0.5 * (grad_v + trans_grad_v);
par_i.spin = 0.5 * (grad_v - trans_grad_v);
// ROD trace
par_i.CalcTraceROD(gv.sph_ndim);
} // for loop end
return OK;
}
// Function used to generate appropriate kernel object according to input
// The kernel object must be deleted afterwards
MKernel* MSolution::GenerateKernel(real havg)
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MKernel *pkernel=NULL;
switch (m_simdata->m_optvars.sph_krtype)
{
// Classic B-Spline kernel
case 1:
pkernel = new MKernelBSpline(gv.sph_ndim, havg);
break;
default:
PrintScreenLog("ERROR: Kernel option not supported.\n");
}
return pkernel;
}
int MSolution::Constitutive()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MMaterialData &mat = sd.mat;
// Update rho using Trace(ROD)
RhoUpdate();
// for (int i=1; i<=gv.sph_np; i++)
// {
// if (!par[i].ToCompute() || par[i].lennardJones /*|| InRigidBody(par[i])*/) continue;
// MMaterial &mater = mat[ par[i].mat ];
// MConstitutive::Constitutive(par[i], mater, &sd.m_filevars.f_logfile);
// }
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones /*|| InRigidBody(par[i])*/) continue;
MMaterial &mater = mat[ par[i].mat ];
MConstitutive::Constitutive(par[i], mater, &sd.m_filevars.f_logfile, gv.sph_dt);
}
return OK;
}
int MSolution::RhoUpdate()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones /*|| InRigidBody(par[i])*/ ) continue;
par[i].rhoold = par[i].rho;
par[i].rho *= ( 1.0 - par[i].tracerod * gv.sph_dt );
}
return OK;
}
int MSolution::Momentum()
{
MSimulationData &sd = *m_simdata;
//Update Ghost particle stress, to be implemented
switch (sd.m_globvars.sph_disctype)
{
// Basic SPH
case 0:
ClassicAcceleration();
if (sd.m_optvars.sph_lennardjones) LennardJonesAcceleration();
break;
// Mixed correction
case 1:
//CorrectedAcceleration();
break;
// According to Morris et al. 1997.
case 2:
MorrisAcceleration();
if (sd.m_optvars.sph_lennardjones) LennardJonesAcceleration();
if (sd.m_optvars.sph_rigid_body) { RigidBodyAcceleration(); /*RigidBodyForce();*/ }
break;
}
return OK;
}
int MSolution::ClassicAcceleration()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MGhostParticleData &gpar = sd.gpar;
int i, j, id, m, n;
real Volj, havg, dWdr;
RealVector dWdx(3), deltasig(3);
// svp=start velocity point, evp=end velocity point
for (i=gv.sph_svp; i<=gv.sph_evp; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones) continue;
MParticle &pari = par[i];
pari.a.clear();
for (j=0; j<pari.nnbr; j++)
{
id = pari.m_nbrlist(j);
MCommonParticle &parj = (id>-1) ? *(MCommonParticle *)&par[id] :
*(MCommonParticle *)&gpar[-id];
// do not consider Lennard-Jones neighbours when computing acceleration
if (parj.lennardJones) continue;
Volj = parj.mass/parj.rho;
havg = AVG(pari.h, parj.h);
//MKernel *pkernel = GenerateKernel(havg);
//pkernel->GradW(dWdx, dWdr, pari.x, parj.x);
//delete pkernel;
MKernelBSpline::GradW(dWdx, dWdr, pari.x, parj.x, gv.sph_ndim, havg);
deltasig.clear();
for (n=0; n<gv.sph_ndim; n++) for (m=0; m<gv.sph_ndim; m++)
deltasig(n) += ( (parj.sigma(m,n)-parj.q(m,n)) / SQR(parj.rho) +
(pari.sigma(m,n)-pari.q(m,n)) / SQR(pari.rho) ) * dWdx(m);
pari.a += parj.mass * deltasig;
}
}
return OK;
}
int MSolution::MorrisAcceleration()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MMaterialData &mat = sd.mat;
MGhostParticleData &gpar = sd.gpar;
int j, id_j, r;
real Volj, havg, dWdr, rab2, product, viscosity_i, viscosity_j;
RealVector dWdx(3);
for (int i=1; i<=gv.sph_np; i++)
{
par[i].a.clear();
if (!par[i].ToCompute() || par[i].lennardJones) continue;
//if (InRigidBody(par[i])) continue;
MParticle &pari = par[i];
viscosity_i = mat[pari.mat].av_l;
for (j=0; j<pari.nnbr; j++)
{
id_j = pari.m_nbrlist(j);
MCommonParticle &parj = (id_j>-1) ? *(MCommonParticle *)&par[id_j] :
*(MCommonParticle *)&gpar[-id_j];
// do not consider Lennard-Jones neighbours when computing acceleration
if (parj.lennardJones) continue;
viscosity_j = mat[parj.mat].av_l;
Volj = parj.mass/parj.rho;
havg = AVG(pari.h, parj.h);
MKernelBSpline::GradW(dWdx, dWdr, pari.x, parj.x, gv.sph_ndim, havg);
for (r=0, rab2=0.0, product=0.0; r<gv.sph_ndim; r++) {
rab2 += SQR(pari.x(r) - parj.x(r));
product += (pari.x(r) - parj.x(r)) * dWdx(r);
}
// First term expresses local pressure change, second expresses viscosity forces
pari.a += -parj.mass * ( parj.p/SQR(parj.rho) + pari.p/SQR(pari.rho) ) * dWdx;
if (InRigidBody(parj) && InRigidBody(pari)) continue;
pari.a += parj.mass * (viscosity_i*pari.rho + viscosity_j*parj.rho) * product /
(pari.rho*parj.rho) / (rab2+0.01*SQR(havg)) * (pari.v-parj.v);
} // for j
// Additional surface tension member, coefficient of material applied to particle i
if (sd.m_optvars.sph_surface_tension)
pari.a += -mat[pari.mat].surfaceTensionCoeff/pari.rho * pari.normalizedNormalDiv*pari.normal;
} // for i
return OK;
}
int MSolution::UpdateVelocity()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MOptionVars &ov = sd.m_optvars;
MParticleData &par = sd.par;
MGhostParticleData &gpar = sd.gpar;
int j, id_j;
real dtn, epsilon, W, Volj;
// Calculate delta t at time n
dtn = AVG(gv.sph_dt, gv.sph_dtold);
// Update velocities
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones || InRigidBody(par[i])) continue;
par[i].v += dtn * par[i].a;
par[i].CalculateVabs(gv.sph_ndim);
//////////////////// Impose cylinder velocity bound conditions/////////////////
//if (ov.bConstrainVelocityCylinder) ConstrainVelocityCylinder(par[i]);
//////////////////// Impose cylinder velocity bound conditions/////////////////
// Temporary for Couette flow - Decuzzi example
//if (par[i].x(1)>6.01e-06) par[i].v = 6.13e-03, 0, 0;
//if (par[i].x(0)>6.01e-06) par[i].v = 0, 6.13e-03, 0;
// Temporary for Morris example, prescribed velocities at the entrance, March 2010.
//if ( par[i].x(1)>1.e-03 && par[i].x(1)<1.5e-03 ) {
// real R=0.5e-03, v0=1.25e-05;
// real vy = (1 - par[i].x(0)*par[i].x(0) / (R*R))*v0;
// par[i].v.clear();
// par[i].v(1)=vy;
//}
// Temporary for shear cavity example,
//prescribed velocities at the top, May 2012.
//if ( par[i].x(2)>=0.96e-03 && par[i].x(2)<1.e-03) {
//
// par[i].v.clear();
// par[i].v(0) = 1.e-04;
// par[i].v(1) = 1.e-04;
//}
/* if ( par[i].x(1)>=1.e-03) {
par[i].v.clear();
par[i].v(0) = 1.e-03;
// par[i].v(1) = 1.e-04;
}
*/
}
// Rigid body velocity update
if (ov.sph_rigid_body) RigidBodyVelocity();
// XSPH option. Correct velocity. Note that currently v and x are held at different times
if (ov.sph_veloc_opt != 1) return OK;
epsilon = 0.05;
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones || InRigidBody(par[i]) ) continue;
MParticle &pari = par[i];
pari.smooth_v.clear();
for (j=0; j<pari.nnbr; j++)
{
id_j = pari.m_nbrlist(j);
MCommonParticle &parj = (id_j>-1) ? *(MCommonParticle *)&par[id_j] :
*(MCommonParticle *)&gpar[-id_j];
MKernel *kernel = GenerateKernel(parj.h);
W = kernel->Kernel(pari.x, parj.x);
delete kernel;
// do not consider Lennard-Jones and rigid body particles in XSPH
if (parj.lennardJones || InRigidBody(parj)) continue;
Volj = parj.mass / AVG(parj.rho,pari.rho);
pari.smooth_v += Volj * (parj.v-pari.v) * W;
}
// Finally, update the velocity
pari.v += epsilon*pari.smooth_v;
// Impose displacement boundary condition
ConstrainQuantity(pari.v, pari.dispbc);
}
return OK;
}
int MSolution::BoundCond()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MBaseAccelVars &bav = sd.m_baseaccelvars;
MParticleData &par = sd.par;
// Apply base accelerations
if (bav.sph_baseaccel)
{
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones) continue;
// Privremeno dodato da se kuglica ne bi ubrzavala (ili da se samo ona ubrzava)
if (!InRigidBody(par[i])) par[i].a += bav.sph_base_a;
}
}
// Apply nodal displacement boundary conditions
for (int i=1; i<=gv.sph_np; i++)
{
if (!par[i].ToCompute() || par[i].lennardJones) continue;
ConstrainQuantity(par[i].a, par[i].dispbc);
}
return OK;
}
// Impose displacement boundary condition on quantity (v or a)
int MSolution::ConstrainQuantity(RealVector &quantity, int code)
{
switch (code)
{
case 1:
// constrained x displacement
quantity(0) = 0.0;
break;
case 2:
// constrained y displacement
quantity(1) = 0.0;
break;
case 3:
// constrained z displacement
quantity(2) = 0.0;
break;
case 4:
// constrained x and y displacement
quantity(0) = 0.0;
quantity(1) = 0.0;
break;
case 5:
// constrained y and z displacement
quantity(1) = 0.0;
quantity(2) = 0.0;
break;
case 6:
// constrained z and x displacement
quantity(2) = 0.0;
quantity(0) = 0.0;
break;
case 7:
// constrained x, y and z displacement
quantity.clear();
break;
}
return OK;
}
int MSolution::MoveParticles()
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MParticleData &par = sd.par;
MSymPerVars &spv = sd.m_sympervars;
real overhead;
int i;
for (i=0; i<3; i++) {
gv.sph_coord_minmax(0,i) = 1.0e20;
gv.sph_coord_minmax(1,i) = -1.0e20;
}
// Update positions and store max and min values for linked list
for (i=1; i<=gv.sph_np; i++)
if (par[i].ToCompute() && !par[i].lennardJones)
par[i].x += par[i].v * gv.sph_dt;
// Treat periodic boundary and create new boundary box
for (i=1; i<=gv.sph_np; i++)
{
for (int m=0; m<gv.sph_ndim; m++)
{
// Check periodic boundary conditions
if ( spv.sph_boundary_code(1,m)==2 && par[i].x(m)>=spv.sph_boundary_x(1,m) ) {
overhead = par[i].x(m)-spv.sph_boundary_x(1,m);
par[i].x(m) = spv.sph_boundary_x(0,m) + overhead;
}
if ( spv.sph_boundary_code(0,m)==2 && par[i].x(m)<=spv.sph_boundary_x(0,m) ) {
overhead = spv.sph_boundary_x(0,m) - par[i].x(m);
par[i].x(m) = spv.sph_boundary_x(0,m) - overhead;
}
// Create new boundbox
gv.sph_coord_minmax(0,m) = MIN(gv.sph_coord_minmax(0,m), par[i].x(m));
gv.sph_coord_minmax(1,m) = MAX(gv.sph_coord_minmax(1,m), par[i].x(m));
}
}
return OK;
}
int MSolution::GhostSetup()
{
MSimulationData &sd = *m_simdata;
MNeighbourVars &nv = sd.m_neighbourvars;
MSymPerVars &spv = sd.m_sympervars;
IntVector up_limit(3), down_limit(3);
RealMatrix &minadd=spv.sph_mincoord_add, &maxadd=spv.sph_maxcoord_add;
RealVector xmin(3),xmax(3),ymin(3),ymax(3),zmin(3),zmax(3);
for (int i=0; i<3; i++) {
xmin(i) = minadd(i,0);
xmax(i) = maxadd(i,0);
ymin(i) = minadd(i,1);
ymax(i) = maxadd(i,1);
zmin(i) = minadd(i,2);
zmax(i) = maxadd(i,2);
}
sd.gpar.DeleteContents();
// Xmin
if (spv.sph_boundary_code(0,0)==2)
{
down_limit(0)=1; down_limit(1)=0; down_limit(2)=0;
up_limit(0)=1; up_limit(1)=nv.sph_gridlim(1)-1; up_limit(2)=nv.sph_gridlim(2)-1;
if ( GenerateGhostParticles(down_limit,up_limit,xmin) != OK ) return ERROR;
}
// Xmax
if (spv.sph_boundary_code(0,0)==2)
{
down_limit(0)=nv.sph_gridlim(0)-2; down_limit(1)=0; down_limit(2)=0;
up_limit(0)=nv.sph_gridlim(0)-2; up_limit(1)=nv.sph_gridlim(1)-1; up_limit(2)=nv.sph_gridlim(2)-1;
if ( GenerateGhostParticles(down_limit,up_limit,xmax) != OK ) return ERROR;
}
// Ymin
if (spv.sph_boundary_code(0,1)==2)
{
down_limit(0)=0; down_limit(1)=1; down_limit(2)=0;
up_limit(0)=nv.sph_gridlim(0)-1; up_limit(1)=1; up_limit(2)=nv.sph_gridlim(2)-1;
if ( GenerateGhostParticles(down_limit,up_limit,ymin) != OK ) return ERROR;
}
// Ymax
if (spv.sph_boundary_code(1,1)==2)
{
down_limit(0)=0; down_limit(1)=nv.sph_gridlim(1)-2; down_limit(2)=0;
up_limit(0)=nv.sph_gridlim(0)-1; up_limit(1)=nv.sph_gridlim(1)-2; up_limit(2)=nv.sph_gridlim(2)-1;
if ( GenerateGhostParticles(down_limit,up_limit,ymax) != OK ) return ERROR;
}
// Zmin
if (spv.sph_boundary_code(0,2)==2)
{
down_limit(0)=0; down_limit(1)=0; down_limit(2)=1;
up_limit(0)=nv.sph_gridlim(0)-1; up_limit(1)=nv.sph_gridlim(1)-1; up_limit(2)=1;
if ( GenerateGhostParticles(down_limit,up_limit,zmin) != OK ) return ERROR;
}
// Zmax
if (spv.sph_boundary_code(1,2)==2)
{
down_limit(0)=0; down_limit(1)=0; down_limit(2)=nv.sph_gridlim(2)-2;
up_limit(0)=nv.sph_gridlim(0)-1; up_limit(1)=nv.sph_gridlim(1)-1; up_limit(2)=nv.sph_gridlim(2)-2;
if ( GenerateGhostParticles(down_limit,up_limit,zmax) != OK ) return ERROR;
}
return OK;
}
// Generate ghost particles from particles in cells sent in limit vars
int MSolution::GenerateGhostParticles(IntVector &down_limit, IntVector &up_limit, RealVector &addVec)
{
MSimulationData &sd = *m_simdata;
MNeighbourVars &nv = sd.m_neighbourvars;
MParticleData &par = sd.par;
MGhostParticleData &gpar = sd.gpar;
int i, j, k, id, n, min_lim, max_lim;
IntVector offset(3);
offset.clear();
// check if periodic boundary is on X (domain decomposition made on X)
if (sd.m_sympervars.sph_boundary_code(0,0) != 2) {
min_lim = MAX( 0, box_procmin(sd.my_id) );
max_lim = MIN( nv.sph_gridlim(0)-1, box_procmax(sd.my_id)+1 );
}
else {
min_lim = down_limit(0);
max_lim = up_limit(0);
}
// Move cell to the other end of the model
for (n=0; n<3; n++)
if (addVec(n)!=0.0) offset(n) = SGN(addVec(n)) * (nv.sph_gridlim(n)-2);
for (k=down_limit(2); k<=up_limit(2); k++)
for (j=down_limit(1); j<=up_limit(1); j++)
for (i=min_lim; i<=max_lim; i++)
{
id = nv.GetGridValue(i,j,k);
if (id<0) continue; // if cell does not contain real particles, continue
while (id>0)
{
gpar.CreateGhostFromReal(par[id], id, addVec,
nv.GetGridValue( i+offset(0),j+offset(1),k+offset(2) ) );
id = par[id].llpointer;
}
}
return OK;
}
// Function to export quantity graph temporarily, hardwired, called by Solution()
int MSolution::PlotOutput(const std::string &filename)
{
MSimulationData &sd = *m_simdata;
MGlobalVars &gv = sd.m_globvars;
MOutputVars &outv = sd.m_outvars;
MParticleData &par = sd.par;
int i, j, num_parts=101;
real W, Volj, Volk, quantity, quantity1;
RealVector xi(3);
RealVector &start_point = outv.sph_plot_start_x;
RealVector &end_point = outv.sph_plot_end_x;
FILE *file;
// Flow between cylinders example - start_point_x=0.5e-03_d, end_point_x=2.5e-03_d
if ( (file = fopen(filename.c_str(), "wt")) == NULL )
{
printf("ERROR: Cannot open %s for plot output.\n", filename.c_str() );
return ERROR;
}
// The velocity distribution over the line
for (i=0; i<=num_parts; i++)
{
xi = start_point + i * (end_point-start_point)/num_parts;
for (j=gv.sph_ssp, quantity=0.0; j<=gv.sph_evp; j++)
{
MParticle &parj = par[j];
MKernel *pkernel = GenerateKernel(parj.h);
W = pkernel->Kernel(xi, parj.x);
delete pkernel;
Volj = parj.mass / parj.rho;
quantity += Volj * parj.rho * W;
}
fprintf(file, "%13.5e%13.5e\n", quantity, xi(0) );
}
// Contour plot for "flow past cylinders" example
/*
for (i=0; i<num_parts; i++)
for (int j=0; j<num_parts; j++)
{
RealVector offset(i*1.0e-01/num_parts, j*1.0e-01/num_parts);
xi = offset;
quantity = 0.;
quantity1 = 0.;
for (int k=gv.sph_ssp; k<=gv.sph_evp; k++)
{
MParticle &park = par[k];
MKernel *pkernel = GenerateKernel(park.h);
W = pkernel->Kernel(xi, park.x);
delete pkernel;
Volk = park.mass / park.rho;
quantity += Volk * sqrt( SQR(park.v(0)) + SQR(park.v(1)) ) * W;
pkernel = GenerateKernel(3*park.h);
W = pkernel->Kernel(xi, park.x);
delete pkernel;
quantity1 += Volk * park.p * W;
}
fprintf(file, "%13.5e%13.5e%13.5e%13.5e\n", xi(0), xi(1), quantity, quantity1);
}
// Putanja 1
for (i=0; i<num_parts; i++)
{
RealVector offset(i*1.0e-01/num_parts, 1.0e-01/2);
xi = offset;
quantity = 0.;
for (int k=gv.sph_ssp; k<=gv.sph_evp; k++)
{