-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathboundary_info.C
More file actions
3366 lines (2730 loc) · 108 KB
/
Copy pathboundary_info.C
File metadata and controls
3366 lines (2730 loc) · 108 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
// The libMesh Finite Element Library.
// Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Local includes
#include "libmesh/libmesh_config.h"
#include "libmesh/libmesh_logging.h"
#include "libmesh/boundary_info.h"
#include "libmesh/distributed_mesh.h"
#include "libmesh/elem.h"
#include "libmesh/mesh_communication.h"
#include "libmesh/mesh_serializer.h"
#include "libmesh/parallel.h"
#include "libmesh/partitioner.h"
#include "libmesh/remote_elem.h"
#include "libmesh/unstructured_mesh.h"
#include "libmesh/elem_side_builder.h"
// TIMPI includes
#include "timpi/parallel_sync.h"
// C++ includes
#include <iterator> // std::distance
namespace
{
// Templated helper function for removing a subset of keys from a
// multimap that further satisfy a given predicate on the
// corresponding values.
template <class Key, class T, class Pred>
void erase_if(std::multimap<Key,T> & map, Key k, Pred pred)
{
auto rng = map.equal_range(k);
auto it = rng.first;
while (it != rng.second)
{
if (pred(it->second))
it = map.erase(it);
else
++it;
}
}
// Similar to the helper function above but doesn't take a key,
// instead it applies the predicate to every value in the map.
template <class Key, class T, class Pred>
void erase_if(std::multimap<Key,T> & map, Pred pred)
{
auto it = map.begin();
while (it != map.end())
{
if (pred(it->second))
it = map.erase(it);
else
++it;
}
}
// Helper func for renumber_id
template <typename Map, typename T>
void renumber_name(Map & m, T old_id, T new_id)
{
if (const auto it = std::as_const(m).find(old_id);
it != m.end())
{
m[new_id] = it->second;
m.erase(it);
}
}
}
namespace libMesh
{
//------------------------------------------------------
// BoundaryInfo static member initializations
const boundary_id_type BoundaryInfo::invalid_id = -123;
//------------------------------------------------------
// BoundaryInfo functions
BoundaryInfo::BoundaryInfo(MeshBase & m) :
ParallelObject(m.comm()),
_mesh (&m),
_children_on_boundary(false)
{
}
BoundaryInfo & BoundaryInfo::operator=(const BoundaryInfo & other_boundary_info)
{
// Overwrite any preexisting boundary info
this->clear();
/**
* We're going to attempt to pull _new_ pointers out of the mesh
* assigned to this boundary info.
*
* This will only work if the mesh assigned to this BoundaryInfo is
* the same mesh object as other_boundary_info _or_ was constructed
* in exactly the same way (or constructed as a copy, or a refined
* copy without renumbering, etc.).
*/
// Copy node boundary info
for (const auto & [node, bid] : other_boundary_info._boundary_node_id)
_boundary_node_id.emplace(_mesh->node_ptr(node->id()), bid);
// Copy edge boundary info
for (const auto & [elem, id_pair] : other_boundary_info._boundary_edge_id)
_boundary_edge_id.emplace(_mesh->elem_ptr(elem->id()), id_pair);
// Copy shellface boundary info
for (const auto & [elem, id_pair] : other_boundary_info._boundary_shellface_id)
_boundary_shellface_id.emplace(_mesh->elem_ptr(elem->id()), id_pair);
// Copy side boundary info
for (const auto & [elem, id_pair] : other_boundary_info._boundary_side_id)
_boundary_side_id.emplace(_mesh->elem_ptr(elem->id()), id_pair);
_boundary_ids = other_boundary_info._boundary_ids;
_global_boundary_ids = other_boundary_info._global_boundary_ids;
_side_boundary_ids = other_boundary_info._side_boundary_ids;
_node_boundary_ids = other_boundary_info._node_boundary_ids;
_sideset_to_nodeset_conversion = other_boundary_info._sideset_to_nodeset_conversion;
_edge_boundary_ids = other_boundary_info._edge_boundary_ids;
_shellface_boundary_ids = other_boundary_info._shellface_boundary_ids;
_ss_id_to_name = other_boundary_info._ss_id_to_name;
_ns_id_to_name = other_boundary_info._ns_id_to_name;
_es_id_to_name = other_boundary_info._es_id_to_name;
return *this;
}
bool BoundaryInfo::operator==(const BoundaryInfo & other_boundary_info) const
{
for (const auto & [other_node, bid] : other_boundary_info._boundary_node_id)
{
const Node * node = this->_mesh->query_node_ptr(other_node->id());
if (!node)
return false;
if (!this->has_boundary_id(node, bid))
return false;
}
for (const auto & [node, bid] : this->_boundary_node_id)
{
const Node * other_node =
other_boundary_info._mesh->query_node_ptr(node->id());
if (!other_node)
return false;
if (!other_boundary_info.has_boundary_id(other_node, bid))
return false;
}
auto compare_edges = [&](const Elem * elem,
const Elem * other_elem,
unsigned short int edge)
{
if (!elem)
return false;
if (!other_elem)
return false;
std::vector<boundary_id_type> our_edges, other_edges;
this->edge_boundary_ids(elem, edge, our_edges);
other_boundary_info.edge_boundary_ids(other_elem, edge, other_edges);
if (our_edges.size() != other_edges.size())
return false;
std::sort(our_edges.begin(), our_edges.end());
std::sort(other_edges.begin(), other_edges.end());
for (auto i : index_range(our_edges))
if (our_edges[i] != other_edges[i])
return false;
return true;
};
for (const auto & [other_elem, edge_id_pair] : other_boundary_info._boundary_edge_id)
{
const Elem * elem = this->_mesh->query_elem_ptr(other_elem->id());
if (!compare_edges(elem, other_elem, edge_id_pair.first))
return false;
}
for (const auto & [elem, edge_id_pair] : this->_boundary_edge_id)
{
const Elem * other_elem = other_boundary_info._mesh->query_elem_ptr(elem->id());
if (!compare_edges(elem, other_elem, edge_id_pair.first))
return false;
}
auto compare_sides = [&](const Elem * elem,
const Elem * other_elem,
unsigned short int side)
{
if (!elem)
return false;
if (!other_elem)
return false;
std::vector<boundary_id_type> our_sides, other_sides;
this->boundary_ids(elem, side, our_sides);
other_boundary_info.boundary_ids(other_elem, side, other_sides);
if (our_sides.size() != other_sides.size())
return false;
std::sort(our_sides.begin(), our_sides.end());
std::sort(other_sides.begin(), other_sides.end());
for (auto i : index_range(our_sides))
if (our_sides[i] != other_sides[i])
return false;
return true;
};
for (const auto & [other_elem, side_id_pair] : other_boundary_info._boundary_side_id)
{
const Elem * elem = this->_mesh->query_elem_ptr(other_elem->id());
if (!compare_sides(elem, other_elem, side_id_pair.first))
return false;
}
for (const auto & [elem, side_id_pair] : this->_boundary_side_id)
{
const Elem * other_elem = other_boundary_info._mesh->query_elem_ptr(elem->id());
if (!compare_sides(elem, other_elem, side_id_pair.first))
return false;
}
auto compare_shellfaces = [&](const Elem * elem,
const Elem * other_elem,
unsigned short int shellface)
{
if (!elem)
return false;
if (!other_elem)
return false;
std::vector<boundary_id_type> our_shellfaces, other_shellfaces;
this->shellface_boundary_ids(elem, shellface, our_shellfaces);
other_boundary_info.shellface_boundary_ids(other_elem, shellface, other_shellfaces);
if (our_shellfaces.size() != other_shellfaces.size())
return false;
std::sort(our_shellfaces.begin(), our_shellfaces.end());
std::sort(other_shellfaces.begin(), other_shellfaces.end());
for (auto i : index_range(our_shellfaces))
if (our_shellfaces[i] != other_shellfaces[i])
return false;
return true;
};
for (const auto & [other_elem, shellface_id_pair] : other_boundary_info._boundary_shellface_id)
{
const Elem * elem = this->_mesh->query_elem_ptr(other_elem->id());
if (!compare_shellfaces(elem, other_elem, shellface_id_pair.first))
return false;
}
for (const auto & [elem, shellface_id_pair] : this->_boundary_shellface_id)
{
const Elem * other_elem = other_boundary_info._mesh->query_elem_ptr(elem->id());
if (!compare_shellfaces(elem, other_elem, shellface_id_pair.first))
return false;
}
if (_children_on_boundary != other_boundary_info._children_on_boundary)
return false;
auto compare_sets = [](const auto & set1, const auto & set2)
{
if (set1.size() != set2.size())
return false;
for (boundary_id_type bid : set1)
if (!set2.count(bid))
return false;
return true;
};
if (!compare_sets(_boundary_ids,
other_boundary_info._boundary_ids) ||
!compare_sets(_global_boundary_ids,
other_boundary_info._global_boundary_ids) ||
!compare_sets(_edge_boundary_ids,
other_boundary_info._edge_boundary_ids) ||
!compare_sets(_node_boundary_ids,
other_boundary_info._node_boundary_ids) ||
!compare_sets(_shellface_boundary_ids,
other_boundary_info._shellface_boundary_ids) ||
!compare_sets(_side_boundary_ids,
other_boundary_info._side_boundary_ids))
return false;
auto compare_maps = [](const auto & map1, const auto & map2)
{
if (map1.size() != map2.size())
return false;
for (const auto & pair : map1)
if (!map2.count(pair.first) ||
map2.at(pair.first) != pair.second)
return false;
return true;
};
if (!compare_maps(_ss_id_to_name,
other_boundary_info._ss_id_to_name) ||
!compare_maps(_ns_id_to_name,
other_boundary_info._ns_id_to_name) ||
!compare_maps(_es_id_to_name,
other_boundary_info._es_id_to_name))
return false;
return true;
}
BoundaryInfo::~BoundaryInfo() = default;
void BoundaryInfo::clear()
{
_boundary_node_id.clear();
_boundary_side_id.clear();
_boundary_edge_id.clear();
_boundary_shellface_id.clear();
_boundary_ids.clear();
_side_boundary_ids.clear();
_node_boundary_ids.clear();
_sideset_to_nodeset_conversion.clear();
_edge_boundary_ids.clear();
_shellface_boundary_ids.clear();
_ss_id_to_name.clear();
_ns_id_to_name.clear();
_es_id_to_name.clear();
}
void BoundaryInfo::regenerate_id_sets()
{
const auto old_ss_id_to_name = _ss_id_to_name;
const auto old_ns_id_to_name = _ns_id_to_name;
const auto old_es_id_to_name = _es_id_to_name;
// Clear the old caches
_boundary_ids.clear();
_side_boundary_ids.clear();
_node_boundary_ids.clear();
_edge_boundary_ids.clear();
_shellface_boundary_ids.clear();
_ss_id_to_name.clear();
_ns_id_to_name.clear();
_es_id_to_name.clear();
// Loop over id maps to regenerate each set.
for (const auto & pr : _boundary_node_id)
{
const boundary_id_type id = pr.second;
_boundary_ids.insert(id);
_node_boundary_ids.insert(id);
if (const auto it = old_ns_id_to_name.find(id);
it != old_ns_id_to_name.end())
_ns_id_to_name.emplace(id, it->second);
}
for (const auto & pr : _boundary_edge_id)
{
const boundary_id_type id = pr.second.second;
_boundary_ids.insert(id);
_edge_boundary_ids.insert(id);
if (const auto it = old_es_id_to_name.find(id);
it != old_es_id_to_name.end())
_es_id_to_name.emplace(id, it->second);
}
for (const auto & pr : _boundary_side_id)
{
const boundary_id_type id = pr.second.second;
_boundary_ids.insert(id);
_side_boundary_ids.insert(id);
if (const auto it = old_ss_id_to_name.find(id);
it != old_ss_id_to_name.end())
_ss_id_to_name.emplace(id, it->second);
}
for (const auto & pr : _boundary_shellface_id)
{
const boundary_id_type id = pr.second.second;
_boundary_ids.insert(id);
_shellface_boundary_ids.insert(id);
}
// Handle global data
_global_boundary_ids = _boundary_ids;
libmesh_assert(_mesh);
if (!_mesh->is_serial())
{
_communicator.set_union(_ss_id_to_name);
_communicator.set_union(_ns_id_to_name);
_communicator.set_union(_es_id_to_name);
_communicator.set_union(_global_boundary_ids);
}
}
void BoundaryInfo::sync (UnstructuredMesh & boundary_mesh)
{
std::set<boundary_id_type> request_boundary_ids(_boundary_ids);
request_boundary_ids.insert(invalid_id);
if (!_mesh->is_serial())
this->comm().set_union(request_boundary_ids);
this->sync(request_boundary_ids,
boundary_mesh);
}
void BoundaryInfo::sync (const std::set<boundary_id_type> & requested_boundary_ids,
UnstructuredMesh & boundary_mesh)
{
// Call the 3 argument version of this function with a dummy value for the third set.
std::set<subdomain_id_type> subdomains_relative_to;
subdomains_relative_to.insert(Elem::invalid_subdomain_id);
this->sync(requested_boundary_ids,
boundary_mesh,
subdomains_relative_to);
}
void BoundaryInfo::sync (const std::set<boundary_id_type> & requested_boundary_ids,
UnstructuredMesh & boundary_mesh,
const std::set<subdomain_id_type> & subdomains_relative_to)
{
LOG_SCOPE("sync()", "BoundaryInfo");
boundary_mesh.clear();
/**
* Deleting 0 elements seems weird, but it's better encapsulating
* than exposing a set_is_serial(false) capability that might be
* easily misused.
*/
if (!_mesh->is_serial())
boundary_mesh.delete_remote_elements();
/**
* If the boundary_mesh is still serial, that means we *can't*
* parallelize it, so to make sure we can construct it in full on
* every processor we'll serialize the interior mesh. Use a
* temporary serializer here.
*/
MeshSerializer serializer
(const_cast<MeshBase &>(*_mesh), boundary_mesh.is_serial());
/**
* Re-create the boundary mesh.
*/
boundary_mesh.set_n_partitions() = _mesh->n_partitions();
std::map<dof_id_type, dof_id_type> node_id_map;
std::map<std::pair<dof_id_type, unsigned char>, dof_id_type> side_id_map;
this->_find_id_maps(requested_boundary_ids, 0, &node_id_map, 0, &side_id_map, subdomains_relative_to);
// Let's add all the boundary nodes we found to the boundary mesh
for (const auto & node : _mesh->node_ptr_range())
{
dof_id_type node_id = node->id();
if (node_id_map.count(node_id))
{
boundary_mesh.add_point(*node, node_id_map[node_id], node->processor_id());
// Copy over all the node's boundary IDs to boundary_mesh
std::vector<boundary_id_type> node_boundary_ids;
this->boundary_ids(node, node_boundary_ids);
for (const auto & node_bid : node_boundary_ids)
boundary_mesh.get_boundary_info().add_node(node_id_map[node_id], node_bid);
}
}
// Add the elements. When syncing a boundary mesh, we also store the
// parent side ids in addition to the interior_parent pointers,
// since this information is frequently needed on boundary meshes.
this->add_elements(requested_boundary_ids,
boundary_mesh,
subdomains_relative_to,
/*store_parent_side_ids=*/true);
// The new elements are currently using the interior mesh's nodes;
// we want them to use the boundary mesh's nodes instead.
// This side's Node pointers still point to the nodes of the original mesh.
// We need to re-point them to the boundary mesh's nodes! Since we copied *ALL* of
// the original mesh's nodes over, we should be guaranteed to have the same ordering.
for (auto & new_elem : boundary_mesh.element_ptr_range())
{
for (auto nn : new_elem->node_index_range())
{
// Get the correct node pointer, based on the id()
Node * new_node =
boundary_mesh.node_ptr(node_id_map[new_elem->node_id(nn)]);
// sanity check: be sure that the new Node exists and its
// global id really matches
libmesh_assert (new_node);
libmesh_assert_equal_to (new_node->id(),
node_id_map[new_elem->node_id(nn)]);
// Assign the new node pointer
new_elem->set_node(nn) = new_node;
}
}
// Don't repartition this mesh; we want it to stay in sync with the
// interior partitioning.
boundary_mesh.partitioner().reset(nullptr);
// Make boundary_mesh nodes and elements contiguous
boundary_mesh.prepare_for_use();
// and finally distribute element partitioning to the nodes
Partitioner::set_node_processor_ids(boundary_mesh);
}
void BoundaryInfo::get_side_and_node_maps (UnstructuredMesh & boundary_mesh,
std::map<dof_id_type, dof_id_type> & node_id_map,
std::map<dof_id_type, unsigned char> & side_id_map,
Real tolerance)
{
LOG_SCOPE("get_side_and_node_maps()", "BoundaryInfo");
node_id_map.clear();
side_id_map.clear();
// For building element sides without extraneous allocation
ElemSideBuilder side_builder;
// Pull objects out of the loop to reduce heap operations
const Elem * interior_parent_side;
for (const auto & boundary_elem : boundary_mesh.active_element_ptr_range())
{
const Elem * interior_parent = boundary_elem->interior_parent();
// Find out which side of interior_parent boundary_elem corresponds to.
// Use distance between average vertex location as a way to check.
unsigned char interior_parent_side_index = 0;
bool found_matching_sides = false;
for (auto side : interior_parent->side_index_range())
{
interior_parent_side = &side_builder(*interior_parent, side);
Real va_distance = (boundary_elem->vertex_average() - interior_parent_side->vertex_average()).norm();
if (va_distance < (tolerance * boundary_elem->hmin()))
{
interior_parent_side_index = cast_int<unsigned char>(side);
found_matching_sides = true;
break;
}
}
libmesh_error_msg_if(!found_matching_sides, "No matching side found within the specified tolerance");
side_id_map[boundary_elem->id()] = interior_parent_side_index;
for (auto local_node_index : boundary_elem->node_index_range())
{
dof_id_type boundary_node_id = boundary_elem->node_id(local_node_index);
dof_id_type interior_node_id = interior_parent_side->node_id(local_node_index);
node_id_map[interior_node_id] = boundary_node_id;
}
}
}
void BoundaryInfo::add_elements(const std::set<boundary_id_type> & requested_boundary_ids,
UnstructuredMesh & boundary_mesh,
bool store_parent_side_ids)
{
// Call the 3 argument version of this function with a dummy value for the third arg.
std::set<subdomain_id_type> subdomains_relative_to;
subdomains_relative_to.insert(Elem::invalid_subdomain_id);
this->add_elements(requested_boundary_ids,
boundary_mesh,
subdomains_relative_to,
store_parent_side_ids);
}
void BoundaryInfo::add_elements(const std::set<boundary_id_type> & requested_boundary_ids,
UnstructuredMesh & boundary_mesh,
const std::set<subdomain_id_type> & subdomains_relative_to,
bool store_parent_side_ids)
{
LOG_SCOPE("add_elements()", "BoundaryInfo");
// We're not prepared to mix serial and distributed meshes in this
// method, so make sure they match from the start.
libmesh_assert_equal_to(_mesh->is_serial(),
boundary_mesh.is_serial());
std::map<std::pair<dof_id_type, unsigned char>, dof_id_type> side_id_map;
this->_find_id_maps(requested_boundary_ids,
0,
nullptr,
boundary_mesh.max_elem_id(),
&side_id_map,
subdomains_relative_to);
// We have to add sides *outside* any element loop, because if
// boundary_mesh and _mesh are the same then those additions can
// invalidate our element iterators. So we just use the element
// loop to make a list of sides to add.
typedef std::vector<std::pair<dof_id_type, unsigned char>>
side_container;
side_container sides_to_add;
for (const auto & elem : _mesh->element_ptr_range())
{
// If the subdomains_relative_to container has the
// invalid_subdomain_id, we fall back on the "old" behavior of
// adding sides regardless of this Elem's subdomain. Otherwise,
// if the subdomains_relative_to container doesn't contain the
// current Elem's subdomain_id(), we won't add any sides from
// it.
if (!subdomains_relative_to.count(Elem::invalid_subdomain_id) &&
!subdomains_relative_to.count(elem->subdomain_id()))
continue;
// Get the top-level parent for this element
const Elem * top_parent = elem->top_parent();
// Find all the boundary side ids for this Elem.
auto bounds = _boundary_side_id.equal_range(top_parent);
for (auto s : elem->side_index_range())
{
bool add_this_side = false;
boundary_id_type this_bcid = invalid_id;
for (const auto & pr : as_range(bounds))
{
this_bcid = pr.second.second;
// if this side is flagged with a boundary condition
// and the user wants this id
if ((pr.second.first == s) &&
(requested_boundary_ids.count(this_bcid)))
{
add_this_side = true;
break;
}
}
// We may still want to add this side if the user called
// sync() with no requested_boundary_ids. This corresponds
// to the "old" style of calling sync() in which the entire
// boundary was copied to the BoundaryMesh, and handles the
// case where elements on the geometric boundary are not in
// any sidesets.
if (bounds.first == bounds.second &&
requested_boundary_ids.count(invalid_id) &&
elem->neighbor_ptr(s) == nullptr)
add_this_side = true;
if (add_this_side)
sides_to_add.emplace_back(elem->id(), s);
}
}
#ifdef LIBMESH_ENABLE_UNIQUE_ID
unique_id_type old_max_unique_id = boundary_mesh.parallel_max_unique_id();
#endif
// Add an "extra" integer for storing the side index of the parent
// Elem which each boundary Elem corresponds to. We do this once
// before any Elems have been added.
unsigned int parent_side_index_tag = store_parent_side_ids ?
boundary_mesh.add_elem_integer("parent_side_index") : libMesh::invalid_uint;
for (const auto & [elem_id, s] : sides_to_add)
{
Elem * elem = _mesh->elem_ptr(elem_id);
// Build the side - do not use a "proxy" element here:
// This will be going into the boundary_mesh and needs to
// stand on its own.
std::unique_ptr<Elem> side (elem->build_side_ptr(s, false));
side->processor_id() = elem->processor_id();
const std::pair<dof_id_type, unsigned char> side_pair(elem_id, s);
libmesh_assert(side_id_map.count(side_pair));
const dof_id_type new_side_id = side_id_map[side_pair];
side->set_id(new_side_id);
#ifdef LIBMESH_ENABLE_UNIQUE_ID
side->set_unique_id(old_max_unique_id + new_side_id);
#endif
// Add the side
Elem * new_elem = boundary_mesh.add_elem(std::move(side));
// If requested, new_elem gets an "extra" integer equal to the
// side id "s" of the interior_parent it corresponds to.
if (store_parent_side_ids)
new_elem->set_extra_integer(parent_side_index_tag, s);
#ifdef LIBMESH_ENABLE_AMR
// Set parent links
if (elem->parent())
{
const std::pair<dof_id_type, unsigned char> parent_side_pair(elem->parent()->id(), s);
libmesh_assert(side_id_map.count(parent_side_pair));
Elem * side_parent = boundary_mesh.elem_ptr(side_id_map[parent_side_pair]);
libmesh_assert(side_parent);
new_elem->set_parent(side_parent);
side_parent->set_refinement_flag(Elem::INACTIVE);
// Figuring out which child we are of our parent
// is a trick. Due to libMesh child numbering
// conventions, if we are an element on a vertex,
// then we share that vertex with our parent, with
// the same local index.
bool found_child = false;
for (auto v : make_range(new_elem->n_vertices()))
if (new_elem->node_ptr(v) == side_parent->node_ptr(v))
{
side_parent->add_child(new_elem, v);
found_child = true;
}
// If we don't share any vertex with our parent,
// then we're the fourth child (index 3) of a
// triangle.
if (!found_child)
{
libmesh_assert_equal_to (new_elem->n_vertices(), 3);
side_parent->add_child(new_elem, 3);
}
}
#endif
new_elem->set_interior_parent (elem);
// On non-local elements on DistributedMesh we might have
// RemoteElem neighbor links to construct
if (!_mesh->is_serial() &&
(elem->processor_id() != this->processor_id()))
{
const unsigned short n_nodes = elem->n_nodes();
const unsigned short bdy_n_sides = new_elem->n_sides();
const unsigned short bdy_n_nodes = new_elem->n_nodes();
// Check every interior side for a RemoteElem
for (auto interior_side : elem->side_index_range())
{
// Might this interior side have a RemoteElem that
// needs a corresponding Remote on a boundary side?
if (elem->neighbor_ptr(interior_side) != remote_elem)
continue;
// Which boundary side?
for (unsigned short boundary_side = 0;
boundary_side != bdy_n_sides; ++boundary_side)
{
// Look for matching node points. This is safe in
// *this* context.
bool found_all_nodes = true;
for (unsigned short boundary_node = 0;
boundary_node != bdy_n_nodes; ++boundary_node)
{
if (!new_elem->is_node_on_side(boundary_node,
boundary_side))
continue;
bool found_this_node = false;
for (unsigned short interior_node = 0;
interior_node != n_nodes; ++interior_node)
{
if (!elem->is_node_on_side(interior_node,
interior_side))
continue;
if (new_elem->point(boundary_node) ==
elem->point(interior_node))
{
found_this_node = true;
break;
}
}
if (!found_this_node)
{
found_all_nodes = false;
break;
}
}
if (found_all_nodes)
{
new_elem->set_neighbor
(boundary_side,
const_cast<RemoteElem *>(remote_elem));
break;
}
}
}
}
}
// We haven't been bothering to keep unique ids consistent on ghost
// elements
if (!boundary_mesh.is_serial())
MeshCommunication().make_node_unique_ids_parallel_consistent(boundary_mesh);
// Make sure we didn't add ids inconsistently
#ifdef DEBUG
# ifdef LIBMESH_HAVE_RTTI
DistributedMesh * parmesh = dynamic_cast<DistributedMesh *>(&boundary_mesh);
if (parmesh)
parmesh->libmesh_assert_valid_parallel_ids();
# endif
#endif
}
void BoundaryInfo::add_node(const dof_id_type node_id,
const boundary_id_type id)
{
const Node * node_ptr = _mesh->query_node_ptr(node_id);
// The user could easily ask for an invalid node id, so let's throw
// an easy-to-understand error message when this happens.
libmesh_error_msg_if(!node_ptr,
"BoundaryInfo::add_node(): Could not retrieve pointer for node "
<< node_id
<< ", no boundary id was added.");
this->add_node (node_ptr, id);
}
void BoundaryInfo::add_node(const Node * node,
const boundary_id_type id)
{
libmesh_error_msg_if(id == invalid_id,
"ERROR: You may not set a boundary ID of "
<< invalid_id
<< "\n That is reserved for internal use.");
// Don't add the same ID twice
for (const auto & pr : as_range(_boundary_node_id.equal_range(node)))
if (pr.second == id)
return;
_boundary_node_id.emplace(node, id);
_boundary_ids.insert(id);
_node_boundary_ids.insert(id); // Also add this ID to the set of node boundary IDs
}
void BoundaryInfo::add_node(const Node * node,
const std::vector<boundary_id_type> & ids)
{
if (ids.empty())
return;
libmesh_assert(node);
// Don't add the same ID twice
auto bounds = _boundary_node_id.equal_range(node);
// The entries in the ids vector may be non-unique. If we expected
// *lots* of ids, it might be fastest to construct a std::set from
// the entries, but for a small number of entries, which is more
// typical, it is probably faster to copy the vector and do sort+unique.
// http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector
std::vector<boundary_id_type> unique_ids(ids.begin(), ids.end());
std::sort(unique_ids.begin(), unique_ids.end());
std::vector<boundary_id_type>::iterator new_end =
std::unique(unique_ids.begin(), unique_ids.end());
for (auto & id : as_range(unique_ids.begin(), new_end))
{
libmesh_error_msg_if(id == invalid_id,
"ERROR: You may not set a boundary ID of "
<< invalid_id
<< "\n That is reserved for internal use.");
bool already_inserted = false;
for (const auto & pr : as_range(bounds))
if (pr.second == id)
{
already_inserted = true;
break;
}
if (already_inserted)
continue;
_boundary_node_id.emplace(node, id);
_boundary_ids.insert(id);
_node_boundary_ids.insert(id); // Also add this ID to the set of node boundary IDs
}
}
void BoundaryInfo::clear_boundary_node_ids()
{
_boundary_node_id.clear();
}
void BoundaryInfo::add_edge(const dof_id_type e,
const unsigned short int edge,
const boundary_id_type id)
{
this->add_edge (_mesh->elem_ptr(e), edge, id);
}
void BoundaryInfo::add_edge(const Elem * elem,
const unsigned short int edge,
const boundary_id_type id)
{
libmesh_assert(elem);
// Only add BCs for level-0 elements.
libmesh_assert_equal_to (elem->level(), 0);
// Only add BCs for edges that exist.
libmesh_assert_less (edge, elem->n_edges());
libmesh_error_msg_if(id == invalid_id,
"ERROR: You may not set a boundary ID of "
<< invalid_id
<< "\n That is reserved for internal use.");
// Don't add the same ID twice
for (const auto & pr : as_range(_boundary_edge_id.equal_range(elem)))
if (pr.second.first == edge &&
pr.second.second == id)
return;
_boundary_edge_id.emplace(elem, std::make_pair(edge, id));
_boundary_ids.insert(id);
_edge_boundary_ids.insert(id); // Also add this ID to the set of edge boundary IDs
}
void BoundaryInfo::add_edge(const Elem * elem,
const unsigned short int edge,
const std::vector<boundary_id_type> & ids)
{
if (ids.empty())