Debian Codegraph @

include/deal.II/numerics/matrix_creator.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2016 - 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_matrix_creator_templates_h
16 #define dealii_matrix_creator_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/function.h>
21 #include <deal.II/base/geometry_info.h>
22 #include <deal.II/base/quadrature.h>
23 #include <deal.II/base/work_stream.h>
24
25 #include <deal.II/dofs/dof_accessor.h>
26 #include <deal.II/dofs/dof_handler.h>
27 #include <deal.II/dofs/dof_tools.h>
28
29 #include <deal.II/fe/fe.h>
30 #include <deal.II/fe/fe_hermite.h>
31 #include <deal.II/fe/fe_values.h>
32 #include <deal.II/fe/mapping.h>
33
34 #include <deal.II/grid/tria_iterator.h>
35
36 #include <deal.II/hp/fe_values.h>
37 #include <deal.II/hp/mapping_collection.h>
38
39 #include <deal.II/lac/block_sparse_matrix.h>
40 #include <deal.II/lac/block_vector.h>
41 #include <deal.II/lac/full_matrix.h>
42 #include <deal.II/lac/sparse_matrix.h>
43 #include <deal.II/lac/vector.h>
44
45 #include <deal.II/numerics/matrix_creator.h>
46
47 #ifdef DEAL_II_WITH_PETSC
48 # include <deal.II/lac/petsc_block_sparse_matrix.h>
49 # include <deal.II/lac/petsc_sparse_matrix.h>
50 # include <deal.II/lac/petsc_vector.h>
51 #endif
52
53 #ifdef DEAL_II_WITH_TRILINOS
54 # include <deal.II/lac/trilinos_block_sparse_matrix.h>
55 # include <deal.II/lac/trilinos_parallel_block_vector.h>
56 # include <deal.II/lac/trilinos_sparse_matrix.h>
57 # include <deal.II/lac/trilinos_vector.h>
58 #endif
59
60
61 #include <algorithm>
62 #include <cmath>
63
64
65 DEAL_II_NAMESPACE_OPEN
66 namespace MatrixCreator
67 {
68 namespace internal
69 {
70 namespace AssemblerData
71 {
72 template <int dim, int spacedim, typename number>
73 struct Scratch
74 {
75 Scratch(const ::dealii::hp::FECollection<dim, spacedim> &fe,
76 const UpdateFlags update_flags,
77 const Function<spacedim, number> *coefficient,
78 const Function<spacedim, number> *rhs_function,
79 const ::dealii::hp::QCollection<dim> &quadrature,
80 const ::dealii::hp::MappingCollection<dim, spacedim> &mapping)
81 : fe_collection(fe)
82 , quadrature_collection(quadrature)
83 , mapping_collection(mapping)
84 , x_fe_values(mapping_collection,
85 fe_collection,
86 quadrature_collection,
87 update_flags)
88 , coefficient_values(quadrature_collection.max_n_quadrature_points())
89 , coefficient_vector_values(
90 quadrature_collection.max_n_quadrature_points(),
91 dealii::Vector<number>(fe_collection.n_components()))
92 , rhs_values(quadrature_collection.max_n_quadrature_points())
93 , rhs_vector_values(quadrature_collection.max_n_quadrature_points(),
94 dealii::Vector<number>(
95 fe_collection.n_components()))
96 , coefficient(coefficient)
97 , rhs_function(rhs_function)
98 , update_flags(update_flags)
99 {}
100
101 Scratch(const Scratch &data)
102 : fe_collection(data.fe_collection)
103 , quadrature_collection(data.quadrature_collection)
104 , mapping_collection(data.mapping_collection)
105 , x_fe_values(mapping_collection,
106 fe_collection,
107 quadrature_collection,
108 data.update_flags)
109 , coefficient_values(data.coefficient_values)
110 , coefficient_vector_values(data.coefficient_vector_values)
111 , rhs_values(data.rhs_values)
112 , rhs_vector_values(data.rhs_vector_values)
113 , coefficient(data.coefficient)
114 , rhs_function(data.rhs_function)
115 , update_flags(data.update_flags)
116 {}
117
118 Scratch &
119 operator=(const Scratch &)
120 {
121 DEAL_II_NOT_IMPLEMENTED();
122 return *this;
123 }
124
125
126 const ::dealii::hp::FECollection<dim, spacedim> &fe_collection;
127 const ::dealii::hp::QCollection<dim> &quadrature_collection;
128 const ::dealii::hp::MappingCollection<dim, spacedim>
129 &mapping_collection;
130
131 ::dealii::hp::FEValues<dim, spacedim> x_fe_values;
132
133 std::vector<number> coefficient_values;
134 std::vector<dealii::Vector<number>> coefficient_vector_values;
135 std::vector<number> rhs_values;
136 std::vector<dealii::Vector<number>> rhs_vector_values;
137
138 const Function<spacedim, number> *coefficient;
139 const Function<spacedim, number> *rhs_function;
140
141 const UpdateFlags update_flags;
142 };
143
144
145 template <typename number>
146 struct CopyData
147 {
148 std::vector<types::global_dof_index> dof_indices;
149 FullMatrix<number> cell_matrix;
150 dealii::Vector<number> cell_rhs;
151 const AffineConstraints<number> *constraints;
152 bool is_locally_owned;
153 };
154 } // namespace AssemblerData
155
156
157 template <int dim, int spacedim, typename CellIterator, typename number>
158 void
159 mass_assembler(
160 const CellIterator &cell,
161 MatrixCreator::internal::AssemblerData::Scratch<dim, spacedim, number>
162 &data,
163 MatrixCreator::internal::AssemblerData::CopyData<number> &copy_data)
164 {
165 copy_data.is_locally_owned = cell->is_locally_owned();
166 if (copy_data.is_locally_owned == false)
167 return;
168
169 data.x_fe_values.reinit(cell);
170 const FEValues<dim, spacedim> &fe_values =
171 data.x_fe_values.get_present_fe_values();
172
173 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
174 n_q_points = fe_values.n_quadrature_points;
175 const FiniteElement<dim, spacedim> &fe = fe_values.get_fe();
176 const unsigned int n_components = fe.n_components();
177
178 Assert(data.rhs_function == nullptr ||
179 data.rhs_function->n_components == 1 ||
180 data.rhs_function->n_components == n_components,
181 ::dealii::MatrixCreator::ExcComponentMismatch());
182 Assert(data.coefficient == nullptr ||
183 data.coefficient->n_components == 1 ||
184 data.coefficient->n_components == n_components,
185 ::dealii::MatrixCreator::ExcComponentMismatch());
186
187 copy_data.cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
188 copy_data.cell_rhs.reinit(dofs_per_cell);
189
190 copy_data.dof_indices.resize(dofs_per_cell);
191 cell->get_dof_indices(copy_data.dof_indices);
192
193 const bool use_rhs_function = data.rhs_function != nullptr;
194 if (use_rhs_function)
195 {
196 if (data.rhs_function->n_components == 1)
197 {
198 data.rhs_values.resize(n_q_points);
199 data.rhs_function->value_list(fe_values.get_quadrature_points(),
200 data.rhs_values);
201 }
202 else
203 {
204 data.rhs_vector_values.resize(
205 n_q_points, dealii::Vector<number>(n_components));
206 data.rhs_function->vector_value_list(
207 fe_values.get_quadrature_points(), data.rhs_vector_values);
208 }
209 }
210
211 const bool use_coefficient = data.coefficient != nullptr;
212 if (use_coefficient)
213 {
214 if (data.coefficient->n_components == 1)
215 {
216 data.coefficient_values.resize(n_q_points);
217 data.coefficient->value_list(fe_values.get_quadrature_points(),
218 data.coefficient_values);
219 }
220 else
221 {
222 data.coefficient_vector_values.resize(
223 n_q_points, dealii::Vector<number>(n_components));
224 data.coefficient->vector_value_list(
225 fe_values.get_quadrature_points(),
226 data.coefficient_vector_values);
227 }
228 }
229
230
231 const std::vector<double> &JxW = fe_values.get_JxW_values();
232 for (unsigned int i = 0; i < dofs_per_cell; ++i)
233 if (fe.is_primitive())
234 {
235 const unsigned int component_i =
236 fe.system_to_component_index(i).first;
237 const double *phi_i = &fe_values.shape_value(i, 0);
238
239 // use symmetry in the mass matrix here:
240 // just need to calculate the diagonal
241 // and half of the elements above the
242 // diagonal
243 for (unsigned int j = i; j < dofs_per_cell; ++j)
244 if ((n_components == 1) ||
245 (fe.system_to_component_index(j).first == component_i))
246 {
247 const double *phi_j = &fe_values.shape_value(j, 0);
248 number add_data = 0;
249 if (use_coefficient)
250 {
251 if (data.coefficient->n_components == 1)
252 for (unsigned int point = 0; point < n_q_points;
253 ++point)
254 add_data +=
255 (data.coefficient_values[point] * phi_i[point] *
256 phi_j[point] * JxW[point]);
257 else
258 for (unsigned int point = 0; point < n_q_points;
259 ++point)
260 add_data +=
261 (data.coefficient_vector_values[point](
262 component_i) *
263 phi_i[point] * phi_j[point] * JxW[point]);
264 }
265 else
266 for (unsigned int point = 0; point < n_q_points; ++point)
267 add_data += phi_i[point] * phi_j[point] * JxW[point];
268
269 // this is even ok for i==j, since then
270 // we just write the same value twice.
271 copy_data.cell_matrix(i, j) = add_data;
272 copy_data.cell_matrix(j, i) = add_data;
273 }
274
275 if (use_rhs_function)
276 {
277 number add_data = 0;
278 if (data.rhs_function->n_components == 1)
279 for (unsigned int point = 0; point < n_q_points; ++point)
280 add_data +=
281 data.rhs_values[point] * phi_i[point] * JxW[point];
282 else
283 for (unsigned int point = 0; point < n_q_points; ++point)
284 add_data += data.rhs_vector_values[point](component_i) *
285 phi_i[point] * JxW[point];
286 copy_data.cell_rhs(i) = add_data;
287 }
288 }
289 else
290 {
291 // non-primitive vector-valued FE, using
292 // symmetry again
293 for (unsigned int j = i; j < dofs_per_cell; ++j)
294 {
295 number add_data = 0;
296 for (unsigned int comp_i = 0; comp_i < n_components; ++comp_i)
297 if (fe.get_nonzero_components(i)[comp_i] &&
298 fe.get_nonzero_components(j)[comp_i])
299 {
300 if (use_coefficient)
301 {
302 if (data.coefficient->n_components == 1)
303 for (unsigned int point = 0; point < n_q_points;
304 ++point)
305 add_data +=
306 (data.coefficient_values[point] *
307 fe_values.shape_value_component(i,
308 point,
309 comp_i) *
310 fe_values.shape_value_component(j,
311 point,
312 comp_i) *
313 JxW[point]);
314 else
315 for (unsigned int point = 0; point < n_q_points;
316 ++point)
317 add_data +=
318 (data.coefficient_vector_values[point](comp_i) *
319 fe_values.shape_value_component(i,
320 point,
321 comp_i) *
322 fe_values.shape_value_component(j,
323 point,
324 comp_i) *
325 JxW[point]);
326 }
327 else
328 for (unsigned int point = 0; point < n_q_points;
329 ++point)
330 add_data +=
331 fe_values.shape_value_component(i, point, comp_i) *
332 fe_values.shape_value_component(j, point, comp_i) *
333 JxW[point];
334 }
335
336 copy_data.cell_matrix(i, j) = add_data;
337 copy_data.cell_matrix(j, i) = add_data;
338 }
339
340 if (use_rhs_function)
341 {
342 number add_data = 0;
343 for (unsigned int comp_i = 0; comp_i < n_components; ++comp_i)
344 if (fe.get_nonzero_components(i)[comp_i])
345 {
346 if (data.rhs_function->n_components == 1)
347 for (unsigned int point = 0; point < n_q_points;
348 ++point)
349 add_data +=
350 data.rhs_values[point] *
351 fe_values.shape_value_component(i, point, comp_i) *
352 JxW[point];
353 else
354 for (unsigned int point = 0; point < n_q_points;
355 ++point)
356 add_data +=
357 data.rhs_vector_values[point](comp_i) *
358 fe_values.shape_value_component(i, point, comp_i) *
359 JxW[point];
360 }
361 copy_data.cell_rhs(i) = add_data;
362 }
363 }
364 }
365
366
367
368 template <int dim, int spacedim, typename CellIterator, typename Number>
369 void
370 laplace_assembler(
371 const CellIterator &cell,
372 MatrixCreator::internal::AssemblerData::Scratch<dim, spacedim, Number>
373 &data,
374 MatrixCreator::internal::AssemblerData::CopyData<Number> &copy_data)
375 {
376 copy_data.is_locally_owned = cell->is_locally_owned();
377 if (copy_data.is_locally_owned == false)
378 return;
379
380 data.x_fe_values.reinit(cell);
381 const FEValues<dim, spacedim> &fe_values =
382 data.x_fe_values.get_present_fe_values();
383
384 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
385 n_q_points = fe_values.n_quadrature_points;
386 const FiniteElement<dim, spacedim> &fe = fe_values.get_fe();
387 const unsigned int n_components = fe.n_components();
388
389 Assert(data.rhs_function == nullptr ||
390 data.rhs_function->n_components == 1 ||
391 data.rhs_function->n_components == n_components,
392 ::dealii::MatrixCreator::ExcComponentMismatch());
393 Assert(data.coefficient == nullptr ||
394 data.coefficient->n_components == 1 ||
395 data.coefficient->n_components == n_components,
396 ::dealii::MatrixCreator::ExcComponentMismatch());
397
398 copy_data.cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
399 copy_data.cell_rhs.reinit(dofs_per_cell);
400 copy_data.dof_indices.resize(dofs_per_cell);
401 cell->get_dof_indices(copy_data.dof_indices);
402
403
404 const bool use_rhs_function = data.rhs_function != nullptr;
405 if (use_rhs_function)
406 {
407 if (data.rhs_function->n_components == 1)
408 {
409 data.rhs_values.resize(n_q_points);
410 data.rhs_function->value_list(fe_values.get_quadrature_points(),
411 data.rhs_values);
412 }
413 else
414 {
415 data.rhs_vector_values.resize(
416 n_q_points, dealii::Vector<Number>(n_components));
417 data.rhs_function->vector_value_list(
418 fe_values.get_quadrature_points(), data.rhs_vector_values);
419 }
420 }
421
422 const bool use_coefficient = data.coefficient != nullptr;
423 if (use_coefficient)
424 {
425 if (data.coefficient->n_components == 1)
426 {
427 data.coefficient_values.resize(n_q_points);
428 data.coefficient->value_list(fe_values.get_quadrature_points(),
429 data.coefficient_values);
430 }
431 else
432 {
433 data.coefficient_vector_values.resize(
434 n_q_points, dealii::Vector<Number>(n_components));
435 data.coefficient->vector_value_list(
436 fe_values.get_quadrature_points(),
437 data.coefficient_vector_values);
438 }
439 }
440
441
442 const std::vector<double> &JxW = fe_values.get_JxW_values();
443 Number add_data;
444 for (unsigned int i = 0; i < dofs_per_cell; ++i)
445 if (fe.is_primitive())
446 {
447 const unsigned int component_i =
448 fe.system_to_component_index(i).first;
449 const Tensor<1, spacedim> *grad_phi_i = &fe_values.shape_grad(i, 0);
450
451 // can use symmetry
452 for (unsigned int j = i; j < dofs_per_cell; ++j)
453 if ((n_components == 1) ||
454 (fe.system_to_component_index(j).first == component_i))
455 {
456 const Tensor<1, spacedim> *grad_phi_j =
457 &fe_values.shape_grad(j, 0);
458 add_data = 0;
459 if (use_coefficient)
460 {
461 if (data.coefficient->n_components == 1)
462 for (unsigned int point = 0; point < n_q_points;
463 ++point)
464 add_data +=
465 ((grad_phi_i[point] * grad_phi_j[point]) *
466 JxW[point] * data.coefficient_values[point]);
467 else
468 for (unsigned int point = 0; point < n_q_points;
469 ++point)
470 add_data += ((grad_phi_i[point] * grad_phi_j[point]) *
471 JxW[point] *
472 data.coefficient_vector_values[point](
473 component_i));
474 }
475 else
476 for (unsigned int point = 0; point < n_q_points; ++point)
477 add_data +=
478 (grad_phi_i[point] * grad_phi_j[point]) * JxW[point];
479
480 copy_data.cell_matrix(i, j) = add_data;
481 copy_data.cell_matrix(j, i) = add_data;
482 }
483
484 if (use_rhs_function)
485 {
486 const double *phi_i = &fe_values.shape_value(i, 0);
487 add_data = 0;
488 if (data.rhs_function->n_components == 1)
489 for (unsigned int point = 0; point < n_q_points; ++point)
490 add_data +=
491 phi_i[point] * JxW[point] * data.rhs_values[point];
492 else
493 for (unsigned int point = 0; point < n_q_points; ++point)
494 add_data += phi_i[point] * JxW[point] *
495 data.rhs_vector_values[point](component_i);
496 copy_data.cell_rhs(i) = add_data;
497 }
498 }
499 else
500 {
501 // non-primitive vector-valued FE
502 for (unsigned int j = i; j < dofs_per_cell; ++j)
503 {
504 add_data = 0;
505 for (unsigned int comp_i = 0; comp_i < n_components; ++comp_i)
506 if (fe.get_nonzero_components(i)[comp_i] &&
507 fe.get_nonzero_components(j)[comp_i])
508 {
509 if (use_coefficient)
510 {
511 if (data.coefficient->n_components == 1)
512 for (unsigned int point = 0; point < n_q_points;
513 ++point)
514 add_data +=
515 ((fe_values.shape_grad_component(i,
516 point,
517 comp_i) *
518 fe_values.shape_grad_component(j,
519 point,
520 comp_i)) *
521 JxW[point] * data.coefficient_values[point]);
522 else
523 for (unsigned int point = 0; point < n_q_points;
524 ++point)
525 add_data +=
526 ((fe_values.shape_grad_component(i,
527 point,
528 comp_i) *
529 fe_values.shape_grad_component(j,
530 point,
531 comp_i)) *
532 JxW[point] *
533 data.coefficient_vector_values[point](comp_i));
534 }
535 else
536 for (unsigned int point = 0; point < n_q_points;
537 ++point)
538 add_data +=
539 (fe_values.shape_grad_component(i, point, comp_i) *
540 fe_values.shape_grad_component(j, point, comp_i)) *
541 JxW[point];
542 }
543
544 copy_data.cell_matrix(i, j) = add_data;
545 copy_data.cell_matrix(j, i) = add_data;
546 }
547
548 if (use_rhs_function)
549 {
550 add_data = 0;
551 for (unsigned int comp_i = 0; comp_i < n_components; ++comp_i)
552 if (fe.get_nonzero_components(i)[comp_i])
553 {
554 if (data.rhs_function->n_components == 1)
555 for (unsigned int point = 0; point < n_q_points;
556 ++point)
557 add_data +=
558 fe_values.shape_value_component(i, point, comp_i) *
559 JxW[point] * data.rhs_values[point];
560 else
561 for (unsigned int point = 0; point < n_q_points;
562 ++point)
563 add_data +=
564 fe_values.shape_value_component(i, point, comp_i) *
565 JxW[point] * data.rhs_vector_values[point](comp_i);
566 }
567 copy_data.cell_rhs(i) = add_data;
568 }
569 }
570 }
571
572
573
574 template <typename number, typename MatrixType, typename VectorType>
575 void
576 copy_local_to_global(const AssemblerData::CopyData<number> &data,
577 MatrixType *matrix,
578 VectorType *right_hand_side)
579 {
580 if (data.is_locally_owned == false)
581 return;
582
583 const unsigned int dofs_per_cell = data.dof_indices.size();
584 Assert(data.cell_matrix.m() == dofs_per_cell, ExcInternalError());
585 Assert(data.cell_matrix.n() == dofs_per_cell, ExcInternalError());
586 Assert((right_hand_side == nullptr) ||
587 (data.cell_rhs.size() == dofs_per_cell),
588 ExcInternalError());
589
590 if (right_hand_side != nullptr)
591 data.constraints->distribute_local_to_global(data.cell_matrix,
592 data.cell_rhs,
593 data.dof_indices,
594 *matrix,
595 *right_hand_side);
596 else
597 data.constraints->distribute_local_to_global(data.cell_matrix,
598 data.dof_indices,
599 *matrix);
600 }
601
602
603
604 namespace AssemblerBoundary
605 {
606 struct Scratch
607 {
608 Scratch() = default;
609 };
610
611 template <int dim, int spacedim, typename number>
612 struct CopyData
613 {
614 CopyData();
615
616 unsigned int dofs_per_cell;
617 std::vector<types::global_dof_index> dofs;
618 std::vector<std::vector<bool>> dof_is_on_face;
619 typename DoFHandler<dim, spacedim>::active_cell_iterator cell;
620 std::vector<FullMatrix<number>> cell_matrix;
621 std::vector<Vector<number>> cell_vector;
622 };
623
624
625 template <int dim, int spacedim, typename number>
626 CopyData<dim, spacedim, number>::CopyData()
627 : dofs_per_cell(numbers::invalid_unsigned_int)
628 {}
629
630
631 } // namespace AssemblerBoundary
632 } // namespace internal
633 } // namespace MatrixCreator
634
635
636 namespace MatrixCreator
637 {
638 template <int dim, int spacedim, typename MatrixType>
639 void
640 create_mass_matrix(
641 const Mapping<dim, spacedim> &mapping,
642 const DoFHandler<dim, spacedim> &dof,
643 const Quadrature<dim> &q,
644 MatrixType &matrix,
645 const Function<spacedim, typename MatrixType::value_type>
646 *const coefficient,
647 const AffineConstraints<typename MatrixType::value_type> &constraints)
648 {
649 Assert(matrix.m() == dof.n_dofs(),
650 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
651 Assert(matrix.n() == dof.n_dofs(),
652 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
653
654 const auto &fe_collection = dof.get_fe_collection();
655 hp::QCollection<dim> q_collection(q);
656 hp::MappingCollection<dim, spacedim> mapping_collection(mapping);
657 MatrixCreator::internal::AssemblerData::
658 Scratch<dim, spacedim, typename MatrixType::value_type>
659 assembler_data(fe_collection,
660 update_values | update_JxW_values |
661 (coefficient != nullptr ? update_quadrature_points :
662 UpdateFlags(0)),
663 coefficient,
664 /*rhs_function=*/nullptr,
665 q_collection,
666 mapping_collection);
667
668 MatrixCreator::internal::AssemblerData::CopyData<
669 typename MatrixType::value_type>
670 copy_data;
671 copy_data.cell_matrix.reinit(
672 assembler_data.fe_collection.max_dofs_per_cell(),
673 assembler_data.fe_collection.max_dofs_per_cell());
674 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
675 copy_data.dof_indices.resize(
676 assembler_data.fe_collection.max_dofs_per_cell());
677 copy_data.constraints = &constraints;
678
679 WorkStream::run(
680 dof.begin_active(),
681 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
682 dof.end()),
683 &MatrixCreator::internal::mass_assembler<
684 dim,
685 spacedim,
686 typename DoFHandler<dim, spacedim>::active_cell_iterator,
687 typename MatrixType::value_type>,
688 [&matrix](
689 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
690 &data) {
691 MatrixCreator::internal::copy_local_to_global(
692 data,
693 &matrix,
694 static_cast<Vector<typename MatrixType::value_type> *>(nullptr));
695 },
696 assembler_data,
697 copy_data);
698
699 matrix.compress(VectorOperation::add);
700 }
701
702
703
704 template <int dim, int spacedim, typename MatrixType>
705 void
706 create_mass_matrix(
707 const DoFHandler<dim, spacedim> &dof,
708 const Quadrature<dim> &q,
709 MatrixType &matrix,
710 const Function<spacedim, typename MatrixType::value_type>
711 *const coefficient,
712 const AffineConstraints<typename MatrixType::value_type> &constraints)
713 {
714 create_mass_matrix(get_default_linear_mapping(dof.get_triangulation()),
715 dof,
716 q,
717 matrix,
718 coefficient,
719 constraints);
720 }
721
722
723
724 template <int dim, int spacedim, typename MatrixType>
725 void
726 create_mass_matrix(
727 const Mapping<dim, spacedim> &mapping,
728 const DoFHandler<dim, spacedim> &dof,
729 const Quadrature<dim> &q,
730 MatrixType &matrix,
731 const Function<spacedim, typename MatrixType::value_type> &rhs,
732 Vector<typename MatrixType::value_type> &rhs_vector,
733 const Function<spacedim, typename MatrixType::value_type>
734 *const coefficient,
735 const AffineConstraints<typename MatrixType::value_type> &constraints)
736 {
737 Assert(matrix.m() == dof.n_dofs(),
738 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
739 Assert(matrix.n() == dof.n_dofs(),
740 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
741
742 const auto &fe_collection = dof.get_fe_collection();
743 hp::QCollection<dim> q_collection(q);
744 hp::MappingCollection<dim, spacedim> mapping_collection(mapping);
745 MatrixCreator::internal::AssemblerData::
746 Scratch<dim, spacedim, typename MatrixType::value_type>
747 assembler_data(fe_collection,
748 update_values | update_JxW_values |
749 update_quadrature_points,
750 coefficient,
751 &rhs,
752 q_collection,
753 mapping_collection);
754 MatrixCreator::internal::AssemblerData::CopyData<
755 typename MatrixType::value_type>
756 copy_data;
757 copy_data.cell_matrix.reinit(
758 assembler_data.fe_collection.max_dofs_per_cell(),
759 assembler_data.fe_collection.max_dofs_per_cell());
760 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
761 copy_data.dof_indices.resize(
762 assembler_data.fe_collection.max_dofs_per_cell());
763 copy_data.constraints = &constraints;
764
765 WorkStream::run(
766 dof.begin_active(),
767 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
768 dof.end()),
769 &MatrixCreator::internal::mass_assembler<
770 dim,
771 spacedim,
772 typename DoFHandler<dim, spacedim>::active_cell_iterator,
773 typename MatrixType::value_type>,
774 [&matrix, &rhs_vector](
775 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
776 &data) {
777 MatrixCreator::internal::copy_local_to_global(data,
778 &matrix,
779 &rhs_vector);
780 },
781 assembler_data,
782 copy_data);
783
784 matrix.compress(VectorOperation::add);
785 }
786
787
788
789 template <int dim, int spacedim, typename MatrixType>
790 void
791 create_mass_matrix(
792 const DoFHandler<dim, spacedim> &dof,
793 const Quadrature<dim> &q,
794 MatrixType &matrix,
795 const Function<spacedim, typename MatrixType::value_type> &rhs,
796 Vector<typename MatrixType::value_type> &rhs_vector,
797 const Function<spacedim, typename MatrixType::value_type>
798 *const coefficient,
799 const AffineConstraints<typename MatrixType::value_type> &constraints)
800 {
801 create_mass_matrix(get_default_linear_mapping(dof.get_triangulation()),
802 dof,
803 q,
804 matrix,
805 rhs,
806 rhs_vector,
807 coefficient,
808 constraints);
809 }
810
811
812
813 template <int dim, int spacedim, typename MatrixType>
814 void
815 create_mass_matrix(
816 const hp::MappingCollection<dim, spacedim> &mapping,
817 const DoFHandler<dim, spacedim> &dof,
818 const hp::QCollection<dim> &q,
819 MatrixType &matrix,
820 const Function<spacedim, typename MatrixType::value_type>
821 *const coefficient,
822 const AffineConstraints<typename MatrixType::value_type> &constraints)
823 {
824 Assert(matrix.m() == dof.n_dofs(),
825 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
826 Assert(matrix.n() == dof.n_dofs(),
827 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
828
829 MatrixCreator::internal::AssemblerData::
830 Scratch<dim, spacedim, typename MatrixType::value_type>
831 assembler_data(dof.get_fe_collection(),
832 update_values | update_JxW_values |
833 (coefficient != nullptr ? update_quadrature_points :
834 UpdateFlags(0)),
835 coefficient,
836 /*rhs_function=*/nullptr,
837 q,
838 mapping);
839 MatrixCreator::internal::AssemblerData::CopyData<
840 typename MatrixType::value_type>
841 copy_data;
842 copy_data.cell_matrix.reinit(
843 assembler_data.fe_collection.max_dofs_per_cell(),
844 assembler_data.fe_collection.max_dofs_per_cell());
845 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
846 copy_data.dof_indices.resize(
847 assembler_data.fe_collection.max_dofs_per_cell());
848 copy_data.constraints = &constraints;
849
850 WorkStream::run(
851 dof.begin_active(),
852 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
853 dof.end()),
854 &MatrixCreator::internal::mass_assembler<
855 dim,
856 spacedim,
857 typename DoFHandler<dim, spacedim>::active_cell_iterator,
858 typename MatrixType::value_type>,
859 [&matrix](
860 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
861 &data) {
862 MatrixCreator::internal::copy_local_to_global(
863 data,
864 &matrix,
865 static_cast<Vector<typename MatrixType::value_type> *>(nullptr));
866 },
867 assembler_data,
868 copy_data);
869
870 matrix.compress(VectorOperation::add);
871 }
872
873
874
875 template <int dim, int spacedim, typename MatrixType>
876 void
877 create_mass_matrix(
878 const DoFHandler<dim, spacedim> &dof,
879 const hp::QCollection<dim> &q,
880 MatrixType &matrix,
881 const Function<spacedim, typename MatrixType::value_type>
882 *const coefficient,
883 const AffineConstraints<typename MatrixType::value_type> &constraints)
884 {
885 create_mass_matrix(hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
886 dof,
887 q,
888 matrix,
889 coefficient,
890 constraints);
891 }
892
893
894
895 template <int dim, int spacedim, typename MatrixType>
896 void
897 create_mass_matrix(
898 const hp::MappingCollection<dim, spacedim> &mapping,
899 const DoFHandler<dim, spacedim> &dof,
900 const hp::QCollection<dim> &q,
901 MatrixType &matrix,
902 const Function<spacedim, typename MatrixType::value_type> &rhs,
903 Vector<typename MatrixType::value_type> &rhs_vector,
904 const Function<spacedim, typename MatrixType::value_type>
905 *const coefficient,
906 const AffineConstraints<typename MatrixType::value_type> &constraints)
907 {
908 Assert(matrix.m() == dof.n_dofs(),
909 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
910 Assert(matrix.n() == dof.n_dofs(),
911 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
912
913 MatrixCreator::internal::AssemblerData::
914 Scratch<dim, spacedim, typename MatrixType::value_type>
915 assembler_data(dof.get_fe_collection(),
916 update_values | update_JxW_values |
917 update_quadrature_points,
918 coefficient,
919 &rhs,
920 q,
921 mapping);
922 MatrixCreator::internal::AssemblerData::CopyData<
923 typename MatrixType::value_type>
924 copy_data;
925 copy_data.cell_matrix.reinit(
926 assembler_data.fe_collection.max_dofs_per_cell(),
927 assembler_data.fe_collection.max_dofs_per_cell());
928 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
929 copy_data.dof_indices.resize(
930 assembler_data.fe_collection.max_dofs_per_cell());
931 copy_data.constraints = &constraints;
932
933 WorkStream::run(
934 dof.begin_active(),
935 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
936 dof.end()),
937 &MatrixCreator::internal::mass_assembler<
938 dim,
939 spacedim,
940 typename DoFHandler<dim, spacedim>::active_cell_iterator,
941 typename MatrixType::value_type>,
942 [&matrix, &rhs_vector](
943 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
944 &data) {
945 MatrixCreator::internal::copy_local_to_global(data,
946 &matrix,
947 &rhs_vector);
948 },
949 assembler_data,
950 copy_data);
951
952 matrix.compress(VectorOperation::add);
953 }
954
955
956
957 template <int dim, int spacedim, typename MatrixType>
958 void
959 create_mass_matrix(
960 const DoFHandler<dim, spacedim> &dof,
961 const hp::QCollection<dim> &q,
962 MatrixType &matrix,
963 const Function<spacedim, typename MatrixType::value_type> &rhs,
964 Vector<typename MatrixType::value_type> &rhs_vector,
965 const Function<spacedim, typename MatrixType::value_type>
966 *const coefficient,
967 const AffineConstraints<typename MatrixType::value_type> &constraints)
968 {
969 create_mass_matrix(hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
970 dof,
971 q,
972 matrix,
973 rhs,
974 rhs_vector,
975 coefficient,
976 constraints);
977 }
978
979
980
981 namespace internal
982 {
983 template <int dim, int spacedim, typename number>
984 void inline create_boundary_mass_matrix_1(
985 const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
986 const MatrixCreator::internal::AssemblerBoundary::Scratch &,
987 MatrixCreator::internal::AssemblerBoundary::
988 CopyData<dim, spacedim, number> &copy_data,
989 const Mapping<dim, spacedim> &mapping,
990 const FiniteElement<dim, spacedim> &fe,
991 const Quadrature<dim - 1> &q,
992 const std::map<types::boundary_id, const Function<spacedim, number> *>
993 &boundary_functions,
994 const Function<spacedim, number> *const coefficient,
995 const std::vector<unsigned int> &component_mapping)
996 {
997 // Most assertions for this function are in the calling function
998 // before creating threads.
999 const unsigned int n_components = fe.n_components();
1000 const unsigned int n_function_components =
1001 boundary_functions.begin()->second->n_components;
1002 const bool fe_is_system = (n_components != 1);
1003 const bool fe_is_hermite =
1004 (dynamic_cast<const FE_Hermite<dim, spacedim> *>(&fe) != nullptr);
1005 const bool fe_is_primitive = fe.is_primitive();
1006
1007 copy_data.cell = cell;
1008 copy_data.dofs_per_cell = fe.n_dofs_per_cell();
1009
1010 UpdateFlags update_flags =
1011 UpdateFlags(update_values | update_JxW_values | update_normal_vectors |
1012 update_quadrature_points);
1013 if (fe_is_hermite)
1014 update_flags = update_flags | update_mapping;
1015 FEFaceValues<dim, spacedim> fe_values(mapping, fe, q, update_flags);
1016
1017 // two variables for the coefficient, one for the two cases
1018 // indicated in the name
1019 std::vector<number> coefficient_values(fe_values.n_quadrature_points, 1.);
1020 std::vector<Vector<number>> coefficient_vector_values(
1021 fe_values.n_quadrature_points, Vector<number>(n_components));
1022 const bool coefficient_is_vector =
1023 (coefficient != nullptr && coefficient->n_components != 1);
1024
1025 std::vector<number> rhs_values_scalar(fe_values.n_quadrature_points);
1026 std::vector<Vector<number>> rhs_values_system(
1027 fe_values.n_quadrature_points, Vector<number>(n_function_components));
1028
1029 copy_data.dofs.resize(copy_data.dofs_per_cell);
1030 cell->get_dof_indices(copy_data.dofs);
1031
1032 std::vector<types::global_dof_index> dofs_on_face_vector;
1033
1034 // Because CopyData objects are reused and emplace_back is
1035 // used, dof_is_on_face, cell_matrix, and cell_vector must be
1036 // cleared before they are reused
1037 copy_data.dof_is_on_face.clear();
1038 copy_data.cell_matrix.clear();
1039 copy_data.cell_vector.clear();
1040
1041 for (const unsigned int face : cell->face_indices())
1042 // check if this face is on that part of the boundary we are
1043 // interested in
1044 if (boundary_functions.find(cell->face(face)->boundary_id()) !=
1045 boundary_functions.end())
1046 {
1047 copy_data.cell_matrix.emplace_back(copy_data.dofs_per_cell,
1048 copy_data.dofs_per_cell);
1049 copy_data.cell_vector.emplace_back(copy_data.dofs_per_cell);
1050 fe_values.reinit(cell, face);
1051
1052 if (fe_is_system)
1053 // FE has several components
1054 {
1055 boundary_functions.find(cell->face(face)->boundary_id())
1056 ->second->vector_value_list(fe_values.get_quadrature_points(),
1057 rhs_values_system);
1058
1059 if (coefficient_is_vector)
1060 // If coefficient is vector valued, fill all
1061 // components
1062 coefficient->vector_value_list(
1063 fe_values.get_quadrature_points(),
1064 coefficient_vector_values);
1065 else
1066 {
1067 // If a scalar function is given, update the
1068 // values, if not, use the default one set in the
1069 // constructor above
1070 if (coefficient != nullptr)
1071 coefficient->value_list(fe_values.get_quadrature_points(),
1072 coefficient_values);
1073 // Copy scalar values into vector
1074 for (unsigned int point = 0;
1075 point < fe_values.n_quadrature_points;
1076 ++point)
1077 coefficient_vector_values[point] =
1078 coefficient_values[point];
1079 }
1080
1081 // Special treatment for Hdiv elements,
1082 // where only normal components should be projected.
1083 // For Hdiv we need to compute (u dot n, v dot n) which
1084 // can be done as sum over dim components of
1085 // u[c] * n[c] * v[c] * n[c] = u[c] * v[c] *
1086 // normal_adjustment[c] Same approach does not work for Hcurl,
1087 // so we throw an exception. Default value 1.0 allows for use
1088 // with non Hdiv elements
1089 std::vector<std::vector<double>> normal_adjustment(
1090 fe_values.n_quadrature_points,
1091 std::vector<double>(n_components, 1.));
1092
1093 for (unsigned int comp = 0; comp < n_components; ++comp)
1094 {
1095 const FiniteElement<dim, spacedim> &base =
1096 fe.base_element(fe.component_to_base_index(comp).first);
1097 const unsigned int bcomp =
1098 fe.component_to_base_index(comp).second;
1099
1100 if (!base.conforms(FiniteElementData<dim>::H1) &&
1101 base.conforms(FiniteElementData<dim>::Hdiv) &&
1102 fe_is_primitive)
1103 DEAL_II_NOT_IMPLEMENTED();
1104
1105 if (!base.conforms(FiniteElementData<dim>::H1) &&
1106 base.conforms(FiniteElementData<dim>::Hcurl))
1107 DEAL_II_NOT_IMPLEMENTED();
1108
1109 if (!base.conforms(FiniteElementData<dim>::H1) &&
1110 base.conforms(FiniteElementData<dim>::Hdiv))
1111 for (unsigned int point = 0;
1112 point < fe_values.n_quadrature_points;
1113 ++point)
1114 normal_adjustment[point][comp] =
1115 fe_values.normal_vector(point)[bcomp] *
1116 fe_values.normal_vector(point)[bcomp];
1117 }
1118
1119 for (unsigned int point = 0;
1120 point < fe_values.n_quadrature_points;
1121 ++point)
1122 {
1123 const double weight = fe_values.JxW(point);
1124 for (unsigned int i = 0; i < fe_values.dofs_per_cell; ++i)
1125 if (fe_is_primitive)
1126 {
1127 for (unsigned int j = 0; j < fe_values.dofs_per_cell;
1128 ++j)
1129 {
1130 if (fe.system_to_component_index(j).first ==
1131 fe.system_to_component_index(i).first)
1132 {
1133 copy_data.cell_matrix.back()(i, j) +=
1134 coefficient_vector_values[point](
1135 fe.system_to_component_index(i).first) *
1136 weight * fe_values.shape_value(j, point) *
1137 fe_values.shape_value(i, point);
1138 }
1139 }
1140 copy_data.cell_vector.back()(i) +=
1141 rhs_values_system[point](
1142 component_mapping[fe.system_to_component_index(i)
1143 .first]) *
1144 fe_values.shape_value(i, point) * weight;
1145 }
1146 else
1147 {
1148 for (unsigned int comp = 0; comp < n_components;
1149 ++comp)
1150 {
1151 for (unsigned int j = 0;
1152 j < fe_values.dofs_per_cell;
1153 ++j)
1154 copy_data.cell_matrix.back()(i, j) +=
1155 coefficient_vector_values[point](comp) *
1156 fe_values.shape_value_component(j,
1157 point,
1158 comp) *
1159 fe_values.shape_value_component(i,
1160 point,
1161 comp) *
1162 normal_adjustment[point][comp] * weight;
1163 copy_data.cell_vector.back()(i) +=
1164 rhs_values_system[point](
1165 component_mapping[comp]) *
1166 fe_values.shape_value_component(i,
1167 point,
1168 comp) *
1169 normal_adjustment[point][comp] * weight;
1170 }
1171 }
1172 }
1173 }
1174 else
1175 // FE is a scalar one
1176 {
1177 boundary_functions.find(cell->face(face)->boundary_id())
1178 ->second->value_list(fe_values.get_quadrature_points(),
1179 rhs_values_scalar);
1180
1181 if (coefficient != nullptr)
1182 coefficient->value_list(fe_values.get_quadrature_points(),
1183 coefficient_values);
1184 for (unsigned int point = 0;
1185 point < fe_values.n_quadrature_points;
1186 ++point)
1187 {
1188 const double weight = fe_values.JxW(point);
1189 for (unsigned int i = 0; i < fe_values.dofs_per_cell; ++i)
1190 {
1191 const double v = fe_values.shape_value(i, point);
1192 for (unsigned int j = 0; j < fe_values.dofs_per_cell;
1193 ++j)
1194 {
1195 const double u = fe_values.shape_value(j, point);
1196 copy_data.cell_matrix.back()(i, j) +=
1197 (coefficient_values[point] * u * v * weight);
1198 }
1199 copy_data.cell_vector.back()(i) +=
1200 rhs_values_scalar[point] * v * weight;
1201 }
1202 }
1203 }
1204
1205
1206 dofs_on_face_vector.resize(fe.n_dofs_per_face(face));
1207 cell->face(face)->get_dof_indices(dofs_on_face_vector);
1208 // for each dof on the cell, have a flag whether it is on
1209 // the face
1210 copy_data.dof_is_on_face.emplace_back(copy_data.dofs_per_cell);
1211 // check for each of the dofs on this cell whether it is
1212 // on the face
1213 for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1214 copy_data.dof_is_on_face.back()[i] =
1215 (std::find(dofs_on_face_vector.begin(),
1216 dofs_on_face_vector.end(),
1217 copy_data.dofs[i]) != dofs_on_face_vector.end());
1218 }
1219 }
1220
1221 template <int dim, int spacedim, typename number>
1222 void
1223 copy_boundary_mass_matrix_1(
1224 const MatrixCreator::internal::AssemblerBoundary::
1225 CopyData<dim, spacedim, number> &copy_data,
1226 const std::map<types::boundary_id, const Function<spacedim, number> *>
1227 &boundary_functions,
1228 const std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1229 SparseMatrix<number> &matrix,
1230 Vector<number> &rhs_vector)
1231 {
1232 // now transfer cell matrix and vector to the whole boundary matrix
1233 //
1234 // in the following: dof[i] holds the global index of the i-th degree of
1235 // freedom on the present cell. If it is also a dof on the boundary, it
1236 // must be a nonzero entry in the dof_to_boundary_mapping and then
1237 // the boundary index of this dof is dof_to_boundary_mapping[dof[i]].
1238 //
1239 // if dof[i] is not on the boundary, it should be zero on the boundary
1240 // therefore on all quadrature points and finally all of its
1241 // entries in the cell matrix and vector should be zero. If not, we
1242 // throw an error (note: because of the evaluation of the shape
1243 // functions only up to machine precision, the term "must be zero"
1244 // really should mean: "should be very small". since this is only an
1245 // assertion and not part of the code, we may choose "very small"
1246 // quite arbitrarily)
1247 //
1248 // the main problem here is that the matrix or vector entry should also
1249 // be zero if the degree of freedom dof[i] is on the boundary, but not
1250 // on the present face, i.e. on another face of the same cell also
1251 // on the boundary. We can therefore not rely on the
1252 // dof_to_boundary_mapping[dof[i]] being !=-1, we really have to
1253 // determine whether dof[i] is a dof on the present face. We do so
1254 // by getting the dofs on the face into @p{dofs_on_face_vector},
1255 // a vector as always. Usually, searching in a vector is
1256 // inefficient, so we copy the dofs into a set, which enables binary
1257 // searches.
1258 unsigned int pos(0);
1259 for (const unsigned int face : copy_data.cell->face_indices())
1260 {
1261 // check if this face is on that part of
1262 // the boundary we are interested in
1263 if (boundary_functions.find(
1264 copy_data.cell->face(face)->boundary_id()) !=
1265 boundary_functions.end())
1266 {
1267 for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1268 {
1269 if (copy_data.dof_is_on_face[pos][i] &&
1270 dof_to_boundary_mapping[copy_data.dofs[i]] !=
1271 numbers::invalid_dof_index)
1272 {
1273 for (unsigned int j = 0; j < copy_data.dofs_per_cell; ++j)
1274 if (copy_data.dof_is_on_face[pos][j] &&
1275 dof_to_boundary_mapping[copy_data.dofs[j]] !=
1276 numbers::invalid_dof_index)
1277 {
1278 AssertIsFinite(copy_data.cell_matrix[pos](i, j));
1279 matrix.add(
1280 dof_to_boundary_mapping[copy_data.dofs[i]],
1281 dof_to_boundary_mapping[copy_data.dofs[j]],
1282 copy_data.cell_matrix[pos](i, j));
1283 }
1284 AssertIsFinite(copy_data.cell_vector[pos](i));
1285 rhs_vector(dof_to_boundary_mapping[copy_data.dofs[i]]) +=
1286 copy_data.cell_vector[pos](i);
1287 }
1288 }
1289 ++pos;
1290 }
1291 }
1292 }
1293
1294
1295 template <>
1296 void inline create_boundary_mass_matrix_1<1, 3, float>(
1297 const DoFHandler<1, 3>::active_cell_iterator & /*cell*/,
1298 const MatrixCreator::internal::AssemblerBoundary::Scratch &,
1299 MatrixCreator::internal::AssemblerBoundary::CopyData<1, 3, float>
1300 & /*copy_data*/,
1301 const Mapping<1, 3> &,
1302 const FiniteElement<1, 3> &,
1303 const Quadrature<0> &,
1304 const std::map<types::boundary_id, const Function<3, float> *>
1305 & /*boundary_functions*/,
1306 const Function<3, float> *const /*coefficient*/,
1307 const std::vector<unsigned int> & /*component_mapping*/)
1308 {
1309 DEAL_II_NOT_IMPLEMENTED();
1310 }
1311
1312
1313 template <>
1314 void inline create_boundary_mass_matrix_1<1, 3, double>(
1315 const DoFHandler<1, 3>::active_cell_iterator & /*cell*/,
1316 const MatrixCreator::internal::AssemblerBoundary::Scratch &,
1317 MatrixCreator::internal::AssemblerBoundary::CopyData<1, 3, double>
1318 & /*copy_data*/,
1319 const Mapping<1, 3> &,
1320 const FiniteElement<1, 3> &,
1321 const Quadrature<0> &,
1322 const std::map<types::boundary_id, const Function<3, double> *>
1323 & /*boundary_functions*/,
1324 const Function<3, double> *const /*coefficient*/,
1325 const std::vector<unsigned int> & /*component_mapping*/)
1326 {
1327 DEAL_II_NOT_IMPLEMENTED();
1328 }
1329
1330 } // namespace internal
1331
1332
1333
1334 template <int dim, int spacedim, typename number>
1335 void
1336 create_boundary_mass_matrix(
1337 const Mapping<dim, spacedim> &mapping,
1338 const DoFHandler<dim, spacedim> &dof,
1339 const Quadrature<dim - 1> &q,
1340 SparseMatrix<number> &matrix,
1341 const std::map<types::boundary_id, const Function<spacedim, number> *>
1342 &boundary_functions,
1343 Vector<number> &rhs_vector,
1344 std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1345 const Function<spacedim, number> *const coefficient,
1346 std::vector<unsigned int> component_mapping)
1347 {
1348 // what would that be in 1d? the
1349 // identity matrix on the boundary
1350 // dofs?
1351 if (dim == 1)
1352 {
1353 DEAL_II_NOT_IMPLEMENTED();
1354 return;
1355 }
1356
1357 const FiniteElement<dim, spacedim> &fe = dof.get_fe();
1358 const unsigned int n_components = fe.n_components();
1359 const bool fe_is_hermite =
1360 (dynamic_cast<const FE_Hermite<dim, spacedim> *>(&fe) != nullptr);
1361
1362 Assert(fe_is_hermite ||
1363 matrix.n() == dof.n_boundary_dofs(boundary_functions),
1364 ExcInternalError());
1365
1366 Assert(matrix.n() == matrix.m(), ExcInternalError());
1367 Assert(matrix.n() == rhs_vector.size(), ExcInternalError());
1368 Assert(boundary_functions.size() != 0, ExcInternalError());
1369 Assert(dof_to_boundary_mapping.size() == dof.n_dofs(), ExcInternalError());
1370 Assert(coefficient == nullptr || coefficient->n_components == 1 ||
1371 coefficient->n_components == n_components,
1372 ExcComponentMismatch());
1373
1374 if (component_mapping.empty())
1375 {
1376 AssertDimension(n_components,
1377 boundary_functions.begin()->second->n_components);
1378 for (unsigned int i = 0; i < n_components; ++i)
1379 component_mapping.push_back(i);
1380 }
1381 else
1382 AssertDimension(n_components, component_mapping.size());
1383
1384 MatrixCreator::internal::AssemblerBoundary::Scratch scratch;
1385 MatrixCreator::internal::AssemblerBoundary::CopyData<dim, spacedim, number>
1386 copy_data;
1387
1388 WorkStream::run(
1389 dof.begin_active(),
1390 dof.end(),
1391 [&mapping, &fe, &q, &boundary_functions, coefficient, &component_mapping](
1392 const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
1393 const MatrixCreator::internal::AssemblerBoundary::Scratch &scratch_data,
1394 MatrixCreator::internal::AssemblerBoundary::
1395 CopyData<dim, spacedim, number> &copy_data) {
1396 internal::create_boundary_mass_matrix_1(cell,
1397 scratch_data,
1398 copy_data,
1399 mapping,
1400 fe,
1401 q,
1402 boundary_functions,
1403 coefficient,
1404 component_mapping);
1405 },
1406 [&boundary_functions, &dof_to_boundary_mapping, &matrix, &rhs_vector](
1407 const MatrixCreator::internal::AssemblerBoundary::
1408 CopyData<dim, spacedim, number> &copy_data) {
1409 internal::copy_boundary_mass_matrix_1(copy_data,
1410 boundary_functions,
1411 dof_to_boundary_mapping,
1412 matrix,
1413 rhs_vector);
1414 },
1415 scratch,
1416 copy_data);
1417 }
1418
1419
1420
1421 namespace internal
1422 {
1423 template <int dim, int spacedim, typename number>
1424 void
1425 create_hp_boundary_mass_matrix_1(
1426 const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
1427 const MatrixCreator::internal::AssemblerBoundary::Scratch &,
1428 MatrixCreator::internal::AssemblerBoundary ::
1429 CopyData<dim, spacedim, number> &copy_data,
1430 const hp::MappingCollection<dim, spacedim> &mapping,
1431 const hp::FECollection<dim, spacedim> &fe_collection,
1432 const hp::QCollection<dim - 1> &q,
1433 const std::map<types::boundary_id, const Function<spacedim, number> *>
1434 &boundary_functions,
1435 const Function<spacedim, number> *const coefficient,
1436 const std::vector<unsigned int> &component_mapping)
1437 {
1438 const unsigned int n_components = fe_collection.n_components();
1439 const unsigned int n_function_components =
1440 boundary_functions.begin()->second->n_components;
1441 const FiniteElement<dim, spacedim> &fe = cell->get_fe();
1442 const bool fe_is_system = (n_components != 1);
1443 const bool fe_is_primitive = fe.is_primitive();
1444
1445 copy_data.cell = cell;
1446 copy_data.dofs_per_cell = fe.n_dofs_per_cell();
1447 copy_data.dofs.resize(copy_data.dofs_per_cell);
1448 cell->get_dof_indices(copy_data.dofs);
1449
1450
1451 UpdateFlags update_flags =
1452 UpdateFlags(update_values | update_JxW_values | update_normal_vectors |
1453 update_quadrature_points);
1454 hp::FEFaceValues<dim, spacedim> x_fe_values(mapping,
1455 fe_collection,
1456 q,
1457 update_flags);
1458
1459 // two variables for the coefficient,
1460 // one for the two cases indicated in
1461 // the name
1462 std::vector<number> coefficient_values;
1463 std::vector<Vector<number>> coefficient_vector_values;
1464
1465 const bool coefficient_is_vector =
1466 (coefficient != nullptr && coefficient->n_components != 1);
1467
1468 std::vector<number> rhs_values_scalar;
1469 std::vector<Vector<number>> rhs_values_system;
1470
1471 std::vector<types::global_dof_index> dofs_on_face_vector;
1472
1473 copy_data.dofs.resize(copy_data.dofs_per_cell);
1474 cell->get_dof_indices(copy_data.dofs);
1475
1476 // Because CopyData objects are reused and that push_back is used,
1477 // dof_is_on_face, cell_matrix, and cell_vector must be cleared before
1478 // they are reused
1479 copy_data.dof_is_on_face.clear();
1480 copy_data.cell_matrix.clear();
1481 copy_data.cell_vector.clear();
1482
1483
1484 for (const unsigned int face : cell->face_indices())
1485 // check if this face is on that part of
1486 // the boundary we are interested in
1487 if (boundary_functions.find(cell->face(face)->boundary_id()) !=
1488 boundary_functions.end())
1489 {
1490 x_fe_values.reinit(cell, face);
1491
1492 const FEFaceValues<dim, spacedim> &fe_values =
1493 x_fe_values.get_present_fe_values();
1494
1495 copy_data.cell_matrix.emplace_back(copy_data.dofs_per_cell,
1496 copy_data.dofs_per_cell);
1497 copy_data.cell_vector.emplace_back(copy_data.dofs_per_cell);
1498
1499 if (fe_is_system)
1500 // FE has several components
1501 {
1502 coefficient_vector_values.resize(fe_values.n_quadrature_points,
1503 Vector<number>(n_components));
1504 coefficient_values.resize(fe_values.n_quadrature_points, 1.);
1505
1506 rhs_values_system.resize(fe_values.n_quadrature_points,
1507 Vector<number>(n_function_components));
1508 boundary_functions.find(cell->face(face)->boundary_id())
1509 ->second->vector_value_list(fe_values.get_quadrature_points(),
1510 rhs_values_system);
1511 if (coefficient_is_vector)
1512 // In case coefficient is vector-valued, fill
1513 // all components
1514 coefficient->vector_value_list(
1515 fe_values.get_quadrature_points(),
1516 coefficient_vector_values);
1517 else
1518 // In case the scalar function is given, update the
1519 // values, if not - use the default (1.0)
1520 {
1521 if (coefficient != nullptr)
1522 coefficient->value_list(fe_values.get_quadrature_points(),
1523 coefficient_values);
1524
1525 for (unsigned int point = 0;
1526 point < fe_values.n_quadrature_points;
1527 ++point)
1528 coefficient_vector_values[point] =
1529 coefficient_values[point];
1530 }
1531
1532 // Special treatment for Hdiv elements,
1533 // where only normal components should be projected.
1534 // For Hdiv we need to compute (u dot n, v dot n) which
1535 // can be done as sum over dim components of
1536 // u[c] * n[c] * v[c] * n[c] = u[c] * v[c] *
1537 // normal_adjustment[c] Same approach does not work for Hcurl,
1538 // so we throw an exception. Default value 1.0 allows for use
1539 // with non Hdiv elements
1540 std::vector<std::vector<double>> normal_adjustment(
1541 fe_values.n_quadrature_points,
1542 std::vector<double>(n_components, 1.));
1543
1544 for (unsigned int comp = 0; comp < n_components; ++comp)
1545 {
1546 const FiniteElement<dim, spacedim> &base =
1547 fe.base_element(fe.component_to_base_index(comp).first);
1548 const unsigned int bcomp =
1549 fe.component_to_base_index(comp).second;
1550
1551 if (!base.conforms(FiniteElementData<dim>::H1) &&
1552 base.conforms(FiniteElementData<dim>::Hdiv) &&
1553 fe_is_primitive)
1554 DEAL_II_NOT_IMPLEMENTED();
1555
1556 if (!base.conforms(FiniteElementData<dim>::H1) &&
1557 base.conforms(FiniteElementData<dim>::Hcurl))
1558 DEAL_II_NOT_IMPLEMENTED();
1559
1560 if (!base.conforms(FiniteElementData<dim>::H1) &&
1561 base.conforms(FiniteElementData<dim>::Hdiv))
1562 for (unsigned int point = 0;
1563 point < fe_values.n_quadrature_points;
1564 ++point)
1565 normal_adjustment[point][comp] =
1566 fe_values.normal_vector(point)[bcomp] *
1567 fe_values.normal_vector(point)[bcomp];
1568 }
1569
1570 for (unsigned int point = 0;
1571 point < fe_values.n_quadrature_points;
1572 ++point)
1573 {
1574 const double weight = fe_values.JxW(point);
1575 for (unsigned int i = 0; i < fe_values.dofs_per_cell; ++i)
1576 if (fe_is_primitive)
1577 {
1578 for (unsigned int j = 0; j < fe_values.dofs_per_cell;
1579 ++j)
1580 {
1581 if (fe.system_to_component_index(i).first ==
1582 fe.system_to_component_index(j).first)
1583 {
1584 copy_data.cell_matrix.back()(i, j) +=
1585 coefficient_vector_values[point](
1586 fe.system_to_component_index(i).first) *
1587 fe_values.shape_value(i, point) *
1588 fe_values.shape_value(j, point) * weight;
1589 }
1590 }
1591
1592 copy_data.cell_vector.back()(i) +=
1593 rhs_values_system[point](
1594 component_mapping[fe.system_to_component_index(i)
1595 .first]) *
1596 fe_values.shape_value(i, point) * weight;
1597 }
1598 else
1599 {
1600 for (unsigned int comp = 0; comp < n_components;
1601 ++comp)
1602 {
1603 for (unsigned int j = 0;
1604 j < fe_values.dofs_per_cell;
1605 ++j)
1606 copy_data.cell_matrix.back()(i, j) +=
1607 coefficient_vector_values[point](comp) *
1608 fe_values.shape_value_component(i,
1609 point,
1610 comp) *
1611 fe_values.shape_value_component(j,
1612 point,
1613 comp) *
1614 normal_adjustment[point][comp] * weight;
1615 copy_data.cell_vector.back()(i) +=
1616 rhs_values_system[point](
1617 component_mapping[comp]) *
1618 fe_values.shape_value_component(i,
1619 point,
1620 comp) *
1621 normal_adjustment[point][comp] * weight;
1622 }
1623 }
1624 }
1625 }
1626 else
1627 // FE is scalar
1628 {
1629 coefficient_values.resize(fe_values.n_quadrature_points, 1.);
1630 rhs_values_scalar.resize(fe_values.n_quadrature_points);
1631 boundary_functions.find(cell->face(face)->boundary_id())
1632 ->second->value_list(fe_values.get_quadrature_points(),
1633 rhs_values_scalar);
1634
1635 if (coefficient != nullptr)
1636 {
1637 coefficient_values.resize(fe_values.n_quadrature_points);
1638 coefficient->value_list(fe_values.get_quadrature_points(),
1639 coefficient_values);
1640 }
1641
1642 for (unsigned int point = 0;
1643 point < fe_values.n_quadrature_points;
1644 ++point)
1645 {
1646 const double weight = fe_values.JxW(point);
1647 for (unsigned int i = 0; i < fe_values.dofs_per_cell; ++i)
1648 {
1649 const double v = fe_values.shape_value(i, point);
1650 for (unsigned int j = 0; j < fe_values.dofs_per_cell;
1651 ++j)
1652 {
1653 const double u = fe_values.shape_value(j, point);
1654 copy_data.cell_matrix.back()(i, j) +=
1655 (coefficient_values[point] * v * u * weight);
1656 }
1657 copy_data.cell_vector.back()(i) +=
1658 rhs_values_scalar[point] * v * weight;
1659 }
1660 }
1661 }
1662
1663 dofs_on_face_vector.resize(fe.n_dofs_per_face(face));
1664 cell->face(face)->get_dof_indices(dofs_on_face_vector,
1665 cell->active_fe_index());
1666 // for each dof on the cell, have a
1667 // flag whether it is on the face
1668 copy_data.dof_is_on_face.emplace_back(copy_data.dofs_per_cell);
1669 // check for each of the dofs on this cell
1670 // whether it is on the face
1671 for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1672 copy_data.dof_is_on_face.back()[i] =
1673 (std::find(dofs_on_face_vector.begin(),
1674 dofs_on_face_vector.end(),
1675 copy_data.dofs[i]) != dofs_on_face_vector.end());
1676 }
1677 }
1678
1679
1680 template <int dim, int spacedim, typename number>
1681 void
1682 copy_hp_boundary_mass_matrix_1(
1683 const MatrixCreator::internal::AssemblerBoundary ::
1684 CopyData<dim, spacedim, number> &copy_data,
1685 const std::map<types::boundary_id, const Function<spacedim, number> *>
1686 &boundary_functions,
1687 const std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1688 SparseMatrix<number> &matrix,
1689 Vector<number> &rhs_vector)
1690 {
1691 // now transfer cell matrix and vector to the whole boundary matrix
1692 //
1693 // in the following: dof[i] holds the global index of the i-th degree of
1694 // freedom on the present cell. If it is also a dof on the boundary, it
1695 // must be a nonzero entry in the dof_to_boundary_mapping and then
1696 // the boundary index of this dof is dof_to_boundary_mapping[dof[i]].
1697 //
1698 // if dof[i] is not on the boundary, it should be zero on the boundary
1699 // therefore on all quadrature points and finally all of its
1700 // entries in the cell matrix and vector should be zero. If not, we
1701 // throw an error (note: because of the evaluation of the shape
1702 // functions only up to machine precision, the term "must be zero"
1703 // really should mean: "should be very small". since this is only an
1704 // assertion and not part of the code, we may choose "very small"
1705 // quite arbitrarily)
1706 //
1707 // the main problem here is that the matrix or vector entry should also
1708 // be zero if the degree of freedom dof[i] is on the boundary, but not
1709 // on the present face, i.e. on another face of the same cell also
1710 // on the boundary. We can therefore not rely on the
1711 // dof_to_boundary_mapping[dof[i]] being !=-1, we really have to
1712 // determine whether dof[i] is a dof on the present face. We do so
1713 // by getting the dofs on the face into @p{dofs_on_face_vector},
1714 // a vector as always. Usually, searching in a vector is
1715 // inefficient, so we copy the dofs into a set, which enables binary
1716 // searches.
1717 unsigned int pos(0);
1718 for (const unsigned int face : copy_data.cell->face_indices())
1719 {
1720 // check if this face is on that part of
1721 // the boundary we are interested in
1722 if (boundary_functions.find(
1723 copy_data.cell->face(face)->boundary_id()) !=
1724 boundary_functions.end())
1725 {
1726 if constexpr (running_in_debug_mode())
1727 {
1728 // in debug mode: compute an element in the matrix which is
1729 // guaranteed to belong to a boundary dof. We do this to check
1730 // that the entries in the cell matrix are guaranteed to be
1731 // zero if the respective dof is not on the boundary. Since
1732 // because of round-off, the actual value of the matrix entry
1733 // may be only close to zero, we assert that it is small
1734 // relative to an element which is guaranteed to be nonzero.
1735 // (absolute smallness does not suffice since the size of the
1736 // domain scales in here)
1737 //
1738 // for this purpose we seek the diagonal of the matrix, where
1739 // there must be an element belonging to the boundary. we take
1740 // the maximum diagonal entry.
1741 types::global_dof_index max_element = 0;
1742 for (const auto index : dof_to_boundary_mapping)
1743 if ((index != numbers::invalid_dof_index) &&
1744 (index > max_element))
1745 max_element = index;
1746 Assert(max_element == matrix.n() - 1, ExcInternalError());
1747
1748 double max_diag_entry = 0;
1749 for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1750 if (std::abs(copy_data.cell_matrix[pos](i, i)) >
1751 max_diag_entry)
1752 max_diag_entry =
1753 std::abs(copy_data.cell_matrix[pos](i, i));
1754 }
1755
1756 for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1757 {
1758 if (copy_data.dof_is_on_face[pos][i] &&
1759 dof_to_boundary_mapping[copy_data.dofs[i]] !=
1760 numbers::invalid_dof_index)
1761 {
1762 for (unsigned int j = 0; j < copy_data.dofs_per_cell; ++j)
1763 if (copy_data.dof_is_on_face[pos][j] &&
1764 dof_to_boundary_mapping[copy_data.dofs[j]] !=
1765 numbers::invalid_dof_index)
1766 {
1767 AssertIsFinite(copy_data.cell_matrix[pos](i, j));
1768 matrix.add(
1769 dof_to_boundary_mapping[copy_data.dofs[i]],
1770 dof_to_boundary_mapping[copy_data.dofs[j]],
1771 copy_data.cell_matrix[pos](i, j));
1772 }
1773 AssertIsFinite(copy_data.cell_vector[pos](i));
1774 rhs_vector(dof_to_boundary_mapping[copy_data.dofs[i]]) +=
1775 copy_data.cell_vector[pos](i);
1776 }
1777 }
1778 ++pos;
1779 }
1780 }
1781 }
1782 } // namespace internal
1783
1784
1785
1786 template <int dim, int spacedim, typename number>
1787 void
1788 create_boundary_mass_matrix(
1789 const DoFHandler<dim, spacedim> &dof,
1790 const Quadrature<dim - 1> &q,
1791 SparseMatrix<number> &matrix,
1792 const std::map<types::boundary_id, const Function<spacedim, number> *> &rhs,
1793 Vector<number> &rhs_vector,
1794 std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1795 const Function<spacedim, number> *const a,
1796 std::vector<unsigned int> component_mapping)
1797 {
1798 create_boundary_mass_matrix(get_default_linear_mapping(
1799 dof.get_triangulation()),
1800 dof,
1801 q,
1802 matrix,
1803 rhs,
1804 rhs_vector,
1805 dof_to_boundary_mapping,
1806 a,
1807 component_mapping);
1808 }
1809
1810
1811
1812 template <int dim, int spacedim, typename number>
1813 void
1814 create_boundary_mass_matrix(
1815 const hp::MappingCollection<dim, spacedim> &mapping,
1816 const DoFHandler<dim, spacedim> &dof,
1817 const hp::QCollection<dim - 1> &q,
1818 SparseMatrix<number> &matrix,
1819 const std::map<types::boundary_id, const Function<spacedim, number> *>
1820 &boundary_functions,
1821 Vector<number> &rhs_vector,
1822 std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1823 const Function<spacedim, number> *const coefficient,
1824 std::vector<unsigned int> component_mapping)
1825 {
1826 // what would that be in 1d? the
1827 // identity matrix on the boundary
1828 // dofs?
1829 if (dim == 1)
1830 {
1831 DEAL_II_NOT_IMPLEMENTED();
1832 return;
1833 }
1834
1835 const hp::FECollection<dim, spacedim> &fe_collection =
1836 dof.get_fe_collection();
1837 const unsigned int n_components = fe_collection.n_components();
1838
1839 Assert(matrix.n() == dof.n_boundary_dofs(boundary_functions),
1840 ExcInternalError());
1841 Assert(matrix.n() == matrix.m(), ExcInternalError());
1842 Assert(matrix.n() == rhs_vector.size(), ExcInternalError());
1843 Assert(boundary_functions.size() != 0, ExcInternalError());
1844 Assert(dof_to_boundary_mapping.size() == dof.n_dofs(), ExcInternalError());
1845 Assert(coefficient == nullptr || coefficient->n_components == 1 ||
1846 coefficient->n_components == n_components,
1847 ExcComponentMismatch());
1848
1849 if (component_mapping.empty())
1850 {
1851 AssertDimension(n_components,
1852 boundary_functions.begin()->second->n_components);
1853 for (unsigned int i = 0; i < n_components; ++i)
1854 component_mapping.push_back(i);
1855 }
1856 else
1857 AssertDimension(n_components, component_mapping.size());
1858
1859 MatrixCreator::internal::AssemblerBoundary::Scratch scratch;
1860 MatrixCreator::internal::AssemblerBoundary::CopyData<dim, spacedim, number>
1861 copy_data;
1862
1863 WorkStream::run(
1864 dof.begin_active(),
1865 dof.end(),
1866 [&mapping,
1867 &fe_collection,
1868 &q,
1869 &boundary_functions,
1870 coefficient,
1871 &component_mapping](
1872 const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
1873 const MatrixCreator::internal::AssemblerBoundary::Scratch &scratch_data,
1874 MatrixCreator::internal::AssemblerBoundary ::
1875 CopyData<dim, spacedim, number> &copy_data) {
1876 internal::create_hp_boundary_mass_matrix_1(cell,
1877 scratch_data,
1878 copy_data,
1879 mapping,
1880 fe_collection,
1881 q,
1882 boundary_functions,
1883 coefficient,
1884 component_mapping);
1885 },
1886 [&boundary_functions, &dof_to_boundary_mapping, &matrix, &rhs_vector](
1887 const MatrixCreator::internal::AssemblerBoundary ::
1888 CopyData<dim, spacedim, number> &copy_data) {
1889 internal::copy_hp_boundary_mass_matrix_1(copy_data,
1890 boundary_functions,
1891 dof_to_boundary_mapping,
1892 matrix,
1893 rhs_vector);
1894 },
1895 scratch,
1896 copy_data);
1897 }
1898
1899
1900
1901 template <int dim, int spacedim, typename number>
1902 void
1903 create_boundary_mass_matrix(
1904 const DoFHandler<dim, spacedim> &dof,
1905 const hp::QCollection<dim - 1> &q,
1906 SparseMatrix<number> &matrix,
1907 const std::map<types::boundary_id, const Function<spacedim, number> *> &rhs,
1908 Vector<number> &rhs_vector,
1909 std::vector<types::global_dof_index> &dof_to_boundary_mapping,
1910 const Function<spacedim, number> *const a,
1911 std::vector<unsigned int> component_mapping)
1912 {
1913 create_boundary_mass_matrix(
1914 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
1915 dof,
1916 q,
1917 matrix,
1918 rhs,
1919 rhs_vector,
1920 dof_to_boundary_mapping,
1921 a,
1922 component_mapping);
1923 }
1924
1925
1926
1927 template <int dim, int spacedim, typename MatrixType>
1928 void
1929 create_laplace_matrix(
1930 const Mapping<dim, spacedim> &mapping,
1931 const DoFHandler<dim, spacedim> &dof,
1932 const Quadrature<dim> &q,
1933 MatrixType &matrix,
1934 const Function<spacedim, typename MatrixType::value_type>
1935 *const coefficient,
1936 const AffineConstraints<typename MatrixType::value_type> &constraints)
1937 {
1938 Assert(matrix.m() == dof.n_dofs(),
1939 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
1940 Assert(matrix.n() == dof.n_dofs(),
1941 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
1942
1943 const auto &fe_collection = dof.get_fe_collection();
1944 hp::QCollection<dim> q_collection(q);
1945 hp::MappingCollection<dim, spacedim> mapping_collection(mapping);
1946 MatrixCreator::internal::AssemblerData::
1947 Scratch<dim, spacedim, typename MatrixType::value_type>
1948 assembler_data(fe_collection,
1949 update_gradients | update_JxW_values |
1950 (coefficient != nullptr ? update_quadrature_points :
1951 UpdateFlags(0)),
1952 coefficient,
1953 /*rhs_function=*/nullptr,
1954 q_collection,
1955 mapping_collection);
1956 MatrixCreator::internal::AssemblerData::CopyData<
1957 typename MatrixType::value_type>
1958 copy_data;
1959 copy_data.cell_matrix.reinit(
1960 assembler_data.fe_collection.max_dofs_per_cell(),
1961 assembler_data.fe_collection.max_dofs_per_cell());
1962 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
1963 copy_data.dof_indices.resize(
1964 assembler_data.fe_collection.max_dofs_per_cell());
1965 copy_data.constraints = &constraints;
1966
1967 WorkStream::run(
1968 dof.begin_active(),
1969 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
1970 dof.end()),
1971 &MatrixCreator::internal::laplace_assembler<
1972 dim,
1973 spacedim,
1974 typename DoFHandler<dim, spacedim>::active_cell_iterator,
1975 typename MatrixType::value_type>,
1976 [&matrix](
1977 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
1978 &data) {
1979 MatrixCreator::internal::copy_local_to_global(
1980 data,
1981 &matrix,
1982 static_cast<Vector<typename MatrixType::value_type> *>(nullptr));
1983 },
1984 assembler_data,
1985 copy_data);
1986
1987 matrix.compress(VectorOperation::add);
1988 }
1989
1990
1991
1992 template <int dim, int spacedim, typename MatrixType>
1993 void
1994 create_laplace_matrix(
1995 const DoFHandler<dim, spacedim> &dof,
1996 const Quadrature<dim> &q,
1997 MatrixType &matrix,
1998 const Function<spacedim, typename MatrixType::value_type>
1999 *const coefficient,
2000 const AffineConstraints<typename MatrixType::value_type> &constraints)
2001 {
2002 create_laplace_matrix(get_default_linear_mapping(dof.get_triangulation()),
2003 dof,
2004 q,
2005 matrix,
2006 coefficient,
2007 constraints);
2008 }
2009
2010
2011
2012 template <int dim, int spacedim, typename MatrixType>
2013 void
2014 create_laplace_matrix(
2015 const Mapping<dim, spacedim> &mapping,
2016 const DoFHandler<dim, spacedim> &dof,
2017 const Quadrature<dim> &q,
2018 MatrixType &matrix,
2019 const Function<spacedim, typename MatrixType::value_type> &rhs,
2020 Vector<typename MatrixType::value_type> &rhs_vector,
2021 const Function<spacedim, typename MatrixType::value_type>
2022 *const coefficient,
2023 const AffineConstraints<typename MatrixType::value_type> &constraints)
2024 {
2025 Assert(matrix.m() == dof.n_dofs(),
2026 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
2027 Assert(matrix.n() == dof.n_dofs(),
2028 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
2029
2030 const auto &fe_collection = dof.get_fe_collection();
2031 hp::QCollection<dim> q_collection(q);
2032 hp::MappingCollection<dim, spacedim> mapping_collection(mapping);
2033 MatrixCreator::internal::AssemblerData::
2034 Scratch<dim, spacedim, typename MatrixType::value_type>
2035 assembler_data(fe_collection,
2036 update_gradients | update_values | update_JxW_values |
2037 update_quadrature_points,
2038 coefficient,
2039 &rhs,
2040 q_collection,
2041 mapping_collection);
2042 MatrixCreator::internal::AssemblerData::CopyData<
2043 typename MatrixType::value_type>
2044 copy_data;
2045 copy_data.cell_matrix.reinit(
2046 assembler_data.fe_collection.max_dofs_per_cell(),
2047 assembler_data.fe_collection.max_dofs_per_cell());
2048 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
2049 copy_data.dof_indices.resize(
2050 assembler_data.fe_collection.max_dofs_per_cell());
2051 copy_data.constraints = &constraints;
2052
2053 WorkStream::run(
2054 dof.begin_active(),
2055 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
2056 dof.end()),
2057 &MatrixCreator::internal::laplace_assembler<
2058 dim,
2059 spacedim,
2060 typename DoFHandler<dim, spacedim>::active_cell_iterator,
2061 typename MatrixType::value_type>,
2062 [&matrix, &rhs_vector](
2063 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
2064 &data) {
2065 MatrixCreator::internal::copy_local_to_global(data,
2066 &matrix,
2067 &rhs_vector);
2068 },
2069 assembler_data,
2070 copy_data);
2071
2072 matrix.compress(VectorOperation::add);
2073 }
2074
2075
2076
2077 template <int dim, int spacedim, typename MatrixType>
2078 void
2079 create_laplace_matrix(
2080 const DoFHandler<dim, spacedim> &dof,
2081 const Quadrature<dim> &q,
2082 MatrixType &matrix,
2083 const Function<spacedim, typename MatrixType::value_type> &rhs,
2084 Vector<typename MatrixType::value_type> &rhs_vector,
2085 const Function<spacedim, typename MatrixType::value_type>
2086 *const coefficient,
2087 const AffineConstraints<typename MatrixType::value_type> &constraints)
2088 {
2089 create_laplace_matrix(get_default_linear_mapping(dof.get_triangulation()),
2090 dof,
2091 q,
2092 matrix,
2093 rhs,
2094 rhs_vector,
2095 coefficient,
2096 constraints);
2097 }
2098
2099
2100
2101 template <int dim, int spacedim, typename MatrixType>
2102 void
2103 create_laplace_matrix(
2104 const hp::MappingCollection<dim, spacedim> &mapping,
2105 const DoFHandler<dim, spacedim> &dof,
2106 const hp::QCollection<dim> &q,
2107 MatrixType &matrix,
2108 const Function<spacedim, typename MatrixType::value_type>
2109 *const coefficient,
2110 const AffineConstraints<typename MatrixType::value_type> &constraints)
2111 {
2112 Assert(matrix.m() == dof.n_dofs(),
2113 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
2114 Assert(matrix.n() == dof.n_dofs(),
2115 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
2116
2117 MatrixCreator::internal::AssemblerData::
2118 Scratch<dim, spacedim, typename MatrixType::value_type>
2119 assembler_data(dof.get_fe_collection(),
2120 update_gradients | update_JxW_values |
2121 (coefficient != nullptr ? update_quadrature_points :
2122 UpdateFlags(0)),
2123 coefficient,
2124 /*rhs_function=*/nullptr,
2125 q,
2126 mapping);
2127 MatrixCreator::internal::AssemblerData::CopyData<
2128 typename MatrixType::value_type>
2129 copy_data;
2130 copy_data.cell_matrix.reinit(
2131 assembler_data.fe_collection.max_dofs_per_cell(),
2132 assembler_data.fe_collection.max_dofs_per_cell());
2133 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
2134 copy_data.dof_indices.resize(
2135 assembler_data.fe_collection.max_dofs_per_cell());
2136 copy_data.constraints = &constraints;
2137
2138 WorkStream::run(
2139 dof.begin_active(),
2140 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
2141 dof.end()),
2142 &MatrixCreator::internal::laplace_assembler<
2143 dim,
2144 spacedim,
2145 typename DoFHandler<dim, spacedim>::active_cell_iterator,
2146 typename MatrixType::value_type>,
2147 [&matrix](
2148 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
2149 &data) {
2150 MatrixCreator::internal::copy_local_to_global(
2151 data,
2152 &matrix,
2153 static_cast<Vector<typename MatrixType::value_type> *>(nullptr));
2154 },
2155 assembler_data,
2156 copy_data);
2157
2158 matrix.compress(VectorOperation::add);
2159 }
2160
2161
2162
2163 template <int dim, int spacedim, typename MatrixType>
2164 void
2165 create_laplace_matrix(
2166 const DoFHandler<dim, spacedim> &dof,
2167 const hp::QCollection<dim> &q,
2168 MatrixType &matrix,
2169 const Function<spacedim, typename MatrixType::value_type>
2170 *const coefficient,
2171 const AffineConstraints<typename MatrixType::value_type> &constraints)
2172 {
2173 create_laplace_matrix(
2174 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
2175 dof,
2176 q,
2177 matrix,
2178 coefficient,
2179 constraints);
2180 }
2181
2182
2183
2184 template <int dim, int spacedim, typename MatrixType>
2185 void
2186 create_laplace_matrix(
2187 const hp::MappingCollection<dim, spacedim> &mapping,
2188 const DoFHandler<dim, spacedim> &dof,
2189 const hp::QCollection<dim> &q,
2190 MatrixType &matrix,
2191 const Function<spacedim, typename MatrixType::value_type> &rhs,
2192 Vector<typename MatrixType::value_type> &rhs_vector,
2193 const Function<spacedim, typename MatrixType::value_type>
2194 *const coefficient,
2195 const AffineConstraints<typename MatrixType::value_type> &constraints)
2196 {
2197 Assert(matrix.m() == dof.n_dofs(),
2198 ExcDimensionMismatch(matrix.m(), dof.n_dofs()));
2199 Assert(matrix.n() == dof.n_dofs(),
2200 ExcDimensionMismatch(matrix.n(), dof.n_dofs()));
2201
2202 MatrixCreator::internal::AssemblerData::
2203 Scratch<dim, spacedim, typename MatrixType::value_type>
2204 assembler_data(dof.get_fe_collection(),
2205 update_gradients | update_values | update_JxW_values |
2206 update_quadrature_points,
2207 coefficient,
2208 &rhs,
2209 q,
2210 mapping);
2211 MatrixCreator::internal::AssemblerData::CopyData<
2212 typename MatrixType::value_type>
2213 copy_data;
2214 copy_data.cell_matrix.reinit(
2215 assembler_data.fe_collection.max_dofs_per_cell(),
2216 assembler_data.fe_collection.max_dofs_per_cell());
2217 copy_data.cell_rhs.reinit(assembler_data.fe_collection.max_dofs_per_cell());
2218 copy_data.dof_indices.resize(
2219 assembler_data.fe_collection.max_dofs_per_cell());
2220 copy_data.constraints = &constraints;
2221
2222 WorkStream::run(
2223 dof.begin_active(),
2224 static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>(
2225 dof.end()),
2226 &MatrixCreator::internal::laplace_assembler<
2227 dim,
2228 spacedim,
2229 typename DoFHandler<dim, spacedim>::active_cell_iterator,
2230 typename MatrixType::value_type>,
2231 [&matrix, &rhs_vector](
2232 const internal::AssemblerData::CopyData<typename MatrixType::value_type>
2233 &data) {
2234 MatrixCreator::internal::copy_local_to_global(data,
2235 &matrix,
2236 &rhs_vector);
2237 },
2238 assembler_data,
2239 copy_data);
2240
2241 matrix.compress(VectorOperation::add);
2242 }
2243
2244
2245
2246 template <int dim, int spacedim, typename MatrixType>
2247 void
2248 create_laplace_matrix(
2249 const DoFHandler<dim, spacedim> &dof,
2250 const hp::QCollection<dim> &q,
2251 MatrixType &matrix,
2252 const Function<spacedim, typename MatrixType::value_type> &rhs,
2253 Vector<typename MatrixType::value_type> &rhs_vector,
2254 const Function<spacedim, typename MatrixType::value_type>
2255 *const coefficient,
2256 const AffineConstraints<typename MatrixType::value_type> &constraints)
2257 {
2258 create_laplace_matrix(
2259 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
2260 dof,
2261 q,
2262 matrix,
2263 rhs,
2264 rhs_vector,
2265 coefficient,
2266 constraints);
2267 }
2268
2269 } // namespace MatrixCreator
2270
2271 DEAL_II_NAMESPACE_CLOSE
2272
2273 #endif