dune-grid 2.11
Loading...
Searching...
No Matches
boundaryprojection.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © 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_GRID_COMMON_BOUNDARYPROJECTION_HH
6#define DUNE_GRID_COMMON_BOUNDARYPROJECTION_HH
7
8//- system includes
9#include <cmath>
10#include <memory>
11
12//- Dune includes
13#include <dune/common/fvector.hh>
14
15#include <dune/geometry/multilineargeometry.hh>
16
19
20namespace Dune
21{
24 template <int dimworld>
26
29 template <int dimworld>
31 : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
32 {
35 typedef typename BaseType :: ObjectStreamType ObjectStreamType;
36
37 using BaseType :: restore;
39
41 typedef FieldVector< double, dimworld> CoordinateType;
44
46 virtual CoordinateType operator() (const CoordinateType& global) const = 0;
47
51 virtual void backup( [[maybe_unused]] ObjectStreamType& buffer ) const
52 {
53 DUNE_THROW(NotImplemented,"DuneBoundaryProjection::backup not overloaded!");
54 }
55
56 template <class BufferImp>
57 void toBuffer( BufferImp& buffer ) const
58 {
59 MessageBufferIF< BufferImp > buf( buffer );
60 toBuffer( buf );
61 }
62
63 template <class BufferImp>
65 {
67 // call virtual interface backup
68 backup( str );
69 std::string data = str.str();
70 const size_t size = data.size();
71 buffer.write( size );
72 for( size_t i=0; i<size; ++i )
73 buffer.write( data[ i ] );
74 }
75
76 template <class BufferImp>
77 static std::unique_ptr< ThisType > restoreFromBuffer( BufferImp & buffer )
78 {
79 MessageBufferIF< BufferImp > buf( buffer );
80 return restoreFromBuffer( buf );
81 }
82
83 template <class BufferImp>
84 static std::unique_ptr< ThisType > restoreFromBuffer( MessageBufferIF< BufferImp > & buffer )
85 {
86 std::string data;
87 size_t size = 0;
88 buffer.read( size );
89 data.resize( size );
90 for( size_t i=0; i<size; ++i )
91 buffer.read( data[ i ] );
92
94 str.write( data.c_str(), size );
95 return BaseType::restore( str );
96 }
97 };
98
99 template < int dimworld >
101 : public DuneBoundaryProjection< dimworld >
102 {
103 protected:
106 public:
108 typedef typename BaseType :: CoordinateType CoordinateType;
109
110 // constructor taking other projection
112 : proj_( proje )
113 {}
114
117
120 {
121 return proj_( global );
122 }
123 };
124
125 // BoundarySegmentWrapper
126 // ----------------------
127
129 template< int dim, int dimworld >
131 : public DuneBoundaryProjection< dimworld >
132 {
135
136 typedef typename Base :: ObjectStreamType ObjectStreamType;
137
138 typedef MultiLinearGeometry<typename Base::CoordinateType::value_type,dim-1,dimworld> FaceMapping;
139
140 public:
143
152 BoundarySegmentWrapper ( const GeometryType &type,
153 const std::vector< CoordinateType > &vertices,
154 const std::shared_ptr< BoundarySegment > &boundarySegment )
155 : faceMapping_( FaceMapping( type, vertices ) ),
156 boundarySegment_( boundarySegment )
157 {}
158
159 BoundarySegmentWrapper( ObjectStreamType& buffer )
160 : faceMapping_( readFaceMapping( buffer ) ),
161 boundarySegment_( BoundarySegment::restore( buffer ).release() )
162 {
163 }
164
166 {
167 return boundarySegment() ( faceMapping_.local( global ) );
168 }
169
171 {
172 return *boundarySegment_;
173 }
174
175 void backup( ObjectStreamType& buffer ) const
176 {
177 // write identifier key first
178 buffer.write( (const char *) &key(), sizeof(int));
179 // now all data
180 GeometryType type = faceMapping_.type();
181 buffer.write( (const char *) &type, sizeof(GeometryType) );
182
183 int corners = faceMapping_.corners() ;
184 buffer.write( (const char *) &corners, sizeof(int) );
185
186 CoordinateType corner( 0 );
187 for( int i=0; i<corners; ++i )
188 {
189 corner = faceMapping_.corner( i );
190 buffer.write( (const char *) &corner[ 0 ], sizeof(double)*CoordinateType::dimension );
191 }
192
193 boundarySegment_->backup( buffer );
194 }
195
196 static void registerFactory()
197 {
198 if( key() < 0 )
199 {
200 key() = Base::template registerFactory< ThisType >();
201 }
202 }
203
204 protected:
205 static int& key()
206 {
207 static int k = -1;
208 return k;
209 }
210
211 FaceMapping readFaceMapping( ObjectStreamType& buffer )
212 {
213 GeometryType type;
214 buffer.read( (char *) &type, sizeof(GeometryType) );
215 int corners = 0;
216 buffer.read( (char *) &corners, sizeof(int) );
217 std::vector< CoordinateType > vertices( corners, CoordinateType(0) );
218 for( int i=0; i<corners; ++i )
219 {
220 buffer.read( (char *) &vertices[ i ][ 0 ], sizeof(double)*CoordinateType::dimension );
221 }
222 return FaceMapping( type, vertices );
223 }
224
225 private:
226 FaceMapping faceMapping_;
227 const std::shared_ptr< BoundarySegment > boundarySegment_;
228 };
229
230
231
233 //
234 // Example of boundary projection projection to a circle
235 //
237 template <int dimworld>
239 {
241 typedef FieldVector< double, dimworld> CoordinateType;
242
244 CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
245 : radius_( radius ) {}
246
249
251 virtual CoordinateType operator() (const CoordinateType& global) const
252 {
253 CoordinateType prj( global );
254 // get adjustment factor
255 const double factor = radius_ / global.two_norm();
256 // adjust
257 prj *= factor;
258 return prj;
259 }
260
261 protected:
263 const double radius_;
264 };
265
266} // end namespace
267
268#endif // #ifndef DUNE_GRID_COMMON_BOUNDARYPROJECTION_HH
Describes the parallel communication interface class for MessageBuffers and DataHandles.
Base class for grid boundary segments of arbitrary geometry.
IteratorRange<... > vertices(const GV &gv)
Iterates over all vertices (entities with dimension 0) of a GridView.
Include standard header files.
Definition agrid.hh:60
Interface class for vertex projection at the boundary.
Definition boundaryprojection.hh:32
DuneBoundaryProjection< dimworld > ThisType
Definition boundaryprojection.hh:33
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition boundaryprojection.hh:51
static std::unique_ptr< ThisType > restoreFromBuffer(MessageBufferIF< BufferImp > &buffer)
Definition boundaryprojection.hh:84
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
void toBuffer(BufferImp &buffer) const
Definition boundaryprojection.hh:57
BaseType::ObjectStreamType ObjectStreamType
Definition boundaryprojection.hh:35
virtual ~DuneBoundaryProjection()
destructor
Definition boundaryprojection.hh:43
static std::unique_ptr< ThisType > restoreFromBuffer(BufferImp &buffer)
Definition boundaryprojection.hh:77
FieldVector< double, dimworld > CoordinateType
Definition boundaryprojection.hh:41
void toBuffer(MessageBufferIF< BufferImp > &buffer) const
Definition boundaryprojection.hh:64
BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType
Definition boundaryprojection.hh:34
BaseType::CoordinateType CoordinateType
type of coordinate vector
Definition boundaryprojection.hh:108
BoundaryProjectionWrapper(const BaseType &proje)
Definition boundaryprojection.hh:111
const BaseType & proj_
Definition boundaryprojection.hh:105
DuneBoundaryProjection< dimworld > BaseType
Definition boundaryprojection.hh:104
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition boundaryprojection.hh:119
~BoundaryProjectionWrapper()
destructor
Definition boundaryprojection.hh:116
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition boundaryprojection.hh:165
void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition boundaryprojection.hh:175
static int & key()
Definition boundaryprojection.hh:205
const BoundarySegment & boundarySegment() const
Definition boundaryprojection.hh:170
BoundarySegmentWrapper(const GeometryType &type, const std::vector< CoordinateType > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
Definition boundaryprojection.hh:152
BoundarySegmentWrapper(ObjectStreamType &buffer)
Definition boundaryprojection.hh:159
Dune::BoundarySegment< dim, dimworld > BoundarySegment
Definition boundaryprojection.hh:142
static void registerFactory()
Definition boundaryprojection.hh:196
FaceMapping readFaceMapping(ObjectStreamType &buffer)
Definition boundaryprojection.hh:211
Base::CoordinateType CoordinateType
Definition boundaryprojection.hh:141
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition boundaryprojection.hh:241
CircleBoundaryProjection(const double radius=std::sqrt((double) dimworld))
constructor taking radius of circle (default = sqrt( dimworld ) )
Definition boundaryprojection.hh:244
virtual CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition boundaryprojection.hh:251
virtual ~CircleBoundaryProjection()
destructor
Definition boundaryprojection.hh:248
const double radius_
radius of circ
Definition boundaryprojection.hh:263
Definition boundarysegment.hh:41
static std::unique_ptr< BoundarySegment > restore(ObjectStreamType &in)
Definition boundarysegment.hh:59
static int registerFactory()
Definition boundarysegment.hh:70
Communication message buffer interface. This class describes the interface for reading and writing da...
Definition datahandleif.hh:33
void write(const T &val)
just wraps the call of the internal buffer method write which writes the data of type T from the buff...
Definition datahandleif.hh:45
void read(T &val)
just wraps the call of the internal buffer method read which reads the data of type T from the buffer...
Definition datahandleif.hh:59