-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMooseMesh.C
4335 lines (3680 loc) · 142 KB
/
MooseMesh.C
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
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#include "MooseMesh.h"
#include "Factory.h"
#include "CacheChangedListsThread.h"
#include "MooseUtils.h"
#include "MooseApp.h"
#include "RelationshipManager.h"
#include "PointListAdaptor.h"
#include "Executioner.h"
#include "NonlinearSystemBase.h"
#include "LinearSystem.h"
#include "AuxiliarySystem.h"
#include "Assembly.h"
#include "SubProblem.h"
#include "MooseVariableBase.h"
#include "MooseMeshUtils.h"
#include "MooseAppCoordTransform.h"
#include <utility>
// libMesh
#include "libmesh/bounding_box.h"
#include "libmesh/boundary_info.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/parallel.h"
#include "libmesh/mesh_communication.h"
#include "libmesh/periodic_boundary_base.h"
#include "libmesh/fe_base.h"
#include "libmesh/fe_interface.h"
#include "libmesh/mesh_inserter_iterator.h"
#include "libmesh/mesh_communication.h"
#include "libmesh/mesh_inserter_iterator.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/parallel.h"
#include "libmesh/parallel_elem.h"
#include "libmesh/parallel_node.h"
#include "libmesh/parallel_ghost_sync.h"
#include "libmesh/utility.h"
#include "libmesh/remote_elem.h"
#include "libmesh/linear_partitioner.h"
#include "libmesh/centroid_partitioner.h"
#include "libmesh/parmetis_partitioner.h"
#include "libmesh/hilbert_sfc_partitioner.h"
#include "libmesh/morton_sfc_partitioner.h"
#include "libmesh/edge_edge2.h"
#include "libmesh/mesh_refinement.h"
#include "libmesh/quadrature.h"
#include "libmesh/boundary_info.h"
#include "libmesh/periodic_boundaries.h"
#include "libmesh/quadrature_gauss.h"
#include "libmesh/point_locator_base.h"
#include "libmesh/default_coupling.h"
#include "libmesh/ghost_point_neighbors.h"
#include "libmesh/fe_type.h"
#include "libmesh/enum_to_string.h"
static const int GRAIN_SIZE =
1; // the grain_size does not have much influence on our execution speed
using namespace libMesh;
// Make newer nanoflann API compatible with older nanoflann versions
#if NANOFLANN_VERSION < 0x150
namespace nanoflann
{
typedef SearchParams SearchParameters;
template <typename T, typename U>
using ResultItem = std::pair<T, U>;
}
#endif
InputParameters
MooseMesh::validParams()
{
InputParameters params = MooseObject::validParams();
MooseEnum parallel_type("DEFAULT REPLICATED DISTRIBUTED", "DEFAULT");
params.addParam<MooseEnum>("parallel_type",
parallel_type,
"DEFAULT: Use libMesh::ReplicatedMesh unless --distributed-mesh is "
"specified on the command line "
"REPLICATED: Always use libMesh::ReplicatedMesh "
"DISTRIBUTED: Always use libMesh::DistributedMesh");
params.addParam<bool>(
"allow_renumbering",
true,
"If allow_renumbering=false, node and element numbers are kept fixed until deletion");
params.addParam<bool>("nemesis",
false,
"If nemesis=true and file=foo.e, actually reads "
"foo.e.N.0, foo.e.N.1, ... foo.e.N.N-1, "
"where N = # CPUs, with NemesisIO.");
MooseEnum dims("1=1 2 3", "1");
params.addParam<MooseEnum>("dim",
dims,
"This is only required for certain mesh formats where "
"the dimension of the mesh cannot be autodetected. "
"In particular you must supply this for GMSH meshes. "
"Note: This is completely ignored for ExodusII meshes!");
params.addParam<MooseEnum>(
"partitioner",
partitioning(),
"Specifies a mesh partitioner to use when splitting the mesh for a parallel computation.");
MooseEnum direction("x y z radial");
params.addParam<MooseEnum>("centroid_partitioner_direction",
direction,
"Specifies the sort direction if using the centroid partitioner. "
"Available options: x, y, z, radial");
MooseEnum patch_update_strategy("never always auto iteration", "never");
params.addParam<MooseEnum>(
"patch_update_strategy",
patch_update_strategy,
"How often to update the geometric search 'patch'. The default is to "
"never update it (which is the most efficient but could be a problem "
"with lots of relative motion). 'always' will update the patch for all "
"secondary nodes at the beginning of every timestep which might be time "
"consuming. 'auto' will attempt to determine at the start of which "
"timesteps the patch for all secondary nodes needs to be updated automatically."
"'iteration' updates the patch at every nonlinear iteration for a "
"subset of secondary nodes for which penetration is not detected. If there "
"can be substantial relative motion between the primary and secondary surfaces "
"during the nonlinear iterations within a timestep, it is advisable to use "
"'iteration' option to ensure accurate contact detection.");
// Note: This parameter is named to match 'construct_side_list_from_node_list' in SetupMeshAction
params.addParam<bool>(
"construct_node_list_from_side_list",
true,
"Whether or not to generate nodesets from the sidesets (usually a good idea).");
params.addParam<unsigned int>(
"patch_size", 40, "The number of nodes to consider in the NearestNode neighborhood.");
params.addParam<unsigned int>("ghosting_patch_size",
"The number of nearest neighbors considered "
"for ghosting purposes when 'iteration' "
"patch update strategy is used. Default is "
"5 * patch_size.");
params.addParam<unsigned int>("max_leaf_size",
10,
"The maximum number of points in each leaf of the KDTree used in "
"the nearest neighbor search. As the leaf size becomes larger,"
"KDTree construction becomes faster but the nearest neighbor search"
"becomes slower.");
params.addParam<bool>("build_all_side_lowerd_mesh",
false,
"True to build the lower-dimensional mesh for all sides.");
params.addParam<bool>("skip_refine_when_use_split",
true,
"True to skip uniform refinements when using a pre-split mesh.");
params.addParam<std::vector<SubdomainID>>(
"add_subdomain_ids",
"The listed subdomain ids will be assumed valid for the mesh. This permits setting up "
"subdomain restrictions for subdomains initially containing no elements, which can occur, "
"for example, in additive manufacturing simulations which dynamically add and remove "
"elements. Names for this subdomains may be provided using add_subdomain_names. In this case "
"this list and add_subdomain_names must contain the same number of items.");
params.addParam<std::vector<SubdomainName>>(
"add_subdomain_names",
"The listed subdomain names will be assumed valid for the mesh. This permits setting up "
"subdomain restrictions for subdomains initially containing no elements, which can occur, "
"for example, in additive manufacturing simulations which dynamically add and remove "
"elements. IDs for this subdomains may be provided using add_subdomain_ids. Otherwise IDs "
"are automatically assigned. In case add_subdomain_ids is set too, both lists must contain "
"the same number of items.");
params.addParam<std::vector<BoundaryID>>(
"add_sideset_ids",
"The listed sideset ids will be assumed valid for the mesh. This permits setting up boundary "
"restrictions for sidesets initially containing no sides. Names for this sidesets may be "
"provided using add_sideset_names. In this case this list and add_sideset_names must contain "
"the same number of items.");
params.addParam<std::vector<BoundaryName>>(
"add_sideset_names",
"The listed sideset names will be assumed valid for the mesh. This permits setting up "
"boundary restrictions for sidesets initially containing no sides. Ids for this sidesets may "
"be provided using add_sideset_ids. In this case this list and add_sideset_ids must contain "
"the same number of items.");
params += MooseAppCoordTransform::validParams();
// This indicates that the derived mesh type accepts a MeshGenerator, and should be set to true in
// derived types that do so.
params.addPrivateParam<bool>("_mesh_generator_mesh", false);
// Whether or not the mesh is pre split
params.addPrivateParam<bool>("_is_split", false);
params.registerBase("MooseMesh");
// groups
params.addParamNamesToGroup(
"dim nemesis patch_update_strategy construct_node_list_from_side_list patch_size",
"Advanced");
params.addParamNamesToGroup("partitioner centroid_partitioner_direction", "Partitioning");
return params;
}
MooseMesh::MooseMesh(const InputParameters & parameters)
: MooseObject(parameters),
Restartable(this, "Mesh"),
PerfGraphInterface(this),
_parallel_type(getParam<MooseEnum>("parallel_type").getEnum<MooseMesh::ParallelType>()),
_use_distributed_mesh(false),
_distribution_overridden(false),
_parallel_type_overridden(false),
_mesh(nullptr),
_partitioner_name(getParam<MooseEnum>("partitioner")),
_partitioner_overridden(false),
_custom_partitioner_requested(false),
_uniform_refine_level(0),
_skip_refine_when_use_split(getParam<bool>("skip_refine_when_use_split")),
_skip_deletion_repartition_after_refine(false),
_is_nemesis(getParam<bool>("nemesis")),
_node_to_elem_map_built(false),
_node_to_active_semilocal_elem_map_built(false),
_patch_size(getParam<unsigned int>("patch_size")),
_ghosting_patch_size(isParamValid("ghosting_patch_size")
? getParam<unsigned int>("ghosting_patch_size")
: 5 * _patch_size),
_max_leaf_size(getParam<unsigned int>("max_leaf_size")),
_patch_update_strategy(
getParam<MooseEnum>("patch_update_strategy").getEnum<Moose::PatchUpdateType>()),
_regular_orthogonal_mesh(false),
_is_split(getParam<bool>("_is_split")),
_has_lower_d(false),
_allow_recovery(true),
_construct_node_list_from_side_list(getParam<bool>("construct_node_list_from_side_list")),
_need_delete(false),
_allow_remote_element_removal(true),
_need_ghost_ghosted_boundaries(true),
_is_displaced(false),
_coord_sys(
declareRestartableData<std::map<SubdomainID, Moose::CoordinateSystemType>>("coord_sys")),
_rz_coord_axis(getParam<MooseEnum>("rz_coord_axis")),
_coord_system_set(false),
_doing_p_refinement(false)
{
if (isParamValid("ghosting_patch_size") && (_patch_update_strategy != Moose::Iteration))
mooseError("Ghosting patch size parameter has to be set in the mesh block "
"only when 'iteration' patch update strategy is used.");
if (isParamValid("coord_block"))
{
if (isParamValid("block"))
paramWarning("block",
"You set both 'Mesh/block' and 'Mesh/coord_block'. The value of "
"'Mesh/coord_block' will be used.");
_provided_coord_blocks = getParam<std::vector<SubdomainName>>("coord_block");
}
else if (isParamValid("block"))
_provided_coord_blocks = getParam<std::vector<SubdomainName>>("block");
if (getParam<bool>("build_all_side_lowerd_mesh"))
// Do not initially allow removal of remote elements
allowRemoteElementRemoval(false);
determineUseDistributedMesh();
}
MooseMesh::MooseMesh(const MooseMesh & other_mesh)
: MooseObject(other_mesh._pars),
Restartable(this, "Mesh"),
PerfGraphInterface(this, "CopiedMesh"),
_built_from_other_mesh(true),
_parallel_type(other_mesh._parallel_type),
_use_distributed_mesh(other_mesh._use_distributed_mesh),
_distribution_overridden(other_mesh._distribution_overridden),
_parallel_type_overridden(other_mesh._parallel_type_overridden),
_mesh(other_mesh.getMesh().clone()),
_partitioner_name(other_mesh._partitioner_name),
_partitioner_overridden(other_mesh._partitioner_overridden),
_custom_partitioner_requested(other_mesh._custom_partitioner_requested),
_uniform_refine_level(other_mesh.uniformRefineLevel()),
_skip_refine_when_use_split(other_mesh._skip_refine_when_use_split),
_skip_deletion_repartition_after_refine(other_mesh._skip_deletion_repartition_after_refine),
_is_nemesis(false),
_node_to_elem_map_built(false),
_node_to_active_semilocal_elem_map_built(false),
_patch_size(other_mesh._patch_size),
_ghosting_patch_size(other_mesh._ghosting_patch_size),
_max_leaf_size(other_mesh._max_leaf_size),
_patch_update_strategy(other_mesh._patch_update_strategy),
_regular_orthogonal_mesh(false),
_is_split(other_mesh._is_split),
_lower_d_interior_blocks(other_mesh._lower_d_interior_blocks),
_lower_d_boundary_blocks(other_mesh._lower_d_boundary_blocks),
_has_lower_d(other_mesh._has_lower_d),
_allow_recovery(other_mesh._allow_recovery),
_construct_node_list_from_side_list(other_mesh._construct_node_list_from_side_list),
_need_delete(other_mesh._need_delete),
_allow_remote_element_removal(other_mesh._allow_remote_element_removal),
_need_ghost_ghosted_boundaries(other_mesh._need_ghost_ghosted_boundaries),
_coord_sys(other_mesh._coord_sys),
_rz_coord_axis(other_mesh._rz_coord_axis),
_subdomain_id_to_rz_coord_axis(other_mesh._subdomain_id_to_rz_coord_axis),
_coord_system_set(other_mesh._coord_system_set),
_provided_coord_blocks(other_mesh._provided_coord_blocks),
_doing_p_refinement(other_mesh._doing_p_refinement)
{
// Note: this calls BoundaryInfo::operator= without changing the
// ownership semantics of either Mesh's BoundaryInfo object.
getMesh().get_boundary_info() = other_mesh.getMesh().get_boundary_info();
const std::set<SubdomainID> & subdomains = other_mesh.meshSubdomains();
for (const auto & sbd_id : subdomains)
setSubdomainName(sbd_id, other_mesh.getMesh().subdomain_name(sbd_id));
// Get references to BoundaryInfo objects to make the code below cleaner...
const BoundaryInfo & other_boundary_info = other_mesh.getMesh().get_boundary_info();
BoundaryInfo & boundary_info = getMesh().get_boundary_info();
// Use the other BoundaryInfo object to build the list of side boundary ids
std::vector<BoundaryID> side_boundaries;
other_boundary_info.build_side_boundary_ids(side_boundaries);
// Assign those boundary ids in our BoundaryInfo object
for (const auto & side_bnd_id : side_boundaries)
boundary_info.sideset_name(side_bnd_id) = other_boundary_info.get_sideset_name(side_bnd_id);
// Do the same thing for node boundary ids
std::vector<BoundaryID> node_boundaries;
other_boundary_info.build_node_boundary_ids(node_boundaries);
for (const auto & node_bnd_id : node_boundaries)
boundary_info.nodeset_name(node_bnd_id) = other_boundary_info.get_nodeset_name(node_bnd_id);
_bounds.resize(other_mesh._bounds.size());
for (std::size_t i = 0; i < _bounds.size(); ++i)
{
_bounds[i].resize(other_mesh._bounds[i].size());
for (std::size_t j = 0; j < _bounds[i].size(); ++j)
_bounds[i][j] = other_mesh._bounds[i][j];
}
updateCoordTransform();
}
MooseMesh::~MooseMesh()
{
freeBndNodes();
freeBndElems();
clearQuadratureNodes();
}
void
MooseMesh::freeBndNodes()
{
// free memory
for (auto & bnode : _bnd_nodes)
delete bnode;
for (auto & it : _node_set_nodes)
it.second.clear();
_node_set_nodes.clear();
for (auto & it : _bnd_node_ids)
it.second.clear();
_bnd_node_ids.clear();
}
void
MooseMesh::freeBndElems()
{
// free memory
for (auto & belem : _bnd_elems)
delete belem;
for (auto & it : _bnd_elem_ids)
it.second.clear();
_bnd_elem_ids.clear();
}
bool
MooseMesh::prepare(const MeshBase * const mesh_to_clone)
{
TIME_SECTION("prepare", 2, "Preparing Mesh", true);
bool called_prepare_for_use = false;
mooseAssert(_mesh, "The MeshBase has not been constructed");
if (!dynamic_cast<DistributedMesh *>(&getMesh()) || _is_nemesis)
// For whatever reason we do not want to allow renumbering here nor ever in the future?
getMesh().allow_renumbering(false);
if (mesh_to_clone)
{
mooseAssert(mesh_to_clone->is_prepared(),
"The mesh we wish to clone from must already be prepared");
_mesh = mesh_to_clone->clone();
_moose_mesh_prepared = false;
}
else if (!_mesh->is_prepared())
{
_mesh->prepare_for_use();
_moose_mesh_prepared = false;
called_prepare_for_use = true;
}
if (_moose_mesh_prepared)
return called_prepare_for_use;
// Collect (local) subdomain IDs
_mesh_subdomains.clear();
for (const auto & elem : getMesh().element_ptr_range())
_mesh_subdomains.insert(elem->subdomain_id());
// add explicitly requested subdomains
if (isParamValid("add_subdomain_ids") && !isParamValid("add_subdomain_names"))
{
// only subdomain ids are explicitly given
const auto & add_subdomain_id = getParam<std::vector<SubdomainID>>("add_subdomain_ids");
_mesh_subdomains.insert(add_subdomain_id.begin(), add_subdomain_id.end());
}
else if (isParamValid("add_subdomain_ids") && isParamValid("add_subdomain_names"))
{
const auto add_subdomain =
getParam<SubdomainID, SubdomainName>("add_subdomain_ids", "add_subdomain_names");
for (const auto & [sub_id, sub_name] : add_subdomain)
{
// add subdomain id
_mesh_subdomains.insert(sub_id);
// set name of the subdomain just added
setSubdomainName(sub_id, sub_name);
}
}
else if (isParamValid("add_subdomain_names"))
{
// the user has defined add_subdomain_names, but not add_subdomain_ids
const auto & add_subdomain_names = getParam<std::vector<SubdomainName>>("add_subdomain_names");
// to define subdomain ids, we need the largest subdomain id defined yet.
subdomain_id_type offset = 0;
if (!_mesh_subdomains.empty())
offset = *_mesh_subdomains.rbegin();
// add all subdomains (and auto-assign ids)
for (const SubdomainName & sub_name : add_subdomain_names)
{
// to avoid two subdomains with the same ID (notably on recover)
if (getSubdomainID(sub_name) != libMesh::Elem::invalid_subdomain_id)
continue;
const auto sub_id = ++offset;
// add subdomain id
_mesh_subdomains.insert(sub_id);
// set name of the subdomain just added
setSubdomainName(sub_id, sub_name);
}
}
// Make sure nodesets have been generated
buildNodeListFromSideList();
// Collect (local) boundary IDs
const std::set<BoundaryID> & local_bids = getMesh().get_boundary_info().get_boundary_ids();
_mesh_boundary_ids.insert(local_bids.begin(), local_bids.end());
const std::set<BoundaryID> & local_node_bids =
getMesh().get_boundary_info().get_node_boundary_ids();
_mesh_nodeset_ids.insert(local_node_bids.begin(), local_node_bids.end());
const std::set<BoundaryID> & local_side_bids =
getMesh().get_boundary_info().get_side_boundary_ids();
_mesh_sideset_ids.insert(local_side_bids.begin(), local_side_bids.end());
// Add explicitly requested sidesets
// This is done *after* the side boundaries (e.g. "right", ...) have been generated.
if (isParamValid("add_sideset_ids") && !isParamValid("add_sideset_names"))
{
const auto & add_sideset_ids = getParam<std::vector<BoundaryID>>("add_sideset_ids");
_mesh_boundary_ids.insert(add_sideset_ids.begin(), add_sideset_ids.end());
_mesh_sideset_ids.insert(add_sideset_ids.begin(), add_sideset_ids.end());
}
else if (isParamValid("add_sideset_ids") && isParamValid("add_sideset_names"))
{
const auto add_sidesets =
getParam<BoundaryID, BoundaryName>("add_sideset_ids", "add_sideset_names");
for (const auto & [sideset_id, sideset_name] : add_sidesets)
{
// add sideset id
_mesh_boundary_ids.insert(sideset_id);
_mesh_sideset_ids.insert(sideset_id);
// set name of the sideset just added
setBoundaryName(sideset_id, sideset_name);
}
}
else if (isParamValid("add_sideset_names"))
{
// the user has defined add_sideset_names, but not add_sideset_ids
const auto & add_sideset_names = getParam<std::vector<BoundaryName>>("add_sideset_names");
// to define sideset ids, we need the largest sideset id defined yet.
boundary_id_type offset = 0;
if (!_mesh_sideset_ids.empty())
offset = *_mesh_sideset_ids.rbegin();
if (!_mesh_boundary_ids.empty())
offset = std::max(offset, *_mesh_boundary_ids.rbegin());
// add all sidesets (and auto-assign ids)
for (const BoundaryName & sideset_name : add_sideset_names)
{
// to avoid two sidesets with the same ID (notably on recover)
if (getBoundaryID(sideset_name) != Moose::INVALID_BOUNDARY_ID)
continue;
const auto sideset_id = ++offset;
// add sideset id
_mesh_boundary_ids.insert(sideset_id);
_mesh_sideset_ids.insert(sideset_id);
// set name of the sideset just added
setBoundaryName(sideset_id, sideset_name);
}
}
// Communicate subdomain and boundary IDs if this is a parallel mesh
if (!getMesh().is_serial())
{
_communicator.set_union(_mesh_subdomains);
_communicator.set_union(_mesh_boundary_ids);
_communicator.set_union(_mesh_nodeset_ids);
_communicator.set_union(_mesh_sideset_ids);
}
if (!_built_from_other_mesh)
{
if (!_coord_system_set)
setCoordSystem(_provided_coord_blocks, getParam<MultiMooseEnum>("coord_type"));
else if (_pars.isParamSetByUser("coord_type"))
mooseError(
"Trying to set coordinate system type information based on the user input file, but "
"the coordinate system type information has already been set programmatically! "
"Either remove your coordinate system type information from the input file, or contact "
"your application developer");
}
// Set general axisymmetric axes if provided
if (isParamValid("rz_coord_blocks") && isParamValid("rz_coord_origins") &&
isParamValid("rz_coord_directions"))
{
const auto rz_coord_blocks = getParam<std::vector<SubdomainName>>("rz_coord_blocks");
const auto rz_coord_origins = getParam<std::vector<Point>>("rz_coord_origins");
const auto rz_coord_directions = getParam<std::vector<RealVectorValue>>("rz_coord_directions");
if (rz_coord_origins.size() == rz_coord_blocks.size() &&
rz_coord_directions.size() == rz_coord_blocks.size())
{
std::vector<std::pair<Point, RealVectorValue>> rz_coord_axes;
for (unsigned int i = 0; i < rz_coord_origins.size(); ++i)
rz_coord_axes.push_back(std::make_pair(rz_coord_origins[i], rz_coord_directions[i]));
setGeneralAxisymmetricCoordAxes(rz_coord_blocks, rz_coord_axes);
if (isParamSetByUser("rz_coord_axis"))
mooseError("The parameter 'rz_coord_axis' may not be provided if 'rz_coord_blocks', "
"'rz_coord_origins', and 'rz_coord_directions' are provided.");
}
else
mooseError("The parameters 'rz_coord_blocks', 'rz_coord_origins', and "
"'rz_coord_directions' must all have the same size.");
}
else if (isParamValid("rz_coord_blocks") || isParamValid("rz_coord_origins") ||
isParamValid("rz_coord_directions"))
mooseError("If any of the parameters 'rz_coord_blocks', 'rz_coord_origins', and "
"'rz_coord_directions' are provided, then all must be provided.");
detectOrthogonalDimRanges();
update();
// Check if there is subdomain name duplication for the same subdomain ID
checkDuplicateSubdomainNames();
_moose_mesh_prepared = true;
return called_prepare_for_use;
}
void
MooseMesh::update()
{
TIME_SECTION("update", 3, "Updating Mesh", true);
// Rebuild the boundary conditions
buildNodeListFromSideList();
// Update the node to elem map
_node_to_elem_map.clear();
_node_to_elem_map_built = false;
_node_to_active_semilocal_elem_map.clear();
_node_to_active_semilocal_elem_map_built = false;
buildNodeList();
buildBndElemList();
cacheInfo();
buildElemIDInfo();
_finite_volume_info_dirty = true;
}
void
MooseMesh::buildLowerDMesh()
{
auto & mesh = getMesh();
if (!mesh.is_serial())
mooseError(
"Hybrid finite element method must use replicated mesh.\nCurrently lower-dimensional mesh "
"does not support mesh re-partitioning and a debug assertion being hit related with "
"neighbors of lower-dimensional element, with distributed mesh.");
// Lower-D element build requires neighboring element information
if (!mesh.is_prepared())
mesh.find_neighbors();
// maximum number of sides of all elements
unsigned int max_n_sides = 0;
// remove existing lower-d element first
std::set<Elem *> deleteable_elems;
for (auto & elem : mesh.element_ptr_range())
if (_lower_d_interior_blocks.count(elem->subdomain_id()) ||
_lower_d_boundary_blocks.count(elem->subdomain_id()))
deleteable_elems.insert(elem);
else if (elem->n_sides() > max_n_sides)
max_n_sides = elem->n_sides();
for (auto & elem : deleteable_elems)
mesh.delete_elem(elem);
for (const auto & id : _lower_d_interior_blocks)
_mesh_subdomains.erase(id);
for (const auto & id : _lower_d_boundary_blocks)
_mesh_subdomains.erase(id);
_lower_d_interior_blocks.clear();
_lower_d_boundary_blocks.clear();
mesh.comm().max(max_n_sides);
deleteable_elems.clear();
// get all side types
std::set<int> interior_side_types;
std::set<int> boundary_side_types;
for (const auto & elem : mesh.active_element_ptr_range())
for (const auto side : elem->side_index_range())
{
Elem * neig = elem->neighbor_ptr(side);
std::unique_ptr<Elem> side_elem(elem->build_side_ptr(side));
if (neig)
interior_side_types.insert(side_elem->type());
else
boundary_side_types.insert(side_elem->type());
}
mesh.comm().set_union(interior_side_types);
mesh.comm().set_union(boundary_side_types);
// assign block ids for different side types
std::map<ElemType, SubdomainID> interior_block_ids;
std::map<ElemType, SubdomainID> boundary_block_ids;
// we assume this id is not used by the mesh
auto id = libMesh::Elem::invalid_subdomain_id - 2;
for (const auto & tpid : interior_side_types)
{
const auto type = ElemType(tpid);
mesh.subdomain_name(id) = "INTERNAL_SIDE_LOWERD_SUBDOMAIN_" + Utility::enum_to_string(type);
interior_block_ids[type] = id;
_lower_d_interior_blocks.insert(id);
if (_mesh_subdomains.count(id) > 0)
mooseError("Trying to add a mesh block with id ", id, " that has existed in the mesh");
_mesh_subdomains.insert(id);
--id;
}
for (const auto & tpid : boundary_side_types)
{
const auto type = ElemType(tpid);
mesh.subdomain_name(id) = "BOUNDARY_SIDE_LOWERD_SUBDOMAIN_" + Utility::enum_to_string(type);
boundary_block_ids[type] = id;
_lower_d_boundary_blocks.insert(id);
if (_mesh_subdomains.count(id) > 0)
mooseError("Trying to add a mesh block with id ", id, " that has existed in the mesh");
_mesh_subdomains.insert(id);
--id;
}
dof_id_type max_elem_id = mesh.max_elem_id();
unique_id_type max_unique_id = mesh.parallel_max_unique_id();
std::vector<Elem *> side_elems;
_higher_d_elem_side_to_lower_d_elem.clear();
for (const auto & elem : mesh.active_element_ptr_range())
{
// skip existing lower-d elements
if (elem->interior_parent())
continue;
for (const auto side : elem->side_index_range())
{
Elem * neig = elem->neighbor_ptr(side);
bool build_side = false;
if (!neig)
build_side = true;
else
{
mooseAssert(!neig->is_remote(), "We error if the mesh is not serial");
if (!neig->active())
build_side = true;
else if (neig->level() == elem->level() && elem->id() < neig->id())
build_side = true;
}
if (build_side)
{
std::unique_ptr<Elem> side_elem(elem->build_side_ptr(side, false));
// The side will be added with the same processor id as the parent.
side_elem->processor_id() = elem->processor_id();
// Add subdomain ID
if (neig)
side_elem->subdomain_id() = interior_block_ids.at(side_elem->type());
else
side_elem->subdomain_id() = boundary_block_ids.at(side_elem->type());
// set ids consistently across processors (these ids will be temporary)
side_elem->set_id(max_elem_id + elem->id() * max_n_sides + side);
side_elem->set_unique_id(max_unique_id + elem->id() * max_n_sides + side);
// Also assign the side's interior parent, so it is always
// easy to figure out the Elem we came from.
// Note: the interior parent could be a ghost element.
side_elem->set_interior_parent(elem);
side_elems.push_back(side_elem.release());
// add link between higher d element to lower d element
auto pair = std::make_pair(elem, side);
auto link = std::make_pair(pair, side_elems.back());
auto ilink = std::make_pair(side_elems.back(), side);
_lower_d_elem_to_higher_d_elem_side.insert(ilink);
_higher_d_elem_side_to_lower_d_elem.insert(link);
}
}
}
// finally, add the lower-dimensional element to the mesh
// Note: lower-d interior element will exist on a processor if its associated interior
// parent exists on a processor whether or not being a ghost. Lower-d elements will
// get its interior parent's processor id.
for (auto & elem : side_elems)
mesh.add_elem(elem);
// we do all the stuff in prepare_for_use such as renumber_nodes_and_elements(),
// update_parallel_id_counts(), cache_elem_dims(), etc. except partitioning here.
const bool skip_partitioning_old = mesh.skip_partitioning();
mesh.skip_partitioning(true);
// Finding neighbors is ambiguous for lower-dimensional elements on interior faces
mesh.allow_find_neighbors(false);
mesh.prepare_for_use();
mesh.skip_partitioning(skip_partitioning_old);
}
const Node &
MooseMesh::node(const dof_id_type i) const
{
mooseDeprecated("MooseMesh::node() is deprecated, please use MooseMesh::nodeRef() instead");
return nodeRef(i);
}
Node &
MooseMesh::node(const dof_id_type i)
{
mooseDeprecated("MooseMesh::node() is deprecated, please use MooseMesh::nodeRef() instead");
return nodeRef(i);
}
const Node &
MooseMesh::nodeRef(const dof_id_type i) const
{
const auto node_ptr = queryNodePtr(i);
mooseAssert(node_ptr, "Missing node");
return *node_ptr;
}
Node &
MooseMesh::nodeRef(const dof_id_type i)
{
return const_cast<Node &>(const_cast<const MooseMesh *>(this)->nodeRef(i));
}
const Node *
MooseMesh::nodePtr(const dof_id_type i) const
{
return &nodeRef(i);
}
Node *
MooseMesh::nodePtr(const dof_id_type i)
{
return &nodeRef(i);
}
const Node *
MooseMesh::queryNodePtr(const dof_id_type i) const
{
if (i > getMesh().max_node_id())
{
auto it = _quadrature_nodes.find(i);
if (it == _quadrature_nodes.end())
return nullptr;
auto & node_ptr = it->second;
mooseAssert(node_ptr, "Uninitialized quadrature node");
return node_ptr;
}
return getMesh().query_node_ptr(i);
}
Node *
MooseMesh::queryNodePtr(const dof_id_type i)
{
return const_cast<Node *>(const_cast<const MooseMesh *>(this)->queryNodePtr(i));
}
void
MooseMesh::meshChanged()
{
TIME_SECTION("meshChanged", 3, "Updating Because Mesh Changed");
update();
// Delete all of the cached ranges
_active_local_elem_range.reset();
_active_node_range.reset();
_active_semilocal_node_range.reset();
_local_node_range.reset();
_bnd_node_range.reset();
_bnd_elem_range.reset();
// Rebuild the ranges
getActiveLocalElementRange();
getActiveNodeRange();
getLocalNodeRange();
getBoundaryNodeRange();
getBoundaryElementRange();
// Call the callback function onMeshChanged
onMeshChanged();
}
void
MooseMesh::onMeshChanged()
{
}
void
MooseMesh::cacheChangedLists()
{
TIME_SECTION("cacheChangedLists", 5, "Caching Changed Lists");
ConstElemRange elem_range(getMesh().local_elements_begin(), getMesh().local_elements_end(), 1);
CacheChangedListsThread cclt(*this);
Threads::parallel_reduce(elem_range, cclt);
_coarsened_element_children.clear();
_refined_elements = std::make_unique<ConstElemPointerRange>(cclt._refined_elements.begin(),
cclt._refined_elements.end());
_coarsened_elements = std::make_unique<ConstElemPointerRange>(cclt._coarsened_elements.begin(),
cclt._coarsened_elements.end());
_coarsened_element_children = cclt._coarsened_element_children;
}
ConstElemPointerRange *
MooseMesh::refinedElementRange() const
{
return _refined_elements.get();
}
ConstElemPointerRange *
MooseMesh::coarsenedElementRange() const
{
return _coarsened_elements.get();
}
const std::vector<const Elem *> &
MooseMesh::coarsenedElementChildren(const Elem * elem) const
{
auto elem_to_child_pair = _coarsened_element_children.find(elem);
mooseAssert(elem_to_child_pair != _coarsened_element_children.end(), "Missing element in map");
return elem_to_child_pair->second;
}
void
MooseMesh::updateActiveSemiLocalNodeRange(std::set<dof_id_type> & ghosted_elems)
{
TIME_SECTION("updateActiveSemiLocalNodeRange", 5, "Updating ActiveSemiLocalNode Range");
_semilocal_node_list.clear();
// First add the nodes connected to local elems
ConstElemRange * active_local_elems = getActiveLocalElementRange();
for (const auto & elem : *active_local_elems)
{
for (unsigned int n = 0; n < elem->n_nodes(); ++n)
{
// Since elem is const here but we require a non-const Node * to
// store in the _semilocal_node_list (otherwise things like
// UpdateDisplacedMeshThread don't work), we are using a
// const_cast. A more long-term fix would be to have
// getActiveLocalElementRange return a non-const ElemRange.
Node * node = const_cast<Node *>(elem->node_ptr(n));
_semilocal_node_list.insert(node);
}
}
// Now add the nodes connected to ghosted_elems
for (const auto & ghost_elem_id : ghosted_elems)
{
Elem * elem = getMesh().elem_ptr(ghost_elem_id);
for (unsigned int n = 0; n < elem->n_nodes(); n++)
{
Node * node = elem->node_ptr(n);
_semilocal_node_list.insert(node);
}
}
// Now create the actual range
_active_semilocal_node_range = std::make_unique<SemiLocalNodeRange>(_semilocal_node_list.begin(),
_semilocal_node_list.end());
}
bool
MooseMesh::isSemiLocal(Node * const node) const
{
return _semilocal_node_list.find(node) != _semilocal_node_list.end();
}
/**
* Helper class for sorting Boundary Nodes so that we always get the same
* order of application for boundary conditions.
*/
class BndNodeCompare
{
public:
BndNodeCompare() {}
bool operator()(const BndNode * const & lhs, const BndNode * const & rhs)
{
if (lhs->_bnd_id < rhs->_bnd_id)
return true;
if (lhs->_bnd_id > rhs->_bnd_id)
return false;
if (lhs->_node->id() < rhs->_node->id())
return true;
if (lhs->_node->id() > rhs->_node->id())
return false;
return false;
}
};
void
MooseMesh::buildNodeList()
{
TIME_SECTION("buildNodeList", 5, "Building Node List");
freeBndNodes();
auto bc_tuples = getMesh().get_boundary_info().build_node_list();
int n = bc_tuples.size();
_bnd_nodes.clear();
_bnd_nodes.reserve(n);
for (const auto & t : bc_tuples)
{
auto node_id = std::get<0>(t);