Debian Codegraph @

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

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2007 - 2024 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_fe_field_function_templates_h
16 #define dealii_fe_field_function_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/fe/fe_values.h>
22
23 #include <deal.II/grid/grid_tools.h>
24 #include <deal.II/grid/grid_tools_cache.h>
25
26 #include <deal.II/hp/fe_collection.h>
27 #include <deal.II/hp/fe_values.h>
28 #include <deal.II/hp/mapping_collection.h>
29 #include <deal.II/hp/q_collection.h>
30
31 #include <deal.II/numerics/fe_field_function.h>
32 #include <deal.II/numerics/vector_tools_common.h>
33
34 #include <tuple>
35
36
37
38 DEAL_II_NAMESPACE_OPEN
39
40 namespace Functions
41 {
42 template <int dim, typename VectorType, int spacedim>
43 FEFieldFunction<dim, VectorType, spacedim>::FEFieldFunction(
44 const DoFHandler<dim, spacedim> &mydh,
45 const VectorType &myv,
46 const Mapping<dim> &mymapping)
47 : Function<dim, typename VectorType::value_type>(
48 mydh.get_fe(0).n_components())
49 , dh(&mydh, "FEFieldFunction")
50 , data_vector(myv)
51 , mapping(mymapping)
52 , cache(dh->get_triangulation(), mymapping)
53 , cell_hint(dh->end())
54 {}
55
56
57
58 template <int dim, typename VectorType, int spacedim>
59 void
60 FEFieldFunction<dim, VectorType, spacedim>::set_active_cell(
61 const typename DoFHandler<dim, spacedim>::active_cell_iterator &newcell)
62 {
63 cell_hint.get() = newcell;
64 }
65
66
67
68 template <int dim, typename VectorType, int spacedim>
69 void
70 FEFieldFunction<dim, VectorType, spacedim>::vector_value(
71 const Point<dim> &p,
72 Vector<typename VectorType::value_type> &values) const
73 {
74 Assert(values.size() == this->n_components,
75 ExcDimensionMismatch(values.size(), this->n_components));
76 typename DoFHandler<dim, spacedim>::active_cell_iterator cell =
77 cell_hint.get();
78 if (cell == dh->end())
79 cell = dh->begin_active();
80
81 std::optional<Point<dim>> qp = get_reference_coordinates(cell, p);
82 if (!qp)
83 {
84 const std::pair<
85 typename dealii::internal::
86 ActiveCellIterator<dim, dim, DoFHandler<dim, spacedim>>::type,
87 Point<dim>>
88 my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p);
89 AssertThrow(my_pair.first.state() == IteratorState::valid &&
90 !my_pair.first->is_artificial(),
91 VectorTools::ExcPointNotAvailableHere());
92
93 cell = my_pair.first;
94 qp = my_pair.second;
95 }
96
97 cell_hint.get() = cell;
98
99 // check that the current cell is available:
100 AssertThrow(!cell->is_artificial(),
101 VectorTools::ExcPointNotAvailableHere());
102
103 // Now we can find out about the point
104 Quadrature<dim> quad = *qp;
105 FEValues<dim> fe_v(mapping, cell->get_fe(), quad, update_values);
106 fe_v.reinit(cell);
107 std::vector<Vector<typename VectorType::value_type>> vector_values(
108 1, Vector<typename VectorType::value_type>(values.size()));
109 fe_v.get_function_values(data_vector, vector_values);
110 values = vector_values[0];
111 }
112
113
114
115 template <int dim, typename VectorType, int spacedim>
116 typename VectorType::value_type
117 FEFieldFunction<dim, VectorType, spacedim>::value(
118 const Point<dim> &p,
119 const unsigned int comp) const
120 {
121 Vector<typename VectorType::value_type> values(this->n_components);
122 vector_value(p, values);
123 return values(comp);
124 }
125
126
127
128 template <int dim, typename VectorType, int spacedim>
129 void
130 FEFieldFunction<dim, VectorType, spacedim>::vector_gradient(
131 const Point<dim> &p,
132 std::vector<Tensor<1, dim, typename VectorType::value_type>> &gradients)
133 const
134 {
135 using number = typename VectorType::value_type;
136 Assert(gradients.size() == this->n_components,
137 ExcDimensionMismatch(gradients.size(), this->n_components));
138 typename DoFHandler<dim, spacedim>::active_cell_iterator cell =
139 cell_hint.get();
140 if (cell == dh->end())
141 cell = dh->begin_active();
142
143 std::optional<Point<dim>> qp = get_reference_coordinates(cell, p);
144 if (!qp)
145 {
146 const std::pair<
147 typename dealii::internal::
148 ActiveCellIterator<dim, dim, DoFHandler<dim, spacedim>>::type,
149 Point<dim>>
150 my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p);
151 AssertThrow(my_pair.first.state() == IteratorState::valid &&
152 !my_pair.first->is_artificial(),
153 VectorTools::ExcPointNotAvailableHere());
154
155 cell = my_pair.first;
156 qp = my_pair.second;
157 }
158
159 // check that the current cell is available:
160 AssertThrow(!cell->is_artificial(),
161 VectorTools::ExcPointNotAvailableHere());
162
163 cell_hint.get() = cell;
164
165 // Now we can find out about the point
166 Quadrature<dim> quad = *qp;
167 FEValues<dim> fe_v(mapping, cell->get_fe(), quad, update_gradients);
168 fe_v.reinit(cell);
169
170 if (this->n_components == 1)
171 {
172 // the size of the @p gradients coincidentally coincides
173 // with the number of quadrature points we evaluate the function at.
174 fe_v.get_function_gradients(data_vector, gradients);
175 }
176 else
177 {
178 // Unfortunately we still need a temporary argument as we want to
179 // evaluate a gradient of a (generally) multicomponent function at
180 // a single quadrature point. Note that the first std::vector<> is
181 // related to the number of quadrature points (always one here), whereas
182 // the second to the number of components.
183 std::vector<std::vector<Tensor<1, dim, number>>> vgrads(
184 1, std::vector<Tensor<1, dim, number>>(this->n_components));
185 fe_v.get_function_gradients(data_vector, vgrads);
186 gradients = vgrads[0];
187 }
188 }
189
190
191
192 template <int dim, typename VectorType, int spacedim>
193 Tensor<1, dim, typename VectorType::value_type>
194 FEFieldFunction<dim, VectorType, spacedim>::gradient(
195 const Point<dim> &p,
196 const unsigned int comp) const
197 {
198 std::vector<Tensor<1, dim, typename VectorType::value_type>> grads(
199 this->n_components);
200 vector_gradient(p, grads);
201 return grads[comp];
202 }
203
204
205
206 template <int dim, typename VectorType, int spacedim>
207 void
208 FEFieldFunction<dim, VectorType, spacedim>::vector_laplacian(
209 const Point<dim> &p,
210 Vector<typename VectorType::value_type> &values) const
211 {
212 Assert(values.size() == this->n_components,
213 ExcDimensionMismatch(values.size(), this->n_components));
214 typename DoFHandler<dim, spacedim>::active_cell_iterator cell =
215 cell_hint.get();
216 if (cell == dh->end())
217 cell = dh->begin_active();
218
219 std::optional<Point<dim>> qp = get_reference_coordinates(cell, p);
220 if (!qp)
221 {
222 const std::pair<
223 typename dealii::internal::
224 ActiveCellIterator<dim, dim, DoFHandler<dim, spacedim>>::type,
225 Point<dim>>
226 my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p);
227 AssertThrow(my_pair.first.state() == IteratorState::valid &&
228 !my_pair.first->is_artificial(),
229 VectorTools::ExcPointNotAvailableHere());
230
231 cell = my_pair.first;
232 qp = my_pair.second;
233 }
234
235 // check that the current cell is available:
236 AssertThrow(!cell->is_artificial(),
237 VectorTools::ExcPointNotAvailableHere());
238
239 cell_hint.get() = cell;
240
241 // Now we can find out about the point
242 Quadrature<dim> quad = *qp;
243 FEValues<dim> fe_v(mapping, cell->get_fe(), quad, update_hessians);
244 fe_v.reinit(cell);
245 std::vector<Vector<typename VectorType::value_type>> vector_values(
246 1, Vector<typename VectorType::value_type>(values.size()));
247 fe_v.get_function_laplacians(data_vector, vector_values);
248 values = vector_values[0];
249 }
250
251
252
253 template <int dim, typename VectorType, int spacedim>
254 typename VectorType::value_type
255 FEFieldFunction<dim, VectorType, spacedim>::laplacian(
256 const Point<dim> &p,
257 const unsigned int comp) const
258 {
259 Vector<typename VectorType::value_type> lap(this->n_components);
260 vector_laplacian(p, lap);
261 return lap[comp];
262 }
263
264
265 // Now the list versions
266 // ==============================
267
268 template <int dim, typename VectorType, int spacedim>
269 void
270 FEFieldFunction<dim, VectorType, spacedim>::vector_value_list(
271 const std::vector<Point<dim>> &points,
272 std::vector<Vector<typename VectorType::value_type>> &values) const
273 {
274 Assert(points.size() == values.size(),
275 ExcDimensionMismatch(points.size(), values.size()));
276
277 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator> cells;
278 std::vector<std::vector<Point<dim>>> qpoints;
279 std::vector<std::vector<unsigned int>> maps;
280
281 const unsigned int n_cells =
282 compute_point_locations(points, cells, qpoints, maps);
283
284 // Create quadrature collection
285 hp::QCollection<dim> quadrature_collection;
286 for (unsigned int i = 0; i < n_cells; ++i)
287 {
288 // Number of quadrature points on this cell
289 unsigned int nq = qpoints[i].size();
290 // Construct a quadrature formula
291 std::vector<double> ww(nq, 1. / nq);
292
293 quadrature_collection.push_back(Quadrature<dim>(qpoints[i], ww));
294 }
295
296 // Now gather all the information we need
297 hp::MappingCollection<dim> mapping_collection(mapping);
298 hp::FEValues<dim> fe_v(mapping_collection,
299 dh->get_fe_collection(),
300 quadrature_collection,
301 update_values);
302 for (unsigned int i = 0; i < n_cells; ++i)
303 {
304 AssertThrow(!cells[i]->is_artificial(),
305 VectorTools::ExcPointNotAvailableHere());
306
307 fe_v.reinit(cells[i], i, 0);
308 const unsigned int nq = qpoints[i].size();
309
310 std::vector<Vector<typename VectorType::value_type>> vector_values(
311 nq, Vector<typename VectorType::value_type>(this->n_components));
312 fe_v.get_present_fe_values().get_function_values(data_vector,
313 vector_values);
314
315 for (unsigned int q = 0; q < nq; ++q)
316 values[maps[i][q]] = vector_values[q];
317 }
318 }
319
320
321
322 template <int dim, typename VectorType, int spacedim>
323 void
324 FEFieldFunction<dim, VectorType, spacedim>::value_list(
325 const std::vector<Point<dim>> &points,
326 std::vector<typename VectorType::value_type> &values,
327 const unsigned int component) const
328 {
329 Assert(points.size() == values.size(),
330 ExcDimensionMismatch(points.size(), values.size()));
331
332 // Simply forward everything to the vector_value_list()
333 // function. This requires a temporary object, but everything we
334 // do here is so expensive that that really doesn't make any
335 // difference any more.
336 std::vector<Vector<typename VectorType::value_type>> vector_values(
337 points.size(),
338 Vector<typename VectorType::value_type>(this->n_components));
339
340 vector_value_list(points, vector_values);
341
342 for (unsigned int q = 0; q < points.size(); ++q)
343 values[q] = vector_values[q](component);
344 }
345
346
347
348 template <int dim, typename VectorType, int spacedim>
349 void
350 FEFieldFunction<dim, VectorType, spacedim>::vector_gradient_list(
351 const std::vector<Point<dim>> &points,
352 std::vector<std::vector<Tensor<1, dim, typename VectorType::value_type>>>
353 &values) const
354 {
355 Assert(points.size() == values.size(),
356 ExcDimensionMismatch(points.size(), values.size()));
357
358 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator> cells;
359 std::vector<std::vector<Point<dim>>> qpoints;
360 std::vector<std::vector<unsigned int>> maps;
361
362 const unsigned int n_cells =
363 compute_point_locations(points, cells, qpoints, maps);
364
365 // Create quadrature collection
366 hp::QCollection<dim> quadrature_collection;
367 for (unsigned int i = 0; i < n_cells; ++i)
368 {
369 // Number of quadrature points on this cell
370 unsigned int nq = qpoints[i].size();
371 // Construct a quadrature formula
372 std::vector<double> ww(nq, 1. / nq);
373
374 quadrature_collection.push_back(Quadrature<dim>(qpoints[i], ww));
375 }
376
377 // Now gather all the information we need
378 hp::MappingCollection<dim> mapping_collection(mapping);
379 hp::FEValues<dim> fe_v(mapping_collection,
380 dh->get_fe_collection(),
381 quadrature_collection,
382 update_gradients);
383 for (unsigned int i = 0; i < n_cells; ++i)
384 {
385 AssertThrow(!cells[i]->is_artificial(),
386 VectorTools::ExcPointNotAvailableHere());
387
388 fe_v.reinit(cells[i], i, 0);
389
390 const unsigned int nq = qpoints[i].size();
391 std::vector<
392 std::vector<Tensor<1, dim, typename VectorType::value_type>>>
393 vgrads(nq,
394 std::vector<Tensor<1, dim, typename VectorType::value_type>>(
395 this->n_components));
396 fe_v.get_present_fe_values().get_function_gradients(data_vector,
397 vgrads);
398
399 for (unsigned int q = 0; q < nq; ++q)
400 {
401 const unsigned int s = vgrads[q].size();
402 values[maps[i][q]].resize(s);
403 for (unsigned int l = 0; l < s; ++l)
404 values[maps[i][q]][l] = vgrads[q][l];
405 }
406 }
407 }
408
409
410
411 template <int dim, typename VectorType, int spacedim>
412 void
413 FEFieldFunction<dim, VectorType, spacedim>::gradient_list(
414 const std::vector<Point<dim>> &points,
415 std::vector<Tensor<1, dim, typename VectorType::value_type>> &values,
416 const unsigned int component) const
417 {
418 Assert(points.size() == values.size(),
419 ExcDimensionMismatch(points.size(), values.size()));
420
421 // Simply forward everything to the vector_gradient_list()
422 // function. This requires a temporary object, but everything we
423 // do here is so expensive that that really doesn't make any
424 // difference any more.
425 std::vector<std::vector<Tensor<1, dim, typename VectorType::value_type>>>
426 vector_values(
427 points.size(),
428 std::vector<Tensor<1, dim, typename VectorType::value_type>>(
429 this->n_components));
430
431 vector_gradient_list(points, vector_values);
432
433 for (unsigned int q = 0; q < points.size(); ++q)
434 values[q] = vector_values[q][component];
435 }
436
437
438
439 template <int dim, typename VectorType, int spacedim>
440 void
441 FEFieldFunction<dim, VectorType, spacedim>::vector_laplacian_list(
442 const std::vector<Point<dim>> &points,
443 std::vector<Vector<typename VectorType::value_type>> &values) const
444 {
445 Assert(points.size() == values.size(),
446 ExcDimensionMismatch(points.size(), values.size()));
447
448 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator> cells;
449 std::vector<std::vector<Point<dim>>> qpoints;
450 std::vector<std::vector<unsigned int>> maps;
451
452 const unsigned int n_cells =
453 compute_point_locations(points, cells, qpoints, maps);
454
455 // Create quadrature collection
456 hp::QCollection<dim> quadrature_collection;
457 for (unsigned int i = 0; i < n_cells; ++i)
458 {
459 // Number of quadrature points on this cell
460 unsigned int nq = qpoints[i].size();
461 // Construct a quadrature formula
462 std::vector<double> ww(nq, 1. / nq);
463
464 quadrature_collection.push_back(Quadrature<dim>(qpoints[i], ww));
465 }
466
467 // Now gather all the information we need
468 hp::MappingCollection<dim> mapping_collection(mapping);
469 hp::FEValues<dim> fe_v(mapping_collection,
470 dh->get_fe_collection(),
471 quadrature_collection,
472 update_hessians);
473 // Now gather all the information we need
474 for (unsigned int i = 0; i < n_cells; ++i)
475 {
476 AssertThrow(!cells[i]->is_artificial(),
477 VectorTools::ExcPointNotAvailableHere());
478
479 fe_v.reinit(cells[i], i, 0);
480
481 const unsigned int nq = qpoints[i].size();
482 std::vector<Vector<typename VectorType::value_type>> vector_values(
483 nq, Vector<typename VectorType::value_type>(this->n_components));
484 fe_v.get_present_fe_values().get_function_laplacians(data_vector,
485 vector_values);
486
487 for (unsigned int q = 0; q < nq; ++q)
488 values[maps[i][q]] = vector_values[q];
489 }
490 }
491
492
493
494 template <int dim, typename VectorType, int spacedim>
495 void
496 FEFieldFunction<dim, VectorType, spacedim>::laplacian_list(
497 const std::vector<Point<dim>> &points,
498 std::vector<typename VectorType::value_type> &values,
499 const unsigned int component) const
500 {
501 Assert(points.size() == values.size(),
502 ExcDimensionMismatch(points.size(), values.size()));
503
504 // Simply forward everything to the vector_gradient_list()
505 // function. This requires a temporary object, but everything we
506 // do here is so expensive that that really doesn't make any
507 // difference any more.
508 std::vector<Vector<typename VectorType::value_type>> vector_values(
509 points.size(),
510 Vector<typename VectorType::value_type>(this->n_components));
511
512 vector_laplacian_list(points, vector_values);
513
514 for (unsigned int q = 0; q < points.size(); ++q)
515 values[q] = vector_values[q](component);
516 }
517
518
519
520 template <int dim, typename VectorType, int spacedim>
521 unsigned int
522 FEFieldFunction<dim, VectorType, spacedim>::compute_point_locations(
523 const std::vector<Point<dim>> &points,
524 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator>
525 &cells,
526 std::vector<std::vector<Point<dim>>> &qpoints,
527 std::vector<std::vector<unsigned int>> &maps) const
528 {
529 // Calling the GridTools routine and preparing output
530 auto cell_qpoint_map =
531 GridTools::compute_point_locations_try_all(cache,
532 points,
533 cell_hint.get());
534 const auto &tria_cells = std::get<0>(cell_qpoint_map);
535 cells.resize(tria_cells.size());
536 unsigned int i = 0;
537 for (const auto &c : tria_cells)
538 cells[i++] = typename DoFHandler<dim, spacedim>::cell_iterator(*c, dh);
539 qpoints = std::get<1>(cell_qpoint_map);
540 maps = std::get<2>(cell_qpoint_map);
541
542 // Ensure that we found all points
543 AssertThrow(std::get<3>(cell_qpoint_map).empty(),
544 VectorTools::ExcPointNotAvailableHere());
545 return cells.size();
546 }
547
548
549 template <int dim, typename VectorType, int spacedim>
550 std::optional<Point<dim>>
551 FEFieldFunction<dim, VectorType, spacedim>::get_reference_coordinates(
552 const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
553 const Point<dim> &point) const
554 {
555 try
556 {
557 Point<dim> qp = mapping.transform_real_to_unit_cell(cell, point);
558 if (GeometryInfo<dim>::is_inside_unit_cell(qp))
559 return qp;
560 else
561 return std::optional<Point<dim>>();
562 }
563 catch (const typename Mapping<dim>::ExcTransformationFailed &)
564 {
565 // transformation failed, so
566 // assume the point is
567 // outside
568 return std::optional<Point<dim>>();
569 }
570 }
571
572 } // namespace Functions
573
574 DEAL_II_NAMESPACE_CLOSE
575
576 #endif