-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAPIPrivate.cpp
More file actions
2160 lines (1967 loc) · 103 KB
/
Copy pathAPIPrivate.cpp
File metadata and controls
2160 lines (1967 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021, SBEL GPU Development Team
// Copyright (c) 2021, University of Wisconsin - Madison
//
// SPDX-License-Identifier: BSD-3-Clause
#include <core/ApiVersion.h>
#include "API.h"
#include "Defines.h"
#include "HostSideHelpers.hpp"
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <cstring>
#include <limits>
#include <algorithm>
namespace deme {
void DEMSolver::assertSysInit(const std::string& method_name) {
if (!sys_initialized) {
DEME_ERROR("DEMSolver's method %s can only be called after calling Initialize()", method_name.c_str());
}
}
void DEMSolver::assertSysNotInit(const std::string& method_name) {
if (sys_initialized) {
DEME_ERROR("DEMSolver's method %s can only be called before calling Initialize()", method_name.c_str());
}
}
void DEMSolver::assignFamilyPersistentContact_impl(
unsigned int N1,
unsigned int N2,
notStupidBool_t is_or_not,
const std::function<bool(family_t, family_t, unsigned int, unsigned int)>& condition) {
if (kT->solverFlags.isHistoryless) {
DEME_ERROR(
"You cannot mark persistent contacts when using a wildcard-less/history-less contact model (since "
"persistency is a part of the history).\nYou can use a different force model, and if you have to use this "
"one, add a placeholder wildcard.");
}
// Get device-major info to host first
kT->previous_idGeometryA.toHost();
kT->previous_idGeometryB.toHost();
kT->previous_contactType.toHost();
kT->contactPersistency.toHost();
if (dT->solverFlags.canFamilyChangeOnDevice) {
dT->familyID.toHost();
}
// What we mark are actually the prev contact arrays. These arrays will be checked by kT and if a contact is marked
// as persistent but not found in CD, it will be added to the contact array.
for (size_t i = 0; i < *(kT->solverScratchSpace.numPrevContacts); i++) {
bodyID_t bodyA = kT->previous_idGeometryA[i];
bodyID_t bodyB = kT->previous_idGeometryB[i];
contact_t c_type = kT->previous_contactType[i];
bodyID_t ownerA = dT->ownerClumpBody[bodyA]; // ownerClumpBody can't change on device
// As for B, it depends on type
bodyID_t ownerB = dT->getGeoOwnerID(bodyB, c_type);
family_t famA = dT->familyID[ownerA];
family_t famB = dT->familyID[ownerB];
if (condition(famA, famB, N1, N2)) {
kT->contactPersistency[i] = is_or_not;
}
}
if (is_or_not == CONTACT_IS_PERSISTENT) {
kT->solverFlags.hasPersistentContacts = true;
dT->solverFlags.hasPersistentContacts = true;
}
kT->contactPersistency.toDevice();
}
void DEMSolver::assignFamilyPersistentContactEither(unsigned int N, notStupidBool_t is_or_not) {
assignFamilyPersistentContact_impl(N, /*no use*/ 0, is_or_not,
[](family_t famA, family_t famB, unsigned int N1, unsigned int N2) {
return ((unsigned int)famA == N1) || ((unsigned int)famB == N1);
});
}
void DEMSolver::assignFamilyPersistentContactBoth(unsigned int N, notStupidBool_t is_or_not) {
assignFamilyPersistentContact_impl(N, /*no use*/ 0, is_or_not,
[](family_t famA, family_t famB, unsigned int N1, unsigned int N2) {
return ((unsigned int)famA == N1) && ((unsigned int)famB == N1);
});
}
void DEMSolver::assignFamilyPersistentContact(unsigned int N1, unsigned int N2, notStupidBool_t is_or_not) {
assignFamilyPersistentContact_impl(N1, N2, is_or_not,
[](family_t famA, family_t famB, unsigned int N1, unsigned int N2) {
return (((unsigned int)famA == N1) && ((unsigned int)famB == N2)) ||
(((unsigned int)famA == N2) && ((unsigned int)famB == N1));
});
}
void DEMSolver::assignPersistentContact(notStupidBool_t is_or_not) {
if (kT->solverFlags.isHistoryless) {
DEME_ERROR(
"You cannot mark persistent contacts when using a wildcard-less/history-less contact model (since "
"persistency is a part of the history).\nYou can use a different force model, and if you have to use this "
"one, add a placeholder wildcard.");
}
kT->contactPersistency.toHost();
// What we mark are actually the prev contact arrays. These arrays will be checked by kT and if a contact is marked
// as persistent but not found in CD, it will be added to the contact array.
for (size_t i = 0; i < *(kT->solverScratchSpace.numPrevContacts); i++) {
kT->contactPersistency[i] = is_or_not;
}
if (is_or_not == CONTACT_IS_PERSISTENT) {
kT->solverFlags.hasPersistentContacts = true;
dT->solverFlags.hasPersistentContacts = true;
}
kT->contactPersistency.toDevice();
}
void DEMSolver::generatePolicyResources() {
// Process the loaded materials. The pre-process of external objects and clumps could add more materials, so this
// call need to go after those pre-process ones.
figureOutMaterialProxies();
// Based on user input, prepare family_mask_matrix (family contact map matrix)
figureOutFamilyMasks();
// Decide bin size (for contact detection)
decideBinSize();
// The method of deciding the thickness of contact margin
decideCDMarginStrat();
}
void DEMSolver::generateEntityResources() {
/*
// Dan and Ruochun decided not to extract unique input values.
// Instead, we trust users: we simply store all clump template info users give.
// So the unique-value-extractor block is disabled and commented.
size_t input_num_clump_types = m_template_clump_mass.size();
// Put unique clump mass values in a set.
m_template_mass_types.insert(m_template_clump_mass.begin(), m_template_clump_mass.end());
for (size_t i = 0; i < input_num_clump_types; i++) {
// Put unique sphere radii values in a set.
m_template_sp_radii_types.insert(m_template_sp_radii.at(i).begin(), m_template_sp_radii.at(i).end());
// Put unique clump sphere component locations in a set.
m_clumps_sp_location_types.insert(m_template_sp_relPos.at(i).begin(), m_template_sp_relPos.at(i).end());
}
// Now rearrange so the original input mass and sphere radii are now stored as the offsets to their respective
// uniques sets.
for (size_t i = 0; i < input_num_clump_types; i++) {
m_template_mass_type_offset.push_back(
std::distance(m_template_mass_types.begin(), m_template_mass_types.find(m_template_clump_mass.at(i))));
std::vector<distinctSphereRadiiOffset_default_t> sp_radii_type_offset(m_template_sp_radii.at(i).size(), 0);
std::vector<distinctSphereRelativePositions_default_t> sp_location_type_offset(
m_template_sp_relPos.at(i).size(), 0);
for (size_t j = 0; j < sp_radii_type_offset.size(); j++) {
sp_radii_type_offset.at(j) = std::distance(m_template_sp_radii_types.begin(),
m_template_sp_radii_types.find(m_template_sp_radii.at(i).at(j)));
sp_location_type_offset.at(j) =
std::distance(m_clumps_sp_location_types.begin(),
m_clumps_sp_location_types.find(m_template_sp_relPos.at(i).at(j)));
}
m_template_sp_radii_type_offset.push_back(sp_radii_type_offset);
m_clumps_sp_location_type_offset.push_back(sp_location_type_offset);
}
nDistinctClumpBodyTopologies = m_template_mass_types.size();
nMatTuples = m_loaded_materials.size();
nDistinctSphereRadii_computed = m_template_sp_radii_types.size();
nDistinctSphereRelativePositions_computed = m_clumps_sp_location_types.size();
*/
// Figure out the parameters related to the simulation `world'
figureOutNV();
addWorldBoundingBox();
// Flatten cached clump templates (from ClumpTemplate structs to float arrays), make ready for transferring to kTdT
preprocessClumpTemplates();
// Flatten some input clump information, to figure out the size of the input, and their associated family numbers
preprocessClumps();
// Figure out info about external objects/clump templates and whether they can be jitified
preprocessAnalyticalObjs();
// Count how many triangle tempaltes are there and flatten them
preprocessTriangleObjs();
}
void DEMSolver::postResourceGen() {
// Compute stats
updateTotalEntityNum();
// If these `computed' numbers are larger than types like materialsOffset_t can hold, then we should error out and
// let the user re-compile (or, should we somehow change the header automatically?)
postResourceGenChecksAndTabKeeping();
}
void DEMSolver::updateTotalEntityNum() {
nDistinctClumpBodyTopologies = m_template_clump_mass.size();
nDistinctMassProperties = nDistinctClumpBodyTopologies + nExtObj + nTriMeshes;
// Also, external objects may introduce more material types
nMatTuples = m_loaded_materials.size();
// Finally, with both user inputs and jit info processed, we can derive the number of owners that we have now
nOwnerBodies = nExtObj + nOwnerClumps + nTriMeshes;
}
void DEMSolver::postResourceGenChecksAndTabKeeping() {
// There is this very cumbersome check if the user wish to jitify clump templates
if (jitify_clump_templates) {
// Can we jitify all clump templates?
bool unable_jitify_all = false;
nDistinctClumpComponents = 0;
nJitifiableClumpComponents = 0;
for (unsigned int i = 0; i < nDistinctClumpBodyTopologies; i++) {
nDistinctClumpComponents += m_template_sp_radii.at(i).size();
// Keep an eye on if the accumulated DistinctClumpComponents gets too many
if ((!unable_jitify_all) && (nDistinctClumpComponents > THRESHOLD_CANT_JITIFY_ALL_COMP)) {
nJitifiableClumpTopo = i;
nJitifiableClumpComponents = nDistinctClumpComponents - m_template_sp_radii.at(i).size();
unable_jitify_all = true;
}
}
if (unable_jitify_all) {
DEME_WARNING(
"There are %u clump templates loaded, but only %u templates (totalling %u components) are jitifiable "
"due to some of the clumps are big and/or there are many types of clumps.\nIt is probably because you "
"have some objects represented by spherical decomposition (a.k.a. have big clumps).\nIn this case, "
"I suggest calling DisableJitifyClumpTemplates() before system initialization to use flattened clump "
"templates.",
nDistinctClumpBodyTopologies, nJitifiableClumpTopo, nJitifiableClumpComponents);
} else {
nJitifiableClumpTopo = nDistinctClumpBodyTopologies;
nJitifiableClumpComponents = nDistinctClumpComponents;
}
}
if (jitify_mass_moi) {
// Sanity check for final number of mass properties/inertia offsets
if (nDistinctMassProperties >= std::numeric_limits<inertiaOffset_t>::max()) {
DEME_ERROR(
"%u different mass properties (from the contribution of clump templates, analytical objects and meshed "
"objects) are loaded, but the max allowance is %u (No.%u is reserved).\nYou may avoid this by calling "
"DisableJitifyMassProperties() before system initialization to disable jitification for mass "
"properties",
nDistinctMassProperties, std::numeric_limits<inertiaOffset_t>::max() - 1,
std::numeric_limits<inertiaOffset_t>::max());
}
}
// Sanity check for analytical geometries
if (nAnalGM > DEME_THRESHOLD_TOO_MANY_ANAL_GEO) {
DEME_WARNING(
"%u analytical geometries are loaded. Because all analytical geometries are jitified, this is a relatively "
"large amount.\nIf just-in-time compilation fails or kernels run slowly, this could be a cause.",
nAnalGM);
}
// Keep tab of some quatities... It has to be done this late, because initialization may add analytical objects to
// the system.
nLastTimeClumpTemplateLoad = nClumpTemplateLoad;
nLastTimeExtObjLoad = nExtObjLoad;
nLastTimeBatchClumpsLoad = nBatchClumpsLoad;
nLastTimeTriObjLoad = nTriObjLoad;
nLastTimeMatNum = m_loaded_materials.size();
nLastTimeClumpTemplateNum = m_templates.size();
nLastTimeFamilyPreNum = m_input_family_prescription.size();
}
void DEMSolver::addAnalCompTemplate(const objType_t type,
const std::shared_ptr<DEMMaterial>& material,
const unsigned int owner,
const float3 pos,
const float3 rot,
const float d1,
const float d2,
const float d3,
const objNormal_t normal) {
m_anal_types.push_back(type);
m_anal_materials.push_back(material->load_order);
m_anal_owner.push_back(owner);
m_anal_comp_pos.push_back(pos);
m_anal_comp_rot.push_back(rot);
m_anal_size_1.push_back(d1);
m_anal_size_2.push_back(d2);
m_anal_size_3.push_back(d3);
float normal_sign = (normal == ENTITY_NORMAL_INWARD) ? 1 : -1;
m_anal_normals.push_back(normal_sign);
}
void DEMSolver::jitifyKernels() {
equipClumpTemplates(m_subs);
equipSimParams(m_subs);
equipMassMoiVolume(m_subs);
equipMaterials(m_subs);
equipAnalGeoTemplates(m_subs);
equipFamilyPrescribedMotions(m_subs);
equipFamilyOnFlyChanges(m_subs);
equipForceModel(m_subs);
equipIntegrationScheme(m_subs);
equipKernelIncludes(m_subs);
// Jitify may require a defined device to derive the arch
std::thread kT_build([&]() {
DEME_GPU_CALL(cudaSetDevice(kT->streamInfo.device));
kT->jitifyKernels(m_subs, m_jitify_options);
});
std::thread dT_build([&]() {
DEME_GPU_CALL(cudaSetDevice(dT->streamInfo.device));
dT->jitifyKernels(m_subs, m_jitify_options);
// Now, inspectors need to be jitified too... but the current design jitify inspector kernels at the first time
// they are used. for (auto& insp : m_inspectors) {
// insp->Initialize(m_subs);
// }
// Solver system's own max vel inspector should be init-ed. Don't bother init-ing it while using, because it is
// called at high frequency, let's save an if check. Forced initialization (since doing it before system
// completes init).
m_approx_max_vel_func->Initialize(m_subs, m_jitify_options, true);
dT->approxMaxVelFunc = m_approx_max_vel_func;
});
kT_build.join();
dT_build.join();
}
void DEMSolver::getContacts_impl(std::vector<bodyID_t>& idA,
std::vector<bodyID_t>& idB,
std::vector<contact_t>& cnt_type,
std::vector<family_t>& famA,
std::vector<family_t>& famB,
std::function<bool(contact_t)> type_func) const {
// Get device-major info to host first
if (dT->solverFlags.canFamilyChangeOnDevice) {
dT->familyID.toHostAsync(dT->streamInfo.stream);
}
dT->idGeometryA.toHostAsync(dT->streamInfo.stream);
dT->idGeometryB.toHostAsync(dT->streamInfo.stream);
dT->contactType.toHostAsync(dT->streamInfo.stream);
size_t num_contacts = dT->getNumContacts();
idA.resize(num_contacts);
idB.resize(num_contacts);
cnt_type.resize(num_contacts);
famA.resize(num_contacts);
famB.resize(num_contacts);
// Try overlapping mem transfer with allocation...
dT->syncMemoryTransfer();
size_t useful_contacts = 0;
for (size_t i = 0; i < num_contacts; i++) {
contact_t this_type = dT->contactType[i];
if (type_func(this_type)) {
idA[useful_contacts] = dT->getGeoOwnerID(dT->idGeometryA[i], this_type);
idB[useful_contacts] = dT->getGeoOwnerID(dT->idGeometryB[i], this_type);
cnt_type[useful_contacts] = this_type;
famA[useful_contacts] = dT->familyID[idA[useful_contacts]];
famB[useful_contacts] = dT->familyID[idB[useful_contacts]];
useful_contacts++;
}
}
idA.resize(useful_contacts);
idB.resize(useful_contacts);
cnt_type.resize(useful_contacts);
famA.resize(useful_contacts);
famB.resize(useful_contacts);
}
void DEMSolver::figureOutNV() {
m_boxLBF = m_target_box_min;
float3 boxSize = m_target_box_max - m_target_box_min;
// Rank the size of XYZ, ascending
float XYZ[3] = {boxSize.x, boxSize.y, boxSize.z};
SPATIAL_DIR rankXYZ[3] = {SPATIAL_DIR::X, SPATIAL_DIR::Y, SPATIAL_DIR::Z};
for (int i = 0; i < 3 - 1; i++)
for (int j = i + 1; j < 3; j++)
if (XYZ[i] > XYZ[j]) {
elemSwap(XYZ + i, XYZ + j);
elemSwap(rankXYZ + i, rankXYZ + j);
}
// Record the size ranking
float userSize321[3] = {XYZ[0], XYZ[1], XYZ[2]};
// Inspect how many times larger the larger one is. Say it is 2 times larger, then one more bit is given to the
// larger one; say 4 times larger, then 2 more bits are given to the larger one. If in between (2, 4), then if it's
// more than sqrt(2) * 2 times larger, then 2 morebits; otherwise, 1 more bit. Why? Do that math then maybe you can
// agree this wastes as little bits as possible.
int n_more_bits_for_me[2] = {0, 0};
while (XYZ[0] < XYZ[1]) {
if (sqrt(2.) * XYZ[0] > XYZ[1]) {
break;
}
n_more_bits_for_me[0]++;
XYZ[0] *= 2.;
}
while (XYZ[1] < XYZ[2]) {
if (sqrt(2.) * XYZ[1] > XYZ[2]) {
break;
}
n_more_bits_for_me[1]++;
XYZ[1] *= 2.;
}
DEME_DEBUG_PRINTF("2nd place uses %d more bits than 3rd, and 1st place uses %d more bits than 2rd.",
n_more_bits_for_me[0], n_more_bits_for_me[1]);
// Then we know how many bits each one would have
int base_bits = ((int)VOXEL_COUNT_POWER2 - 2 * n_more_bits_for_me[0] - n_more_bits_for_me[1]) / 3;
int left_over = ((int)VOXEL_COUNT_POWER2 - 2 * n_more_bits_for_me[0] - n_more_bits_for_me[1]) % 3;
int bits_3rd = base_bits;
int bits_2nd = bits_3rd + n_more_bits_for_me[0];
int bits_1st = bits_2nd + n_more_bits_for_me[1];
while (left_over > 0) {
// Try giving to losers... unless the loser did not suffer bits penalty, in which case give to the larger one
if (bits_3rd < bits_2nd) {
bits_3rd++;
} else if (bits_2nd < bits_1st) {
bits_2nd++;
} else {
bits_1st++;
}
left_over--;
}
DEME_DEBUG_PRINTF("After assigning left-overs, 3rd, 2nd and 1st have bits: %d, %d, %d.", bits_3rd, bits_2nd,
bits_1st);
int bits[3] = {bits_3rd, bits_2nd, bits_1st};
if (m_box_dir_length_is_exact == SPATIAL_DIR::NONE) {
// Have to use the largest l, given the voxel budget
double l3 =
(double)userSize321[0] / (double)std::pow(2., (int)VOXEL_RES_POWER2) / (double)std::pow(2., bits_3rd);
double l2 =
(double)userSize321[1] / (double)std::pow(2., (int)VOXEL_RES_POWER2) / (double)std::pow(2., bits_2nd);
double l1 =
(double)userSize321[2] / (double)std::pow(2., (int)VOXEL_RES_POWER2) / (double)std::pow(2., bits_1st);
l = std::max(l3, std::max(l2, l1));
} else {
// Find which dir user wants to be exact
int exact_dir_no = find_array_offset(rankXYZ, m_box_dir_length_is_exact, 3);
int not_exact_dir[2];
if (exact_dir_no == 0) {
not_exact_dir[0] = 1;
not_exact_dir[1] = 2;
} else if (exact_dir_no == 1) {
not_exact_dir[0] = 0;
not_exact_dir[1] = 2;
} else {
not_exact_dir[0] = 0;
not_exact_dir[1] = 1;
}
// We hope this l is big enough...
l = (double)userSize321[exact_dir_no] / (double)std::pow(2., (int)VOXEL_RES_POWER2) /
(double)std::pow(2., bits[exact_dir_no]);
while (l * (double)std::pow(2., (int)VOXEL_RES_POWER2) * (double)std::pow(2., bits[not_exact_dir[1]]) <
userSize321[not_exact_dir[1]]) {
// Borrow a bit from this dir...
bits[exact_dir_no] -= 1;
bits[not_exact_dir[1]] += 1;
l = (double)userSize321[exact_dir_no] / (double)std::pow(2., (int)VOXEL_RES_POWER2) /
(double)std::pow(2., bits[exact_dir_no]);
}
while (l * (double)std::pow(2., (int)VOXEL_RES_POWER2) * (double)std::pow(2., bits[not_exact_dir[0]]) <
userSize321[not_exact_dir[0]]) {
// Borrow a bit from this dir...
bits[exact_dir_no] -= 1;
bits[not_exact_dir[0]] += 1;
l = (double)userSize321[exact_dir_no] / (double)std::pow(2., (int)VOXEL_RES_POWER2) /
(double)std::pow(2., bits[exact_dir_no]);
}
}
DEME_DEBUG_PRINTF(
"After final tweak concerning possible exact direction requirements, 3rd, 2nd and 1st have bits: %d, %d, %d.",
bits[0], bits[1], bits[2]);
nvXp2 = bits[find_array_offset(rankXYZ, SPATIAL_DIR::X, 3)];
nvYp2 = bits[find_array_offset(rankXYZ, SPATIAL_DIR::Y, 3)];
nvZp2 = bits[find_array_offset(rankXYZ, SPATIAL_DIR::Z, 3)];
// Calculating `world' size by the input nvXp2 and l
m_voxelSize = (double)((size_t)1 << VOXEL_RES_POWER2) * (double)l;
m_boxX = m_voxelSize * (double)((size_t)1 << nvXp2);
m_boxY = m_voxelSize * (double)((size_t)1 << nvYp2);
m_boxZ = m_voxelSize * (double)((size_t)1 << nvZp2);
}
void DEMSolver::decideBinSize() {
// find the smallest radius
for (auto elem : m_template_sp_radii) {
for (auto radius : elem) {
if (radius < m_smallest_radius) {
m_smallest_radius = radius;
}
}
}
// use_user_defined_bin_size records whether the user explicitly gave a number for bin size
if (m_smallest_radius > DEME_TINY_FLOAT) {
if (use_user_defined_bin_size != INIT_BIN_SIZE_TYPE::EXPLICIT) {
m_binSize = m_binSize_as_multiple * m_smallest_radius;
}
} else {
if (use_user_defined_bin_size == INIT_BIN_SIZE_TYPE::MULTI_MIN_SPH) {
DEME_ERROR(
"There are spheres in clump templates that have near-zero radii (%.9g), and the user did not specify "
"the bin size (for contact detection)!\nBecause the bin size is supposed to be defaulted to the size "
"of the smallest sphere, now the solver does not know what to do.",
m_smallest_radius);
} else {
DEME_WARNING(
"There are spheres in clump templates that have near-zero radii (%.9g)! Please make sure this is "
"intentional.",
m_smallest_radius);
}
}
m_num_bins = hostCalcBinNum(nbX, nbY, nbZ, m_voxelSize, m_binSize, nvXp2, nvYp2, nvZp2);
// It's better to compute num of bins this way, rather than...
// (uint64_t)(m_boxX / m_binSize + 1) * (uint64_t)(m_boxY / m_binSize + 1) * (uint64_t)(m_boxZ / m_binSize + 1);
// because the space bins and voxels can cover may be larger than the user-defined sim domain
// Now we have a bin size. We adjust it here if the user specified a target init number.
if (use_user_defined_bin_size == INIT_BIN_SIZE_TYPE::TARGET_NUM) {
size_t prev_num = m_num_bins;
while ((double)m_num_bins < 0.67 * m_target_init_bin_num || (double)m_num_bins > 1.5 * m_target_init_bin_num) {
if (m_num_bins < m_target_init_bin_num) {
m_binSize *= 0.8;
} else {
m_binSize *= 1.2;
}
m_num_bins = hostCalcBinNum(nbX, nbY, nbZ, m_voxelSize, m_binSize, nvXp2, nvYp2, nvZp2);
// If changed size relationship, good enough.
if ((prev_num < m_target_init_bin_num && m_num_bins >= m_target_init_bin_num) ||
(prev_num >= m_target_init_bin_num && m_num_bins < m_target_init_bin_num)) {
break;
}
prev_num = m_num_bins;
}
}
// A final safety check: Do we have more bins that our data type can handle?
if (m_num_bins > std::numeric_limits<binID_t>::max() - 1) {
if (use_user_defined_bin_size != INIT_BIN_SIZE_TYPE::EXPLICIT) {
DEME_WARNING(
"%zu initial bins created with size %.6g. This is more than max allowance %zu. Auto-adjusting...",
m_num_bins, m_binSize, (size_t)(std::numeric_limits<binID_t>::max() - 1));
while (m_num_bins > std::numeric_limits<binID_t>::max() - 1) {
m_binSize *= 1.5;
m_num_bins = hostCalcBinNum(nbX, nbY, nbZ, m_voxelSize, m_binSize, nvXp2, nvYp2, nvZp2);
}
DEME_WARNING(
"Bin size auto-adjusted to %.6g, now we have %zu initial bins. Note this number may be large and it "
"potentially slows down kT.\nUsing SetInitBinNumTarget to set a reasonable target initial bin number "
"is recommended.",
m_binSize, m_num_bins);
} else {
DEME_ERROR(
"The simulation world has %zu bins (for domain partitioning in contact detection), but the largest bin "
"ID that we can have is %zu.\nYou can try to make bins larger via SetInitBinSize, or redefine binID_t "
"and recompile.",
m_num_bins, (size_t)(std::numeric_limits<binID_t>::max() - 1));
}
}
}
void DEMSolver::decideCDMarginStrat() {
switch (m_max_v_finder_type) {
case (MARGIN_FINDER_TYPE::DEM_INSPECTOR):
break;
case (MARGIN_FINDER_TYPE::DEFAULT):
// Default strategy is to use an inspector
m_approx_max_vel_func = this->CreateInspector("absv");
m_max_v_finder_type = MARGIN_FINDER_TYPE::DEM_INSPECTOR;
break;
}
}
void DEMSolver::reportInitStats() const {
DEME_INFO("\n");
DEME_INFO("Number of system devices detected: %d", GpuManager::scanNumDevices());
DEME_INFO("Number of active devices used by DEME: %d", dTkT_GpuManager->getNumDevices());
DEME_INFO("User-specified X-dimension range: [%.7g, %.7g]", m_user_box_min.x, m_user_box_max.x);
DEME_INFO("User-specified Y-dimension range: [%.7g, %.7g]", m_user_box_min.y, m_user_box_max.y);
DEME_INFO("User-specified Z-dimension range: [%.7g, %.7g]", m_user_box_min.z, m_user_box_max.z);
DEME_INFO("User-specified dimensions should NOT be larger than the following simulation world.");
DEME_INFO("The dimension of the simulation world: %.17g, %.17g, %.17g", m_boxX, m_boxY, m_boxZ);
DEME_INFO("Simulation world X range: [%.7g, %.7g]", m_boxLBF.x, m_boxLBF.x + m_boxX);
DEME_INFO("Simulation world Y range: [%.7g, %.7g]", m_boxLBF.y, m_boxLBF.y + m_boxY);
DEME_INFO("Simulation world Z range: [%.7g, %.7g]", m_boxLBF.z, m_boxLBF.z + m_boxZ);
DEME_INFO("The length unit in this simulation is: %.17g", l);
DEME_INFO("The edge length of a voxel: %.17g", m_voxelSize);
DEME_INFO("The initial time step size: %.7g", m_ts_size);
DEME_INFO("The initial edge length of a bin: %.17g", m_binSize);
DEME_INFO("The initial number of bins: %zu", m_num_bins);
DEME_INFO("The total number of clumps: %zu", nOwnerClumps);
DEME_INFO("The combined number of component spheres: %zu", nSpheresGM);
DEME_INFO("The total number of analytical objects: %u", nExtObj);
DEME_INFO("The total number of meshes: %zu", nTriMeshes);
DEME_INFO("Grand total number of owners: %zu", nOwnerBodies);
DEME_INFO("The number of material types: %u", nMatTuples);
switch (m_force_model->type) {
case (FORCE_MODEL::HERTZIAN):
DEME_INFO("History-based Hertzian contact model is in use.");
break;
case (FORCE_MODEL::HERTZIAN_FRICTIONLESS):
DEME_INFO("Frictionless Hertzian contact model is in use.");
break;
case (FORCE_MODEL::CUSTOM):
DEME_INFO("A user-custom force model is in use.");
break;
default:
DEME_INFO("An unknown force model is in use, this is probably not going well...");
}
if (use_user_defined_expand_factor) {
DEME_INFO(
"All geometries are enlarged/thickened by %.6g (estimated with the initial step size and update frequency) "
"for contact detection purpose.",
m_expand_factor);
DEME_INFO("This in the case of the smallest sphere, means enlarging radius by %.6g%%.",
(m_expand_factor / m_smallest_radius) * 100.0);
} else {
DEME_INFO("The solver to set to adaptively change the contact margin size.");
float initFutureDrift = (m_suggestedFutureDrift < 0.) ? 10.0 : m_suggestedFutureDrift;
float expand_factor = (m_expand_safety_multi * AN_EXAMPLE_MAX_VEL_FOR_SHOWING_MARGIN_SIZE + m_expand_base_vel) *
initFutureDrift * m_ts_size;
DEME_STEP_METRIC(
"To give an example, all geometries may be enlarged/thickened by around %.6g (estimated with the initial "
"step size, initial update frequency and velocity %.4g) for contact detection purpose.",
expand_factor, AN_EXAMPLE_MAX_VEL_FOR_SHOWING_MARGIN_SIZE);
DEME_STEP_METRIC("This in the case of the smallest sphere, means enlarging radius by %.6g%%.",
(expand_factor / m_smallest_radius) * 100.0);
}
DEME_INFO("\n");
// Debug outputs
DEME_DEBUG_EXEC(printf("These owners are tracked: ");
for (const auto& tracked
: m_tracked_objs) { printf("%zu, ", (size_t)tracked->ownerID); } printf("\n"););
DEME_DEBUG_EXEC(printf("Meshes' owner--offset pairs: ");
for (const auto& mesh
: m_meshes) { printf("{%zu, %u}, ", (size_t)mesh->owner, mesh->cache_offset); } printf("\n"););
}
void DEMSolver::preprocessAnalyticalObjs() {
// nExtObj can increase in mid-simulation if the user re-initialize using an `Add' flavor
nExtObj += cached_extern_objs.size();
unsigned int thisExtObj = 0;
for (const auto& ext_obj : cached_extern_objs) {
// Load mass and MOI properties into arrays waiting to be transfered to kTdT
m_ext_obj_mass.push_back(ext_obj->mass);
m_ext_obj_moi.push_back(ext_obj->MOI);
// Then load this ext obj's components
unsigned int this_num_anal_ent = 0;
auto comp_params = ext_obj->entity_params;
auto comp_mat = ext_obj->materials;
m_input_ext_obj_xyz.push_back(ext_obj->init_pos);
m_input_ext_obj_rot.push_back(ext_obj->init_oriQ);
m_input_ext_obj_family.push_back(ext_obj->family_code);
for (unsigned int i = 0; i < ext_obj->types.size(); i++) {
auto param = comp_params.at(this_num_anal_ent);
this_num_anal_ent++;
switch (ext_obj->types.at(i)) {
case OBJ_COMPONENT::PLANE:
addAnalCompTemplate(ANAL_OBJ_TYPE_PLANE, comp_mat.at(i), thisExtObj, param.plane.position,
param.plane.normal);
break;
case OBJ_COMPONENT::PLATE:
addAnalCompTemplate(ANAL_OBJ_TYPE_PLATE, comp_mat.at(i), thisExtObj, param.plate.center,
param.plate.normal, param.plate.h_dim_x, param.plate.h_dim_y);
break;
case OBJ_COMPONENT::CYL_INF:
addAnalCompTemplate(ANAL_OBJ_TYPE_CYL_INF, comp_mat.at(i), thisExtObj, param.cyl.center,
param.cyl.dir, param.cyl.radius, 0, 0, param.cyl.normal);
break;
default:
DEME_ERROR("There is at least one analytical boundary that has a type not supported.");
}
}
nAnalGM += this_num_anal_ent;
m_ext_obj_comp_num.push_back(this_num_anal_ent);
thisExtObj++;
}
}
void DEMSolver::preprocessClumpTemplates() {
// We really only have to sort clump templates if we wish to jitify clump templates
if (jitify_clump_templates) {
// A sort based on the number of components of each clump type is needed, so larger clumps are near the end of
// the array, so we can always jitify the smaller clumps, and leave larger ones in GPU global memory
std::sort(m_templates.begin(), m_templates.end(),
[](auto& left, auto& right) { return left->nComp < right->nComp; });
// A mapping is needed to transform the user-defined clump type array so that it matches the new, rearranged
// clump template array
std::unordered_map<unsigned int, unsigned int> old_mark_to_new;
for (unsigned int i = 0; i < m_templates.size(); i++) {
old_mark_to_new[m_templates.at(i)->mark] = i;
DEME_DEBUG_PRINTF("Clump template re-order: %u->%u, nComp: %u", m_templates.at(i)->mark, i,
m_templates.at(i)->nComp);
}
// If the user then add more clumps to the system (without adding templates, which mandates a
// re-initialization), mapping again is not needed, because now we redefine each template's mark to be the same
// as their current position in template array
for (unsigned int i = 0; i < m_templates.size(); i++) {
m_templates.at(i)->mark = i;
}
}
// Build the clump template number--name map
for (const auto& clump_template : m_templates) {
m_template_number_name_map[clump_template->mark] = clump_template->m_name;
}
// Now we can flatten clump templates
for (const auto& clump : m_templates) {
m_template_clump_mass.push_back(clump->mass);
m_template_clump_moi.push_back(clump->MOI);
m_template_sp_radii.push_back(clump->radii);
m_template_sp_relPos.push_back(clump->relPos);
m_template_clump_volume.push_back(clump->volume);
// m_template_sp_mat_ids is an array of ints that represent the indices of the material array
std::vector<unsigned int> this_clump_sp_mat_ids;
for (const std::shared_ptr<DEMMaterial>& this_material : clump->materials) {
this_clump_sp_mat_ids.push_back(this_material->load_order);
}
m_template_sp_mat_ids.push_back(this_clump_sp_mat_ids);
DEME_DEBUG_EXEC(printf("Input clump No.%zu has material types: ", m_template_clump_mass.size() - 1);
for (unsigned int i = 0; i < this_clump_sp_mat_ids.size();
i++) { printf("%d, ", this_clump_sp_mat_ids.at(i)); } printf("\n"););
}
}
void DEMSolver::preprocessClumps() {
nExtraContacts = 0;
for (auto& a_batch : cached_input_clump_batches) {
nOwnerClumps += a_batch->GetNumClumps();
nExtraContacts += a_batch->GetNumContacts();
nSpheresGM += a_batch->GetNumSpheres();
// Family number is flattened here, only because figureOutFamilyMasks() needs it
m_input_clump_family.insert(m_input_clump_family.end(), a_batch->families.begin(), a_batch->families.end());
}
DEME_DEBUG_PRINTF("This time, %zu existing contact pairs were loaded by user", nExtraContacts);
}
void DEMSolver::preprocessTriangleObjs() {
nTriMeshes += cached_mesh_objs.size();
unsigned int thisMeshObj = 0;
for (const auto& mesh_obj : cached_mesh_objs) {
if (!(mesh_obj->isMaterialSet)) {
DEME_ERROR(
"A meshed object is loaded but does not have associated material.\nPlease assign material to meshes "
"via SetMaterial.");
}
// Put the mesh into the host-side cache
m_meshes.push_back(mesh_obj);
// Note that cache_offset needs to be modified by dT in init. This info is important if we need to modify the
// mesh later on.
if (mesh_obj->mass < 1e-15 || length(mesh_obj->MOI) < 1e-15) {
DEME_WARNING(
"A mesh is instructed to have near-zero (or negative) mass or moment of inertia (mass: %.9g, MOI "
"magnitude: %.9g). This could destabilize the simulation.\nPlease make sure this is intentional.",
mesh_obj->mass, length(mesh_obj->MOI));
}
m_mesh_obj_mass.push_back(mesh_obj->mass);
m_mesh_obj_moi.push_back(mesh_obj->MOI);
m_input_mesh_obj_xyz.push_back(mesh_obj->init_pos);
m_input_mesh_obj_rot.push_back(mesh_obj->init_oriQ);
m_input_mesh_obj_family.push_back(mesh_obj->family_code);
m_mesh_facet_owner.insert(m_mesh_facet_owner.end(), mesh_obj->GetNumTriangles(), thisMeshObj);
for (unsigned int i = 0; i < mesh_obj->GetNumTriangles(); i++) {
m_mesh_facet_materials.push_back(mesh_obj->materials.at(i)->load_order);
DEMTriangle tri = mesh_obj->GetTriangle(i);
// If we wish to correct surface orientation based on given vertex normals, rather than using RHR...
if (mesh_obj->use_mesh_normals) {
int normal_i = mesh_obj->m_face_n_indices.at(i).x; // normals at each vertex of this triangle
float3 normal = mesh_obj->m_normals.at(normal_i);
// Generate normal using RHR from nodes 1, 2, and 3
float3 AB = tri.p2 - tri.p1;
float3 AC = tri.p3 - tri.p1;
float3 cross_product = cross(AB, AC);
// If the normal created by a RHR traversal is not correct, switch two vertices
if (dot(cross_product, normal) < 0) {
float3 tmp = tri.p2;
tri.p2 = tri.p3;
tri.p3 = tmp;
}
}
m_mesh_facets.push_back(tri);
}
nTriGM += mesh_obj->GetNumTriangles();
thisMeshObj++;
}
}
void DEMSolver::figureOutMaterialProxies() {
// It now got completely integrated to the equipMaterials part
}
void DEMSolver::figureOutFamilyMasks() {
// Figure out the unique family numbers for a sanity check
std::vector<unsigned int> unique_clump_families = hostUniqueVector<unsigned int>(m_input_clump_family);
if (any_of(unique_clump_families.begin(), unique_clump_families.end(),
[](unsigned int i) { return i == RESERVED_FAMILY_NUM; })) {
DEME_WARNING(
"Some clumps are instructed to have family number %u.\nThis family number is reserved for "
"completely fixed boundaries. Using it on your simulation entities will make them fixed, regardless of "
"your specification.\nYou can change family_t if you indeed need more families to work with.",
RESERVED_FAMILY_NUM);
}
// We always know the size of the mask matrix, and we init it as all-allow
m_family_mask_matrix.clear();
m_family_mask_matrix.resize((NUM_AVAL_FAMILIES + 1) * NUM_AVAL_FAMILIES / 2, DONT_PREVENT_CONTACT);
// Then we figure out the masks
for (const auto& a_pair : m_input_no_contact_pairs) {
// Convert user-input pairs into impl-level pairs
unsigned int implID1 = a_pair.ID1;
unsigned int implID2 = a_pair.ID2;
// Now fill in the mask matrix
unsigned int posInMat = locateMaskPair<unsigned int>(implID1, implID2);
m_family_mask_matrix.at(posInMat) = PREVENT_CONTACT;
}
// Then, figure out each family's prescription info and put it into an array
// Multiple user prescription input entries can work on the same array entry
m_unique_family_prescription.resize(NUM_AVAL_FAMILIES);
for (const auto& preInfo : m_input_family_prescription) {
unsigned int user_family = preInfo.family;
auto& this_family_info = m_unique_family_prescription.at(user_family);
this_family_info.used = true;
this_family_info.family = user_family;
if (preInfo.linPosPre != "none") {
this_family_info.linPosPre = preInfo.linPosPre;
}
if (preInfo.linPosX != "none") {
this_family_info.linPosX = preInfo.linPosX;
}
if (preInfo.linPosY != "none") {
this_family_info.linPosY = preInfo.linPosY;
}
if (preInfo.linPosZ != "none") {
this_family_info.linPosZ = preInfo.linPosZ;
}
if (preInfo.oriQ != "none") {
this_family_info.oriQ = preInfo.oriQ;
}
// If it is not none, then it is automatically dictated by prescribed motion and will not accept influence by
// other sim entities
if (preInfo.linVelPre != "none") {
this_family_info.linVelPre = preInfo.linVelPre;
}
if (preInfo.linVelX != "none") {
this_family_info.linVelX = preInfo.linVelX;
}
if (preInfo.linVelY != "none") {
this_family_info.linVelY = preInfo.linVelY;
}
if (preInfo.linVelZ != "none") {
this_family_info.linVelZ = preInfo.linVelZ;
}
if (preInfo.rotVelPre != "none") {
this_family_info.rotVelPre = preInfo.rotVelPre;
}
if (preInfo.rotVelX != "none") {
this_family_info.rotVelX = preInfo.rotVelX;
}
if (preInfo.rotVelY != "none") {
this_family_info.rotVelY = preInfo.rotVelY;
}
if (preInfo.rotVelZ != "none") {
this_family_info.rotVelZ = preInfo.rotVelZ;
}
// Possibly the user explicitly ordered this family to not accept influence from other sim entities; if it is
// the case, we enforce that here.
this_family_info.linVelXPrescribed = this_family_info.linVelXPrescribed || preInfo.linVelXPrescribed;
this_family_info.linVelYPrescribed = this_family_info.linVelYPrescribed || preInfo.linVelYPrescribed;
this_family_info.linVelZPrescribed = this_family_info.linVelZPrescribed || preInfo.linVelZPrescribed;
this_family_info.rotVelXPrescribed = this_family_info.rotVelXPrescribed || preInfo.rotVelXPrescribed;
this_family_info.rotVelYPrescribed = this_family_info.rotVelYPrescribed || preInfo.rotVelYPrescribed;
this_family_info.rotVelZPrescribed = this_family_info.rotVelZPrescribed || preInfo.rotVelZPrescribed;
this_family_info.rotPosPrescribed = this_family_info.rotPosPrescribed || preInfo.rotPosPrescribed;
this_family_info.linPosXPrescribed = this_family_info.linPosXPrescribed || preInfo.linPosXPrescribed;
this_family_info.linPosYPrescribed = this_family_info.linPosYPrescribed || preInfo.linPosYPrescribed;
this_family_info.linPosZPrescribed = this_family_info.linPosZPrescribed || preInfo.linPosZPrescribed;
// Then register the accelerations that are added on top of `normal physics'
if (preInfo.accPre != "none") {
this_family_info.accPre = preInfo.accPre;
}
if (preInfo.accX != "none") {
this_family_info.accX = preInfo.accX;
}
if (preInfo.accY != "none") {
this_family_info.accY = preInfo.accY;
}
if (preInfo.accZ != "none") {
this_family_info.accZ = preInfo.accZ;
}
if (preInfo.angAccPre != "none") {
this_family_info.angAccPre = preInfo.angAccPre;
}
if (preInfo.angAccX != "none") {
this_family_info.angAccX = preInfo.angAccX;
}
if (preInfo.angAccY != "none") {
this_family_info.angAccY = preInfo.angAccY;
}
if (preInfo.angAccZ != "none") {
this_family_info.angAccZ = preInfo.angAccZ;
}
}
for (const auto& this_family_info : m_unique_family_prescription) {
if (!this_family_info.used)
continue;
unsigned int user_family = this_family_info.family;
DEME_DEBUG_PRINTF("User family %u has prescribed position: %s, %s, %s, %s", user_family,
this_family_info.linPosPre.c_str(), this_family_info.linPosX.c_str(),
this_family_info.linPosY.c_str(), this_family_info.linPosZ.c_str());
DEME_DEBUG_PRINTF("User family %u has prescribed lin vel: %s, %s, %s, %s", user_family,
this_family_info.linVelPre.c_str(), this_family_info.linVelX.c_str(),
this_family_info.linVelY.c_str(), this_family_info.linVelZ.c_str());
DEME_DEBUG_PRINTF("User family %u has prescribed ang vel: %s, %s, %s, %s", user_family,
this_family_info.rotVelPre.c_str(), this_family_info.rotVelX.c_str(),
this_family_info.rotVelY.c_str(), this_family_info.rotVelZ.c_str());
}
}
void DEMSolver::addWorldBoundingBox() {
// Now, add the bounding box for the simulation `world' if instructed.
// Note the positions to add these planes are determined by the user-wanted box sizes, not m_boxXYZ which is the max
// possible box size.
if (m_user_add_bounding_box == "none")
return;
bool top = false, bottom = false, sides = false;
switch (hash_charr(m_user_add_bounding_box.c_str())) {
case ("only_bottom"_):
bottom = true;
break;
case ("only_sides"_):
sides = true;
break;
case ("top_open"_):
bottom = true;
sides = true;
break;
case ("all"_):
bottom = true;
sides = true;
top = true;
break;
default:
DEME_ERROR("Domain bounding BC instruction %s is unknown.", m_user_add_bounding_box.c_str());
}
auto box = this->AddExternalObject();
if (bottom) {
float3 bottom_loc = (m_user_box_min + m_user_box_max) / 2.;
bottom_loc.z = m_user_box_min.z;
box->AddPlane(bottom_loc, make_float3(0, 0, 1), m_bounding_box_material);
}
if (sides) {
float3 center = (m_user_box_min + m_user_box_max) / 2.;
float3 left = center;
left.x = m_user_box_min.x;
box->AddPlane(left, make_float3(1, 0, 0), m_bounding_box_material);
float3 right = center;
right.x = m_user_box_max.x;
box->AddPlane(right, make_float3(-1, 0, 0), m_bounding_box_material);
float3 front = center;