Debian Codegraph @

include/deal.II/hp/mapping_collection.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2005 - 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_mapping_collection_h
16 #define dealii_mapping_collection_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/enable_observer_pointer.h>
21
22 #include <deal.II/fe/fe.h>
23 #include <deal.II/fe/mapping.h>
24 #include <deal.II/fe/mapping_q1.h>
25
26 #include <deal.II/hp/collection.h>
27
28 #include <memory>
29 #include <vector>
30
31 DEAL_II_NAMESPACE_OPEN
32
33 namespace hp
34 {
35 /**
36 * This class implements a collection of mapping objects in the same way as
37 * the hp::FECollection implements a collection of finite element classes.
38 *
39 * It implements the concepts stated in the
40 * @ref hpcollection
41 * topic described in the doxygen documentation.
42 *
43 * Although it is recommended to supply an appropriate mapping for each
44 * finite element kind used in a hp-computation, the MappingCollection class
45 * implements a conversion constructor from a single mapping. Therefore it
46 * is possible to offer only a single mapping to the hp::FEValues class
47 * instead of a hp::MappingCollection. This is for the convenience of the
48 * user, as many simple geometries do not require different mappings along
49 * the boundary to achieve optimal convergence rates. Hence providing a
50 * single mapping object will usually suffice. See the hp::FEValues class
51 * for the rules which mapping will be selected for a given cell.
52 *
53 * @ingroup hp hpcollection
54 */
55 template <int dim, int spacedim = dim>
56 class MappingCollection : public Collection<Mapping<dim, spacedim>>
57 {
58 public:
59 /**
60 * Default constructor. Leads to an empty collection that can later be
61 * filled using push_back().
62 */
63 MappingCollection() = default;
64
65 /**
66 * Conversion constructor. This constructor creates a MappingCollection
67 * from a single mapping. More mappings can be added with push_back(), if
68 * desired, though it would probably be clearer to add all mappings the
69 * same way.
70 */
71 explicit MappingCollection(const Mapping<dim, spacedim> &mapping);
72
73 /**
74 * Constructor. This constructor creates a MappingCollection from one or
75 * more mapping objects passed to the constructor. For this
76 * call to be valid, all arguments need to be of types derived
77 * from class Mapping<dim,spacedim>.
78 */
79 template <class... MappingTypes>
80 explicit MappingCollection(const MappingTypes &...mappings);
81
82 /**
83 * Copy constructor.
84 */
85 MappingCollection(
86 const MappingCollection<dim, spacedim> &mapping_collection);
87
88 /**
89 * Move constructor.
90 *
91 * @note The implementation of standard datatypes may change with different
92 * libraries, so their move members may or may not be flagged non-throwing.
93 * We need to explicitly set the noexcept specifier according to its
94 * member variables to still get the performance benefits (and to satisfy
95 * clang-tidy).
96 */
97 MappingCollection(MappingCollection<dim, spacedim> &&) noexcept(
98 std::is_nothrow_move_constructible_v<
99 std::vector<std::shared_ptr<const Mapping<dim, spacedim>>>>
100 &&std::is_nothrow_move_constructible_v<std::function<
101 unsigned int(const typename hp::MappingCollection<dim, spacedim> &,
102 const unsigned int)>>) = default;
103
104 /**
105 * Move assignment operator.
106 */
107 MappingCollection<dim, spacedim> &
108 operator=(MappingCollection<dim, spacedim> &&) = default; // NOLINT
109
110 /**
111 * Add a new mapping to the MappingCollection. Generally, you will
112 * want to use the same order for mappings as for the elements of
113 * the hp::FECollection object you use. However, the same
114 * considerations as discussed with the hp::QCollection::push_back()
115 * function also apply in the current context.
116 *
117 * This class creates a copy of the given mapping object, i.e., you can
118 * do things like <tt>push_back(MappingQ<dim>(3));</tt>. The internal copy
119 * is later destroyed by this object upon destruction of the entire
120 * collection.
121 */
122 void
123 push_back(const Mapping<dim, spacedim> &new_mapping);
124 };
125
126
127 /**
128 * Many places in the library by default use (bi-,tri-)linear mappings
129 * unless users explicitly provide a different mapping to use. In these
130 * cases, the called function has to create a $Q_1$ mapping object, i.e., an
131 * object of kind MappingQ(1). This is costly. It would also be
132 * costly to create such objects as static objects in the affected
133 * functions, because static objects are never destroyed throughout the
134 * lifetime of a program, even though they only have to be created once the
135 * first time code runs through a particular function.
136 *
137 * In order to avoid creation of (static or dynamic) $Q_1$ mapping objects
138 * in these contexts throughout the library, this class defines a static
139 * collection of mappings with a single $Q_1$ mapping object. This
140 * collection can then be used in all of those places where such a
141 * collection is needed.
142 */
143 template <int dim, int spacedim = dim>
144 struct StaticMappingQ1
145 {
146 public:
147 /**
148 * The publicly available static $Q_1$ mapping collection object.
149 */
150 static MappingCollection<dim, spacedim> mapping_collection;
151 };
152
153
154
155 /* --------------- inline functions ------------------- */
156
157 template <int dim, int spacedim>
158 template <class... MappingTypes>
159 MappingCollection<dim, spacedim>::MappingCollection(
160 const MappingTypes &...mappings)
161 {
162 static_assert(
163 is_base_of_all<Mapping<dim, spacedim>, MappingTypes...>::value,
164 "Not all of the input arguments of this function "
165 "are derived from Mapping<dim, spacedim>!");
166
167 // loop over all of the given arguments and add the mappings to
168 // this collection. Inlining the definition of mapping_pointers causes
169 // internal compiler errors on GCC 7.1.1 so we define it separately:
170 const auto mapping_pointers = {
171 (static_cast<const Mapping<dim, spacedim> *>(&mappings))...};
172 for (const auto p : mapping_pointers)
173 push_back(*p);
174 }
175
176
177
178 template <int dim, int spacedim>
179 MappingCollection<dim, spacedim>
180 StaticMappingQ1<dim, spacedim>::mapping_collection =
181 MappingCollection<dim, spacedim>(MappingQ1<dim, spacedim>{});
182
183
184 #ifndef DOXYGEN
185 // Declare the existence of explicit instantiations of the class
186 // above. This is not strictly necessary, but tells the compiler to
187 // avoid instantiating templates that we know are instantiated in
188 // .cc files and so can be referenced without implicit
189 // instantiations. In the current case, this also avoids certain
190 // warnings issues by clang and newer (LLVM-based) Intel compilers.
191 //
192 // Unfortunately, this does not seem to work when building modules
193 // because the compiler (well, Clang at least) then just doesn't
194 // instantiate these classes at all, even though their members are
195 // defined and explicitly instantiated in a .cc file.
196 # ifndef DEAL_II_BUILDING_CXX20_MODULE
197 extern template struct StaticMappingQ1<1, 1>;
198 extern template struct StaticMappingQ1<1, 2>;
199 extern template struct StaticMappingQ1<1, 3>;
200 extern template struct StaticMappingQ1<2, 2>;
201 extern template struct StaticMappingQ1<2, 3>;
202 extern template struct StaticMappingQ1<3, 3>;
203
204 # ifndef _MSC_VER
205 extern template MappingCollection<1, 1>
206 StaticMappingQ1<1, 1>::mapping_collection;
207 extern template MappingCollection<1, 2>
208 StaticMappingQ1<1, 2>::mapping_collection;
209 extern template MappingCollection<1, 3>
210 StaticMappingQ1<1, 3>::mapping_collection;
211 extern template MappingCollection<2, 2>
212 StaticMappingQ1<2, 2>::mapping_collection;
213 extern template MappingCollection<2, 3>
214 StaticMappingQ1<2, 3>::mapping_collection;
215 extern template MappingCollection<3, 3>
216 StaticMappingQ1<3, 3>::mapping_collection;
217 # endif
218 # endif
219 #endif
220
221 } // namespace hp
222
223
224 DEAL_II_NAMESPACE_CLOSE
225
226 #endif