Debian Codegraph @

include/deal.II/lac/affine_constraints.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1999 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_affine_constraints_templates_h
16 #define dealii_affine_constraints_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/memory_consumption.h>
21 #include <deal.II/base/parallel.h>
22 #include <deal.II/base/table.h>
23 #include <deal.II/base/thread_local_storage.h>
24 #include <deal.II/base/thread_management.h>
25 #include <deal.II/base/trilinos_utilities.h>
26
27 #include <deal.II/lac/affine_constraints.h>
28 #include <deal.II/lac/block_sparse_matrix.h>
29 #include <deal.II/lac/block_sparse_matrix_ez.h>
30 #include <deal.II/lac/block_sparsity_pattern.h>
31 #include <deal.II/lac/block_vector.h>
32 #include <deal.II/lac/chunk_sparse_matrix.h>
33 #include <deal.II/lac/diagonal_matrix.h>
34 #include <deal.II/lac/dynamic_sparsity_pattern.h>
35 #include <deal.II/lac/full_matrix.h>
36 #include <deal.II/lac/la_parallel_block_vector.h>
37 #include <deal.II/lac/la_parallel_vector.h>
38 #include <deal.II/lac/matrix_block.h>
39 #include <deal.II/lac/petsc_block_sparse_matrix.h>
40 #include <deal.II/lac/petsc_block_vector.h>
41 #include <deal.II/lac/petsc_sparse_matrix.h>
42 #include <deal.II/lac/petsc_vector.h>
43 #include <deal.II/lac/sparse_matrix.h>
44 #include <deal.II/lac/sparse_matrix_ez.h>
45 #include <deal.II/lac/sparsity_pattern.h>
46 #include <deal.II/lac/trilinos_block_sparse_matrix.h>
47 #include <deal.II/lac/trilinos_parallel_block_vector.h>
48 #include <deal.II/lac/trilinos_sparse_matrix.h>
49 #include <deal.II/lac/trilinos_tpetra_block_sparse_matrix.h>
50 #include <deal.II/lac/trilinos_tpetra_block_vector.h>
51 #include <deal.II/lac/trilinos_tpetra_sparse_matrix.h>
52 #include <deal.II/lac/trilinos_tpetra_vector.h>
53 #include <deal.II/lac/trilinos_vector.h>
54
55 #include <boost/container/small_vector.hpp>
56 #include <boost/range/iterator_range_core.hpp>
57 #include <boost/serialization/complex.hpp>
58 #include <boost/serialization/utility.hpp>
59
60 #include <Kokkos_Core.hpp>
61
62 #include <algorithm>
63 #include <complex>
64 #include <iomanip>
65 #include <numeric>
66 #include <ostream>
67 #include <set>
68
69 DEAL_II_NAMESPACE_OPEN
70
71
72
73 template <typename number>
74 std::size_t
75 AffineConstraints<number>::ConstraintLine::memory_consumption() const
76 {
77 return (MemoryConsumption::memory_consumption(index) +
78 MemoryConsumption::memory_consumption(entries) +
79 MemoryConsumption::memory_consumption(inhomogeneity));
80 }
81
82
83
84 template <typename number>
85 void
86 AffineConstraints<number>::add_constraint(
87 const size_type constrained_dof,
88 const ArrayView<const std::pair<size_type, number>> &dependencies,
89 const number inhomogeneity)
90 {
91 Assert(sorted == false, ExcMatrixIsClosed());
92 Assert(is_constrained(constrained_dof) == false,
93 ExcMessage("You cannot add a constraint for a degree of freedom "
94 "that is already constrained."));
95
96 // In debug mode, make sure that columns don't show up more than
97 // once. Do this by sorting an array of the column indices. Try to
98 // avoid memory allocation by using a vector type that allocates up
99 // to 25 entries on the stack -- this is enough for the constraints
100 // of Q4 elements in 3d, and so should cover the vast majority of
101 // cases. If we have a constraint with more dependencies, then
102 // that's just going to require a heap allocation.
103 if constexpr (running_in_debug_mode())
104 {
105 {
106 boost::container::small_vector<size_type, 25> column_indices;
107 column_indices.reserve(dependencies.size());
108 for (const auto &d : dependencies)
109 column_indices.emplace_back(d.first);
110 std::sort(column_indices.begin(), column_indices.end());
111 Assert(
112 std::adjacent_find(column_indices.begin(), column_indices.end()) ==
113 column_indices.end(),
114 ExcMessage(
115 "You are trying to insert a constraint that lists the same "
116 "degree of freedom more than once on the right hand side. This is "
117 "not allowed."));
118 }
119 }
120
121
122 // The following can happen when we compute with distributed meshes and dof
123 // handlers and we constrain a degree of freedom whose number we don't have
124 // locally. if we don't abort here the program will try to allocate several
125 // terabytes of memory to resize the various arrays below :-)
126 Assert(constrained_dof != numbers::invalid_size_type, ExcInternalError());
127
128 // if necessary enlarge vector of existing entries for cache
129 const size_type line_index = calculate_line_index(constrained_dof);
130 if (line_index >= lines_cache.size())
131 lines_cache.resize(std::max(2 * static_cast<size_type>(lines_cache.size()),
132 line_index + 1),
133 numbers::invalid_size_type);
134
135 // Push a new line to the end of the list and fill it with the
136 // provided information:
137 ConstraintLine &constraint = lines.emplace_back();
138 constraint.index = constrained_dof;
139 constraint.entries.reserve(dependencies.size());
140 for (const auto &[column, weight] : dependencies)
141 {
142 Assert(column != constrained_dof,
143 ExcMessage("You cannot constrain a degree of freedom against "
144 "itself."));
145 constraint.entries.emplace_back(column, weight);
146 }
147 constraint.inhomogeneity = inhomogeneity;
148
149 // Record the new constraint in the cache:
150 Assert(lines_cache[line_index] == numbers::invalid_size_type,
151 ExcInternalError());
152 lines_cache[line_index] = lines.size() - 1;
153 }
154
155
156
157 template <typename number>
158 void
159 AffineConstraints<number>::constrain_dof_to_zero(
160 const size_type constrained_dof)
161 {
162 Assert(sorted == false, ExcMatrixIsClosed());
163
164 // The following can happen when we compute with distributed meshes and dof
165 // handlers and we constrain a degree of freedom whose number we don't have
166 // locally. if we don't abort here the program will try to allocate several
167 // terabytes of memory to resize the various arrays below :-)
168 Assert(constrained_dof != numbers::invalid_size_type, ExcInternalError());
169
170 // if necessary enlarge vector of existing entries for cache
171 const size_type line_index = calculate_line_index(constrained_dof);
172 if (line_index >= lines_cache.size())
173 lines_cache.resize(std::max(2 * static_cast<size_type>(lines_cache.size()),
174 line_index + 1),
175 numbers::invalid_size_type);
176
177 // Let's check whether the DoF is already constrained. This is only allowed
178 // if it had previously been constrained to zero, and only then.
179 if (lines_cache[line_index] != numbers::invalid_size_type)
180 {
181 Assert(lines[lines_cache[line_index]].entries.empty() &&
182 (lines[lines_cache[line_index]].inhomogeneity == number(0.)),
183 ExcMessage("You cannot constrain a degree of freedom "
184 "to zero that is is already constrained to "
185 "something else."));
186 }
187 else
188 {
189 // Push a new line to the end of the list and fill it with the
190 // provided information:
191 ConstraintLine &constraint = lines.emplace_back();
192 constraint.index = constrained_dof;
193 constraint.inhomogeneity = 0.;
194
195 // Record the new constraint in the cache:
196 Assert(lines_cache[line_index] == numbers::invalid_size_type,
197 ExcInternalError());
198 lines_cache[line_index] = lines.size() - 1;
199 }
200 }
201
202
203
204 template <typename number>
205 typename AffineConstraints<number>::LineRange
206 AffineConstraints<number>::get_lines() const
207 {
208 return boost::make_iterator_range(lines.begin(), lines.end());
209 }
210
211
212
213 template <typename number>
214 bool
215 AffineConstraints<number>::is_consistent_in_parallel(
216 const std::vector<IndexSet> &locally_owned_dofs,
217 const IndexSet &locally_active_dofs,
218 const MPI_Comm mpi_communicator,
219 const bool verbose) const
220 {
221 // Helper to return a ConstraintLine object that belongs to row @p row.
222 // If @p row is not constrained or not stored locally, return an empty
223 // constraint object that would correspond to a zero constraints
224 auto get_line = [&](const size_type line_n) -> ConstraintLine {
225 const size_type line_index = calculate_line_index(line_n);
226 if (line_index >= lines_cache.size() ||
227 lines_cache[line_index] == numbers::invalid_size_type)
228 {
229 const ConstraintLine empty = {line_n, {}, 0.0};
230 return empty;
231 }
232 else
233 return lines[lines_cache[line_index]];
234 };
235
236 // identify non-owned rows and send to owner:
237 std::map<unsigned int, std::vector<ConstraintLine>> to_send;
238
239 const unsigned int myid =
240 dealii::Utilities::MPI::this_mpi_process(mpi_communicator);
241 const unsigned int nproc =
242 dealii::Utilities::MPI::n_mpi_processes(mpi_communicator);
243
244 // We will send all locally active dofs that are not locally owned for
245 // checking. Note that we allow constraints to differ on locally_relevant (and
246 // not active) DoFs.
247 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
248 IndexSet non_owned = locally_active_dofs;
249 non_owned.subtract_set(locally_owned_dofs[myid]);
250 for (unsigned int owner = 0; owner < nproc; ++owner)
251 {
252 // find all lines to send to @p owner
253 const IndexSet indices_to_send = non_owned & locally_owned_dofs[owner];
254 for (const auto row_idx : indices_to_send)
255 {
256 to_send[owner].emplace_back(get_line(row_idx));
257 }
258 }
259
260 const std::map<unsigned int, std::vector<ConstraintLine>> received =
261 Utilities::MPI::some_to_some(mpi_communicator, to_send);
262
263 unsigned int inconsistent = 0;
264
265 // from each processor:
266 for (const auto &[sender, constraints] : received)
267 {
268 // for each incoming line:
269 for (const ConstraintLine &constraint : constraints)
270 {
271 const ConstraintLine reference = get_line(constraint.index);
272
273 if (constraint.inhomogeneity != reference.inhomogeneity)
274 {
275 ++inconsistent;
276
277 if (verbose)
278 std::cout << "Proc " << myid << " got line " << constraint.index
279 << " from " << sender << " inhomogeneity "
280 << constraint.inhomogeneity
281 << " != " << reference.inhomogeneity << std::endl;
282 }
283 else if (constraint.entries != reference.entries)
284 {
285 ++inconsistent;
286 if (verbose)
287 std::cout << "Proc " << myid << " got line " << constraint.index
288 << " from " << sender << " with wrong values!"
289 << std::endl;
290 }
291 }
292 }
293
294 const unsigned int total =
295 Utilities::MPI::sum(inconsistent, mpi_communicator);
296 if (verbose && total > 0 && myid == 0)
297 std::cout << total << " inconsistent lines discovered!" << std::endl;
298 return total == 0;
299 }
300
301
302 namespace internal
303 {
304 // A helper function that sorts and normalizes the constraints
305 // provided through the function's argument:
306 template <typename number>
307 void
308 sort_and_make_unique(
309 std::vector<typename dealii::AffineConstraints<number>::ConstraintLine>
310 &constraints)
311 {
312 using ConstraintLine =
313 typename dealii::AffineConstraints<number>::ConstraintLine;
314
315 if (constraints.empty())
316 return;
317
318 // First sort the array of constraints by their index:
319 std::sort(constraints.begin(),
320 constraints.end(),
321 [](const ConstraintLine &l1, const ConstraintLine &l2) {
322 return l1.index < l2.index;
323 });
324
325 // It is possible that two processes have computed constraints
326 // for the same DoF differently (for example, in parallel because
327 // they have visited different faces of the same cell because
328 // on different processes, different neighbor cells are artificial.
329 //
330 // This is ok, as long as after resolution of all chains of
331 // constraints we end up with the same constraint. But at this
332 // point, we just don't know yet. We deal with this by simply
333 // dropping constraints for DoFs for which there is a previous
334 // constraint in the list:
335 constraints.erase(std::unique(constraints.begin(),
336 constraints.end(),
337 [](const ConstraintLine &a,
338 const ConstraintLine &b) {
339 return (a.index == b.index);
340 }),
341 constraints.end());
342
343 // For those constraints that survive, sort the right hand side arrays:
344 for (ConstraintLine &entry : constraints)
345 std::sort(entry.entries.begin(),
346 entry.entries.end(),
347 [](const auto &l1, const auto &l2) {
348 return l1.first < l2.first;
349 });
350 }
351
352 template <typename number>
353 void
354 compute_locally_relevant_constraints(
355 const dealii::AffineConstraints<number> &constraints_in,
356 const IndexSet &locally_owned_dofs,
357 const IndexSet &locally_relevant_dofs,
358 const MPI_Comm mpi_communicator,
359 const bool first_run,
360 std::vector<typename dealii::AffineConstraints<number>::ConstraintLine>
361 &locally_relevant_constraints)
362 {
363 // The result vector filled step by step.
364 using ConstraintLine =
365 typename dealii::AffineConstraints<number>::ConstraintLine;
366
367 #ifndef DEAL_II_WITH_MPI
368 AssertThrow(false, ExcNotImplemented()); // one should not come here
369 (void)constraints_in;
370 (void)locally_owned_dofs;
371 (void)locally_relevant_dofs;
372 (void)mpi_communicator;
373 (void)first_run;
374 (void)locally_relevant_constraints;
375 #else
376
377 [[maybe_unused]] const unsigned int my_rank =
378 Utilities::MPI::this_mpi_process(mpi_communicator);
379
380 // step 0: Collect the indices of constrained DoFs we know of:
381 IndexSet my_constraint_indices(locally_owned_dofs.size());
382 {
383 std::vector<types::global_dof_index> indices;
384 indices.reserve(constraints_in.n_constraints());
385 for (const ConstraintLine &line : constraints_in.get_lines())
386 if (locally_owned_dofs.is_element(line.index) == false)
387 indices.push_back(line.index);
388 std::sort(indices.begin(), indices.end());
389 my_constraint_indices.add_indices(indices.begin(), indices.end());
390 }
391
392 // step 1: Identify the owners of DoFs we know to be constrained but do
393 // not own.
394 const auto [owners_of_my_constraints, constrained_indices_by_ranks] =
395 Utilities::MPI::compute_index_owner_and_requesters(locally_owned_dofs,
396 my_constraint_indices,
397 mpi_communicator);
398
399 // step 2: Collect all locally owned constraints into a data structure
400 // that we can send to other processes that want to know
401 // about them.
402 if (first_run)
403 {
404 std::map<unsigned int, std::vector<ConstraintLine>> send_data;
405
406 // For each constraint we know of but owned by another process, create a
407 // copy of the constraint and add it to the list of things to send to
408 // other processes:
409 for (unsigned int constraint_index = 0;
410 constraint_index < owners_of_my_constraints.size();
411 ++constraint_index)
412 {
413 ConstraintLine entry;
414
415 const types::global_dof_index index =
416 my_constraint_indices.nth_index_in_set(constraint_index);
417
418 entry.index = index;
419 if (const std::vector<std::pair<types::global_dof_index, number>>
420 *constraints = constraints_in.get_constraint_entries(index))
421 entry.entries = *constraints;
422 entry.inhomogeneity = constraints_in.get_inhomogeneity(index);
423
424
425 Assert(owners_of_my_constraints[constraint_index] != my_rank,
426 ExcInternalError());
427 send_data[owners_of_my_constraints[constraint_index]].push_back(
428 entry);
429 }
430
431 // Now exchange this data between processes:
432 const std::map<unsigned int, std::vector<ConstraintLine>>
433 received_data =
434 Utilities::MPI::some_to_some(mpi_communicator, send_data);
435
436 // Finally join things with the constraints we know about and own
437 // ourselves, collate everything we received into one array, sort,
438 // and make it unique:
439 for (const ConstraintLine &line : constraints_in.get_lines())
440 if (locally_owned_dofs.is_element(line.index))
441 locally_relevant_constraints.push_back(line);
442 for (const auto &[rank, constraints] : received_data)
443 locally_relevant_constraints.insert(
444 locally_relevant_constraints.end(),
445 constraints.begin(),
446 constraints.end());
447
448 sort_and_make_unique<number>(locally_relevant_constraints);
449 }
450
451 // step 3: communicate constraints so that each process knows how the
452 // locally relevant dofs are constrained
453 {
454 // First determine the owners of those locally relevant dofs we
455 // don't own ourselves.
456 IndexSet locally_relevant_dofs_non_local = locally_relevant_dofs;
457 locally_relevant_dofs_non_local.subtract_set(locally_owned_dofs);
458
459 // We are, however, not actually interested in who owns a specific
460 // DoF we have among our locally-stored-but-not-locally-owned constraints.
461 // Rather, we want to know who requested information about our own
462 // locally-owned DoFs. That's because we will want to send our own
463 // locally-owned constraints to those processes for which these are
464 // in their own locally-relevant index sets.
465 const auto [_, requesters_and_requested_constraints] =
466 Utilities::MPI::compute_index_owner_and_requesters(
467 locally_owned_dofs,
468 locally_relevant_dofs_non_local,
469 mpi_communicator);
470
471 std::map<unsigned int, std::vector<ConstraintLine>> send_data;
472 for (const auto &[destination, requested_indices] :
473 requesters_and_requested_constraints)
474 {
475 Assert(destination != my_rank, ExcInternalError());
476 Assert(requested_indices.is_subset_of(locally_owned_dofs),
477 ExcInternalError());
478
479 std::vector<ConstraintLine> data;
480
481 IndexSet::ElementIterator i = requested_indices.begin();
482
483 typename std::vector<ConstraintLine>::const_iterator j =
484 std::lower_bound(locally_relevant_constraints.begin(),
485 locally_relevant_constraints.end(),
486 *i,
487 [&](const auto &a, const auto &b) {
488 return a.index < b;
489 });
490
491 while ((i != requested_indices.end()) &&
492 (j != locally_relevant_constraints.end()))
493 {
494 if (*i == j->index)
495 {
496 data.push_back(*j);
497 ++i;
498 ++j;
499 }
500 else if (*i < j->index)
501 ++i;
502 else
503 ++j;
504 }
505
506 send_data[destination] = std::move(data);
507 }
508
509 const std::map<unsigned int, std::vector<ConstraintLine>> received_data =
510 Utilities::MPI::some_to_some(mpi_communicator, send_data);
511
512 // Finally collate everything into one array, sort, and make it unique:
513 for (const auto &[sender, constraints] : received_data)
514 locally_relevant_constraints.insert(locally_relevant_constraints.end(),
515 constraints.begin(),
516 constraints.end());
517
518 sort_and_make_unique<number>(locally_relevant_constraints);
519 }
520
521 #endif
522 }
523 } // namespace internal
524
525
526
527 template <typename number>
528 void
529 AffineConstraints<number>::make_consistent_in_parallel(
530 const IndexSet &locally_owned_dofs,
531 const IndexSet &constraints_to_make_consistent_,
532 const MPI_Comm mpi_communicator)
533 {
534 // We need to resolve chains of constraints in this function, and that
535 // is exactly what close() does. So call this function at the beginning
536 // to make sure we start working with constraints that are already
537 // resolved to the degree possible before exchanging information.
538 // We do this here so that it is also done if we choose the early
539 // exit in the following statement.
540 if (sorted == false)
541 close();
542
543 if (Utilities::MPI::n_mpi_processes(mpi_communicator) == 1)
544 return; // Nothing to do, since serial.
545
546 Assert(this->local_lines.size() > 0,
547 ExcMessage(
548 "This functionality requires that the AffineConstraints object "
549 "knows for which degrees of freedom it can store constraints. "
550 "Please initialize this object with the corresponding index sets."));
551
552 // Container for indices that are constrained or that other indices are
553 // constrained against. Generously reserve memory for it, assuming the worst
554 // case that each constraint line involves two distinct DoF indices.
555 std::vector<types::global_dof_index> constrained_indices;
556 constrained_indices.reserve(2 * n_constraints());
557
558 // This IndexSet keeps track of the locally stored constraints on this
559 // AffineConstraints object: If we have received constrained indices that we
560 // don't know of, we need to expand our playing field.
561 IndexSet locally_stored_constraints;
562
563 // This IndexSet contains those DoFs about which we want to know all
564 // constraints. We will receive constraints for these DoFs against other DoFs,
565 // which might be constrained themselves. To successfully resolve all chains
566 // of constraints, we need to know the constraints of all these DoFs, and we
567 // keep track of the relevant DoFs here.
568 IndexSet constraints_to_make_consistent = constraints_to_make_consistent_;
569
570 // This IndexSet stores DoFs from the previous iteration. If the old and new
571 // index sets match, we have converged.
572 IndexSet constraints_made_consistent;
573
574 std::vector<typename dealii::AffineConstraints<number>::ConstraintLine>
575 imported_constraints;
576
577 const unsigned int max_iterations = 10;
578 unsigned int iteration_count = 0;
579 for (; iteration_count < max_iterations; ++iteration_count)
580 {
581 // 1) Get all locally relevant constraints we need to know about:
582 internal::compute_locally_relevant_constraints(
583 *this,
584 locally_owned_dofs,
585 constraints_to_make_consistent,
586 mpi_communicator,
587 iteration_count == 0,
588 imported_constraints);
589
590 // 2) Add untracked DoFs to the index sets.
591 constrained_indices.clear();
592 for (const auto &line : imported_constraints)
593 {
594 constrained_indices.push_back(line.index);
595 for (const auto &entry : line.entries)
596 constrained_indices.push_back(entry.first);
597 }
598 std::sort(constrained_indices.begin(), constrained_indices.end());
599
600 locally_stored_constraints = this->local_lines;
601 locally_stored_constraints.add_indices(constrained_indices.begin(),
602 constrained_indices.end());
603
604 constraints_made_consistent = constraints_to_make_consistent;
605 constraints_to_make_consistent.add_indices(constrained_indices.begin(),
606 constrained_indices.end());
607
608 // 3) Clear and refill this constraint matrix. Also resolve chains:
609 this->reinit(locally_owned_dofs, locally_stored_constraints);
610 for (const auto &line : imported_constraints)
611 this->add_constraint(line.index, line.entries, line.inhomogeneity);
612 this->close();
613
614 // 4) Stop loop if converged.
615 const bool constraints_converged =
616 (Utilities::MPI::min(
617 (constraints_to_make_consistent == constraints_made_consistent ? 1 :
618 0),
619 mpi_communicator) == 1);
620 if (constraints_converged)
621 break;
622 }
623
624 AssertThrow(iteration_count < max_iterations,
625 ExcMessage(
626 "make_consistent_in_parallel() did not converge after " +
627 Utilities::to_string(max_iterations) + " iterations."));
628
629 Assert(this->is_consistent_in_parallel(
630 Utilities::MPI::all_gather(mpi_communicator, locally_owned_dofs),
631 constraints_to_make_consistent,
632 mpi_communicator),
633 ExcInternalError());
634 }
635
636
637
638 template <typename number>
639 void
640 AffineConstraints<number>::add_lines(const std::set<size_type> &lines)
641 {
642 for (const size_type &line : lines)
643 add_line(line);
644 }
645
646
647
648 template <typename number>
649 void
650 AffineConstraints<number>::add_lines(const std::vector<bool> &lines)
651 {
652 for (size_type i = 0; i < lines.size(); ++i)
653 if (lines[i] == true)
654 add_line(i);
655 }
656
657
658
659 template <typename number>
660 void
661 AffineConstraints<number>::add_lines(const IndexSet &lines)
662 {
663 for (size_type i = 0; i < lines.n_elements(); ++i)
664 add_line(lines.nth_index_in_set(i));
665 }
666
667
668
669 template <typename number>
670 void
671 AffineConstraints<number>::add_entries(
672 const size_type constrained_dof_index,
673 const std::vector<std::pair<size_type, number>> &col_weight_pairs)
674 {
675 Assert(sorted == false, ExcMatrixIsClosed());
676 Assert(is_constrained(constrained_dof_index),
677 ExcLineInexistent(constrained_dof_index));
678
679 ConstraintLine &line =
680 lines[lines_cache[calculate_line_index(constrained_dof_index)]];
681 Assert(line.index == constrained_dof_index, ExcInternalError());
682
683 // if in debug mode, check whether an entry for this column already
684 // exists and if its the same as the one entered at present
685 //
686 // in any case: skip this entry if an entry for this column already
687 // exists, since we don't want to enter it twice
688 line.entries.reserve(line.entries.size() + col_weight_pairs.size());
689 for (const std::pair<size_type, number> &col_weight_pair : col_weight_pairs)
690 {
691 Assert(constrained_dof_index != col_weight_pair.first,
692 ExcMessage("Can't constrain a degree of freedom to itself"));
693 bool entry_exists = false;
694 for (const std::pair<size_type, number> &entry : line.entries)
695 if (entry.first == col_weight_pair.first)
696 {
697 // entry exists, break innermost loop
698 Assert(entry.second == col_weight_pair.second,
699 ExcEntryAlreadyExists(constrained_dof_index,
700 col_weight_pair.first,
701 entry.second,
702 col_weight_pair.second));
703 entry_exists = true;
704 break;
705 }
706
707 if (entry_exists == false)
708 line.entries.push_back(col_weight_pair);
709 }
710 }
711
712
713
714 template <typename number>
715 void
716 AffineConstraints<number>::add_selected_constraints(
717 const AffineConstraints &constraints,
718 const IndexSet &filter)
719 {
720 if (constraints.n_constraints() == 0)
721 return;
722
723 Assert(filter.size() > constraints.lines.back().index,
724 ExcMessage(
725 "The filter must be larger than the given constraints object."));
726 for (const ConstraintLine &line : constraints.lines)
727 if (filter.is_element(line.index))
728 {
729 const size_type row = filter.index_within_set(line.index);
730 add_line(row);
731 set_inhomogeneity(row, line.inhomogeneity);
732 for (const std::pair<size_type, number> &entry : line.entries)
733 if (filter.is_element(entry.first))
734 add_entry(row, filter.index_within_set(entry.first), entry.second);
735 }
736 }
737
738
739
740 template <typename number>
741 void
742 AffineConstraints<number>::close()
743 {
744 if (sorted == true)
745 return;
746
747 // sort the lines
748 std::sort(lines.begin(),
749 lines.end(),
750 [](const ConstraintLine &l1, const ConstraintLine &l2) {
751 return l1.index < l2.index;
752 });
753
754 // update list of pointers and give the vector a sharp size since we
755 // won't modify the size any more after this point.
756 {
757 std::vector<size_type> new_lines(lines_cache.size(),
758 numbers::invalid_size_type);
759 size_type counter = 0;
760 for (const ConstraintLine &line : lines)
761 {
762 new_lines[calculate_line_index(line.index)] = counter;
763 ++counter;
764 }
765 lines_cache = std::move(new_lines);
766 }
767
768 // in debug mode: check whether we really set the pointers correctly.
769 for (size_type i = 0; i < lines_cache.size(); ++i)
770 if (lines_cache[i] != numbers::invalid_size_type)
771 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
772 ExcInternalError());
773
774 // The second part is that we need to work on the individual lines.
775 // Let us start by stripping zero entries. That would mean that in the linear
776 // constraint for a node, x_i = ax_1 + bx_2 + ..., another node times 0
777 // appears. obviously, 0*something can be omitted.
778 //
779 // This can be done in parallel:
780 parallel::apply_to_subranges(
781 lines.begin(),
782 lines.end(),
783 [](const typename std::vector<ConstraintLine>::iterator begin,
784 const typename std::vector<ConstraintLine>::iterator end) {
785 for (ConstraintLine &line : boost::iterator_range<
786 typename std::vector<ConstraintLine>::iterator>(begin, end))
787 line.entries.erase(
788 std::remove_if(line.entries.begin(),
789 line.entries.end(),
790 [](const std::pair<size_type, number> &p) {
791 return p.second == number(0.);
792 }),
793 line.entries.end());
794 },
795 /* grainsize = */ 100);
796
797
798
799 // In debug mode we are computing an estimate for the maximum number
800 // of constraints so that we can bail out if there is a cycle in the
801 // constraints (which is easier than searching for cycles in the graph).
802 //
803 // Let us figure out the largest dof index. This is an upper bound for the
804 // number of constraints because it is an approximation for the number of dofs
805 // in our system.
806 [[maybe_unused]] size_type largest_idx = 0;
807 if constexpr (running_in_debug_mode())
808 {
809 for (const ConstraintLine &line : lines)
810 for (const std::pair<size_type, number> &entry : line.entries)
811 largest_idx = std::max(largest_idx, entry.first);
812 }
813
814 // replace references to dofs that are themselves constrained. note that
815 // because we may replace references to other dofs that may themselves be
816 // constrained to third ones, we have to iterate over all this until we
817 // replace no chains of constraints any more
818 //
819 // the iteration replaces references to constrained degrees of freedom by
820 // second-order references. for example if x3=x0/2+x2/2 and x2=x0/2+x1/2,
821 // then the new list will be x3=x0/2+x0/4+x1/4. note that x0 appear
822 // twice. we will throw this duplicate out in the following step, where
823 // we sort the list so that throwing out duplicates becomes much more
824 // efficient. also, we have to do it only once, rather than in each
825 // iteration
826 [[maybe_unused]] size_type iteration = 0;
827 bool chained_constraint_replaced = false;
828 std::vector<bool> line_finalized(lines.size(), false);
829 do
830 {
831 chained_constraint_replaced = false;
832 for (unsigned int line_index = 0; line_index < lines.size(); ++line_index)
833 if (line_finalized[line_index] == false)
834 {
835 ConstraintLine &line = lines[line_index];
836
837 // we need to keep track of how many replacements we do in this
838 // line, because we can end up in a cycle A->B->C->A without the
839 // number of entries growing.
840 [[maybe_unused]] size_type n_replacements = 0;
841
842 // loop over all entries of this line (including ones that we
843 // have appended in this go around) and see whether they are
844 // further constrained. ignore elements that we don't store on
845 // the current processor.
846 const unsigned int n_original_entries = line.entries.size();
847 bool has_sub_constraints = false;
848 const size_type lines_cache_size = lines_cache.size();
849 for (unsigned int entry = 0; entry < n_original_entries; ++entry)
850 {
851 const size_type dof_index =
852 calculate_line_index(line.entries[entry].first);
853
854 if (dof_index < lines_cache_size &&
855 lines_cache[dof_index] != numbers::invalid_size_type)
856 {
857 // Now we found an index that is further constrained, so
858 // we need to resolve the entries. We may replace some of
859 // them, and we may add some, but we only walk through the
860 // original entries (and also don't touch the
861 // replacements) even though in principle we could also do
862 // the ones we have added or replaced. There is no harm in
863 // doing so: we will simply treat those in the next round
864 // around of the outer iteration.
865 has_sub_constraints = true;
866
867 const number weight = line.entries[entry].second;
868 Assert(line.entries[entry].first != line.index,
869 ExcMessage("Cycle in constraints detected!"));
870
871 const ConstraintLine &constrained_line =
872 lines[lines_cache[dof_index]];
873 Assert(constrained_line.index == line.entries[entry].first,
874 ExcInternalError());
875
876 // now we have to replace an entry by its expansion. we do
877 // that by overwriting the entry by the first entry of the
878 // expansion and adding the remaining ones to the end,
879 // where we will later process them once more
880 //
881 // we can of course only do that if the DoF that we are
882 // currently handling is constrained by a linear combination
883 // of other dofs:
884 if (constrained_line.entries.size() > 0)
885 {
886 for (size_type i = 0;
887 i < constrained_line.entries.size();
888 ++i)
889 Assert(line.entries[entry].first !=
890 constrained_line.entries[i].first,
891 ExcMessage("Cycle in constraints detected!"));
892
893 // replace first entry, then tack the rest to the end
894 // of the list
895 line.entries[entry] = std::pair<size_type, number>(
896 constrained_line.entries[0].first,
897 constrained_line.entries[0].second * weight);
898
899 for (size_type i = 1;
900 i < constrained_line.entries.size();
901 ++i)
902 line.entries.emplace_back(
903 constrained_line.entries[i].first,
904 constrained_line.entries[i].second * weight);
905
906 if constexpr (library_build_mode ==
907 LibraryBuildMode::debug)
908 {
909 // keep track of how many entries we replace in this
910 // line. If we do more than there are constraints or
911 // dofs in our system, we must have a cycle.
912 ++n_replacements;
913 Assert(n_replacements / 2 < largest_idx,
914 ExcMessage(
915 "Cycle in constraints detected!"));
916 }
917 }
918 else
919 // the DoF that we encountered is not constrained by a
920 // linear combination of other dofs but is equal to just
921 // the inhomogeneity (i.e. its chain of entries is
922 // empty). in that case, we can't just overwrite the
923 // current entry, but we have to actually eliminate
924 // it. we do not want to change the loop length above we
925 // do so by setting the 'first' entry to
926 // invalid_size_type here to finally remove entries in a
927 // second loop
928 {
929 line.entries[entry].first = numbers::invalid_size_type;
930 }
931
932 line.inhomogeneity +=
933 constrained_line.inhomogeneity * weight;
934 }
935 }
936
937 // If none of the entries in the current line refer to DoFs that are
938 // themselves constrained, then we can move on:
939 if (has_sub_constraints == false)
940 line_finalized[line_index] = true;
941 else
942 {
943 chained_constraint_replaced = true;
944
945 // Now delete the elements we have marked for deletion.
946 auto remaining_entries = line.entries.begin();
947 for (const auto &entry : line.entries)
948 if (entry.first != numbers::invalid_size_type)
949 {
950 *remaining_entries = entry;
951 ++remaining_entries;
952 }
953 line.entries.erase(remaining_entries, line.entries.end());
954 }
955 }
956
957 if constexpr (running_in_debug_mode())
958 {
959 // increase iteration count. note that we should not iterate more
960 // times than there are constraints, since this puts a natural upper
961 // bound on the length of constraint chains
962 ++iteration;
963 Assert(iteration <= lines.size() + 1, ExcInternalError());
964 }
965 }
966 while (chained_constraint_replaced == true);
967
968 // Finally sort the entries and re-scale them if necessary. in this step,
969 // we also throw out duplicates as mentioned above. moreover, as some
970 // entries might have had zero weights, we replace them by a vector with
971 // sharp sizes.
972 //
973 // This is again an operation that works on each line separately. It can be
974 // run in parallel:
975 parallel::apply_to_subranges(
976 lines.begin(),
977 lines.end(),
978 [](const typename std::vector<ConstraintLine>::iterator &begin,
979 const typename std::vector<ConstraintLine>::iterator &end) {
980 for (ConstraintLine &line : boost::iterator_range<
981 typename std::vector<ConstraintLine>::iterator>(begin, end))
982 {
983 unsigned int duplicates = 0;
984 bool is_sorted_without_duplicates = true;
985 for (unsigned int i = 1; i < line.entries.size(); ++i)
986 if (!(line.entries[i - 1].first < line.entries[i].first))
987 {
988 is_sorted_without_duplicates = false;
989 break;
990 }
991 if (is_sorted_without_duplicates == false)
992 {
993 std::sort(line.entries.begin(),
994 line.entries.end(),
995 [](const std::pair<unsigned int, number> &a,
996 const std::pair<unsigned int, number> &b) -> bool {
997 // Just look at the index, ignore the value.
998 return a.first < b.first;
999 });
1000
1001 // loop over the now sorted list and see whether any of the
1002 // entries references the same dofs more than once in order to
1003 // find how many non-duplicate entries we have. This lets us
1004 // allocate the correct amount of memory for the constraint
1005 // entries.
1006 for (size_type i = 1; i < line.entries.size(); ++i)
1007 if (line.entries[i].first == line.entries[i - 1].first)
1008 ++duplicates;
1009 }
1010
1011 if (duplicates > 0 || (line.entries.size() < line.entries.capacity()))
1012 {
1013 typename ConstraintLine::Entries new_entries;
1014
1015 // if we have no duplicates, copy verbatim the entries. this way,
1016 // the final size is of the vector is correct.
1017 if (duplicates == 0)
1018 new_entries = line.entries;
1019 else
1020 {
1021 // otherwise, we need to go through the list and resolve the
1022 // duplicates
1023 new_entries.reserve(line.entries.size() - duplicates);
1024 new_entries.push_back(line.entries[0]);
1025 for (size_type j = 1; j < line.entries.size(); ++j)
1026 if (line.entries[j].first == line.entries[j - 1].first)
1027 {
1028 Assert(new_entries.back().first ==
1029 line.entries[j].first,
1030 ExcInternalError());
1031 new_entries.back().second += line.entries[j].second;
1032 }
1033 else
1034 new_entries.push_back(line.entries[j]);
1035
1036 Assert(new_entries.size() == line.entries.size() - duplicates,
1037 ExcInternalError());
1038
1039 // make sure there are really no duplicates left and that the
1040 // list is still sorted
1041 for (size_type j = 1; j < new_entries.size(); ++j)
1042 {
1043 Assert(new_entries[j].first != new_entries[j - 1].first,
1044 ExcInternalError());
1045 Assert(new_entries[j].first > new_entries[j - 1].first,
1046 ExcInternalError());
1047 }
1048 }
1049
1050 // replace old list of constraints for this dof by the new one
1051 line.entries.swap(new_entries);
1052 }
1053
1054 // Finally do the following check: if the sum of weights for the
1055 // constraints is close to one, but not exactly one, then rescale all
1056 // the weights so that they sum up to 1. this adds a little numerical
1057 // stability and avoids all sorts of problems where the actual value
1058 // is close to, but not quite what we expected
1059 //
1060 // the case where the weights don't quite sum up happens when we
1061 // compute the interpolation weights "on the fly", i.e. not from
1062 // precomputed tables. in this case, the interpolation weights are
1063 // also subject to round-off
1064 number sum = 0.;
1065 for (const std::pair<size_type, number> &entry : line.entries)
1066 sum += entry.second;
1067 if (std::abs(sum - number(1.)) < 1.e-13 &&
1068 std::abs(sum - number(1.)) > 0.)
1069 {
1070 const number inverse_sum = number(1.) / sum;
1071 for (std::pair<size_type, number> &entry : line.entries)
1072 entry.second *= inverse_sum;
1073 line.inhomogeneity *= inverse_sum;
1074 }
1075 }
1076 },
1077 /* grainsize = */ 100);
1078
1079 // if in debug mode: check that no dof is constrained to another dof that
1080 // is also constrained. exclude dofs from this check whose constraint
1081 // lines are not stored on the local processor
1082 Assert(std::none_of(lines.begin(),
1083 lines.end(),
1084 [this](const ConstraintLine &line) {
1085 for (const std::pair<size_type, number> &entry :
1086 line.entries)
1087 if ((local_lines.size() == 0) ||
1088 (local_lines.is_element(entry.first)))
1089 {
1090 // make sure that entry->first is not the index of
1091 // a line itself
1092 const bool is_cycle = is_constrained(entry.first);
1093 if (is_cycle)
1094 return true;
1095 }
1096 return false;
1097 }),
1098 ExcMessage("The constraints represented by this object have a cycle. "
1099 "This is not allowed."));
1100
1101 // Now also compute which indices we need in distribute().
1102 //
1103 // This processor owns only part of the vectors we will work on. One may think
1104 // that every processor should be able to simply communicate those elements it
1105 // owns and for which it knows that they act as sources to constrained DoFs to
1106 // the owner of these DoFs. This would lead to a scheme where all we need to
1107 // do is to add some local elements to (possibly non-local) ones and then call
1108 // compress().
1109 //
1110 // Alas, this scheme does not work as evidenced by the disaster of
1111 // bug #51 (originally stored in the Google Code repository, but now
1112 // unfortunately no longer available because that platform has gone
1113 // away) and reversion of one attempt that implements this in svn
1114 // r29662. Rather, we need to get a vector that has all the
1115 // *sources* or constraints we own locally, possibly as ghost vector
1116 // elements, then read from them, and finally throw away the ghosted
1117 // vector. Implement this in the following.
1118 needed_elements_for_distribute = locally_owned_dofs;
1119
1120 if (needed_elements_for_distribute !=
1121 complete_index_set(locally_owned_dofs.size()))
1122 {
1123 std::vector<types::global_dof_index> additional_elements;
1124 for (const ConstraintLine &line : lines)
1125 if (locally_owned_dofs.is_element(line.index))
1126 for (const std::pair<size_type, number> &entry : line.entries)
1127 if (!locally_owned_dofs.is_element(entry.first))
1128 additional_elements.emplace_back(entry.first);
1129 std::sort(additional_elements.begin(), additional_elements.end());
1130 needed_elements_for_distribute.add_indices(additional_elements.begin(),
1131 additional_elements.end());
1132 }
1133
1134 sorted = true;
1135 }
1136
1137
1138
1139 template <typename number>
1140 bool
1141 AffineConstraints<number>::is_closed() const
1142 {
1143 return sorted || (n_constraints() == 0);
1144 }
1145
1146
1147
1148 template <typename number>
1149 bool
1150 AffineConstraints<number>::is_closed(const MPI_Comm comm) const
1151 {
1152 return Utilities::MPI::min(static_cast<unsigned int>(is_closed()), comm) == 1;
1153 }
1154
1155
1156
1157 template <typename number>
1158 void
1159 AffineConstraints<number>::shift(const size_type offset)
1160 {
1161 if (local_lines.size() == 0)
1162 lines_cache.insert(lines_cache.begin(), offset, numbers::invalid_size_type);
1163 else
1164 {
1165 // shift local_lines
1166 IndexSet new_local_lines(local_lines.size() + offset);
1167 new_local_lines.add_indices(local_lines, offset);
1168 std::swap(local_lines, new_local_lines);
1169 }
1170
1171 for (ConstraintLine &line : lines)
1172 {
1173 line.index += offset;
1174 for (std::pair<size_type, number> &entry : line.entries)
1175 entry.first += offset;
1176 }
1177
1178 if constexpr (running_in_debug_mode())
1179 {
1180 // make sure that lines, lines_cache and local_lines
1181 // are still linked correctly
1182 for (size_type index = 0; index < lines_cache.size(); ++index)
1183 Assert(lines_cache[index] == numbers::invalid_size_type ||
1184 calculate_line_index(lines[lines_cache[index]].index) == index,
1185 ExcInternalError());
1186 }
1187 }
1188
1189
1190
1191 template <typename number>
1192 AffineConstraints<number>
1193 AffineConstraints<number>::get_view(const IndexSet &mask) const
1194 {
1195 Assert(sorted == true, ExcMatrixNotClosed());
1196
1197 AffineConstraints<number> view;
1198
1199 // If index sets were associated with the current object, take views
1200 // of those as well:
1201 if (locally_owned_dofs != IndexSet())
1202 {
1203 Assert(locally_owned_dofs.size() == mask.size(),
1204 ExcMessage("This operation only makes sense if the index "
1205 "space described by the mask is the same as "
1206 "the index space associated with the "
1207 "AffineConstraints object into which you take "
1208 "a view."));
1209 Assert(local_lines.size() == mask.size(),
1210 ExcMessage("This operation only makes sense if the index "
1211 "space described by the mask is the same as "
1212 "the index space associated with the "
1213 "AffineConstraints object into which you take "
1214 "a view."));
1215
1216 view.reinit(locally_owned_dofs.get_view(mask),
1217 local_lines.get_view(mask));
1218 }
1219
1220 for (const ConstraintLine &line : lines)
1221 if (mask.is_element(line.index))
1222 {
1223 if constexpr (running_in_debug_mode())
1224 {
1225 for (const std::pair<size_type, number> &entry : line.entries)
1226 {
1227 Assert(
1228 mask.is_element(entry.first),
1229 ExcMessage(
1230 "In creating a view of an AffineConstraints "
1231 "object, the constraint on degree of freedom " +
1232 std::to_string(line.index) + " (which corresponds to the " +
1233 std::to_string(mask.index_within_set(line.index)) +
1234 "th degree of freedom selected in the mask) "
1235 "is constrained against degree of freedom " +
1236 std::to_string(entry.first) +
1237 ", but this degree of freedom is not listed in the mask and "
1238 "consequently cannot be transcribed into the index space "
1239 "of the output object."));
1240 }
1241 }
1242
1243 std::vector<std::pair<size_type, number>> translated_entries =
1244 line.entries;
1245 for (auto &entry : translated_entries)
1246 entry.first = mask.index_within_set(entry.first);
1247
1248 view.add_constraint(mask.index_within_set(line.index),
1249 translated_entries,
1250 line.inhomogeneity);
1251 }
1252
1253 view.close();
1254 return view;
1255 }
1256
1257
1258
1259 template <typename number>
1260 void
1261 AffineConstraints<number>::clear()
1262 {
1263 {
1264 std::vector<ConstraintLine> tmp;
1265 lines.swap(tmp);
1266 }
1267
1268 {
1269 std::vector<size_type> tmp;
1270 lines_cache.swap(tmp);
1271 }
1272
1273 locally_owned_dofs = {};
1274 local_lines = {};
1275 needed_elements_for_distribute = {};
1276
1277 sorted = false;
1278 }
1279
1280
1281
1282 template <typename number>
1283 void
1284 AffineConstraints<number>::reinit()
1285 {
1286 reinit(IndexSet(), IndexSet());
1287 }
1288
1289
1290
1291 template <typename number>
1292 void
1293 AffineConstraints<number>::reinit(const IndexSet &locally_stored_constraints)
1294 {
1295 reinit(locally_stored_constraints, locally_stored_constraints);
1296 }
1297
1298
1299
1300 template <typename number>
1301 void
1302 AffineConstraints<number>::reinit(const IndexSet &locally_owned_dofs,
1303 const IndexSet &locally_stored_constraints)
1304 {
1305 // First clear previous content
1306 clear();
1307
1308 // Then set the objects that describe the index sets of DoFs we care about:
1309 Assert(locally_owned_dofs.is_subset_of(locally_stored_constraints),
1310 ExcMessage("The set of locally stored constraints needs to be a "
1311 "superset of the locally owned DoFs."));
1312
1313 this->locally_owned_dofs = locally_owned_dofs;
1314 this->local_lines = locally_stored_constraints;
1315
1316 // make sure the IndexSet is compressed. Otherwise this can lead to crashes
1317 // that are hard to find (only happen in release mode).
1318 // see tests/mpi/affine_constraints_crash_01
1319 this->locally_owned_dofs.compress();
1320 this->local_lines.compress();
1321 }
1322
1323
1324
1325 template <typename number>
1326 bool
1327 AffineConstraints<number>::is_identity_constrained(const size_type line_n) const
1328 {
1329 if (is_constrained(line_n) == false)
1330 return false;
1331
1332 const ConstraintLine &line = lines[lines_cache[calculate_line_index(line_n)]];
1333 Assert(line.index == line_n, ExcInternalError());
1334
1335 // return if an entry for this line was found and if it has only one
1336 // entry equal to 1.0
1337 return (line.entries.size() == 1) && (line.entries[0].second == number(1.0));
1338 }
1339
1340
1341 template <typename number>
1342 bool
1343 AffineConstraints<number>::are_identity_constrained(
1344 const size_type line_n_1,
1345 const size_type line_n_2) const
1346 {
1347 if (is_constrained(line_n_1) == true)
1348 {
1349 const ConstraintLine &line =
1350 lines[lines_cache[calculate_line_index(line_n_1)]];
1351 Assert(line.index == line_n_1, ExcInternalError());
1352
1353 // return if an entry for this line was found and if it has only one
1354 // entry equal to 1.0 and that one is index2
1355 return ((line.entries.size() == 1) &&
1356 (line.entries[0].first == line_n_2) &&
1357 (line.entries[0].second == number(1.0)));
1358 }
1359 else if (is_constrained(line_n_2) == true)
1360 {
1361 const ConstraintLine &line =
1362 lines[lines_cache[calculate_line_index(line_n_2)]];
1363 Assert(line.index == line_n_2, ExcInternalError());
1364
1365 // return if an entry for this line was found and if it has only one
1366 // entry equal to 1.0 and that one is line_n_1
1367 return ((line.entries.size() == 1) &&
1368 (line.entries[0].first == line_n_1) &&
1369 (line.entries[0].second == number(1.0)));
1370 }
1371 else
1372 return false;
1373 }
1374
1375
1376
1377 template <typename number>
1378 typename AffineConstraints<number>::size_type
1379 AffineConstraints<number>::max_constraint_indirections() const
1380 {
1381 size_type return_value = 0;
1382 for (const ConstraintLine &line : lines)
1383 // use static cast, since typeof(size)==std::size_t, which is !=
1384 // size_type on AIX
1385 return_value =
1386 std::max(return_value, static_cast<size_type>(line.entries.size()));
1387
1388 return return_value;
1389 }
1390
1391
1392
1393 template <typename number>
1394 bool
1395 AffineConstraints<number>::has_inhomogeneities() const
1396 {
1397 for (const ConstraintLine &line : lines)
1398 if (line.inhomogeneity != number(0.))
1399 return true;
1400
1401 return false;
1402 }
1403
1404
1405
1406 template <typename number>
1407 void
1408 AffineConstraints<number>::print(std::ostream &out) const
1409 {
1410 for (const ConstraintLine &line : lines)
1411 {
1412 // output the list of constraints as pairs of dofs and their weights
1413 if (line.entries.size() > 0)
1414 {
1415 for (const std::pair<size_type, number> &entry : line.entries)
1416 out << " " << line.index << ' ' << entry.first << ": "
1417 << entry.second << '\n';
1418
1419 // print out inhomogeneity.
1420 if (line.inhomogeneity != number(0.))
1421 out << " " << line.index << ": " << line.inhomogeneity << '\n';
1422 }
1423 else
1424 // but also output something if the constraint simply reads
1425 // x[13]=0, i.e. where the right hand side is not a linear
1426 // combination of other dofs
1427 {
1428 if (line.inhomogeneity != number(0.))
1429 out << " " << line.index << " = " << line.inhomogeneity << '\n';
1430 else
1431 out << " " << line.index << " = 0\n";
1432 }
1433 }
1434
1435 AssertThrow(out.fail() == false, ExcIO());
1436 }
1437
1438
1439
1440 template <typename number>
1441 void
1442 AffineConstraints<number>::write_dot(std::ostream &out) const
1443 {
1444 out << "digraph constraints {" << std::endl;
1445 for (size_type i = 0; i != lines.size(); ++i)
1446 {
1447 // same concept as in the previous function
1448 if (lines[i].entries.size() > 0)
1449 for (size_type j = 0; j < lines[i].entries.size(); ++j)
1450 out << " " << lines[i].index << "->" << lines[i].entries[j].first
1451 << "; // weight: " << lines[i].entries[j].second << '\n';
1452 else
1453 out << " " << lines[i].index << '\n';
1454 }
1455 out << '}' << std::endl;
1456 }
1457
1458
1459
1460 template <typename number>
1461 std::size_t
1462 AffineConstraints<number>::memory_consumption() const
1463 {
1464 return (MemoryConsumption::memory_consumption(lines) +
1465 MemoryConsumption::memory_consumption(lines_cache) +
1466 MemoryConsumption::memory_consumption(sorted) +
1467 MemoryConsumption::memory_consumption(local_lines));
1468 }
1469
1470
1471
1472 template <typename number>
1473 void
1474 AffineConstraints<number>::resolve_indices(
1475 std::vector<types::global_dof_index> &indices) const
1476 {
1477 const unsigned int indices_size = indices.size();
1478 const std::vector<std::pair<types::global_dof_index, number>> *line_ptr;
1479 for (unsigned int i = 0; i < indices_size; ++i)
1480 {
1481 line_ptr = get_constraint_entries(indices[i]);
1482 // if the index is constraint, the constraints indices are added to the
1483 // indices vector
1484 if (line_ptr != nullptr)
1485 {
1486 const unsigned int line_size = line_ptr->size();
1487 for (unsigned int j = 0; j < line_size; ++j)
1488 indices.push_back((*line_ptr)[j].first);
1489 }
1490 }
1491
1492 // keep only the unique elements
1493 std::sort(indices.begin(), indices.end());
1494 indices.erase(std::unique(indices.begin(), indices.end()), indices.end());
1495 }
1496
1497
1498
1499 template <typename number>
1500 void
1501 AffineConstraints<number>::condense(SparsityPattern &sparsity) const
1502 {
1503 Assert(sorted == true, ExcMatrixNotClosed());
1504 Assert(sparsity.is_compressed() == false, ExcMatrixIsClosed());
1505 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
1506
1507 // store for each index whether it must be distributed or not. If entry
1508 // is numbers::invalid_unsigned_int, no distribution is necessary.
1509 // otherwise, the number states which line in the AffineConstraints object
1510 // handles this index
1511 std::vector<size_type> distribute(sparsity.n_rows(),
1512 numbers::invalid_size_type);
1513
1514 for (size_type c = 0; c < lines.size(); ++c)
1515 distribute[lines[c].index] = c;
1516
1517 const size_type n_rows = sparsity.n_rows();
1518 for (size_type row = 0; row < n_rows; ++row)
1519 {
1520 if (distribute[row] == numbers::invalid_size_type)
1521 {
1522 // regular line. loop over cols all valid cols. note that this
1523 // changes the line we are presently working on: we add additional
1524 // entries. these are put to the end of the row. however, as
1525 // constrained nodes cannot be constrained to other constrained
1526 // nodes, nothing will happen if we run into these added nodes, as
1527 // they can't be distributed further. we might store the position of
1528 // the last old entry and stop work there, but since operating on
1529 // the newly added ones only takes two comparisons (column index
1530 // valid, distribute[column] necessarily
1531 // ==numbers::invalid_size_type), it is cheaper to not do so and
1532 // run right until the end of the line
1533 for (SparsityPattern::iterator entry = sparsity.begin(row);
1534 ((entry != sparsity.end(row)) && entry->is_valid_entry());
1535 ++entry)
1536 {
1537 const size_type column = entry->column();
1538
1539 if (distribute[column] != numbers::invalid_size_type)
1540 {
1541 // distribute entry at regular row @p{row} and irregular
1542 // column sparsity.colnums[j]
1543 for (size_type q = 0;
1544 q != lines[distribute[column]].entries.size();
1545 ++q)
1546 sparsity.add(row,
1547 lines[distribute[column]].entries[q].first);
1548 }
1549 }
1550 }
1551 else
1552 // row must be distributed. note that here the present row is not
1553 // touched (unlike above)
1554 {
1555 for (SparsityPattern::iterator entry = sparsity.begin(row);
1556 (entry != sparsity.end(row)) && entry->is_valid_entry();
1557 ++entry)
1558 {
1559 const size_type column = entry->column();
1560 if (distribute[column] == numbers::invalid_size_type)
1561 // distribute entry at irregular row @p{row} and regular
1562 // column sparsity.colnums[j]
1563 for (size_type q = 0;
1564 q != lines[distribute[row]].entries.size();
1565 ++q)
1566 sparsity.add(lines[distribute[row]].entries[q].first, column);
1567 else
1568 // distribute entry at irregular row @p{row} and irregular
1569 // column sparsity.get_column_numbers()[j]
1570 for (size_type p = 0;
1571 p != lines[distribute[row]].entries.size();
1572 ++p)
1573 for (size_type q = 0;
1574 q != lines[distribute[column]].entries.size();
1575 ++q)
1576 sparsity.add(lines[distribute[row]].entries[p].first,
1577 lines[distribute[column]].entries[q].first);
1578 }
1579 }
1580 }
1581
1582 sparsity.compress();
1583 }
1584
1585
1586
1587 template <typename number>
1588 void
1589 AffineConstraints<number>::condense(BlockSparsityPattern &sparsity) const
1590 {
1591 Assert(sorted == true, ExcMatrixNotClosed());
1592 Assert(sparsity.is_compressed() == false, ExcMatrixIsClosed());
1593 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
1594 Assert(sparsity.n_block_rows() == sparsity.n_block_cols(), ExcNotQuadratic());
1595 Assert(sparsity.get_column_indices() == sparsity.get_row_indices(),
1596 ExcNotQuadratic());
1597
1598 const BlockIndices &index_mapping = sparsity.get_column_indices();
1599
1600 const size_type n_blocks = sparsity.n_block_rows();
1601
1602 // store for each index whether it must be distributed or not. If entry
1603 // is numbers::invalid_unsigned_int, no distribution is necessary.
1604 // otherwise, the number states which line in the AffineConstraints object
1605 // handles this index
1606 std::vector<size_type> distribute(sparsity.n_rows(),
1607 numbers::invalid_size_type);
1608
1609 for (size_type c = 0; c < lines.size(); ++c)
1610 distribute[lines[c].index] = c;
1611
1612 const size_type n_rows = sparsity.n_rows();
1613 for (size_type row = 0; row < n_rows; ++row)
1614 {
1615 // get index of this row within the blocks
1616 const std::pair<size_type, size_type> block_index =
1617 index_mapping.global_to_local(row);
1618 const size_type block_row = block_index.first;
1619
1620 if (distribute[row] == numbers::invalid_size_type)
1621 // regular line. loop over all columns and see whether this column
1622 // must be distributed
1623 {
1624 // to loop over all entries in this row, we have to loop over all
1625 // blocks in this blockrow and the corresponding row therein
1626 for (size_type block_col = 0; block_col < n_blocks; ++block_col)
1627 {
1628 const SparsityPattern &block_sparsity =
1629 sparsity.block(block_row, block_col);
1630
1631 for (SparsityPattern::const_iterator entry =
1632 block_sparsity.begin(block_index.second);
1633 (entry != block_sparsity.end(block_index.second)) &&
1634 entry->is_valid_entry();
1635 ++entry)
1636 {
1637 const size_type global_col =
1638 index_mapping.local_to_global(block_col, entry->column());
1639
1640 if (distribute[global_col] != numbers::invalid_size_type)
1641 // distribute entry at regular row @p{row} and
1642 // irregular column global_col
1643 {
1644 for (size_type q = 0;
1645 q != lines[distribute[global_col]].entries.size();
1646 ++q)
1647 sparsity.add(
1648 row, lines[distribute[global_col]].entries[q].first);
1649 }
1650 }
1651 }
1652 }
1653 else
1654 {
1655 // row must be distributed. split the whole row into the chunks
1656 // defined by the blocks
1657 for (size_type block_col = 0; block_col < n_blocks; ++block_col)
1658 {
1659 const SparsityPattern &block_sparsity =
1660 sparsity.block(block_row, block_col);
1661
1662 for (SparsityPattern::const_iterator entry =
1663 block_sparsity.begin(block_index.second);
1664 (entry != block_sparsity.end(block_index.second)) &&
1665 entry->is_valid_entry();
1666 ++entry)
1667 {
1668 const size_type global_col =
1669 index_mapping.local_to_global(block_col, entry->column());
1670
1671 if (distribute[global_col] == numbers::invalid_size_type)
1672 // distribute entry at irregular row @p{row} and
1673 // regular column global_col.
1674 {
1675 for (size_type q = 0;
1676 q != lines[distribute[row]].entries.size();
1677 ++q)
1678 sparsity.add(lines[distribute[row]].entries[q].first,
1679 global_col);
1680 }
1681 else
1682 // distribute entry at irregular row @p{row} and
1683 // irregular column @p{global_col}
1684 {
1685 for (size_type p = 0;
1686 p != lines[distribute[row]].entries.size();
1687 ++p)
1688 for (size_type q = 0;
1689 q != lines[distribute[global_col]].entries.size();
1690 ++q)
1691 sparsity.add(
1692 lines[distribute[row]].entries[p].first,
1693 lines[distribute[global_col]].entries[q].first);
1694 }
1695 }
1696 }
1697 }
1698 }
1699
1700 sparsity.compress();
1701 }
1702
1703
1704
1705 template <typename number>
1706 void
1707 AffineConstraints<number>::condense(DynamicSparsityPattern &sparsity) const
1708 {
1709 Assert(sorted == true, ExcMatrixNotClosed());
1710 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
1711
1712 // store for each index whether it must be distributed or not. If entry
1713 // is numbers::invalid_unsigned_int, no distribution is necessary.
1714 // otherwise, the number states which line in the constraints object
1715 // handles this index
1716 std::vector<size_type> distribute(sparsity.n_rows(),
1717 numbers::invalid_size_type);
1718
1719 for (size_type c = 0; c < lines.size(); ++c)
1720 distribute[lines[c].index] = c;
1721
1722 const size_type n_rows = sparsity.n_rows();
1723 for (size_type row = 0; row < n_rows; ++row)
1724 {
1725 if (distribute[row] == numbers::invalid_size_type)
1726 // regular line. loop over cols. note that as we proceed to
1727 // distribute cols, the loop may get longer
1728 for (size_type j = 0; j < sparsity.row_length(row); ++j)
1729 {
1730 const size_type column = sparsity.column_number(row, j);
1731
1732 if (distribute[column] != numbers::invalid_size_type)
1733 {
1734 // distribute entry at regular row @p{row} and irregular
1735 // column column. note that this changes the line we are
1736 // presently working on: we add additional entries. if we
1737 // add another entry at a column behind the present one, we
1738 // will encounter it later on (but since it can't be
1739 // further constrained, won't have to do anything about
1740 // it). if we add it up front of the present column, we
1741 // will find the present column later on again as it was
1742 // shifted back (again nothing happens, in particular no
1743 // endless loop, as when we encounter it the second time we
1744 // won't be able to add more entries as they all already
1745 // exist, but we do the same work more often than
1746 // necessary, and the loop gets longer), so move the cursor
1747 // one to the right in the case that we add an entry up
1748 // front that did not exist before. check whether it
1749 // existed before by tracking the length of this row
1750 size_type old_rowlength = sparsity.row_length(row);
1751 for (size_type q = 0;
1752 q != lines[distribute[column]].entries.size();
1753 ++q)
1754 {
1755 const size_type new_col =
1756 lines[distribute[column]].entries[q].first;
1757
1758 sparsity.add(row, new_col);
1759
1760 const size_type new_rowlength = sparsity.row_length(row);
1761 if ((new_col < column) && (old_rowlength != new_rowlength))
1762 ++j;
1763 old_rowlength = new_rowlength;
1764 }
1765 }
1766 }
1767 else
1768 // row must be distributed
1769 for (size_type j = 0; j < sparsity.row_length(row); ++j)
1770 {
1771 const size_type column = sparsity.column_number(row, j);
1772
1773 if (distribute[column] == numbers::invalid_size_type)
1774 // distribute entry at irregular row @p{row} and regular
1775 // column sparsity.colnums[j]
1776 for (size_type q = 0; q != lines[distribute[row]].entries.size();
1777 ++q)
1778 sparsity.add(lines[distribute[row]].entries[q].first, column);
1779 else
1780 // distribute entry at irregular row @p{row} and irregular
1781 // column sparsity.get_column_numbers()[j]
1782 for (size_type p = 0; p != lines[distribute[row]].entries.size();
1783 ++p)
1784 for (size_type q = 0;
1785 q != lines[distribute[sparsity.column_number(row, j)]]
1786 .entries.size();
1787 ++q)
1788 sparsity.add(lines[distribute[row]].entries[p].first,
1789 lines[distribute[sparsity.column_number(row, j)]]
1790 .entries[q]
1791 .first);
1792 }
1793 }
1794 }
1795
1796
1797
1798 template <typename number>
1799 void
1800 AffineConstraints<number>::condense(BlockDynamicSparsityPattern &sparsity) const
1801 {
1802 Assert(sorted == true, ExcMatrixNotClosed());
1803 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
1804 Assert(sparsity.n_block_rows() == sparsity.n_block_cols(), ExcNotQuadratic());
1805 Assert(sparsity.get_column_indices() == sparsity.get_row_indices(),
1806 ExcNotQuadratic());
1807
1808 const BlockIndices &index_mapping = sparsity.get_column_indices();
1809
1810 const size_type n_blocks = sparsity.n_block_rows();
1811
1812 // store for each index whether it must be distributed or not. If entry
1813 // is numbers::invalid_unsigned_int, no distribution is necessary.
1814 // otherwise, the number states which line in the constraints object
1815 // handles this index
1816 std::vector<size_type> distribute(sparsity.n_rows(),
1817 numbers::invalid_size_type);
1818
1819 for (size_type c = 0; c < lines.size(); ++c)
1820 distribute[lines[c].index] = static_cast<signed int>(c);
1821
1822 const size_type n_rows = sparsity.n_rows();
1823 for (size_type row = 0; row < n_rows; ++row)
1824 {
1825 // get index of this row within the blocks
1826 const std::pair<size_type, size_type> block_index =
1827 index_mapping.global_to_local(row);
1828 const size_type block_row = block_index.first;
1829 const size_type local_row = block_index.second;
1830
1831 if (distribute[row] == numbers::invalid_size_type)
1832 // regular line. loop over all columns and see whether this column
1833 // must be distributed. note that as we proceed to distribute cols,
1834 // the loop over cols may get longer.
1835 //
1836 // don't try to be clever here as in the algorithm for the
1837 // DynamicSparsityPattern, as that would be much more
1838 // complicated here. after all, we know that compressed patterns
1839 // are inefficient...
1840 {
1841 // to loop over all entries in this row, we have to loop over all
1842 // blocks in this blockrow and the corresponding row therein
1843 for (size_type block_col = 0; block_col < n_blocks; ++block_col)
1844 {
1845 const DynamicSparsityPattern &block_sparsity =
1846 sparsity.block(block_row, block_col);
1847
1848 for (size_type j = 0; j < block_sparsity.row_length(local_row);
1849 ++j)
1850 {
1851 const size_type global_col = index_mapping.local_to_global(
1852 block_col, block_sparsity.column_number(local_row, j));
1853
1854 if (distribute[global_col] != numbers::invalid_size_type)
1855 // distribute entry at regular row @p{row} and
1856 // irregular column global_col
1857 {
1858 for (size_type q = 0;
1859 q != lines[distribute[global_col]].entries.size();
1860 ++q)
1861 sparsity.add(
1862 row, lines[distribute[global_col]].entries[q].first);
1863 }
1864 }
1865 }
1866 }
1867 else
1868 {
1869 // row must be distributed. split the whole row into the chunks
1870 // defined by the blocks
1871 for (size_type block_col = 0; block_col < n_blocks; ++block_col)
1872 {
1873 const DynamicSparsityPattern &block_sparsity =
1874 sparsity.block(block_row, block_col);
1875
1876 for (size_type j = 0; j < block_sparsity.row_length(local_row);
1877 ++j)
1878 {
1879 const size_type global_col = index_mapping.local_to_global(
1880 block_col, block_sparsity.column_number(local_row, j));
1881
1882 if (distribute[global_col] == numbers::invalid_size_type)
1883 // distribute entry at irregular row @p{row} and
1884 // regular column global_col.
1885 {
1886 for (size_type q = 0;
1887 q != lines[distribute[row]].entries.size();
1888 ++q)
1889 sparsity.add(lines[distribute[row]].entries[q].first,
1890 global_col);
1891 }
1892 else
1893 // distribute entry at irregular row @p{row} and
1894 // irregular column @p{global_col}
1895 {
1896 for (size_type p = 0;
1897 p != lines[distribute[row]].entries.size();
1898 ++p)
1899 for (size_type q = 0;
1900 q != lines[distribute[global_col]].entries.size();
1901 ++q)
1902 sparsity.add(
1903 lines[distribute[row]].entries[p].first,
1904 lines[distribute[global_col]].entries[q].first);
1905 }
1906 }
1907 }
1908 }
1909 }
1910 }
1911
1912
1913
1914 template <typename number>
1915 void
1916 AffineConstraints<number>::condense(SparseMatrix<number> &uncondensed) const
1917 {
1918 Vector<number> dummy(0);
1919 condense(uncondensed, dummy);
1920 }
1921
1922
1923
1924 template <typename number>
1925 void
1926 AffineConstraints<number>::condense(
1927 BlockSparseMatrix<number> &uncondensed) const
1928 {
1929 BlockVector<number> dummy(0);
1930 condense(uncondensed, dummy);
1931 }
1932
1933
1934
1935 template <typename number>
1936 template <typename VectorType>
1937 void
1938 AffineConstraints<number>::condense(const VectorType &vec_ghosted,
1939 VectorType &vec) const
1940 {
1941 Assert(sorted == true, ExcMatrixNotClosed());
1942
1943 // if this is called with different arguments, we need to copy the data
1944 // over:
1945 if (&vec != &vec_ghosted)
1946 vec = vec_ghosted;
1947
1948 // distribute all entries, and set them to zero. do so in two loops
1949 // because in the first one we need to add to elements and in the second
1950 // one we need to set elements to zero. for parallel vectors, this can
1951 // only work if we can put a compress() in between, but we don't want to
1952 // call compress() twice per entry
1953 for (const ConstraintLine &line : lines)
1954 {
1955 // in case the constraint is inhomogeneous, this function is not
1956 // appropriate. Throw an exception.
1957 Assert(line.inhomogeneity == number(0.),
1958 ExcMessage("Inhomogeneous constraint cannot be condensed "
1959 "without any matrix specified."));
1960
1961 const typename VectorType::value_type old_value = vec_ghosted(line.index);
1962 for (const std::pair<size_type, number> &entry : line.entries)
1963 if (vec.in_local_range(entry.first) == true)
1964 vec(entry.first) +=
1965 (static_cast<typename VectorType::value_type>(old_value) *
1966 entry.second);
1967 }
1968
1969 vec.compress(VectorOperation::add);
1970
1971 for (const ConstraintLine &line : lines)
1972 if (vec.in_local_range(line.index) == true)
1973 vec(line.index) = 0.;
1974
1975 vec.compress(VectorOperation::insert);
1976 }
1977
1978
1979
1980 template <typename number>
1981 template <typename VectorType>
1982 void
1983 AffineConstraints<number>::condense(VectorType &vec) const
1984 {
1985 condense(vec, vec);
1986 }
1987
1988
1989
1990 template <typename number>
1991 template <typename VectorType>
1992 void
1993 AffineConstraints<number>::condense(SparseMatrix<number> &uncondensed,
1994 VectorType &vec) const
1995 {
1996 // check whether we work on real vectors or we just used a dummy when
1997 // calling the other function above.
1998 const bool use_vectors = vec.size() == 0 ? false : true;
1999
2000 const SparsityPattern &sparsity = uncondensed.get_sparsity_pattern();
2001
2002 Assert(sorted == true, ExcMatrixNotClosed());
2003 Assert(sparsity.is_compressed() == true, ExcMatrixNotClosed());
2004 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
2005 if (use_vectors == true)
2006 AssertDimension(vec.size(), sparsity.n_rows());
2007
2008 number average_diagonal = 0.;
2009 for (size_type i = 0; i < uncondensed.m(); ++i)
2010 average_diagonal += std::abs(uncondensed.diag_element(i));
2011 average_diagonal /= uncondensed.m();
2012
2013 // store for each index whether it must be distributed or not. If entry
2014 // is invalid_size_type, no distribution is necessary. otherwise, the
2015 // number states which line in the constraints object handles this index
2016 std::vector<size_type> distribute(sparsity.n_rows(),
2017 numbers::invalid_size_type);
2018
2019 for (size_type c = 0; c < lines.size(); ++c)
2020 distribute[lines[c].index] = c;
2021
2022 const size_type n_rows = sparsity.n_rows();
2023 for (size_type row = 0; row < n_rows; ++row)
2024 {
2025 if (distribute[row] == numbers::invalid_size_type)
2026 // regular line. loop over cols
2027 {
2028 for (typename SparseMatrix<number>::iterator entry =
2029 uncondensed.begin(row);
2030 entry != uncondensed.end(row);
2031 ++entry)
2032 {
2033 const size_type column = entry->column();
2034
2035 // end of row reached? this should not happen, since we only
2036 // operate on compressed matrices!
2037 Assert(column != SparsityPattern::invalid_entry,
2038 ExcMatrixNotClosed());
2039
2040 if (distribute[column] != numbers::invalid_size_type)
2041 // distribute entry at regular row @p row and irregular
2042 // column sparsity.get_column_numbers()[j]; set old entry
2043 // to zero
2044 {
2045 for (size_type q = 0;
2046 q != lines[distribute[column]].entries.size();
2047 ++q)
2048 {
2049 // need a temporary variable to avoid errors like no
2050 // known conversion from 'complex<typename
2051 // ProductType<float, double>::type>' to 'const
2052 // complex<float>' for 3rd argument
2053 number v = static_cast<number>(entry->value());
2054 v *= lines[distribute[column]].entries[q].second;
2055 uncondensed.add(
2056 row, lines[distribute[column]].entries[q].first, v);
2057 }
2058
2059 // need to subtract this element from the vector. this
2060 // corresponds to an explicit elimination in the
2061 // respective row of the inhomogeneous constraint in the
2062 // matrix with Gauss elimination
2063 if (use_vectors == true)
2064 vec(row) -= static_cast<number>(entry->value()) *
2065 lines[distribute[column]].inhomogeneity;
2066
2067 // set old value to zero
2068 entry->value() = 0.;
2069 }
2070 }
2071 }
2072 else
2073 // row must be distributed
2074 {
2075 for (typename SparseMatrix<number>::iterator entry =
2076 uncondensed.begin(row);
2077 entry != uncondensed.end(row);
2078 ++entry)
2079 {
2080 const size_type column = entry->column();
2081
2082 // end of row reached? this should not happen, since we only
2083 // operate on compressed matrices!
2084 Assert(column != SparsityPattern::invalid_entry,
2085 ExcMatrixNotClosed());
2086
2087 if (distribute[column] == numbers::invalid_size_type)
2088 // distribute entry at irregular row @p row and regular
2089 // column column. set old entry to zero
2090 {
2091 for (size_type q = 0;
2092 q != lines[distribute[row]].entries.size();
2093 ++q)
2094 {
2095 // need a temporary variable to avoid errors like
2096 // no known conversion from 'complex<typename
2097 // ProductType<float, double>::type>' to 'const
2098 // complex<float>' for 3rd argument
2099 number v = static_cast<number>(entry->value());
2100 v *= lines[distribute[row]].entries[q].second;
2101 uncondensed.add(lines[distribute[row]].entries[q].first,
2102 column,
2103 v);
2104 }
2105
2106 // set old entry to zero
2107 entry->value() = 0.;
2108 }
2109 else
2110 // distribute entry at irregular row @p row and irregular
2111 // column @p column set old entry to one on main diagonal,
2112 // zero otherwise
2113 {
2114 for (size_type p = 0;
2115 p != lines[distribute[row]].entries.size();
2116 ++p)
2117 {
2118 for (size_type q = 0;
2119 q != lines[distribute[column]].entries.size();
2120 ++q)
2121 {
2122 // need a temporary variable to avoid errors like
2123 // no known conversion from 'complex<typename
2124 // ProductType<float, double>::type>' to 'const
2125 // complex<float>' for 3rd argument
2126 number v = static_cast<number>(entry->value());
2127 v *= lines[distribute[row]].entries[p].second *
2128 lines[distribute[column]].entries[q].second;
2129 uncondensed.add(
2130 lines[distribute[row]].entries[p].first,
2131 lines[distribute[column]].entries[q].first,
2132 v);
2133 }
2134
2135 if (use_vectors == true)
2136 vec(lines[distribute[row]].entries[p].first) -=
2137 static_cast<number>(entry->value()) *
2138 lines[distribute[row]].entries[p].second *
2139 lines[distribute[column]].inhomogeneity;
2140 }
2141
2142 // set old entry to correct value
2143 entry->value() = (row == column ? average_diagonal : 0.);
2144 }
2145 }
2146
2147 // take care of vector
2148 if (use_vectors == true)
2149 {
2150 for (size_type q = 0; q != lines[distribute[row]].entries.size();
2151 ++q)
2152 vec(lines[distribute[row]].entries[q].first) +=
2153 (vec(row) * lines[distribute[row]].entries[q].second);
2154
2155 vec(lines[distribute[row]].index) = 0.;
2156 }
2157 }
2158 }
2159 }
2160
2161
2162
2163 template <typename number>
2164 template <typename BlockVectorType>
2165 void
2166 AffineConstraints<number>::condense(BlockSparseMatrix<number> &uncondensed,
2167 BlockVectorType &vec) const
2168 {
2169 // check whether we work on real vectors or we just used a dummy when
2170 // calling the other function above.
2171 const bool use_vectors = vec.n_blocks() == 0 ? false : true;
2172
2173 const size_type blocks = uncondensed.n_block_rows();
2174
2175 const BlockSparsityPattern &sparsity = uncondensed.get_sparsity_pattern();
2176
2177 Assert(sorted == true, ExcMatrixNotClosed());
2178 Assert(sparsity.is_compressed() == true, ExcMatrixNotClosed());
2179 Assert(sparsity.n_rows() == sparsity.n_cols(), ExcNotQuadratic());
2180 Assert(sparsity.n_block_rows() == sparsity.n_block_cols(), ExcNotQuadratic());
2181 Assert(sparsity.n_block_rows() == sparsity.n_block_cols(), ExcNotQuadratic());
2182 Assert(sparsity.get_column_indices() == sparsity.get_row_indices(),
2183 ExcNotQuadratic());
2184
2185 if (use_vectors == true)
2186 {
2187 AssertDimension(vec.size(), sparsity.n_rows());
2188 AssertDimension(vec.n_blocks(), sparsity.n_block_rows());
2189 }
2190
2191 number average_diagonal = 0.;
2192 for (size_type b = 0; b < uncondensed.n_block_rows(); ++b)
2193 for (size_type i = 0; i < uncondensed.block(b, b).m(); ++i)
2194 average_diagonal += std::abs(uncondensed.block(b, b).diag_element(i));
2195 average_diagonal /= uncondensed.m();
2196
2197 const BlockIndices &index_mapping = sparsity.get_column_indices();
2198
2199 // store for each index whether it must be distributed or not. If entry
2200 // is numbers::invalid_size_type, no distribution is necessary.
2201 // otherwise, the number states which line in the constraints object
2202 // handles this index
2203 std::vector<size_type> distribute(sparsity.n_rows(),
2204 numbers::invalid_size_type);
2205
2206 for (size_type c = 0; c < lines.size(); ++c)
2207 distribute[lines[c].index] = c;
2208
2209 const size_type n_rows = sparsity.n_rows();
2210 for (size_type row = 0; row < n_rows; ++row)
2211 {
2212 // get index of this row within the blocks
2213 const std::pair<size_type, size_type> block_index =
2214 index_mapping.global_to_local(row);
2215 const size_type block_row = block_index.first;
2216
2217 if (distribute[row] == numbers::invalid_size_type)
2218 // regular line. loop over all columns and see whether this column
2219 // must be distributed
2220 {
2221 // to loop over all entries in this row, we have to loop over all
2222 // blocks in this blockrow and the corresponding row therein
2223 for (size_type block_col = 0; block_col < blocks; ++block_col)
2224 {
2225 for (typename SparseMatrix<number>::iterator entry =
2226 uncondensed.block(block_row, block_col)
2227 .begin(block_index.second);
2228 entry != uncondensed.block(block_row, block_col)
2229 .end(block_index.second);
2230 ++entry)
2231 {
2232 const size_type global_col =
2233 index_mapping.local_to_global(block_col, entry->column());
2234
2235 if (distribute[global_col] != numbers::invalid_size_type)
2236 // distribute entry at regular row @p row and irregular
2237 // column global_col; set old entry to zero
2238 {
2239 const number old_value = entry->value();
2240
2241 for (size_type q = 0;
2242 q != lines[distribute[global_col]].entries.size();
2243 ++q)
2244 uncondensed.add(
2245 row,
2246 lines[distribute[global_col]].entries[q].first,
2247 old_value *
2248 lines[distribute[global_col]].entries[q].second);
2249
2250 // need to subtract this element from the vector.
2251 // this corresponds to an explicit elimination in the
2252 // respective row of the inhomogeneous constraint in
2253 // the matrix with Gauss elimination
2254 if (use_vectors == true)
2255 vec(row) -= static_cast<number>(entry->value()) *
2256 lines[distribute[global_col]].inhomogeneity;
2257
2258 entry->value() = 0.;
2259 }
2260 }
2261 }
2262 }
2263 else
2264 {
2265 // row must be distributed. split the whole row into the chunks
2266 // defined by the blocks
2267 for (size_type block_col = 0; block_col < blocks; ++block_col)
2268 {
2269 for (typename SparseMatrix<number>::iterator entry =
2270 uncondensed.block(block_row, block_col)
2271 .begin(block_index.second);
2272 entry != uncondensed.block(block_row, block_col)
2273 .end(block_index.second);
2274 ++entry)
2275 {
2276 const size_type global_col =
2277 index_mapping.local_to_global(block_col, entry->column());
2278
2279 if (distribute[global_col] == numbers::invalid_size_type)
2280 // distribute entry at irregular row @p row and regular
2281 // column global_col. set old entry to zero
2282 {
2283 const number old_value = entry->value();
2284
2285 for (size_type q = 0;
2286 q != lines[distribute[row]].entries.size();
2287 ++q)
2288 uncondensed.add(
2289 lines[distribute[row]].entries[q].first,
2290 global_col,
2291 old_value * lines[distribute[row]].entries[q].second);
2292
2293 entry->value() = 0.;
2294 }
2295 else
2296 // distribute entry at irregular row @p row and
2297 // irregular column @p global_col set old entry to one
2298 // if on main diagonal, zero otherwise
2299 {
2300 const number old_value = entry->value();
2301
2302 for (size_type p = 0;
2303 p != lines[distribute[row]].entries.size();
2304 ++p)
2305 {
2306 for (size_type q = 0;
2307 q !=
2308 lines[distribute[global_col]].entries.size();
2309 ++q)
2310 uncondensed.add(
2311 lines[distribute[row]].entries[p].first,
2312 lines[distribute[global_col]].entries[q].first,
2313 old_value *
2314 lines[distribute[row]].entries[p].second *
2315 lines[distribute[global_col]]
2316 .entries[q]
2317 .second);
2318
2319 if (use_vectors == true)
2320 vec(lines[distribute[row]].entries[p].first) -=
2321 old_value *
2322 lines[distribute[row]].entries[p].second *
2323 lines[distribute[global_col]].inhomogeneity;
2324 }
2325
2326 entry->value() =
2327 (row == global_col ? average_diagonal : 0.);
2328 }
2329 }
2330 }
2331
2332 // take care of vector
2333 if (use_vectors == true)
2334 {
2335 for (size_type q = 0; q != lines[distribute[row]].entries.size();
2336 ++q)
2337 vec(lines[distribute[row]].entries[q].first) +=
2338 (vec(row) * lines[distribute[row]].entries[q].second);
2339
2340 vec(lines[distribute[row]].index) = 0.;
2341 }
2342 }
2343 }
2344 }
2345
2346
2347
2348 // TODO: I'm sure the following could be made more elegant by using a bit of
2349 // introspection using static member variables of the various vector
2350 // classes to dispatch between the different functions, rather than using
2351 // knowledge of the individual types
2352
2353 // number of functions to select the right implementation for set_zero().
2354 namespace internal
2355 {
2356 namespace AffineConstraintsImplementation
2357 {
2358 using size_type = types::global_dof_index;
2359
2360 template <typename VectorType>
2361 void
2362 set_zero_parallel(const std::vector<size_type> &cm,
2363 VectorType &vec,
2364 size_type shift = 0)
2365 {
2366 Assert(!vec.has_ghost_elements(), ExcInternalError());
2367 IndexSet locally_owned = vec.locally_owned_elements();
2368 for (const size_type index : cm)
2369 {
2370 // If shift>0 then we are working on a part of a BlockVector
2371 // so vec(i) is actually the global entry i+shift.
2372 // We first make sure the line falls into the range of vec,
2373 // then check if is part of the local part of the vector, before
2374 // finally setting value to 0.
2375 if (index < shift)
2376 continue;
2377 const size_type idx = index - shift;
2378 if (idx < vec.size() && locally_owned.is_element(idx))
2379 internal::ElementAccess<VectorType>::set(0., idx, vec);
2380 }
2381 }
2382
2383 template <typename number>
2384 void
2385 set_zero_parallel(const std::vector<size_type> &cm,
2386 LinearAlgebra::distributed::Vector<number> &vec,
2387 size_type shift = 0)
2388 {
2389 for (const size_type index : cm)
2390 {
2391 // If shift>0 then we are working on a part of a BlockVector
2392 // so vec(i) is actually the global entry i+shift.
2393 // We first make sure the line falls into the range of vec,
2394 // then check if is part of the local part of the vector, before
2395 // finally setting the value to 0.
2396 if (index < shift)
2397 continue;
2398 const size_type idx = index - shift;
2399 if (vec.in_local_range(idx))
2400 vec(idx) = 0.;
2401 }
2402 vec.zero_out_ghost_values();
2403 }
2404
2405 template <typename number>
2406 void
2407 set_zero_parallel(
2408 const std::vector<size_type> &cm,
2409 LinearAlgebra::distributed::Vector<number, MemorySpace::Default> &vec,
2410 size_type shift = 0)
2411 {
2412 Assert(shift == 0, ExcNotImplemented());
2413 std::vector<size_type> constrained_local_dofs_host;
2414 constrained_local_dofs_host.reserve(cm.size());
2415
2416 for (const auto global_index : cm)
2417 if (vec.in_local_range(global_index))
2418 constrained_local_dofs_host.push_back(
2419 vec.get_partitioner()->global_to_local(global_index));
2420
2421 const int n_constraints = constrained_local_dofs_host.size();
2422 Kokkos::View<size_type *, MemorySpace::Default::kokkos_space>
2423 constrained_local_dofs_device(
2424 Kokkos::view_alloc(Kokkos::WithoutInitializing,
2425 "constrained_local_dofs_device"),
2426 n_constraints);
2427 Kokkos::deep_copy(constrained_local_dofs_device,
2428 Kokkos::View<size_type *, Kokkos::HostSpace>(
2429 constrained_local_dofs_host.data(),
2430 constrained_local_dofs_host.size()));
2431
2432 using ExecutionSpace =
2433 MemorySpace::Default::kokkos_space::execution_space;
2434 ExecutionSpace exec;
2435 auto *local_values = vec.get_values();
2436 Kokkos::parallel_for(
2437 "dealii::set_zero_parallel",
2438 Kokkos::RangePolicy<ExecutionSpace>(exec, 0, n_constraints),
2439 KOKKOS_LAMBDA(int i) {
2440 local_values[constrained_local_dofs_device[i]] = 0;
2441 });
2442
2443 vec.zero_out_ghost_values();
2444 }
2445
2446 template <typename VectorType>
2447 void
2448 set_zero_in_parallel(const std::vector<size_type> &cm,
2449 VectorType &vec,
2450 std::bool_constant<false>)
2451 {
2452 set_zero_parallel(cm, vec, 0);
2453 }
2454
2455 // in parallel for BlockVectors
2456 template <typename VectorType>
2457 void
2458 set_zero_in_parallel(const std::vector<size_type> &cm,
2459 VectorType &vec,
2460 std::bool_constant<true>)
2461 {
2462 size_type start_shift = 0;
2463 for (size_type j = 0; j < vec.n_blocks(); ++j)
2464 {
2465 set_zero_parallel(cm, vec.block(j), start_shift);
2466 start_shift += vec.block(j).size();
2467 }
2468 }
2469
2470 template <typename VectorType>
2471 void
2472 set_zero_serial(const std::vector<size_type> &cm, VectorType &vec)
2473 {
2474 for (const size_type index : cm)
2475 vec(index) = 0.;
2476 }
2477
2478 template <typename VectorType>
2479 void
2480 set_zero_all(const std::vector<size_type> &cm, VectorType &vec)
2481 {
2482 set_zero_in_parallel<VectorType>(
2483 cm, vec, std::bool_constant<IsBlockVector<VectorType>::value>());
2484 vec.compress(VectorOperation::insert);
2485 }
2486
2487 template <class T>
2488 void
2489 set_zero_all(const std::vector<size_type> &cm, dealii::Vector<T> &vec)
2490 {
2491 set_zero_serial(cm, vec);
2492 }
2493
2494 template <class T>
2495 void
2496 set_zero_all(const std::vector<size_type> &cm, dealii::BlockVector<T> &vec)
2497 {
2498 set_zero_serial(cm, vec);
2499 }
2500 } // namespace AffineConstraintsImplementation
2501 } // namespace internal
2502
2503
2504
2505 template <typename number>
2506 template <typename VectorType>
2507 void
2508 AffineConstraints<number>::distribute_local_to_global(
2509 const Vector<number> &local_vector,
2510 const std::vector<size_type> &local_dof_indices,
2511 VectorType &global_vector,
2512 const FullMatrix<number> &local_matrix) const
2513 {
2514 distribute_local_to_global(local_vector,
2515 local_dof_indices,
2516 local_dof_indices,
2517 global_vector,
2518 local_matrix,
2519 true);
2520 }
2521
2522
2523
2524 template <typename number>
2525 template <typename VectorType>
2526 void
2527 AffineConstraints<number>::distribute_local_to_global(
2528 const Vector<number> &local_vector,
2529 const std::vector<size_type> &local_dof_indices_row,
2530 const std::vector<size_type> &local_dof_indices_col,
2531 VectorType &global_vector,
2532 const FullMatrix<number> &local_matrix,
2533 bool diagonal) const
2534 {
2535 Assert(sorted == true, ExcMatrixNotClosed());
2536 AssertDimension(local_vector.size(), local_dof_indices_row.size());
2537 AssertDimension(local_matrix.m(), local_dof_indices_row.size());
2538 AssertDimension(local_matrix.n(), local_dof_indices_col.size());
2539 Assert(global_vector.has_ghost_elements() == false, ExcGhostsPresent());
2540
2541 // diagonal checks if we have only one index set (if both are equal
2542 // diagonal should be set to true).
2543 // If true we do both, assembly of the right hand side (next lines)
2544 // and (see further below) modifications of the right hand side
2545 // according to the inhomogeneous constraints.
2546 // Otherwise we only modify the right hand side according to
2547 // local_matrix and the inhomogeneous constraints, and omit the vector add.
2548
2549 const size_type m_local_dofs = local_dof_indices_row.size();
2550 const size_type n_local_dofs = local_dof_indices_col.size();
2551 if (lines.empty())
2552 {
2553 if (diagonal)
2554 global_vector.add(local_dof_indices_row, local_vector);
2555 }
2556 else
2557 for (size_type i = 0; i < n_local_dofs; ++i)
2558 {
2559 // check whether the current index is
2560 // constrained. if not, just write the entry
2561 // into the vector. otherwise, need to resolve
2562 // the constraint
2563 if (is_constrained(local_dof_indices_col[i]) == false)
2564 {
2565 if (diagonal)
2566 global_vector(local_dof_indices_row[i]) += local_vector(i);
2567 continue;
2568 }
2569
2570 // find the constraint line to the given
2571 // global dof index
2572 const size_type line_index =
2573 calculate_line_index(local_dof_indices_col[i]);
2574 AssertIndexRange(line_index, lines_cache.size());
2575 AssertIndexRange(lines_cache[line_index], lines.size());
2576 const ConstraintLine &position = lines[lines_cache[line_index]];
2577
2578 // Gauss elimination of the matrix columns with the inhomogeneity.
2579 // Go through them one by one and again check whether they are
2580 // constrained. If so, distribute the constraint
2581 const auto val = position.inhomogeneity;
2582 if (val != number(0.))
2583 for (size_type j = 0; j < m_local_dofs; ++j)
2584 {
2585 if (is_constrained(local_dof_indices_row[j]) == false)
2586 {
2587 global_vector(local_dof_indices_row[j]) -=
2588 val * local_matrix(j, i);
2589 continue;
2590 }
2591
2592 const number matrix_entry = local_matrix(j, i);
2593
2594 if (matrix_entry == number())
2595 continue;
2596
2597 const ConstraintLine &position_j =
2598 lines[lines_cache[calculate_line_index(
2599 local_dof_indices_row[j])]];
2600
2601 for (size_type q = 0; q < position_j.entries.size(); ++q)
2602 {
2603 Assert(!(!local_lines.size() ||
2604 local_lines.is_element(
2605 position_j.entries[q].first)) ||
2606 is_constrained(position_j.entries[q].first) == false,
2607 ExcMessage("Tried to distribute to a fixed dof."));
2608 global_vector(position_j.entries[q].first) -=
2609 val * position_j.entries[q].second * matrix_entry;
2610 }
2611 }
2612
2613 // now distribute the constraint,
2614 // but make sure we don't touch
2615 // the entries of fixed dofs
2616 if (diagonal)
2617 {
2618 for (size_type j = 0; j < position.entries.size(); ++j)
2619 {
2620 Assert(!(!local_lines.size() ||
2621 local_lines.is_element(position.entries[j].first)) ||
2622 is_constrained(position.entries[j].first) == false,
2623 ExcMessage("Tried to distribute to a fixed dof."));
2624 global_vector(position.entries[j].first) +=
2625 local_vector(i) * position.entries[j].second;
2626 }
2627 }
2628 }
2629 }
2630
2631 namespace internal
2632 {
2633 // create an output vector that consists of the input vector's locally owned
2634 // elements plus some ghost elements that need to be imported from elsewhere
2635 //
2636 // this is an operation that is different for all vector types and so we
2637 // need a few overloads
2638 #ifdef DEAL_II_WITH_TRILINOS
2639 inline void
2640 import_vector_with_ghost_elements(
2641 const TrilinosWrappers::MPI::Vector &vec,
2642 const IndexSet & /*locally_owned_elements*/,
2643 const IndexSet &needed_elements,
2644 TrilinosWrappers::MPI::Vector &output,
2645 const std::bool_constant<false> /*is_block_vector*/)
2646 {
2647 Assert(!vec.has_ghost_elements(), ExcGhostsPresent());
2648 # ifdef DEAL_II_WITH_MPI
2649 const Epetra_MpiComm *mpi_comm =
2650 dynamic_cast<const Epetra_MpiComm *>(&vec.trilinos_vector().Comm());
2651
2652 Assert(mpi_comm != nullptr, ExcInternalError());
2653 output.reinit(needed_elements, mpi_comm->GetMpiComm());
2654 # else
2655 output.reinit(needed_elements, MPI_COMM_SELF);
2656 # endif
2657 output = vec;
2658 }
2659 #endif
2660
2661
2662
2663 #ifdef DEAL_II_TRILINOS_WITH_TPETRA
2664 template <typename Number, typename MemorySpace>
2665 inline void
2666 import_vector_with_ghost_elements(
2667 const LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &vec,
2668 const IndexSet &locally_owned_elements,
2669 const IndexSet &needed_elements,
2670 LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &output,
2671 const std::bool_constant<false> /*is_block_vector*/)
2672 {
2673 Assert(!vec.has_ghost_elements(), ExcGhostsPresent());
2674 IndexSet parallel_partitioner = locally_owned_elements;
2675 parallel_partitioner.add_indices(needed_elements);
2676
2677 const MPI_Comm mpi_comm = Utilities::Trilinos::teuchos_comm_to_mpi_comm(
2678 vec.trilinos_vector().getMap()->getComm());
2679
2680 output.reinit(locally_owned_elements, needed_elements, mpi_comm);
2681
2682 output = vec;
2683 }
2684 #endif // DEAL_II_TRILINOS_WITH_TPETRA
2685
2686
2687
2688 #ifdef DEAL_II_WITH_PETSC
2689 inline void
2690 import_vector_with_ghost_elements(
2691 const PETScWrappers::MPI::Vector &vec,
2692 const IndexSet &locally_owned_elements,
2693 const IndexSet &needed_elements,
2694 PETScWrappers::MPI::Vector &output,
2695 const std::bool_constant<false> /*is_block_vector*/)
2696 {
2697 output.reinit(locally_owned_elements,
2698 needed_elements,
2699 vec.get_mpi_communicator());
2700 output = vec;
2701 }
2702 #endif
2703
2704
2705
2706 template <typename number>
2707 void
2708 import_vector_with_ghost_elements(
2709 const LinearAlgebra::distributed::Vector<number> &vec,
2710 const IndexSet &locally_owned_elements,
2711 const IndexSet &needed_elements,
2712 LinearAlgebra::distributed::Vector<number> &output,
2713 const std::bool_constant<false> /*is_block_vector*/)
2714 {
2715 // TODO: the in vector might already have all elements. need to find a
2716 // way to efficiently avoid the copy then
2717 const_cast<LinearAlgebra::distributed::Vector<number> &>(vec)
2718 .zero_out_ghost_values();
2719 output.reinit(locally_owned_elements,
2720 needed_elements,
2721 vec.get_mpi_communicator());
2722 output = vec;
2723 output.update_ghost_values();
2724 }
2725
2726
2727
2728 // all other vector non-block vector types are sequential and we should
2729 // not have this function called at all -- so throw an exception
2730 template <typename Vector>
2731 void
2732 import_vector_with_ghost_elements(
2733 const Vector & /*vec*/,
2734 const IndexSet & /*locally_owned_elements*/,
2735 const IndexSet & /*needed_elements*/,
2736 Vector & /*output*/,
2737 const std::bool_constant<false> /*is_block_vector*/)
2738 {
2739 Assert(false, ExcMessage("We shouldn't even get here!"));
2740 }
2741
2742 // for block vectors, simply dispatch to the individual blocks
2743 template <typename VectorType>
2744 void
2745 import_vector_with_ghost_elements(
2746 const VectorType &vec,
2747 const IndexSet &locally_owned_elements,
2748 const IndexSet &needed_elements,
2749 VectorType &output,
2750 const std::bool_constant<true> /*is_block_vector*/)
2751 {
2752 output.reinit(vec.n_blocks());
2753
2754 types::global_dof_index block_start = 0;
2755 for (unsigned int b = 0; b < vec.n_blocks(); ++b)
2756 {
2757 import_vector_with_ghost_elements(
2758 vec.block(b),
2759 locally_owned_elements.get_view(block_start,
2760 block_start + vec.block(b).size()),
2761 needed_elements.get_view(block_start,
2762 block_start + vec.block(b).size()),
2763 output.block(b),
2764 std::bool_constant<false>());
2765 block_start += vec.block(b).size();
2766 }
2767
2768 output.collect_sizes();
2769 }
2770 } // namespace internal
2771
2772
2773 namespace internal
2774 {
2775 namespace AffineConstraints
2776 {
2777 template <typename VectorType,
2778 std::enable_if_t<internal::is_block_vector<VectorType> == false>
2779 * = nullptr>
2780 MPI_Comm
2781 get_mpi_communicator(const VectorType &vec)
2782 {
2783 return vec.get_mpi_communicator();
2784 }
2785
2786
2787
2788 template <typename VectorType,
2789 std::enable_if_t<internal::is_block_vector<VectorType> == true>
2790 * = nullptr>
2791 MPI_Comm
2792 get_mpi_communicator(const VectorType &vec)
2793 {
2794 Assert(vec.n_blocks() > 0, ExcInternalError());
2795 return vec.block(0).get_mpi_communicator();
2796 }
2797
2798
2799
2800 template <typename V>
2801 using is_compressed_op = decltype(std::declval<V>().is_compressed());
2802 } // namespace AffineConstraints
2803 } // namespace internal
2804
2805
2806
2807 template <typename number>
2808 template <typename VectorType>
2809 void
2810 AffineConstraints<number>::distribute(VectorType &vec) const
2811 {
2812 Assert(sorted == true, ExcMatrixNotClosed());
2813
2814 if constexpr (internal::is_supported_operation<
2815 internal::AffineConstraints::is_compressed_op,
2816 VectorType>)
2817 Assert(
2818 vec.is_compressed(),
2819 ExcMessage(
2820 "To distribute a parallel vector, the vector must "
2821 "not contain any previously written-to or added-to entries that "
2822 "have not already been sent to these entries' owners. But "
2823 "the vector you passed in has such entries. You need to call "
2824 "compress() on the vector first, before passing it to this function."));
2825
2826 // if the vector type supports parallel storage and if the vector actually
2827 // does store only part of the vector, distributing is slightly more
2828 // complicated. we might be able to skip the complicated part if one
2829 // processor owns everything and pretend that this is a sequential vector,
2830 // but it is difficult for the other processors to know whether they should
2831 // not do anything or if other processors will create a temporary vector,
2832 // exchange data (requiring communication, maybe even with the processors
2833 // that do not own anything because of that particular parallel model), and
2834 // call compress() finally. the first case here is for the complicated case,
2835 // the last else is for the simple case (sequential vector)
2836 const IndexSet vec_owned_elements = vec.locally_owned_elements();
2837
2838 if constexpr (dealii::is_serial_vector<VectorType>::value == false)
2839 {
2840 // First check whether there are any constraints at all. If
2841 // there aren't, then there is nothing we need to do and we can
2842 // save the considerable effort below to communicate information
2843 // -- this is for sure the case on the first (not yet adaptively
2844 // refined) level of computations, for example:
2845 if (Utilities::MPI::sum(lines.size(),
2846 internal::AffineConstraints::get_mpi_communicator(
2847 vec)) > 0)
2848 {
2849 // We are looking at parallel vectors here. Of course, they could be
2850 // used in sequential contexts, but at least if we know that we are
2851 // in a parallel context with more than one process, we should check
2852 // that the user of this class provided index sets to the constructor
2853 // or the reinit() functions:
2854 if (Utilities::MPI::n_mpi_processes(
2855 internal::AffineConstraints::get_mpi_communicator(vec)) > 1)
2856 {
2857 Assert(local_lines != IndexSet(),
2858 ExcMessage(
2859 "You are using the AffineConstraints class in a "
2860 "program that is running in parallel. In this case, "
2861 "it is inefficient to store all constraints: This class "
2862 "should really only store constraints for those degrees "
2863 "of freedom that are locally relevant. It is considered "
2864 "a bug not to do that. Please call either the "
2865 "constructor of this class, or one of its reinit() "
2866 "functions that provide this class with index sets "
2867 "of the locally owned and the locally relevant "
2868 "degrees of freedom."));
2869
2870 Assert(vec_owned_elements.size() == locally_owned_dofs.size(),
2871 ExcMessage("You have previously initialized this "
2872 "AffineConstraints object with an index set "
2873 "that stated that vectors have size " +
2874 std::to_string(locally_owned_dofs.size()) +
2875 " entries, but you are now calling "
2876 "AffineConstraints::distribute() with a vector "
2877 "of size " +
2878 std::to_string(vec_owned_elements.size()) +
2879 "."));
2880 }
2881
2882
2883 // Check that the set of indices we will import is a superset of
2884 // the locally-owned ones. This *should* be the case if, as one
2885 // would expect, the AffineConstraint object was initialized
2886 // with a locally-relevant index set that is indeed a superset
2887 // of the locally-owned indices. But you never know what people
2888 // pass as arguments...
2889 if constexpr (running_in_debug_mode())
2890 {
2891 if (needed_elements_for_distribute != IndexSet())
2892 {
2893 Assert(vec_owned_elements.size() ==
2894 needed_elements_for_distribute.size(),
2895 ExcMessage(
2896 "You have previously initialized this "
2897 "AffineConstraints object with an index set "
2898 "that stated that vectors have size " +
2899 std::to_string(locally_owned_dofs.size()) +
2900 " entries, but you are now calling "
2901 "AffineConstraints::distribute() with a vector "
2902 "of size " +
2903 std::to_string(vec_owned_elements.size()) + "."));
2904
2905 for (const auto i : vec_owned_elements)
2906 Assert(needed_elements_for_distribute.is_element(i),
2907 ExcInternalError());
2908 }
2909 }
2910
2911 VectorType ghosted_vector;
2912
2913 // It is possible that the user is using a parallel vector type,
2914 // but is running a non-parallel program (for example, step-31
2915 // does this). In this case, it is (perhaps?) not a bug to not
2916 // set an IndexSet for the local_lines and the
2917 // locally_owned_lines -- they are simply both empty sets in
2918 // that case. If that is so, we could just assign
2919 // 'ghosted_vector=vec;'. But this is dangerous. Not having set
2920 // index sets could also have been a bug, and we would get
2921 // downstream errors about accessing elements that are not
2922 // locally available. Rather, if no index sets were provided,
2923 // simply import the *entire* vector.
2924 if (local_lines != IndexSet())
2925 {
2926 Assert(vec_owned_elements.size() ==
2927 needed_elements_for_distribute.size(),
2928 ExcInternalError());
2929
2930 Assert(needed_elements_for_distribute != IndexSet(),
2931 ExcInternalError());
2932 internal::import_vector_with_ghost_elements(
2933 vec,
2934 vec_owned_elements,
2935 needed_elements_for_distribute,
2936 ghosted_vector,
2937 std::bool_constant<IsBlockVector<VectorType>::value>());
2938 }
2939 else
2940 {
2941 Assert(needed_elements_for_distribute == IndexSet(),
2942 ExcInternalError());
2943
2944 // TODO: We should really consider it a bug if this parallel
2945 // truly is distributed (and not just a parallel vector type
2946 // used for a sequential program). Assert that we are really
2947 // working in a sequential context.
2948
2949 internal::import_vector_with_ghost_elements(
2950 vec,
2951 vec_owned_elements,
2952 complete_index_set(vec_owned_elements.size()),
2953 ghosted_vector,
2954 std::bool_constant<IsBlockVector<VectorType>::value>());
2955 }
2956
2957 for (const ConstraintLine &line : lines)
2958 if (vec_owned_elements.is_element(line.index))
2959 {
2960 typename VectorType::value_type new_value = line.inhomogeneity;
2961 for (const std::pair<size_type, number> &entry : line.entries)
2962 new_value +=
2963 (static_cast<typename VectorType::value_type>(
2964 internal::ElementAccess<VectorType>::get(ghosted_vector,
2965 entry.first)) *
2966 entry.second);
2967 AssertIsFinite(new_value);
2968 internal::ElementAccess<VectorType>::set(new_value,
2969 line.index,
2970 vec);
2971 }
2972
2973 // now compress to communicate the entries that we added to
2974 // and that weren't to local processors to the owner
2975 //
2976 // this shouldn't be strictly necessary but it probably doesn't
2977 // hurt either
2978 vec.compress(VectorOperation::insert);
2979 }
2980 }
2981 else
2982 // purely sequential vector (either because the type doesn't
2983 // support anything else or because it's completely stored
2984 // locally)
2985 {
2986 for (const ConstraintLine &next_constraint : lines)
2987 {
2988 // fill entry in line
2989 // next_constraint.index by adding the
2990 // different contributions
2991 typename VectorType::value_type new_value =
2992 next_constraint.inhomogeneity;
2993 for (const std::pair<size_type, number> &entry :
2994 next_constraint.entries)
2995 new_value +=
2996 (static_cast<typename VectorType::value_type>(
2997 internal::ElementAccess<VectorType>::get(vec, entry.first)) *
2998 entry.second);
2999 AssertIsFinite(new_value);
3000 internal::ElementAccess<VectorType>::set(new_value,
3001 next_constraint.index,
3002 vec);
3003 }
3004 }
3005 }
3006
3007 // Some helper definitions for the local_to_global functions.
3008 namespace internal
3009 {
3010 namespace AffineConstraints
3011 {
3012 inline Distributing::Distributing(const size_type global_row,
3013 const size_type local_row)
3014 : global_row(global_row)
3015 , local_row(local_row)
3016 , constraint_position(numbers::invalid_size_type)
3017 {}
3018
3019
3020
3021 inline Distributing::Distributing(const Distributing &in)
3022 : constraint_position(numbers::invalid_size_type)
3023 {
3024 *this = (in);
3025 }
3026
3027
3028
3029 inline Distributing &
3030 Distributing::operator=(const Distributing &in)
3031 {
3032 global_row = in.global_row;
3033 local_row = in.local_row;
3034 // the constraints pointer should not contain any data here.
3035 Assert(constraint_position == numbers::invalid_size_type,
3036 ExcInternalError());
3037
3038 if (in.constraint_position != numbers::invalid_size_type)
3039 {
3040 constraint_position = in.constraint_position;
3041 in.constraint_position = numbers::invalid_size_type;
3042 }
3043 return *this;
3044 }
3045
3046
3047
3048 template <typename number>
3049 DataCache<number>::DataCache()
3050 : row_length(8)
3051 {}
3052
3053
3054
3055 template <typename number>
3056 void
3057 DataCache<number>::reinit()
3058 {
3059 individual_size.resize(0);
3060 data.resize(0);
3061 }
3062
3063
3064
3065 template <typename number>
3066 size_type
3067 DataCache<number>::insert_new_index(
3068 const std::pair<size_type, number> &pair)
3069 {
3070 Assert(row_length > 0, ExcInternalError());
3071 const unsigned int index = individual_size.size();
3072 individual_size.push_back(1);
3073 data.resize(individual_size.size() * row_length);
3074 data[index * row_length] = pair;
3075 individual_size[index] = 1;
3076 return index;
3077 }
3078
3079
3080
3081 template <typename number>
3082 void
3083 DataCache<number>::append_index(const size_type index,
3084 const std::pair<size_type, number> &pair)
3085 {
3086 AssertIndexRange(index, individual_size.size());
3087 const size_type my_length = individual_size[index];
3088 if (my_length == row_length)
3089 {
3090 AssertDimension(data.size(), individual_size.size() * row_length);
3091 // no space left in this row, need to double row_length and
3092 // rearrange the data items. Move all items to the right except the
3093 // first one, starting at the back. Since individual_size contains
3094 // at least one element when we get here, subtracting 1 works fine.
3095 data.resize(2 * data.size());
3096 for (size_type i = individual_size.size() - 1; i > 0; --i)
3097 {
3098 const auto ptr = data.data();
3099 std::move_backward(ptr + i * row_length,
3100 ptr + i * row_length + individual_size[i],
3101 ptr + i * 2 * row_length + individual_size[i]);
3102 }
3103 row_length *= 2;
3104 }
3105 data[index * row_length + my_length] = pair;
3106 individual_size[index] = my_length + 1;
3107 }
3108
3109
3110
3111 template <typename number>
3112 size_type
3113 DataCache<number>::get_size(const size_type index) const
3114 {
3115 return individual_size[index];
3116 }
3117
3118
3119
3120 template <typename number>
3121 const std::pair<size_type, number> *
3122 DataCache<number>::get_entry(const size_type index) const
3123 {
3124 return &data[index * row_length];
3125 }
3126
3127
3128
3129 template <typename number>
3130 GlobalRowsFromLocal<number>::GlobalRowsFromLocal()
3131 : n_active_rows(0)
3132 , n_inhomogeneous_rows(0)
3133 {}
3134
3135
3136
3137 template <typename number>
3138 void
3139 GlobalRowsFromLocal<number>::reinit(const size_type n_local_rows)
3140 {
3141 total_row_indices.resize(n_local_rows);
3142 for (unsigned int i = 0; i < n_local_rows; ++i)
3143 total_row_indices[i].constraint_position = numbers::invalid_size_type;
3144 n_active_rows = n_local_rows;
3145 n_inhomogeneous_rows = 0;
3146 data_cache.reinit();
3147 }
3148
3149
3150
3151 template <typename number>
3152 void
3153 GlobalRowsFromLocal<number>::print(std::ostream &os)
3154 {
3155 os << "Active rows " << n_active_rows << std::endl
3156 << "Constr rows " << n_constraints() << std::endl
3157 << "Inhom rows " << n_inhomogeneous_rows << std::endl
3158 << "Local: ";
3159 for (const auto &total_row_index : total_row_indices)
3160 os << ' ' << std::setw(4) << total_row_index.local_row;
3161 os << std::endl << "Global:";
3162 for (const auto &total_row_index : total_row_indices)
3163 os << ' ' << std::setw(4) << total_row_index.global_row;
3164 os << std::endl << "ConPos:";
3165 for (const auto &total_row_index : total_row_indices)
3166 os << ' ' << std::setw(4) << total_row_index.constraint_position;
3167 os << std::endl;
3168 }
3169
3170
3171
3172 template <typename number>
3173 size_type
3174 GlobalRowsFromLocal<number>::size() const
3175 {
3176 return n_active_rows;
3177 }
3178
3179
3180
3181 template <typename number>
3182 size_type
3183 GlobalRowsFromLocal<number>::size(const size_type counter_index) const
3184 {
3185 return (total_row_indices[counter_index].constraint_position ==
3186 numbers::invalid_size_type ?
3187 0 :
3188 data_cache.get_size(
3189 total_row_indices[counter_index].constraint_position));
3190 }
3191
3192
3193
3194 template <typename number>
3195 size_type
3196 GlobalRowsFromLocal<number>::global_row(const size_type counter_index) const
3197 {
3198 return total_row_indices[counter_index].global_row;
3199 }
3200
3201
3202
3203 template <typename number>
3204 size_type &
3205 GlobalRowsFromLocal<number>::global_row(const size_type counter_index)
3206 {
3207 return total_row_indices[counter_index].global_row;
3208 }
3209
3210
3211
3212 template <typename number>
3213 size_type
3214 GlobalRowsFromLocal<number>::local_row(const size_type counter_index) const
3215 {
3216 return total_row_indices[counter_index].local_row;
3217 }
3218
3219
3220
3221 template <typename number>
3222 size_type &
3223 GlobalRowsFromLocal<number>::local_row(const size_type counter_index)
3224 {
3225 return total_row_indices[counter_index].local_row;
3226 }
3227
3228
3229
3230 template <typename number>
3231 size_type
3232 GlobalRowsFromLocal<number>::local_row(
3233 const size_type counter_index,
3234 const size_type index_in_constraint) const
3235 {
3236 return (data_cache.get_entry(total_row_indices[counter_index]
3237 .constraint_position)[index_in_constraint])
3238 .first;
3239 }
3240
3241
3242
3243 template <typename number>
3244 number
3245 GlobalRowsFromLocal<number>::constraint_value(
3246 const size_type counter_index,
3247 const size_type index_in_constraint) const
3248 {
3249 return (data_cache.get_entry(total_row_indices[counter_index]
3250 .constraint_position)[index_in_constraint])
3251 .second;
3252 }
3253
3254
3255
3256 template <typename number>
3257 bool
3258 GlobalRowsFromLocal<number>::have_indirect_rows() const
3259 {
3260 return data_cache.individual_size.empty() == false;
3261 }
3262
3263
3264
3265 template <typename number>
3266 void
3267 GlobalRowsFromLocal<number>::insert_constraint(
3268 const size_type constrained_local_dof)
3269 {
3270 --n_active_rows;
3271 total_row_indices[n_active_rows].local_row = constrained_local_dof;
3272 total_row_indices[n_active_rows].global_row = numbers::invalid_size_type;
3273 }
3274
3275
3276
3277 template <typename number>
3278 size_type
3279 GlobalRowsFromLocal<number>::n_constraints() const
3280 {
3281 return total_row_indices.size() - n_active_rows;
3282 }
3283
3284
3285
3286 template <typename number>
3287 size_type
3288 GlobalRowsFromLocal<number>::n_inhomogeneities() const
3289 {
3290 return n_inhomogeneous_rows;
3291 }
3292
3293
3294
3295 template <typename number>
3296 void
3297 GlobalRowsFromLocal<number>::set_ith_constraint_inhomogeneous(
3298 const size_type i)
3299 {
3300 Assert(i >= n_inhomogeneous_rows, ExcInternalError());
3301 std::swap(total_row_indices[n_active_rows + i],
3302 total_row_indices[n_active_rows + n_inhomogeneous_rows]);
3303 ++n_inhomogeneous_rows;
3304 }
3305
3306
3307
3308 template <typename number>
3309 size_type
3310 GlobalRowsFromLocal<number>::constraint_origin(size_type i) const
3311 {
3312 return total_row_indices[n_active_rows + i].local_row;
3313 }
3314
3315
3316 // a function that appends an additional row to the list of values, or
3317 // appends a value to an already existing row. Similar functionality as for
3318 // std::map<size_type,Distributing>, but here done for a
3319 // std::vector<Distributing>, much faster for short lists as we have them
3320 // here
3321 template <typename number>
3322 inline void
3323 GlobalRowsFromLocal<number>::insert_index(const size_type global_row,
3324 const size_type local_row,
3325 const number constraint_value)
3326 {
3327 using index_iterator = std::vector<Distributing>::iterator;
3328 index_iterator pos, pos1;
3329 Distributing row_value(global_row);
3330 std::pair<size_type, number> constraint(local_row, constraint_value);
3331
3332 // check whether the list was really sorted before entering here
3333 for (size_type i = 1; i < n_active_rows; ++i)
3334 Assert(total_row_indices[i - 1] < total_row_indices[i],
3335 ExcInternalError());
3336
3337 pos = Utilities::lower_bound(total_row_indices.begin(),
3338 total_row_indices.begin() + n_active_rows,
3339 row_value);
3340 if (pos->global_row == global_row)
3341 pos1 = pos;
3342 else
3343 {
3344 pos1 = total_row_indices.insert(pos, row_value);
3345 ++n_active_rows;
3346 }
3347
3348 if (pos1->constraint_position == numbers::invalid_size_type)
3349 pos1->constraint_position = data_cache.insert_new_index(constraint);
3350 else
3351 data_cache.append_index(pos1->constraint_position, constraint);
3352 }
3353
3354 // this sort algorithm sorts std::vector<Distributing>, but does not take
3355 // the constraints into account. this means that in case that constraints
3356 // are already inserted, this function does not work as expected. Use
3357 // shellsort, which is very fast in case the indices are already sorted
3358 // (which is the usual case with DG elements), and not too slow in other
3359 // cases
3360 template <typename number>
3361 inline void
3362 GlobalRowsFromLocal<number>::sort()
3363 {
3364 size_type i, j, j2, temp, templ, istep;
3365 size_type step;
3366
3367 // check whether the constraints are really empty.
3368 const size_type length = size();
3369
3370 // make sure that we are in the range of the vector
3371 AssertIndexRange(length, total_row_indices.size() + 1);
3372 for (size_type i = 0; i < length; ++i)
3373 Assert(total_row_indices[i].constraint_position ==
3374 numbers::invalid_size_type,
3375 ExcInternalError());
3376
3377 step = length / 2;
3378 while (step > 0)
3379 {
3380 for (i = step; i < length; ++i)
3381 {
3382 istep = step;
3383 j = i;
3384 j2 = j - istep;
3385 temp = total_row_indices[i].global_row;
3386 templ = total_row_indices[i].local_row;
3387 if (total_row_indices[j2].global_row > temp)
3388 {
3389 while ((j >= istep) &&
3390 (total_row_indices[j2].global_row > temp))
3391 {
3392 total_row_indices[j].global_row =
3393 total_row_indices[j2].global_row;
3394 total_row_indices[j].local_row =
3395 total_row_indices[j2].local_row;
3396 j = j2;
3397 j2 -= istep;
3398 }
3399 total_row_indices[j].global_row = temp;
3400 total_row_indices[j].local_row = templ;
3401 }
3402 }
3403 step = step >> 1;
3404 }
3405 }
3406
3407
3408
3409 /**
3410 * This class is an accessor class to scratch data that is used
3411 * during calls to distribute_local_to_global and
3412 * add_entries_local_to_global. In order to avoid frequent memory
3413 * allocation, we keep the data alive from one call to the next in
3414 * a static variable. Since we want to allow for different number
3415 * types in matrices, this is a template.
3416 *
3417 * Since each thread gets its private version of scratch data out of the
3418 * ThreadLocalStorage, no conflicting access can occur. For this to be
3419 * valid, we need to make sure that no call within
3420 * distribute_local_to_global is made that by itself can spawn tasks.
3421 * Otherwise, we might end up in a situation where several threads fight for
3422 * the data.
3423 */
3424 template <typename number>
3425 class ScratchDataAccessor
3426 {
3427 public:
3428 /**
3429 * Constructor. Takes the scratch data object for the current
3430 * thread out of the provided object and marks it as used.
3431 */
3432 ScratchDataAccessor(
3433 Threads::ThreadLocalStorage<ScratchData<number>> &tls_scratch_data)
3434 : my_scratch_data(&tls_scratch_data.get())
3435 {
3436 Assert(my_scratch_data->in_use == false,
3437 ExcMessage(
3438 "Access to thread-local scratch data tried, but it is already "
3439 "in use"));
3440 my_scratch_data->in_use = true;
3441 }
3442
3443 /**
3444 * Destructor. Mark scratch data as available again.
3445 */
3446 ~ScratchDataAccessor()
3447 {
3448 my_scratch_data->in_use = false;
3449 }
3450
3451 /**
3452 * Dereferencing operator.
3453 */
3454 ScratchData<number> &
3455 operator*()
3456 {
3457 return *my_scratch_data;
3458 }
3459
3460 /**
3461 * Dereferencing operator.
3462 */
3463 ScratchData<number> *
3464 operator->()
3465 {
3466 return my_scratch_data;
3467 }
3468
3469 private:
3470 ScratchData<number> *my_scratch_data;
3471 };
3472
3473
3474 // function for block matrices: Find out where in the list of local dofs
3475 // (sorted according to global ids) the individual blocks start.
3476 // Transform the global indices to block-local indices in order to be
3477 // able to use functions like vector.block(1)(block_local_id), instead of
3478 // vector(global_id). This avoids transforming indices one-by-one later
3479 // on.
3480 template <typename number, typename BlockType>
3481 inline void
3482 make_block_starts(const BlockType &block_object,
3483 GlobalRowsFromLocal<number> &global_rows,
3484 std::vector<size_type> &block_starts)
3485 {
3486 AssertDimension(block_starts.size(), block_object.n_block_rows() + 1);
3487
3488 using row_iterator = std::vector<Distributing>::iterator;
3489 row_iterator block_indices = global_rows.total_row_indices.begin();
3490
3491 const size_type num_blocks = block_object.n_block_rows();
3492 const size_type n_active_rows = global_rows.size();
3493
3494 // find end of rows.
3495 block_starts[0] = 0;
3496 for (size_type i = 1; i < num_blocks; ++i)
3497 {
3498 row_iterator first_block = Utilities::lower_bound(
3499 block_indices,
3500 global_rows.total_row_indices.begin() + n_active_rows,
3501 Distributing(block_object.get_row_indices().block_start(i)));
3502 block_starts[i] = first_block - global_rows.total_row_indices.begin();
3503 block_indices = first_block;
3504 }
3505 block_starts[num_blocks] = n_active_rows;
3506
3507 // transform row indices to block-local index space
3508 for (size_type i = block_starts[1]; i < n_active_rows; ++i)
3509 global_rows.global_row(i) =
3510 block_object.get_row_indices()
3511 .global_to_local(global_rows.global_row(i))
3512 .second;
3513 }
3514
3515 // same as before, but for std::vector<uint> instead of
3516 // GlobalRowsFromLocal. Used in functions for sparsity patterns.
3517 template <typename BlockType>
3518 inline void
3519 make_block_starts(const BlockType &block_object,
3520 std::vector<size_type> &row_indices,
3521 std::vector<size_type> &block_starts)
3522 {
3523 AssertDimension(block_starts.size(), block_object.n_block_rows() + 1);
3524
3525 using row_iterator = std::vector<size_type>::iterator;
3526 row_iterator col_indices = row_indices.begin();
3527
3528 const size_type num_blocks = block_object.n_block_rows();
3529
3530 // find end of rows.
3531 block_starts[0] = 0;
3532 for (size_type i = 1; i < num_blocks; ++i)
3533 {
3534 row_iterator first_block =
3535 Utilities::lower_bound(col_indices,
3536 row_indices.end(),
3537 block_object.get_row_indices().block_start(
3538 i));
3539 block_starts[i] = first_block - row_indices.begin();
3540 col_indices = first_block;
3541 }
3542 block_starts[num_blocks] = row_indices.size();
3543
3544 // transform row indices to local index space
3545 for (size_type i = block_starts[1]; i < row_indices.size(); ++i)
3546 row_indices[i] =
3547 block_object.get_row_indices().global_to_local(row_indices[i]).second;
3548 }
3549
3550 // resolves constraints of one column at the innermost loop. goes through
3551 // the origin of each global entry and finds out which data we need to
3552 // collect.
3553 template <typename number>
3554 inline number
3555 resolve_matrix_entry(const GlobalRowsFromLocal<number> &global_rows,
3556 const GlobalRowsFromLocal<number> &global_cols,
3557 const size_type i,
3558 const size_type j,
3559 const size_type loc_row,
3560 const FullMatrix<number> &local_matrix)
3561 {
3562 const size_type loc_col = global_cols.local_row(j);
3563 number col_val;
3564
3565 // case 1: row has direct contribution in local matrix. decide whether col
3566 // has a direct contribution. if not, set the value to zero.
3567 if (loc_row != numbers::invalid_size_type)
3568 {
3569 col_val = ((loc_col != numbers::invalid_size_type) ?
3570 local_matrix(loc_row, loc_col) :
3571 0);
3572
3573 // account for indirect contributions by constraints in column
3574 for (size_type p = 0; p < global_cols.size(j); ++p)
3575 col_val += (local_matrix(loc_row, global_cols.local_row(j, p)) *
3576 global_cols.constraint_value(j, p));
3577 }
3578
3579 // case 2: row has no direct contribution in local matrix
3580 else
3581 col_val = 0;
3582
3583 // account for indirect contributions by constraints in row, going through
3584 // the direct and indirect references in the given column.
3585 for (size_type q = 0; q < global_rows.size(i); ++q)
3586 {
3587 number add_this =
3588 (loc_col != numbers::invalid_size_type) ?
3589 local_matrix(global_rows.local_row(i, q), loc_col) :
3590 0;
3591
3592 for (size_type p = 0; p < global_cols.size(j); ++p)
3593 add_this += (local_matrix(global_rows.local_row(i, q),
3594 global_cols.local_row(j, p)) *
3595 global_cols.constraint_value(j, p));
3596 col_val += add_this * global_rows.constraint_value(i, q);
3597 }
3598 return col_val;
3599 }
3600
3601 // computes all entries that need to be written into global_rows[i]. Lists
3602 // the resulting values in val_ptr, and the corresponding column indices in
3603 // col_ptr.
3604 template <typename number>
3605 inline void
3606 resolve_matrix_row(const GlobalRowsFromLocal<number> &global_rows,
3607 const GlobalRowsFromLocal<number> &global_cols,
3608 const size_type i,
3609 const size_type column_start,
3610 const size_type column_end,
3611 const FullMatrix<number> &local_matrix,
3612 size_type *&col_ptr,
3613 number *&val_ptr)
3614 {
3615 if (column_end == column_start)
3616 return;
3617
3618 AssertIndexRange(column_end - 1, global_cols.size());
3619 const size_type loc_row = global_rows.local_row(i);
3620
3621 // fast function if there are no indirect references to any of the local
3622 // rows at all on this set of dofs (saves a lot of checks). the only check
3623 // we actually need to perform is whether the matrix element is zero.
3624 if (global_rows.have_indirect_rows() == false &&
3625 global_cols.have_indirect_rows() == false)
3626 {
3627 AssertIndexRange(loc_row, local_matrix.m());
3628 const number *matrix_ptr = &local_matrix(loc_row, 0);
3629
3630 for (size_type j = column_start; j < column_end; ++j)
3631 {
3632 const size_type loc_col = global_cols.local_row(j);
3633 AssertIndexRange(loc_col, local_matrix.n());
3634 const number col_val = matrix_ptr[loc_col];
3635 if (col_val != number())
3636 {
3637 *val_ptr++ = static_cast<number>(col_val);
3638 *col_ptr++ = global_cols.global_row(j);
3639 }
3640 }
3641 }
3642
3643 // more difficult part when there are indirect references and when we need
3644 // to do some more checks.
3645 else
3646 {
3647 for (size_type j = column_start; j < column_end; ++j)
3648 {
3649 number col_val = resolve_matrix_entry(
3650 global_rows, global_cols, i, j, loc_row, local_matrix);
3651
3652 // if we got some nontrivial value, append it to the array of
3653 // values.
3654 if (col_val != number())
3655 {
3656 *val_ptr++ = static_cast<number>(col_val);
3657 *col_ptr++ = global_cols.global_row(j);
3658 }
3659 }
3660 }
3661 }
3662
3663 // specialized function that can write into the row of a
3664 // SparseMatrix<number>.
3665 namespace dealiiSparseMatrix
3666 {
3667 template <typename SparseMatrixIterator, typename LocalType>
3668 inline void
3669 add_value(const LocalType value,
3670 const size_type row,
3671 const size_type column,
3672 SparseMatrixIterator &matrix_values)
3673 {
3674 if (value != LocalType())
3675 {
3676 while (matrix_values->column() < column)
3677 ++matrix_values;
3678
3679 // Ensure that we haven't walked off the end of the row and
3680 // accidentally found the column in a later row. This should
3681 // not have happened since we believed we know that we enter
3682 // entries sorted by column, and that all columns are represented
3683 // in the current matrix row. But it's worth checking.
3684 Assert(matrix_values->row() == row, ExcInternalError());
3685
3686 // Then also assert that the column index actually exists.
3687 Assert(matrix_values->column() == column,
3688 typename SparseMatrix<
3689 typename SparseMatrixIterator::MatrixType::value_type>::
3690 ExcInvalidIndex(row, column));
3691
3692 // Now so convinced, let us add the relevant value:
3693 matrix_values->value() += value;
3694 }
3695 }
3696 } // namespace dealiiSparseMatrix
3697
3698 // similar as before, now with shortcut for deal.II sparse matrices. this
3699 // lets us avoid using extra arrays, and does all the operations just in
3700 // place, i.e., in the respective matrix row
3701 template <typename number>
3702 inline void
3703 resolve_matrix_row(const GlobalRowsFromLocal<number> &global_rows,
3704 const size_type i,
3705 const size_type column_start,
3706 const size_type column_end,
3707 const FullMatrix<number> &local_matrix,
3708 SparseMatrix<number> *sparse_matrix)
3709 {
3710 if (column_end == column_start)
3711 return;
3712
3713 AssertIndexRange(column_end - 1, global_rows.size());
3714 const SparsityPattern &sparsity = sparse_matrix->get_sparsity_pattern();
3715
3716 if (sparsity.n_nonzero_elements() == 0)
3717 return;
3718
3719 const size_type row = global_rows.global_row(i);
3720 const size_type loc_row = global_rows.local_row(i);
3721
3722 typename SparseMatrix<number>::iterator matrix_values =
3723 sparse_matrix->begin(row);
3724 const bool optimize_diagonal = sparsity.n_rows() == sparsity.n_cols();
3725
3726 // distinguish three cases about what can happen for checking whether the
3727 // diagonal is the first element of the row. this avoids if statements at
3728 // the innermost loop positions
3729
3730 if (!optimize_diagonal) // case 1: no diagonal optimization in matrix
3731 {
3732 if (global_rows.have_indirect_rows() == false)
3733 {
3734 AssertIndexRange(loc_row, local_matrix.m());
3735 const number *matrix_ptr = &local_matrix(loc_row, 0);
3736
3737 for (size_type j = column_start; j < column_end; ++j)
3738 {
3739 const size_type loc_col = global_rows.local_row(j);
3740 const number col_val = matrix_ptr[loc_col];
3741 dealiiSparseMatrix::add_value(col_val,
3742 row,
3743 global_rows.global_row(j),
3744 matrix_values);
3745 }
3746 }
3747 else
3748 {
3749 for (size_type j = column_start; j < column_end; ++j)
3750 {
3751 number col_val = resolve_matrix_entry(
3752 global_rows, global_rows, i, j, loc_row, local_matrix);
3753 dealiiSparseMatrix::add_value(col_val,
3754 row,
3755 global_rows.global_row(j),
3756 matrix_values);
3757 }
3758 }
3759 }
3760 else if (i >= column_start && i < column_end) // case 2: can split loop
3761 {
3762 ++matrix_values; // jump over diagonal element
3763 if (global_rows.have_indirect_rows() == false)
3764 {
3765 AssertIndexRange(loc_row, local_matrix.m());
3766 const number *matrix_ptr = &local_matrix(loc_row, 0);
3767
3768 sparse_matrix->begin(row)->value() += matrix_ptr[loc_row];
3769 for (size_type j = column_start; j < i; ++j)
3770 {
3771 const size_type loc_col = global_rows.local_row(j);
3772 const number col_val = matrix_ptr[loc_col];
3773 dealiiSparseMatrix::add_value(col_val,
3774 row,
3775 global_rows.global_row(j),
3776 matrix_values);
3777 }
3778 for (size_type j = i + 1; j < column_end; ++j)
3779 {
3780 const size_type loc_col = global_rows.local_row(j);
3781 const number col_val = matrix_ptr[loc_col];
3782 dealiiSparseMatrix::add_value(col_val,
3783 row,
3784 global_rows.global_row(j),
3785 matrix_values);
3786 }
3787 }
3788 else
3789 {
3790 sparse_matrix->begin(row)->value() += resolve_matrix_entry(
3791 global_rows, global_rows, i, i, loc_row, local_matrix);
3792 for (size_type j = column_start; j < i; ++j)
3793 {
3794 number col_val = resolve_matrix_entry(
3795 global_rows, global_rows, i, j, loc_row, local_matrix);
3796 dealiiSparseMatrix::add_value(col_val,
3797 row,
3798 global_rows.global_row(j),
3799 matrix_values);
3800 }
3801 for (size_type j = i + 1; j < column_end; ++j)
3802 {
3803 number col_val = resolve_matrix_entry(
3804 global_rows, global_rows, i, j, loc_row, local_matrix);
3805 dealiiSparseMatrix::add_value(col_val,
3806 row,
3807 global_rows.global_row(j),
3808 matrix_values);
3809 }
3810 }
3811 }
3812 // case 3: can't say - need to check inside the loop
3813 else if (global_rows.have_indirect_rows() == false)
3814 {
3815 ++matrix_values; // jump over diagonal element
3816 AssertIndexRange(loc_row, local_matrix.m());
3817 const number *matrix_ptr = &local_matrix(loc_row, 0);
3818
3819 for (size_type j = column_start; j < column_end; ++j)
3820 {
3821 const size_type loc_col = global_rows.local_row(j);
3822 const number col_val = matrix_ptr[loc_col];
3823 if (row == global_rows.global_row(j))
3824 sparse_matrix->begin(row)->value() += col_val;
3825 else
3826 dealiiSparseMatrix::add_value(col_val,
3827 row,
3828 global_rows.global_row(j),
3829 matrix_values);
3830 }
3831 }
3832 else
3833 {
3834 ++matrix_values; // jump over diagonal element
3835 for (size_type j = column_start; j < column_end; ++j)
3836 {
3837 number col_val = resolve_matrix_entry(
3838 global_rows, global_rows, i, j, loc_row, local_matrix);
3839 if (row == global_rows.global_row(j))
3840 sparse_matrix->begin(row)->value() += col_val;
3841 else
3842 dealiiSparseMatrix::add_value(col_val,
3843 row,
3844 global_rows.global_row(j),
3845 matrix_values);
3846 }
3847 }
3848 }
3849
3850 // Same function to resolve all entries that will be added to the given
3851 // global row global_rows[i] as before, now for sparsity pattern
3852 template <typename number>
3853 inline void
3854 resolve_matrix_row(const GlobalRowsFromLocal<number> &global_rows,
3855 const size_type i,
3856 const size_type column_start,
3857 const size_type column_end,
3858 const Table<2, bool> &dof_mask,
3859 std::vector<size_type>::iterator &col_ptr)
3860 {
3861 if (column_end == column_start)
3862 return;
3863
3864 const size_type loc_row = global_rows.local_row(i);
3865
3866 // fast function if there are no indirect references to any of the local
3867 // rows at all on this set of dofs
3868 if (global_rows.have_indirect_rows() == false)
3869 {
3870 Assert(loc_row < dof_mask.n_rows(), ExcInternalError());
3871
3872 for (size_type j = column_start; j < column_end; ++j)
3873 {
3874 const size_type loc_col = global_rows.local_row(j);
3875 Assert(loc_col < dof_mask.n_cols(), ExcInternalError());
3876
3877 if (dof_mask(loc_row, loc_col) == true)
3878 *col_ptr++ = global_rows.global_row(j);
3879 }
3880 }
3881
3882 // slower functions when there are indirect references and when we need to
3883 // do some more checks.
3884 else
3885 {
3886 for (size_type j = column_start; j < column_end; ++j)
3887 {
3888 const size_type loc_col = global_rows.local_row(j);
3889 if (loc_row != numbers::invalid_size_type)
3890 {
3891 Assert(loc_row < dof_mask.n_rows(), ExcInternalError());
3892 if (loc_col != numbers::invalid_size_type)
3893 {
3894 Assert(loc_col < dof_mask.n_cols(), ExcInternalError());
3895 if (dof_mask(loc_row, loc_col) == true)
3896 goto add_this_index;
3897 }
3898
3899 for (size_type p = 0; p < global_rows.size(j); ++p)
3900 if (dof_mask(loc_row, global_rows.local_row(j, p)) == true)
3901 goto add_this_index;
3902 }
3903
3904 for (size_type q = 0; q < global_rows.size(i); ++q)
3905 {
3906 if (loc_col != numbers::invalid_size_type)
3907 {
3908 Assert(loc_col < dof_mask.n_cols(), ExcInternalError());
3909 if (dof_mask(global_rows.local_row(i, q), loc_col) ==
3910 true)
3911 goto add_this_index;
3912 }
3913
3914 for (size_type p = 0; p < global_rows.size(j); ++p)
3915 if (dof_mask(global_rows.local_row(i, q),
3916 global_rows.local_row(j, p)) == true)
3917 goto add_this_index;
3918 }
3919
3920 continue;
3921 // if we got some nontrivial value, append it to the array of
3922 // values.
3923 add_this_index:
3924 *col_ptr++ = global_rows.global_row(j);
3925 }
3926 }
3927 }
3928
3929 // to make sure that the global matrix remains invertible, we need to do
3930 // something with the diagonal elements. Add the average of the
3931 // absolute values of the local matrix diagonals, so the resulting entry
3932 // will always be positive and furthermore be in the same order of magnitude
3933 // as the other elements of the matrix. If all local matrix diagonals are
3934 // zero, add the l1 norm of the local matrix divided by the matrix size
3935 // to the diagonal of the global matrix. If the entire local matrix is zero,
3936 // add 1 to the diagonal of the global matrix.
3937 //
3938 // note that this also captures the special case that a dof is both
3939 // constrained and fixed (this can happen for hanging nodes in 3d that also
3940 // happen to be on the boundary). in that case, following the program flow
3941 // in distribute_local_to_global, it is realized that when distributing the
3942 // row and column no elements of the matrix are actually touched if all the
3943 // degrees of freedom to which this dof is constrained are also constrained
3944 // (the usual case with hanging nodes in 3d). however, in the line below, we
3945 // do actually do something with this dof
3946 template <typename number, typename MatrixType, typename VectorType>
3947 inline void
3948 set_matrix_diagonals(
3949 const internal::AffineConstraints::GlobalRowsFromLocal<number>
3950 &global_rows,
3951 const std::vector<size_type> &local_dof_indices,
3952 const FullMatrix<number> &local_matrix,
3953 const dealii::AffineConstraints<number> &constraints,
3954 MatrixType &global_matrix,
3955 VectorType &global_vector,
3956 bool use_inhomogeneities_for_rhs)
3957 {
3958 if (global_rows.n_constraints() > 0)
3959 {
3960 number average_diagonal = number();
3961 for (size_type i = 0; i < local_matrix.m(); ++i)
3962 average_diagonal += std::abs(local_matrix(i, i));
3963 average_diagonal /= static_cast<number>(local_matrix.m());
3964
3965 // handle the case that all diagonal elements are zero
3966 if (average_diagonal == static_cast<number>(0.))
3967 {
3968 average_diagonal = static_cast<number>(local_matrix.l1_norm()) /
3969 static_cast<number>(local_matrix.m());
3970 // if the entire matrix is zero, use 1. for the diagonal
3971 if (average_diagonal == static_cast<number>(0.))
3972 average_diagonal = static_cast<number>(1.);
3973 }
3974
3975 for (size_type i = 0; i < global_rows.n_constraints(); ++i)
3976 {
3977 const size_type local_row = global_rows.constraint_origin(i);
3978 const size_type global_row = local_dof_indices[local_row];
3979
3980 const number current_diagonal =
3981 local_matrix(local_row, local_row);
3982 if (std::abs(current_diagonal) != 0.)
3983 {
3984 global_matrix.add(global_row,
3985 global_row,
3986 std::abs(current_diagonal));
3987 // if the use_inhomogeneities_for_rhs flag is set to true, the
3988 // inhomogeneities are used to create the global vector.
3989 // instead of fill in a zero in the ith components with an
3990 // inhomogeneity, we set those to:
3991 // inhomogeneity(i)*global_matrix (i,i).
3992 if (use_inhomogeneities_for_rhs == true)
3993 global_vector(global_row) +=
3994 current_diagonal *
3995 constraints.get_inhomogeneity(global_row);
3996 }
3997 else
3998 {
3999 global_matrix.add(global_row, global_row, average_diagonal);
4000
4001 if (use_inhomogeneities_for_rhs == true)
4002 global_vector(global_row) +=
4003 average_diagonal *
4004 constraints.get_inhomogeneity(global_row);
4005 }
4006 }
4007 }
4008 }
4009
4010 // similar function as the one above for setting matrix diagonals, but now
4011 // doing that for sparsity patterns when setting them up using
4012 // add_entries_local_to_global(). In case we keep constrained entries, add
4013 // all the rows and columns related to the constrained dof, otherwise just
4014 // add the diagonal
4015 template <typename number>
4016 inline void
4017 set_sparsity_diagonals(
4018 const internal::AffineConstraints::GlobalRowsFromLocal<number>
4019 &global_rows,
4020 const std::vector<size_type> &local_dof_indices,
4021 const Table<2, bool> &dof_mask,
4022 const bool keep_constrained_entries,
4023 ScratchData<number> &scratch_data,
4024 SparsityPatternBase &sparsity_pattern)
4025 {
4026 // if we got constraints, need to add the diagonal element and, if the
4027 // user requested so, also the rest of the entries in rows and columns
4028 // that have been left out above
4029 if (global_rows.n_constraints() > 0)
4030 {
4031 std::vector<std::pair<size_type, size_type>> &cell_entries =
4032 scratch_data.new_entries;
4033 cell_entries.resize(0);
4034 cell_entries.reserve(local_dof_indices.size());
4035 for (size_type i = 0; i < global_rows.n_constraints(); ++i)
4036 {
4037 const size_type local_row = global_rows.constraint_origin(i);
4038 const size_type global_row = local_dof_indices[local_row];
4039 if (keep_constrained_entries == true)
4040 {
4041 for (size_type j = 0; j < local_dof_indices.size(); ++j)
4042 {
4043 if (dof_mask(local_row, j) == true)
4044 cell_entries.emplace_back(global_row,
4045 local_dof_indices[j]);
4046 if (dof_mask(j, local_row) == true)
4047 cell_entries.emplace_back(local_dof_indices[j],
4048 global_row);
4049 }
4050 }
4051 else
4052 // don't keep constrained entries - just add the diagonal.
4053 cell_entries.emplace_back(global_row, global_row);
4054 }
4055 sparsity_pattern.add_entries(make_array_view(cell_entries));
4056 }
4057 }
4058
4059 } // end of namespace AffineConstraints
4060 } // end of namespace internal
4061
4062
4063
4064 // Basic idea of setting up a list of
4065 // all global dofs: first find all rows and columns
4066 // that we are going to write touch,
4067 // and then go through the
4068 // lines and collect all the local rows that
4069 // are related to it.
4070 template <typename number>
4071 void
4072 AffineConstraints<number>::make_sorted_row_list(
4073 const std::vector<size_type> &local_dof_indices,
4074 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows) const
4075 {
4076 const size_type n_local_dofs = local_dof_indices.size();
4077 AssertDimension(n_local_dofs, global_rows.size());
4078
4079 // when distributing the local data to the global matrix, we can quite
4080 // cheaply sort the indices (obviously, this introduces the need for
4081 // allocating some memory on the way, but we need to do this only for rows,
4082 // whereas the distribution process itself goes over rows and columns). This
4083 // has the advantage that when writing into the global matrix, we can make
4084 // use of the sortedness.
4085
4086 // so the first step is to create a sorted list of all row values that are
4087 // possible. these values are either the rows from unconstrained dofs, or
4088 // some indices introduced by dofs constrained to a combination of some
4089 // other dofs. regarding the data type, choose a <tt>std::vector</tt> of a
4090 // pair of unsigned ints (for global columns) and internal data (containing
4091 // local columns + possible jumps from constraints). Choosing
4092 // <tt>std::map</tt> or anything else M.K. knows of would be much more
4093 // expensive here!
4094
4095 // cache whether we have to resolve any indirect rows generated from
4096 // resolving constrained dofs.
4097 size_type added_rows = 0;
4098
4099 // first add the indices in an unsorted way and only keep track of the
4100 // constraints that appear. They are resolved in a second step.
4101 for (size_type i = 0; i < n_local_dofs; ++i)
4102 {
4103 if (is_constrained(local_dof_indices[i]) == false)
4104 {
4105 global_rows.global_row(added_rows) = local_dof_indices[i];
4106 global_rows.local_row(added_rows++) = i;
4107 }
4108 else
4109 global_rows.insert_constraint(i);
4110 }
4111 global_rows.sort();
4112
4113 const size_type n_constrained_rows = n_local_dofs - added_rows;
4114 for (size_type i = 0; i < n_constrained_rows; ++i)
4115 {
4116 const size_type local_row = global_rows.constraint_origin(i);
4117 AssertIndexRange(local_row, n_local_dofs);
4118 const size_type global_row = local_dof_indices[local_row];
4119 Assert(is_constrained(global_row), ExcInternalError());
4120 const ConstraintLine &position =
4121 lines[lines_cache[calculate_line_index(global_row)]];
4122 if (position.inhomogeneity != number(0.))
4123 global_rows.set_ith_constraint_inhomogeneous(i);
4124 for (size_type q = 0; q < position.entries.size(); ++q)
4125 global_rows.insert_index(position.entries[q].first,
4126 local_row,
4127 position.entries[q].second);
4128 }
4129 }
4130
4131
4132
4133 // Same function as before, but now do only extract the global indices that
4134 // come from the local ones without storing their origin. Used for sparsity
4135 // pattern generation.
4136 template <typename number>
4137 inline void
4138 AffineConstraints<number>::make_sorted_row_list(
4139 const std::vector<size_type> &local_dof_indices,
4140 std::vector<size_type> &active_dofs) const
4141 {
4142 const size_type n_local_dofs = local_dof_indices.size();
4143 size_type added_rows = 0;
4144 for (size_type i = 0; i < n_local_dofs; ++i)
4145 {
4146 if (is_constrained(local_dof_indices[i]) == false)
4147 {
4148 active_dofs[added_rows++] = local_dof_indices[i];
4149 continue;
4150 }
4151
4152 active_dofs[n_local_dofs - i + added_rows - 1] = i;
4153 }
4154 std::sort(active_dofs.begin(), active_dofs.begin() + added_rows);
4155
4156 const size_type n_constrained_dofs = n_local_dofs - added_rows;
4157 for (size_type i = n_constrained_dofs; i > 0; --i)
4158 {
4159 const size_type local_row = active_dofs.back();
4160
4161 // remove constrained entry since we are going to resolve it in place
4162 active_dofs.pop_back();
4163 const size_type global_row = local_dof_indices[local_row];
4164 const ConstraintLine &position =
4165 lines[lines_cache[calculate_line_index(global_row)]];
4166 for (size_type q = 0; q < position.entries.size(); ++q)
4167 {
4168 const size_type new_index = position.entries[q].first;
4169 // in case all dofs are constrained, we might insert at
4170 // active_dofs.begin(), but we should never insert before that
4171 AssertIndexRange(i - 1, active_dofs.size() + 1);
4172 if (i > active_dofs.size() ||
4173 active_dofs[active_dofs.size() - i] < new_index)
4174 active_dofs.insert(active_dofs.end() - i + 1, new_index);
4175
4176 // make binary search to find where to put the new index in order to
4177 // keep the list sorted
4178 else
4179 {
4180 std::vector<size_type>::iterator it =
4181 Utilities::lower_bound(active_dofs.begin(),
4182 active_dofs.end() - i + 1,
4183 new_index);
4184 if (*it != new_index)
4185 active_dofs.insert(it, new_index);
4186 }
4187 }
4188 }
4189 }
4190
4191
4192
4193 // Resolve the constraints from the vector and apply inhomogeneities.
4194 template <typename number>
4195 template <typename MatrixScalar, typename VectorScalar>
4196 inline typename ProductType<VectorScalar, MatrixScalar>::type
4197 AffineConstraints<number>::resolve_vector_entry(
4198 const size_type i,
4199 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
4200 const Vector<VectorScalar> &local_vector,
4201 const std::vector<size_type> &local_dof_indices,
4202 const FullMatrix<MatrixScalar> &local_matrix) const
4203 {
4204 const size_type loc_row = global_rows.local_row(i);
4205 const size_type n_inhomogeneous_rows = global_rows.n_inhomogeneities();
4206 typename ProductType<VectorScalar, MatrixScalar>::type val = 0;
4207 // has a direct contribution from some local entry. If we have inhomogeneous
4208 // constraints, compute the contribution of the inhomogeneity in the current
4209 // row.
4210 if (loc_row != numbers::invalid_size_type)
4211 {
4212 val = local_vector(loc_row);
4213 for (size_type i = 0; i < n_inhomogeneous_rows; ++i)
4214 val -= (local_matrix(loc_row, global_rows.constraint_origin(i)) *
4215 lines[lines_cache[calculate_line_index(
4216 local_dof_indices[global_rows.constraint_origin(i)])]]
4217 .inhomogeneity);
4218 }
4219
4220 // go through the indirect contributions
4221 for (size_type q = 0; q < global_rows.size(i); ++q)
4222 {
4223 const size_type loc_row_q = global_rows.local_row(i, q);
4224 typename ProductType<VectorScalar, MatrixScalar>::type add_this =
4225 local_vector(loc_row_q);
4226 for (size_type k = 0; k < n_inhomogeneous_rows; ++k)
4227 add_this -=
4228 (local_matrix(loc_row_q, global_rows.constraint_origin(k)) *
4229 lines[lines_cache[calculate_line_index(
4230 local_dof_indices[global_rows.constraint_origin(k)])]]
4231 .inhomogeneity);
4232 val += add_this * global_rows.constraint_value(i, q);
4233 }
4234 return val;
4235 }
4236
4237
4238
4239 // internal implementation for distribute_local_to_global for standard
4240 // (non-block) matrices
4241 template <typename number>
4242 template <typename MatrixType, typename VectorType>
4243 void
4244 AffineConstraints<number>::distribute_local_to_global(
4245 const FullMatrix<number> &local_matrix,
4246 const Vector<number> &local_vector,
4247 const std::vector<size_type> &local_dof_indices,
4248 MatrixType &global_matrix,
4249 VectorType &global_vector,
4250 const bool use_inhomogeneities_for_rhs,
4251 const std::bool_constant<false>) const
4252 {
4253 // FIXME: static_assert MatrixType::value_type == number
4254
4255 // check whether we work on real vectors or we just used a dummy when
4256 // calling the other function above.
4257 const bool use_vectors =
4258 (local_vector.size() == 0 && global_vector.size() == 0) ? false : true;
4259 const bool use_dealii_matrix =
4260 std::is_same_v<MatrixType, SparseMatrix<number>>;
4261
4262 AssertDimension(local_matrix.n(), local_dof_indices.size());
4263 AssertDimension(local_matrix.m(), local_dof_indices.size());
4264 Assert(global_vector.has_ghost_elements() == false, ExcGhostsPresent());
4265 Assert(global_matrix.m() == global_matrix.n(), ExcNotQuadratic());
4266 if (use_vectors == true)
4267 {
4268 AssertDimension(local_matrix.m(), local_vector.size());
4269 AssertDimension(global_matrix.m(), global_vector.size());
4270 }
4271 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
4272
4273 const size_type n_local_dofs = local_dof_indices.size();
4274
4275 typename internal::AffineConstraints::ScratchDataAccessor<number>
4276 scratch_data(this->scratch_data);
4277
4278 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows =
4279 scratch_data->global_rows;
4280 global_rows.reinit(n_local_dofs);
4281 make_sorted_row_list(local_dof_indices, global_rows);
4282
4283 const size_type n_actual_dofs = global_rows.size();
4284
4285 // create arrays for the column data (indices and values) that will then be
4286 // written into the matrix. Shortcut for deal.II sparse matrix. We can use
4287 // the scratch data if we have a double matrix. Otherwise, we need to create
4288 // an array in any case since we cannot know about the actual data type in
4289 // the AffineConstraints class (unless we do cast). This involves a little
4290 // bit of logic to determine the type of the matrix value.
4291 std::vector<size_type> &cols = scratch_data->columns;
4292 std::vector<number> &vals = scratch_data->values;
4293 // create arrays for writing into the vector as well
4294 std::vector<size_type> &vector_indices = scratch_data->vector_indices;
4295 std::vector<typename VectorType::value_type> &vector_values =
4296 scratch_data->vector_values;
4297 vector_indices.resize(n_actual_dofs);
4298 vector_values.resize(n_actual_dofs);
4299 SparseMatrix<number> *sparse_matrix =
4300 dynamic_cast<SparseMatrix<number> *>(&global_matrix);
4301 if (use_dealii_matrix == false)
4302 {
4303 cols.resize(n_actual_dofs);
4304 vals.resize(n_actual_dofs);
4305 }
4306 else
4307 Assert(sparse_matrix != nullptr, ExcInternalError());
4308
4309 // now do the actual job. go through all the global rows that we will touch
4310 // and call resolve_matrix_row for each of those.
4311 size_type local_row_n = 0;
4312 for (size_type i = 0; i < n_actual_dofs; ++i)
4313 {
4314 const size_type row = global_rows.global_row(i);
4315
4316 // calculate all the data that will be written into the matrix row.
4317 if (use_dealii_matrix == false)
4318 {
4319 size_type *col_ptr = cols.data();
4320 // cast is uncritical here and only used to avoid compiler
4321 // warnings. We never access a non-double array
4322 number *val_ptr = vals.data();
4323 internal::AffineConstraints::resolve_matrix_row(global_rows,
4324 global_rows,
4325 i,
4326 0,
4327 n_actual_dofs,
4328 local_matrix,
4329 col_ptr,
4330 val_ptr);
4331 const size_type n_values = col_ptr - cols.data();
4332 if (n_values > 0)
4333 global_matrix.add(row,
4334 n_values,
4335 cols.data(),
4336 vals.data(),
4337 /* elide zero additions */ false,
4338 /* sorted by column index */ true);
4339 }
4340 else
4341 internal::AffineConstraints::resolve_matrix_row(
4342 global_rows, i, 0, n_actual_dofs, local_matrix, sparse_matrix);
4343
4344 // now to the vectors. besides doing the same job as we did above (i.e.,
4345 // distribute the content of the local vector into the global one), need
4346 // to account for inhomogeneities here: this corresponds to eliminating
4347 // the respective column in the local matrix with value on the right
4348 // hand side.
4349 if (use_vectors == true)
4350 {
4351 const typename VectorType::value_type val = resolve_vector_entry(
4352 i, global_rows, local_vector, local_dof_indices, local_matrix);
4353 AssertIsFinite(val);
4354
4355 if (val != typename VectorType::value_type())
4356 {
4357 vector_indices[local_row_n] = row;
4358 vector_values[local_row_n] = val;
4359 ++local_row_n;
4360 }
4361 }
4362 }
4363 // Drop the elements of vector_indices and vector_values that we do not use
4364 // (we may always elide writing zero values to vectors)
4365 const size_type n_local_rows = local_row_n;
4366 vector_indices.resize(n_local_rows);
4367 vector_values.resize(n_local_rows);
4368
4369 // While the standard case is that these types are equal, they need not be, so
4370 // only do a bulk update if they are. Note that the types in the arguments to
4371 // add must be equal if we have a Trilinos or PETSc vector but do not have to
4372 // be if we have a deal.II native vector: one could further optimize this for
4373 // Vector, LinearAlgebra::distributed::vector, etc.
4374 if constexpr (std::is_same_v<typename VectorType::value_type, number>)
4375 {
4376 global_vector.add(vector_indices, vector_values);
4377 }
4378 else
4379 {
4380 for (size_type row_n = 0; row_n < n_local_rows; ++row_n)
4381 {
4382 global_vector(vector_indices[row_n]) +=
4383 static_cast<typename VectorType::value_type>(vector_values[row_n]);
4384 }
4385 }
4386
4387 internal::AffineConstraints::set_matrix_diagonals(
4388 global_rows,
4389 local_dof_indices,
4390 local_matrix,
4391 *this,
4392 global_matrix,
4393 global_vector,
4394 use_inhomogeneities_for_rhs);
4395 }
4396
4397
4398
4399 // similar function as above, but now specialized for block matrices. See the
4400 // other function for additional comments.
4401 template <typename number>
4402 template <typename MatrixType, typename VectorType>
4403 void
4404 AffineConstraints<number>::distribute_local_to_global(
4405 const FullMatrix<number> &local_matrix,
4406 const Vector<number> &local_vector,
4407 const std::vector<size_type> &local_dof_indices,
4408 MatrixType &global_matrix,
4409 VectorType &global_vector,
4410 const bool use_inhomogeneities_for_rhs,
4411 const std::bool_constant<true>) const
4412 {
4413 const bool use_vectors =
4414 (local_vector.size() == 0 && global_vector.size() == 0) ? false : true;
4415 const bool use_dealii_matrix =
4416 std::is_same_v<MatrixType, BlockSparseMatrix<number>>;
4417
4418 AssertDimension(local_matrix.n(), local_dof_indices.size());
4419 AssertDimension(local_matrix.m(), local_dof_indices.size());
4420 Assert(global_vector.has_ghost_elements() == false, ExcGhostsPresent());
4421 Assert(global_matrix.m() == global_matrix.n(), ExcNotQuadratic());
4422 Assert(global_matrix.n_block_rows() == global_matrix.n_block_cols(),
4423 ExcNotQuadratic());
4424 if (use_vectors == true)
4425 {
4426 AssertDimension(local_matrix.m(), local_vector.size());
4427 AssertDimension(global_matrix.m(), global_vector.size());
4428 }
4429 Assert(sorted == true, ExcMatrixNotClosed());
4430
4431 typename internal::AffineConstraints::ScratchDataAccessor<number>
4432 scratch_data(this->scratch_data);
4433
4434 const size_type n_local_dofs = local_dof_indices.size();
4435 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows =
4436 scratch_data->global_rows;
4437 global_rows.reinit(n_local_dofs);
4438
4439 make_sorted_row_list(local_dof_indices, global_rows);
4440 const size_type n_actual_dofs = global_rows.size();
4441
4442 std::vector<size_type> &global_indices = scratch_data->vector_indices;
4443 if (use_vectors == true)
4444 {
4445 global_indices.resize(n_actual_dofs);
4446 for (size_type i = 0; i < n_actual_dofs; ++i)
4447 global_indices[i] = global_rows.global_row(i);
4448 }
4449
4450 // additional construct that also takes care of block indices.
4451 const size_type num_blocks = global_matrix.n_block_rows();
4452 std::vector<size_type> &block_starts = scratch_data->block_starts;
4453 block_starts.resize(num_blocks + 1);
4454 internal::AffineConstraints::make_block_starts(global_matrix,
4455 global_rows,
4456 block_starts);
4457
4458 std::vector<size_type> &cols = scratch_data->columns;
4459 std::vector<number> &vals = scratch_data->values;
4460 if (use_dealii_matrix == false)
4461 {
4462 cols.resize(n_actual_dofs);
4463 vals.resize(n_actual_dofs);
4464 }
4465
4466 // the basic difference to the non-block variant from now onwards is that we
4467 // go through the blocks of the matrix separately, which allows us to set
4468 // the block entries individually
4469 for (size_type block = 0; block < num_blocks; ++block)
4470 {
4471 const size_type next_block = block_starts[block + 1];
4472 for (size_type i = block_starts[block]; i < next_block; ++i)
4473 {
4474 const size_type row = global_rows.global_row(i);
4475
4476 for (size_type block_col = 0; block_col < num_blocks; ++block_col)
4477 {
4478 const size_type start_block = block_starts[block_col],
4479 end_block = block_starts[block_col + 1];
4480 if (use_dealii_matrix == false)
4481 {
4482 size_type *col_ptr = cols.data();
4483 number *val_ptr = vals.data();
4484 internal::AffineConstraints::resolve_matrix_row(global_rows,
4485 global_rows,
4486 i,
4487 start_block,
4488 end_block,
4489 local_matrix,
4490 col_ptr,
4491 val_ptr);
4492 const size_type n_values = col_ptr - cols.data();
4493 if (n_values > 0)
4494 global_matrix.block(block, block_col)
4495 .add(
4496 row, n_values, cols.data(), vals.data(), false, true);
4497 }
4498 else
4499 {
4500 SparseMatrix<number> *sparse_matrix =
4501 dynamic_cast<SparseMatrix<number> *>(
4502 &global_matrix.block(block, block_col));
4503 Assert(sparse_matrix != nullptr, ExcInternalError());
4504 internal::AffineConstraints::resolve_matrix_row(
4505 global_rows,
4506 i,
4507 start_block,
4508 end_block,
4509 local_matrix,
4510 sparse_matrix);
4511 }
4512 }
4513
4514 if (use_vectors == true)
4515 {
4516 const number val = resolve_vector_entry(
4517 i, global_rows, local_vector, local_dof_indices, local_matrix);
4518
4519 if (val != number())
4520 global_vector(global_indices[i]) +=
4521 static_cast<typename VectorType::value_type>(val);
4522 }
4523 }
4524 }
4525
4526 internal::AffineConstraints::set_matrix_diagonals(
4527 global_rows,
4528 local_dof_indices,
4529 local_matrix,
4530 *this,
4531 global_matrix,
4532 global_vector,
4533 use_inhomogeneities_for_rhs);
4534 }
4535
4536
4537
4538 template <typename number>
4539 template <typename MatrixType>
4540 void
4541 AffineConstraints<number>::distribute_local_to_global(
4542 const FullMatrix<number> &local_matrix,
4543 const std::vector<size_type> &row_indices,
4544 const std::vector<size_type> &col_indices,
4545 MatrixType &global_matrix) const
4546 {
4547 distribute_local_to_global(
4548 local_matrix, row_indices, *this, col_indices, global_matrix);
4549 }
4550
4551
4552
4553 template <typename number>
4554 template <typename MatrixType>
4555 void
4556 AffineConstraints<number>::distribute_local_to_global(
4557 const FullMatrix<number> &local_matrix,
4558 const std::vector<size_type> &row_indices,
4559 const AffineConstraints<number> &col_constraint_matrix,
4560 const std::vector<size_type> &col_indices,
4561 MatrixType &global_matrix) const
4562 {
4563 AssertDimension(local_matrix.m(), row_indices.size());
4564 AssertDimension(local_matrix.n(), col_indices.size());
4565
4566 const size_type n_local_row_dofs = row_indices.size();
4567 const size_type n_local_col_dofs = col_indices.size();
4568
4569 typename internal::AffineConstraints::ScratchDataAccessor<
4570 typename MatrixType::value_type>
4571 scratch_data(this->scratch_data);
4572
4573 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows =
4574 scratch_data->global_rows;
4575 global_rows.reinit(n_local_row_dofs);
4576
4577 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_cols =
4578 scratch_data->global_columns;
4579 global_cols.reinit(n_local_col_dofs);
4580
4581 make_sorted_row_list(row_indices, global_rows);
4582 col_constraint_matrix.make_sorted_row_list(col_indices, global_cols);
4583
4584 const size_type n_actual_row_dofs = global_rows.size();
4585 const size_type n_actual_col_dofs = global_cols.size();
4586
4587 // create arrays for the column data (indices and values) that will then be
4588 // written into the matrix. Shortcut for deal.II sparse matrix
4589 std::vector<size_type> &cols = scratch_data->columns;
4590 std::vector<number> &vals = scratch_data->values;
4591 cols.resize(n_actual_col_dofs);
4592 vals.resize(n_actual_col_dofs);
4593
4594 // now do the actual job.
4595 for (size_type i = 0; i < n_actual_row_dofs; ++i)
4596 {
4597 const size_type row = global_rows.global_row(i);
4598
4599 // calculate all the data that will be written into the matrix row.
4600 size_type *col_ptr = cols.data();
4601 number *val_ptr = vals.data();
4602 internal::AffineConstraints::resolve_matrix_row(global_rows,
4603 global_cols,
4604 i,
4605 0,
4606 n_actual_col_dofs,
4607 local_matrix,
4608 col_ptr,
4609 val_ptr);
4610 const size_type n_values = col_ptr - cols.data();
4611 if (n_values > 0)
4612 global_matrix.add(row, n_values, cols.data(), vals.data(), false, true);
4613 }
4614 }
4615
4616
4617
4618 template <typename number>
4619 void
4620 AffineConstraints<number>::add_entries_local_to_global(
4621 const std::vector<size_type> &local_dof_indices,
4622 SparsityPatternBase &sparsity_pattern,
4623 const bool keep_constrained_entries,
4624 const Table<2, bool> &dof_mask) const
4625 {
4626 Assert(sparsity_pattern.n_rows() == sparsity_pattern.n_cols(),
4627 ExcNotQuadratic());
4628
4629 const size_type n_local_dofs = local_dof_indices.size();
4630 typename internal::AffineConstraints::ScratchDataAccessor<number>
4631 scratch_data(this->scratch_data);
4632
4633 const bool dof_mask_is_active = (dof_mask.n_rows() == n_local_dofs);
4634 if (dof_mask_is_active == true)
4635 {
4636 AssertDimension(dof_mask.n_cols(), n_local_dofs);
4637 }
4638 else
4639 {
4640 // if the dof mask is not active, all we have to do is to add some indices
4641 // in a matrix format. To do this, we first create an array of all the
4642 // indices that are to be added. these indices are the local dof indices
4643 // plus some indices that come from constraints.
4644 std::vector<size_type> &actual_dof_indices = scratch_data->columns;
4645 actual_dof_indices.resize(n_local_dofs);
4646 make_sorted_row_list(local_dof_indices, actual_dof_indices);
4647 const size_type n_actual_dofs = actual_dof_indices.size();
4648
4649 // now add the indices we collected above to the sparsity pattern. Very
4650 // easy here - just add the same array to all the rows...
4651 for (size_type i = 0; i < n_actual_dofs; ++i)
4652 sparsity_pattern.add_row_entries(actual_dof_indices[i],
4653 make_array_view(actual_dof_indices),
4654 true);
4655
4656 // need to add the whole row and column structure in case we keep
4657 // constrained entries.
4658 std::vector<std::pair<size_type, size_type>> &cell_entries =
4659 scratch_data->new_entries;
4660 cell_entries.resize(0);
4661 cell_entries.reserve(n_local_dofs);
4662 for (size_type i = 0; i < n_local_dofs; ++i)
4663 if (is_constrained(local_dof_indices[i]))
4664 {
4665 if (keep_constrained_entries == true)
4666 for (size_type j = 0; j < n_local_dofs; ++j)
4667 {
4668 cell_entries.emplace_back(local_dof_indices[i],
4669 local_dof_indices[j]);
4670 cell_entries.emplace_back(local_dof_indices[j],
4671 local_dof_indices[i]);
4672 }
4673 else
4674 {
4675 cell_entries.emplace_back(local_dof_indices[i],
4676 local_dof_indices[i]);
4677 }
4678 }
4679 sparsity_pattern.add_entries(make_array_view(cell_entries));
4680
4681 return;
4682 }
4683
4684 // complicated case: we need to filter out some indices. then the function
4685 // gets similar to the function for distributing matrix entries, see there
4686 // for additional comments.
4687 internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows =
4688 scratch_data->global_rows;
4689 global_rows.reinit(n_local_dofs);
4690 make_sorted_row_list(local_dof_indices, global_rows);
4691 const size_type n_actual_dofs = global_rows.size();
4692
4693 // create arrays for the column indices that will then be written into the
4694 // sparsity pattern.
4695 std::vector<size_type> &cols = scratch_data->columns;
4696 cols.resize(n_actual_dofs);
4697
4698 for (size_type i = 0; i < n_actual_dofs; ++i)
4699 {
4700 std::vector<size_type>::iterator col_ptr = cols.begin();
4701 const size_type row = global_rows.global_row(i);
4702 internal::AffineConstraints::resolve_matrix_row(
4703 global_rows, i, 0, n_actual_dofs, dof_mask, col_ptr);
4704
4705 // finally, write all the information that accumulated under the given
4706 // process into the global matrix row and into the vector
4707 if (col_ptr != cols.begin())
4708 sparsity_pattern.add_row_entries(row,
4709 make_array_view(cols.begin(), col_ptr),
4710 true);
4711 }
4712 internal::AffineConstraints::set_sparsity_diagonals(global_rows,
4713 local_dof_indices,
4714 dof_mask,
4715 keep_constrained_entries,
4716 *scratch_data,
4717 sparsity_pattern);
4718 }
4719
4720
4721
4722 template <typename number>
4723 void
4724 AffineConstraints<number>::add_entries_local_to_global(
4725 const std::vector<size_type> &row_indices,
4726 const AffineConstraints<number> &col_constraints,
4727 const std::vector<size_type> &col_indices,
4728 SparsityPatternBase &sparsity_pattern,
4729 const bool keep_constrained_entries,
4730 const Table<2, bool> &dof_mask) const
4731 {
4732 const size_type n_local_rows = row_indices.size();
4733 const size_type n_local_cols = col_indices.size();
4734
4735 // Early return if the length of row and column indices is zero, relevant
4736 // for the usage with FE_Nothing.
4737 if (n_local_cols == 0 && n_local_rows == 0)
4738 return;
4739
4740 typename internal::AffineConstraints::ScratchDataAccessor<number>
4741 scratch_data(this->scratch_data);
4742 std::vector<std::pair<size_type, size_type>> &cell_entries =
4743 scratch_data->new_entries;
4744 cell_entries.resize(0);
4745 cell_entries.reserve(n_local_rows * n_local_cols);
4746
4747 // if constrained entries should be kept, need to add rows and columns of
4748 // those to the sparsity pattern
4749 if (keep_constrained_entries == true)
4750 {
4751 for (const size_type row_index : row_indices)
4752 if (is_constrained(row_index))
4753 for (const size_type col_index : col_indices)
4754 cell_entries.emplace_back(row_index, col_index);
4755 for (const size_type col_index : col_indices)
4756 if (col_constraints.is_constrained(col_index))
4757 for (const size_type row_index : row_indices)
4758 cell_entries.emplace_back(row_index, col_index);
4759 sparsity_pattern.add_entries(make_array_view(cell_entries));
4760 }
4761
4762 // if the dof mask is not active, all we have to do is to add some indices
4763 // in a matrix format. To do this, we first create an array of all the
4764 // indices that are to be added. these indices are the local dof indices
4765 // plus some indices that come from constraints.
4766 const bool dof_mask_is_active =
4767 dof_mask.n_rows() == n_local_rows && dof_mask.n_cols() == n_local_cols;
4768 if (dof_mask_is_active == false)
4769 {
4770 std::vector<size_type> &rows = scratch_data->rows;
4771 std::vector<size_type> &cols = scratch_data->columns;
4772 rows.resize(n_local_rows);
4773 cols.resize(n_local_cols);
4774 // TODO these fills may not be necessary: previously we assumed all zeros
4775 // which seems incorrect. At least this way things will crash
4776 std::fill(rows.begin(),
4777 rows.end(),
4778 std::numeric_limits<size_type>::max());
4779 std::fill(cols.begin(),
4780 cols.end(),
4781 std::numeric_limits<size_type>::max());
4782 make_sorted_row_list(row_indices, rows);
4783 col_constraints.make_sorted_row_list(col_indices, cols);
4784 const size_type n_actual_rows = rows.size();
4785
4786 // now add the indices we collected above to the sparsity pattern. Very
4787 // easy here - just add the same array to all the rows...
4788 for (size_type i = 0; i < n_actual_rows; ++i)
4789 sparsity_pattern.add_row_entries(rows[i], make_array_view(cols), true);
4790 return;
4791 }
4792
4793 // TODO: implement this
4794 DEAL_II_NOT_IMPLEMENTED();
4795 }
4796
4797
4798
4799 template <typename number>
4800 void
4801 AffineConstraints<number>::add_entries_local_to_global(
4802 const std::vector<size_type> &row_indices,
4803 const std::vector<size_type> &col_indices,
4804 SparsityPatternBase &sparsity_pattern,
4805 const bool keep_constrained_entries,
4806 const Table<2, bool> &dof_mask) const
4807 {
4808 // Call the function with the same name that takes a column constraint as well
4809 add_entries_local_to_global(row_indices,
4810 *this,
4811 col_indices,
4812 sparsity_pattern,
4813 keep_constrained_entries,
4814 dof_mask);
4815 }
4816
4817 DEAL_II_NAMESPACE_CLOSE
4818
4819 #endif