-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmesh.tcc
More file actions
2338 lines (2078 loc) · 81.9 KB
/
mesh.tcc
File metadata and controls
2338 lines (2078 loc) · 81.9 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
// Constructor with id
template <unsigned Tdim>
mpm::Mesh<Tdim>::Mesh(unsigned id, bool isoparametric)
: id_{id}, isoparametric_{isoparametric} {
// Check if the dimension is between 1 & 3
static_assert((Tdim >= 1 && Tdim <= 3), "Invalid global dimension");
//! Logger
std::string logger = "mesh::" + std::to_string(id);
console_ = std::make_unique<spdlog::logger>(logger, mpm::stdout_sink);
particles_.clear();
}
//! Create nodes from coordinates
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::create_nodes(mpm::Index gnid,
const std::string& node_type,
const std::vector<VectorDim>& coordinates,
bool check_duplicates) {
bool status = true;
try {
// Check if nodal coordinates is empty
if (coordinates.empty())
throw std::runtime_error("List of coordinates is empty");
// Iterate over all coordinates
for (const auto& node_coordinates : coordinates) {
// Add node to mesh and check
bool insert_status = this->add_node(
// Create a node of particular
Factory<mpm::NodeBase<Tdim>, mpm::Index,
const Eigen::Matrix<double, Tdim, 1>&>::instance()
->create(node_type, static_cast<mpm::Index>(gnid),
node_coordinates),
check_duplicates);
// Increment node id
if (insert_status) ++gnid;
// When addition of node fails
else
throw std::runtime_error("Addition of node to mesh failed!");
}
} catch (std::exception& exception) {
console_->error("{} #{}: {}\n", __FILE__, __LINE__, exception.what());
status = false;
}
return status;
}
//! Add a node to the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::add_node(const std::shared_ptr<mpm::NodeBase<Tdim>>& node,
bool check_duplicates) {
bool insertion_status = nodes_.add(node, check_duplicates);
// Add node to map
if (insertion_status) map_nodes_.insert(node->id(), node);
return insertion_status;
}
//! Remove a node from the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::remove_node(
const std::shared_ptr<mpm::NodeBase<Tdim>>& node) {
const mpm::Index id = node->id();
// Remove a node if found in the container
return (nodes_.remove(node) && map_nodes_.remove(id));
}
//! Iterate over nodes
template <unsigned Tdim>
template <typename Toper>
void mpm::Mesh<Tdim>::iterate_over_nodes(Toper oper) {
#pragma omp parallel for schedule(runtime)
for (auto nitr = nodes_.cbegin(); nitr != nodes_.cend(); ++nitr) oper(*nitr);
}
//! Iterate over particle set
template <unsigned Tdim>
template <typename Toper>
void mpm::Mesh<Tdim>::iterate_over_node_set(int set_id, Toper oper) {
// If set id is -1, use all nodes
if (set_id == -1) {
this->iterate_over_nodes(oper);
} else {
// Iterate over the node set
auto nodes = node_sets_.at(set_id);
#pragma omp parallel for schedule(runtime)
for (auto nitr = nodes.cbegin(); nitr != nodes.cend(); ++nitr) {
oper(*nitr);
}
}
}
//! Iterate over nodes
template <unsigned Tdim>
template <typename Toper, typename Tpred>
void mpm::Mesh<Tdim>::iterate_over_nodes_predicate(Toper oper, Tpred pred) {
#pragma omp parallel for schedule(runtime)
for (auto nitr = nodes_.cbegin(); nitr != nodes_.cend(); ++nitr) {
if (pred(*nitr)) oper(*nitr);
}
}
//! Create a list of active nodes in mesh
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_active_nodes() {
// Clear existing list of active nodes
this->active_nodes_.clear();
for (auto nitr = nodes_.cbegin(); nitr != nodes_.cend(); ++nitr)
if ((*nitr)->status()) this->active_nodes_.add(*nitr);
}
//! Iterate over active nodes
template <unsigned Tdim>
template <typename Toper>
void mpm::Mesh<Tdim>::iterate_over_active_nodes(Toper oper) {
#pragma omp parallel for schedule(runtime)
for (auto nitr = active_nodes_.cbegin(); nitr != active_nodes_.cend(); ++nitr)
oper(*nitr);
}
#ifdef USE_MPI
#ifdef USE_HALO_EXCHANGE
//! Nodal halo exchange
template <unsigned Tdim>
template <typename Ttype, unsigned Tnparam, typename Tgetfunctor,
typename Tsetfunctor>
void mpm::Mesh<Tdim>::nodal_halo_exchange(Tgetfunctor getter,
Tsetfunctor setter) {
// Create vector of nodal vectors
unsigned nnodes = this->domain_shared_nodes_.size();
// Get number of MPI ranks
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
if (mpi_size > 1) {
// Vector of send requests
std::vector<MPI_Request> send_requests;
send_requests.reserve(ncomms_);
unsigned j = 0;
// Non-blocking send
for (unsigned i = 0; i < nnodes; ++i) {
Ttype property = getter(domain_shared_nodes_[i]);
std::set<unsigned> node_mpi_ranks = domain_shared_nodes_[i]->mpi_ranks();
for (auto& node_rank : node_mpi_ranks) {
if (node_rank != mpi_rank) {
MPI_Isend(&property, Tnparam, MPI_DOUBLE, node_rank, 0,
MPI_COMM_WORLD, &send_requests[j]);
++j;
}
}
}
// send complete
for (unsigned i = 0; i < ncomms_; ++i)
MPI_Wait(&send_requests[i], MPI_STATUS_IGNORE);
for (unsigned i = 0; i < nnodes; ++i) {
// Get value at current node
Ttype property = getter(domain_shared_nodes_[i]);
std::set<unsigned> node_mpi_ranks = domain_shared_nodes_[i]->mpi_ranks();
// Receive from all shared ranks
for (auto& node_rank : node_mpi_ranks) {
if (node_rank != mpi_rank) {
Ttype value;
MPI_Recv(&value, Tnparam, MPI_DOUBLE, node_rank, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
property += value;
}
}
setter(domain_shared_nodes_[i], property);
}
}
}
#else
//! All reduce over nodal scalar property
template <unsigned Tdim>
template <typename Ttype, unsigned Tnparam, typename Tgetfunctor,
typename Tsetfunctor>
void mpm::Mesh<Tdim>::nodal_halo_exchange(Tgetfunctor getter,
Tsetfunctor setter) {
// Create vector of nodal scalars
std::vector<Ttype> prop_get(nhalo_nodes_, mpm::zero<Ttype>());
std::vector<Ttype> prop_set(nhalo_nodes_, mpm::zero<Ttype>());
#pragma omp parallel for schedule(runtime) shared(prop_get)
for (auto nitr = domain_shared_nodes_.cbegin();
nitr != domain_shared_nodes_.cend(); ++nitr)
prop_get.at((*nitr)->ghost_id()) = getter((*nitr));
MPI_Allreduce(prop_get.data(), prop_set.data(), nhalo_nodes_ * Tnparam,
MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#pragma omp parallel for schedule(runtime)
for (auto nitr = domain_shared_nodes_.cbegin();
nitr != domain_shared_nodes_.cend(); ++nitr)
setter((*nitr), prop_set.at((*nitr)->ghost_id()));
}
#endif
#endif
//! Create cells from node lists
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::create_cells(
mpm::Index gcid, const std::shared_ptr<mpm::Element<Tdim>>& element,
const std::vector<std::vector<mpm::Index>>& cells, bool check_duplicates) {
bool status = true;
try {
// Check if nodes in cell list is not empty
if (cells.empty())
throw std::runtime_error("List of nodes of cells is empty");
for (const auto& nodes : cells) {
// Create cell with element
auto cell = std::make_shared<mpm::Cell<Tdim>>(gcid, nodes.size(), element,
this->isoparametric_);
// Cell local node id
unsigned local_nid = 0;
// For nodeids in a given cell
for (auto nid : nodes) {
cell->add_node(local_nid, map_nodes_[nid]);
++local_nid;
}
// Add cell to mesh
bool insert_cell = false;
// Check if cell has all nodes before inserting to mesh
if (cell->nnodes() == nodes.size()) {
// Initialise cell before insertion
cell->initialise();
// If cell is initialised insert to mesh
if (cell->is_initialised())
insert_cell = this->add_cell(cell, check_duplicates);
} else
throw std::runtime_error("Invalid node ids for cell!");
// Increment global cell id
if (insert_cell) ++gcid;
// When addition of cell fails
else
throw std::runtime_error("Addition of cell to mesh failed!");
}
} catch (std::exception& exception) {
console_->error("{} #{}: {}\n", __FILE__, __LINE__, exception.what());
status = false;
}
return status;
}
//! Add a cell to the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::add_cell(const std::shared_ptr<mpm::Cell<Tdim>>& cell,
bool check_duplicates) {
bool insertion_status = cells_.add(cell, check_duplicates);
// Add cell to map
if (insertion_status) map_cells_.insert(cell->id(), cell);
return insertion_status;
}
//! Remove a cell from the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::remove_cell(
const std::shared_ptr<mpm::Cell<Tdim>>& cell) {
const mpm::Index id = cell->id();
// Remove a cell if found in the container
return (cells_.remove(cell) && map_cells_.remove(id));
}
//! Iterate over cells
template <unsigned Tdim>
template <typename Toper>
void mpm::Mesh<Tdim>::iterate_over_cells(Toper oper) {
#pragma omp parallel for schedule(runtime)
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr) oper(*citr);
}
//! Create cells from node lists
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_cell_neighbours() {
// Initialize and compute node cell map
tsl::robin_map<mpm::Index, std::set<mpm::Index>> node_cell_map;
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr) {
// Populate node_cell_map with the node_id and multiple cell_id
auto cell_id = (*citr)->id();
for (auto id : (*citr)->nodes_id()) node_cell_map[id].insert(cell_id);
}
#pragma omp parallel for schedule(runtime)
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr) {
// Iterate over each node in current cell
for (auto id : (*citr)->nodes_id()) {
auto cell_id = (*citr)->id();
// Get the cells associated with each node
for (auto neighbour_id : node_cell_map[id])
if (neighbour_id != cell_id) (*citr)->add_neighbour(neighbour_id);
}
}
}
//! Compute average cell size
template <unsigned Tdim>
double mpm::Mesh<Tdim>::compute_average_cell_size() const {
double mesh_size = 0.0;
#pragma omp parallel for schedule(runtime) reduction(+ : mesh_size)
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr)
mesh_size += (*citr)->mean_length();
mesh_size *= 1. / cells_.size();
return mesh_size;
}
//! Find global number of particles across MPI ranks / cell
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_nglobal_particles_cells() {
int mpi_rank = 0;
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr) {
int nparticles;
// Determine the rank of the broadcast emitter process
if ((*citr)->rank() == mpi_rank) nparticles = (*citr)->nparticles();
MPI_Bcast(&nparticles, 1, MPI_INT, (*citr)->rank(), MPI_COMM_WORLD);
// Receive broadcast and update on all ranks
(*citr)->nglobal_particles(nparticles);
}
#endif
}
//! Find particle neighbours for all particle
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_particle_neighbours() {
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr)
this->find_particle_neighbours(*citr);
}
//! Find particle neighbours for specific cell particle
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_particle_neighbours(
const std::shared_ptr<mpm::Cell<Tdim>>& cell) {
int mpi_rank = 0;
#ifdef USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
// Particles in current cell
std::vector<mpm::Index> neighbour_particles = cell->particles();
// Loop over all neighboring cells, and append particle ids from each cell
for (const auto& neighbour_cell_id : cell->neighbours()) {
// Get the MPI rank of the neighbour cell
int neighbour_cell_rank = map_cells_[neighbour_cell_id]->rank();
if (neighbour_cell_rank != cell->rank()) {
#ifdef USE_MPI
// Send particle ids
if (neighbour_cell_rank == mpi_rank) {
// Get particle ids from each cell
auto send_particle_ids = map_cells_[neighbour_cell_id]->particles();
// Get size of the particle ids
int pid_size = send_particle_ids.size();
// Send the size of the particles in cell
MPI_Send(&pid_size, 1, MPI_INT, cell->rank(), neighbour_cell_id,
MPI_COMM_WORLD);
// Send particle ids if it is not empty
if (pid_size > 0)
MPI_Send(send_particle_ids.data(), pid_size, MPI_UNSIGNED_LONG_LONG,
cell->rank(), neighbour_cell_id, MPI_COMM_WORLD);
}
// Receive particle ids in the current MPI rank
if (cell->rank() == mpi_rank) {
// Particle ids at local cell MPI rank
std::vector<mpm::Index> received_particle_ids;
int nparticles = 0;
MPI_Recv(&nparticles, 1, MPI_INT, neighbour_cell_rank,
neighbour_cell_id, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (nparticles > 0) {
received_particle_ids.resize(nparticles);
MPI_Recv(received_particle_ids.data(), nparticles,
MPI_UNSIGNED_LONG_LONG, neighbour_cell_rank,
neighbour_cell_id, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
neighbour_particles.insert(neighbour_particles.end(),
received_particle_ids.begin(),
received_particle_ids.end());
}
#endif
} else {
const auto& particle_ids = map_cells_[neighbour_cell_id]->particles();
neighbour_particles.insert(neighbour_particles.end(),
particle_ids.begin(), particle_ids.end());
}
}
// Assign neighbouring particle ids to particles in the current cell
for (auto particle_id : cell->particles())
map_particles_[particle_id]->assign_neighbours(neighbour_particles);
}
//! Find ghost cell neighbours
template <unsigned Tdim>
void mpm::Mesh<Tdim>::find_ghost_boundary_cells() {
#ifdef USE_MPI
// Get number of MPI ranks
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
bool check_duplicates = true;
if (mpi_size > 1) {
ghost_cells_.clear();
local_ghost_cells_.clear();
ghost_cells_neighbour_ranks_.clear();
// Iterate through cells
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr) {
std::set<unsigned> neighbour_ranks;
// If cell rank is the current MPI rank
if ((*citr)->rank() == mpi_rank) {
// Iterate through the neighbours of a cell
auto neighbours = (*citr)->neighbours();
for (auto neighbour : neighbours) {
// If the neighbour is in a different MPI rank
if (map_cells_[neighbour]->rank() != mpi_rank) {
ghost_cells_.add(map_cells_[neighbour], check_duplicates);
// Add mpi rank to set
neighbour_ranks.insert(map_cells_[neighbour]->rank());
}
}
}
// Set the number of different MPI rank neighbours to a ghost cell
if (neighbour_ranks.size() > 0) {
// Also add the current cell, as this would be a receiver
local_ghost_cells_.add(*citr, check_duplicates);
// Update the neighbouring ranks of the local ghost cell
std::vector<unsigned> mpi_neighbours;
for (auto rank : neighbour_ranks) mpi_neighbours.emplace_back(rank);
ghost_cells_neighbour_ranks_[(*citr)->id()] = mpi_neighbours;
}
}
}
#endif
}
//! Number of particles in the mesh with specific type
template <unsigned Tdim>
mpm::Index mpm::Mesh<Tdim>::nparticles(const std::string& particle_type) const {
return std::count_if(
particles_.cbegin(), particles_.cend(),
[&ptype =
particle_type](const std::shared_ptr<mpm::ParticleBase<Tdim>>& ptr) {
return (ptr)->type() == ptype;
});
}
//! Find ncells in rank
template <unsigned Tdim>
mpm::Index mpm::Mesh<Tdim>::ncells_rank(bool active_cells) {
unsigned ncells_rank = 0;
int mpi_rank = 0;
int mpi_size = 1;
#ifdef USE_MPI
// Get number of MPI ranks
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
if (active_cells) {
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr)
if ((*citr)->rank() == mpi_rank && (*citr)->nparticles() > 0)
ncells_rank += 1;
} else {
for (auto citr = cells_.cbegin(); citr != cells_.cend(); ++citr)
if ((*citr)->rank() == mpi_rank) ncells_rank += 1;
}
return ncells_rank;
}
//! Find nnodes in rank
template <unsigned Tdim>
mpm::Index mpm::Mesh<Tdim>::nnodes_rank() {
unsigned nnodes_rank = 0;
int mpi_rank = 0;
#ifdef USE_MPI
// Get number of MPI ranks
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
for (auto nitr = nodes_.cbegin(); nitr != nodes_.cend(); ++nitr) {
// Get MPI ranks for the node
auto mpi_ranks = (*nitr)->mpi_ranks();
// Check if the local rank is in the list of ranks for the node
const bool local_node = mpi_ranks.find(mpi_rank) != mpi_ranks.end();
if (local_node) nnodes_rank += 1;
}
return nnodes_rank;
}
//! Create cells from node lists
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::generate_material_points(
unsigned nquadratures, const std::string& particle_type,
const std::vector<unsigned>& material_ids, int cset_id, unsigned pset_id) {
bool status = true;
try {
if (cells_.size() > 0) {
// Particle ids
std::vector<mpm::Index> pids;
unsigned before_generation = this->nparticles();
bool checks = false;
// Get material
std::vector<std::shared_ptr<mpm::Material<Tdim>>> materials;
for (auto m_id : material_ids)
materials.emplace_back(materials_.at(m_id));
// If set id is -1, use all cells
auto cset = (cset_id == -1) ? this->cells_ : cell_sets_.at(cset_id);
// Iterate over each cell to generate points
for (auto citr = cset.cbegin(); citr != cset.cend(); ++citr) {
(*citr)->assign_quadrature(nquadratures);
// Genereate particles at the Gauss points
const auto cpoints = (*citr)->generate_points();
// Iterate over each coordinate to generate material points
for (const auto& coordinates : cpoints) {
// Particle id
mpm::Index pid = particles_.size();
// Create particle
auto particle =
Factory<mpm::ParticleBase<Tdim>, mpm::Index,
const Eigen::Matrix<double, Tdim, 1>&>::instance()
->create(particle_type, static_cast<mpm::Index>(pid),
coordinates);
// Add particle to mesh
status = this->add_particle(particle, checks);
if (status) {
map_particles_[pid]->assign_cell(*citr);
for (unsigned phase = 0; phase < materials.size(); phase++)
map_particles_[pid]->assign_material(materials[phase], phase);
pids.emplace_back(pid);
} else
throw std::runtime_error("Generate particles in mesh failed");
}
}
if (before_generation == this->nparticles())
throw std::runtime_error("No particles were generated!");
// Add particles to set
status = this->particle_sets_
.insert(std::pair<mpm::Index, std::vector<mpm::Index>>(
pset_id, pids))
.second;
if (!status) throw std::runtime_error("Particle set creation failed");
console_->info(
"Generate points:\n# of cells: {}\nExpected # of points: {}\n"
"# of points generated: {}",
cells_.size(), cells_.size() * std::pow(nquadratures, Tdim),
particles_.size());
} else
throw std::runtime_error("No cells are found in the mesh!");
} catch (std::exception& exception) {
console_->error("{} #{}: {}\n", __FILE__, __LINE__, exception.what());
status = false;
}
return status;
}
//! Create particles from coordinates
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::create_particles(
const std::string& particle_type, const std::vector<VectorDim>& coordinates,
const std::vector<unsigned>& material_ids, unsigned pset_id,
bool check_duplicates) {
bool status = true;
try {
// Particle ids
std::vector<mpm::Index> pids;
// Get material
std::vector<std::shared_ptr<mpm::Material<Tdim>>> materials;
for (auto m_id : material_ids) materials.emplace_back(materials_.at(m_id));
// Check if particle coordinates is empty
if (coordinates.empty())
throw std::runtime_error("List of coordinates is empty");
// Iterate over particle coordinates
for (const auto& particle_coordinates : coordinates) {
// Particle id
mpm::Index pid = particles_.size();
// Create particle
auto particle = Factory<mpm::ParticleBase<Tdim>, mpm::Index,
const Eigen::Matrix<double, Tdim, 1>&>::instance()
->create(particle_type, static_cast<mpm::Index>(pid),
particle_coordinates);
// Add particle to mesh and check
bool insert_status = this->add_particle(particle, check_duplicates);
// If insertion is successful
if (insert_status) {
for (unsigned phase = 0; phase < materials.size(); phase++)
map_particles_[pid]->assign_material(materials[phase], phase);
pids.emplace_back(pid);
} else
throw std::runtime_error("Addition of particle to mesh failed!");
}
// Add particles to set
status = this->particle_sets_
.insert(std::pair<mpm::Index, std::vector<mpm::Index>>(pset_id,
pids))
.second;
if (!status) throw std::runtime_error("Particle set creation failed");
} catch (std::exception& exception) {
console_->error("{} #{}: {}\n", __FILE__, __LINE__, exception.what());
status = false;
}
return status;
}
//! Add a particle pointer to the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::add_particle(
const std::shared_ptr<mpm::ParticleBase<Tdim>>& particle, bool checks) {
bool status = false;
try {
if (checks) {
// Add only if particle can be located in any cell of the mesh
if (this->locate_particle_cells(particle)) {
status = particles_.add(particle, checks);
particles_cell_ids_.insert(std::pair<mpm::Index, mpm::Index>(
particle->id(), particle->cell_id()));
map_particles_.insert(particle->id(), particle);
} else {
throw std::runtime_error("Particle not found in mesh");
}
} else {
status = particles_.add(particle, checks);
particles_cell_ids_.insert(std::pair<mpm::Index, mpm::Index>(
particle->id(), particle->cell_id()));
map_particles_.insert(particle->id(), particle);
}
if (!status) throw std::runtime_error("Particle addition failed");
} catch (std::exception& exception) {
console_->error("{} #{}: {}\n", __FILE__, __LINE__, exception.what());
status = false;
}
return status;
}
//! Remove a particle pointer from the mesh
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::remove_particle(
const std::shared_ptr<mpm::ParticleBase<Tdim>>& particle) {
const mpm::Index id = particle->id();
// Remove associated cell for the particle
map_particles_[id]->remove_cell();
// Remove a particle if found in the container and map
return (particles_.remove(particle) && map_particles_.remove(id));
}
//! Remove a particle by id
template <unsigned Tdim>
bool mpm::Mesh<Tdim>::remove_particle_by_id(mpm::Index id) {
// Remove associated cell for the particle
map_particles_[id]->remove_cell();
bool result = particles_.remove(map_particles_[id]);
return (result && map_particles_.remove(id));
}
//! Remove a particle by id
template <unsigned Tdim>
void mpm::Mesh<Tdim>::remove_particles(const std::vector<mpm::Index>& pids) {
if (!pids.empty()) {
// Get MPI rank
int mpi_size = 1;
#ifdef USE_MPI
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
#endif
for (auto& id : pids) {
map_particles_[id]->remove_cell();
map_particles_.remove(id);
}
// Get number of particles to reserve size
unsigned nparticles = this->nparticles();
// Clear particles and start a new element of particles
particles_.clear();
particles_.reserve(static_cast<int>(nparticles / mpi_size));
// Iterate over the map of particles and add them to container
for (auto& particle : map_particles_)
particles_.add(particle.second, false);
}
}
//! Remove all particles in a cell given cell id
template <unsigned Tdim>
void mpm::Mesh<Tdim>::remove_all_nonrank_particles() {
// Get MPI rank
int mpi_rank = 0;
int mpi_size = 1;
#ifdef USE_MPI
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
// Remove associated cell for the particle
for (auto citr = this->cells_.cbegin(); citr != this->cells_.cend(); ++citr) {
// If cell is non empty
if ((*citr)->particles().size() != 0 && (*citr)->rank() != mpi_rank) {
auto pids = (*citr)->particles();
// Remove particles from map
for (auto& id : pids) {
map_particles_[id]->remove_cell();
map_particles_.remove(id);
}
(*citr)->clear_particle_ids();
}
}
// Get number of particles to reserve size
unsigned nparticles = this->nparticles();
// Clear particles and start a new element of particles
particles_.clear();
particles_.reserve(static_cast<int>(nparticles / mpi_size));
// Iterate over the map of particles and add them to container
for (auto& particle : map_particles_) particles_.add(particle.second, false);
}
//! Transfer all particles in cells that are not in local rank
template <unsigned Tdim>
void mpm::Mesh<Tdim>::transfer_halo_particles() {
#ifdef USE_MPI
// Get number of MPI ranks
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
if (mpi_size > 1) {
// Stable count buffers + properly sized requests
const std::size_t nghost = this->ghost_cells_.size();
std::vector<unsigned> send_counts(nghost, 0);
std::vector<MPI_Request> send_requests;
send_requests.resize(nghost);
// Collect particles
std::vector<mpm::Index> remove_pids;
// Iterate through the ghost cells and send counts (nonblocking)
std::size_t i = 0;
for (auto citr = this->ghost_cells_.cbegin();
citr != this->ghost_cells_.cend(); ++citr, ++i) {
const auto& particle_ids = (*citr)->particles();
send_counts[i] = static_cast<unsigned>(particle_ids.size());
MPI_Isend(&send_counts[i], 1, MPI_UNSIGNED, (*citr)->rank(), 1,
MPI_COMM_WORLD, &send_requests[i]);
}
// Iterate through the ghost cells and send payloads (blocking, unchanged)
for (auto citr = this->ghost_cells_.cbegin();
citr != this->ghost_cells_.cend(); ++citr) {
// Send number of particles to receiver rank
const auto& particle_ids = (*citr)->particles();
for (auto& id : particle_ids) {
// Create a vector of serialized particle
std::vector<uint8_t> buffer = map_particles_[id]->serialize();
MPI_Send(buffer.data(), static_cast<int>(buffer.size()), MPI_UINT8_T,
(*citr)->rank(), 0, MPI_COMM_WORLD);
// Particles to be removed from the current rank
remove_pids.emplace_back(id);
}
(*citr)->clear_particle_ids();
}
// Remove all sent particles
this->remove_particles(remove_pids);
// Wait for all count sends to complete
for (std::size_t k = 0; k < nghost; ++k)
MPI_Wait(&send_requests[k], MPI_STATUS_IGNORE);
// Particle id
mpm::Index pid = 0;
// Initial particle coordinates
Eigen::Matrix<double, Tdim, 1> pcoordinates =
Eigen::Matrix<double, Tdim, 1>::Zero();
// Iterate through the local ghost cells and receive particles
for (auto citr = this->local_ghost_cells_.cbegin();
citr != this->local_ghost_cells_.cend(); ++citr) {
std::vector<unsigned> neighbour_ranks =
ghost_cells_neighbour_ranks_[(*citr)->id()];
// Total number of particles (one count per neighbour rank)
std::vector<unsigned> nrank_particles(neighbour_ranks.size(), 0);
for (unsigned ir = 0; ir < neighbour_ranks.size(); ++ir)
MPI_Recv(&nrank_particles[ir], 1, MPI_UNSIGNED, neighbour_ranks[ir], 1,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Receive number of particles
unsigned nrecv_particles =
std::accumulate(nrank_particles.begin(), nrank_particles.end(), 0u);
for (unsigned j = 0; j < nrecv_particles; ++j) {
// Retrieve information about the incoming message
MPI_Status status;
MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
// Get buffer size
int size = 0;
MPI_Get_count(&status, MPI_UINT8_T, &size);
// Allocate the buffer now that we know how many elements there are
std::vector<uint8_t> buffer(static_cast<std::size_t>(size));
// Receive from the probed source+tag
MPI_Recv(buffer.data(), size, MPI_UINT8_T, status.MPI_SOURCE,
status.MPI_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
uint8_t* bufptr = const_cast<uint8_t*>(buffer.data());
int position = 0;
// Get particle type
unsigned ptype = 0;
MPI_Unpack(bufptr, static_cast<int>(buffer.size()), &position, &ptype,
1, MPI_UNSIGNED, MPI_COMM_WORLD);
std::string particle_type = mpm::ParticleTypeName.at(ptype);
// Get materials material ids
unsigned nmaterials = 0;
MPI_Unpack(bufptr, static_cast<int>(buffer.size()), &position,
&nmaterials, 1, MPI_UNSIGNED, MPI_COMM_WORLD);
// Vector of materials
std::vector<std::shared_ptr<mpm::Material<Tdim>>> materials;
materials.reserve(static_cast<std::size_t>(nmaterials));
for (unsigned k = 0; k < nmaterials; ++k) {
unsigned mat_id = 0;
MPI_Unpack(bufptr, static_cast<int>(buffer.size()), &position,
&mat_id, 1, MPI_UNSIGNED, MPI_COMM_WORLD);
materials.emplace_back(materials_.at(mat_id));
}
// Create particle
auto particle =
Factory<mpm::ParticleBase<Tdim>, mpm::Index,
const Eigen::Matrix<double, Tdim, 1>&>::instance()
->create(particle_type, static_cast<mpm::Index>(pid),
pcoordinates);
particle->deserialize(buffer, materials);
// Add particle to mesh
this->add_particle(particle, true);
}
}
}
#endif
}
//! Transfer all particles in cells that are not in local rank
template <unsigned Tdim>
void mpm::Mesh<Tdim>::transfer_nonrank_particles(
const std::vector<mpm::Index>& exchange_cells) {
#ifdef USE_MPI
// Get number of MPI ranks
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
if (mpi_size > 1) {
std::vector<MPI_Request> send_requests;
send_requests.reserve(exchange_cells.size());
std::vector<MPI_Request> send_particle_requests;
send_particle_requests.reserve(exchange_cells.size() * 100);
unsigned nsend_requests = 0;
unsigned np = 0;
std::vector<mpm::Index> remove_pids;
// Iterate through the ghost cells and send particles
for (auto cid : exchange_cells) {
// Get cell pointer
auto cell = map_cells_[cid];
// If the previous rank of cell is the current MPI rank,
// then send all particles
if ((cell->rank() != cell->previous_mpirank()) &&
(cell->previous_mpirank() == mpi_rank)) {
// Send number of particles to receiver rank
unsigned nparticles = cell->nparticles();
MPI_Ibsend(&nparticles, 1, MPI_UNSIGNED, cell->rank(), 0,
MPI_COMM_WORLD, &send_requests[nsend_requests]);
auto particle_ids = cell->particles();
for (auto& id : particle_ids) {
// Create a vector of serialized particle
std::vector<uint8_t> buffer = map_particles_[id]->serialize();
MPI_Ibsend(buffer.data(), buffer.size(), MPI_UINT8_T, cell->rank(), 0,
MPI_COMM_WORLD, &send_particle_requests[np]);
++np;
// Particles to be removed from the current rank
remove_pids.emplace_back(id);
}
cell->clear_particle_ids();
++nsend_requests;
}
}
// Remove all sent particles
this->remove_particles(remove_pids);
// Send complete iterate only upto valid send requests
for (unsigned i = 0; i < nsend_requests; ++i)
MPI_Wait(&send_requests[i], MPI_STATUS_IGNORE);
// Send particles complete
for (unsigned i = 0; i < np; ++i)
MPI_Wait(&send_particle_requests[i], MPI_STATUS_IGNORE);
// Particle id
mpm::Index pid = 0;
// Initial particle coordinates
Eigen::Matrix<double, Tdim, 1> pcoordinates =
Eigen::Matrix<double, Tdim, 1>::Zero();
// Iterate through the ghost cells and receive particles
for (auto cid : exchange_cells) {
// Get cell pointer
auto cell = map_cells_[cid];
// If the current rank is the MPI rank receive particles
if ((cell->rank() != cell->previous_mpirank()) &&
(cell->rank() == mpi_rank)) {
// Receive number of particles
unsigned nrecv_particles;
MPI_Recv(&nrecv_particles, 1, MPI_UNSIGNED, cell->previous_mpirank(), 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (unsigned j = 0; j < nrecv_particles; ++j) {
// Retrieve information about the incoming message
MPI_Status status;
MPI_Probe(cell->previous_mpirank(), 0, MPI_COMM_WORLD, &status);
// Get buffer size
int size;
MPI_Get_count(&status, MPI_UINT8_T, &size);
// Allocate the buffer now that we know how many elements there are
std::vector<uint8_t> buffer;
buffer.resize(size);
// Finally receive the message
MPI_Recv(buffer.data(), size, MPI_UINT8_T, cell->previous_mpirank(),
0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
uint8_t* bufptr = const_cast<uint8_t*>(&buffer[0]);
int position = 0;
// Get particle type
int ptype;
MPI_Unpack(bufptr, buffer.size(), &position, &ptype, 1, MPI_INT,
MPI_COMM_WORLD);
std::string particle_type = mpm::ParticleTypeName.at(ptype);
// Get materials material id
int nmaterials = 0;
MPI_Unpack(bufptr, buffer.size(), &position, &nmaterials, 1,
MPI_UNSIGNED, MPI_COMM_WORLD);
std::vector<std::shared_ptr<mpm::Material<Tdim>>> materials;
materials.reserve(nmaterials);
for (unsigned k = 0; k < nmaterials; ++k) {
int mat_id;
MPI_Unpack(bufptr, buffer.size(), &position, &mat_id, 1,
MPI_UNSIGNED, MPI_COMM_WORLD);
// Get material
materials.emplace_back(materials_.at(mat_id));
}
// Create particle
auto particle =
Factory<mpm::ParticleBase<Tdim>, mpm::Index,
const Eigen::Matrix<double, Tdim, 1>&>::instance()
->create(particle_type, static_cast<mpm::Index>(pid),
pcoordinates);
particle->deserialize(buffer, materials);
// Add particle to mesh
this->add_particle(particle, true);
}
}
}
}
#endif
}
//! Resume cell ranks and partitioned domain
template <unsigned Tdim>
void mpm::Mesh<Tdim>::resume_domain_cell_ranks() {