dune-grid 2.9.0
geometrygrid/geometry.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_GEOGRID_GEOMETRY_HH
6#define DUNE_GEOGRID_GEOMETRY_HH
7
8#include <utility>
9
10#include <dune/common/typetraits.hh>
11
12#include <dune/geometry/multilineargeometry.hh>
13#include <dune/geometry/referenceelements.hh>
14#include <dune/geometry/type.hh>
15
18
19namespace Dune
20{
21
22 namespace GeoGrid
23 {
24
25 // InferHasSingleGeometryType
26 // --------------------------
27
28 template< class hasSingleGeometryType, int dim, int mydim >
30 {
31 private:
32 static const unsigned int id = hasSingleGeometryType::topologyId;
33 static const unsigned int idMask = (1u << mydim) - 1u;
34
35 public:
36 static const bool v = hasSingleGeometryType::v && ((mydim == dim) || ((id | 1u) == 1u) || ((id | 1u) == idMask));
37 static const unsigned int topologyId = (v ? id & idMask : ~0u);
38 };
39
40 template< class hasSingleGeometryType, int dim >
41 struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 1 >
42 {
43 static const bool v = true;
44 static const unsigned int topologyId = GeometryTypes::cube(1).id();
45 };
46
47 template< class hasSingleGeometryType, int dim >
48 struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 0 >
49 {
50 static const bool v = true;
51 static const unsigned int topologyId = GeometryTypes::cube(1).id();
52 };
53
54
55
56 // GeometryTraits
57 // --------------
58
59 template< class Grid >
61 {
62 typedef typename std::remove_const< Grid >::type::Traits Traits;
63
64 typedef typename Traits::ctype ctype;
65
66 typedef Impl::FieldMatrixHelper< ctype > MatrixHelper;
67
68 static ctype tolerance () { return 16 * std::numeric_limits< ctype >::epsilon(); }
69
70 template< int mydim, int cdim >
72 {
74 };
75
76 template< int mydim >
78 : public InferHasSingleGeometryType< Capabilities::hasSingleGeometryType< Grid >, Traits::dimension, mydim >
79 {};
80 };
81
82
83
84 // Geometry
85 // --------
86
87 template< int mydim, int cdim, class Grid >
89 {
91
92 typedef typename std::remove_const< Grid >::type::Traits Traits;
93
94 template< int, int, class > friend class Geometry;
95
96 public:
97 typedef typename Traits::ctype ctype;
98
99 static const int mydimension = mydim;
100 static const int coorddimension = cdim;
101 static const int dimension = Traits::dimension;
102 static const int codimension = dimension - mydimension;
103
104 protected:
105 typedef CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping;
106
107 struct Mapping
108 : public BasicMapping
109 {
110 template< class CoordVector >
111 Mapping ( const GeometryType &type, const CoordVector &coords )
112 : BasicMapping( type, coords ),
113 refCount_( 0 )
114 {}
115
116 void addReference () { ++refCount_; }
117 bool removeReference () { return (--refCount_ == 0); }
118
119 private:
120 unsigned int refCount_;
121 };
122
123 public:
124 typedef typename Mapping::LocalCoordinate LocalCoordinate;
125 typedef typename Mapping::GlobalCoordinate GlobalCoordinate;
126
127 typedef typename Mapping::JacobianTransposed JacobianTransposed;
128 typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;
129 typedef typename Mapping::Jacobian Jacobian;
130 typedef typename Mapping::JacobianInverse JacobianInverse;
131
132
133 Geometry () : grid_( nullptr ), mapping_( nullptr ) {}
134
135 explicit Geometry ( const Grid &grid ) : grid_( &grid ), mapping_( nullptr ) {}
136
137 template< class CoordVector >
138 Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
139 : grid_( &grid )
140 {
141 assert( int( type.dim() ) == mydimension );
142 void *mappingStorage = grid.allocateStorage( sizeof( Mapping ) );
143 mapping_ = new( mappingStorage ) Mapping( type, coords );
144 mapping_->addReference();
145 }
146
147 Geometry ( const This &other )
148 : grid_( other.grid_ ),
149 mapping_( other.mapping_ )
150 {
151 if( mapping_ )
152 mapping_->addReference();
153 }
154
155 Geometry ( This&& other )
156 : grid_( other.grid_ ),
157 mapping_( other.mapping_ )
158 {
159 other.grid_ = nullptr;
160 other.mapping_ = nullptr;
161 }
162
164 {
165 if( mapping_ && mapping_->removeReference() )
166 destroyMapping();
167 }
168
169 const This &operator= ( const This &other )
170 {
171 if( other.mapping_ )
172 other.mapping_->addReference();
173 if( mapping_ && mapping_->removeReference() )
174 destroyMapping();
175 grid_ = other.grid_;
176 mapping_ = other.mapping_;
177 return *this;
178 }
179
180 const This &operator= ( This&& other )
181 {
182 using std::swap;
183 swap( grid_, other.grid_ );
184 swap( mapping_, other.mapping_ );
185 return *this;
186 }
187
188 explicit operator bool () const { return bool( mapping_ ); }
189
190 bool affine () const { return mapping_->affine(); }
191 GeometryType type () const { return mapping_->type(); }
192
193 int corners () const { return mapping_->corners(); }
194 GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
195 GlobalCoordinate center () const { return mapping_->center(); }
196
197 GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
198 LocalCoordinate local ( const GlobalCoordinate &global ) const { return mapping_->local( global ); }
199
200 ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
201 ctype volume () const { return mapping_->volume(); }
202
203 JacobianTransposed jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
204 JacobianInverseTransposed jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }
205
206 Jacobian jacobian ( const LocalCoordinate &local ) const { return mapping_->jacobian( local ); }
207 JacobianInverse jacobianInverse ( const LocalCoordinate &local ) const { return mapping_->jacobianInverse( local ); }
208
209 const Grid &grid () const { assert( grid_ ); return *grid_; }
210
211 private:
212 void destroyMapping ()
213 {
214 mapping_->~Mapping();
215 grid().deallocateStorage( mapping_, sizeof( Mapping ) );
216 }
217
218 const Grid *grid_;
219 Mapping* mapping_;
220 };
221
222 } // namespace GeoGrid
223
224} // namespace Dune
225
226#endif // #ifndef DUNE_GEOGRID_GEOMETRY_HH
void swap(Dune::PersistentContainer< G, T > &a, Dune::PersistentContainer< G, T > &b)
Definition: utility/persistentcontainer.hh:83
Include standard header files.
Definition: agrid.hh:60
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
Grid abstract base class.
Definition: common/grid.hh:375
Definition: cornerstorage.hh:22
Definition: cornerstorage.hh:174
Definition: geometrygrid/geometry.hh:30
static const unsigned int topologyId
Definition: geometrygrid/geometry.hh:37
static const bool v
Definition: geometrygrid/geometry.hh:36
Definition: geometrygrid/geometry.hh:61
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:64
Impl::FieldMatrixHelper< ctype > MatrixHelper
Definition: geometrygrid/geometry.hh:66
static ctype tolerance()
Definition: geometrygrid/geometry.hh:68
std::remove_const< Grid >::type::Traits Traits
Definition: geometrygrid/geometry.hh:62
Definition: geometrygrid/geometry.hh:72
GeoGrid::CornerStorage< mydim, cdim, Grid > Type
Definition: geometrygrid/geometry.hh:73
Definition: geometrygrid/geometry.hh:79
Definition: geometrygrid/geometry.hh:89
GlobalCoordinate global(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:197
Geometry(const Grid &grid)
Definition: geometrygrid/geometry.hh:135
int corners() const
Definition: geometrygrid/geometry.hh:193
static const int codimension
Definition: geometrygrid/geometry.hh:102
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:97
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:204
Geometry(const Grid &grid, const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:138
static const int dimension
Definition: geometrygrid/geometry.hh:101
Mapping::Jacobian Jacobian
Definition: geometrygrid/geometry.hh:129
Geometry(const This &other)
Definition: geometrygrid/geometry.hh:147
static const int mydimension
Definition: geometrygrid/geometry.hh:99
CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping
Definition: geometrygrid/geometry.hh:105
bool affine() const
Definition: geometrygrid/geometry.hh:190
Geometry(This &&other)
Definition: geometrygrid/geometry.hh:155
Mapping::JacobianInverse JacobianInverse
Definition: geometrygrid/geometry.hh:130
GlobalCoordinate corner(const int i) const
Definition: geometrygrid/geometry.hh:194
Geometry()
Definition: geometrygrid/geometry.hh:133
JacobianInverse jacobianInverse(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:207
static const int coorddimension
Definition: geometrygrid/geometry.hh:100
Jacobian jacobian(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:206
Mapping::JacobianTransposed JacobianTransposed
Definition: geometrygrid/geometry.hh:127
GeometryType type() const
Definition: geometrygrid/geometry.hh:191
ctype volume() const
Definition: geometrygrid/geometry.hh:201
GlobalCoordinate center() const
Definition: geometrygrid/geometry.hh:195
const Grid & grid() const
Definition: geometrygrid/geometry.hh:209
~Geometry()
Definition: geometrygrid/geometry.hh:163
ctype integrationElement(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:200
const This & operator=(const This &other)
Definition: geometrygrid/geometry.hh:169
JacobianTransposed jacobianTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:203
Mapping::GlobalCoordinate GlobalCoordinate
Definition: geometrygrid/geometry.hh:125
Mapping::JacobianInverseTransposed JacobianInverseTransposed
Definition: geometrygrid/geometry.hh:128
Mapping::LocalCoordinate LocalCoordinate
Definition: geometrygrid/geometry.hh:124
Definition: geometrygrid/geometry.hh:109
Mapping(const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:111
bool removeReference()
Definition: geometrygrid/geometry.hh:117
void addReference()
Definition: geometrygrid/geometry.hh:116
A set of traits classes to store static information about grid implementation.