/
dof_map.C
2922 lines (2361 loc) · 92.2 KB
/
dof_map.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
// The libMesh Finite Element Library.
// Copyright (C) 2002-2012 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
// C++ Includes -------------------------------------
#include <set>
#include <algorithm> // for std::fill, std::equal_range, std::max, std::lower_bound, etc.
// Local Includes -----------------------------------
#include "libmesh/coupling_matrix.h"
#include "libmesh/dense_matrix.h"
#include "libmesh/dense_vector_base.h"
#include "libmesh/dirichlet_boundaries.h"
#include "libmesh/dof_map.h"
#include "libmesh/elem.h"
#include "libmesh/fe_interface.h"
#include "libmesh/fe_type.h"
#include "libmesh/fe_base.h" // FEBase::build() for continuity test
#include "libmesh/libmesh_logging.h"
#include "libmesh/mesh_base.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/numeric_vector.h"
#include "libmesh/parallel.h"
#include "libmesh/periodic_boundaries.h"
#include "libmesh/sparse_matrix.h"
#include "libmesh/sparsity_pattern.h"
#include "libmesh/string_to_enum.h"
#include "libmesh/threads.h"
namespace libMesh
{
// ------------------------------------------------------------
// DofMap member functions
AutoPtr<SparsityPattern::Build> DofMap::build_sparsity
(const MeshBase& mesh) const
{
libmesh_assert (mesh.is_prepared());
libmesh_assert (this->n_variables());
START_LOG("build_sparsity()", "DofMap");
// Compute the sparsity structure of the global matrix. This can be
// fed into a PetscMatrix to allocate exacly the number of nonzeros
// necessary to store the matrix. This algorithm should be linear
// in the (# of elements)*(# nodes per element)
// We can be more efficient in the threaded sparsity pattern assembly
// if we don't need the exact pattern. For some sparse matrix formats
// a good upper bound will suffice.
// See if we need to include sparsity pattern entries for coupling
// between neighbor dofs
bool implicit_neighbor_dofs = this->use_coupled_neighbor_dofs(mesh);
// We can compute the sparsity pattern in parallel on multiple
// threads. The goal is for each thread to compute the full sparsity
// pattern for a subset of elements. These sparsity patterns can
// be efficiently merged in the SparsityPattern::Build::join()
// method, especially if there is not too much overlap between them.
// Even better, if the full sparsity pattern is not needed then
// the number of nonzeros per row can be estimated from the
// sparsity patterns created on each thread.
AutoPtr<SparsityPattern::Build> sp
(new SparsityPattern::Build (mesh,
*this,
this->_dof_coupling,
implicit_neighbor_dofs,
need_full_sparsity_pattern));
Threads::parallel_reduce (ConstElemRange (mesh.active_local_elements_begin(),
mesh.active_local_elements_end()), *sp);
sp->parallel_sync();
#ifndef NDEBUG
// Avoid declaring these variables unless asserts are enabled.
const unsigned int proc_id = mesh.processor_id();
const unsigned int n_dofs_on_proc = this->n_dofs_on_processor(proc_id);
#endif
libmesh_assert_equal_to (sp->sparsity_pattern.size(), n_dofs_on_proc);
STOP_LOG("build_sparsity()", "DofMap");
// Check to see if we have any extra stuff to add to the sparsity_pattern
if (_extra_sparsity_function)
{
if (_augment_sparsity_pattern)
{
libmesh_here();
libMesh::out << "WARNING: You have specified both an extra sparsity function and object.\n"
<< " Are you sure this is what you meant to do??"
<< std::endl;
}
_extra_sparsity_function
(sp->sparsity_pattern, sp->n_nz,
sp->n_oz, _extra_sparsity_context);
}
if (_augment_sparsity_pattern)
_augment_sparsity_pattern->augment_sparsity_pattern
(sp->sparsity_pattern, sp->n_nz, sp->n_oz);
return sp;
}
DofMap::DofMap(const unsigned int number) :
_dof_coupling(NULL),
_variables(),
_variable_groups(),
_sys_number(number),
_matrices(),
_first_df(),
_end_df(),
_send_list(),
_augment_sparsity_pattern(NULL),
_extra_sparsity_function(NULL),
_extra_sparsity_context(NULL),
_augment_send_list(NULL),
_extra_send_list_function(NULL),
_extra_send_list_context(NULL),
need_full_sparsity_pattern(false),
_n_nz(NULL),
_n_oz(NULL),
_n_dfs(0),
_n_SCALAR_dofs(0)
#ifdef LIBMESH_ENABLE_AMR
, _n_old_dfs(0),
_first_old_df(),
_end_old_df()
#endif
#ifdef LIBMESH_ENABLE_CONSTRAINTS
, _dof_constraints()
#endif
#ifdef LIBMESH_ENABLE_PERIODIC
, _periodic_boundaries(new PeriodicBoundaries)
#endif
#ifdef LIBMESH_ENABLE_DIRICHLET
, _dirichlet_boundaries(new DirichletBoundaries)
#endif
{
_matrices.clear();
}
// Destructor
DofMap::~DofMap()
{
this->clear();
#ifdef LIBMESH_ENABLE_PERIODIC
delete _periodic_boundaries;
#endif
#ifdef LIBMESH_ENABLE_DIRICHLET
delete _dirichlet_boundaries;
#endif
}
#ifdef LIBMESH_ENABLE_PERIODIC
bool DofMap::is_periodic_boundary (const unsigned int boundaryid) const
{
if (_periodic_boundaries->count(boundaryid) != 0)
return true;
return false;
}
#endif
// void DofMap::add_variable (const Variable &var)
// {
// libmesh_error();
// _variables.push_back (var);
// }
void DofMap::add_variable_group (const VariableGroup &var_group)
{
_variable_groups.push_back(var_group);
for (unsigned int var=0; var<var_group.n_variables(); var++)
_variables.push_back (var_group(var));
}
void DofMap::attach_matrix (SparseMatrix<Number>& matrix)
{
parallel_only();
// We shouldn't be trying to re-attach the same matrices repeatedly
libmesh_assert (std::find(_matrices.begin(), _matrices.end(),
&matrix) == _matrices.end());
_matrices.push_back(&matrix);
matrix.attach_dof_map (*this);
// If we've already computed sparsity, then it's too late
// to wait for "compute_sparsity" to help with sparse matrix
// initialization, and we need to handle this matrix individually
bool computed_sparsity_already =
((_n_nz && !_n_nz->empty()) ||
(_n_oz && !_n_oz->empty()));
CommWorld.max(computed_sparsity_already);
if (computed_sparsity_already &&
matrix.need_full_sparsity_pattern())
{
// We'd better have already computed the full sparsity pattern
// if we need it here
libmesh_assert(need_full_sparsity_pattern);
libmesh_assert(_sp.get());
matrix.update_sparsity_pattern (_sp->sparsity_pattern);
}
if (matrix.need_full_sparsity_pattern())
need_full_sparsity_pattern = true;
}
bool DofMap::is_attached (SparseMatrix<Number>& matrix)
{
return (std::find(_matrices.begin(), _matrices.end(),
&matrix) != _matrices.end());
}
DofObject* DofMap::node_ptr(MeshBase& mesh, unsigned int i) const
{
return mesh.node_ptr(i);
}
DofObject* DofMap::elem_ptr(MeshBase& mesh, unsigned int i) const
{
return mesh.elem(i);
}
template <typename iterator_type>
void DofMap::set_nonlocal_dof_objects(iterator_type objects_begin,
iterator_type objects_end,
MeshBase &mesh,
dofobject_accessor objects)
{
// This function must be run on all processors at once
parallel_only();
// First, iterate over local objects to find out how many
// are on each processor
std::vector<unsigned int>
ghost_objects_from_proc(libMesh::n_processors(), 0);
iterator_type it = objects_begin;
for (; it != objects_end; ++it)
{
DofObject *obj = *it;
if (obj)
{
unsigned int obj_procid = obj->processor_id();
// We'd better be completely partitioned by now
libmesh_assert_not_equal_to (obj_procid, DofObject::invalid_processor_id);
ghost_objects_from_proc[obj_procid]++;
}
}
std::vector<unsigned int> objects_on_proc(libMesh::n_processors(), 0);
CommWorld.allgather(ghost_objects_from_proc[libMesh::processor_id()],
objects_on_proc);
#ifdef DEBUG
for (unsigned int p=0; p != libMesh::n_processors(); ++p)
libmesh_assert_less_equal (ghost_objects_from_proc[p], objects_on_proc[p]);
#endif
// Request sets to send to each processor
std::vector<std::vector<unsigned int> >
requested_ids(libMesh::n_processors());
// We know how many of our objects live on each processor, so
// reserve() space for requests from each.
for (unsigned int p=0; p != libMesh::n_processors(); ++p)
if (p != libMesh::processor_id())
requested_ids[p].reserve(ghost_objects_from_proc[p]);
for (it = objects_begin; it != objects_end; ++it)
{
DofObject *obj = *it;
if (obj->processor_id() != DofObject::invalid_processor_id)
requested_ids[obj->processor_id()].push_back(obj->id());
}
#ifdef DEBUG
for (unsigned int p=0; p != libMesh::n_processors(); ++p)
libmesh_assert_equal_to (requested_ids[p].size(), ghost_objects_from_proc[p]);
#endif
// Next set ghost object n_comps from other processors
for (unsigned int p=1; p != libMesh::n_processors(); ++p)
{
// Trade my requests with processor procup and procdown
unsigned int procup = (libMesh::processor_id() + p) %
libMesh::n_processors();
unsigned int procdown = (libMesh::n_processors() +
libMesh::processor_id() - p) %
libMesh::n_processors();
std::vector<unsigned int> request_to_fill;
CommWorld.send_receive(procup, requested_ids[procup],
procdown, request_to_fill);
// Fill those requests
const unsigned int
sys_num = this->sys_number(),
n_var_groups = this->n_variable_groups();
std::vector<unsigned int> ghost_data
(request_to_fill.size() * 2 * n_var_groups);
for (unsigned int i=0; i != request_to_fill.size(); ++i)
{
DofObject *requested = (this->*objects)(mesh, request_to_fill[i]);
libmesh_assert(requested);
libmesh_assert_equal_to (requested->processor_id(), libMesh::processor_id());
libmesh_assert_equal_to (requested->n_var_groups(sys_num), n_var_groups);
for (unsigned int vg=0; vg != n_var_groups; ++vg)
{
unsigned int n_comp_g =
requested->n_comp_group(sys_num, vg);
ghost_data[i*2*n_var_groups+vg] = n_comp_g;
unsigned int first_dof = n_comp_g ?
requested->vg_dof_base(sys_num, vg) : 0;
libmesh_assert_not_equal_to (first_dof, DofObject::invalid_id);
ghost_data[i*2*n_var_groups+n_var_groups+vg] = first_dof;
}
}
// Trade back the results
std::vector<unsigned int> filled_request;
CommWorld.send_receive(procdown, ghost_data,
procup, filled_request);
// And copy the id changes we've now been informed of
libmesh_assert_equal_to (filled_request.size(),
requested_ids[procup].size() * 2 * n_var_groups);
for (unsigned int i=0; i != requested_ids[procup].size(); ++i)
{
DofObject *requested = (this->*objects)(mesh, requested_ids[procup][i]);
libmesh_assert(requested);
libmesh_assert_equal_to (requested->processor_id(), procup);
for (unsigned int vg=0; vg != n_var_groups; ++vg)
{
unsigned int n_comp_g = filled_request[i*2*n_var_groups+vg];
requested->set_n_comp_group(sys_num, vg, n_comp_g);
if (n_comp_g)
{
unsigned int first_dof =
filled_request[i*2*n_var_groups+n_var_groups+vg];
libmesh_assert_not_equal_to (first_dof, DofObject::invalid_id);
requested->set_vg_dof_base
(sys_num, vg, first_dof);
}
}
}
}
#ifdef DEBUG
// Double check for invalid dofs
for (it = objects_begin; it != objects_end; ++it)
{
DofObject *obj = *it;
libmesh_assert (obj);
unsigned int n_variables = obj->n_vars(this->sys_number());
for (unsigned int v=0; v != n_variables; ++v)
{
unsigned int n_comp =
obj->n_comp(this->sys_number(), v);
unsigned int first_dof = n_comp ?
obj->dof_number(this->sys_number(), v, 0) : 0;
libmesh_assert_not_equal_to (first_dof, DofObject::invalid_id);
}
}
#endif
}
void DofMap::reinit(MeshBase& mesh)
{
libmesh_assert (mesh.is_prepared());
START_LOG("reinit()", "DofMap");
const unsigned int
sys_num = this->sys_number(),
n_var_groups = this->n_variable_groups();
// The DofObjects need to know how many variable groups we have, and
// how many variables there are in each group.
std::vector<unsigned int> n_vars_per_group; /**/ n_vars_per_group.reserve (n_var_groups);
for (unsigned int vg=0; vg<n_var_groups; vg++)
n_vars_per_group.push_back (this->variable_group(vg).n_variables());
#ifdef LIBMESH_ENABLE_AMR
//------------------------------------------------------------
// Clear the old_dof_objects for all the nodes
// and elements so that we can overwrite them
{
MeshBase::node_iterator node_it = mesh.nodes_begin();
const MeshBase::node_iterator node_end = mesh.nodes_end();
for ( ; node_it != node_end; ++node_it)
{
(*node_it)->clear_old_dof_object();
libmesh_assert (!(*node_it)->old_dof_object);
}
MeshBase::element_iterator elem_it = mesh.elements_begin();
const MeshBase::element_iterator elem_end = mesh.elements_end();
for ( ; elem_it != elem_end; ++elem_it)
{
(*elem_it)->clear_old_dof_object();
libmesh_assert (!(*elem_it)->old_dof_object);
}
}
//------------------------------------------------------------
// Set the old_dof_objects for the elements that
// weren't just created, if these old dof objects
// had variables
{
MeshBase::element_iterator elem_it = mesh.elements_begin();
const MeshBase::element_iterator elem_end = mesh.elements_end();
for ( ; elem_it != elem_end; ++elem_it)
{
Elem* elem = *elem_it;
// Skip the elements that were just refined
if (elem->refinement_flag() == Elem::JUST_REFINED) continue;
for (unsigned int n=0; n<elem->n_nodes(); n++)
{
Node* node = elem->get_node(n);
if (node->old_dof_object == NULL)
if (node->has_dofs(sys_num))
node->set_old_dof_object();
}
libmesh_assert (!elem->old_dof_object);
if (elem->has_dofs(sys_num))
elem->set_old_dof_object();
}
}
#endif // #ifdef LIBMESH_ENABLE_AMR
//------------------------------------------------------------
// Then set the number of variables for each \p DofObject
// equal to n_variables() for this system. This will
// handle new \p DofObjects that may have just been created
{
// All the nodes
MeshBase::node_iterator node_it = mesh.nodes_begin();
const MeshBase::node_iterator node_end = mesh.nodes_end();
for ( ; node_it != node_end; ++node_it)
(*node_it)->set_n_vars_per_group(sys_num, n_vars_per_group);
// All the elements
MeshBase::element_iterator elem_it = mesh.elements_begin();
const MeshBase::element_iterator elem_end = mesh.elements_end();
for ( ; elem_it != elem_end; ++elem_it)
(*elem_it)->set_n_vars_per_group(sys_num, n_vars_per_group);
}
// Zero _n_SCALAR_dofs, it will be updated below.
this->_n_SCALAR_dofs = 0;
//------------------------------------------------------------
// Next allocate space for the DOF indices
for (unsigned int vg=0; vg<n_var_groups; vg++)
{
const VariableGroup &vg_description = this->variable_group(vg);
const unsigned int n_var_in_group = vg_description.n_variables();
const FEType& base_fe_type = vg_description.type();
// Don't need to loop over elements for a SCALAR variable
// Just increment _n_SCALAR_dofs
if(base_fe_type.family == SCALAR)
{
this->_n_SCALAR_dofs += base_fe_type.order*n_var_in_group;
continue;
}
// This should be constant even on p-refined elements
const bool extra_hanging_dofs =
FEInterface::extra_hanging_dofs(base_fe_type);
// For all the active elements
MeshBase::element_iterator elem_it = mesh.active_elements_begin();
const MeshBase::element_iterator elem_end = mesh.active_elements_end();
// Count vertex degrees of freedom first
for ( ; elem_it != elem_end; ++elem_it)
{
Elem* elem = *elem_it;
libmesh_assert(elem);
// Skip the numbering if this variable is
// not active on this element's subdomain
if (!vg_description.active_on_subdomain(elem->subdomain_id()))
continue;
const ElemType type = elem->type();
const unsigned int dim = elem->dim();
FEType fe_type = base_fe_type;
#ifdef LIBMESH_ENABLE_AMR
// Make sure we haven't done more p refinement than we can
// handle
if (elem->p_level() + base_fe_type.order >
FEInterface::max_order(base_fe_type, type))
{
# ifdef DEBUG
if (FEInterface::max_order(base_fe_type,type) <
static_cast<unsigned int>(base_fe_type.order))
{
libMesh::err
<< "ERROR: Finite element "
<< Utility::enum_to_string(base_fe_type.family)
<< " on geometric element "
<< Utility::enum_to_string(type) << std::endl
<< "only supports FEInterface::max_order = "
<< FEInterface::max_order(base_fe_type,type)
<< ", not fe_type.order = " << base_fe_type.order
<< std::endl;
libmesh_error();
}
libMesh::err
<< "WARNING: Finite element "
<< Utility::enum_to_string(base_fe_type.family)
<< " on geometric element "
<< Utility::enum_to_string(type) << std::endl
<< "could not be p refined past FEInterface::max_order = "
<< FEInterface::max_order(base_fe_type,type)
<< std::endl;
# endif
elem->set_p_level(FEInterface::max_order(base_fe_type,type)
- base_fe_type.order);
}
#endif
fe_type.order = static_cast<Order>(fe_type.order +
elem->p_level());
// Allocate the vertex DOFs
for (unsigned int n=0; n<elem->n_nodes(); n++)
{
Node* node = elem->get_node(n);
if (elem->is_vertex(n))
{
const unsigned int old_node_dofs =
node->n_comp_group(sys_num, vg);
const unsigned int vertex_dofs =
std::max(FEInterface::n_dofs_at_node(dim, fe_type,
type, n),
old_node_dofs);
// Some discontinuous FEs have no vertex dofs
if (vertex_dofs > old_node_dofs)
{
node->set_n_comp_group(sys_num, vg,
vertex_dofs);
// Abusing dof_number to set a "this is a
// vertex" flag
node->set_vg_dof_base(sys_num, vg,
vertex_dofs);
// std::cout << "sys_num,vg,old_node_dofs,vertex_dofs="
// << sys_num << ","
// << vg << ","
// << old_node_dofs << ","
// << vertex_dofs << '\n',
// node->debug_buffer();
// libmesh_assert_equal_to (vertex_dofs, node->n_comp(sys_num, vg));
// libmesh_assert_equal_to (vertex_dofs, node->vg_dof_base(sys_num, vg));
}
}
}
} // done counting vertex dofs
// count edge & face dofs next
elem_it = mesh.active_elements_begin();
for ( ; elem_it != elem_end; ++elem_it)
{
Elem* elem = *elem_it;
libmesh_assert(elem);
// Skip the numbering if this variable is
// not active on this element's subdomain
if (!vg_description.active_on_subdomain(elem->subdomain_id()))
continue;
const ElemType type = elem->type();
const unsigned int dim = elem->dim();
FEType fe_type = base_fe_type;
fe_type.order = static_cast<Order>(fe_type.order +
elem->p_level());
// Allocate the edge and face DOFs
for (unsigned int n=0; n<elem->n_nodes(); n++)
{
Node* node = elem->get_node(n);
const unsigned int old_node_dofs =
node->n_comp_group(sys_num, vg);
const unsigned int vertex_dofs = old_node_dofs?
node->vg_dof_base (sys_num,vg):0;
const unsigned int new_node_dofs =
FEInterface::n_dofs_at_node(dim, fe_type, type, n);
// We've already allocated vertex DOFs
if (elem->is_vertex(n))
{
libmesh_assert_greater_equal (old_node_dofs, vertex_dofs);
// //if (vertex_dofs < new_node_dofs)
// std::cout << "sys_num,vg,old_node_dofs,vertex_dofs,new_node_dofs="
// << sys_num << ","
// << vg << ","
// << old_node_dofs << ","
// << vertex_dofs << ","
// << new_node_dofs << '\n',
// node->debug_buffer();
libmesh_assert_greater_equal (vertex_dofs, new_node_dofs);
}
// We need to allocate the rest
else
{
// If this has no dofs yet, it needs no vertex
// dofs, so we just give it edge or face dofs
if (!old_node_dofs)
{
node->set_n_comp_group(sys_num, vg,
new_node_dofs);
// Abusing dof_number to set a "this has no
// vertex dofs" flag
if (new_node_dofs)
node->set_vg_dof_base(sys_num, vg,
0);
}
// If this has dofs, but has no vertex dofs,
// it may still need more edge or face dofs if
// we're p-refined.
else if (vertex_dofs == 0)
{
if (new_node_dofs > old_node_dofs)
{
node->set_n_comp_group(sys_num, vg,
new_node_dofs);
node->set_vg_dof_base(sys_num, vg,
vertex_dofs);
}
}
// If this is another element's vertex,
// add more (non-overlapping) edge/face dofs if
// necessary
else if (extra_hanging_dofs)
{
if (new_node_dofs > old_node_dofs - vertex_dofs)
{
node->set_n_comp_group(sys_num, vg,
vertex_dofs + new_node_dofs);
node->set_vg_dof_base(sys_num, vg,
vertex_dofs);
}
}
// If this is another element's vertex, add any
// (overlapping) edge/face dofs if necessary
else
{
libmesh_assert_greater_equal (old_node_dofs, vertex_dofs);
if (new_node_dofs > old_node_dofs)
{
node->set_n_comp_group(sys_num, vg,
new_node_dofs);
node->set_vg_dof_base (sys_num, vg,
vertex_dofs);
}
}
}
}
// Allocate the element DOFs
const unsigned int dofs_per_elem =
FEInterface::n_dofs_per_elem(dim, fe_type,
type);
elem->set_n_comp_group(sys_num, vg, dofs_per_elem);
}
} // end loop over variable groups
// Calling DofMap::reinit() by itself makes little sense,
// so we won't bother with nonlocal DofObjects.
// Those will be fixed by distribute_dofs
//------------------------------------------------------------
// Finally, clear all the current DOF indices
// (distribute_dofs expects them cleared!)
this->invalidate_dofs(mesh);
STOP_LOG("reinit()", "DofMap");
}
void DofMap::invalidate_dofs(MeshBase& mesh) const
{
const unsigned int sys_num = this->sys_number();
// All the nodes
MeshBase::node_iterator node_it = mesh.nodes_begin();
const MeshBase::node_iterator node_end = mesh.nodes_end();
for ( ; node_it != node_end; ++node_it)
(*node_it)->invalidate_dofs(sys_num);
// All the elements
MeshBase::element_iterator elem_it = mesh.active_elements_begin();
const MeshBase::element_iterator elem_end = mesh.active_elements_end();
for ( ; elem_it != elem_end; ++elem_it)
(*elem_it)->invalidate_dofs(sys_num);
}
void DofMap::clear()
{
// we don't want to clear
// the coupling matrix!
// It should not change...
//_dof_coupling->clear();
_variables.clear();
_variable_groups.clear();
_first_df.clear();
_end_df.clear();
_send_list.clear();
this->clear_sparsity();
need_full_sparsity_pattern = false;
#ifdef LIBMESH_ENABLE_AMR
_dof_constraints.clear();
_n_old_dfs = 0;
_first_old_df.clear();
_end_old_df.clear();
#endif
_matrices.clear();
_n_dfs = 0;
}
void DofMap::distribute_dofs (MeshBase& mesh)
{
// This function must be run on all processors at once
parallel_only();
// Log how long it takes to distribute the degrees of freedom
START_LOG("distribute_dofs()", "DofMap");
libmesh_assert (mesh.is_prepared());
const unsigned int proc_id = libMesh::processor_id();
const unsigned int n_proc = libMesh::n_processors();
// libmesh_assert_greater (this->n_variables(), 0);
libmesh_assert_less (proc_id, n_proc);
// re-init in case the mesh has changed
this->reinit(mesh);
// By default distribute variables in a
// var-major fashion, but allow run-time
// specification
bool node_major_dofs = libMesh::on_command_line ("--node_major_dofs");
// The DOF counter, will be incremented as we encounter
// new degrees of freedom
unsigned int next_free_dof = 0;
// Clear the send list before we rebuild it
_send_list.clear();
// Set temporary DOF indices on this processor
if (node_major_dofs)
this->distribute_local_dofs_node_major (next_free_dof, mesh);
else
this->distribute_local_dofs_var_major (next_free_dof, mesh);
// Get DOF counts on all processors
std::vector<unsigned int> dofs_on_proc(n_proc, 0);
CommWorld.allgather(next_free_dof, dofs_on_proc);
// Resize and fill the _first_df and _end_df arrays
#ifdef LIBMESH_ENABLE_AMR
_first_old_df = _first_df;
_end_old_df = _end_df;
#endif
_first_df.resize(n_proc);
_end_df.resize (n_proc);
// Get DOF offsets
_first_df[0] = 0;
for (unsigned int i=1; i < n_proc; ++i)
_first_df[i] = _end_df[i-1] = _first_df[i-1] + dofs_on_proc[i-1];
_end_df[n_proc-1] = _first_df[n_proc-1] + dofs_on_proc[n_proc-1];
// Clear all the current DOF indices
// (distribute_dofs expects them cleared!)
this->invalidate_dofs(mesh);
next_free_dof = _first_df[proc_id];
// Set permanent DOF indices on this processor
if (node_major_dofs)
this->distribute_local_dofs_node_major (next_free_dof, mesh);
else
this->distribute_local_dofs_var_major (next_free_dof, mesh);
libmesh_assert_equal_to (next_free_dof, _end_df[proc_id]);
//------------------------------------------------------------
// At this point, all n_comp and dof_number values on local
// DofObjects should be correct, but a ParallelMesh might have
// incorrect values on non-local DofObjects. Let's request the
// correct values from each other processor.
if (libMesh::n_processors() > 1)
{
this->set_nonlocal_dof_objects(mesh.nodes_begin(),
mesh.nodes_end(),
mesh, &DofMap::node_ptr);
this->set_nonlocal_dof_objects(mesh.elements_begin(),
mesh.elements_end(),
mesh, &DofMap::elem_ptr);
#ifdef DEBUG
MeshTools::libmesh_assert_valid_dof_ids(mesh);
#endif
}
// Set the total number of degrees of freedom
#ifdef LIBMESH_ENABLE_AMR
_n_old_dfs = _n_dfs;
#endif
_n_dfs = _end_df[n_proc-1];
STOP_LOG("distribute_dofs()", "DofMap");
// Note that in the add_neighbors_to_send_list nodes on processor
// boundaries that are shared by multiple elements are added for
// each element.
this->add_neighbors_to_send_list(mesh);
// Here we used to clean up that data structure; now System and
// EquationSystems call that for us, after we've added constraint
// dependencies to the send_list too.
// this->sort_send_list ();
}
void DofMap::distribute_local_dofs_node_major(unsigned int &next_free_dof,
MeshBase& mesh)
{
const unsigned int sys_num = this->sys_number();
const unsigned int n_var_groups = this->n_variable_groups();
//-------------------------------------------------------------------------
// First count and assign temporary numbers to local dofs
MeshBase::element_iterator elem_it = mesh.active_local_elements_begin();
const MeshBase::element_iterator elem_end = mesh.active_local_elements_end();
for ( ; elem_it != elem_end; ++elem_it)
{
// Only number dofs connected to active
// elements on this processor.
Elem* elem = *elem_it;
const unsigned int n_nodes = elem->n_nodes();
// First number the nodal DOFS
for (unsigned int n=0; n<n_nodes; n++)
{
Node* node = elem->get_node(n);
for (unsigned vg=0; vg<n_var_groups; vg++)
{
const VariableGroup &vg_description(this->variable_group(vg));
if( (vg_description.type().family != SCALAR) &&
(vg_description.active_on_subdomain(elem->subdomain_id())) )
{
// assign dof numbers (all at once) if this is
// our node and if they aren't already there
if ((node->n_comp_group(sys_num,vg) > 0) &&
(node->processor_id() == libMesh::processor_id()) &&
(node->vg_dof_base(sys_num,vg) ==
DofObject::invalid_id))
{
node->set_vg_dof_base(sys_num,
vg,
next_free_dof);
next_free_dof += (vg_description.n_variables()*
node->n_comp_group(sys_num,vg));
//node->debug_buffer();
}
}
}
}
// Now number the element DOFS
for (unsigned vg=0; vg<n_var_groups; vg++)
{
const VariableGroup &vg_description(this->variable_group(vg));
if ( (vg_description.type().family != SCALAR) &&
(vg_description.active_on_subdomain(elem->subdomain_id())) )
if (elem->n_comp_group(sys_num,vg) > 0)
{
libmesh_assert_equal_to (elem->vg_dof_base(sys_num,vg),
DofObject::invalid_id);
elem->set_vg_dof_base(sys_num,
vg,
next_free_dof);
next_free_dof += (vg_description.n_variables()*