Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
quadraturerulecache.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef QUADRATURERULECACHE_HH
5#define QUADRATURERULECACHE_HH
6
7#include <mutex>
8#include <shared_mutex>
9#include <map>
10#include <algorithm>
11
12#include <dune/geometry/type.hh>
13
15
19
21
22
23
40{
41
42 template<typename FE>
43 static constexpr bool isLocalFiniteElement = requires(const FE& fe)
44 {
45 fe.type();
46 fe.localBasis().order();
47 };
48
49 public:
50
54 gt_(Dune::GeometryTypes::none(0)),
55 order_(0),
56 refinement_(0),
57 lumping_(false)
58 {}
59
62 template<class LocalFiniteElement>
63 requires isLocalFiniteElement<LocalFiniteElement>
64 QuadratureRuleKey(const LocalFiniteElement& fe)
65 {
66 gt_ = fe.type();
67 order_ = fe.localBasis().order();
69 lumping_ = false;
70 }
71
78 QuadratureRuleKey(const Dune::GeometryType& gt, const int order, const int refinement=0, bool lumping=false) :
79 gt_(gt),
83 {}
84
91 QuadratureRuleKey(const int dim, const int order, const int refinement=0, bool lumping=false) :
92 gt_(Dune::GeometryTypes::none(dim)),
96 {}
97
103 bool operator<(const QuadratureRuleKey& other) const
104 {
105 if (gt_ < other.gt_)
106 return true;
107 if (gt_ == other.gt_)
108 {
109 if (order_ < other.order_)
110 return true;
111 if (order_ == other.order_)
112 {
113 if (refinement_ < other.refinement_)
114 return true;
115 if (refinement_ == other.refinement_)
116 return lumping_ < other.lumping_;
117 }
118 }
119 return false;
120 }
121
123 {
124 return gt_;
125 }
126
128 {
129 gt_ = gt;
130 }
131
132 int order() const
133 {
134 return order_;
135 }
136
137 void setOrder(int order)
138 {
139 order_ = order;
140 }
141
142 int refinement() const
143 {
144 return refinement_;
145 }
146
148 {
150 }
151
152 bool lumping() const
153 {
154 return lumping_;
155 }
156
158 {
160 }
161
162
163
165 {
166 int derivativeOrder = gt_.isSimplex() ? std::max(0,order_-1) : order_;
167 return QuadratureRuleKey(gt_, derivativeOrder, refinement_, lumping_);
168 }
169
171 {
172 Dune::GeometryType resultGT = gt_.isNone() ? other.gt_ : gt_;
173 return QuadratureRuleKey(resultGT, std::max(order_, other.order_), std::max(refinement_, other.refinement_), (lumping_ or other.lumping_));
174 }
175
177 {
178 Dune::GeometryType resultGT = gt_.isNone() ? other.gt_ : gt_;
179 return QuadratureRuleKey(resultGT, order_ + other.order_, std::max(refinement_, other.refinement_), (lumping_ or other.lumping_));
180 }
181
183 {
185 }
186
187 protected:
188
193};
194
195
196
197
198template <class coord_type, int dim>
200{
202 static ContainerType quadRules;
203 static std::shared_mutex mut;
204
205 public:
206 static const Dune::QuadratureRule<coord_type,dim>& rule(const Dune::GeometryType& gt, const int order, int refinement)
207 {
208 return rule(QuadratureRuleKey(gt, order, refinement));
209 }
210
212 {
213 const Dune::GeometryType& gt = index.geometryType();
214 const int order = index.order();
215 const int refinement = index.refinement();
216 const bool lumping = index.lumping();
217
218 // Non-refined and non-lumped rules can directly be obtained from Dune::QuadratureRules
219 if ((not lumping) and (refinement==0))
220 return Dune::template QuadratureRules<coord_type, dim>::rule(gt, order);
221
222 // For other rules we first search if they already exist in the cache.
223 // Since concurrent read-access is fine, we can use a shared lock that
224 // can be obtained multiple times in parallel.
225 {
226 auto lock = std::shared_lock(mut);
227 auto it = quadRules.find(index);
228 if (it != quadRules.end())
229 return it->second;
230 }
231
232 // If the rule was not found in the cache, we have to create it.
233 // This requires an exclusive lock.
234 auto lock = std::scoped_lock(mut);
235 auto it = quadRules.find(index);
236
237 // First check if the rule was meanwhile (since the search above) created.
238 if (it != quadRules.end())
239 return it->second;
240
241 // If it was not found, we have to create it. This is safe due to the exclusive lock.
242 if (lumping)
243 it = quadRules.emplace(index, LumpingQuadratureRule<coord_type,dim>(gt)).first;
244 else if ((refinement==1) and (gt.isSimplex()))
247 else if ((refinement>1) and (gt.isSimplex()))
249 Dune::QuadratureRules<coord_type, dim>::rule(gt, order), refinement)).first;
250 else
251 DUNE_THROW(Dune::NotImplemented, "Quadrature rule for given QuadratureRuleKey(" << gt << "," << order << "," << refinement << " is not implemented!");
252 return it->second;
253 }
254};
255// define static template members
257template <class coord_type, int dim> std::shared_mutex QuadratureRuleCache<coord_type,dim>::mut;
258
259
260#endif
261
size_type dim() const
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
constexpr bool isNone() const
constexpr bool isSimplex() const
Construct composite quadrature rules for simplex elements.
Definition compositequadraturerule.hh:20
A lumping quadrature rule.
Definition lumpingquadraturerule.hh:26
Definition quadraturerulecache.hh:200
static const Dune::QuadratureRule< coord_type, dim > & rule(const QuadratureRuleKey &index)
Definition quadraturerulecache.hh:211
static const Dune::QuadratureRule< coord_type, dim > & rule(const Dune::GeometryType &gt, const int order, int refinement)
Definition quadraturerulecache.hh:206
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:40
int order() const
Definition quadraturerulecache.hh:132
const Dune::GeometryType & geometryType() const
Definition quadraturerulecache.hh:122
QuadratureRuleKey(const int dim, const int order, const int refinement=0, bool lumping=false)
Constructor for rules to integrate analytical functions (i.e., not related to a grid)
Definition quadraturerulecache.hh:91
QuadratureRuleKey square() const
Definition quadraturerulecache.hh:182
QuadratureRuleKey(const Dune::GeometryType &gt, const int order, const int refinement=0, bool lumping=false)
Constructor with given element type, order and refinement level.
Definition quadraturerulecache.hh:78
void setRefinement(int refinement)
Definition quadraturerulecache.hh:147
bool lumping() const
Definition quadraturerulecache.hh:152
QuadratureRuleKey derivative() const
Definition quadraturerulecache.hh:164
bool operator<(const QuadratureRuleKey &other) const
defines order relation on QuadratureRuleKeys
Definition quadraturerulecache.hh:103
QuadratureRuleKey product(const QuadratureRuleKey &other) const
Definition quadraturerulecache.hh:176
QuadratureRuleKey(const LocalFiniteElement &fe)
Create a key for a quadrature rule that can integrate a given local basis exactly.
Definition quadraturerulecache.hh:64
int refinement_
Definition quadraturerulecache.hh:191
bool lumping_
Definition quadraturerulecache.hh:192
int order_
Definition quadraturerulecache.hh:190
Dune::GeometryType gt_
Definition quadraturerulecache.hh:189
void setLumping(bool lumping)
Definition quadraturerulecache.hh:157
QuadratureRuleKey sum(const QuadratureRuleKey &other) const
Definition quadraturerulecache.hh:170
QuadratureRuleKey()
Default constructor, same as QuadratureRuleKey(0,0)
Definition quadraturerulecache.hh:53
int refinement() const
Definition quadraturerulecache.hh:142
void setGeometryType(const Dune::GeometryType &gt)
Definition quadraturerulecache.hh:127
void setOrder(int order)
Definition quadraturerulecache.hh:137
Definition refinedquadraturerule.hh:21
Construct composite quadrature rules from other quadrature rules.
T emplace(T... args)
T end(T... args)
T find(T... args)
T max(T... args)
static bool value(const LocalFiniteElementType &)
Definition refinedfehelper.hh:20