dune-geometry  2.11
mappedgeometry.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_GEOMETRY_MAPPEDGEOMETRY_HH
6 #define DUNE_GEOMETRY_MAPPEDGEOMETRY_HH
7 
8 #include <cassert>
9 #include <limits>
10 #include <optional>
11 #include <stdexcept>
12 #include <type_traits>
13 
14 #include <dune/common/copyableoptional.hh>
15 #include <dune/common/exceptions.hh>
16 #include <dune/common/fmatrix.hh>
17 #include <dune/common/fvector.hh>
18 #include <dune/common/math.hh>
19 #include <dune/common/transpose.hh>
22 #include <dune/geometry/type.hh>
26 
27 namespace Dune {
28 
63 template <class Map, class Geo>
65 {
66 public:
68  using LocalCoordinate = typename Geo::LocalCoordinate;
69 
71  using GlobalCoordinate = std::remove_reference_t<decltype(std::declval<Map>()(std::declval<typename Geo::GlobalCoordinate>()))>;
72 
74  using ctype = typename Geo::ctype;
75 
77  static constexpr int mydimension = LocalCoordinate::size();
78 
80  static constexpr int coorddimension = GlobalCoordinate::size();
81 
83  using Volume = std::remove_reference_t<decltype(Dune::power(std::declval<ctype>(),mydimension))>;
84 
86  using Jacobian = FieldMatrix<ctype, coorddimension, mydimension>;
87 
89  using JacobianTransposed = FieldMatrix<ctype, mydimension, coorddimension>;
90 
92  using JacobianInverse = FieldMatrix<ctype, mydimension, coorddimension>;
93 
95  using JacobianInverseTransposed = FieldMatrix<ctype, coorddimension, mydimension>;
96 
97 private:
99  using ReferenceElement = typename ReferenceElements::ReferenceElement;
100 
101 protected:
102  using MatrixHelper = Impl::FieldMatrixHelper<ctype>;
103 
104  // type of the mapping representation the geometry parametrization
105  using Mapping = Map;
106 
107  // type of the geometry that is wrapped
108  using Geometry = Geo;
109 
110  // type of a mapping representing the derivative of `Map` w.r.t. `GlobalCoordinate`
111  using DerivativeMapping = std::remove_reference_t<decltype(derivative(std::declval<Map>()))>;
112 
113 public:
122  template <class Geo_, class Map_,
123  std::enable_if_t<Dune::IsInteroperable<Map, Map_>::value, int> = 0,
124  std::enable_if_t<Dune::IsInteroperable<Geo, Geo_>::value, int> = 0>
125  MappedGeometry (Map_&& mapping, Geo_&& geometry, bool affine = false)
126  : mapping_(std::forward<Map_>(mapping))
127  , dMapping_(derivative(*mapping_))
128  , geometry_(std::forward<Geo_>(geometry))
129  , affine_(affine)
130  {}
131 
137  bool affine () const
138  {
139  return affine_;
140  }
141 
144  {
145  return geometry_.type();
146  }
147 
149  int corners () const
150  {
151  return geometry_.corners();
152  }
153 
155  GlobalCoordinate corner (int i) const
156  {
157  assert( (i >= 0) && (i < corners()) );
158  return mapping()(geometry_.corner(i));
159  }
160 
163  {
164  return mapping()(geometry_.center());
165  }
166 
177  {
178  return mapping()(geometry_.global(local));
179  }
180 
197  LocalCoordinate local (const GlobalCoordinate& y, Impl::GaussNewtonOptions<ctype> opts = {}) const
198  {
199  LocalCoordinate x = refElement().position(0,0);
200  Impl::GaussNewtonErrorCode err = Impl::gaussNewton(
201  [&](const LocalCoordinate& local) { return this->global(local); },
202  [&](const LocalCoordinate& local) { return this->jacobianTransposed(local); },
203  y, x, opts
204  );
205 
206  if (err != Impl::GaussNewtonErrorCode::OK)
207  DUNE_THROW(Dune::Exception,
208  "Local coordinate can not be recovered from global coordinate, error code = " << int(err) << "\n"
209  << " (global(x) - y).two_norm() = " << (global(x) - y).two_norm()
210  << " > tol = " << opts.absTol);
211 
212  return x;
213  }
214 
226  {
227  return MatrixHelper::sqrtDetAAT(jacobianTransposed(local));
228  }
229 
241  Volume volume (Impl::ConvergenceOptions<ctype> opts = {}) const
242  {
244  if (affine())
245  return vol0;
246 
247  using std::abs;
248  for (int p = 2; p < opts.maxIt; ++p) {
250  if (abs(vol1 - vol0) < opts.absTol)
251  return vol1;
252 
253  vol0 = vol1;
254  }
255  return vol0;
256  }
257 
260  {
261  Volume vol(0);
262  for (const auto& qp : quadRule)
263  vol += integrationElement(qp.position()) * qp.weight();
264  return vol;
265  }
266 
273  {
274  auto&& jLocal = geometry_.jacobian(local);
275  auto&& jMapping = (*dMapping_)(geometry_.global(local));
276  return jMapping * jLocal;
277  }
278 
285  {
286  return transpose(jacobian(local));
287  }
288 
297  {
298  JacobianInverse out;
299  MatrixHelper::leftInvA(jacobian(local), out);
300  return out;
301  }
302 
311  {
312  return transpose(jacobianInverse(local));
313  }
314 
316  friend ReferenceElement referenceElement (const MappedGeometry& geometry)
317  {
318  return geometry.refElement();
319  }
320 
321 protected:
322  // the internal stored reference element
323  ReferenceElement refElement () const
324  {
325  return referenceElement(geometry_);
326  }
327 
328 private:
329  // internal reference to the stored mapping
330  const Mapping& mapping () const
331  {
332  return *mapping_;
333  }
334 
335  // internal reference to the wrapped geometry
336  const Geometry& geometry () const
337  {
338  return geometry_;
339  }
340 
341 private:
343  CopyableOptional<Mapping> mapping_;
344 
346  CopyableOptional<DerivativeMapping> dMapping_;
347 
349  Geometry geometry_;
350 
352  bool affine_;
353 };
354 
355 // deduction guides
356 template <class Map, class Geo>
357 MappedGeometry (const Map&, const Geo&)
358  -> MappedGeometry<Map,Geo>;
359 
360 template <class Map, class Geo>
361 MappedGeometry (const Map&, const Geo&, bool)
362  -> MappedGeometry<Map,Geo>;
363 
364 } // end namespace Dune
365 
366 #endif // DUNE_GEOMETRY_MAPPEDGEOMETRY_HH
MappedGeometry(const Map &, const Geo &) -> MappedGeometry< Map, Geo >
JacobianTransposed jacobianTransposed(const LocalCoordinate &local) const
Obtain the transposed of the Jacobian.
Definition: mappedgeometry.hh:284
MappedGeometry(Map_ &&mapping, Geo_ &&geometry, bool affine=false)
Constructor from mapping to parametrize the geometry.
Definition: mappedgeometry.hh:125
GeometryType type() const
Obtain the geometry type from the reference element.
Definition: mappedgeometry.hh:143
A unique label for each type of element that can occur in a grid.
Volume volume(const QuadratureRule< ctype, mydimension > &quadRule) const
Obtain the volume of the mapping&#39;s image by given quadrature rules.
Definition: mappedgeometry.hh:259
typename Geo::ctype ctype
coordinate type
Definition: mappedgeometry.hh:74
GlobalCoordinate corner(int i) const
Obtain coordinates of the i-th corner.
Definition: mappedgeometry.hh:155
STL namespace.
Geometry parametrized by a LocalFunction and a LocalGeometry.
Definition: mappedgeometry.hh:64
Jacobian jacobian(const LocalCoordinate &local) const
Obtain the Jacobian.
Definition: mappedgeometry.hh:272
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed
type of jacobian inverse transposed
Definition: mappedgeometry.hh:95
std::remove_reference_t< decltype(std::declval< Map >()(std::declval< typename Geo::GlobalCoordinate >()))> GlobalCoordinate
type of global coordinates
Definition: mappedgeometry.hh:71
GlobalCoordinate global(const LocalCoordinate &local) const
Evaluate the coordinate mapping.
Definition: mappedgeometry.hh:176
static constexpr int coorddimension
coordinate dimension
Definition: mappedgeometry.hh:80
FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed
type of jacobian transposed
Definition: mappedgeometry.hh:89
typename Geo::LocalCoordinate LocalCoordinate
type of local coordinates
Definition: mappedgeometry.hh:68
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:113
FieldMatrix< ctype, coorddimension, mydimension > Jacobian
type of jacobian
Definition: mappedgeometry.hh:86
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local) const
Obtain the transposed of the Jacobian&#39;s inverse.
Definition: mappedgeometry.hh:310
std::remove_reference_t< decltype(derivative(std::declval< Map >()))> DerivativeMapping
Definition: mappedgeometry.hh:111
friend ReferenceElement referenceElement(const MappedGeometry &geometry)
Obtain the reference-element related to this geometry.
Definition: mappedgeometry.hh:316
Map Mapping
Definition: mappedgeometry.hh:105
Abstract base class for quadrature rules.
Definition: quadraturerules.hh:213
ReferenceElement refElement() const
Definition: mappedgeometry.hh:323
ctype integrationElement(const LocalCoordinate &local) const
Obtain the integration element.
Definition: mappedgeometry.hh:225
FieldMatrix< ctype, mydimension, coorddimension > JacobianInverse
type of jacobian inverse
Definition: mappedgeometry.hh:92
Class providing access to the singletons of the reference elements.
Definition: affinegeometry.hh:37
A container for all quadrature rules of dimension dim
Definition: quadraturerules.hh:260
typename Container::ReferenceElement ReferenceElement
The reference element type.
Definition: referenceelements.hh:146
Definition: quadraturerules.hh:194
Geo Geometry
Definition: mappedgeometry.hh:108
Impl::FieldMatrixHelper< ctype > MatrixHelper
Definition: mappedgeometry.hh:102
JacobianInverse jacobianInverse(const LocalCoordinate &local) const
Obtain the Jacobian&#39;s inverse.
Definition: mappedgeometry.hh:296
int corners() const
Obtain number of corners of the corresponding reference element.
Definition: mappedgeometry.hh:149
GlobalCoordinate center() const
Map the center of the wrapped geometry.
Definition: mappedgeometry.hh:162
Definition: affinegeometry.hh:21
Volume volume(Impl::ConvergenceOptions< ctype > opts={}) const
Obtain the volume of the mapping&#39;s image.
Definition: mappedgeometry.hh:241
static constexpr int mydimension
geometry dimension
Definition: mappedgeometry.hh:77
bool affine() const
Is this mapping affine? Not in general, since we don&#39;t know anything about the mapping. The returned value can be given in the constructor of the class.
Definition: mappedgeometry.hh:137
std::remove_reference_t< decltype(Dune::power(std::declval< ctype >(), mydimension))> Volume
type of volume
Definition: mappedgeometry.hh:83
LocalCoordinate local(const GlobalCoordinate &y, Impl::GaussNewtonOptions< ctype > opts={}) const
Evaluate the inverse coordinate mapping.
Definition: mappedgeometry.hh:197