-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lulesh.chpl
1695 lines (1282 loc) · 51.4 KB
/
lulesh.chpl
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
/*
Derived from the DARPA/Livermore Unstructured Lagrangian Explicit
Shock Hydrodynamics (LULESH)
https://computation.llnl.gov/casc/ShockHydro/
Original port to Chapel by Brandon Holt (8/2011). Further
improvements for the sake of performance and/or generality made by
Sung-Eun Choi (12/2011, 11/2012), Jeff Keasler (3/2012), and Brad
Chamberlain (3-4,9-11/2012, 2/2013).
Notes on the Initial Implementation
-----------------------------------
This implementation was designed to mirror the overall structure of
the C++ Lulesh but use Chapel constructs where they can help make
the code more readable, easier to maintain, or more
'elegant'. Function names are preserved for the most part, with some
additional helper functions, and original comments from the C++ code
are interspersed approximately where they belong to give an idea of
how the two codes line up. One major difference for this Chapel
version is the use of a number of module-level variables and
constants.
Status:
This code remains a work-in-progress as we gain further experience
with it. Proposed improvements are noted in the README in this
directory and (in some cases) in TODO comments in the code.
*/
use Time, // to get timing routines for benchmarking
BlockDist; // for block-distributed arrays
use luleshInit; // initialization code for data set
/* The configuration parameters for lulesh. These can be set on the
compiler command line using -s<paramName>=<value>. For example,
chpl -suseBlockDist=true
useBlockDist : says whether or not to block-distribute the arrays.
The default is based on the value of CHPL_COMM, as
an indication of whether this is a single- or multi-
locale execution.
use3DRepresentation : indicates whether the element node arrays
should be stored using a 3D representation
(limiting the execution to cube inputs) or
the more general 1D representation (supporting
arbitrary data sets).
useSparseMaterials : indicates whether sparse domains/arrays should be
used to represent the materials. Sparse domains
are more realistic in that they permit an arbitrary
subset of the problem space to store a material.
Dense domains are sufficient for LULESH since there's
an assumption that the material spans all cells.
printWarnings : prints performance-oriented warnings to prevent
surprises.
*/
config param useBlockDist = (CHPL_COMM != "none"),
use3DRepresentation = false,
useSparseMaterials = true,
printWarnings = true;
//
// Sanity check to ensure that input files aren't used with the 3D
// representation
//
if (use3DRepresentation && (luleshInit.filename != "")) then
halt("The 3D representation does not support reading input from files");
/* Configuration constants: Override defaults on executable's command-line */
config const initialEnergy = 3.948746e+7; // initial energy value
config const showProgress = false, // print time and dt values on each step
debug = false, // print various debug info
doTiming = true, // time the main timestep loop
printCoords = true; // print the final computed coordinates
/* Compile-time constants */
param XI_M = 0x003,
XI_M_SYMM = 0x001,
XI_M_FREE = 0x002,
XI_P = 0x00c,
XI_P_SYMM = 0x004,
XI_P_FREE = 0x008,
ETA_M = 0x030,
ETA_M_SYMM = 0x010,
ETA_M_FREE = 0x020,
ETA_P = 0x0c0,
ETA_P_SYMM = 0x040,
ETA_P_FREE = 0x080,
ZETA_M = 0x300,
ZETA_M_SYMM = 0x100,
ZETA_M_FREE = 0x200,
ZETA_P = 0xc00,
ZETA_P_SYMM = 0x400,
ZETA_P_FREE = 0x800;
/* Set up the problem size */
const (numElems, numNodes) = initProblemSize();
/* Declare abstract problem domains */
const ElemSpace = if use3DRepresentation
then {0..#elemsPerEdge, 0..#elemsPerEdge, 0..#elemsPerEdge}
else {0..#numElems},
NodeSpace = if use3DRepresentation
then {0..#nodesPerEdge, 0..#nodesPerEdge, 0..#nodesPerEdge}
else {0..#numNodes};
/* Declare the (potentially distributed) problem domains */
const Elems = if useBlockDist then ElemSpace dmapped Block(ElemSpace)
else ElemSpace,
Nodes = if useBlockDist then NodeSpace dmapped Block(NodeSpace)
else NodeSpace;
/* The coordinates */
var x, y, z: [Nodes] real;
/* The number of nodes per element. In a rank-independent version,
this could be written 2**rank */
param nodesPerElem = 8;
// We could name this, but chose not to since it doesn't add that much clarity
//
// const elemNeighbors = 1..nodesPerElem;
/* The element-to-node mapping */
var elemToNode: [Elems] nodesPerElem*index(Nodes);
/* the Greek variables */
var lxim, lxip, letam, letap, lzetam, lzetap: [Elems] index(Elems);
/* the X, Y, Z Symmetry values */
var XSym, YSym, ZSym: sparse subdomain(Nodes);
/* Constants */
const u_cut = 1.0e-7, /* velocity tolerance */
hgcoef = 3.0, /* hourglass control */
qstop = 1.0e+12, /* excessive q indicator */
monoq_max_slope = 1.0,
monoq_limiter_mult = 2.0,
e_cut = 1.0e-7, /* energy tolerance */
p_cut = 1.0e-7, /* pressure tolerance */
ss4o3 = 4.0/3.0,
q_cut = 1.0e-7, /* q tolerance */
v_cut = 1.0e-10, /* relative volume tolerance */
qlc_monoq = 0.5, /* linear term coef for q */
qqc_monoq = 2.0/3.0, /* quadratic term coef for q */
qqc = 2.0,
qqc2 = 64.0 * qqc**2,
eosvmax = 1.0e+9,
eosvmin = 1.0e-9,
pmin = 0.0, /* pressure floor */
emin = -1.0e+15, /* energy floor */
dvovmax = 0.1, /* maximum allowable volume change */
refdens = 1.0, /* reference density */
deltatimemultlb = 1.1,
deltatimemultub = 1.2,
dtmax = 1.0e-2; /* maximum allowable time increment */
config const stoptime = 1.0e-2, /* end time for simulation */
maxcycles = max(int), /* max number of cycles to simulate */
dtfixed = -1.0e-7; /* fixed time increment */
/* The list of material elements */
const MatElems: MatElemsType = if useSparseMaterials then enumerateMatElems()
else Elems;
proc MatElemsType type {
if useSparseMaterials {
if (printWarnings && useBlockDist && numLocales > 1) then
writeln("WARNING: The LULESH Material Elements (MatElems) are not yet\n",
" distributed, so result in excessive memory use on,\n",
" and communication with, locale 0\n");
return sparse subdomain(Elems);
} else
return Elems.type;
}
iter enumerateMatElems() {
if (printWarnings && useBlockDist && numLocales > 1) then
writeln("WARNING: generation of matrix elements is serial and\n",
" unlikely to scale");
for i in Elems do
yield i;
}
/* Element fields */
var elemBC: [Elems] int,
e: [Elems] real, // energy
p: [Elems] real, // pressure
q: [Elems] real, // q
ql: [Elems] real, // linear term for q
qq: [Elems] real, // quadratic term for q
v: [Elems] real = 1.0, //relative volume
vnew: [Elems] real,
volo: [Elems] real, // reference volume
delv: [Elems] real, // m_vnew - m_v
vdov: [Elems] real, // volume derivative over volume
arealg: [Elems] real, // elem characteristic length
ss: [Elems] real, // "sound speed"
elemMass: [Elems] real; // mass
/* Nodal fields */
var xd, yd, zd: [Nodes] real, // velocities
xdd, ydd, zdd: [Nodes] real, // acceleration
fx, fy, fz: [Nodes] atomic real, // forces
nodalMass: [Nodes] real; // mass
/* Parameters */
var time = 0.0, // current time
deltatime = 1.0e-7, // variable time increment
dtcourant = 1.0e20, // courant constraint
dthydro = 1.0e20, // volume change constraint
cycle = 0; // iteration count for simulation
proc main() {
if debug then writeln("Lulesh -- Problem Size = ", numElems);
initLulesh();
var st: real;
if doTiming then st = getCurrentTime();
while (time < stoptime && cycle < maxcycles) {
const iterTime = if showProgress then getCurrentTime() else 0.0;
TimeIncrement();
LagrangeLeapFrog();
if debug {
deprintatomic("[[ Forces ]]", fx, fy, fz);
deprint("[[ Positions ]]", x, y, z);
deprint("[[ p, e, q ]]", p, e, q);
}
if showProgress then
writef("time = %er, dt=%er, %s", time, deltatime,
if doTiming then ", elapsed = " + (getCurrentTime()-iterTime) +"\n"
else "\n");
}
if (cycle == maxcycles) {
writeln("Stopped early due to reaching maxnumsteps");
}
if doTiming {
const et = getCurrentTime();
writeln("Total Time: ", et-st);
writeln("Time/Cycle: ", (et-st)/cycle);
}
writeln("Number of cycles: ", cycle);
if printCoords {
var outfile = open("coords.out", iomode.cw);
var writer = outfile.writer();
var fmtstr = if debug then "%1.9re %1.9er %1.9er\n"
else "%1.4er %1.4er %1.4er\n";
for i in Nodes do
writer.writef(fmtstr, x[i], y[i], z[i]);
writer.close();
outfile.close();
}
}
/* Initialization functions */
proc initLulesh() {
// initialize the coordinates
initCoordinates(x,y,z);
// initialize the element to node mapping
initElemToNodeMapping(elemToNode);
// initialize the greek symbols
initGreekVars(lxim, lxip, letam, letap, lzetam, lzetap);
// initialize the symmetry plane locations
initXSyms(XSym);
initYSyms(YSym);
initZSyms(ZSym);
/* embed hexehedral elements in nodal point lattice */
//calculated on the fly using: elemToNodes(i: index(Elems)): index(Nodes)
// initialize the masses
initMasses();
// initialize the boundary conditions
const octantCorner = initBoundaryConditions();
// deposit the energy for Sedov Problem
e[octantCorner] = initialEnergy;
}
proc initMasses() {
// This is a temporary array used to accumulate masses in parallel
// without losing updates by using 'atomic' variables
var massAccum: [Nodes] atomic real;
forall eli in Elems {
var x_local, y_local, z_local: 8*real;
localizeNeighborNodes(eli, x, x_local, y, y_local, z, z_local);
const volume = CalcElemVolume(x_local, y_local, z_local);
volo[eli] = volume;
elemMass[eli] = volume;
for neighbor in elemToNodes[eli] do
massAccum[neighbor].add(volume);
}
// When we're done, copy the accumulated masses into nodalMass, at
// which point the massAccum array can go away (and will at the
// procedure's return
forall i in Nodes do
nodalMass[i] = massAccum[i].read() / 8.0;
if debug {
writeln("ElemMass:");
for mass in elemMass do writeln(mass);
writeln("NodalMass:");
for mass in nodalMass do writeln(mass);
}
}
proc initBoundaryConditions() {
var surfaceNode: [Nodes] int;
forall n in XSym do
surfaceNode[n] = 1;
forall n in YSym do
surfaceNode[n] = 1;
forall n in ZSym do
surfaceNode[n] = 1;
forall e in Elems do {
var mask: int;
for i in 1..nodesPerElem do
mask += surfaceNode[elemToNode[e][i]] << (i-1);
// TODO: make an inlined function for this little idiom? (and below)
if ((mask & 0x0f) == 0x0f) then elemBC[e] |= ZETA_M_SYMM;
if ((mask & 0xf0) == 0xf0) then elemBC[e] |= ZETA_P_SYMM;
if ((mask & 0x33) == 0x33) then elemBC[e] |= ETA_M_SYMM;
if ((mask & 0xcc) == 0xcc) then elemBC[e] |= ETA_P_SYMM;
if ((mask & 0x99) == 0x99) then elemBC[e] |= XI_M_SYMM;
if ((mask & 0x66) == 0x66) then elemBC[e] |= XI_P_SYMM;
}
//
// We find the octant corner by looking for the element with
// all three SYMM flags set, which will have the largest
// integral value. Thus, we can use a maxloc to identify it.
//
const (check, loc) = maxloc reduce zip(elemBC, Elems);
if debug then writeln("Found the octant corner at: ", loc);
if (check != (XI_M_SYMM | ETA_M_SYMM | ZETA_M_SYMM)) then
halt("maxloc got a value of ", check, " at loc ", loc);
// TODO: This is an example of an array that, in a distributed
// memory code, would typically be completely local and only storing
// the local nodes owned by the locale -- noting that some nodes
// are logically owned by multiple locales and therefore would
// redundantly be stored in both locales' surfaceNode arrays -- it's
// essentially local scratchspace that does not need to be communicated
// or kept coherent across locales.
//
surfaceNode = 0;
/* the free surfaces */
var freeSurface: sparse subdomain(Nodes);
// initialize the free surface
initFreeSurface(freeSurface);
forall n in freeSurface do
surfaceNode[n] = 1;
forall e in Elems do {
var mask: int;
for i in 1..nodesPerElem do
mask += surfaceNode[elemToNode[e][i]] << (i-1);
if ((mask & 0x0f) == 0x0f) then elemBC[e] |= ZETA_M_FREE;
if ((mask & 0xf0) == 0xf0) then elemBC[e] |= ZETA_P_FREE;
if ((mask & 0x33) == 0x33) then elemBC[e] |= ETA_M_FREE;
if ((mask & 0xcc) == 0xcc) then elemBC[e] |= ETA_P_FREE;
if ((mask & 0x99) == 0x99) then elemBC[e] |= XI_M_FREE;
if ((mask & 0x66) == 0x66) then elemBC[e] |= XI_P_FREE;
}
if debug {
writeln("elemBC:");
for b in elemBC do writeln(b);
}
return loc;
}
/* Helper functions */
inline proc localizeNeighborNodes(eli: index(Elems),
x: [] real, ref x_local: 8*real,
y: [] real, ref y_local: 8*real,
z: [] real, ref z_local: 8*real) {
for i in 1..nodesPerElem {
const noi = elemToNode[eli][i];
x_local[i] = x[noi];
y_local[i] = y[noi];
z_local[i] = z[noi];
}
}
inline proc TripleProduct(x1, y1, z1, x2, y2, z2, x3, y3, z3) {
return x1*(y2*z3 - z2*y3) + x2*(z1*y3 - y1*z3) + x3*(y1*z2 - z1*y2);
}
proc CalcElemVolume(x, y, z) {
const dx61 = x[7] - x[2],
dy61 = y[7] - y[2],
dz61 = z[7] - z[2],
dx70 = x[8] - x[1],
dy70 = y[8] - y[1],
dz70 = z[8] - z[1],
dx63 = x[7] - x[4],
dy63 = y[7] - y[4],
dz63 = z[7] - z[4],
dx20 = x[3] - x[1],
dy20 = y[3] - y[1],
dz20 = z[3] - z[1],
dx50 = x[6] - x[1],
dy50 = y[6] - y[1],
dz50 = z[6] - z[1],
dx64 = x[7] - x[5],
dy64 = y[7] - y[5],
dz64 = z[7] - z[5],
dx31 = x[4] - x[2],
dy31 = y[4] - y[2],
dz31 = z[4] - z[2],
dx72 = x[8] - x[3],
dy72 = y[8] - y[3],
dz72 = z[8] - z[3],
dx43 = x[5] - x[4],
dy43 = y[5] - y[4],
dz43 = z[5] - z[4],
dx57 = x[6] - x[8],
dy57 = y[6] - y[8],
dz57 = z[6] - z[8],
dx14 = x[2] - x[5],
dy14 = y[2] - y[5],
dz14 = z[2] - z[5],
dx25 = x[3] - x[6],
dy25 = y[3] - y[6],
dz25 = z[3] - z[6];
const volume = TripleProduct(dx31 + dx72, dx63, dx20,
dy31 + dy72, dy63, dy20,
dz31 + dz72, dz63, dz20) +
TripleProduct(dx43 + dx57, dx64, dx70,
dy43 + dy57, dy64, dy70,
dz43 + dz57, dz64, dz70) +
TripleProduct(dx14 + dx25, dx61, dx50,
dy14 + dy25, dy61, dy50,
dz14 + dz25, dz61, dz50);
return volume / 12.0;
}
proc InitStressTermsForElems(p, q, sigxx, sigyy, sigzz: [?D] real) {
forall i in D {
sigxx[i] = -p[i] - q[i];
sigyy[i] = -p[i] - q[i];
sigzz[i] = -p[i] - q[i];
}
}
proc CalcElemShapeFunctionDerivatives(x: 8*real, y: 8*real, z: 8*real,
ref b_x: 8*real,
ref b_y: 8*real,
ref b_z: 8*real,
ref volume: real) {
const fjxxi = .125 * ((x[7]-x[1]) + (x[6]-x[4]) - (x[8]-x[2]) - (x[5]-x[3])),
fjxet = .125 * ((x[7]-x[1]) - (x[6]-x[4]) + (x[8]-x[2]) - (x[5]-x[3])),
fjxze = .125 * ((x[7]-x[1]) + (x[6]-x[4]) + (x[8]-x[2]) + (x[5]-x[3])),
fjyxi = .125 * ((y[7]-y[1]) + (y[6]-y[4]) - (y[8]-y[2]) - (y[5]-y[3])),
fjyet = .125 * ((y[7]-y[1]) - (y[6]-y[4]) + (y[8]-y[2]) - (y[5]-y[3])),
fjyze = .125 * ((y[7]-y[1]) + (y[6]-y[4]) + (y[8]-y[2]) + (y[5]-y[3])),
fjzxi = .125 * ((z[7]-z[1]) + (z[6]-z[4]) - (z[8]-z[2]) - (z[5]-z[3])),
fjzet = .125 * ((z[7]-z[1]) - (z[6]-z[4]) + (z[8]-z[2]) - (z[5]-z[3])),
fjzze = .125 * ((z[7]-z[1]) + (z[6]-z[4]) + (z[8]-z[2]) + (z[5]-z[3]));
/* compute cofactors */
const cjxxi = (fjyet * fjzze) - (fjzet * fjyze),
cjxet = - (fjyxi * fjzze) + (fjzxi * fjyze),
cjxze = (fjyxi * fjzet) - (fjzxi * fjyet),
cjyxi = - (fjxet * fjzze) + (fjzet * fjxze),
cjyet = (fjxxi * fjzze) - (fjzxi * fjxze),
cjyze = - (fjxxi * fjzet) + (fjzxi * fjxet),
cjzxi = (fjxet * fjyze) - (fjyet * fjxze),
cjzet = - (fjxxi * fjyze) + (fjyxi * fjxze),
cjzze = (fjxxi * fjyet) - (fjyxi * fjxet);
/* calculate partials :
this need only be done for l = 0,1,2,3 since , by symmetry ,
(6,7,4,5) = - (0,1,2,3) .
*/
b_x[1] = - cjxxi - cjxet - cjxze;
b_x[2] = cjxxi - cjxet - cjxze;
b_x[3] = cjxxi + cjxet - cjxze;
b_x[4] = - cjxxi + cjxet - cjxze;
b_x[5] = -b_x[3];
b_x[6] = -b_x[4];
b_x[7] = -b_x[1];
b_x[8] = -b_x[2];
b_y[1] = - cjyxi - cjyet - cjyze;
b_y[2] = cjyxi - cjyet - cjyze;
b_y[3] = cjyxi + cjyet - cjyze;
b_y[4] = - cjyxi + cjyet - cjyze;
b_y[5] = -b_y[3];
b_y[6] = -b_y[4];
b_y[7] = -b_y[1];
b_y[8] = -b_y[2];
b_z[1] = - cjzxi - cjzet - cjzze;
b_z[2] = cjzxi - cjzet - cjzze;
b_z[3] = cjzxi + cjzet - cjzze;
b_z[4] = - cjzxi + cjzet - cjzze;
b_z[5] = -b_z[3];
b_z[6] = -b_z[4];
b_z[7] = -b_z[1];
b_z[8] = -b_z[2];
/* calculate jacobian determinant (volume) */
volume = 8.0 * ( fjxet * cjxet + fjyet * cjyet + fjzet * cjzet);
}
proc CalcElemNodeNormals(ref pfx: 8*real, ref pfy: 8*real, ref pfz: 8*real,
x: 8*real, y: 8*real, z: 8*real) {
proc ElemFaceNormal(param n1, param n2, param n3, param n4) {
const bisectX0 = 0.5 * (x[n4] + x[n3] - x[n2] - x[n1]),
bisectY0 = 0.5 * (y[n4] + y[n3] - y[n2] - y[n1]),
bisectZ0 = 0.5 * (z[n4] + z[n3] - z[n2] - z[n1]),
bisectX1 = 0.5 * (x[n3] + x[n2] - x[n4] - x[n1]),
bisectY1 = 0.5 * (y[n3] + y[n2] - y[n4] - y[n1]),
bisectZ1 = 0.5 * (z[n3] + z[n2] - z[n4] - z[n1]),
areaX = 0.25 * (bisectY0 * bisectZ1 - bisectZ0 * bisectY1),
areaY = 0.25 * (bisectZ0 * bisectX1 - bisectX0 * bisectZ1),
areaZ = 0.25 * (bisectX0 * bisectY1 - bisectY0 * bisectX1);
var rx, ry, rz: 8*real; //results
(rx[n1], rx[n2], rx[n3], rx[n4]) = (areaX, areaX, areaX, areaX);
(ry[n1], ry[n2], ry[n3], ry[n4]) = (areaY, areaY, areaY, areaY);
(rz[n1], rz[n2], rz[n3], rz[n4]) = (areaZ, areaZ, areaZ, areaZ);
return (rx, ry, rz);
}
// calculate total normal from each face (faces are made up of
// combinations of nodes)
(pfx, pfy, pfz) = ElemFaceNormal(1,2,3,4) + ElemFaceNormal(1,5,6,2) +
ElemFaceNormal(2,6,7,3) + ElemFaceNormal(3,7,8,4) +
ElemFaceNormal(4,8,5,1) + ElemFaceNormal(5,8,7,6);
}
proc SumElemStressesToNodeForces(b_x: 8*real, b_y: 8*real, b_z: 8*real,
stress_xx:real,
stress_yy:real,
stress_zz: real,
ref fx: 8*real,
ref fy: 8*real,
ref fz: 8*real) {
for i in 1..8 {
fx[i] = -(stress_xx * b_x[i]);
fy[i] = -(stress_yy * b_y[i]);
fz[i] = -(stress_zz * b_z[i]);
}
}
proc CalcElemVolumeDerivative(x: 8*real, y: 8*real, z: 8*real) {
proc VoluDer(param n0, param n1, param n2, param n3, param n4, param n5) {
const ox = (y[n1] + y[n2]) * (z[n0] + z[n1])
- (y[n0] + y[n1]) * (z[n1] + z[n2])
+ (y[n0] + y[n4]) * (z[n3] + z[n4])
- (y[n3] + y[n4]) * (z[n0] + z[n4])
- (y[n2] + y[n5]) * (z[n3] + z[n5])
+ (y[n3] + y[n5]) * (z[n2] + z[n5]),
oy = - (x[n1] + x[n2]) * (z[n0] + z[n1])
+ (x[n0] + x[n1]) * (z[n1] + z[n2])
- (x[n0] + x[n4]) * (z[n3] + z[n4])
+ (x[n3] + x[n4]) * (z[n0] + z[n4])
+ (x[n2] + x[n5]) * (z[n3] + z[n5])
- (x[n3] + x[n5]) * (z[n2] + z[n5]),
oz = - (y[n1] + y[n2]) * (x[n0] + x[n1])
+ (y[n0] + y[n1]) * (x[n1] + x[n2])
- (y[n0] + y[n4]) * (x[n3] + x[n4])
+ (y[n3] + y[n4]) * (x[n0] + x[n4])
+ (y[n2] + y[n5]) * (x[n3] + x[n5])
- (y[n3] + y[n5]) * (x[n2] + x[n5]);
return (ox/12.0, oy/12.0, oz/12.0);
}
var dvdx, dvdy, dvdz: 8*real;
(dvdx[1], dvdy[1], dvdz[1]) = VoluDer(2,3,4,5,6,8);
(dvdx[4], dvdy[4], dvdz[4]) = VoluDer(1,2,3,8,5,7);
(dvdx[3], dvdy[3], dvdz[3]) = VoluDer(4,1,2,7,8,6);
(dvdx[2], dvdy[2], dvdz[2]) = VoluDer(3,4,1,6,7,5);
(dvdx[5], dvdy[5], dvdz[5]) = VoluDer(8,7,6,1,4,2);
(dvdx[6], dvdy[6], dvdz[6]) = VoluDer(5,8,7,2,1,3);
(dvdx[7], dvdy[7], dvdz[7]) = VoluDer(6,5,8,3,2,4);
(dvdx[8], dvdy[8], dvdz[8]) = VoluDer(7,6,5,4,3,1);
return (dvdx, dvdy, dvdz);
}
inline proc CalcElemFBHourglassForce(xd: 8*real, yd: 8*real, zd: 8*real,
hourgam: 8*(4*real),
coefficient: real,
ref hgfx: 8*real,
ref hgfy: 8*real,
ref hgfz: 8*real) {
var hx, hy, hz: 4*real;
// reduction
for i in 1..4 {
for j in 1..8 {
hx[i] += hourgam[j][i] * xd[j];
hy[i] += hourgam[j][i] * yd[j];
hz[i] += hourgam[j][i] * zd[j];
}
}
for i in 1..8 {
var shx, shy, shz: real;
for j in 1..4 {
shx += hourgam[i][j] * hx[j];
shy += hourgam[i][j] * hy[j];
shz += hourgam[i][j] * hz[j];
}
hgfx[i] = coefficient * shx;
hgfy[i] = coefficient * shy;
hgfz[i] = coefficient * shz;
}
}
proc CalcElemCharacteristicLength(x, y, z, volume) {
proc AreaFace(param p0, param p1, param p2, param p3) {
const fx = (x[p2] - x[p0]) - (x[p3] - x[p1]),
fy = (y[p2] - y[p0]) - (y[p3] - y[p1]),
fz = (z[p2] - z[p0]) - (z[p3] - z[p1]),
gx = (x[p2] - x[p0]) + (x[p3] - x[p1]),
gy = (y[p2] - y[p0]) + (y[p3] - y[p1]),
gz = (z[p2] - z[p0]) + (z[p3] - z[p1]),
area = (fx * fx + fy * fy + fz * fz) *
(gx * gx + gy * gy + gz * gz) -
(fx * gx + fy * gy + fz * gz) *
(fx * gx + fy * gy + fz * gz);
return area ;
}
const charLength = max(AreaFace(1, 2, 3, 4),
AreaFace(5, 6, 7, 8),
AreaFace(1, 2, 6, 5),
AreaFace(2, 3, 7, 6),
AreaFace(3, 4, 8, 7),
AreaFace(4, 1, 5, 8));
return 4.0 * volume / sqrt(charLength);
}
proc CalcElemVelocityGradient(xvel, yvel, zvel, pfx, pfy, pfz,
detJ, ref d: 6*real) {
const inv_detJ = 1.0 / detJ;
d[1] = inv_detJ * ( pfx[1] * (xvel[1]-xvel[7])
+ pfx[2] * (xvel[2]-xvel[8])
+ pfx[3] * (xvel[3]-xvel[5])
+ pfx[4] * (xvel[4]-xvel[6]) );
d[2] = inv_detJ * ( pfy[1] * (yvel[1]-yvel[7])
+ pfy[2] * (yvel[2]-yvel[8])
+ pfy[3] * (yvel[3]-yvel[5])
+ pfy[4] * (yvel[4]-yvel[6]) );
d[3] = inv_detJ * ( pfz[1] * (zvel[1]-zvel[7])
+ pfz[2] * (zvel[2]-zvel[8])
+ pfz[3] * (zvel[3]-zvel[5])
+ pfz[4] * (zvel[4]-zvel[6]) );
const dyddx = inv_detJ * ( pfx[1] * (yvel[1]-yvel[7])
+ pfx[2] * (yvel[2]-yvel[8])
+ pfx[3] * (yvel[3]-yvel[5])
+ pfx[4] * (yvel[4]-yvel[6]) ),
dxddy = inv_detJ * ( pfy[1] * (xvel[1]-xvel[7])
+ pfy[2] * (xvel[2]-xvel[8])
+ pfy[3] * (xvel[3]-xvel[5])
+ pfy[4] * (xvel[4]-xvel[6]) ),
dzddx = inv_detJ * ( pfx[1] * (zvel[1]-zvel[7])
+ pfx[2] * (zvel[2]-zvel[8])
+ pfx[3] * (zvel[3]-zvel[5])
+ pfx[4] * (zvel[4]-zvel[6]) ),
dxddz = inv_detJ * ( pfz[1] * (xvel[1]-xvel[7])
+ pfz[2] * (xvel[2]-xvel[8])
+ pfz[3] * (xvel[3]-xvel[5])
+ pfz[4] * (xvel[4]-xvel[6]) ),
dzddy = inv_detJ * ( pfy[1] * (zvel[1]-zvel[7])
+ pfy[2] * (zvel[2]-zvel[8])
+ pfy[3] * (zvel[3]-zvel[5])
+ pfy[4] * (zvel[4]-zvel[6]) ),
dyddz = inv_detJ * ( pfz[1] * (yvel[1]-yvel[7])
+ pfz[2] * (yvel[2]-yvel[8])
+ pfz[3] * (yvel[3]-yvel[5])
+ pfz[4] * (yvel[4]-yvel[6]) );
d[6] = 0.5 * ( dxddy + dyddx );
d[5] = 0.5 * ( dxddz + dzddx );
d[4] = 0.5 * ( dzddy + dyddz );
}
proc CalcPressureForElems(p_new: [?D] real, bvc, pbvc,
e_old, compression, vnewc,
pmin: real, p_cut: real, eosvmax: real) {
//
// TODO: Uncomment local once sparse domain is distributed
//
forall i in D /* do local */ {
const c1s = 2.0 / 3.0;
bvc[i] = c1s * (compression[i] + 1.0);
pbvc[i] = c1s;
}
forall i in D {
p_new[i] = bvc[i] * e_old[i];
if abs(p_new[i]) < p_cut then p_new[i] = 0.0;
if vnewc[i] >= eosvmax then p_new[i] = 0.0; //impossible?
if p_new[i] < pmin then p_new[i] = pmin;
}
}
proc TimeIncrement() {
var targetdt = stoptime - time;
if dtfixed <= 0.0 && cycle != 0 { //don't do this the first cycle
var olddt = deltatime,
newdt = 1.0e20;
if dtcourant < newdt then newdt = dtcourant / 2.0;
if dthydro < newdt then newdt = 2.0/3.0 * dthydro;
const ratio = newdt / olddt;
if ratio >= 1.0 {
if ratio < deltatimemultlb then newdt = olddt;
else if ratio > deltatimemultub then newdt = olddt * deltatimemultub;
}
if newdt > dtmax then newdt = dtmax;
deltatime = newdt;
}
/* TRY TO PREVENT VERY SMALL SCALING ON THE NEXT CYCLE */
if targetdt > deltatime && targetdt < (4.0/3.0 * deltatime) {
targetdt = 2.0/3.0 * deltatime;
}
if targetdt < deltatime then deltatime = targetdt;
time += deltatime;
cycle += 1;
}
inline proc LagrangeLeapFrog() {
/* calculate nodal forces, accelerations, velocities, positions, with
* applied boundary conditions and slide surface considerations */
LagrangeNodal();
/* calculate element quantities (i.e. velocity gradient & q), and update
* material states */
LagrangeElements();
CalcTimeConstraintsForElems();
}
inline proc LagrangeNodal() {
CalcForceForNodes();
CalcAccelerationForNodes();
ApplyAccelerationBoundaryConditionsForNodes();
CalcVelocityForNodes(deltatime, u_cut);
CalcPositionForNodes(deltatime);
}
inline proc LagrangeElements() {
CalcLagrangeElements();
/* Calculate Q. (Monotonic q option requires communication) */
CalcQForElems();
ApplyMaterialPropertiesForElems();
UpdateVolumesForElems();
}
inline proc CalcTimeConstraintsForElems() {
/* evaluate time constraint */
CalcCourantConstraintForElems();
/* check hydro constraint */
CalcHydroConstraintForElems();
}
inline proc computeDTF(indx) {
const myvdov = vdov[indx];
if myvdov == 0.0 then
return max(real);
const myarealg = arealg[indx];
var dtf = ss[indx]**2;
if myvdov < 0.0 then
dtf += qqc2 * myarealg**2 * myvdov**2;
dtf = sqrt(dtf);
dtf = myarealg / dtf;
return dtf;
}
proc CalcCourantConstraintForElems() {
const val = min reduce [indx in MatElems] computeDTF(indx);
if (val != max(real)) then
dtcourant = val;
}
inline proc calcDtHydroTmp(indx) {
const myvdov = vdov[indx];
if (myvdov == 0.0) then
return max(real);
else
return dvovmax / (abs(myvdov)+1.0e-20);
}
proc CalcHydroConstraintForElems() {
const val = min reduce [indx in MatElems] calcDtHydroTmp(indx);
if (val != max(real)) then
dthydro = val;
}
/* calculate nodal forces, accelerations, velocities, positions, with
* applied boundary conditions and slide surface considerations */
proc CalcForceForNodes() {
//zero out all forces
forall x in fx do x.write(0);
forall y in fy do y.write(0);
forall z in fz do z.write(0);
/* Calcforce calls partial, force, hourq */
CalcVolumeForceForElems();
/* Calculate Nodal Forces at domain boundaries */
// this was commented out in C++ code, so left out here
}
proc CalcVolumeForceForElems() {
var sigxx, sigyy, sigzz, determ: [Elems] real;
/* Sum contributions to total stress tensor */
InitStressTermsForElems(p, q, sigxx, sigyy, sigzz);
/* call elemlib stress integration loop to produce nodal forces from
material stresses. */
IntegrateStressForElems(sigxx, sigyy, sigzz, determ);
/* check for negative element volume */
forall e in Elems {
if determ[e] <= 0.0 then
halt("can't have negative volume (determ[", e, "]=", determ[e], ")");
}