-
Notifications
You must be signed in to change notification settings - Fork 7
/
MpsParticle.cpp
8993 lines (8050 loc) · 349 KB
/
MpsParticle.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
// Copyright (c) 2021 Rubens AMARO
// Distributed under the MIT License.
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <experimental/filesystem>
// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
// input file
#include "json.hpp"
// mkdir for Linux
#include <sys/stat.h>
// mkdir for Windows
#if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) || defined(__BORLANDC__)
#include <direct.h>
#endif
#include <sys/time.h>
#include "MpsParticle.h"
#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <Eigen/IterativeLinearSolvers>
#include <Eigen/LU>
#include <Eigen/Sparse>
#include <Eigen/Dense>
using namespace std;
// Constructor declaration
MpsParticle::MpsParticle()
{
}
// Destructor declaration
MpsParticle::~MpsParticle()
{
}
// Return time
double MpsParticle::getTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec) + (double)(tv.tv_usec) * 1.0e-6);
}
void MpsParticle::displayInfo(const int intervalIter) {
if(numOfIterations%intervalIter == 0) {
timerEnd = getTime();
int seconds, hours, minutes;
seconds = int(timerEnd - timerStart);
//seconds = int(timer_end - timer_sta);
minutes = seconds / 60;
hours = minutes / 60;
printf("Iteration: %5dth Time: %lfsec Num. Particles: %d Max Velocity: %lfm/s Courant: %lf",
numOfIterations, timeCurrent, numParticles, velMax, CFLcurrent);
if(fluidType == viscType::NON_NEWTONIAN) {
printf(" CFLvisc: %lf", CFLvisc);
}
if(mpsType == calcPressType::IMPLICIT_PND || mpsType == calcPressType::IMPLICIT_PND_DIVU){
printf(" Solver iterations: %3d Estimated error: %.2e", solverIter, solverError);
}
printf(" RunTime: %02dh%02dm%02dsec\n", int(hours), int(minutes%60), int(seconds%60));
}
}
// Initialize elements of the class
void MpsParticle::init() {
// Read and allocate memory for data
readInputFile();
// Write header of output txt files (force and pressure)
// part->writeHeaderTxtFiles(); (NOT WORKING !!!)
// Allocation of buckets
allocateBuckets();
// Setting parameters
setParameters();
// Set Periodic Boundary Condition of the bucket
setBucketBC();
// Update particle ID's in buckets
updateBuckets();
// Verify if particle is out of domain
checkParticleOutDomain();
}
// Update variables at 0th step
void MpsParticle::stepZero() {
// Initial PND
setInitialPndNumberOfNeigh();
if(wallType == boundaryWallType::POLYGON) {
// Contribution to mean PND due polygon wall
meanWallPnd();
}
// Mean of PND
meanPnd();
// Mean fluid neighbor PND
meanNeighFluidPnd();
// Update type of particle
if(freeSurfType == calcBCType::PND_ARC) {
// Compute fluid particles normal vector
calcNormalParticles();
if(wallType == boundaryWallType::POLYGON) {
// Contribution to normal vector due polygon wall
calcWallNormalParticles();
}
}
updateParticleBC();
// Compute pressure
if(mpsType == calcPressType::EXPLICIT) {
calcPressEMPS();
}
else if(mpsType == calcPressType::WEAKLY) {
calcPressWCMPS();
}
else if(mpsType == calcPressType::IMPLICIT_PND)
{
solvePressurePoissonPnd();
}
else if(mpsType == calcPressType::IMPLICIT_PND_DIVU)
{
calcVelDivergence();
if(wallType == boundaryWallType::POLYGON) {
if(slipCondition == slipBC::FREE_SLIP) {
calcWallSlipVelDivergence(); // Free-Slip condition
}
else if(slipCondition == slipBC::NO_SLIP) {
calcWallNoSlipVelDivergence(); // No-Slip condition
}
}
solvePressurePoissonPndDivU();
}
// Write header for vtu files
writePvd();
// Delete all files inside the simulation folder
deleteDirectoryFiles();
// Write VTK file of buckets
writeBuckets();
}
// Return the square distance between thwo particles "i" and "j"
void MpsParticle::sqrDistBetweenParticles(const int j,
const double rxi, const double ryi, const double rzi,
double &rx, double &ry, double &rz, double &rij2) {
rx = pos[j*3 ] - rxi;
ry = pos[j*3+1] - ryi;
rz = pos[j*3+2] - rzi;
rij2 = rx*rx+ry*ry+rz*rz;
}
// Return the square distance between thwo particles "i" and "j" considering Periodic BC
void MpsParticle::sqrDistBetweenParticles(const int j,
const double rxi, const double ryi, const double rzi,
double &rx, double &ry, double &rz, double &rij2,
const double plx, const double ply, const double plz) {
rx = (pos[j*3 ] + plx) - rxi;
ry = (pos[j*3+1] + ply) - ryi;
rz = (pos[j*3+2] + plz) - rzi;
rij2 = rx*rx+ry*ry+rz*rz;
}
// Get Periodic lenghts
void MpsParticle::getPeriodicLengths(const int jb, double &perlx, double &perly,
double &perlz) {
int bPBC = bucketPeriodicBC[jb];
perlx = bPBC*periodicLength[0];
perly = bPBC*periodicLength[1];
perlz = bPBC*periodicLength[2];
}
// Return the bucket coordinates for particle "i"
void MpsParticle::bucketCoordinates(int &bx, int &by, int &bz,
const double rxi, const double ryi, const double rzi) {
bx = (int)((rxi - domainMinX)*invBucketSide + 1.0e-8);
by = (int)((ryi - domainMinY)*invBucketSide + 1.0e-8);
bz = (int)((rzi - domainMinZ)*invBucketSide + 1.0e-8);
}
// Weight function
double MpsParticle::weight(const double dst, const double re, const int wijType) {
switch (wijType) {
case 0:
return re/dst - 1.0;
case 1:
return re/dst + dst/re - 2.0;
case 2:
return re/dst - dst/re;
case 3:
return (1.0-dst/re)*(1.0-dst/re)*(1.0-dst/re);
case 4:
return (1.0-dst/re)*(1.0-dst/re);
default:
return re/dst - 1.0;
}
}
// Weight function for gradient
double MpsParticle::weightGradient(const double dst, const double re, const int wijType) {
switch (wijType) {
case 0:
return re/dst - 1.0;
case 1:
return re/dst + dst/re - 2.0;
case 2:
return re/dst - dst/re;
case 3:
return (1.0-dst/re)*(1.0-dst/re)*(1.0-dst/re);
case 4:
return (1.0-dst/re)*(1.0-dst/re);
default:
return re/dst - 1.0;
}
}
// Derivate of weight function
double MpsParticle::delWeight(const double dst, const double re, const int wijType) {
switch (wijType) {
case 0:
return -re/(dst*dst);
case 1:
return -re/(dst*dst) + 1.0/re;
case 2:
return -re/(dst*dst) - 1.0/re;
case 3:
return -3.0/re*(1.0-dst/re)*(1.0-dst/re);
case 4:
return -2.0/re*(1.0-dst/re);
default:
return -re/(dst*dst);
}
}
////////////////////////////////////////////////////////////
// Functions called only at the initial instant (t=0)
////////////////////////////////////////////////////////////
// Read input data from file .json to class MpsParticle
void MpsParticle::readInputFile() {
// Runtime start
timerStart = getTime();
char json_folder[] = "input/";
char json_file_char [1000];
char json_path_char [1000];
bool readOK = false;
printf(" ____________________________________________________________ \n");
printf("| |\n");
printf("| E-MPS/WC-MPS/MPS |\n");
printf("| Explicit/Weakly-compressible/Semi-implicit |\n");
printf("| Moving Particle Simulation |\n");
printf("| |\n");
printf("| University of Sao Paulo - Brazil |\n");
printf("| |\n");
printf("| by Rubens Augusto Amaro Junior |\n");
printf("|____________________________________________________________|\n\n");
while (readOK == false)
{
printf("Enter the name of the MPS input file:\n");
scanf("%s", json_file_char);
printf("\n");
// char *json_file_char = new char[json_file.length()+1];
// strcpy (json_file_char, json_file.c_str());
// json_file_char now contains a c-string copy of json_file
strcat(json_file_char, ".json");
snprintf(json_path_char, 1000, "%s%s", json_folder, json_file_char);
//tries to read the input json file
js = fopen(json_path_char, "r");
if (js == NULL) {
printf("Error reading the input file %s. Try again.\n", json_path_char);
}
else {
readOK = true;
}
}
//printf("Input file: %s\n", json_file_char);
printf("Reading JSON File... ");
using json = nlohmann::json;
// read a JSON file
ifstream ifs(json_path_char);
//json je;
//ifs >> je;
//json je = json::parse(ifs);
// Set parameter ignore_comments to true in the parse function to ignore // or /* */ comments.
// Comments will then be treated as whitespace.
// If skip_comments is set to true, the comments are skipped during parsing
json je = json::parse(ifs,
/* callback */ nullptr,
/* allow exceptions */ true,
/* skip_comments */ true);
// Access the values
// Types of simulations and output
wallType = je.at("flags").value("wall_type", 1);
femOn = je.at("flags").value("fem_MESH", false);
forcedOn = je.at("flags").value("forced_MESH", false);
vtuType = je.at("flags").at("output_VTU").value("type", 0);
freeSurfWall = je.at("flags").at("output_VTU").value("only_freeSurface", false);
outputPnd = je.at("flags").at("output_VTU").value("pnd", false);
outputNeigh = je.at("flags").at("output_VTU").value("neigh", false);
outputDeviation = je.at("flags").at("output_VTU").value("deviation", false);
outputConcentration = je.at("flags").at("output_VTU").value("concentration", false);
outputAuxiliar = je.at("flags").at("output_VTU").value("auxiliar", false);
outputNonNewtonian = je.at("flags").at("output_VTU").value("non_newtonian", false);
txtPress = je.at("flags").at("output_TXT").value("press", false);
txtForce = je.at("flags").at("output_TXT").value("force", false);
// Paths of input and output files/folders
gridFilename = je.at("pathNames").value("particle_grid_file", "oops");
meshRigidFilename = je.at("pathNames").value("mesh_rigid_file", "oops");
meshDeformableFilename = je.at("pathNames").value("mesh_deformable_file", "oops");
meshForcedFilename = je.at("pathNames").value("mesh_forced_file", "oops");
vtuOutputFoldername = je.at("pathNames").value("vtu_output_folder", "oops");
forceTxtFilename = je.at("pathNames").value("forceTxt_file", "oops");
pressTxtFilename = je.at("pathNames").value("pressTxt_file", "oops");
// Geometry dimension limits
domainMinX = je.at("domain").at("min").value("x", 0.0);
domainMinY = je.at("domain").at("min").value("y", 0.0);
domainMinZ = je.at("domain").at("min").value("z", 0.0);
domainMaxX = je.at("domain").at("max").value("x", 0.0);
domainMaxY = je.at("domain").at("max").value("y", 0.0);
domainMaxZ = je.at("domain").at("max").value("z", 0.0);
// Domain Boundary Condition
domainTypeBC = je.at("domain").at("boundary").value("type", 0);
numBC = 1;
limitTypeBC = je.at("domain").at("boundary").value("limit", 0);
periodicDirectionX = je.at("domain").at("boundary").at("direction").value("x", false);
periodicDirectionY = je.at("domain").at("boundary").at("direction").value("y", false);
periodicDirectionZ = je.at("domain").at("boundary").at("direction").value("z", false);
// Physical parameters
densityFluid = je.at("physical").value("fluid_density", 1000.0);
densityWall = je.at("physical").value("wall_density", 1000.0);
KNM_VS1 = je.at("physical").value("kinematic_visc", 0.000001);
gravityX = je.at("physical").at("gravity").value("x", 0.0);
gravityY = je.at("physical").at("gravity").value("y", 0.0);
gravityZ = je.at("physical").at("gravity").value("z", -9.81);
// Rheological parameters
KNM_VS2 = je.at("physical").at("rheological").value("kinematic_visc_phase_2", 0.000001);
DNS_FL1 = je.at("physical").at("rheological").value("fluid_density_phase_1", 1000.0);
DNS_FL2 = je.at("physical").at("rheological").value("fluid_density_phase_2", 1540.0);
DNS_SDT = je.at("physical").at("rheological").value("sediment_density", 1540.0);
fluidType = je.at("physical").at("rheological").value("fluid_type", 0);
N = je.at("physical").at("rheological").value("power_law_index", 1.2);
MEU0 = je.at("physical").at("rheological").value("consistency_index", 0.03);
PHI_1 = je.at("physical").at("rheological").at("phi").value("lower", 0.541);
PHI_WAL = je.at("physical").at("rheological").at("phi").value("wall", 0.541);
PHI_BED = je.at("physical").at("rheological").at("phi").value("bed", 0.541);
PHI_2 = je.at("physical").at("rheological").at("phi").value("second", 0.6);
cohes = je.at("physical").at("rheological").value("cohes_coeff", 0.0);
Fraction_method = je.at("physical").at("rheological").value("fraction_method", 2);
//visc_max = je.at("physical").at("rheological").value("visc_max", 20);
DG = je.at("physical").at("rheological").value("grain_size", 0.0035);
I0 = je.at("physical").at("rheological").value("I0", 0.75);
mm = je.at("physical").at("rheological").value("mm", 100.0);
stress_calc_method = je.at("physical").at("rheological").value("stress_calc_method", 1);
visc_itr_num = je.at("physical").at("rheological").at("viscosity").value("iter_num", 1);
visc_error = je.at("physical").at("rheological").at("viscosity").value("error", 0.0);
visc_ave = je.at("physical").at("rheological").at("viscosity").value("average", 0.0);
Cd = je.at("physical").at("rheological").value("drag_coeff", 0.47);
VF_min = je.at("physical").at("rheological").at("volume_fraction").value("min", 0.25);
VF_max = je.at("physical").at("rheological").at("volume_fraction").value("max", 0.65);
// Numerical parameters
dim = je.at("numerical").value("dimension", 3.0);
partDist = je.at("numerical").value("particle_dist", 0.01);
timeStep = je.at("numerical").value("time_step", 0.0005);
timeSimulation = je.at("numerical").value("final_time", 1.0);
iterOutput = je.at("numerical").value("iter_output", 80);
cflNumber = je.at("numerical").value("CFL_number", 0.2);
weightType = je.at("numerical").value("weight_type", 0);
slipCondition = je.at("numerical").value("slip_condition", 0);
reS = je.at("numerical").at("effective_radius").value("small", 2.1);
reL = je.at("numerical").at("effective_radius").value("large", 2.1);
gradientType = je.at("numerical").at("gradient").value("type", 3);
gradientCorrection = je.at("numerical").at("gradient").value("correction", false);
relaxPress = je.at("numerical").at("gradient").value("relax_fact", 1.0);
mpsType = je.at("numerical").value("mps_type", 1);
soundSpeed = je.at("numerical").at("explicit_mps").at("equation_state").value("speed_sound", 15.0);
gamma = je.at("numerical").at("explicit_mps").at("equation_state").value("gamma", 7.0);
solverType = je.at("numerical").at("semi_implicit_mps").value("solver_type", 0);
alphaCompressibility = je.at("numerical").at("semi_implicit_mps").at("weak_compressibility").value("alpha", 0.000001);
relaxPND = je.at("numerical").at("semi_implicit_mps").at("source_term").value("relax_pnd", 0.001);
shiftingType = je.at("numerical").at("particle_shifting").value("type", 2);
dri = je.at("numerical").at("particle_shifting").value("DRI", 0.01);
coefA = je.at("numerical").at("particle_shifting").value("coef_A", 2.0);
machNumber = je.at("numerical").at("particle_shifting").value("mach_number", 0.1);
VEL_A = je.at("numerical").at("particle_shifting").value("adj_vel_A", 0.1);
pndType = je.at("numerical").at("pnd").value("type", 0);
diffusiveCoef = je.at("numerical").at("pnd").value("diffusive_coeff", 0.35);
repulsiveForceType = je.at("numerical").at("wall_repulsive_force").value("type", 2);
reRepulsiveForce = je.at("numerical").at("wall_repulsive_force").value("re", 0.5);
expectMaxVelocity = je.at("numerical").at("wall_repulsive_force").value("maxVel", 6.0);
repForceCoefMitsume = je.at("numerical").at("wall_repulsive_force").at("coefficient").value("Mitsume", 40000000.0);
repForceCoefLennardJones = je.at("numerical").at("wall_repulsive_force").at("coefficient").value("Lennard-Jones", 2.0);
repForceCoefMonaghanKajtar = je.at("numerical").at("wall_repulsive_force").at("coefficient").value("Monaghan-Kajtar", 1.0);
EPS_RE = je.at("numerical").at("wall_repulsive_force").value("eps_re", 0.01);
freeSurfType = je.at("numerical").at("free_surface_threshold").value("type", 0);
pndThreshold = je.at("numerical").at("free_surface_threshold").value("pnd", 0.98);
neighThreshold = je.at("numerical").at("free_surface_threshold").value("neigh", 0.85);
npcdThreshold = je.at("numerical").at("free_surface_threshold").value("NPCD", 0.20);
thetaThreshold = je.at("numerical").at("free_surface_threshold").value("ARC", 45.0);
normThreshold = je.at("numerical").at("free_surface_threshold").value("normal", 0.1);
collisionType = je.at("numerical").at("particle_collision").value("type", 0);
collisionRatio = je.at("numerical").at("particle_collision").value("ratio", 0.20);
distLimitRatio = je.at("numerical").at("particle_collision").value("dist_limit_ratio", 0.85);
lambdaCollision = je.at("numerical").at("particle_collision").value("lambda", 0.20);
ghost = je.at("numerical").at("particle_type").value("ghost", -1);
fluid = je.at("numerical").at("particle_type").value("fluid", 0);
wall = je.at("numerical").at("particle_type").value("wall", 2);
dummyWall = je.at("numerical").at("particle_type").value("dummyWall", 3);
surface = je.at("numerical").at("boundary_type").value("free_surface", 1);
inner = je.at("numerical").at("boundary_type").value("inner", 0);
other = je.at("numerical").at("boundary_type").value("other", -1);
numPartTypes = 3;
printf("OK\n");
printf("Reading GRID File... ");
readMpsParticleFile(gridFilename);
printf("OK\n");
// Extend domain
domainMinX = domainMinX - partDist*3.0;
domainMinY = domainMinY - partDist*3.0;
domainMinZ = domainMinZ - partDist*3.0;
domainMaxX = domainMaxX + partDist*3.0;
domainMaxY = domainMaxY + partDist*3.0;
domainMaxZ = domainMaxZ + partDist*3.0;
if((int)dim == 2) {
domainMinZ = 0.0;
domainMaxZ = 0.0;
}
// Number of meshs
numOfRigidMesh = 0; numOfDeformableMesh = 0; numOfForcedMesh = 0;
if(wallType == 1) numOfRigidMesh = 1;
if(femOn == true) numOfDeformableMesh = 1;
if(forcedOn == true) numOfForcedMesh = 1;
numOfMeshs = numOfRigidMesh + numOfDeformableMesh + numOfForcedMesh;
// // Print the values
// cout << "INPUT FILE .JSON" << endl;
// cout << "Number of Meshs: " << numOfMeshs << " | ";
// cout << "WallType:" << wallType << " | ";
// cout << "GiraffeOn: " << femOn << " | ";
// cout << "forcedOn: " << forcedOn << " | ";
// cout << "vtuType: " << vtuType << " | ";
// cout << "freeSurfWall: " << freeSurfWall << " | ";
// cout << "outputPnd: " << outputPnd << " | ";
// cout << "outputNeigh: " << outputNeigh << " | ";
// cout << "outputDeviation: " << outputDeviation << " | ";
// cout << "outputConcentration: " << outputConcentration << " | ";
// cout << "outputAuxiliar: " << outputAuxiliar << " | ";
// cout << "outputNonNewtonian: " << outputNonNewtonian << " | ";
// cout << "txtPress: " << txtPress << " | ";
// cout << "txtForce: " << txtForce << endl;
// cout << "gridFilename: " << gridFilename << endl;
// cout << "meshRigidFilename: " << meshRigidFilename << endl;
// cout << "meshDeformableFilename: " << meshDeformableFilename << endl;
// cout << "meshForcedFilename: " << meshForcedFilename << endl;
// cout << "vtuOutputFoldername: " << vtuOutputFoldername << endl;
// cout << "forceTxtFilename: " << forceTxtFilename << endl;
// cout << "pressTxtFilename: " << pressTxtFilename << endl;
// cout << "domainMin: " << domainMinX << ": " << domainMinY << ": " << domainMinZ << endl;
// cout << "domainMax: " << domainMaxX << ": " << domainMaxY << ": " << domainMaxZ << endl;
// cout << "gravity: " << gravityX << ": " << gravityY << ": " << gravityZ << endl;
// cout << "densityFluid: " << densityFluid << " | ";
// cout << "densityWall: " << densityWall << " | ";
// cout << "KNM_VS1-2: " << KNM_VS1 << ": " << KNM_VS2 << " | ";
// cout << "DNS_FL1-2-DST: " << DNS_FL1 << ": " << DNS_FL2 << ": " << DNS_SDT << endl;
// cout << "fluidType: " << fluidType << " | ";
// cout << "N: " << N << " | ";
// cout << "MEU0: " << MEU0 << " | ";
// cout << "PHI-FL-WAL-BED-2: " << PHI_1 << ": " << PHI_WAL << ": " << PHI_BED << ": " << PHI_2 << endl;
// cout << "cohes: " << cohes << " | ";
// cout << "Fraction_method: " << Fraction_method << " | ";
// cout << "DG: " << DG << " | ";
// cout << "I0: " << I0 << " | ";
// cout << "mm: " << mm << " | ";
// cout << "stress_calc_method: " << stress_calc_method << " | ";
// cout << "visc_itr_num-error-ave: " << visc_itr_num << ": " << visc_error << ": " << visc_ave << endl;
// cout << "Cd: " << Cd << " | ";
// cout << "VFminmax: " << VF_min << ": " << VF_max << endl;
// cout << "dim: " << dim << " | ";
// cout << "lo: " << partDist << " | ";
// cout << "dt: " << timeStep << " | ";
// cout << "tf: " << timeSimulation << " | ";
// cout << "itO: " << iterOutput << " | ";
// cout << "CFL: " << cflNumber << endl;
// cout << "mpsType: " << mpsType << " | ";
// cout << "weightType: " << weightType << " | ";
// cout << "slip: " << slipCondition << " | ";
// cout << "reSL: " << reS << ": " << reL << endl;
// cout << "gradientType: " << gradientType << " | ";
// cout << "gradientCorrection: " << gradientCorrection << " | ";
// cout << "relaxPress: " << relaxPress << " | ";
// cout << "soundSpeed: " << soundSpeed << " | ";
// cout << "solverType: " << solverType << endl;
// cout << "alphaCompressibility: " << alphaCompressibility << " | ";
// cout << "relaxPND: " << relaxPND << endl;
// cout << "shiftingType: " << shiftingType << " | ";
// cout << "dri: " << dri << " | ";
// cout << "coefA: " << coefA << " | ";
// cout << "machNumber: " << machNumber << " | ";
// cout << "VEL_A: " << VEL_A << " | ";
// cout << "pndType: " << pndType << endl;
// cout << "diffusiveCoef: " << diffusiveCoef << " | ";
// cout << "repulsiveForceType: " << repulsiveForceType << " | ";
// cout << "reRepulsiveForce: " << reRepulsiveForce << " | ";
// cout << "expectMaxVelocity: " << expectMaxVelocity << " | ";
// cout << "repForceCoefMitsume" << repForceCoefMitsume << " | ";
// cout << "repForceCoefLennardJones: " << repForceCoefLennardJones << " | ";
// cout << "repForceCoefMonaghanKajtar: " << repForceCoefMonaghanKajtar << endl;
// cout << "EPS_RE: " << EPS_RE << " | ";
// cout << "freeSurfType: " << freeSurfType << " | ";
// cout << "pndThreshold: " << pndThreshold << " | ";
// cout << "neighThreshold: " << neighThreshold << " | ";
// cout << "npcdThreshold: " << npcdThreshold << " | ";
// cout << "thetaThreshold: " << thetaThreshold << " | ";
// cout << "normThreshold: " << normThreshold << " | ";
// cout << "collisionType: " << collisionType << " | ";
// cout << "collisionRatio: " << collisionRatio << " | ";
// cout << "distLimitRatio: " << distLimitRatio << " | ";
// cout << "lambdaCollision: " << lambdaCollision << endl;
// cout << "ghost: " << ghost << " | ";
// cout << "fluid: " << fluid << " | ";
// cout << "wall: " << wall << " | ";
// cout << "surface: " << surface << " | ";
// cout << "inner: " << inner << " | ";
// cout << "other: " << other << " | ";
// cout << "numParticles: " << numPartTypes << endl;
// // cout << endl;
// Close .json file
fclose(js);
}
// Read data from file .grid to class MpsParticle
void MpsParticle::readMpsParticleFile(const std::string& grid_file) {
char *grid_file_char = new char[grid_file.length()+1];
strcpy (grid_file_char, grid_file.c_str());
// grid_file_char now contains a c-string copy of grid_file
fp = fopen(grid_file_char, "r");
if(fp == NULL) perror ("Error opening grid file");
int zeroZero;
fscanf(fp,"%d",&zeroZero);
fscanf(fp,"%d",&numParticles); // Read number of particles
numParticlesZero = numParticles;
// printf("Number of particles: %d\n",numParticles);
// Memory allocation
// Scalars
particleType = (int*)malloc(sizeof(int)*numParticles); // Particle type
particleBC = (int*)malloc(sizeof(int)*numParticles); // BC particle type
numNeigh = (int*)malloc(sizeof(int)*numParticles); // Number of neighbors
press = (double*)malloc(sizeof(double)*numParticles); // Particle pressure
pressAverage = (double*)malloc(sizeof(double)*numParticles); // Time averaged particle pressure
pndi = (double*)malloc(sizeof(double)*numParticles); // PND
pndki = (double*)malloc(sizeof(double)*numParticles); // PND step k
pndski = (double*)malloc(sizeof(double)*numParticles); // Mean fluid neighbor PND step k
pndSmall = (double*)malloc(sizeof(double)*numParticles); // PND small = sum(wij)
npcdDeviation2 = (double*)malloc(sizeof(double)*numParticles); // NPCD deviation modulus
concentration = (double*)malloc(sizeof(double)*numParticles); // Concentration
velDivergence = (double*)malloc(sizeof(double)*numParticles); // Divergence of velocity
diffusiveTerm = (double*)malloc(sizeof(double)*numParticles); // Diffusive term
Dns = (double*)malloc(sizeof(double)*numPartTypes); // Density
invDns = (double*)malloc(sizeof(double)*numPartTypes); // Inverse of Density
// Vectors
acc = (double*)malloc(sizeof(double)*numParticles*3); // Particle acceleration
accStar = (double*)malloc(sizeof(double)*numParticles*3); // Particle acceleration due gravity and viscosity
pos = (double*)malloc(sizeof(double)*numParticles*3); // Particle position
vel = (double*)malloc(sizeof(double)*numParticles*3); // Particle velocity
npcdDeviation = (double*)malloc(sizeof(double)*numParticles*3); // NPCD deviation
gradConcentration = (double*)malloc(sizeof(double)*numParticles*3); // Gradient of concentration
correcMatrixRow1 = (double*)malloc(sizeof(double)*numParticles*3); // Correction matrix - Row 1
correcMatrixRow2 = (double*)malloc(sizeof(double)*numParticles*3); // Correction matrix - Row 2
correcMatrixRow3 = (double*)malloc(sizeof(double)*numParticles*3); // Correction matrix - Row 3
normal = (double*)malloc(sizeof(double)*numParticles*3); // Particle normal
dvelCollision = (double*)malloc(sizeof(double)*numParticles*3); // Variation of velocity due collision
// Polygons
// Scalars
nearMeshType = (int*)malloc(sizeof(int)*numParticles); // Type of mesh near particle
particleNearWall = (bool*)malloc(sizeof(bool)*numParticles); // Particle near polygon wall
numNeighWallContribution = (int*)malloc(sizeof(int)*numParticles); // Number of neighbors due wall
pndWallContribution = (double*)malloc(sizeof(double)*numParticles); // PND wall
deviationDotPolygonNormal = (double*)malloc(sizeof(double)*numParticles); // Deviation vector X polygonal wall
numNeighborsSurfaceParticles = (double*)malloc(sizeof(double)*numParticles);// Number of free-surface particle neighbors
distParticleWall2 = (double*)malloc(sizeof(double)*numParticles); // Squared distance of particle to triangle mesh
// Vectors
particleAtWallPos = (double*)malloc(sizeof(double)*numParticles*3); // Particle at wall coordinate
mirrorParticlePos = (double*)malloc(sizeof(double)*numParticles*3); // Mirrored particle coordinate
wallParticleForce1 = (double*)malloc(sizeof(double)*numParticles*3); // Wall-Particle force
wallParticleForce2 = (double*)malloc(sizeof(double)*numParticles*3); // Wall-Particle force
polygonNormal = (double*)malloc(sizeof(double)*numParticles*3); // Polygon normal
// Posk = (double*)malloc(sizeof(double)*numParticles*3); // Particle coordinates
// Velk = (double*)malloc(sizeof(double)*numParticles*3); // Particle velocity
// Acv = (double*)malloc(sizeof(double)*numParticles*3); // Part
// Non-Newtonian
// Scalars
PTYPE = (int*)malloc(sizeof(int)*numParticles); // Type of fluid
Cv = (double*)malloc(sizeof(double)*numParticles); // Concentration
II = (double*)malloc(sizeof(double)*numParticles); // Invariant
MEU = (double*)malloc(sizeof(double)*numParticles); // Dynamic viscosity
MEU_Y = (double*)malloc(sizeof(double)*numParticles); // Dynamic viscosity ??
Inertia = (double*)malloc(sizeof(double)*numParticles); //
pnew = (double*)malloc(sizeof(double)*numParticles); // New pressure
p_rheo_new = (double*)malloc(sizeof(double)*numParticles); //
RHO = (double*)malloc(sizeof(double)*numParticles); // Fluid density
p_smooth = (double*)malloc(sizeof(double)*numParticles); //
VF = (double*)malloc(sizeof(double)*numParticles); //
S12 = (double*)malloc(sizeof(double)*numParticles); //
S13 = (double*)malloc(sizeof(double)*numParticles); //
S23 = (double*)malloc(sizeof(double)*numParticles); //
S11 = (double*)malloc(sizeof(double)*numParticles); //
S22 = (double*)malloc(sizeof(double)*numParticles); //
S33 = (double*)malloc(sizeof(double)*numParticles); //
// FSI
// Scalars
elementID = (int*)malloc(sizeof(int)*numParticles); // Element ID
// Vectors
forceWall = (double*)malloc(sizeof(double)*numParticles*3); // Force on wall
// Solver PPE
pressurePPE = Eigen::VectorXd::Zero(numParticles);
sourceTerm = Eigen::VectorXd::Zero(numParticles);
// Set values from .grid file
for(int i=0; i<numParticles; i++) {
int a[2];
double b[8];
// Uncomment here to read .prof file
//fscanf(fp," %d %d %lf %lf %lf %lf %lf %lf %lf %lf",&a[0],&a[1],&b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6],&b[7]);
// Uncomment here to read .grid file
a[0] = 0;
fscanf(fp,"%d %lf %lf %lf %lf %lf %lf %lf %lf",&a[1],&b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6],&b[7]);
particleType[i]=a[1];
pos[i*3]=b[0]; pos[i*3+1]=b[1]; pos[i*3+2]=b[2];
vel[i*3]=b[3]; vel[i*3+1]=b[4]; vel[i*3+2]=b[5];
// | | |
// i*3 = x i*3+1 = y i*3+2 = z
press[i]=b[6]; pressAverage[i]=b[7];
// printf("X: %d %lf %lf %lf %lf %lf %lf %lf %lf\n",particleType[i],pos[3*i],pos[3*i+1],pos[3*i+2],vel[3*i],vel[3*i+1],vel[3*i+2],press[i],pressAverage[i]);
// Posk[i*3]=b[0]; Posk[i*3+1]=b[1]; Posk[i*3+2]=b[2];
// Velk[i*3]=b[3]; Velk[i*3+1]=b[4]; Velk[i*3+2]=b[5];
}
// Close .grid file
fclose(fp);
// Set vectors to zero
for(int i=0;i<numParticles*3;i++) {
acc[i]=0.0;accStar[i]=0.0;npcdDeviation[i]=0.0;gradConcentration[i]=0.0;
correcMatrixRow1[i]=0.0;correcMatrixRow2[i]=0.0;correcMatrixRow3[i]=0.0;normal[i]=0.0;dvelCollision[i]=0.0;//Acv[i]=0.0;
particleAtWallPos[i]=0.0;mirrorParticlePos[i]=0.0;wallParticleForce1[i]=0.0;wallParticleForce2[i]=0.0;polygonNormal[i]=0.0;
forceWall[i]=0.0;
}
// Set scalars to zero or infinity(10e8)
for(int i=0; i<numParticles; i++) {
particleBC[i]=0;numNeigh[i]=0;numNeighWallContribution[i]=0;elementID[i]=0;
particleNearWall[i]=false;
nearMeshType[i]=meshType::FIXED;
pndi[i]=0.0;pndki[i]=0.0;pndski[i]=0.0;pndSmall[i]=0.0;npcdDeviation2[i]=0.0;concentration[i]=0.0;
velDivergence[i]=0.0;diffusiveTerm[i]=0.0;pndWallContribution[i]=0.0;deviationDotPolygonNormal[i]=0.0;
numNeighborsSurfaceParticles[i]=0.0;Cv[i]=0.0;II[i]=0.0;MEU_Y[i]=0.0;Inertia[i]=0.0;pnew[i]=0.0;
p_rheo_new[i]=0.0;p_smooth[i]=0.0;VF[i]=0.0;S12[i]=0.0;S13[i]=0.0;S23[i]=0.0;S11[i]=0.0;S22[i]=0.0;S33[i]=0.0;
distParticleWall2[i]=10e8*partDist;
}
// Assign type and density
for(int i=0; i<numParticles; i++) {
/*
// Assign type and density
if(pos[i*3+2] <= 0.3) {
PTYPE[i]=2;
RHO[i] = DNS_FL2;
// CHANGED Only at the first time step
MEU[i] = KNM_VS2 * DNS_FL2;
}
else {
PTYPE[i]=1;
RHO[i] = DNS_FL1;
// CHANGED Only at the first time step
MEU[i] = KNM_VS1 * DNS_FL1;
}
*/
if(fluidType == viscType::NEWTONIAN) {
RHO[i] = DNS_FL1;
PTYPE[i] = 1;
MEU[i] = KNM_VS1 * DNS_FL1;
}
// Multiphase simulations - Granular Fluid
if(fluidType == viscType::NON_NEWTONIAN) {
// Assign type and density
if(particleType[i] == 1) {
particleType[i] = 0;
PTYPE[i] = 2;
RHO[i] = DNS_FL2;
// CHANGED Only at the first time step
MEU[i] = KNM_VS2 * DNS_FL2;
}
else {
//particleType[i] = 0;
PTYPE[i] = 1;
RHO[i] = DNS_FL1;
// CHANGED Only at the first time step
MEU[i] = KNM_VS1 * DNS_FL1;
}
}
}
}
// Allocation of buckets
// Murotani et al., 2015. Performance improvements of differential operators code for MPS method on GPU.
void MpsParticle::allocateBuckets() {
reS = partDist*reS; // Influence radius small
reL = partDist*reL; // Influence radius large
reS2 = reS*reS; // Influence radius small to square
reL2 = reL*reL; // Influence radius large to square
EPS_RE = EPS_RE*reS2/4.0;
reRepulsiveForce = partDist*reRepulsiveForce; // Influence radius for repulsive force
// First guess of buckets values
bucketSide = reL*(1.0+cflNumber); // Length of one bucket side
invBucketSide = 1.0/bucketSide;
numBucketsX = (int)((domainMaxX - domainMinX)*invBucketSide) + 3; // Number of buckets in the x direction in the analysis domain
numBucketsY = (int)((domainMaxY - domainMinY)*invBucketSide) + 3; // Number of buckets in the y direction in the analysis domain
numBucketsZ = (int)((domainMaxZ - domainMinZ)*invBucketSide) + 3; // Number of buckets in the z direction in the analysis domain
if((int)dim == 2) { numBucketsZ = 1; }
bucketTypeBC = (int*)malloc(sizeof(int) * numBC); // Type of Domain Boundary Condition in Bucket
periodicDirection = (bool*)malloc(sizeof(bool) * numBC*3); // Periodic direction in domain (x, y, or z)
//periodicLength = (double*)malloc(sizeof(double) * numBC*3); // Periodic length in x, y and z direction
for(int b=0; b<numBC; b++){
// domainTypeBC == 0: None
// domainTypeBC == 1: Periodic
bucketTypeBC[b] = domainTypeBC;
periodicDirection[b*3 ] = periodicDirectionX;
periodicDirection[b*3+1] = periodicDirectionY;
periodicDirection[b*3+2] = periodicDirectionZ;
}
periodicLength[0] = periodicLength[1] = periodicLength[2] = 0.0; // Periodic length in x, y and z direction
// Compute domain limits and adjust the buckets values
if(wallType == boundaryWallType::PARTICLE || domainTypeBC == 1) {
calcDomainLimits();
invBucketSide = 1.0/bucketSide;
}
numBucketsXY = numBucketsX*numBucketsY;
numBucketsXYZ = numBucketsX*numBucketsY*numBucketsZ; // Number of buckets in analysis area
std::cout << std::endl << "DomainMIN: " << domainMinX << " " << domainMinY << " " << domainMinZ;
std::cout << std::endl << "DomainMAX: " << domainMaxX << " " << domainMaxY << " " << domainMaxZ;
if(domainTypeBC == 1) {
std::cout << std::endl << "PhysicMIN: " << physDomMinX << " " << physDomMinY << " " << physDomMinZ;
std::cout << std::endl << "PhysicMAX: " << physDomMaxX << " " << physDomMaxY << " " << physDomMaxZ;
}
std::cout << std::endl << "Num Buckt: " << numBucketsX << " " << numBucketsY << " " << numBucketsZ;
std::cout << std::endl << "BucktSide: " << bucketSide << " 1/BucketSide: " << invBucketSide;
std::cout << std::endl << "PeriodicL: " << periodicLength[0] << " " << periodicLength[1] << " " << periodicLength[2];
std::cout << std::endl;
firstParticleInBucket = (int*)malloc(sizeof(int) * numBucketsXYZ); // First particle number stored in the bucket
lastParticleInBucket = (int*)malloc(sizeof(int) * numBucketsXYZ); // Last particle number stored in the bucket
nextParticleInSameBucket = (int*)malloc(sizeof(int) * numParticles); // Next particle number in the same bucket
bucketPeriodicBC = (int*)malloc(sizeof(int) * numBucketsXYZ); // Periodic Boundary Condition of the bucket
}
// Set parameters
void MpsParticle::setParameters() {
pndSmallZero = pndLargeZero = pndGradientZero = lambdaZero = numNeighZero = 0.0;
int lmin = ceil(reL/partDist) + 1;
int lmax = ceil(reL/partDist) + 2;
int flag2D = 0;
int flag3D = 1;
if((int)dim == 2) {
flag2D = 1;
flag3D = 0;
}
for(int ix= -lmin; ix<lmax; ix++) {
for(int iy= -lmin; iy<lmax; iy++) {
for(int iz= -lmin*flag3D; iz<lmax*flag3D+flag2D; iz++) {
double x = partDist* (double)ix;
double y = partDist* (double)iy;
double z = partDist* (double)iz;
double dst2 = x*x+y*y+z*z;
if(dst2 <= reL2) {
if(dst2 <= 1.0e-8) continue; // equals to zero
double dst = sqrt(dst2);
pndLargeZero += weight(dst, reL, weightType); // Initial particle number density (large)
lambdaZero += dst2 * weight(dst, reL, weightType);
numNeighZero += 1; // Initial number of neighbors
if(dst2 <= reS2) {
pndSmallZero += weight(dst, reS, weightType); // Initial particle number density (small)
pndGradientZero += weightGradient(dst, reS, weightType); // Initial particle number density (gradient operator)
}
}
}}}
lambdaZero = lambdaZero/pndLargeZero; // Coefficient λ of Laplacian model
coeffViscosity = 2.0*KNM_VS1*dim/(pndLargeZero*lambdaZero); // Coefficient used to calculate viscosity term
coeffViscMultiphase = 2.0*dim/(pndLargeZero*lambdaZero); // Coefficient used to calculate viscosity term Multiphase
coeffPressEMPS = soundSpeed*soundSpeed/pndSmallZero; // Coefficient used to calculate pressure E-MPS
coeffPressGrad = -dim/pndGradientZero; // Coefficient used to calculate pressure gradient term
coeffPressWCMPS = soundSpeed*soundSpeed; // Coefficient used to calculate pressure WC-MPS
coeffShifting1 = dri*partDist/pndSmallZero; // Coefficient used to adjust velocity type 1
coeffShifting2 = coefA*partDist*partDist*cflNumber*machNumber; // Coefficient used to adjust velocity type 2
coeffPPE = 2.0*dim/(pndLargeZero*lambdaZero); // Coefficient used to PPE
coeffPPESource = relaxPND/(timeStep*timeStep*pndSmallZero); // Coefficient used to PPE source term
Dns[partType::FLUID]=densityFluid; Dns[partType::WALL]=densityWall;
invDns[partType::FLUID]=1.0/densityFluid; invDns[partType::WALL]=1.0/densityWall;
invPartDist = 1.0/partDist;
distCollisionLimit = partDist*distLimitRatio; // A distance that does not allow further access between particles
distCollisionLimit2 = distCollisionLimit*distCollisionLimit;
restitutionCollision = 1.0 + collisionRatio;
numOfIterations = 0; // Number of iterations
fileNumber = 0; // File number
timeCurrent = 0.0; // Simulation time
velMax = 0.0; // Maximum flow velocity
CFLcurrent = cflNumber; // Current Courant number
betaPnd = pndThreshold*pndSmallZero; // Surface cte PND
betaNeigh = neighThreshold*numNeighZero; // Surface cte Neighbors
delta2 = npcdThreshold*npcdThreshold*partDist*partDist; // Surface cte NPCD
thetaArc = thetaThreshold/180.0*3.14159265; // Surface cte theta ARC
hThreshold2 = 1.33*1.33*partDist*partDist; // Surface cte radius ARC
dstThreshold2 = 2.0*hThreshold2; // Surface cte radius ARC
normThreshold2 = normThreshold*normThreshold; // Surface cte Normal
//cout << "lo: " << partDist << " m, dt: " << timeStep << " s, PND0Small: " << pndSmallZero << " PND0Large: " << pndLargeZero << " PND0Grad: " << pndGradientZero << " lambda: " << lambdaZero << std::endl;
//cout << "bPnd: " << betaPnd << "betaNeigh: " << betaNeigh << endl;
}
// Set initial PND and number of neighbors
void MpsParticle::setInitialPndNumberOfNeigh() {
#pragma omp parallel for schedule(dynamic,64)
for(int i=0; i<numParticles; i++) {
double posXi = pos[i*3 ]; double posYi = pos[i*3+1]; double posZi = pos[i*3+2];
double posMirrorXi = mirrorParticlePos[i*3 ]; double posMirrorYi = mirrorParticlePos[i*3+1]; double posMirrorZi = mirrorParticlePos[i*3+2];
double wSum = 0.0;
numNeigh[i] = 0;
npcdDeviation[i*3] = npcdDeviation[i*3+1] = npcdDeviation[i*3+2] = 0.0;
int ix, iy, iz;
bucketCoordinates(ix, iy, iz, posXi, posYi, posZi);
int minZ = (iz-1)*((int)(dim-2.0)); int maxZ = (iz+1)*((int)(dim-2.0));
for(int jz=minZ;jz<=maxZ;jz++) {
for(int jy=iy-1;jy<=iy+1;jy++) {
for(int jx=ix-1;jx<=ix+1;jx++) {
int jb = jz*numBucketsXY + jy*numBucketsX + jx;
int j = firstParticleInBucket[jb];
if(j == -1) continue;
double plx, ply, plz;
getPeriodicLengths(jb, plx, ply, plz);
while(true) {
double v0ij, v1ij, v2ij, v0imj, v1imj, v2imj, dstij2, dstimj2;
// Particle square distance r_ij^2 = (Xj - Xi_temporary_position)^2
sqrDistBetweenParticles(j, posXi, posYi, posZi, v0ij, v1ij, v2ij, dstij2, plx, ply, plz);
// Mirror particle square distance r_imj^2 = (Xj - Xim_temporary_position)^2
sqrDistBetweenParticles(j, posMirrorXi, posMirrorYi, posMirrorZi, v0imj, v1imj, v2imj, dstimj2, plx, ply, plz);
// If j is inside the neighborhood of i and
// is not at the same side of im (avoid real j in the virtual neihborhood)
if(dstij2 < reL2 && (dstij2 < dstimj2 || wallType == boundaryWallType::PARTICLE)) {
if(j != i) {
numNeigh[i] += 1;
if(dstij2 < reS2) {
double dst = sqrt(dstij2);
double wS = weight(dst, reS, weightType);
pndi[i] += wS;
//dst = dst*invPartDist;
//wS = weight(dst, reS*invPartDist, weightType);
//npcdDeviation[i*3 ] += v0ij*wS*invPartDist;
//npcdDeviation[i*3+1] += v1ij*wS*invPartDist;
//npcdDeviation[i*3+2] += v2ij*wS*invPartDist;
npcdDeviation[i*3 ] += v0ij*wS;
npcdDeviation[i*3+1] += v1ij*wS;
npcdDeviation[i*3+2] += v2ij*wS;
wSum += wS;
}
}
}
j = nextParticleInSameBucket[j];
if(j == -1) break;
}
}}}
// Add PND due wall polygon
pndi[i] += pndWallContribution[i];
if(particleType[i] == wall)
pndi[i] = pndSmallZero;
pndSmall[i] = pndi[i];
pndki[i] = pndi[i];
// Add Number of neighbors due wall polygon
numNeigh[i] += numNeighWallContribution[i];
if(wSum > 1.0e-8) {
npcdDeviation[i*3 ] /= pndSmall[i];
npcdDeviation[i*3+1] /= pndSmall[i];
npcdDeviation[i*3+2] /= pndSmall[i];
//npcdDeviation[i*3 ] /= wSum;
//npcdDeviation[i*3+1] /= wSum;
//npcdDeviation[i*3+2] /= wSum;
}
npcdDeviation2[i] = npcdDeviation[i*3]*npcdDeviation[i*3] + npcdDeviation[i*3+1]*npcdDeviation[i*3+1] +
npcdDeviation[i*3+2]*npcdDeviation[i*3+2];
//deviationDotPolygonNormal[i] = npcdDeviation[i*3]*polygonNormal[i*3]+npcdDeviation[i*3+1]*polygonNormal[i*3+1]+npcdDeviation[i*3+2]*polygonNormal[i*3+2];
if(npcdDeviation[i*3]*polygonNormal[i*3]+npcdDeviation[i*3+1]*polygonNormal[i*3+1]+npcdDeviation[i*3+2]*polygonNormal[i*3+2] < 0.0)
deviationDotPolygonNormal[i] = 1;
else
deviationDotPolygonNormal[i] = -1;
}
}
// Compute domain limits
void MpsParticle::calcDomainLimits()
{
double **limDom;
limDom = new double *[3];
for(int i=0; i<3; i++) limDom[i] = new double[3];
// limitTypeBC = 0: Border particle positions
// limitTypeBC = 1: Domain limits min and max
if(limitTypeBC == 0) {
// wall_type = 0: Use border particle positions to define all domain limits
// wall_type = 1: Use border particle positions to define periodic domain limits
limDom[0][0] = limDom[0][1] = pos[0*3 ];
limDom[1][0] = limDom[1][1] = pos[0*3+1];
limDom[2][0] = limDom[2][1] = pos[0*3+2];
for(int i=0; i<numParticles; i++) {
double posXi = pos[i*3 ]; double posYi = pos[i*3+1]; double posZi = pos[i*3+2];
limDom[0][0] = min(limDom[0][0], posXi);
limDom[0][1] = max(limDom[0][1], posXi);
limDom[1][0] = min(limDom[1][0], posYi);
limDom[1][1] = max(limDom[1][1], posYi);
limDom[2][0] = min(limDom[2][0], posZi);
limDom[2][1] = max(limDom[2][1], posZi);
}
int testdim = 0;
if(limDom[0][0] != limDom[0][1])
testdim++;
if(limDom[1][0] != limDom[1][1])
testdim++;
if(limDom[2][0] != limDom[2][1])
testdim++;
if(testdim != dim) {
fprintf(stderr, "\n Dimensions in json [%d] and grid file [%d] do not match!\n\n", int(dim), testdim);
exit(10);
}
}
else {
// Adopt min and max values from json to define domain limits
limDom[0][0] = domainMinX; limDom[0][1] = domainMaxX;
limDom[1][0] = domainMinY; limDom[1][1] = domainMaxY;
limDom[2][0] = domainMinZ; limDom[2][1] = domainMaxZ;
}
// domainTypeBC == 0: None
// domainTypeBC == 1: Periodic
if(domainTypeBC == 0 && wallType == boundaryWallType::PARTICLE) { // Whithout any special domain boundary condition
// Shift half particle distance
limDom[0][0] -= 0.5*partDist; limDom[0][1] += 0.5*partDist;
limDom[1][0] -= 0.5*partDist; limDom[1][1] += 0.5*partDist;
if(dim==3) {
limDom[2][0] -= 0.5*partDist; limDom[2][1] += 0.5*partDist;
}
for(int k=0; k<dim; k++) {
limDom[k][0] -= bucketSide;
limDom[k][1] += bucketSide;
if(k == 0) {
numBucketsX = (long)((limDom[k][1]-limDom[k][0])/bucketSide+1);
limDom[k][1] = limDom[k][0] + numBucketsX*bucketSide; // Adjust the maximum limit o X
}
else if(k == 1) {
numBucketsY = (long)((limDom[k][1]-limDom[k][0])/bucketSide+1);
limDom[k][1] = limDom[k][0] + numBucketsY*bucketSide; // Adjust the maximum limit o Y