Debian Codegraph @

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

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 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_vector_tools_rhs_templates_h
16 #define dealii_vector_tools_rhs_templates_h
17
18 #include <deal.II/fe/mapping_q1.h>
19
20 #include <deal.II/hp/fe_values.h>
21
22 #include <deal.II/lac/block_vector.h>
23 #include <deal.II/lac/la_parallel_block_vector.h>
24 #include <deal.II/lac/la_parallel_vector.h>
25 #include <deal.II/lac/petsc_block_vector.h>
26 #include <deal.II/lac/petsc_vector.h>
27 #include <deal.II/lac/trilinos_parallel_block_vector.h>
28 #include <deal.II/lac/trilinos_vector.h>
29
30 #include <deal.II/numerics/vector_tools_rhs.h>
31
32 #include <set>
33
34
35 DEAL_II_NAMESPACE_OPEN
36
37 namespace VectorTools
38 {
39 template <int dim, int spacedim, typename VectorType>
40 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
41 void create_boundary_right_hand_side(
42 const Mapping<dim, spacedim> &mapping,
43 const DoFHandler<dim, spacedim> &dof_handler,
44 const Quadrature<dim - 1> &quadrature,
45 const Function<spacedim, typename VectorType::value_type> &rhs_function,
46 VectorType &rhs_vector,
47 const std::set<types::boundary_id> &boundary_ids)
48 {
49 const FiniteElement<dim> &fe = dof_handler.get_fe();
50 AssertDimension(fe.n_components(), rhs_function.n_components);
51 AssertDimension(rhs_vector.size(), dof_handler.n_dofs());
52
53 rhs_vector = typename VectorType::value_type(0.0);
54
55 UpdateFlags update_flags =
56 UpdateFlags(update_values | update_quadrature_points | update_JxW_values);
57 FEFaceValues<dim> fe_values(mapping, fe, quadrature, update_flags);
58
59 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
60 n_q_points = fe_values.n_quadrature_points,
61 n_components = fe.n_components();
62
63 std::vector<types::global_dof_index> dofs(dofs_per_cell);
64 Vector<double> cell_vector(dofs_per_cell);
65
66 if (n_components == 1)
67 {
68 std::vector<double> rhs_values(n_q_points);
69
70 for (const auto &cell : dof_handler.active_cell_iterators())
71 for (const unsigned int face : cell->face_indices())
72 if (cell->face(face)->at_boundary() &&
73 (boundary_ids.empty() ||
74 (boundary_ids.find(cell->face(face)->boundary_id()) !=
75 boundary_ids.end())))
76 {
77 fe_values.reinit(cell, face);
78
79 const std::vector<double> &weights = fe_values.get_JxW_values();
80 rhs_function.value_list(fe_values.get_quadrature_points(),
81 rhs_values);
82
83 cell_vector = 0;
84 for (unsigned int point = 0; point < n_q_points; ++point)
85 for (unsigned int i = 0; i < dofs_per_cell; ++i)
86 cell_vector(i) += rhs_values[point] *
87 fe_values.shape_value(i, point) *
88 weights[point];
89
90 cell->get_dof_indices(dofs);
91
92 for (unsigned int i = 0; i < dofs_per_cell; ++i)
93 rhs_vector(dofs[i]) += cell_vector(i);
94 }
95 }
96 else
97 {
98 std::vector<Vector<double>> rhs_values(n_q_points,
99 Vector<double>(n_components));
100
101 for (const auto &cell : dof_handler.active_cell_iterators())
102 for (const unsigned int face : cell->face_indices())
103 if (cell->face(face)->at_boundary() &&
104 (boundary_ids.empty() ||
105 (boundary_ids.find(cell->face(face)->boundary_id()) !=
106 boundary_ids.end())))
107 {
108 fe_values.reinit(cell, face);
109
110 const std::vector<double> &weights = fe_values.get_JxW_values();
111 rhs_function.vector_value_list(
112 fe_values.get_quadrature_points(), rhs_values);
113
114 cell_vector = 0;
115
116 // Use the faster code if the
117 // FiniteElement is primitive
118 if (fe.is_primitive())
119 {
120 for (unsigned int point = 0; point < n_q_points; ++point)
121 for (unsigned int i = 0; i < dofs_per_cell; ++i)
122 {
123 const unsigned int component =
124 fe.system_to_component_index(i).first;
125
126 cell_vector(i) += rhs_values[point](component) *
127 fe_values.shape_value(i, point) *
128 weights[point];
129 }
130 }
131 else
132 {
133 // And the full featured
134 // code, if vector valued
135 // FEs are used
136 for (unsigned int point = 0; point < n_q_points; ++point)
137 for (unsigned int i = 0; i < dofs_per_cell; ++i)
138 for (unsigned int comp_i = 0; comp_i < n_components;
139 ++comp_i)
140 if (fe.get_nonzero_components(i)[comp_i])
141 {
142 cell_vector(i) +=
143 rhs_values[point](comp_i) *
144 fe_values.shape_value_component(i,
145 point,
146 comp_i) *
147 weights[point];
148 }
149 }
150
151 cell->get_dof_indices(dofs);
152
153 for (unsigned int i = 0; i < dofs_per_cell; ++i)
154 rhs_vector(dofs[i]) += cell_vector(i);
155 }
156 }
157 }
158
159
160
161 template <int dim, int spacedim, typename VectorType>
162 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
163 void create_boundary_right_hand_side(
164 const DoFHandler<dim, spacedim> &dof_handler,
165 const Quadrature<dim - 1> &quadrature,
166 const Function<spacedim, typename VectorType::value_type> &rhs_function,
167 VectorType &rhs_vector,
168 const std::set<types::boundary_id> &boundary_ids)
169 {
170 create_boundary_right_hand_side(StaticMappingQ1<dim>::mapping,
171 dof_handler,
172 quadrature,
173 rhs_function,
174 rhs_vector,
175 boundary_ids);
176 }
177
178
179
180 template <int dim, int spacedim, typename VectorType>
181 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
182 void create_boundary_right_hand_side(
183 const hp::MappingCollection<dim, spacedim> &mapping,
184 const DoFHandler<dim, spacedim> &dof_handler,
185 const hp::QCollection<dim - 1> &quadrature,
186 const Function<spacedim, typename VectorType::value_type> &rhs_function,
187 VectorType &rhs_vector,
188 const std::set<types::boundary_id> &boundary_ids)
189 {
190 const hp::FECollection<dim> &fe = dof_handler.get_fe_collection();
191 AssertDimension(fe.n_components(), rhs_function.n_components);
192 AssertDimension(rhs_vector.size(), dof_handler.n_dofs());
193
194 rhs_vector = typename VectorType::value_type(0.0);
195
196 UpdateFlags update_flags =
197 UpdateFlags(update_values | update_quadrature_points | update_JxW_values);
198 hp::FEFaceValues<dim> x_fe_values(mapping, fe, quadrature, update_flags);
199
200 const unsigned int n_components = fe.n_components();
201
202 std::vector<types::global_dof_index> dofs(fe.max_dofs_per_cell());
203 Vector<double> cell_vector(fe.max_dofs_per_cell());
204
205 if (n_components == 1)
206 {
207 std::vector<double> rhs_values;
208
209 for (const auto &cell : dof_handler.active_cell_iterators())
210 for (const unsigned int face : cell->face_indices())
211 if (cell->face(face)->at_boundary() &&
212 (boundary_ids.empty() ||
213 (boundary_ids.find(cell->face(face)->boundary_id()) !=
214 boundary_ids.end())))
215 {
216 x_fe_values.reinit(cell, face);
217
218 const FEFaceValues<dim> &fe_values =
219 x_fe_values.get_present_fe_values();
220
221 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
222 n_q_points = fe_values.n_quadrature_points;
223 rhs_values.resize(n_q_points);
224
225 const std::vector<double> &weights = fe_values.get_JxW_values();
226 rhs_function.value_list(fe_values.get_quadrature_points(),
227 rhs_values);
228
229 cell_vector = 0;
230 for (unsigned int point = 0; point < n_q_points; ++point)
231 for (unsigned int i = 0; i < dofs_per_cell; ++i)
232 cell_vector(i) += rhs_values[point] *
233 fe_values.shape_value(i, point) *
234 weights[point];
235
236 dofs.resize(dofs_per_cell);
237 cell->get_dof_indices(dofs);
238
239 for (unsigned int i = 0; i < dofs_per_cell; ++i)
240 rhs_vector(dofs[i]) += cell_vector(i);
241 }
242 }
243 else
244 {
245 std::vector<Vector<double>> rhs_values;
246
247 for (const auto &cell : dof_handler.active_cell_iterators())
248 for (const unsigned int face : cell->face_indices())
249 if (cell->face(face)->at_boundary() &&
250 (boundary_ids.empty() ||
251 (boundary_ids.find(cell->face(face)->boundary_id()) !=
252 boundary_ids.end())))
253 {
254 x_fe_values.reinit(cell, face);
255
256 const FEFaceValues<dim> &fe_values =
257 x_fe_values.get_present_fe_values();
258
259 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
260 n_q_points = fe_values.n_quadrature_points;
261 rhs_values.resize(n_q_points, Vector<double>(n_components));
262
263 const std::vector<double> &weights = fe_values.get_JxW_values();
264 rhs_function.vector_value_list(
265 fe_values.get_quadrature_points(), rhs_values);
266
267 cell_vector = 0;
268
269 // Use the faster code if the
270 // FiniteElement is primitive
271 if (cell->get_fe().is_primitive())
272 {
273 for (unsigned int point = 0; point < n_q_points; ++point)
274 for (unsigned int i = 0; i < dofs_per_cell; ++i)
275 {
276 const unsigned int component =
277 cell->get_fe().system_to_component_index(i).first;
278
279 cell_vector(i) += rhs_values[point](component) *
280 fe_values.shape_value(i, point) *
281 weights[point];
282 }
283 }
284 else
285 {
286 // And the full featured
287 // code, if vector valued
288 // FEs are used
289 for (unsigned int point = 0; point < n_q_points; ++point)
290 for (unsigned int i = 0; i < dofs_per_cell; ++i)
291 for (unsigned int comp_i = 0; comp_i < n_components;
292 ++comp_i)
293 if (cell->get_fe().get_nonzero_components(i)[comp_i])
294 {
295 cell_vector(i) +=
296 rhs_values[point](comp_i) *
297 fe_values.shape_value_component(i,
298 point,
299 comp_i) *
300 weights[point];
301 }
302 }
303 dofs.resize(dofs_per_cell);
304 cell->get_dof_indices(dofs);
305
306 for (unsigned int i = 0; i < dofs_per_cell; ++i)
307 rhs_vector(dofs[i]) += cell_vector(i);
308 }
309 }
310 }
311
312
313
314 template <int dim, int spacedim, typename VectorType>
315 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
316 void create_boundary_right_hand_side(
317 const DoFHandler<dim, spacedim> &dof_handler,
318 const hp::QCollection<dim - 1> &quadrature,
319 const Function<spacedim, typename VectorType::value_type> &rhs_function,
320 VectorType &rhs_vector,
321 const std::set<types::boundary_id> &boundary_ids)
322 {
323 create_boundary_right_hand_side(
324 hp::StaticMappingQ1<dim>::mapping_collection,
325 dof_handler,
326 quadrature,
327 rhs_function,
328 rhs_vector,
329 boundary_ids);
330 }
331
332
333
334 template <int dim, int spacedim, typename VectorType>
335 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
336 void create_right_hand_side(
337 const Mapping<dim, spacedim> &mapping,
338 const DoFHandler<dim, spacedim> &dof_handler,
339 const Quadrature<dim> &quadrature,
340 const Function<spacedim, typename VectorType::value_type> &rhs_function,
341 VectorType &rhs_vector,
342 const AffineConstraints<typename VectorType::value_type> &constraints)
343 {
344 using Number = typename VectorType::value_type;
345
346 const FiniteElement<dim, spacedim> &fe = dof_handler.get_fe();
347 AssertDimension(fe.n_components(), rhs_function.n_components);
348 AssertDimension(rhs_vector.size(), dof_handler.n_dofs());
349 rhs_vector = typename VectorType::value_type(0.0);
350
351 UpdateFlags update_flags =
352 UpdateFlags(update_values | update_quadrature_points | update_JxW_values);
353 FEValues<dim, spacedim> fe_values(mapping, fe, quadrature, update_flags);
354
355 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
356 n_q_points = fe_values.n_quadrature_points,
357 n_components = fe.n_components();
358
359 std::vector<types::global_dof_index> dofs(dofs_per_cell);
360 Vector<Number> cell_vector(dofs_per_cell);
361
362 if (n_components == 1)
363 {
364 std::vector<Number> rhs_values(n_q_points);
365
366 for (const auto &cell : dof_handler.active_cell_iterators())
367 if (cell->is_locally_owned())
368 {
369 fe_values.reinit(cell);
370
371 const std::vector<double> &weights = fe_values.get_JxW_values();
372 rhs_function.value_list(fe_values.get_quadrature_points(),
373 rhs_values);
374
375 cell_vector = 0;
376 for (unsigned int point = 0; point < n_q_points; ++point)
377 for (unsigned int i = 0; i < dofs_per_cell; ++i)
378 cell_vector(i) += rhs_values[point] *
379 fe_values.shape_value(i, point) *
380 weights[point];
381
382 cell->get_dof_indices(dofs);
383
384 constraints.distribute_local_to_global(cell_vector,
385 dofs,
386 rhs_vector);
387 }
388 }
389 else
390 {
391 std::vector<Vector<Number>> rhs_values(n_q_points,
392 Vector<Number>(n_components));
393
394 for (const auto &cell : dof_handler.active_cell_iterators())
395 if (cell->is_locally_owned())
396 {
397 fe_values.reinit(cell);
398
399 const std::vector<double> &weights = fe_values.get_JxW_values();
400 rhs_function.vector_value_list(fe_values.get_quadrature_points(),
401 rhs_values);
402
403 cell_vector = 0;
404 // Use the faster code if the
405 // FiniteElement is primitive
406 if (fe.is_primitive())
407 {
408 for (unsigned int point = 0; point < n_q_points; ++point)
409 for (unsigned int i = 0; i < dofs_per_cell; ++i)
410 {
411 const unsigned int component =
412 fe.system_to_component_index(i).first;
413
414 cell_vector(i) += rhs_values[point](component) *
415 fe_values.shape_value(i, point) *
416 weights[point];
417 }
418 }
419 else
420 {
421 // Otherwise do it the way
422 // proposed for vector valued
423 // elements
424 for (unsigned int point = 0; point < n_q_points; ++point)
425 for (unsigned int i = 0; i < dofs_per_cell; ++i)
426 for (unsigned int comp_i = 0; comp_i < n_components;
427 ++comp_i)
428 if (fe.get_nonzero_components(i)[comp_i])
429 {
430 cell_vector(i) +=
431 rhs_values[point](comp_i) *
432 fe_values.shape_value_component(i,
433 point,
434 comp_i) *
435 weights[point];
436 }
437 }
438 cell->get_dof_indices(dofs);
439
440 constraints.distribute_local_to_global(cell_vector,
441 dofs,
442 rhs_vector);
443 }
444 }
445
446 rhs_vector.compress(VectorOperation::values::add);
447 }
448
449
450
451 template <int dim, int spacedim, typename VectorType>
452 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
453 void create_right_hand_side(
454 const DoFHandler<dim, spacedim> &dof_handler,
455 const Quadrature<dim> &quadrature,
456 const Function<spacedim, typename VectorType::value_type> &rhs_function,
457 VectorType &rhs_vector,
458 const AffineConstraints<typename VectorType::value_type> &constraints)
459 {
460 create_right_hand_side(get_default_linear_mapping(
461 dof_handler.get_triangulation()),
462 dof_handler,
463 quadrature,
464 rhs_function,
465 rhs_vector,
466 constraints);
467 }
468
469
470
471 template <int dim, int spacedim, typename VectorType>
472 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
473 void create_right_hand_side(
474 const hp::MappingCollection<dim, spacedim> &mapping,
475 const DoFHandler<dim, spacedim> &dof_handler,
476 const hp::QCollection<dim> &quadrature,
477 const Function<spacedim, typename VectorType::value_type> &rhs_function,
478 VectorType &rhs_vector,
479 const AffineConstraints<typename VectorType::value_type> &constraints)
480 {
481 using Number = typename VectorType::value_type;
482
483 const hp::FECollection<dim, spacedim> &fe = dof_handler.get_fe_collection();
484 AssertDimension(fe.n_components(), rhs_function.n_components);
485 AssertDimension(rhs_vector.size(), dof_handler.n_dofs());
486 rhs_vector = typename VectorType::value_type(0.0);
487
488 UpdateFlags update_flags =
489 UpdateFlags(update_values | update_quadrature_points | update_JxW_values);
490 hp::FEValues<dim, spacedim> x_fe_values(mapping,
491 fe,
492 quadrature,
493 update_flags);
494
495 const unsigned int n_components = fe.n_components();
496
497 std::vector<types::global_dof_index> dofs(fe.max_dofs_per_cell());
498 Vector<Number> cell_vector(fe.max_dofs_per_cell());
499
500 if (n_components == 1)
501 {
502 std::vector<Number> rhs_values;
503
504 for (const auto &cell : dof_handler.active_cell_iterators())
505 if (cell->is_locally_owned())
506 {
507 x_fe_values.reinit(cell);
508
509 const FEValues<dim, spacedim> &fe_values =
510 x_fe_values.get_present_fe_values();
511
512 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
513 n_q_points = fe_values.n_quadrature_points;
514 rhs_values.resize(n_q_points);
515 dofs.resize(dofs_per_cell);
516 cell_vector.reinit(dofs_per_cell);
517
518 const auto &weights = fe_values.get_JxW_values();
519 rhs_function.value_list(fe_values.get_quadrature_points(),
520 rhs_values);
521
522 cell_vector = 0;
523 for (unsigned int point = 0; point < n_q_points; ++point)
524 for (unsigned int i = 0; i < dofs_per_cell; ++i)
525 cell_vector(i) += rhs_values[point] *
526 fe_values.shape_value(i, point) *
527 weights[point];
528
529 cell->get_dof_indices(dofs);
530
531 constraints.distribute_local_to_global(cell_vector,
532 dofs,
533 rhs_vector);
534 }
535 }
536 else
537 {
538 std::vector<Vector<Number>> rhs_values;
539
540 for (const auto &cell : dof_handler.active_cell_iterators())
541 if (cell->is_locally_owned())
542 {
543 x_fe_values.reinit(cell);
544
545 const FEValues<dim, spacedim> &fe_values =
546 x_fe_values.get_present_fe_values();
547
548 const unsigned int dofs_per_cell = fe_values.dofs_per_cell,
549 n_q_points = fe_values.n_quadrature_points;
550 rhs_values.resize(n_q_points, Vector<Number>(n_components));
551 dofs.resize(dofs_per_cell);
552 cell_vector.reinit(dofs_per_cell);
553
554 const auto &weights = fe_values.get_JxW_values();
555 rhs_function.vector_value_list(fe_values.get_quadrature_points(),
556 rhs_values);
557
558 cell_vector = 0;
559
560 // Use the faster code if the
561 // FiniteElement is primitive
562 if (cell->get_fe().is_primitive())
563 {
564 for (unsigned int point = 0; point < n_q_points; ++point)
565 for (unsigned int i = 0; i < dofs_per_cell; ++i)
566 {
567 const unsigned int component =
568 cell->get_fe().system_to_component_index(i).first;
569
570 cell_vector(i) += rhs_values[point](component) *
571 fe_values.shape_value(i, point) *
572 weights[point];
573 }
574 }
575 else
576 {
577 // Otherwise do it the way proposed
578 // for vector valued elements
579 for (unsigned int point = 0; point < n_q_points; ++point)
580 for (unsigned int i = 0; i < dofs_per_cell; ++i)
581 for (unsigned int comp_i = 0; comp_i < n_components;
582 ++comp_i)
583 if (cell->get_fe().get_nonzero_components(i)[comp_i])
584 {
585 cell_vector(i) +=
586 rhs_values[point](comp_i) *
587 fe_values.shape_value_component(i,
588 point,
589 comp_i) *
590 weights[point];
591 }
592 }
593
594 cell->get_dof_indices(dofs);
595
596 constraints.distribute_local_to_global(cell_vector,
597 dofs,
598 rhs_vector);
599 }
600 }
601
602 rhs_vector.compress(VectorOperation::values::add);
603 }
604
605
606
607 template <int dim, int spacedim, typename VectorType>
608 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
609 void create_right_hand_side(
610 const DoFHandler<dim, spacedim> &dof_handler,
611 const hp::QCollection<dim> &quadrature,
612 const Function<spacedim, typename VectorType::value_type> &rhs_function,
613 VectorType &rhs_vector,
614 const AffineConstraints<typename VectorType::value_type> &constraints)
615 {
616 create_right_hand_side(
617 hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
618 dof_handler,
619 quadrature,
620 rhs_function,
621 rhs_vector,
622 constraints);
623 }
624 } // namespace VectorTools
625
626 DEAL_II_NAMESPACE_CLOSE
627
628 #endif // dealii_vector_tools_rhs_templates_h