/
MooseMesh.C
1750 lines (1457 loc) · 51.2 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
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/
#include "MooseMesh.h"
#include "Factory.h"
#include "NonlinearSystem.h"
#include "CacheChangedListsThread.h"
#include "Assembly.h"
#include "MooseUtils.h"
#include "MooseApp.h"
// libMesh
#include "libmesh/boundary_info.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/parallel.h"
#include "libmesh/mesh_communication.h"
#include "libmesh/parallel_mesh.h"
#include "libmesh/periodic_boundary_base.h"
#include "libmesh/fe_interface.h"
#include "libmesh/serial_mesh.h"
static const int GRAIN_SIZE = 1; // the grain_size does not have much influence on our execution speed
template<>
InputParameters validParams<MooseMesh>()
{
InputParameters params = validParams<MooseObject>();
MooseEnum mesh_distribution_type("PARALLEL=0, SERIAL, DEFAULT", "DEFAULT");
params.addParam<MooseEnum>("distribution", mesh_distribution_type,
"PARALLEL: Always use libMesh::ParallelMesh "
"SERIAL: Always use libMesh::SerialMesh "
"DEFAULT: Use libMesh::SerialMesh unless --parallel-mesh is specified on the command line");
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", "3");
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.addPrivateParam<std::string>("built_by_action", "setup_mesh");
// groups
params.addParamNamesToGroup("dim", "Advanced");
params.addParamNamesToGroup("nemesis", "Advanced");
return params;
}
MooseMesh::MooseMesh(const std::string & name, InputParameters parameters) :
MooseObject(name, parameters),
_mesh_distribution_type(getParam<MooseEnum>("distribution")),
_use_parallel_mesh(false),
_mesh(NULL),
_uniform_refine_level(0),
_is_changed(false),
_is_nemesis(getParam<bool>("nemesis")),
_is_prepared(false),
_refined_elements(NULL),
_coarsened_elements(NULL),
_active_local_elem_range(NULL),
_active_semilocal_node_range(NULL),
_active_node_range(NULL),
_local_node_range(NULL),
_bnd_node_range(NULL),
_bnd_elem_range(NULL),
_node_to_elem_map_built(false),
_patch_size(40),
_regular_orthogonal_mesh(false)
{
switch (_mesh_distribution_type)
{
case 0: // PARALLEL
_use_parallel_mesh = true;
break;
case 1: // SERIAL
break;
case 2: // DEFAULT
{
// The user did not specify 'distribution = XYZ' in the input file,
// so we allow the --parallel-mesh command line arg to possibly turn
// on ParallelMesh. If the command line arg is not present, we pick SerialMesh.
if (_app.getParallelMeshOnCommandLine())
_use_parallel_mesh = true;
break;
}
default:
mooseError("Unknown mesh distribution type!");
}
// If the user specifies 'nemesis = true' in the Mesh block, we
// must use ParallelMesh.
if (_is_nemesis)
_use_parallel_mesh = true;
unsigned dim = getParam<MooseEnum>("dim");
if (_use_parallel_mesh)
_mesh = new ParallelMesh(dim);
else
_mesh = new SerialMesh(dim);
}
MooseMesh::MooseMesh(const MooseMesh & other_mesh) :
MooseObject(other_mesh._name, other_mesh._pars),
_mesh_distribution_type(other_mesh._mesh_distribution_type),
_use_parallel_mesh(other_mesh._use_parallel_mesh),
_mesh(other_mesh.getMesh().clone().release()),
_uniform_refine_level(0),
_is_changed(false),
_is_nemesis(false),
_is_prepared(false),
_refined_elements(NULL),
_coarsened_elements(NULL),
_active_local_elem_range(NULL),
_active_semilocal_node_range(NULL),
_active_node_range(NULL),
_local_node_range(NULL),
_bnd_node_range(NULL),
_bnd_elem_range(NULL),
_node_to_elem_map_built(false),
_patch_size(40),
_regular_orthogonal_mesh(false)
{
*(getMesh().boundary_info) = *(other_mesh.getMesh().boundary_info);
const std::set<SubdomainID> & subdomains = other_mesh.meshSubdomains();
for (std::set<SubdomainID>::const_iterator it = subdomains.begin(); it != subdomains.end(); ++it)
{
SubdomainID sid = *it;
setSubdomainName(sid, other_mesh.getMesh().subdomain_name(sid));
}
std::vector<BoundaryID> side_boundaries;
other_mesh.getMesh().boundary_info->build_side_boundary_ids(side_boundaries);
for (std::vector<BoundaryID>::const_iterator it = side_boundaries.begin(); it != side_boundaries.end(); ++it)
{
BoundaryID bid = *it;
getMesh().boundary_info->sideset_name(bid) = other_mesh.getMesh().boundary_info->sideset_name(bid);
}
std::vector<BoundaryID> node_boundaries;
other_mesh.getMesh().boundary_info->build_node_boundary_ids(node_boundaries);
for (std::vector<BoundaryID>::const_iterator it = node_boundaries.begin(); it != node_boundaries.end(); ++it)
{
BoundaryID bid = *it;
getMesh().boundary_info->nodeset_name(bid) = other_mesh.getMesh().boundary_info->nodeset_name(bid);
}
}
MooseMesh::~MooseMesh()
{
freeBndNodes();
freeBndElems();
clearQuadratureNodes();
delete _active_local_elem_range;
delete _active_node_range;
delete _active_semilocal_node_range;
delete _local_node_range;
delete _bnd_node_range;
delete _bnd_elem_range;
delete _mesh;
}
void
MooseMesh::freeBndNodes()
{
// free memory
for (std::vector<BndNode *>::iterator it = _bnd_nodes.begin(); it != _bnd_nodes.end(); ++it)
delete (*it);
for (std::map<short int, std::vector<unsigned int> >::iterator it = _node_set_nodes.begin(); it != _node_set_nodes.end(); ++it)
it->second.clear();
_node_set_nodes.clear();
_bnd_node_ids.clear();
}
void
MooseMesh::freeBndElems()
{
// free memory
for (std::vector<BndElement *>::iterator it = _bnd_elems.begin(); it != _bnd_elems.end(); ++it)
delete (*it);
}
void
MooseMesh::prepare()
{
if (dynamic_cast<ParallelMesh *>(&getMesh()) && !_is_nemesis)
{
// Call prepare_for_use() and allow renumbering
getMesh().allow_renumbering(true);
getMesh().prepare_for_use();
}
else
{
// Call prepare_for_use() and DO NOT allow renumbering
getMesh().allow_renumbering(false);
getMesh().prepare_for_use();
}
// Collect (local) subdomain IDs
const MeshBase::element_iterator el_end = getMesh().elements_end();
for (MeshBase::element_iterator el = getMesh().elements_begin(); el != el_end; ++el)
_mesh_subdomains.insert((*el)->subdomain_id());
// Collect (local) boundary IDs
const std::set<BoundaryID>& local_bids = getMesh().boundary_info->get_boundary_ids();
_mesh_boundary_ids.insert(local_bids.begin(), local_bids.end());
// Communicate subdomain and boundary IDs if this is a parallel mesh
if (!getMesh().is_serial())
{
// Pack our subdomain IDs into a vector
std::vector<SubdomainID> mesh_subdomains_vector(_mesh_subdomains.begin(),
_mesh_subdomains.end());
// Gather them all into an enlarged vector
Parallel::allgather(mesh_subdomains_vector);
// Attempt to insert any new IDs into the set (any existing ones will be skipped)
_mesh_subdomains.insert(mesh_subdomains_vector.begin(),
mesh_subdomains_vector.end());
// Pack our boundary IDs into a vector for communication
std::vector<BoundaryID> mesh_boundary_ids_vector(_mesh_boundary_ids.begin(),
_mesh_boundary_ids.end());
// Gather them all into an enlarged vector
Parallel::allgather(mesh_boundary_ids_vector);
// Attempt to insert any new IDs into the set (any existing ones will be skipped)
_mesh_boundary_ids.insert(mesh_boundary_ids_vector.begin(),
mesh_boundary_ids_vector.end());
}
detectOrthogonalDimRanges();
update();
// Prepared has been called
_is_prepared = true;
}
void
MooseMesh::update()
{
// Rebuild the boundary conditions
buildNodeListFromSideList();
//Update the node to elem map
_node_to_elem_map.clear();
_node_to_elem_map_built = false;
buildNodeList();
buildBndElemList();
cacheInfo();
}
const Node &
MooseMesh::node(const unsigned int i) const
{
if(i > getMesh().max_node_id())
return *(*_quadrature_nodes.find(i)).second;
return getMesh().node(i);
}
Node &
MooseMesh::node(const unsigned int i)
{
if(i > getMesh().max_node_id())
return *_quadrature_nodes[i];
return getMesh().node(i);
}
const Node*
MooseMesh::nodePtr(const unsigned int i) const
{
if(i > getMesh().max_node_id())
return (*_quadrature_nodes.find(i)).second;
return getMesh().node_ptr(i);
}
Node*
MooseMesh::nodePtr(const unsigned int i)
{
if(i > getMesh().max_node_id())
return _quadrature_nodes[i];
return getMesh().node_ptr(i);
}
void
MooseMesh::meshChanged()
{
update();
// Rebuild the active local element range
delete _active_local_elem_range;
_active_local_elem_range = NULL;
// Rebuild the node range
delete _active_node_range;
_active_node_range = NULL;
// Rebuild the semilocal range
delete _active_semilocal_node_range;
_active_semilocal_node_range = NULL;
// Rebuild the local node range
delete _local_node_range;
_local_node_range = NULL;
// Rebuild the boundary node range
delete _bnd_node_range;
_bnd_node_range = NULL;
// Rebuild the ranges
getActiveLocalElementRange();
getActiveNodeRange();
getLocalNodeRange();
getBoundaryNodeRange();
// Lets the output system know that the mesh has changed recently.
_is_changed = true;
}
void
MooseMesh::cacheChangedLists()
{
ConstElemRange elem_range(getMesh().local_elements_begin(), getMesh().local_elements_end(), 1);
CacheChangedListsThread cclt(*this);
Threads::parallel_reduce(elem_range, cclt);
delete _refined_elements;
delete _coarsened_elements;
_coarsened_element_children.clear();
_refined_elements = new ConstElemPointerRange(cclt._refined_elements.begin(), cclt._refined_elements.end());
_coarsened_elements = new ConstElemPointerRange(cclt._coarsened_elements.begin(), cclt._coarsened_elements.end());
_coarsened_element_children = cclt._coarsened_element_children;
}
ConstElemPointerRange *
MooseMesh::refinedElementRange()
{
return _refined_elements;
}
ConstElemPointerRange *
MooseMesh::coarsenedElementRange()
{
return _coarsened_elements;
}
std::vector<const Elem *> &
MooseMesh::coarsenedElementChildren(const Elem * elem)
{
return _coarsened_element_children[elem];
}
void
MooseMesh::updateActiveSemiLocalNodeRange(std::set<unsigned int> & ghosted_elems)
{
_semilocal_node_list.clear();
// First add the nodes connected to local elems
ConstElemRange * active_local_elems = getActiveLocalElementRange();
for(ConstElemRange::const_iterator it=active_local_elems->begin();
it!=active_local_elems->end();
++it)
{
const Elem * elem = *it;
for(unsigned int n=0; n<elem->n_nodes(); n++)
{
Node * node = elem->get_node(n);
_semilocal_node_list.insert(node);
}
}
// Now add the nodes connected to ghosted_elems
for(std::set<unsigned int>::iterator it=ghosted_elems.begin();
it!=ghosted_elems.end();
++it)
{
Elem * elem = getMesh().elem(*it);
for(unsigned int n=0; n<elem->n_nodes(); n++)
{
Node * node = elem->get_node(n);
_semilocal_node_list.insert(node);
}
}
delete _active_semilocal_node_range;
// Now create the actual range
_active_semilocal_node_range = new SemiLocalNodeRange(_semilocal_node_list.begin(), _semilocal_node_list.end());
}
void
MooseMesh::buildNodeList()
{
freeBndNodes();
/// Boundary node list (node ids and corresponding side-set ids, arrays always have the same length)
std::vector<unsigned int> nodes;
std::vector<short int> ids;
getMesh().boundary_info->build_node_list(nodes, ids);
int n = nodes.size();
_bnd_nodes.resize(n);
for (int i = 0; i < n; i++)
{
_bnd_nodes[i] = new BndNode(&getMesh().node(nodes[i]), ids[i]);
_node_set_nodes[ids[i]].push_back(nodes[i]);
_bnd_node_ids.insert(nodes[i]);
}
_bnd_nodes.reserve(_bnd_nodes.size() + _extra_bnd_nodes.size());
for(unsigned int i=0; i<_extra_bnd_nodes.size(); i++)
{
BndNode * bnode = new BndNode(_extra_bnd_nodes[i]._node, _extra_bnd_nodes[i]._bnd_id);
_bnd_nodes.push_back(bnode);
_bnd_node_ids.insert(_extra_bnd_nodes[i]._node->id());
}
}
void
MooseMesh::buildBndElemList()
{
freeBndElems();
/// Boundary node list (node ids and corresponding side-set ids, arrays always have the same length)
std::vector<unsigned int> elems;
std::vector<unsigned short int> sides;
std::vector<boundary_id_type> ids;
getMesh().boundary_info->build_side_list(elems, sides, ids);
int n = elems.size();
_bnd_elems.resize(n);
for (int i = 0; i < n; i++)
{
_bnd_elems[i] = new BndElement(getMesh().elem(elems[i]), sides[i], ids[i]);
}
}
std::map<unsigned int, std::vector<unsigned int> > &
MooseMesh::nodeToElemMap()
{
if(!_node_to_elem_map_built)
{
_node_to_elem_map_built = true;
MeshBase::const_element_iterator el = getMesh().elements_begin();
const MeshBase::const_element_iterator end = getMesh().elements_end();
for (; el != end; ++el)
for (unsigned int n=0; n<(*el)->n_nodes(); n++)
_node_to_elem_map[(*el)->node(n)].push_back((*el)->id());
}
return _node_to_elem_map;
}
ConstElemRange *
MooseMesh::getActiveLocalElementRange()
{
if (!_active_local_elem_range)
{
_active_local_elem_range = new ConstElemRange(getMesh().active_local_elements_begin(),
getMesh().active_local_elements_end(), GRAIN_SIZE);
}
return _active_local_elem_range;
}
NodeRange *
MooseMesh::getActiveNodeRange()
{
if (!_active_node_range)
{
_active_node_range = new NodeRange(getMesh().active_nodes_begin(),
getMesh().active_nodes_end(), GRAIN_SIZE);
}
return _active_node_range;
}
SemiLocalNodeRange *
MooseMesh::getActiveSemiLocalNodeRange()
{
mooseAssert(_active_semilocal_node_range, "_active_semilocal_node_range has not been created yet!");
/*
if (!_active_node_range)
{
_active_semilocal_node_range = new NodeRange(getMesh().local_nodes_begin(),
getMesh().local_nodes_end(), GRAIN_SIZE);
}
*/
return _active_semilocal_node_range;
}
ConstNodeRange *
MooseMesh::getLocalNodeRange()
{
if (!_local_node_range)
{
_local_node_range = new ConstNodeRange(getMesh().local_nodes_begin(),
getMesh().local_nodes_end(), GRAIN_SIZE);
}
return _local_node_range;
}
ConstBndNodeRange *
MooseMesh::getBoundaryNodeRange()
{
if (!_bnd_node_range)
{
_bnd_node_range = new ConstBndNodeRange(bndNodesBegin(),
bndNodesEnd(), GRAIN_SIZE);
}
return _bnd_node_range;
}
ConstBndElemRange *
MooseMesh::getBoundaryElementRange()
{
if (!_bnd_elem_range)
{
_bnd_elem_range = new ConstBndElemRange(bndElemsBegin(),
bndElemsEnd(), GRAIN_SIZE);
}
return _bnd_elem_range;
}
void
MooseMesh::cacheInfo()
{
const MeshBase::element_iterator end = getMesh().elements_end();
for (MeshBase::element_iterator el = getMesh().elements_begin(); el != end; ++el)
{
Elem * elem = *el;
unsigned int subdomain_id = elem->subdomain_id();
for(unsigned int side=0; side<elem->n_sides(); side++)
{
std::vector<BoundaryID> boundaryids = boundaryIDs(elem, side);
for(unsigned int i=0; i<boundaryids.size(); i++)
_subdomain_boundary_ids[subdomain_id].insert(boundaryids[i]);
}
for(unsigned int nd = 0; nd < elem->n_nodes(); ++nd)
{
Node & node = *elem->get_node(nd);
_block_node_list[node.id()].insert(elem->subdomain_id());
}
}
}
std::set<SubdomainID> &
MooseMesh::getNodeBlockIds(const Node & node)
{
return _block_node_list[node.id()];
}
// default begin() accessor
bnd_node_iterator
MooseMesh::bndNodesBegin ()
{
Predicates::NotNull<bnd_node_iterator_imp> p;
return bnd_node_iterator(_bnd_nodes.begin(), _bnd_nodes.end(), p);
}
// default end() accessor
bnd_node_iterator
MooseMesh::bndNodesEnd ()
{
Predicates::NotNull<bnd_node_iterator_imp> p;
return bnd_node_iterator(_bnd_nodes.end(), _bnd_nodes.end(), p);
}
// default begin() accessor
bnd_elem_iterator
MooseMesh::bndElemsBegin ()
{
Predicates::NotNull<bnd_elem_iterator_imp> p;
return bnd_elem_iterator(_bnd_elems.begin(), _bnd_elems.end(), p);
}
// default end() accessor
bnd_elem_iterator
MooseMesh::bndElemsEnd ()
{
Predicates::NotNull<bnd_elem_iterator_imp> p;
return bnd_elem_iterator(_bnd_elems.end(), _bnd_elems.end(), p);
}
const Node *
MooseMesh::addUniqueNode(const Point & p, Real tol)
{
/**
* Looping through the mesh nodes each time we add a point is very slow. To speed things
* up we keep a local data structure
*/
if (getMesh().n_nodes() != _node_map.size())
{
_node_map.clear();
_node_map.reserve(getMesh().n_nodes());
const libMesh::MeshBase::node_iterator end = getMesh().nodes_end();
for (libMesh::MeshBase::node_iterator i=getMesh().nodes_begin(); i != end; ++i)
{
_node_map.push_back(*i);
}
}
Node *node = NULL;
for (unsigned int i=0; i<_node_map.size(); ++i)
{
if (p.relative_fuzzy_equals(*_node_map[i], tol))
{
node = _node_map[i];
break;
}
}
if (node == NULL)
{
node = getMesh().add_node(new Node(p));
_node_map.push_back(node);
}
/* Alternative method
libMesh::MeshBase::node_iterator i = getMesh().nodes_begin();
libMesh::MeshBase::node_iterator i_end = getMesh().nodes_end();
Node *node = NULL;
for (; i != i_end; ++i)
{
if (p.relative_fuzzy_equals(**i, tol))
{
node = *i;
break;
}
}
if (node == NULL)
node = getMesh().add_node(new Node(p));
*/
mooseAssert(node != NULL, "Node is NULL");
return node;
}
Node *
MooseMesh::addQuadratureNode(const Elem * elem, const unsigned short int side, const unsigned int qp, BoundaryID bid, const Point & point)
{
Node * qnode;
if(_elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side].find(qp) == _elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side].end())
{
// Create a new node id starting from the max node id and counting down. This will be the least
// likely to collide with an existing node id.
unsigned int max_id = std::numeric_limits<unsigned int>::max()-100;
unsigned int new_id = max_id - _quadrature_nodes.size();
if(new_id <= getMesh().max_node_id())
mooseError("Quadrature node id collides with existing node id!");
qnode = new Node(point, new_id);
// Keep track of this new node in two different ways for easy lookup
_quadrature_nodes[new_id] = qnode;
_elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side][qp] = qnode;
_node_to_elem_map[new_id].push_back(elem->id());
}
else
qnode = _elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side][qp];
BndNode * bnode = new BndNode(qnode, bid);
_bnd_nodes.push_back(bnode);
_bnd_node_ids.insert(qnode->id());
_extra_bnd_nodes.push_back(*bnode);
// Do this so the range will be regenerated next time it is accessed
delete _bnd_node_range;
_bnd_node_range = NULL;
return qnode;
}
Node *
MooseMesh::getQuadratureNode(const Elem * elem, const unsigned short int side, const unsigned int qp)
{
mooseAssert(_elem_to_side_to_qp_to_quadrature_nodes.find(elem->id()) != _elem_to_side_to_qp_to_quadrature_nodes.end(), "Elem has no quadrature nodes!");
mooseAssert(_elem_to_side_to_qp_to_quadrature_nodes[elem->id()].find(side) != _elem_to_side_to_qp_to_quadrature_nodes[elem->id()].end(), "Side has no quadrature nodes!");
mooseAssert(_elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side].find(qp) != _elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side].end(), "qp not found on side!");
return _elem_to_side_to_qp_to_quadrature_nodes[elem->id()][side][qp];
}
void
MooseMesh::clearQuadratureNodes()
{
{ // Delete all the quadrature nodes
std::map<unsigned int, Node *>::iterator it = _quadrature_nodes.begin();
std::map<unsigned int, Node *>::iterator end = _quadrature_nodes.end();
for(; it != end; ++it)
delete it->second;
}
_quadrature_nodes.clear();
_elem_to_side_to_qp_to_quadrature_nodes.clear();
_extra_bnd_nodes.clear();
}
BoundaryID
MooseMesh::getBoundaryID(const BoundaryName & boundary_name) const
{
if (boundary_name == "ANY_BOUNDARY_ID")
mooseError("Please use getBoundaryIDs() when passing \"ANY_BOUNDARY_ID\"");
BoundaryID id;
std::istringstream ss(boundary_name);
if (!(ss >> id))
id = getMesh().boundary_info->get_id_by_name(boundary_name);
return id;
}
std::vector<BoundaryID>
MooseMesh::getBoundaryIDs(const std::vector<BoundaryName> & boundary_name, bool generate_unknown) const
{
std::vector<BoundaryID> ids(boundary_name.size());
std::map<BoundaryID, std::string> & sideset_map = getMesh().boundary_info->set_sideset_name_map();
std::map<BoundaryID, std::string> & nodeset_map = getMesh().boundary_info->set_nodeset_name_map();
std::set<BoundaryID> boundary_ids = getMesh().boundary_info->get_boundary_ids();
BoundaryID max_boundary_id = boundary_ids.empty() ? 0 : *(getMesh().boundary_info->get_boundary_ids().rbegin());
for(unsigned int i=0; i<boundary_name.size(); i++)
{
if (boundary_name[i] == "ANY_BOUNDARY_ID")
{
ids.assign(_mesh_boundary_ids.begin(), _mesh_boundary_ids.end());
if (i)
mooseWarning("You passed \"ANY_BOUNDARY_ID\" in addition to other boundary_names. This may be a logic error.");
break;
}
BoundaryID id;
std::istringstream ss(boundary_name[i]);
if (!(ss >> id))
{
/**
* If the converstion from a name to a number fails, that means that this must be a named
* boundary. We will look in the complete map for this sideset and create a new name/ID pair
* if requested.
*/
if (generate_unknown
&& !MooseUtils::doesMapContainValue(sideset_map, std::string(boundary_name[i]))
&& !MooseUtils::doesMapContainValue(nodeset_map, std::string(boundary_name[i])))
id = ++max_boundary_id;
else
id = getMesh().boundary_info->get_id_by_name(boundary_name[i]);
}
ids[i] = id;
}
return ids;
}
SubdomainID
MooseMesh::getSubdomainID(const SubdomainName & subdomain_name) const
{
if (subdomain_name == "ANY_BLOCK_ID")
mooseError("Please use getSubdomainIDs() when passing \"ANY_BLOCK_ID\"");
SubdomainID id;
std::istringstream ss(subdomain_name);
if (!(ss >> id))
id = getMesh().get_id_by_name(subdomain_name);
return id;
}
std::vector<SubdomainID>
MooseMesh::getSubdomainIDs(const std::vector<SubdomainName> & subdomain_name) const
{
std::vector<SubdomainID> ids(subdomain_name.size());
for(unsigned int i=0; i<subdomain_name.size(); i++)
{
if (subdomain_name[i] == "ANY_BLOCK_ID")
{
ids.assign(_mesh_subdomains.begin(), _mesh_subdomains.end());
if (i)
mooseWarning("You passed \"ANY_BLOCK_ID\" in addition to other sudomain_names. This may be a logic error.");
break;
}
SubdomainID id;
std::istringstream ss(subdomain_name[i]);
if (!(ss >> id))
id = getMesh().get_id_by_name(subdomain_name[i]);
ids[i] = id;
}
return ids;
}
void
MooseMesh::setSubdomainName(SubdomainID subdomain_id, SubdomainName name)
{
getMesh().subdomain_name(subdomain_id) = name;
}
void
MooseMesh::setBoundaryName(BoundaryID boundary_id, BoundaryName name)
{
std::vector<BoundaryID> side_boundaries;
getMesh().boundary_info->build_side_boundary_ids(side_boundaries);
// We need to figure out if this boundary is a sideset or nodeset
if (std::find(side_boundaries.begin(), side_boundaries.end(), boundary_id) != side_boundaries.end())
getMesh().boundary_info->sideset_name(boundary_id) = name;
else
getMesh().boundary_info->nodeset_name(boundary_id) = name;
}
void
MooseMesh::buildPeriodicNodeMap(std::multimap<unsigned int, unsigned int> & periodic_node_map, unsigned int var_number, PeriodicBoundaries *pbs) const
{
mooseAssert(!Threads::in_threads, "This function should only be called outside of a threaded region due to the use of PointLocator");
periodic_node_map.clear();
MeshBase::const_element_iterator it = getMesh().active_local_elements_begin();
MeshBase::const_element_iterator it_end = getMesh().active_local_elements_end();
AutoPtr<PointLocatorBase> point_locator = getMesh().sub_point_locator();
for (; it != it_end; ++it)
{
const Elem *elem = *it;
for (unsigned int s=0; s<elem->n_sides(); ++s)
{
if (elem->neighbor(s))
continue;
const std::vector<boundary_id_type>& bc_ids = getMesh().boundary_info->boundary_ids (elem, s);
for (std::vector<boundary_id_type>::const_iterator id_it = bc_ids.begin(); id_it!=bc_ids.end(); ++id_it)
{
const boundary_id_type boundary_id = *id_it;
const PeriodicBoundaryBase *periodic = pbs->boundary(boundary_id);
if (periodic && periodic->is_my_variable(var_number))
{
const Elem* neigh = pbs->neighbor(boundary_id, *point_locator, elem, s);
unsigned int s_neigh = getMesh().boundary_info->side_with_boundary_id (neigh, periodic->pairedboundary);
AutoPtr<Elem> elem_side = elem->build_side(s);
AutoPtr<Elem> neigh_side = neigh->build_side(s_neigh);
// At this point we have matching sides - lets find matching nodes
for (unsigned int i=0; i<elem_side->n_nodes(); ++i)
{
Node *master_node = elem->get_node(i);
Point master_point = periodic->get_corresponding_pos(*master_node);
for (unsigned int j=0; j<neigh_side->n_nodes(); ++j)
{
Node *slave_node = neigh_side->get_node(j);
if (master_point.absolute_fuzzy_equals(*slave_node))
{
// Avoid inserting any duplicates
std::pair<std::multimap<unsigned int, unsigned int>::iterator, std::multimap<unsigned int, unsigned int>::iterator> iters =
periodic_node_map.equal_range(master_node->id());
bool found = false;
for (std::multimap<unsigned int, unsigned int>::iterator map_it = iters.first; map_it != iters.second; ++map_it)
if (map_it->second == slave_node->id())
found = true;
if (!found)
periodic_node_map.insert(std::make_pair(master_node->id(), slave_node->id()));
}
}
}
}
}
}
}
}
void
MooseMesh::buildPeriodicNodeSets(std::map<BoundaryID, std::set<unsigned int> > & periodic_node_sets, unsigned int var_number, PeriodicBoundaries *pbs) const
{
periodic_node_sets.clear();
std::vector<unsigned int> nl;
std::vector<boundary_id_type> il;
getMesh().boundary_info->build_node_list(nl, il);
// Loop over all the boundary nodes adding the periodic nodes to the appropriate set
for (unsigned int i=0; i<nl.size(); ++i)
{
// Is this current node on a known periodic boundary?
if (periodic_node_sets.find(il[i]) != periodic_node_sets.end())
periodic_node_sets[il[i]].insert(nl[i]);
else // This still might be a periodic node but we just haven't seen this boundary_id yet
{
const PeriodicBoundaryBase *periodic = pbs->boundary(il[i]);
if (periodic && periodic->is_my_variable(var_number))
periodic_node_sets[il[i]].insert(nl[i]);
}
}
}
bool
MooseMesh::detectOrthogonalDimRanges(Real tol)
{
// If this mesh is already regular orthogonal, we don't need to do any extra work!
if (_regular_orthogonal_mesh)
{
// Make sure that bounds has also been set
if (_bounds.size() != LIBMESH_DIM)
mooseError("\"_regular_orthogonal_mesh\" has been set, but \"_bounds\" has not been properly initialized.");
return true;
}
std::vector<Real> min(3, std::numeric_limits<Real>::max());
std::vector<Real> max(3, std::numeric_limits<Real>::min());
unsigned int dim = getMesh().mesh_dimension();
// Find the bounding box of our mesh
const MeshBase::node_iterator nd_end = getMesh().nodes_end();
for (MeshBase::node_iterator nd = getMesh().nodes_begin(); nd != nd_end; ++nd)
{
Node &node = **nd;
for (unsigned int i=0; i<dim; ++i)
{
if (node(i) < min[i])
min[i] = node(i);
if (node(i) > max[i])
max[i] = node(i);
}
}
_extreme_nodes.resize(8); // 2^LIBMESH_DIM
// Now make sure that there are actual nodes at all of the extremes
unsigned int extreme_matches = 0;
std::vector<unsigned int> comp_map(3);
for (MeshBase::node_iterator nd = getMesh().nodes_begin(); nd != nd_end; ++nd)
{
// See if the current node is located at one of the extremes
Node &node = **nd;
unsigned int coord_match = 0;
for (unsigned int i=0; i<dim; ++i)
{
if (std::abs(node(i) - min[i]) < tol)
{
comp_map[i] = MIN;
++coord_match;
}
else if (std::abs(node(i) - max[i]) <tol)
{
comp_map[i] = MAX;
++coord_match;