-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathdistributed_mesh.C
More file actions
1853 lines (1477 loc) · 55.6 KB
/
distributed_mesh.C
File metadata and controls
1853 lines (1477 loc) · 55.6 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/distributed_mesh.h"
// libMesh includes
#include "libmesh/boundary_info.h"
#include "libmesh/elem.h"
#include "libmesh/libmesh_logging.h"
#include "libmesh/mesh_communication.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/partitioner.h"
#include "libmesh/string_to_enum.h"
// TIMPI includes
#include "timpi/parallel_implementation.h"
#include "timpi/parallel_sync.h"
namespace libMesh
{
// ------------------------------------------------------------
// DistributedMesh class member functions
DistributedMesh::DistributedMesh (const Parallel::Communicator & comm_in,
unsigned char d) :
UnstructuredMesh (comm_in,d), _is_serial(true),
_is_serial_on_proc_0(true),
_deleted_coarse_elements(false),
_n_nodes(0), _n_elem(0), _max_node_id(0), _max_elem_id(0),
_next_free_local_node_id(this->processor_id()),
_next_free_local_elem_id(this->processor_id()),
_next_free_unpartitioned_node_id(this->n_processors()),
_next_free_unpartitioned_elem_id(this->n_processors())
#ifdef LIBMESH_ENABLE_UNIQUE_ID
, _next_unpartitioned_unique_id(this->n_processors())
#endif
{
#ifdef LIBMESH_ENABLE_UNIQUE_ID
_next_unique_id = this->processor_id();
#endif
const std::string default_partitioner = "parmetis";
const std::string my_partitioner =
libMesh::command_line_value("--default-partitioner",
default_partitioner);
_partitioner = Partitioner::build
(Utility::string_to_enum<PartitionerType>(my_partitioner));
}
DistributedMesh & DistributedMesh::operator= (DistributedMesh && other_mesh)
{
LOG_SCOPE("operator=(&&)", "DistributedMesh");
// Move assign as an UnstructuredMesh.
this->UnstructuredMesh::operator=(std::move(other_mesh));
// Nodes and elements belong to DistributedMesh and have to be
// moved before we can move arbitrary GhostingFunctor, Partitioner,
// etc. subclasses.
this->move_nodes_and_elements(std::move(other_mesh));
// But move_nodes_and_elems misses (or guesses about) some of our
// subclass values, and we want more precision than a guess.
_deleted_coarse_elements = other_mesh._deleted_coarse_elements;
_extra_ghost_elems = std::move(other_mesh._extra_ghost_elems);
// Handle remaining MeshBase moves.
this->post_dofobject_moves(std::move(other_mesh));
return *this;
}
MeshBase & DistributedMesh::assign(MeshBase && other_mesh)
{
*this = std::move(cast_ref<DistributedMesh&>(other_mesh));
return *this;
}
std::string_view DistributedMesh::subclass_first_difference_from (const MeshBase & other_mesh_base) const
{
const DistributedMesh * dist_mesh_ptr =
dynamic_cast<const DistributedMesh *>(&other_mesh_base);
if (!dist_mesh_ptr)
return "DistributedMesh class";
const DistributedMesh & other_mesh = *dist_mesh_ptr;
#define CHECK_MEMBER(member_name) \
if (member_name != other_mesh.member_name) \
return #member_name;
CHECK_MEMBER(_is_serial);
CHECK_MEMBER(_is_serial_on_proc_0);
CHECK_MEMBER(_deleted_coarse_elements);
CHECK_MEMBER(_n_nodes);
CHECK_MEMBER(_n_elem);
CHECK_MEMBER(_max_node_id);
CHECK_MEMBER(_max_elem_id);
// We expect these things to change in a prepare_for_use();
// they're conceptually "mutable"...
/*
CHECK_MEMBER(_next_free_local_node_id);
CHECK_MEMBER(_next_free_local_elem_id);
CHECK_MEMBER(_next_free_unpartitioned_node_id);
CHECK_MEMBER(_next_free_unpartitioned_elem_id);
#ifdef LIBMESH_ENABLE_UNIQUE_ID
CHECK_MEMBER(_next_unpartitioned_unique_id);
#endif
*/
if (!this->nodes_and_elements_equal(other_mesh))
return "nodes and/or elements";
if (_extra_ghost_elems.size() !=
other_mesh._extra_ghost_elems.size())
return "_extra_ghost_elems size";
for (auto & elem : _extra_ghost_elems)
{
libmesh_assert(this->query_elem_ptr(elem->id()) == elem);
const Elem * other_elem = other_mesh.query_elem_ptr(elem->id());
if (!other_elem ||
!other_mesh._extra_ghost_elems.count(const_cast<Elem *>(other_elem)))
return "_extra_ghost_elems entry";
}
return "";
}
DistributedMesh::~DistributedMesh ()
{
this->DistributedMesh::clear(); // Free nodes and elements
}
// This might be specialized later, but right now it's just here to
// make sure the compiler doesn't give us a default (non-deep) copy
// constructor instead.
DistributedMesh::DistributedMesh (const DistributedMesh & other_mesh) :
DistributedMesh(static_cast<const MeshBase &>(other_mesh))
{
_is_serial = other_mesh._is_serial;
_is_serial_on_proc_0 = other_mesh._is_serial_on_proc_0;
_deleted_coarse_elements = other_mesh._deleted_coarse_elements;
_n_nodes = other_mesh.n_nodes();
_n_elem = other_mesh.n_elem();
_max_node_id = other_mesh.max_node_id();
_max_elem_id = other_mesh.max_elem_id();
_next_free_local_node_id =
other_mesh._next_free_local_node_id;
_next_free_local_elem_id =
other_mesh._next_free_local_elem_id;
_next_free_unpartitioned_node_id =
other_mesh._next_free_unpartitioned_node_id;
_next_free_unpartitioned_elem_id =
other_mesh._next_free_unpartitioned_elem_id;
#ifdef LIBMESH_ENABLE_UNIQUE_ID
_next_unique_id =
other_mesh._next_unique_id;
_next_unpartitioned_unique_id =
other_mesh._next_unpartitioned_unique_id;
#endif
// Need to copy extra_ghost_elems
for (auto & elem : other_mesh._extra_ghost_elems)
_extra_ghost_elems.insert(this->elem_ptr(elem->id()));
}
DistributedMesh::DistributedMesh (const MeshBase & other_mesh) :
UnstructuredMesh (other_mesh), _is_serial(other_mesh.is_serial()),
_is_serial_on_proc_0(other_mesh.is_serial()),
_deleted_coarse_elements(true), // better safe than sorry...
_n_nodes(0), _n_elem(0), _max_node_id(0), _max_elem_id(0),
_next_free_local_node_id(this->processor_id()),
_next_free_local_elem_id(this->processor_id()),
_next_free_unpartitioned_node_id(this->n_processors()),
_next_free_unpartitioned_elem_id(this->n_processors())
{
// Just copy, skipping preparation
this->copy_nodes_and_elements(other_mesh, true, 0, 0, 0, nullptr, true);
this->allow_find_neighbors(other_mesh.allow_find_neighbors());
this->allow_renumbering(other_mesh.allow_renumbering());
this->allow_remote_element_removal(other_mesh.allow_remote_element_removal());
this->skip_partitioning(other_mesh.skip_partitioning());
this->copy_constraint_rows(other_mesh);
this->_preparation = other_mesh.preparation();
auto & this_boundary_info = this->get_boundary_info();
const auto & other_boundary_info = other_mesh.get_boundary_info();
this_boundary_info = other_boundary_info;
this->set_subdomain_name_map() = other_mesh.get_subdomain_name_map();
#ifdef LIBMESH_ENABLE_UNIQUE_ID
_next_unique_id = other_mesh.parallel_max_unique_id() +
this->processor_id();
_next_unpartitioned_unique_id = _next_unique_id +
(this->n_processors() - this->processor_id());
#endif
this->update_parallel_id_counts();
}
void DistributedMesh::move_nodes_and_elements(MeshBase && other_meshbase)
{
DistributedMesh & other_mesh = cast_ref<DistributedMesh&>(other_meshbase);
this->_nodes = std::move(other_mesh._nodes);
this->_n_nodes = other_mesh.n_nodes();
this->_elements = std::move(other_mesh._elements);
this->_n_elem = other_mesh.n_elem();
_is_serial = other_mesh.is_serial();
_is_serial_on_proc_0 = other_mesh.is_serial_on_zero();
_deleted_coarse_elements = true; // Better safe than sorry
_max_node_id = other_mesh.max_node_id();
_max_elem_id = other_mesh.max_elem_id();
_next_free_local_node_id = other_mesh._next_free_local_node_id;
_next_free_local_elem_id = other_mesh._next_free_local_elem_id;
_next_free_unpartitioned_node_id = other_mesh._next_free_unpartitioned_node_id;
_next_free_unpartitioned_elem_id = other_mesh._next_free_unpartitioned_elem_id;
#ifdef LIBMESH_ENABLE_UNIQUE_ID
_next_unpartitioned_unique_id = other_mesh._next_unpartitioned_unique_id;
#endif
}
// We use cached values for these so they can be called
// from one processor without bothering the rest, but
// we may need to update those caches before doing a full
// renumbering
void DistributedMesh::update_parallel_id_counts()
{
// This function must be run on all processors at once
parallel_object_only();
_n_elem = this->parallel_n_elem();
_n_nodes = this->parallel_n_nodes();
_max_node_id = this->parallel_max_node_id();
_max_elem_id = this->parallel_max_elem_id();
if (_next_free_unpartitioned_elem_id < _max_elem_id)
_next_free_unpartitioned_elem_id =
((_max_elem_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
if (_next_free_local_elem_id < _max_elem_id)
_next_free_local_elem_id =
((_max_elem_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
if (_next_free_unpartitioned_node_id < _max_node_id)
_next_free_unpartitioned_node_id =
((_max_node_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
if (_next_free_local_node_id < _max_node_id)
_next_free_local_node_id =
((_max_node_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
#ifdef LIBMESH_ENABLE_UNIQUE_ID
_next_unique_id = this->parallel_max_unique_id();
_next_unpartitioned_unique_id =
((_next_unique_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
_next_unique_id =
((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
#endif
this->_preparation.has_synched_id_counts = true;
}
// Or in debug mode we may want to test the uncached values without
// changing the cache
dof_id_type DistributedMesh::parallel_n_elem() const
{
// This function must be run on all processors at once
parallel_object_only();
dof_id_type n_local = this->n_local_elem();
this->comm().sum(n_local);
n_local += this->n_unpartitioned_elem();
return n_local;
}
dof_id_type DistributedMesh::parallel_max_elem_id() const
{
// This function must be run on all processors at once
parallel_object_only();
dof_id_type max_local = 0;
dofobject_container<Elem>::const_reverse_veclike_iterator
rit = _elements.rbegin();
const dofobject_container<Elem>::const_reverse_veclike_iterator
rend = _elements.rend();
// Look for the maximum element id. Search backwards through
// elements so we can break out early. Beware of nullptr entries that
// haven't yet been cleared from _elements.
for (; rit != rend; ++rit)
{
const DofObject *d = *rit;
if (d)
{
libmesh_assert(_elements[d->id()] == d);
max_local = d->id() + 1;
break;
}
}
this->comm().max(max_local);
return max_local;
}
#ifdef LIBMESH_ENABLE_UNIQUE_ID
unique_id_type DistributedMesh::parallel_max_unique_id() const
{
// This function must be run on all processors at once
parallel_object_only();
unique_id_type max_local = std::max(_next_unique_id,
_next_unpartitioned_unique_id);
this->comm().max(max_local);
return max_local;
}
void DistributedMesh::set_next_unique_id(unique_id_type id)
{
_next_unique_id = id;
_next_unpartitioned_unique_id =
((_next_unique_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
_next_unique_id =
((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
}
#endif
dof_id_type DistributedMesh::parallel_n_nodes() const
{
// This function must be run on all processors at once
parallel_object_only();
dof_id_type n_local = this->n_local_nodes();
this->comm().sum(n_local);
n_local += this->n_unpartitioned_nodes();
return n_local;
}
dof_id_type DistributedMesh::parallel_max_node_id() const
{
// This function must be run on all processors at once
parallel_object_only();
dof_id_type max_local = 0;
dofobject_container<Node>::const_reverse_veclike_iterator
rit = _nodes.rbegin();
const dofobject_container<Node>::const_reverse_veclike_iterator
rend = _nodes.rend();
// Look for the maximum node id. Search backwards through
// nodes so we can break out early. Beware of nullptr entries that
// haven't yet been cleared from _nodes
for (; rit != rend; ++rit)
{
const DofObject *d = *rit;
if (d)
{
libmesh_assert(_nodes[d->id()] == d);
max_local = d->id() + 1;
break;
}
}
this->comm().max(max_local);
return max_local;
}
const Point & DistributedMesh::point (const dof_id_type i) const
{
return this->node_ref(i);
}
const Node * DistributedMesh::node_ptr (const dof_id_type i) const
{
libmesh_assert(_nodes[i]);
libmesh_assert_equal_to (_nodes[i]->id(), i);
return _nodes[i];
}
Node * DistributedMesh::node_ptr (const dof_id_type i)
{
libmesh_assert(_nodes[i]);
libmesh_assert_equal_to (_nodes[i]->id(), i);
return _nodes[i];
}
const Node * DistributedMesh::query_node_ptr (const dof_id_type i) const
{
if (const auto it = _nodes.find(i);
it != _nodes.end())
{
const Node * n = *it;
libmesh_assert (!n || n->id() == i);
return n;
}
return nullptr;
}
Node * DistributedMesh::query_node_ptr (const dof_id_type i)
{
if (auto it = _nodes.find(i);
it != _nodes.end())
{
Node * n = *it;
libmesh_assert (!n || n->id() == i);
return n;
}
return nullptr;
}
const Elem * DistributedMesh::elem_ptr (const dof_id_type i) const
{
libmesh_assert(_elements[i]);
libmesh_assert_equal_to (_elements[i]->id(), i);
return _elements[i];
}
Elem * DistributedMesh::elem_ptr (const dof_id_type i)
{
libmesh_assert(_elements[i]);
libmesh_assert_equal_to (_elements[i]->id(), i);
return _elements[i];
}
const Elem * DistributedMesh::query_elem_ptr (const dof_id_type i) const
{
if (const auto it = _elements.find(i);
it != _elements.end())
{
const Elem * e = *it;
libmesh_assert (!e || e->id() == i);
return e;
}
return nullptr;
}
Elem * DistributedMesh::query_elem_ptr (const dof_id_type i)
{
if (auto it = _elements.find(i);
it != _elements.end())
{
Elem * e = *it;
libmesh_assert (!e || e->id() == i);
return e;
}
return nullptr;
}
Elem * DistributedMesh::add_elem (Elem * e)
{
// Don't try to add nullptrs!
libmesh_assert(e);
// Trying to add an existing element is a no-op
if (e->valid_id() && _elements[e->id()] == e)
return e;
const processor_id_type elem_procid = e->processor_id();
if (!e->valid_id())
{
// We should only be creating new ids past the end of the range
// of existing ids
libmesh_assert_greater_equal(_next_free_unpartitioned_elem_id,
_max_elem_id);
libmesh_assert_greater_equal(_next_free_local_elem_id, _max_elem_id);
// Use the unpartitioned ids for unpartitioned elems, and
// temporarily for ghost elems
dof_id_type * next_id = &_next_free_unpartitioned_elem_id;
if (elem_procid == this->processor_id())
next_id = &_next_free_local_elem_id;
e->set_id (*next_id);
}
{
// Advance next_ids up high enough that each is pointing to an
// unused id and any subsequent increments will still point us
// to unused ids
_max_elem_id = std::max(_max_elem_id,
static_cast<dof_id_type>(e->id()+1));
if (_next_free_unpartitioned_elem_id < _max_elem_id)
_next_free_unpartitioned_elem_id =
((_max_elem_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
if (_next_free_local_elem_id < _max_elem_id)
_next_free_local_elem_id =
((_max_elem_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
#ifndef NDEBUG
// We need a const dofobject_container so we don't inadvertently create
// nullptr entries when testing for non-nullptr ones
const dofobject_container<Elem> & const_elements = _elements;
#endif
libmesh_assert(!const_elements[_next_free_unpartitioned_elem_id]);
libmesh_assert(!const_elements[_next_free_local_elem_id]);
}
// Don't try to overwrite existing elems
libmesh_assert (!_elements[e->id()]);
_elements[e->id()] = e;
// Try to make the cached elem data more accurate
if (elem_procid == this->processor_id() ||
elem_procid == DofObject::invalid_processor_id)
_n_elem++;
#ifdef LIBMESH_ENABLE_UNIQUE_ID
if (!e->valid_unique_id())
{
if (processor_id() == e->processor_id())
{
e->set_unique_id(_next_unique_id);
_next_unique_id += this->n_processors() + 1;
}
else
{
e->set_unique_id(_next_unpartitioned_unique_id);
_next_unpartitioned_unique_id += this->n_processors() + 1;
}
}
else
{
_next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
_next_unique_id =
((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
}
#endif
// Unpartitioned elems should be added on every processor
// And shouldn't be added in the same batch as ghost elems
// But we might be just adding on processor 0 to
// broadcast later
// #ifdef DEBUG
// if (elem_procid == DofObject::invalid_processor_id)
// {
// dof_id_type elem_id = e->id();
// this->comm().max(elem_id);
// libmesh_assert_equal_to (elem_id, e->id());
// }
// #endif
// Make sure any new element is given space for any extra integers
// we've requested
e->add_extra_integers(_elem_integer_names.size(),
_elem_integer_default_values);
// And set mapping type and data on any new element
e->set_mapping_type(this->default_mapping_type());
e->set_mapping_data(this->default_mapping_data());
return e;
}
Elem * DistributedMesh::add_elem (std::unique_ptr<Elem> e)
{
// The mesh now takes ownership of the Elem. Eventually the guts of
// add_elem() will get moved to a private helper function, and
// calling add_elem() directly will be deprecated.
return add_elem(e.release());
}
Elem * DistributedMesh::insert_elem (Elem * e)
{
if (_elements[e->id()])
this->delete_elem(_elements[e->id()]);
#ifdef LIBMESH_ENABLE_UNIQUE_ID
if (!e->valid_unique_id())
{
if (processor_id() == e->processor_id())
{
e->set_unique_id(_next_unique_id);
_next_unique_id += this->n_processors() + 1;
}
else
{
e->set_unique_id(_next_unpartitioned_unique_id);
_next_unpartitioned_unique_id += this->n_processors() + 1;
}
}
else
{
_next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
_next_unique_id =
((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
}
#endif
// Try to make the cached elem data more accurate
processor_id_type elem_procid = e->processor_id();
if (elem_procid == this->processor_id() ||
elem_procid == DofObject::invalid_processor_id)
_n_elem++;
_elements[e->id()] = e;
// Make sure any new element is given space for any extra integers
// we've requested
e->add_extra_integers(_elem_integer_names.size(),
_elem_integer_default_values);
// And set mapping type and data on any new element
e->set_mapping_type(this->default_mapping_type());
e->set_mapping_data(this->default_mapping_data());
return e;
}
Elem * DistributedMesh::insert_elem (std::unique_ptr<Elem> e)
{
// The mesh now takes ownership of the Elem. Eventually the guts of
// insert_elem(Elem*) will get moved to a private helper function, and
// calling insert_elem(Elem*) directly will be deprecated.
return insert_elem(e.release());
}
void DistributedMesh::delete_elem(Elem * e)
{
libmesh_assert (e);
// Try to make the cached elem data more accurate
_n_elem--;
// Was this a coarse element, not just a coarsening where we still
// have some ancestor structure? Was it a *local* element, that we
// might have been depending on as an owner of local nodes? We'll
// have to be more careful with our nodes in contract() later; no
// telling if we just locally orphaned a node that should be
// globally retained.
if (e->processor_id() == this->processor_id() &&
!e->parent())
_deleted_coarse_elements = true;
// Delete the element from the BoundaryInfo object
this->get_boundary_info().remove(e);
// But not yet from the container; we might invalidate
// an iterator that way!
//_elements.erase(e->id());
// Instead, we set it to nullptr for now
_elements[e->id()] = nullptr;
// delete the element
delete e;
}
void DistributedMesh::renumber_elem(const dof_id_type old_id,
const dof_id_type new_id)
{
// This could be a no-op
if (old_id == new_id)
return;
Elem * el = _elements[old_id];
libmesh_assert (el);
libmesh_assert_equal_to (el->id(), old_id);
el->set_id(new_id);
libmesh_assert (!_elements[new_id]);
_elements[new_id] = el;
_elements.erase(old_id);
}
Node * DistributedMesh::add_point (const Point & p,
const dof_id_type id,
const processor_id_type proc_id)
{
Node * old_n = this->query_node_ptr(id);
if (old_n)
{
*old_n = p;
old_n->processor_id() = proc_id;
return old_n;
}
Node * n = Node::build(p, id).release();
n->processor_id() = proc_id;
return DistributedMesh::add_node(n);
}
void DistributedMesh::own_node (Node & n)
{
// This had better be a node in our mesh
libmesh_assert(_nodes[n.id()] == &n);
_nodes[n.id()] = nullptr;
_n_nodes--;
n.set_id(DofObject::invalid_id);
n.processor_id() = this->processor_id();
this->add_node(&n);
}
Node * DistributedMesh::add_node (Node * n)
{
// Don't try to add nullptrs!
libmesh_assert(n);
// Trying to add an existing node is a no-op
if (n->valid_id() && _nodes[n->id()] == n)
return n;
const processor_id_type node_procid = n->processor_id();
if (!n->valid_id())
{
// We should only be creating new ids past the end of the range
// of existing ids
libmesh_assert_greater_equal(_next_free_unpartitioned_node_id,
_max_node_id);
libmesh_assert_greater_equal(_next_free_local_node_id, _max_node_id);
// Use the unpartitioned ids for unpartitioned nodes,
// and temporarily for ghost nodes
dof_id_type * next_id = &_next_free_unpartitioned_node_id;
if (node_procid == this->processor_id())
next_id = &_next_free_local_node_id;
n->set_id (*next_id);
}
{
// Advance next_ids up high enough that each is pointing to an
// unused id and any subsequent increments will still point us
// to unused ids
_max_node_id = std::max(_max_node_id,
static_cast<dof_id_type>(n->id()+1));
if (_next_free_unpartitioned_node_id < _max_node_id)
_next_free_unpartitioned_node_id =
((_max_node_id-1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->n_processors();
if (_next_free_local_node_id < _max_node_id)
_next_free_local_node_id =
((_max_node_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
#ifndef NDEBUG
// We need a const dofobject_container so we don't inadvertently create
// nullptr entries when testing for non-nullptr ones
const dofobject_container<Node> & const_nodes = _nodes;
#endif
libmesh_assert(!const_nodes[_next_free_unpartitioned_node_id]);
libmesh_assert(!const_nodes[_next_free_local_node_id]);
}
// Don't try to overwrite existing nodes
libmesh_assert (!_nodes[n->id()]);
_nodes[n->id()] = n;
// Try to make the cached node data more accurate
if (node_procid == this->processor_id() ||
node_procid == DofObject::invalid_processor_id)
_n_nodes++;
#ifdef LIBMESH_ENABLE_UNIQUE_ID
if (!n->valid_unique_id())
{
if (processor_id() == n->processor_id())
{
n->set_unique_id(_next_unique_id);
_next_unique_id += this->n_processors() + 1;
}
else
{
n->set_unique_id(_next_unpartitioned_unique_id);
_next_unpartitioned_unique_id += this->n_processors() + 1;
}
}
else
{
_next_unique_id = std::max(_next_unique_id, n->unique_id()+1);
_next_unique_id =
((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
(this->n_processors() + 1) + this->processor_id();
}
#endif
n->add_extra_integers(_node_integer_names.size(),
_node_integer_default_values);
// Unpartitioned nodes should be added on every processor
// And shouldn't be added in the same batch as ghost nodes
// But we might be just adding on processor 0 to
// broadcast later
// #ifdef DEBUG
// if (node_procid == DofObject::invalid_processor_id)
// {
// dof_id_type node_id = n->id();
// this->comm().max(node_id);
// libmesh_assert_equal_to (node_id, n->id());
// }
// #endif
return n;
}
Node * DistributedMesh::add_node (std::unique_ptr<Node> n)
{
// The mesh now takes ownership of the Node. Eventually the guts of
// add_node() will get moved to a private helper function, and
// calling add_node() directly will be deprecated.
return add_node(n.release());
}
void DistributedMesh::delete_node(Node * n)
{
libmesh_assert(n);
libmesh_assert(_nodes[n->id()]);
// Try to make the cached elem data more accurate
_n_nodes--;
// Delete the node from the BoundaryInfo object
this->get_boundary_info().remove(n);
_constraint_rows.erase(n);
// But not yet from the container; we might invalidate
// an iterator that way!
//_nodes.erase(n->id());
// Instead, we set it to nullptr for now
_nodes[n->id()] = nullptr;
// delete the node
delete n;
}
void DistributedMesh::renumber_node(const dof_id_type old_id,
const dof_id_type new_id)
{
// This could be a no-op
if (old_id == new_id)
return;
Node * nd = _nodes[old_id];
libmesh_assert (nd);
libmesh_assert_equal_to (nd->id(), old_id);
// If we have nodes shipped to this processor for NodeConstraints
// use, then those nodes will exist in _nodes, but may not be
// locatable via a TopologyMap due to the insufficiency of elements
// connecting to them. If local refinement then wants to create a
// *new* node in the same location, it will initially get a temporary
// id, and then make_node_ids_parallel_consistent() will try to move
// it to the canonical id. We need to account for this case to
// avoid false positives and memory leaks.
#ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
if (_nodes[new_id])
{
libmesh_assert_equal_to (*(Point *)_nodes[new_id],
*(Point *)_nodes[old_id]);
_nodes.erase(new_id);
}
#else
// If we aren't shipping nodes for NodeConstraints, there should be
// no reason for renumbering one node onto another.
libmesh_assert (!_nodes[new_id]);
#endif
_nodes[new_id] = nd;
nd->set_id(new_id);
_nodes.erase(old_id);
}
void DistributedMesh::clear ()
{
// Call parent clear function
MeshBase::clear();
// Clear our elements and nodes
// There is no need to remove them from
// the BoundaryInfo data structure since we
// already cleared it.
this->DistributedMesh::clear_elems();
for (auto & node : _nodes)
delete node;
_nodes.clear();
// We're no longer distributed if we were before
_is_serial = true;