-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmpm_base.tcc
1226 lines (1062 loc) · 42.9 KB
/
mpm_base.tcc
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
//! Constructor
template <unsigned Tdim>
mpm::MPMBase<Tdim>::MPMBase(const std::shared_ptr<IO>& io) : mpm::MPM(io) {
//! Logger
console_ = spdlog::get("MPMBase");
// Create a mesh with global id 0
const mpm::Index id = 0;
// Set analysis step to start at 0
step_ = 0;
// Set mesh as isoparametric
bool isoparametric = is_isoparametric();
mesh_ = std::make_shared<mpm::Mesh<Tdim>>(id, isoparametric);
// Create constraints
constraints_ = std::make_shared<mpm::Constraints<Tdim>>(mesh_);
// Empty all materials
materials_.clear();
// Variable list
tsl::robin_map<std::string, VariableType> variables = {
// Scalar variables
{"mass", VariableType::Scalar},
{"volume", VariableType::Scalar},
{"mass_density", VariableType::Scalar},
// Vector variables
{"displacements", VariableType::Vector},
{"velocities", VariableType::Vector},
// Tensor variables
{"strains", VariableType::Tensor},
{"stresses", VariableType::Tensor}};
try {
analysis_ = io_->analysis();
// Time-step size
dt_ = analysis_["dt"].template get<double>();
// Number of time steps
nsteps_ = analysis_["nsteps"].template get<mpm::Index>();
// nload balance
if (analysis_.find("nload_balance_steps") != analysis_.end())
nload_balance_steps_ =
analysis_["nload_balance_steps"].template get<mpm::Index>();
// Locate particles
if (analysis_.find("locate_particles") != analysis_.end())
locate_particles_ = analysis_["locate_particles"].template get<bool>();
// Stress update method (USF/USL/MUSL)
try {
if (analysis_.find("mpm_scheme") != analysis_.end())
stress_update_ = analysis_["mpm_scheme"].template get<std::string>();
} catch (std::exception& exception) {
console_->warn(
"{} #{}: {}. Stress update method is not specified, using USF as "
"default",
__FILE__, __LINE__, exception.what());
}
// Velocity update
try {
velocity_update_ = analysis_["velocity_update"].template get<bool>();
} catch (std::exception& exception) {
console_->warn(
"{} #{}: Velocity update parameter is not specified, using default "
"as false",
__FILE__, __LINE__, exception.what());
velocity_update_ = false;
}
// Damping
try {
if (analysis_.find("damping") != analysis_.end()) {
if (!initialise_damping(analysis_.at("damping")))
throw std::runtime_error("Damping parameters are not defined");
}
} catch (std::exception& exception) {
console_->warn("{} #{}: Damping is not specified, using none as default",
__FILE__, __LINE__, exception.what());
}
// Math functions
try {
// Get materials properties
auto math_functions = io_->json_object("math_functions");
if (!math_functions.empty())
this->initialise_math_functions(math_functions);
} catch (std::exception& exception) {
console_->warn("{} #{}: No math functions are defined", __FILE__,
__LINE__, exception.what());
}
post_process_ = io_->post_processing();
// Output steps
output_steps_ = post_process_["output_steps"].template get<mpm::Index>();
} catch (std::domain_error& domain_error) {
console_->error("{} {} Get analysis object: {}", __FILE__, __LINE__,
domain_error.what());
abort();
}
// VTK particle variables
// Initialise container with empty vector
vtk_vars_.insert(
std::make_pair(mpm::VariableType::Scalar, std::vector<std::string>()));
vtk_vars_.insert(
std::make_pair(mpm::VariableType::Vector, std::vector<std::string>()));
vtk_vars_.insert(
std::make_pair(mpm::VariableType::Tensor, std::vector<std::string>()));
if ((post_process_.find("vtk") != post_process_.end()) &&
post_process_.at("vtk").is_array() &&
post_process_.at("vtk").size() > 0) {
// Iterate over vtk
for (unsigned i = 0; i < post_process_.at("vtk").size(); ++i) {
std::string attribute =
post_process_["vtk"][i].template get<std::string>();
if (variables.find(attribute) != variables.end())
vtk_vars_[variables.at(attribute)].emplace_back(attribute);
else {
console_->warn(
"{} #{}: VTK variable '{}' was specified, but is not available "
"in variable list",
__FILE__, __LINE__, attribute);
}
}
} else {
console_->warn(
"{} #{}: No VTK variables were specified, none will be generated",
__FILE__, __LINE__);
}
// VTK state variables
bool vtk_statevar = false;
if ((post_process_.find("vtk_statevars") != post_process_.end()) &&
post_process_.at("vtk_statevars").is_array() &&
post_process_.at("vtk_statevars").size() > 0) {
// Iterate over state_vars
for (const auto& svars : post_process_["vtk_statevars"]) {
// Phase id
unsigned phase_id = 0;
if (svars.contains("phase_id"))
phase_id = svars.at("phase_id").template get<unsigned>();
// State variables
if (svars.at("statevars").is_array() &&
svars.at("statevars").size() > 0) {
// Insert vtk_statevars_
const std::vector<std::string> state_var = svars["statevars"];
vtk_statevars_.insert(std::make_pair(phase_id, state_var));
vtk_statevar = true;
} else {
vtk_statevar = false;
break;
}
}
}
if (!vtk_statevar)
console_->warn(
"{} #{}: No VTK statevariable were specified, none will be generated",
__FILE__, __LINE__);
}
// Initialise mesh
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::initialise_mesh() {
// Initialise MPI rank and size
int mpi_rank = 0;
int mpi_size = 1;
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
// Get number of MPI ranks
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#endif
// Get mesh properties
auto mesh_props = io_->json_object("mesh");
// Get Mesh reader from JSON object
const std::string io_type = mesh_props["io_type"].template get<std::string>();
bool check_duplicates = true;
try {
check_duplicates = mesh_props["check_duplicates"].template get<bool>();
} catch (std::exception& exception) {
console_->warn(
"{} #{}: Check duplicates, not specified setting default as true",
__FILE__, __LINE__, exception.what());
check_duplicates = true;
}
// Create a mesh reader
auto mesh_io = Factory<mpm::IOMesh<Tdim>>::instance()->create(io_type);
auto nodes_begin = std::chrono::steady_clock::now();
// Global Index
mpm::Index gid = 0;
// Node type
const auto node_type = mesh_props["node_type"].template get<std::string>();
// Mesh file
std::string mesh_file =
io_->file_name(mesh_props["mesh"].template get<std::string>());
// Create nodes from file
bool node_status =
mesh_->create_nodes(gid, // global id
node_type, // node type
mesh_io->read_mesh_nodes(mesh_file), // coordinates
check_duplicates); // check dups
if (!node_status)
throw std::runtime_error(
"mpm::base::init_mesh(): Addition of nodes to mesh failed");
auto nodes_end = std::chrono::steady_clock::now();
console_->info("Rank {} Read nodes: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
nodes_end - nodes_begin)
.count());
// Read and assign node sets
this->node_entity_sets(mesh_props, check_duplicates);
// Read nodal euler angles and assign rotation matrices
this->node_euler_angles(mesh_props, mesh_io);
// Read and assign velocity constraints
this->nodal_velocity_constraints(mesh_props, mesh_io);
// Read and assign friction constraints
this->nodal_frictional_constraints(mesh_props, mesh_io);
// Initialise cell
auto cells_begin = std::chrono::steady_clock::now();
// Shape function name
const auto cell_type = mesh_props["cell_type"].template get<std::string>();
// Shape function
std::shared_ptr<mpm::Element<Tdim>> element =
Factory<mpm::Element<Tdim>>::instance()->create(cell_type);
// Create cells from file
bool cell_status =
mesh_->create_cells(gid, // global id
element, // element tyep
mesh_io->read_mesh_cells(mesh_file), // Node ids
check_duplicates); // Check dups
if (!cell_status)
throw std::runtime_error(
"mpm::base::init_mesh(): Addition of cells to mesh failed");
// Compute cell neighbours
mesh_->find_cell_neighbours();
// Read and assign cell sets
this->cell_entity_sets(mesh_props, check_duplicates);
auto cells_end = std::chrono::steady_clock::now();
console_->info("Rank {} Read cells: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
cells_end - cells_begin)
.count());
}
// Initialise particles
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::initialise_particles() {
// Initialise MPI rank and size
int mpi_rank = 0;
int mpi_size = 1;
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
// Get number of MPI ranks
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#endif
// Get mesh properties
auto mesh_props = io_->json_object("mesh");
// Get Mesh reader from JSON object
const std::string io_type = mesh_props["io_type"].template get<std::string>();
// Check duplicates default set to true
bool check_duplicates = true;
if (mesh_props.find("check_duplicates") != mesh_props.end())
check_duplicates = mesh_props["check_duplicates"].template get<bool>();
auto particles_gen_begin = std::chrono::steady_clock::now();
// Get particles properties
auto json_particles = io_->json_object("particles");
for (const auto& json_particle : json_particles) {
// Generate particles
bool gen_status =
mesh_->generate_particles(io_, json_particle["generator"]);
if (!gen_status)
std::runtime_error(
"mpm::base::init_particles() Generate particles failed");
}
auto particles_gen_end = std::chrono::steady_clock::now();
console_->info("Rank {} Generate particles: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
particles_gen_end - particles_gen_begin)
.count());
auto particles_locate_begin = std::chrono::steady_clock::now();
// Create a mesh reader
auto particle_io = Factory<mpm::IOMesh<Tdim>>::instance()->create(io_type);
// Read and assign particles cells
this->particles_cells(mesh_props, particle_io);
// Locate particles in cell
auto unlocatable_particles = mesh_->locate_particles_mesh();
if (!unlocatable_particles.empty())
throw std::runtime_error(
"mpm::base::init_particles() Particle outside the mesh domain");
// Write particles and cells to file
particle_io->write_particles_cells(
io_->output_file("particles-cells", ".txt", uuid_, 0, 0).string(),
mesh_->particles_cells());
auto particles_locate_end = std::chrono::steady_clock::now();
console_->info("Rank {} Locate particles: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
particles_locate_end - particles_locate_begin)
.count());
auto particles_volume_begin = std::chrono::steady_clock::now();
// Compute volume
mesh_->iterate_over_particles(std::bind(
&mpm::ParticleBase<Tdim>::compute_volume, std::placeholders::_1));
// Read and assign particles volumes
this->particles_volumes(mesh_props, particle_io);
// Read and assign particles stresses
this->particles_stresses(mesh_props, particle_io);
auto particles_volume_end = std::chrono::steady_clock::now();
console_->info("Rank {} Read volume, velocity and stresses: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
particles_volume_end - particles_volume_begin)
.count());
// Particle entity sets
this->particle_entity_sets(check_duplicates);
// Read and assign particles velocity constraints
this->particle_velocity_constraints();
console_->info("Rank {} Create particle sets: {} ms", mpi_rank,
std::chrono::duration_cast<std::chrono::milliseconds>(
particles_volume_end - particles_volume_begin)
.count());
// Material id update using particle sets
try {
auto material_sets = io_->json_object("material_sets");
if (!material_sets.empty()) {
for (const auto& material_set : material_sets) {
unsigned material_id =
material_set["material_id"].template get<unsigned>();
unsigned phase_id = mpm::ParticlePhase::Solid;
if (material_set.contains("phase_id"))
phase_id = material_set["phase_id"].template get<unsigned>();
unsigned pset_id = material_set["pset_id"].template get<unsigned>();
// Update material_id for particles in each pset
mesh_->iterate_over_particle_set(
pset_id, std::bind(&mpm::ParticleBase<Tdim>::assign_material,
std::placeholders::_1,
materials_.at(material_id), phase_id));
}
}
} catch (std::exception& exception) {
console_->warn("{} #{}: Material sets are not specified", __FILE__,
__LINE__, exception.what());
}
}
// Initialise materials
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::initialise_materials() {
// Get materials properties
auto materials = io_->json_object("materials");
for (const auto material_props : materials) {
// Get material type
const std::string material_type =
material_props["type"].template get<std::string>();
// Get material id
auto material_id = material_props["id"].template get<unsigned>();
// Create a new material from JSON object
auto mat =
Factory<mpm::Material<Tdim>, unsigned, const Json&>::instance()->create(
material_type, std::move(material_id), material_props);
// Add material to list
auto result = materials_.insert(std::make_pair(mat->id(), mat));
// If insert material failed
if (!result.second)
throw std::runtime_error(
"mpm::base::init_materials(): New material cannot be added, "
"insertion failed");
}
// Copy materials to mesh
mesh_->initialise_material_models(this->materials_);
}
//! Checkpoint resume
template <unsigned Tdim>
bool mpm::MPMBase<Tdim>::checkpoint_resume() {
bool checkpoint = true;
try {
// TODO: Set phase
const unsigned phase = 0;
int mpi_rank = 0;
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
if (!analysis_["resume"]["resume"].template get<bool>())
throw std::runtime_error("Resume analysis option is disabled!");
// Get unique analysis id
this->uuid_ = analysis_["resume"]["uuid"].template get<std::string>();
// Get step
this->step_ = analysis_["resume"]["step"].template get<mpm::Index>();
// Input particle h5 file for resume
std::string attribute = "particles";
std::string extension = ".h5";
auto particles_file =
io_->output_file(attribute, extension, uuid_, step_, this->nsteps_)
.string();
// Load particle information from file
mesh_->read_particles_hdf5(phase, particles_file);
// Clear all particle ids
mesh_->iterate_over_cells(
std::bind(&mpm::Cell<Tdim>::clear_particle_ids, std::placeholders::_1));
// Locate particles
auto unlocatable_particles = mesh_->locate_particles_mesh();
if (!unlocatable_particles.empty())
throw std::runtime_error("Particle outside the mesh domain");
// Increament step
++this->step_;
console_->info("Checkpoint resume at step {} of {}", this->step_,
this->nsteps_);
} catch (std::exception& exception) {
console_->info("{} {} Resume failed, restarting analysis: {}", __FILE__,
__LINE__, exception.what());
this->step_ = 0;
checkpoint = false;
}
return checkpoint;
}
//! Write HDF5 files
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::write_hdf5(mpm::Index step, mpm::Index max_steps) {
// Write input geometry to vtk file
std::string attribute = "particles";
std::string extension = ".h5";
auto particles_file =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
const unsigned phase = 0;
mesh_->write_particles_hdf5(phase, particles_file);
}
#ifdef USE_VTK
//! Write VTK files
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::write_vtk(mpm::Index step, mpm::Index max_steps) {
// VTK PolyData writer
auto vtk_writer = std::make_unique<VtkWriter>(mesh_->particle_coordinates());
// Write mesh on step 0
// Get active node pairs use true
if (step % nload_balance_steps_ == 0)
vtk_writer->write_mesh(
io_->output_file("mesh", ".vtp", uuid_, step, max_steps).string(),
mesh_->nodal_coordinates(), mesh_->node_pairs(true));
// Write input geometry to vtk file
const std::string extension = ".vtp";
const std::string attribute = "geometry";
auto meshfile =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
vtk_writer->write_geometry(meshfile);
// MPI parallel vtk file
int mpi_rank = 0;
int mpi_size = 1;
bool write_mpi_rank = false;
#ifdef USE_MPI
// Get MPI rank
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
// Get number of MPI ranks
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#endif
//! VTK scalar variables
for (const auto& attribute : vtk_vars_.at(mpm::VariableType::Scalar)) {
// Write scalar
auto file =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
vtk_writer->write_scalar_point_data(
file, mesh_->particles_scalar_data(attribute), attribute);
// Write a parallel MPI VTK container file
#ifdef USE_MPI
if (mpi_rank == 0 && mpi_size > 1) {
auto parallel_file = io_->output_file(attribute, ".pvtp", uuid_, step,
max_steps, write_mpi_rank)
.string();
vtk_writer->write_parallel_vtk(parallel_file, attribute, mpi_size, step,
max_steps);
}
#endif
}
//! VTK vector variables
for (const auto& attribute : vtk_vars_.at(mpm::VariableType::Vector)) {
// Write vector
auto file =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
vtk_writer->write_vector_point_data(
file, mesh_->particles_vector_data(attribute), attribute);
// Write a parallel MPI VTK container file
#ifdef USE_MPI
if (mpi_rank == 0 && mpi_size > 1) {
auto parallel_file = io_->output_file(attribute, ".pvtp", uuid_, step,
max_steps, write_mpi_rank)
.string();
vtk_writer->write_parallel_vtk(parallel_file, attribute, mpi_size, step,
max_steps);
}
#endif
}
//! VTK tensor variables
for (const auto& attribute : vtk_vars_.at(mpm::VariableType::Tensor)) {
// Write vector
auto file =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
vtk_writer->write_tensor_point_data(
file, mesh_->template particles_tensor_data<6>(attribute), attribute);
// Write a parallel MPI VTK container file
#ifdef USE_MPI
if (mpi_rank == 0 && mpi_size > 1) {
auto parallel_file = io_->output_file(attribute, ".pvtp", uuid_, step,
max_steps, write_mpi_rank)
.string();
vtk_writer->write_parallel_vtk(parallel_file, attribute, mpi_size, step,
max_steps, 9);
}
#endif
}
// VTK state variables
for (auto const& vtk_statevar : vtk_statevars_) {
unsigned phase_id = vtk_statevar.first;
for (const auto& attribute : vtk_statevar.second) {
std::string phase_attribute =
"phase" + std::to_string(phase_id) + attribute;
// Write state variables
auto file =
io_->output_file(phase_attribute, extension, uuid_, step, max_steps)
.string();
vtk_writer->write_scalar_point_data(
file, mesh_->particles_statevars_data(attribute, phase_id),
phase_attribute);
// Write a parallel MPI VTK container file
#ifdef USE_MPI
if (mpi_rank == 0 && mpi_size > 1) {
auto parallel_file = io_->output_file(phase_attribute, ".pvtp", uuid_,
step, max_steps, write_mpi_rank)
.string();
unsigned ncomponents = 1;
vtk_writer->write_parallel_vtk(parallel_file, phase_attribute, mpi_size,
step, max_steps, ncomponents);
}
#endif
}
}
}
#endif
#ifdef USE_PARTIO
//! Write Partio files
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::write_partio(mpm::Index step, mpm::Index max_steps) {
// MPI parallel partio file
int mpi_rank = 0;
int mpi_size = 1;
#ifdef USE_MPI
// Get MPI rank
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
// Get number of MPI ranks
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#endif
// Get Partio file extensions
const std::string extension = ".bgeo";
const std::string attribute = "partio";
// Create filename
auto file =
io_->output_file(attribute, extension, uuid_, step, max_steps).string();
// Write partio file
mpm::partio::write_particles(file, mesh_->particles_hdf5());
}
#endif // USE_PARTIO
//! Return if a mesh is isoparametric
template <unsigned Tdim>
bool mpm::MPMBase<Tdim>::is_isoparametric() {
bool isoparametric = true;
try {
const auto mesh_props = io_->json_object("mesh");
isoparametric = mesh_props.at("isoparametric").template get<bool>();
} catch (std::exception& exception) {
console_->warn(
"{} {} Isoparametric status of mesh: {}\n Setting mesh as "
"isoparametric.",
__FILE__, __LINE__, exception.what());
isoparametric = true;
}
return isoparametric;
}
//! Initialise loads
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::initialise_loads() {
auto loads = io_->json_object("external_loading_conditions");
// Initialise gravity loading
if (loads.at("gravity").is_array() &&
loads.at("gravity").size() == gravity_.size()) {
for (unsigned i = 0; i < gravity_.size(); ++i) {
gravity_[i] = loads.at("gravity").at(i);
}
} else {
throw std::runtime_error("Specified gravity dimension is invalid");
}
// Create a file reader
const std::string io_type =
io_->json_object("mesh")["io_type"].template get<std::string>();
auto reader = Factory<mpm::IOMesh<Tdim>>::instance()->create(io_type);
// Read and assign particles surface tractions
if (loads.find("particle_surface_traction") != loads.end()) {
for (const auto& ptraction : loads["particle_surface_traction"]) {
// Get the math function
std::shared_ptr<FunctionBase> tfunction = nullptr;
// If a math function is defined set to function or use scalar
if (ptraction.find("math_function_id") != ptraction.end())
tfunction = math_functions_.at(
ptraction.at("math_function_id").template get<unsigned>());
// Set id
int pset_id = ptraction.at("pset_id").template get<int>();
// Direction
unsigned dir = ptraction.at("dir").template get<unsigned>();
// Traction
double traction = ptraction.at("traction").template get<double>();
// Create particle surface tractions
bool particles_tractions =
mesh_->create_particles_tractions(tfunction, pset_id, dir, traction);
if (!particles_tractions)
throw std::runtime_error(
"Particles tractions are not properly assigned");
}
} else
console_->warn("No particle surface traction is defined for the analysis");
// Read and assign nodal concentrated forces
if (loads.find("concentrated_nodal_forces") != loads.end()) {
for (const auto& nforce : loads["concentrated_nodal_forces"]) {
// Forces are specified in a file
if (nforce.find("file") != nforce.end()) {
std::string force_file = nforce.at("file").template get<std::string>();
bool nodal_forces = mesh_->assign_nodal_concentrated_forces(
reader->read_forces(io_->file_name(force_file)));
if (!nodal_forces)
throw std::runtime_error(
"Nodal force file is invalid, forces are not properly "
"assigned");
set_node_concentrated_force_ = true;
} else {
// Get the math function
std::shared_ptr<FunctionBase> ffunction = nullptr;
if (nforce.find("math_function_id") != nforce.end())
ffunction = math_functions_.at(
nforce.at("math_function_id").template get<unsigned>());
// Set id
int nset_id = nforce.at("nset_id").template get<int>();
// Direction
unsigned dir = nforce.at("dir").template get<unsigned>();
// Traction
double force = nforce.at("force").template get<double>();
// Read and assign nodal concentrated forces
bool nodal_force = mesh_->assign_nodal_concentrated_forces(
ffunction, nset_id, dir, force);
if (!nodal_force)
throw std::runtime_error(
"Concentrated nodal forces are not properly assigned");
set_node_concentrated_force_ = true;
}
}
} else
console_->warn("No concentrated nodal force is defined for the analysis");
}
//! Initialise math functions
template <unsigned Tdim>
bool mpm::MPMBase<Tdim>::initialise_math_functions(const Json& math_functions) {
bool status = true;
try {
// Get materials properties
for (const auto& function_props : math_functions) {
// Get math function id
auto function_id = function_props["id"].template get<unsigned>();
// Get function type
const std::string function_type =
function_props["type"].template get<std::string>();
// Create a new function from JSON object
auto function =
Factory<mpm::FunctionBase, unsigned, const Json&>::instance()->create(
function_type, std::move(function_id), function_props);
// Add material to list
auto insert_status =
math_functions_.insert(std::make_pair(function->id(), function));
// If insert material failed
if (!insert_status.second) {
status = false;
throw std::runtime_error(
"Invalid properties for new math function, fn insertion failed");
}
}
} catch (std::exception& exception) {
console_->error("#{}: Reading math functions: {}", __LINE__,
exception.what());
status = false;
}
return status;
}
//! Node entity sets
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::node_entity_sets(const Json& mesh_props,
bool check_duplicates) {
try {
if (mesh_props.find("entity_sets") != mesh_props.end()) {
std::string entity_sets =
mesh_props["entity_sets"].template get<std::string>();
if (!io_->file_name(entity_sets).empty()) {
bool node_sets = mesh_->create_node_sets(
(io_->entity_sets(io_->file_name(entity_sets), "node_sets")),
check_duplicates);
if (!node_sets)
throw std::runtime_error("Node sets are not properly assigned");
}
} else
throw std::runtime_error("Entity set JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Entity sets are undefined {} ", __LINE__,
exception.what());
}
}
//! Node Euler angles
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::node_euler_angles(
const Json& mesh_props, const std::shared_ptr<mpm::IOMesh<Tdim>>& mesh_io) {
try {
if (mesh_props.find("boundary_conditions") != mesh_props.end() &&
mesh_props["boundary_conditions"].find("nodal_euler_angles") !=
mesh_props["boundary_conditions"].end()) {
std::string euler_angles =
mesh_props["boundary_conditions"]["nodal_euler_angles"]
.template get<std::string>();
if (!io_->file_name(euler_angles).empty()) {
bool rotation_matrices = mesh_->compute_nodal_rotation_matrices(
mesh_io->read_euler_angles(io_->file_name(euler_angles)));
if (!rotation_matrices)
throw std::runtime_error(
"Euler angles are not properly assigned/computed");
}
} else
throw std::runtime_error("Euler angles JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Euler angles are undefined {} ", __LINE__,
exception.what());
}
}
// Nodal velocity constraints
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::nodal_velocity_constraints(
const Json& mesh_props, const std::shared_ptr<mpm::IOMesh<Tdim>>& mesh_io) {
try {
// Read and assign velocity constraints
if (mesh_props.find("boundary_conditions") != mesh_props.end() &&
mesh_props["boundary_conditions"].find("velocity_constraints") !=
mesh_props["boundary_conditions"].end()) {
// Iterate over velocity constraints
for (const auto& constraints :
mesh_props["boundary_conditions"]["velocity_constraints"]) {
// Velocity constraints are specified in a file
if (constraints.find("file") != constraints.end()) {
std::string velocity_constraints_file =
constraints.at("file").template get<std::string>();
bool velocity_constraints =
constraints_->assign_nodal_velocity_constraints(
mesh_io->read_velocity_constraints(
io_->file_name(velocity_constraints_file)));
if (!velocity_constraints)
throw std::runtime_error(
"Velocity constraints are not properly assigned");
} else {
// Set id
int nset_id = constraints.at("nset_id").template get<int>();
// Direction
unsigned dir = constraints.at("dir").template get<unsigned>();
// Velocity
double velocity = constraints.at("velocity").template get<double>();
// Add velocity constraint to mesh
auto velocity_constraint =
std::make_shared<mpm::VelocityConstraint>(nset_id, dir, velocity);
bool velocity_constraints =
constraints_->assign_nodal_velocity_constraint(
nset_id, velocity_constraint);
if (!velocity_constraints)
throw std::runtime_error(
"Nodal velocity constraint is not properly assigned");
}
}
} else
throw std::runtime_error("Velocity constraints JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Velocity constraints are undefined {} ", __LINE__,
exception.what());
}
}
// Nodal frictional constraints
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::nodal_frictional_constraints(
const Json& mesh_props, const std::shared_ptr<mpm::IOMesh<Tdim>>& mesh_io) {
try {
// Read and assign friction constraints
if (mesh_props.find("boundary_conditions") != mesh_props.end() &&
mesh_props["boundary_conditions"].find("friction_constraints") !=
mesh_props["boundary_conditions"].end()) {
// Iterate over velocity constraints
for (const auto& constraints :
mesh_props["boundary_conditions"]["friction_constraints"]) {
// Friction constraints are specified in a file
if (constraints.find("file") != constraints.end()) {
std::string friction_constraints_file =
constraints.at("file").template get<std::string>();
bool friction_constraints =
constraints_->assign_nodal_friction_constraints(
mesh_io->read_friction_constraints(
io_->file_name(friction_constraints_file)));
if (!friction_constraints)
throw std::runtime_error(
"Friction constraints are not properly assigned");
} else {
// Set id
int nset_id = constraints.at("nset_id").template get<int>();
// Direction
unsigned dir = constraints.at("dir").template get<unsigned>();
// Sign n
int sign_n = constraints.at("sign_n").template get<int>();
// Friction
double friction = constraints.at("friction").template get<double>();
// Add friction constraint to mesh
auto friction_constraint = std::make_shared<mpm::FrictionConstraint>(
nset_id, dir, sign_n, friction);
bool friction_constraints =
constraints_->assign_nodal_frictional_constraint(
nset_id, friction_constraint);
if (!friction_constraints)
throw std::runtime_error(
"Nodal friction constraint is not properly assigned");
}
}
} else
throw std::runtime_error("Friction constraints JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Friction conditions are undefined {} ", __LINE__,
exception.what());
}
}
//! Cell entity sets
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::cell_entity_sets(const Json& mesh_props,
bool check_duplicates) {
try {
if (mesh_props.find("entity_sets") != mesh_props.end()) {
// Read and assign cell sets
std::string entity_sets =
mesh_props["entity_sets"].template get<std::string>();
if (!io_->file_name(entity_sets).empty()) {
bool cell_sets = mesh_->create_cell_sets(
(io_->entity_sets(io_->file_name(entity_sets), "cell_sets")),
check_duplicates);
if (!cell_sets)
throw std::runtime_error("Cell sets are not properly assigned");
}
} else
throw std::runtime_error("Cell entity sets JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Cell entity sets are undefined {} ", __LINE__,
exception.what());
}
}
// Particles cells
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::particles_cells(
const Json& mesh_props,
const std::shared_ptr<mpm::IOMesh<Tdim>>& particle_io) {
try {
if (mesh_props.find("particle_cells") != mesh_props.end()) {
std::string fparticles_cells =
mesh_props["particle_cells"].template get<std::string>();
if (!io_->file_name(fparticles_cells).empty()) {
bool particles_cells =
mesh_->assign_particles_cells(particle_io->read_particles_cells(
io_->file_name(fparticles_cells)));
if (!particles_cells)
throw std::runtime_error(
"Particle cells are not properly assigned to particles");
}
} else
throw std::runtime_error("Particle cells JSON not found");
} catch (std::exception& exception) {
console_->warn("#{}: Particle cells are undefined {} ", __LINE__,
exception.what());
}
}
// Particles volumes
template <unsigned Tdim>
void mpm::MPMBase<Tdim>::particles_volumes(
const Json& mesh_props,
const std::shared_ptr<mpm::IOMesh<Tdim>>& particle_io) {
try {
if (mesh_props.find("particles_volumes") != mesh_props.end()) {
std::string fparticles_volumes =
mesh_props["particles_volumes"].template get<std::string>();