-
Notifications
You must be signed in to change notification settings - Fork 707
/
affine_constraints.templates.h
4483 lines (3869 loc) · 167 KB
/
affine_constraints.templates.h
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
// ---------------------------------------------------------------------
//
// Copyright (C) 1999 - 2022 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, 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.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_affine_constraints_templates_h
#define dealii_affine_constraints_templates_h
#include <deal.II/base/config.h>
#include <deal.II/base/cuda_size.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/mpi_compute_index_owner_internal.h>
#include <deal.II/base/table.h>
#include <deal.II/base/thread_local_storage.h>
#include <deal.II/lac/affine_constraints.h>
#include <deal.II/lac/block_sparse_matrix.h>
#include <deal.II/lac/block_sparse_matrix_ez.h>
#include <deal.II/lac/block_sparsity_pattern.h>
#include <deal.II/lac/block_vector.h>
#include <deal.II/lac/chunk_sparse_matrix.h>
#include <deal.II/lac/diagonal_matrix.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/la_parallel_block_vector.h>
#include <deal.II/lac/la_parallel_vector.h>
#include <deal.II/lac/la_vector.h>
#include <deal.II/lac/matrix_block.h>
#include <deal.II/lac/petsc_block_sparse_matrix.h>
#include <deal.II/lac/petsc_block_vector.h>
#include <deal.II/lac/petsc_sparse_matrix.h>
#include <deal.II/lac/petsc_vector.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/sparse_matrix_ez.h>
#include <deal.II/lac/sparsity_pattern.h>
#include <deal.II/lac/trilinos_block_sparse_matrix.h>
#include <deal.II/lac/trilinos_parallel_block_vector.h>
#include <deal.II/lac/trilinos_sparse_matrix.h>
#include <deal.II/lac/trilinos_vector.h>
#include <boost/serialization/complex.hpp>
#include <boost/serialization/utility.hpp>
#include <algorithm>
#include <complex>
#include <iomanip>
#include <numeric>
#include <ostream>
#include <set>
DEAL_II_NAMESPACE_OPEN
template <typename number>
bool
AffineConstraints<number>::ConstraintLine::operator<(
const ConstraintLine &a) const
{
return index < a.index;
}
template <typename number>
bool
AffineConstraints<number>::ConstraintLine::operator==(
const ConstraintLine &a) const
{
return index == a.index;
}
template <typename number>
std::size_t
AffineConstraints<number>::ConstraintLine::memory_consumption() const
{
return (MemoryConsumption::memory_consumption(index) +
MemoryConsumption::memory_consumption(entries) +
MemoryConsumption::memory_consumption(inhomogeneity));
}
template <typename number>
const typename AffineConstraints<number>::LineRange
AffineConstraints<number>::get_lines() const
{
return boost::make_iterator_range(lines.begin(), lines.end());
}
template <typename number>
bool
AffineConstraints<number>::is_consistent_in_parallel(
const std::vector<IndexSet> &locally_owned_dofs,
const IndexSet & locally_active_dofs,
const MPI_Comm & mpi_communicator,
const bool verbose) const
{
// Helper to return a ConstraintLine object that belongs to row @p row.
// If @p row is not constrained or not stored locally, return an empty
// constraint object that would correspond to a zero constraints
auto get_line = [&](const size_type line_n) -> ConstraintLine {
const size_type line_index = calculate_line_index(line_n);
if (line_index >= lines_cache.size() ||
lines_cache[line_index] == numbers::invalid_size_type)
{
const ConstraintLine empty = {line_n, {}, 0.0};
return empty;
}
else
return lines[lines_cache[line_index]];
};
// identify non-owned rows and send to owner:
std::map<unsigned int, std::vector<ConstraintLine>> to_send;
const unsigned int myid =
dealii::Utilities::MPI::this_mpi_process(mpi_communicator);
const unsigned int nproc =
dealii::Utilities::MPI::n_mpi_processes(mpi_communicator);
// We will send all locally active dofs that are not locally owned for
// checking. Note that we allow constraints to differ on locally_relevant (and
// not active) DoFs.
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
IndexSet non_owned = locally_active_dofs;
non_owned.subtract_set(locally_owned_dofs[myid]);
for (unsigned int owner = 0; owner < nproc; ++owner)
{
// find all lines to send to @p owner
IndexSet indices_to_send = non_owned & locally_owned_dofs[owner];
for (const auto row_idx : indices_to_send)
{
to_send[owner].emplace_back(get_line(row_idx));
}
}
const std::map<unsigned int, std::vector<ConstraintLine>> received =
Utilities::MPI::some_to_some(mpi_communicator, to_send);
unsigned int inconsistent = 0;
// from each processor:
for (const auto &kv : received)
{
// for each incoming line:
for (const auto &lineit : kv.second)
{
const ConstraintLine reference = get_line(lineit.index);
if (lineit.inhomogeneity != reference.inhomogeneity)
{
++inconsistent;
if (verbose)
std::cout << "Proc " << myid << " got line " << lineit.index
<< " from " << kv.first << " inhomogeneity "
<< lineit.inhomogeneity
<< " != " << reference.inhomogeneity << std::endl;
}
else if (lineit.entries != reference.entries)
{
++inconsistent;
if (verbose)
std::cout << "Proc " << myid << " got line " << lineit.index
<< " from " << kv.first << " with wrong values!"
<< std::endl;
}
}
}
const unsigned int total =
Utilities::MPI::sum(inconsistent, mpi_communicator);
if (verbose && total > 0 && myid == 0)
std::cout << total << " inconsistent lines discovered!" << std::endl;
return total == 0;
}
namespace internal
{
template <typename number>
std::vector<
std::tuple<types::global_dof_index,
number,
std::vector<std::pair<types::global_dof_index, number>>>>
compute_locally_relevant_constraints(
const dealii::AffineConstraints<number> &constraints_in,
const IndexSet & locally_owned_dofs,
const IndexSet & locally_relevant_dofs,
const MPI_Comm mpi_communicator)
{
using ConstraintType =
std::tuple<types::global_dof_index,
number,
std::vector<std::pair<types::global_dof_index, number>>>;
// The result vector filled step by step.
std::vector<ConstraintType> locally_relevant_constraints;
#ifndef DEAL_II_WITH_MPI
AssertThrow(false, ExcNotImplemented()); // one should not come here
(void)constraints_in;
(void)locally_owned_dofs;
(void)locally_relevant_dofs;
(void)mpi_communicator;
#else
const unsigned int my_rank =
Utilities::MPI::this_mpi_process(mpi_communicator);
// helper function
const auto sort_constraints = [&]() {
std::sort(locally_relevant_constraints.begin(),
locally_relevant_constraints.end(),
[](const auto &a, const auto &b) {
return std::get<0>(a) < std::get<0>(b);
});
locally_relevant_constraints.erase(
std::unique(locally_relevant_constraints.begin(),
locally_relevant_constraints.end(),
[](const auto &a, const auto &b) {
return std::get<0>(a) == std::get<0>(b);
}),
locally_relevant_constraints.end());
};
// 0) collect constrained indices of the current object
IndexSet constrained_indices(locally_owned_dofs.size());
std::vector<types::global_dof_index> constrained_indices_temp;
for (const auto &line : constraints_in.get_lines())
constrained_indices_temp.push_back(line.index);
constrained_indices.add_indices(constrained_indices_temp.begin(),
constrained_indices_temp.end());
// step 1: identify owners of constraints
std::vector<unsigned int> constrained_indices_owners(
constrained_indices.n_elements());
Utilities::MPI::internal::ComputeIndexOwner::ConsensusAlgorithmsPayload
constrained_indices_process(locally_owned_dofs,
constrained_indices,
mpi_communicator,
constrained_indices_owners,
true);
Utilities::MPI::ConsensusAlgorithms::Selector<
std::vector<std::pair<types::global_dof_index, types::global_dof_index>>,
std::vector<unsigned int>>
consensus_algorithm;
consensus_algorithm.run(constrained_indices_process, mpi_communicator);
// step 2: collect all locally owned constraints
const auto constrained_indices_by_ranks =
constrained_indices_process.get_requesters();
{
const unsigned int tag = Utilities::MPI::internal::Tags::
affine_constraints_make_consistent_in_parallel_0;
// ... collect data and sort according to owner
std::map<unsigned int, std::vector<ConstraintType>> send_data_temp;
for (unsigned int i = 0; i < constrained_indices_owners.size(); ++i)
{
ConstraintType entry;
const types::global_dof_index index =
constrained_indices.nth_index_in_set(i);
std::get<0>(entry) = index;
if (constraints_in.is_inhomogeneously_constrained(index))
std::get<1>(entry) = constraints_in.get_inhomogeneity(index);
const auto constraints = constraints_in.get_constraint_entries(index);
if (constraints)
for (const auto &i : *constraints)
std::get<2>(entry).push_back(i);
if (constrained_indices_owners[i] == my_rank)
locally_relevant_constraints.push_back(entry);
else
send_data_temp[constrained_indices_owners[i]].push_back(entry);
}
std::map<unsigned int, std::vector<char>> send_data;
for (const auto &i : send_data_temp)
send_data[i.first] = Utilities::pack(i.second, false);
std::vector<MPI_Request> requests;
requests.reserve(send_data.size());
// ... send data
for (const auto &i : send_data)
{
if (i.first == my_rank)
continue;
requests.push_back({});
const int ierr = MPI_Isend(i.second.data(),
i.second.size(),
MPI_CHAR,
i.first,
tag,
mpi_communicator,
&requests.back());
AssertThrowMPI(ierr);
}
// ... receive data
unsigned int n_rec_ranks = 0;
for (const auto &i : constrained_indices_by_ranks)
if (i.first != my_rank)
n_rec_ranks++;
for (unsigned int i = 0; i < n_rec_ranks; ++i)
{
MPI_Status status;
int ierr = MPI_Probe(MPI_ANY_SOURCE, tag, mpi_communicator, &status);
AssertThrowMPI(ierr);
int message_length;
ierr = MPI_Get_count(&status, MPI_CHAR, &message_length);
AssertThrowMPI(ierr);
std::vector<char> buffer(message_length);
ierr = MPI_Recv(buffer.data(),
buffer.size(),
MPI_CHAR,
status.MPI_SOURCE,
tag,
mpi_communicator,
MPI_STATUS_IGNORE);
AssertThrowMPI(ierr);
const auto data =
Utilities::unpack<std::vector<ConstraintType>>(buffer, false);
for (const auto &i : data)
locally_relevant_constraints.push_back(i);
}
const int ierr =
MPI_Waitall(requests.size(), requests.data(), MPI_STATUSES_IGNORE);
AssertThrowMPI(ierr);
sort_constraints();
}
// step 3: communicate constraints so that each process know how the
// locally locally relevant dofs are constrained
{
const unsigned int tag = Utilities::MPI::internal::Tags::
affine_constraints_make_consistent_in_parallel_1;
// ... determine owners of locally relevant dofs
IndexSet locally_relevant_dofs_non_local = locally_relevant_dofs;
locally_relevant_dofs_non_local.subtract_set(locally_owned_dofs);
std::vector<unsigned int> locally_relevant_dofs_owners(
locally_relevant_dofs_non_local.n_elements());
Utilities::MPI::internal::ComputeIndexOwner::ConsensusAlgorithmsPayload
locally_relevant_dofs_process(locally_owned_dofs,
locally_relevant_dofs_non_local,
mpi_communicator,
locally_relevant_dofs_owners,
true);
Utilities::MPI::ConsensusAlgorithms::Selector<
std::vector<
std::pair<types::global_dof_index, types::global_dof_index>>,
std::vector<unsigned int>>
consensus_algorithm;
consensus_algorithm.run(locally_relevant_dofs_process, mpi_communicator);
const auto locally_relevant_dofs_by_ranks =
locally_relevant_dofs_process.get_requesters();
std::map<unsigned int, std::vector<char>> send_data;
std::vector<MPI_Request> requests;
requests.reserve(send_data.size());
// ... send data
for (const auto &rank_and_indices : locally_relevant_dofs_by_ranks)
{
Assert(rank_and_indices.first != my_rank, ExcInternalError());
std::vector<ConstraintType> data;
for (const auto index : rank_and_indices.second)
{
// note: at this stage locally_relevant_constraints still
// contains only locally owned constraints
const auto prt =
std::find_if(locally_relevant_constraints.begin(),
locally_relevant_constraints.end(),
[index](const auto &a) {
return std::get<0>(a) == index;
});
if (prt != locally_relevant_constraints.end())
data.push_back(*prt);
}
send_data[rank_and_indices.first] = Utilities::pack(data, false);
requests.push_back({});
const int ierr = MPI_Isend(send_data[rank_and_indices.first].data(),
send_data[rank_and_indices.first].size(),
MPI_CHAR,
rank_and_indices.first,
tag,
mpi_communicator,
&requests.back());
AssertThrowMPI(ierr);
}
// ... receive data
const unsigned int n_rec_ranks = [&]() {
// count number of ranks from where data will be received from
// by looping locally_relevant_dofs_owners and identifying unique
// rank (ignoring the current rank)
std::set<unsigned int> rec_ranks;
for (const unsigned int rank : locally_relevant_dofs_owners)
if (rank != my_rank)
rec_ranks.insert(rank);
return rec_ranks.size();
}();
for (unsigned int counter = 0; counter < n_rec_ranks; ++counter)
{
MPI_Status status;
int ierr = MPI_Probe(MPI_ANY_SOURCE, tag, mpi_communicator, &status);
AssertThrowMPI(ierr);
int message_length;
ierr = MPI_Get_count(&status, MPI_CHAR, &message_length);
AssertThrowMPI(ierr);
std::vector<char> buffer(message_length);
ierr = MPI_Recv(buffer.data(),
buffer.size(),
MPI_CHAR,
status.MPI_SOURCE,
tag,
mpi_communicator,
MPI_STATUS_IGNORE);
AssertThrowMPI(ierr);
const auto received_locally_relevant_constrain =
Utilities::unpack<std::vector<ConstraintType>>(buffer, false);
for (const auto &locally_relevant_constrain :
received_locally_relevant_constrain)
locally_relevant_constraints.push_back(locally_relevant_constrain);
}
const int ierr =
MPI_Waitall(requests.size(), requests.data(), MPI_STATUSES_IGNORE);
AssertThrowMPI(ierr);
sort_constraints();
}
#endif
return locally_relevant_constraints;
}
} // namespace internal
template <typename number>
void
AffineConstraints<number>::make_consistent_in_parallel(
const IndexSet &locally_owned_dofs,
const IndexSet &locally_relevant_dofs,
const MPI_Comm mpi_communicator)
{
if (Utilities::MPI::job_supports_mpi() == false ||
Utilities::MPI::n_mpi_processes(mpi_communicator) == 1)
return; // nothing to do, since serial
Assert(sorted == false, ExcMatrixIsClosed());
// 1) get all locally relevant constraints ("temporal constraint matrix")
const auto temporal_constraint_matrix =
internal::compute_locally_relevant_constraints(*this,
locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
// 2) clear the content of this constraint matrix
lines.clear();
lines_cache.clear();
// 3) refill this constraint matrix
for (const auto &line : temporal_constraint_matrix)
{
const types::global_dof_index index = std::get<0>(line);
// ... line
this->add_line(index);
// ... inhomogeneity
if (std::get<1>(line) != number())
this->set_inhomogeneity(index, std::get<1>(line));
// ... entries
if (std::get<2>(line).size() > 0)
for (const auto &j : std::get<2>(line))
this->add_entry(index, j.first, j.second);
}
#ifdef DEBUG
Assert(this->is_consistent_in_parallel(
Utilities::MPI::all_gather(mpi_communicator, locally_owned_dofs),
locally_relevant_dofs,
mpi_communicator),
ExcInternalError());
#endif
}
template <typename number>
void
AffineConstraints<number>::add_lines(const std::set<size_type> &lines)
{
for (const size_type &line : lines)
add_line(line);
}
template <typename number>
void
AffineConstraints<number>::add_lines(const std::vector<bool> &lines)
{
for (size_type i = 0; i < lines.size(); ++i)
if (lines[i] == true)
add_line(i);
}
template <typename number>
void
AffineConstraints<number>::add_lines(const IndexSet &lines)
{
for (size_type i = 0; i < lines.n_elements(); ++i)
add_line(lines.nth_index_in_set(i));
}
template <typename number>
void
AffineConstraints<number>::add_entries(
const size_type constrained_dof_index,
const std::vector<std::pair<size_type, number>> &col_weight_pairs)
{
Assert(sorted == false, ExcMatrixIsClosed());
Assert(is_constrained(constrained_dof_index),
ExcLineInexistant(constrained_dof_index));
ConstraintLine &line =
lines[lines_cache[calculate_line_index(constrained_dof_index)]];
Assert(line.index == constrained_dof_index, ExcInternalError());
// if in debug mode, check whether an entry for this column already
// exists and if its the same as the one entered at present
//
// in any case: skip this entry if an entry for this column already
// exists, since we don't want to enter it twice
for (const std::pair<size_type, number> &col_weight_pair : col_weight_pairs)
{
Assert(constrained_dof_index != col_weight_pair.first,
ExcMessage("Can't constrain a degree of freedom to itself"));
bool entry_exists = false;
for (const std::pair<size_type, number> &entry : line.entries)
if (entry.first == col_weight_pair.first)
{
// entry exists, break innermost loop
Assert(entry.second == col_weight_pair.second,
ExcEntryAlreadyExists(constrained_dof_index,
col_weight_pair.first,
entry.second,
col_weight_pair.second));
entry_exists = true;
break;
}
if (entry_exists == false)
line.entries.push_back(col_weight_pair);
}
}
template <typename number>
void
AffineConstraints<number>::add_selected_constraints(
const AffineConstraints &constraints,
const IndexSet & filter)
{
if (constraints.n_constraints() == 0)
return;
Assert(filter.size() > constraints.lines.back().index,
ExcMessage(
"The filter must be larger than the given constraints object."));
for (const ConstraintLine &line : constraints.lines)
if (filter.is_element(line.index))
{
const size_type row = filter.index_within_set(line.index);
add_line(row);
set_inhomogeneity(row, line.inhomogeneity);
for (const std::pair<size_type, number> &entry : line.entries)
if (filter.is_element(entry.first))
add_entry(row, filter.index_within_set(entry.first), entry.second);
}
}
template <typename number>
void
AffineConstraints<number>::close()
{
if (sorted == true)
return;
// sort the lines
std::sort(lines.begin(), lines.end());
// update list of pointers and give the vector a sharp size since we
// won't modify the size any more after this point.
{
std::vector<size_type> new_lines(lines_cache.size(),
numbers::invalid_size_type);
size_type counter = 0;
for (const ConstraintLine &line : lines)
{
new_lines[calculate_line_index(line.index)] = counter;
++counter;
}
std::swap(lines_cache, new_lines);
}
// in debug mode: check whether we really set the pointers correctly.
for (size_type i = 0; i < lines_cache.size(); ++i)
if (lines_cache[i] != numbers::invalid_size_type)
Assert(i == calculate_line_index(lines[lines_cache[i]].index),
ExcInternalError());
// first, strip zero entries, as we have to do that only once
for (ConstraintLine &line : lines)
// first remove zero entries. that would mean that in the linear
// constraint for a node, x_i = ax_1 + bx_2 + ..., another node times 0
// appears. obviously, 0*something can be omitted
line.entries.erase(std::remove_if(line.entries.begin(),
line.entries.end(),
[](
const std::pair<size_type, number> &p) {
return p.second == number(0.);
}),
line.entries.end());
#ifdef DEBUG
// In debug mode we are computing an estimate for the maximum number
// of constraints so that we can bail out if there is a cycle in the
// constraints (which is easier than searching for cycles in the graph).
//
// Let us figure out the largest dof index. This is an upper bound for the
// number of constraints because it is an approximation for the number of dofs
// in our system.
size_type largest_idx = 0;
for (const ConstraintLine &line : lines)
for (const std::pair<size_type, number> &entry : line.entries)
largest_idx = std::max(largest_idx, entry.first);
#endif
// replace references to dofs that are themselves constrained. note that
// because we may replace references to other dofs that may themselves be
// constrained to third ones, we have to iterate over all this until we
// replace no chains of constraints any more
//
// the iteration replaces references to constrained degrees of freedom by
// second-order references. for example if x3=x0/2+x2/2 and x2=x0/2+x1/2,
// then the new list will be x3=x0/2+x0/4+x1/4. note that x0 appear
// twice. we will throw this duplicate out in the following step, where
// we sort the list so that throwing out duplicates becomes much more
// efficient. also, we have to do it only once, rather than in each
// iteration
#ifdef DEBUG
size_type iteration = 0;
#endif
while (true)
{
bool chained_constraint_replaced = false;
for (ConstraintLine &line : lines)
{
#ifdef DEBUG
// we need to keep track of how many replacements we do in this line,
// because we can end up in a cycle A->B->C->A without the number of
// entries growing.
size_type n_replacements = 0;
#endif
// loop over all entries of this line (including ones that we
// have appended in this go around) and see whether they are
// further constrained. ignore elements that we don't store on
// the current processor
size_type entry = 0;
while (entry < line.entries.size())
if (((local_lines.size() == 0) ||
(local_lines.is_element(line.entries[entry].first))) &&
is_constrained(line.entries[entry].first))
{
// ok, this entry is further constrained:
chained_constraint_replaced = true;
// look up the chain of constraints for this entry
const size_type dof_index = line.entries[entry].first;
const number weight = line.entries[entry].second;
Assert(dof_index != line.index,
ExcMessage("Cycle in constraints detected!"));
const ConstraintLine &constrained_line =
lines[lines_cache[calculate_line_index(dof_index)]];
Assert(constrained_line.index == dof_index, ExcInternalError());
// now we have to replace an entry by its expansion. we do
// that by overwriting the entry by the first entry of the
// expansion and adding the remaining ones to the end,
// where we will later process them once more
//
// we can of course only do that if the DoF that we are
// currently handle is constrained by a linear combination
// of other dofs:
if (constrained_line.entries.size() > 0)
{
for (size_type i = 0; i < constrained_line.entries.size();
++i)
Assert(dof_index != constrained_line.entries[i].first,
ExcMessage("Cycle in constraints detected!"));
// replace first entry, then tack the rest to the end
// of the list
line.entries[entry] = std::pair<size_type, number>(
constrained_line.entries[0].first,
constrained_line.entries[0].second * weight);
for (size_type i = 1; i < constrained_line.entries.size();
++i)
line.entries.emplace_back(
constrained_line.entries[i].first,
constrained_line.entries[i].second * weight);
#ifdef DEBUG
// keep track of how many entries we replace in this
// line. If we do more than there are constraints or
// dofs in our system, we must have a cycle.
++n_replacements;
Assert(n_replacements / 2 < largest_idx,
ExcMessage("Cycle in constraints detected!"));
if (n_replacements / 2 >= largest_idx)
return; // this enables us to test for this Exception.
#endif
}
else
// the DoF that we encountered is not constrained by a
// linear combination of other dofs but is equal to just
// the inhomogeneity (i.e. its chain of entries is
// empty). in that case, we can't just overwrite the
// current entry, but we have to actually eliminate it
{
line.entries.erase(line.entries.begin() + entry);
}
line.inhomogeneity += constrained_line.inhomogeneity * weight;
// now that we're here, do not increase index by one but
// rather make another pass for the present entry because
// we have replaced the present entry by another one, or
// because we have deleted it and shifted all following
// ones one forward
}
else
// entry not further constrained. just move ahead by one
++entry;
}
// if we didn't do anything in this round, then quit the loop
if (chained_constraint_replaced == false)
break;
#ifdef DEBUG
// increase iteration count. note that we should not iterate more
// times than there are constraints, since this puts a natural upper
// bound on the length of constraint chains
++iteration;
Assert(iteration <= lines.size(), ExcInternalError());
#endif
}
// finally sort the entries and re-scale them if necessary. in this step,
// we also throw out duplicates as mentioned above. moreover, as some
// entries might have had zero weights, we replace them by a vector with
// sharp sizes.
for (ConstraintLine &line : lines)
{
std::sort(line.entries.begin(),
line.entries.end(),
[](const std::pair<unsigned int, number> &a,
const std::pair<unsigned int, number> &b) -> bool {
// Let's use lexicogrpahic ordering with std::abs for number
// type (it might be complex valued).
return (a.first < b.first) ||
(a.first == b.first &&
std::abs(a.second) < std::abs(b.second));
});
// loop over the now sorted list and see whether any of the entries
// references the same dofs more than once in order to find how many
// non-duplicate entries we have. This lets us allocate the correct
// amount of memory for the constraint entries.
size_type duplicates = 0;
for (size_type i = 1; i < line.entries.size(); ++i)
if (line.entries[i].first == line.entries[i - 1].first)
duplicates++;
if (duplicates > 0 || line.entries.size() < line.entries.capacity())
{
typename ConstraintLine::Entries new_entries;
// if we have no duplicates, copy verbatim the entries. this way,
// the final size is of the vector is correct.
if (duplicates == 0)
new_entries = line.entries;
else
{
// otherwise, we need to go through the list and resolve the
// duplicates
new_entries.reserve(line.entries.size() - duplicates);
new_entries.push_back(line.entries[0]);
for (size_type j = 1; j < line.entries.size(); ++j)
if (line.entries[j].first == line.entries[j - 1].first)
{
Assert(new_entries.back().first == line.entries[j].first,
ExcInternalError());
new_entries.back().second += line.entries[j].second;
}
else
new_entries.push_back(line.entries[j]);
Assert(new_entries.size() == line.entries.size() - duplicates,
ExcInternalError());
// make sure there are really no duplicates left and that the
// list is still sorted
for (size_type j = 1; j < new_entries.size(); ++j)
{
Assert(new_entries[j].first != new_entries[j - 1].first,
ExcInternalError());
Assert(new_entries[j].first > new_entries[j - 1].first,
ExcInternalError());
}
}
// replace old list of constraints for this dof by the new one
line.entries.swap(new_entries);
}
// Finally do the following check: if the sum of weights for the
// constraints is close to one, but not exactly one, then rescale all
// the weights so that they sum up to 1. this adds a little numerical
// stability and avoids all sorts of problems where the actual value
// is close to, but not quite what we expected
//
// the case where the weights don't quite sum up happens when we
// compute the interpolation weights "on the fly", i.e. not from
// precomputed tables. in this case, the interpolation weights are
// also subject to round-off
number sum = 0.;
for (const std::pair<size_type, number> &entry : line.entries)
sum += entry.second;
if (std::abs(sum - number(1.)) < 1.e-13)
{
for (std::pair<size_type, number> &entry : line.entries)
entry.second /= sum;
line.inhomogeneity /= sum;
}
} // end of loop over all constraint lines
#ifdef DEBUG
// if in debug mode: check that no dof is constrained to another dof that
// is also constrained. exclude dofs from this check whose constraint
// lines are not stored on the local processor
for (const ConstraintLine &line : lines)
for (const std::pair<size_type, number> &entry : line.entries)
if ((local_lines.size() == 0) || (local_lines.is_element(entry.first)))
{
// make sure that entry->first is not the index of a line itself
const bool is_circle = is_constrained(entry.first);
Assert(is_circle == false,
ExcDoFConstrainedToConstrainedDoF(line.index, entry.first));
}
#endif
sorted = true;
}
template <typename number>
bool
AffineConstraints<number>::is_closed() const
{
return sorted || (n_constraints() == 0);
}
template <typename number>
bool
AffineConstraints<number>::is_closed(const MPI_Comm &comm) const
{
return Utilities::MPI::min(static_cast<unsigned int>(is_closed()), comm) == 1;
}
template <typename number>
void
AffineConstraints<number>::merge(
const AffineConstraints<number> &other_constraints,
const MergeConflictBehavior merge_conflict_behavior,
const bool allow_different_local_lines)
{
(void)allow_different_local_lines;
Assert(allow_different_local_lines ||
local_lines == other_constraints.local_lines,
ExcMessage(
"local_lines for this and the other objects are not the same "
"although allow_different_local_lines is false."));
// store the previous state with respect to sorting
const bool object_was_sorted = sorted;
sorted = false;
// first action is to fold into the present object possible constraints
// in the second object. we don't strictly need to do this any more since
// the AffineConstraints container has learned to deal with chains of
// constraints in the close() function, but we have traditionally done
// this and it's not overly hard to do.
//
// for this, loop over all constraints and replace the constraint lines
// with a new one where constraints are replaced if necessary.
typename ConstraintLine::Entries tmp;
for (ConstraintLine &line : lines)
{
tmp.clear();
for (const std::pair<size_type, number> &entry : line.entries)
{
// if the present dof is not stored, or not constrained, or if we
// won't take the constraint from the other object, then simply copy
// it over