Debian Codegraph @

include/deal.II/base/quadrature_point_data.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2016 - 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_quadrature_point_data_h
16 #define dealii_quadrature_point_data_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/enable_observer_pointer.h>
21 #include <deal.II/base/quadrature.h>
22
23 #include <deal.II/distributed/tria.h>
24
25 #include <deal.II/fe/fe.h>
26 #include <deal.II/fe/fe_tools.h>
27
28 #include <deal.II/grid/tria.h>
29 #include <deal.II/grid/tria_accessor.h>
30
31 #include <deal.II/lac/vector.h>
32
33 #include <map>
34 #include <optional>
35 #include <type_traits>
36 #include <vector>
37
38 DEAL_II_NAMESPACE_OPEN
39
40 /**
41 * @addtogroup Quadrature
42 * @{
43 */
44
45 /**
46 * A class for storing at each cell represented by iterators of type @p CellIteratorType
47 * a vector of data @p DataType .
48 * The underlying structure and the initialize() method of this class are
49 * designed in such a way that one could use different child classes derived
50 * from the base DataType class to store data on a given cell. This implies the
51 * usage of pointers, in our case -- std::shared_ptr.
52 *
53 * The type @p DataType is arbitrary, but when using a class derived from
54 * TransferableQuadraturePointData one can use the facilities of
55 * parallel::distributed::ContinuousQuadratureDataTransfer.
56 *
57 * @note The data type stored on each cell can be different.
58 * However, within the cell this class stores a vector of objects of a single
59 * data type. For this reason, this class may not be sufficiently flexible when,
60 * for example, adopting a level-set approach to describe material behavior.
61 */
62 template <typename CellIteratorType, typename DataType>
63 class CellDataStorage : public EnableObserverPointer
64 {
65 public:
66 /**
67 * Default constructor.
68 */
69 CellDataStorage() = default;
70
71 /**
72 * Default destructor.
73 */
74 ~CellDataStorage() override = default;
75
76 /**
77 * Initialize data on the @p cell to store @p number_of_data_points_per_cell of objects of type @p T .
78 * The typename @p T is possibly another class which is derived from the
79 * base @p DataType class. In order to initialize the vector of objects
80 * we have to assume that the class @p T has a default constructor.
81 * This function has to be called on every cell where data is to be stored.
82 *
83 * After the data is initialized, it can be modified using the get_data()
84 * function.
85 *
86 * @note Subsequent calls of this function with the same @p cell will not
87 * alter the objects associated with it. In order to remove the stored data,
88 * use the erase() function.
89 *
90 * @note It is possible to use different types @p T for different cells which
91 * may reflect, for example, different constitutive models of continuum
92 * mechanics in different parts of the domain.
93 *
94 * @note The first time this method is called, it stores a ObserverPointer to the
95 * Triangulation object that owns the cell. The future invocations of this
96 * method expects the cell to be from the same stored triangulation.
97 *
98 * @pre The type @p T needs to either equal @p DataType, or be a class derived
99 * from @p DataType. @p T needs to be default constructible.
100 */
101 template <typename T = DataType>
102 void
103 initialize(const CellIteratorType &cell,
104 const unsigned int number_of_data_points_per_cell);
105
106 /**
107 * Same as above but for a range of iterators starting at @p cell_start
108 * until, but not including, @p cell_end for all locally owned cells, i.e.
109 * for which `cell->is_locally_owned()==true` .
110 */
111 template <typename T = DataType>
112 void
113 initialize(
114 const CellIteratorType &cell_start,
115 const typename std_cxx20::type_identity<CellIteratorType>::type &cell_end,
116 const unsigned int number_of_data_points_per_cell);
117
118 /**
119 * Removes data stored at the @p cell. Returns true if the data was removed.
120 * If no data is attached to the @p cell, this function will not do anything
121 * and returns false.
122 *
123 * @note This function will also check that there are no
124 * outstanding references to the data stored on this cell. That is to say,
125 * that the only references to the stored data are that made by this class.
126 */
127 bool
128 erase(const CellIteratorType &cell);
129
130 /**
131 * Clear all the data stored in this object.
132 */
133 void
134 clear();
135
136 /**
137 * Get a vector of the data located at @p cell .
138 * A possible additional typename @p T is the class to which the base class
139 * DataType could be cast. Since @p DataType is stored as shared pointers,
140 * there is minimal overhead in returning a vector by value instead of by
141 * reference.
142 * This allows flexibility if class @p T is not the same as @p DataType on a
143 * cell-by-cell basis.
144 *
145 * @pre The type @p T needs to match the class provided to initialize() .
146 *
147 * @pre @p cell must be from the same Triangulation that is used to
148 * initialize() the cell data.
149 */
150 template <typename T = DataType>
151 std::vector<std::shared_ptr<T>>
152 get_data(const CellIteratorType &cell);
153
154 /**
155 * Get a vector of constant pointers to data located at @p cell .
156 * A possible additional typename @p T is the class to which the base class
157 * DataType could be cast. Since @p DataType is stored as shared pointers,
158 * there is minimal overhead in returning a vector by value instead of by
159 * reference.
160 * This allows flexibility if class @p T is not the same as @p DataType on a
161 * cell-by-cell basis.
162 *
163 * @pre The type @p T needs to match the class provided to initialize() .
164 *
165 * @pre @p cell must be from the same Triangulation that is used to
166 * initialize() the cell data.
167 */
168 template <typename T = DataType>
169 std::vector<std::shared_ptr<const T>>
170 get_data(const CellIteratorType &cell) const;
171
172 /**
173 * Returns a std::optional indicating whether @p cell contains an
174 * associated data or not. If data is available, dereferencing the
175 * std::optional reveals a vector of pointers to the underlying data
176 * at the quadrature points.
177 * A possible additional typename @p T is the class to which the base class
178 * DataType could be cast. Since @p DataType is stored as shared pointers,
179 * there is minimal overhead in returning a vector by value instead of by
180 * reference.
181 * This allows flexibility if class @p T is not the same as @p DataType on a
182 * cell-by-cell basis.
183 *
184 * @pre The type @p T needs to match the class provided to initialize().
185 * @pre @p cell must be from the same Triangulation that is used to
186 * initialize() the cell data.
187 */
188 template <typename T = DataType>
189 std::optional<std::vector<std::shared_ptr<T>>>
190 try_get_data(const CellIteratorType &cell);
191
192 /**
193 * Returns a std::optional indicating whether @p cell contains an
194 * associated data or not. If data is available, dereferencing the
195 * std::optional reveals a vector of constant pointers to the
196 * underlying data at the quadrature points.
197 * A possible additional typename @p T is the class to which the base class
198 * DataType could be cast. Since @p DataType is stored as shared pointers,
199 * there is minimal overhead in returning a vector by value instead of by
200 * reference.
201 * This allows flexibility if class @p T is not the same as @p DataType on a
202 * cell-by-cell basis.
203 *
204 * @pre The type @p T needs to match the class provided to initialize().
205 * @pre @p cell must be from the same Triangulation that is used to
206 * initialize() the cell data.
207 */
208 template <typename T = DataType>
209 std::optional<std::vector<std::shared_ptr<const T>>>
210 try_get_data(const CellIteratorType &cell) const;
211
212 private:
213 /**
214 * Number of dimensions
215 */
216 static constexpr unsigned int dimension =
217 CellIteratorType::AccessorType::dimension;
218
219 /**
220 * Number of space dimensions
221 */
222 static constexpr unsigned int space_dimension =
223 CellIteratorType::AccessorType::space_dimension;
224
225 /**
226 * To ensure that all the cells in the CellDataStorage come from the same
227 * Triangulation, we need to store a reference to that Triangulation within
228 * the class.
229 */
230 ObserverPointer<const Triangulation<dimension, space_dimension>,
231 CellDataStorage<CellIteratorType, DataType>>
232 tria;
233
234 /**
235 * A map to store a vector of data on each cell.
236 * We need to use CellId as the key because it remains unique during
237 * adaptive refinement.
238 */
239 std::map<CellId, std::vector<std::shared_ptr<DataType>>> map;
240
241 /**
242 * @addtogroup Exceptions
243 */
244 DeclExceptionMsg(
245 ExcCellDataTypeMismatch,
246 "Cell data is being retrieved with a type which is different than the type used to initialize it");
247
248 /**
249 * @addtogroup Exceptions
250 */
251 DeclExceptionMsg(
252 ExcTriangulationMismatch,
253 "The provided cell iterator does not belong to the triangulation that corresponds to the CellDataStorage object.");
254 };
255
256
257 /**
258 * An abstract class which specifies requirements for data on
259 * a single quadrature point to be transferable during refinement or
260 * repartitioning.
261 *
262 * This class provides a framework by which derived classes representing data at
263 * quadrature points can declare how many scalar values they store, and then
264 * implement functions that pack and unpack these scalars into arrays.
265 * These arrays are used to transfer data from quadrature points of one cell to
266 * quadrature points of another cell as well as between processors
267 * upon mesh refinement and repartitioning.
268 * The transfer of quadrature point data between parent and child cells requires
269 * some kind of projection and/or interpolation.
270 * One possible implementation is via the L2 projection and prolongation
271 * matrices as implemented in
272 * parallel::distributed::ContinuousQuadratureDataTransfer class.
273 *
274 * To store and access instances of classes derived from this class, see the
275 * CellDataStorage class.
276 */
277 class TransferableQuadraturePointData
278 {
279 public:
280 /**
281 * Default constructor.
282 */
283 TransferableQuadraturePointData() = default;
284
285 /**
286 * Default virtual destructor.
287 */
288 virtual ~TransferableQuadraturePointData() = default;
289
290 /**
291 * Return the total number of values which will be
292 * packed/unpacked from the user's DataType class. Consequently it is also
293 * the size of the vectors in pack_values() and unpack_values() .
294 */
295 virtual unsigned int
296 number_of_values() const = 0;
297
298 /**
299 * A virtual function that have to be implemented in derived classes to
300 * pack all data stored in the derived class into a vector @p values .
301 * This vector will contain all scalar and/or Tensorial data local to each
302 * quadrature point.
303 *
304 * @note The function will be called with @p values of size number_of_values().
305 * The implementation may still have an assert to check that it is indeed the
306 * case.
307 */
308 virtual void
309 pack_values(std::vector<double> &values) const = 0;
310
311 /**
312 * The opposite of the above, namely
313 * unpack a vector @p values into the data stored in this class.
314 *
315 * @note The function will be called with @p values of size number_of_values().
316 * The implementation may still have an assert to check that it is indeed the
317 * case.
318 */
319 virtual void
320 unpack_values(const std::vector<double> &values) = 0;
321 };
322
323
324 #ifdef DEAL_II_WITH_P4EST
325 namespace parallel
326 {
327 namespace distributed
328 {
329 /**
330 * A class for the transfer of continuous data stored at quadrature points
331 * when performing h-adaptive refinement of
332 * parallel::distributed::Triangulation .
333 *
334 * <h3>Implementation details</h3>
335 *
336 * This class implements the transfer of the quadrature point data between
337 * cells in case of adaptive refinement using L2 projection. That also
338 * includes automatic shipping of information between different processors.
339 *
340 * To that end, the constructor of the class is provided with three main
341 * objects:
342 * scalar FiniteElement @p projection_fe, @p mass_quadrature and @p data_quadrature
343 * Quadrature rules.
344 * First, the data located at @p data_quadrature of each cell is L2-projected
345 * to the continuous space defined by a single FiniteElement @p projection_fe .
346 * This is achieved using
347 * FETools::compute_projection_from_quadrature_points_matrix(). In doing so
348 * the @ref GlossMassMatrix "mass matrix" of this element is required, which will be calculated
349 * with the @p mass_quadrature rule . Should the cell now belong to another processor,
350 * the data is then sent to this processor. The class makes use of a feature
351 * of p4est (and parallel::distributed::Triangulation) that allows one to
352 * attach information to cells during mesh refinement and rebalancing. On
353 * receiving information on the target cell, the data is projected back to
354 * the quadrature points using the matrix calculated by
355 * FETools::compute_interpolation_to_quadrature_points_matrix() .
356 * In the case that local refinement is performed, this class first
357 * project local DoF values of the parent element to each child.
358 *
359 *
360 * This class is templated by @p DataType type, however the user's @p DataType class
361 * has to be derived from the TransferableQuadraturePointData class. In
362 * practice that amounts to implementing the following three functions shown
363 * below for a quadrature point data with 2 scalars:
364 * @code
365 * class MyQData : public TransferableQuadraturePointData
366 * {
367 * public:
368 * double elasticity_parameter_lambda;
369 * double elasticity_parameter_mu;
370 * unsigned int number_of_values() const
371 * {
372 * return 2;
373 * }
374 *
375 * // a function to pack scalars into a vector
376 * void pack_values(std::vector<double> &values) const
377 * {
378 * Assert (values.size()==2, ExcInternalError());
379 * values[0] = elasticity_parameter_lambda;
380 * values[1] = elasticity_parameter_mu;
381 * }
382 *
383 * void unpack_values(const std::vector<double> &values)
384 * {
385 * Assert (values.size() ==2, ExcInternalError());
386 * elasticity_parameter_lambda = values[0];
387 * elasticity_parameter_mu = values[1];
388 * }
389 * };
390 * @endcode
391 * Note that the order of packing and unpacking has to be the same.
392 *
393 * This class can then be use with CellDataStorage in the following way:
394 * @code
395 * CellDataStorage<typename Triangulation<dim,dim>::cell_iterator,MyQData>
396 * data_storage;
397 * parallel::distributed::ContinuousQuadratureDataTransfer<dim,MyQData>
398 * data_transfer(FE_Q<dim>(2),QGauss<dim>(3),QGauss<dim>(4));
399 * //...populate data for all active cells in data_storage
400 * //...mark cells for refinement...
401 * data_transfer.prepare_for_coarsening_and_refinement(triangulation,data_storage);
402 * triangulation.execute_coarsening_and_refinement();
403 * //...initialize quadrature point data on new cells by calling
404 * // CellDataStorage::initialize()
405 * data_transfer.interpolate();
406 * @endcode
407 * This approach can be extended to quadrature point data with Tensor
408 * objects of arbitrary order, although with a little bit more work in
409 * packing and unpacking of data inside MyQData class.
410 *
411 * @note Currently coarsening is not supported.
412 *
413 * @note The functionality provided by this class can alternatively be achieved
414 * using parallel::distributed::SolutionTransfer. However, that would
415 * require the following steps: (i) create an auxiliary DoFHandler with a
416 * (discontinuous Galerkin) FiniteElement which has enough components to
417 * represent all data stored at the quadrature points; (ii) project the data
418 * to the FiniteElement space and thereby store results in global vectors;
419 * (iii) use parallel::distributed::SolutionTransfer to project FE vectors
420 * to the new mesh; and (iv) finally project the data back to the quadrature
421 * points on the new mesh via FEValues class. The
422 * ContinuousQuadratureDataTransfer class aims at simplifying the whole
423 * process by only requiring that the quadrature point data class is derived
424 * from the TransferableQuadraturePointData. Everything else will be done
425 * automatically.
426 *
427 * @note This class is not well suited to situations where the values stored
428 * at quadrature points represent samples from a discontinuous field. An
429 * example for such a situation would be where the data stored at the
430 * quadrature points represents the elastic or plastic state of a material,
431 * i.e., a property that varies discontinuously within a solid. In such
432 * cases, trying to transfer data from the quadrature points to a finite
433 * element field that is continuous (at least within one cell) will likely
434 * yield over and undershoots that, once evaluated at a different set of
435 * quadrature points (on child or parent cells) results in values that will
436 * not make much sense.
437 */
438 template <int dim, typename DataType>
439 class ContinuousQuadratureDataTransfer
440 {
441 public:
442 static_assert(
443 std::is_base_of_v<TransferableQuadraturePointData, DataType>,
444 "User's DataType class should be derived from TransferableQuadraturePointData");
445
446 /**
447 * An alias for a cell.
448 */
449 using CellIteratorType =
450 typename parallel::distributed::Triangulation<dim>::cell_iterator;
451
452 /**
453 * Constructor which takes the FiniteElement @p projection_fe , the quadrature
454 * rule @p mass_quadrature used to integrate its local @ref GlossMassMatrix "mass matrix" and
455 * finally the quadrature rule @p data_quadrature which is used to store @p DataType.
456 *
457 * @pre @p projection_fe has to be scalar-valued.
458 *
459 * @note Since this class does projection on cell-by-cell basis,
460 * @p projection_fe is only required to be continuous within the cell.
461 */
462 ContinuousQuadratureDataTransfer(const FiniteElement<dim> &projection_fe,
463 const Quadrature<dim> &mass_quadrature,
464 const Quadrature<dim> &data_quadrature);
465
466 /**
467 * Prepare for coarsening and refinement of a triangulation @p tria .
468 * @p data_storage represents the cell data which should be transferred
469 * and it should be initialized for each locally owned active cell.
470 *
471 * @note Although CellDataStorage class allows storing on different cells
472 * different objects derived from the base class, here we assume that
473 * @p data_storage contains objects of the same type, more specifically
474 * they pack/unpack the same data.
475 */
476 void
477 prepare_for_coarsening_and_refinement(
478 parallel::distributed::Triangulation<dim> &tria,
479 CellDataStorage<CellIteratorType, DataType> &data_storage);
480
481 /**
482 * Interpolate the data previously stored in this object before the mesh
483 * was refined or coarsened onto the quadrature points of the currently
484 * active set of cells.
485 *
486 * @note Before calling this function the user is expected to populate the
487 * data stored in the @p data_storage object provided to prepare_for_coarsening_and_refinement()
488 * at new cells using CellDataStorage::initialize(). If that is not the
489 * case, an exception will be thrown in debug mode.
490 */
491 void
492 interpolate();
493
494 private:
495 /**
496 * A callback function used to pack the data on the current mesh into
497 * objects that can later be retrieved after refinement, coarsening and
498 * repartitioning.
499 */
500 std::vector<char>
501 pack_function(
502 const typename parallel::distributed::Triangulation<dim>::cell_iterator
503 &cell,
504 const CellStatus status);
505
506 /**
507 * A callback function used to unpack the data on the current mesh that
508 * has been packed up previously on the mesh before refinement,
509 * coarsening and repartitioning.
510 */
511 void
512 unpack_function(
513 const typename parallel::distributed::Triangulation<dim>::cell_iterator
514 &cell,
515 const CellStatus status,
516 const boost::iterator_range<std::vector<char>::const_iterator>
517 &data_range);
518
519 /**
520 * FiniteElement used to project data from and to quadrature points.
521 */
522 const std::unique_ptr<const FiniteElement<dim>> projection_fe;
523
524 /**
525 * The size of the data that will be sent, which depends on the DataType
526 * class.
527 */
528 std::size_t data_size_in_bytes;
529
530 /**
531 * Number of quadrature points at which DataType is stored.
532 */
533 const unsigned int n_q_points;
534
535 /**
536 * Projection matrix from the quadrature points to local DoFs for a single
537 * scalar.
538 */
539 FullMatrix<double> project_to_fe_matrix;
540
541 /**
542 * Projection matrix from the local DoFs to quadrature points for a single
543 * scalar.
544 */
545 FullMatrix<double> project_to_qp_matrix;
546
547 /**
548 * Auxiliary matrix which represents projection of each internal value
549 * stored
550 * at the quadrature point (second index) to the local DoFs of the @p projection_fe
551 * (first index).
552 */
553 FullMatrix<double> matrix_dofs;
554
555 /**
556 * Projection of @p matrix_dofs to each child cell in case of adaptive refinement.
557 */
558 FullMatrix<double> matrix_dofs_child;
559
560 /**
561 * Auxiliary matrix which represents data (second index) stored at each
562 * quadrature point (first index).
563 */
564 FullMatrix<double> matrix_quadrature;
565
566 /**
567 * The handle that the parallel::distributed::Triangulation has assigned
568 * to this object while registering the pack_callback function.
569 */
570 unsigned int handle;
571
572 /**
573 * A pointer to the CellDataStorage class whose data will be transferred.
574 */
575 CellDataStorage<CellIteratorType, DataType> *data_storage;
576
577 /**
578 * A pointer to the distributed triangulation to which cell data is
579 * attached.
580 */
581 parallel::distributed::Triangulation<dim> *triangulation;
582 };
583
584 } // namespace distributed
585
586 } // namespace parallel
587
588 #endif
589
590 /** @} */
591
592 #ifndef DOXYGEN
593
594 // ------------------- inline and template functions ----------------
595
596 //--------------------------------------------------------------------
597 // CellDataStorage
598 //--------------------------------------------------------------------
599
600 template <typename CellIteratorType, typename DataType>
601 template <typename T>
602 inline void
603 CellDataStorage<CellIteratorType, DataType>::initialize(
604 const CellIteratorType &cell,
605 const unsigned int n_q_points)
606 {
607 static_assert(std::is_base_of_v<DataType, T>,
608 "User's T class should be derived from user's DataType class");
609 // The first time this method is called, it has to initialize the reference
610 // to the triangulation object
611 if (!tria)
612 tria = &cell->get_triangulation();
613 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
614
615 const auto key = cell->id();
616 if (map.find(key) == map.end())
617 {
618 map[key] = std::vector<std::shared_ptr<DataType>>(n_q_points);
619 // we need to initialize one-by-one as the std::vector<>(q, T())
620 // will end with a single same T object stored in each element of the
621 // vector:
622 const auto it = map.find(key);
623 for (unsigned int q = 0; q < n_q_points; ++q)
624 it->second[q] = std::make_shared<T>();
625 }
626 }
627
628
629
630 template <typename CellIteratorType, typename DataType>
631 template <typename T>
632 inline void
633 CellDataStorage<CellIteratorType, DataType>::initialize(
634 const CellIteratorType &cell_start,
635 const typename std_cxx20::type_identity<CellIteratorType>::type &cell_end,
636 const unsigned int number)
637 {
638 for (CellIteratorType it = cell_start; it != cell_end; ++it)
639 if (it->is_locally_owned())
640 initialize<T>(it, number);
641 }
642
643
644
645 template <typename CellIteratorType, typename DataType>
646 inline bool
647 CellDataStorage<CellIteratorType, DataType>::erase(const CellIteratorType &cell)
648 {
649 const auto key = cell->id();
650 const auto it = map.find(key);
651 if (it == map.end())
652 return false;
653 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
654 for (unsigned int i = 0; i < it->second.size(); ++i)
655 {
656 Assert(
657 it->second[i].use_count() == 1,
658 ExcMessage(
659 "Can not erase the cell data multiple objects reference its data."));
660 }
661
662 return (map.erase(key) == 1);
663 }
664
665
666
667 template <typename CellIteratorType, typename DataType>
668 inline void
669 CellDataStorage<CellIteratorType, DataType>::clear()
670 {
671 // Do not call
672 // map.clear();
673 // as we want to be sure no one uses the stored objects. Loop manually:
674 auto it = map.begin();
675 while (it != map.end())
676 {
677 // loop over all objects and see if no one is using them
678 for (unsigned int i = 0; i < it->second.size(); ++i)
679 {
680 Assert(
681 it->second[i].use_count() == 1,
682 ExcMessage(
683 "Can not erase the cell data, multiple objects reference it."));
684 }
685 it = map.erase(it);
686 }
687 }
688
689
690
691 template <typename CellIteratorType, typename DataType>
692 template <typename T>
693 inline std::vector<std::shared_ptr<T>>
694 CellDataStorage<CellIteratorType, DataType>::get_data(
695 const CellIteratorType &cell)
696 {
697 static_assert(std::is_base_of_v<DataType, T>,
698 "User's T class should be derived from user's DataType class");
699 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
700
701 const auto it = map.find(cell->id());
702 Assert(it != map.end(), ExcMessage("Could not find data for the cell"));
703
704 // It would be nice to have a specialized version of this function for
705 // T==DataType. However explicit (i.e full) specialization of a member
706 // template is only allowed when the enclosing class is also explicitly (i.e
707 // fully) specialized. Thus, stick with copying of shared pointers even when
708 // the T==DataType:
709 std::vector<std::shared_ptr<T>> res(it->second.size());
710 for (unsigned int q = 0; q < res.size(); ++q)
711 {
712 res[q] = std::dynamic_pointer_cast<T>(it->second[q]);
713 Assert(res[q], ExcCellDataTypeMismatch());
714 }
715 return res;
716 }
717
718
719
720 template <typename CellIteratorType, typename DataType>
721 template <typename T>
722 inline std::vector<std::shared_ptr<const T>>
723 CellDataStorage<CellIteratorType, DataType>::get_data(
724 const CellIteratorType &cell) const
725 {
726 static_assert(std::is_base_of_v<DataType, T>,
727 "User's T class should be derived from user's DataType class");
728 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
729
730 const auto it = map.find(cell->id());
731 Assert(it != map.end(), ExcMessage("Could not find QP data for the cell"));
732
733 // Cast base class to the desired class. This has to be done irrespectively of
734 // T==DataType as we need to return shared_ptr<const T> to make sure the user
735 // does not modify the content of QP objects
736 std::vector<std::shared_ptr<const T>> res(it->second.size());
737 for (unsigned int q = 0; q < res.size(); ++q)
738 {
739 res[q] = std::dynamic_pointer_cast<const T>(it->second[q]);
740 Assert(res[q], ExcCellDataTypeMismatch());
741 }
742 return res;
743 }
744
745 template <typename CellIteratorType, typename DataType>
746 template <typename T>
747 inline std::optional<std::vector<std::shared_ptr<T>>>
748 CellDataStorage<CellIteratorType, DataType>::try_get_data(
749 const CellIteratorType &cell)
750 {
751 static_assert(std::is_base_of_v<DataType, T>,
752 "User's T class should be derived from user's DataType class");
753 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
754
755 const auto it = map.find(cell->id());
756 if (it != map.end())
757 {
758 // Cast base class to the desired class. This has to be done
759 // irrespectively of T==DataType as we need to return
760 // shared_ptr<const T> to make sure the user
761 // does not modify the content of QP objects
762 std::vector<std::shared_ptr<T>> result(it->second.size());
763 for (unsigned int q = 0; q < result.size(); ++q)
764 {
765 result[q] = std::dynamic_pointer_cast<T>(it->second[q]);
766 Assert(result[q], ExcCellDataTypeMismatch());
767 }
768 return {result};
769 }
770 else
771 {
772 return {};
773 }
774 }
775
776 template <typename CellIteratorType, typename DataType>
777 template <typename T>
778 inline std::optional<std::vector<std::shared_ptr<const T>>>
779 CellDataStorage<CellIteratorType, DataType>::try_get_data(
780 const CellIteratorType &cell) const
781 {
782 static_assert(std::is_base_of_v<DataType, T>,
783 "User's T class should be derived from user's DataType class");
784 Assert(&cell->get_triangulation() == tria, ExcTriangulationMismatch());
785
786 const auto it = map.find(cell->id());
787 if (it != map.end())
788 {
789 // Cast base class to the desired class. This has to be done
790 // irrespectively of T==DataType as we need to return
791 // shared_ptr<const T> to make sure the user
792 // does not modify the content of QP objects
793 std::vector<std::shared_ptr<const T>> result(it->second.size());
794 for (unsigned int q = 0; q < result.size(); ++q)
795 {
796 result[q] = std::dynamic_pointer_cast<const T>(it->second[q]);
797 Assert(result[q], ExcCellDataTypeMismatch());
798 }
799 return {result};
800 }
801 else
802 {
803 return {};
804 }
805 }
806
807 //--------------------------------------------------------------------
808 // ContinuousQuadratureDataTransfer
809 //--------------------------------------------------------------------
810
811
812 /*
813 * Pack cell data of type @p DataType stored using @p data_storage in @p cell
814 * at each quadrature point to @p matrix_data. Here @p matrix_data is a matrix
815 * whose first index corresponds to different quadrature points on the cell
816 * whereas the second index represents different values stored at each
817 * quadrature point in the DataType class.
818 */
819 template <typename CellIteratorType, typename DataType>
820 inline void
821 pack_cell_data(const CellIteratorType &cell,
822 const CellDataStorage<CellIteratorType, DataType> *data_storage,
823 FullMatrix<double> &matrix_data)
824 {
825 static_assert(std::is_base_of_v<TransferableQuadraturePointData, DataType>,
826 "User's DataType class should be derived from QPData");
827
828 if (const auto qpd = data_storage->try_get_data(cell))
829 {
830 const unsigned int m = qpd->size();
831 Assert(m > 0, ExcInternalError());
832 const unsigned int n = (*qpd)[0]->number_of_values();
833 matrix_data.reinit(m, n);
834
835 std::vector<double> single_qp_data(n);
836 for (unsigned int q = 0; q < m; ++q)
837 {
838 (*qpd)[q]->pack_values(single_qp_data);
839 AssertDimension(single_qp_data.size(), n);
840
841 for (unsigned int i = 0; i < n; ++i)
842 matrix_data(q, i) = single_qp_data[i];
843 }
844 }
845 else
846 {
847 matrix_data.reinit({0, 0});
848 }
849 }
850
851
852
853 /*
854 * the opposite of the pack function above.
855 */
856 template <typename CellIteratorType, typename DataType>
857 inline void
858 unpack_to_cell_data(const CellIteratorType &cell,
859 const FullMatrix<double> &values_at_qp,
860 CellDataStorage<CellIteratorType, DataType> *data_storage)
861 {
862 static_assert(std::is_base_of_v<TransferableQuadraturePointData, DataType>,
863 "User's DataType class should be derived from QPData");
864
865 if (const auto qpd = data_storage->try_get_data(cell))
866 {
867 const unsigned int n = values_at_qp.n();
868 AssertDimension((*qpd)[0]->number_of_values(), n);
869
870 std::vector<double> single_qp_data(n);
871 AssertDimension(qpd->size(), values_at_qp.m());
872
873 for (unsigned int q = 0; q < qpd->size(); ++q)
874 {
875 for (unsigned int i = 0; i < n; ++i)
876 single_qp_data[i] = values_at_qp(q, i);
877 (*qpd)[q]->unpack_values(single_qp_data);
878 }
879 }
880 }
881
882
883 # ifdef DEAL_II_WITH_P4EST
884
885 namespace parallel
886 {
887 namespace distributed
888 {
889 template <int dim, typename DataType>
890 inline ContinuousQuadratureDataTransfer<dim, DataType>::
891 ContinuousQuadratureDataTransfer(const FiniteElement<dim> &projection_fe_,
892 const Quadrature<dim> &lhs_quadrature,
893 const Quadrature<dim> &rhs_quadrature)
894 : projection_fe(
895 std::unique_ptr<const FiniteElement<dim>>(projection_fe_.clone()))
896 , data_size_in_bytes(0)
897 , n_q_points(rhs_quadrature.size())
898 , project_to_fe_matrix(projection_fe->n_dofs_per_cell(), n_q_points)
899 , project_to_qp_matrix(n_q_points, projection_fe->n_dofs_per_cell())
900 , handle(numbers::invalid_unsigned_int)
901 , data_storage(nullptr)
902 , triangulation(nullptr)
903 {
904 Assert(
905 projection_fe->n_components() == 1,
906 ExcMessage(
907 "ContinuousQuadratureDataTransfer requires scalar FiniteElement"));
908
909 FETools::compute_projection_from_quadrature_points_matrix(
910 *projection_fe.get(),
911 lhs_quadrature,
912 rhs_quadrature,
913 project_to_fe_matrix);
914
915 FETools::compute_interpolation_to_quadrature_points_matrix(
916 *projection_fe.get(), rhs_quadrature, project_to_qp_matrix);
917 }
918
919
920
921 template <int dim, typename DataType>
922 inline void
923 ContinuousQuadratureDataTransfer<dim, DataType>::
924 prepare_for_coarsening_and_refinement(
925 parallel::distributed::Triangulation<dim> &tr_,
926 CellDataStorage<CellIteratorType, DataType> &data_storage_)
927 {
928 Assert(data_storage == nullptr,
929 ExcMessage("This function can be called only once"));
930 triangulation = &tr_;
931 data_storage = &data_storage_;
932
933 handle = triangulation->register_data_attach(
934 [this](const typename parallel::distributed::Triangulation<
935 dim>::cell_iterator &cell,
936 const CellStatus status) {
937 return this->pack_function(cell, status);
938 },
939 /*returns_variable_size_data=*/true);
940 }
941
942
943
944 template <int dim, typename DataType>
945 inline void
946 ContinuousQuadratureDataTransfer<dim, DataType>::interpolate()
947 {
948 triangulation->notify_ready_to_unpack(
949 handle,
950 [this](const typename parallel::distributed::Triangulation<
951 dim>::cell_iterator &cell,
952 const CellStatus status,
953 const boost::iterator_range<std::vector<char>::const_iterator>
954 &data_range) {
955 this->unpack_function(cell, status, data_range);
956 });
957
958 // invalidate the pointers
959 data_storage = nullptr;
960 triangulation = nullptr;
961 }
962
963
964
965 template <int dim, typename DataType>
966 inline std::vector<char>
967 ContinuousQuadratureDataTransfer<dim, DataType>::pack_function(
968 const typename parallel::distributed::Triangulation<dim>::cell_iterator
969 &cell,
970 const CellStatus /*status*/)
971 {
972 pack_cell_data(cell, data_storage, matrix_quadrature);
973
974 // project to FE
975 const unsigned int number_of_values = matrix_quadrature.n();
976 matrix_dofs.reinit(project_to_fe_matrix.m(), number_of_values);
977 if (number_of_values > 0)
978 project_to_fe_matrix.mmult(matrix_dofs, matrix_quadrature);
979
980 return Utilities::pack(matrix_dofs, /*allow_compression=*/false);
981 }
982
983
984
985 template <int dim, typename DataType>
986 inline void
987 ContinuousQuadratureDataTransfer<dim, DataType>::unpack_function(
988 const typename parallel::distributed::Triangulation<dim>::cell_iterator
989 &cell,
990 const CellStatus status,
991 const boost::iterator_range<std::vector<char>::const_iterator>
992 &data_range)
993 {
994 Assert((status != CellStatus::children_will_be_coarsened),
995 ExcNotImplemented());
996 (void)status;
997
998 matrix_dofs =
999 Utilities::unpack<FullMatrix<double>>(data_range.begin(),
1000 data_range.end(),
1001 /*allow_compression=*/false);
1002 const unsigned int number_of_values = matrix_dofs.n();
1003 if (number_of_values == 0)
1004 return;
1005
1006 matrix_quadrature.reinit(n_q_points, number_of_values);
1007
1008 if (cell->has_children())
1009 {
1010 // we need to first use prolongation matrix to get dofvalues on child
1011 // cells based on dofvalues stored in the parent's data_store
1012 matrix_dofs_child.reinit(projection_fe->n_dofs_per_cell(),
1013 number_of_values);
1014 for (unsigned int child = 0; child < cell->n_children(); ++child)
1015 if (cell->child(child)->is_locally_owned())
1016 {
1017 projection_fe
1018 ->get_prolongation_matrix(child, cell->refinement_case())
1019 .mmult(matrix_dofs_child, matrix_dofs);
1020
1021 // now we do the usual business of evaluating FE on quadrature
1022 // points:
1023 project_to_qp_matrix.mmult(matrix_quadrature,
1024 matrix_dofs_child);
1025
1026 // finally, put back into the map:
1027 unpack_to_cell_data(cell->child(child),
1028 matrix_quadrature,
1029 data_storage);
1030 }
1031 }
1032 else
1033 {
1034 // if there are no children, evaluate FE field at
1035 // rhs_quadrature points.
1036 project_to_qp_matrix.mmult(matrix_quadrature, matrix_dofs);
1037
1038 // finally, put back into the map:
1039 unpack_to_cell_data(cell, matrix_quadrature, data_storage);
1040 }
1041 }
1042
1043 } // namespace distributed
1044
1045 } // namespace parallel
1046
1047 # endif // DEAL_II_WITH_P4EST
1048
1049 #endif // DOXYGEN
1050 DEAL_II_NAMESPACE_CLOSE
1051
1052 #endif