dune-grid 2.9.0
boundaryprojection.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_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
20
21namespace Dune
22{
25 template <int dimworld>
26 struct DuneBoundaryProjection;
27
30 template <int dimworld>
32 : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
33 {
37
38 using BaseType :: restore;
40
42 typedef FieldVector< double, dimworld> CoordinateType;
45
47 virtual CoordinateType operator() (const CoordinateType& global) const = 0;
48
52 virtual void backup( [[maybe_unused]] ObjectStreamType& buffer ) const
53 {
54 DUNE_THROW(NotImplemented,"DuneBoundaryProjection::backup not overloaded!");
55 }
56
57 template <class BufferImp>
58 void toBuffer( BufferImp& buffer ) const
59 {
60 MessageBufferIF< BufferImp > buf( buffer );
61 toBuffer( buf );
62 }
63
64 template <class BufferImp>
66 {
68 // call virtual interface backup
69 backup( str );
70 std::string data = str.str();
71 const size_t size = data.size();
72 buffer.write( size );
73 for( size_t i=0; i<size; ++i )
74 buffer.write( data[ i ] );
75 }
76
77 template <class BufferImp>
78 static std::unique_ptr< ThisType > restoreFromBuffer( BufferImp & buffer )
79 {
80 MessageBufferIF< BufferImp > buf( buffer );
81 return restoreFromBuffer( buf );
82 }
83
84 template <class BufferImp>
85 static std::unique_ptr< ThisType > restoreFromBuffer( MessageBufferIF< BufferImp > & buffer )
86 {
87 std::string data;
88 size_t size = 0;
89 buffer.read( size );
90 data.resize( size );
91 for( size_t i=0; i<size; ++i )
92 buffer.read( data[ i ] );
93
95 str.write( data.c_str(), size );
96 return BaseType::restore( str );
97 }
98 };
99
100 template < int dimworld >
102 : public DuneBoundaryProjection< dimworld >
103 {
104 protected:
107 public:
110
111 // constructor taking other projection
113 : proj_( proje )
114 {}
115
118
121 {
122 return proj_( global );
123 }
124 };
125
126 // BoundarySegmentWrapper
127 // ----------------------
128
130 template< int dim, int dimworld >
132 : public DuneBoundaryProjection< dimworld >
133 {
136
138
139 typedef MultiLinearGeometry<typename Base::CoordinateType::value_type,dim-1,dimworld> FaceMapping;
140
141 public:
144
154 const std::vector< CoordinateType > &vertices,
155 const std::shared_ptr< BoundarySegment > &boundarySegment )
156 : faceMapping_( FaceMapping( type, vertices ) ),
157 boundarySegment_( boundarySegment )
158 {}
159
161 : faceMapping_( readFaceMapping( buffer ) ),
162 boundarySegment_( BoundarySegment::restore( buffer ).release() )
163 {
164 }
165
167 {
168 return boundarySegment() ( faceMapping_.local( global ) );
169 }
170
172 {
173 return *boundarySegment_;
174 }
175
176 void backup( ObjectStreamType& buffer ) const
177 {
178 // write identifier key first
179 buffer.write( (const char *) &key(), sizeof(int));
180 // now all data
181 GeometryType type = faceMapping_.type();
182 buffer.write( (const char *) &type, sizeof(GeometryType) );
183
184 int corners = faceMapping_.corners() ;
185 buffer.write( (const char *) &corners, sizeof(int) );
186
187 CoordinateType corner( 0 );
188 for( int i=0; i<corners; ++i )
189 {
190 corner = faceMapping_.corner( i );
191 buffer.write( (const char *) &corner[ 0 ], sizeof(double)*CoordinateType::dimension );
192 }
193
194 boundarySegment_->backup( buffer );
195 }
196
197 static void registerFactory()
198 {
199 if( key() < 0 )
200 {
201 key() = Base::template registerFactory< ThisType >();
202 }
203 }
204
205 protected:
206 static int& key()
207 {
208 static int k = -1;
209 return k;
210 }
211
212 FaceMapping readFaceMapping( ObjectStreamType& buffer )
213 {
214 GeometryType type;
215 buffer.read( (char *) &type, sizeof(GeometryType) );
216 int corners = 0;
217 buffer.read( (char *) &corners, sizeof(int) );
218 std::vector< CoordinateType > vertices( corners, CoordinateType(0) );
219 for( int i=0; i<corners; ++i )
220 {
221 buffer.read( (char *) &vertices[ i ][ 0 ], sizeof(double)*CoordinateType::dimension );
222 }
223 return FaceMapping( type, vertices );
224 }
225
226 private:
227 FaceMapping faceMapping_;
228 const std::shared_ptr< BoundarySegment > boundarySegment_;
229 };
230
231
232
234 //
235 // Example of boundary projection projection to a circle
236 //
238 template <int dimworld>
240 {
242 typedef FieldVector< double, dimworld> CoordinateType;
243
245 CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
246 : radius_( radius ) {}
247
250
252 virtual CoordinateType operator() (const CoordinateType& global) const
253 {
254 CoordinateType prj( global );
255 // get adjustment factor
256 const double factor = radius_ / global.two_norm();
257 // adjust
258 prj *= factor;
259 return prj;
260 }
261
262 protected:
264 const double radius_;
265 };
266
267} // end namespace
268
269#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.
Include standard header files.
Definition: agrid.hh:60
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:33
DuneBoundaryProjection< dimworld > ThisType
Definition: boundaryprojection.hh:34
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:52
static std::unique_ptr< ThisType > restoreFromBuffer(MessageBufferIF< BufferImp > &buffer)
Definition: boundaryprojection.hh:85
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
void toBuffer(BufferImp &buffer) const
Definition: boundaryprojection.hh:58
BaseType::ObjectStreamType ObjectStreamType
Definition: boundaryprojection.hh:36
virtual ~DuneBoundaryProjection()
destructor
Definition: boundaryprojection.hh:44
static std::unique_ptr< ThisType > restoreFromBuffer(BufferImp &buffer)
Definition: boundaryprojection.hh:78
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:42
void toBuffer(MessageBufferIF< BufferImp > &buffer) const
Definition: boundaryprojection.hh:65
BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType
Definition: boundaryprojection.hh:35
Definition: boundaryprojection.hh:103
BaseType::CoordinateType CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:109
BoundaryProjectionWrapper(const BaseType &proje)
Definition: boundaryprojection.hh:112
const BaseType & proj_
Definition: boundaryprojection.hh:106
DuneBoundaryProjection< dimworld > BaseType
Definition: boundaryprojection.hh:105
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:120
~BoundaryProjectionWrapper()
destructor
Definition: boundaryprojection.hh:117
Definition: boundaryprojection.hh:133
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:166
void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:176
static int & key()
Definition: boundaryprojection.hh:206
const BoundarySegment & boundarySegment() const
Definition: boundaryprojection.hh:171
BoundarySegmentWrapper(const GeometryType &type, const std::vector< CoordinateType > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
Definition: boundaryprojection.hh:153
BoundarySegmentWrapper(ObjectStreamType &buffer)
Definition: boundaryprojection.hh:160
Dune::BoundarySegment< dim, dimworld > BoundarySegment
Definition: boundaryprojection.hh:143
static void registerFactory()
Definition: boundaryprojection.hh:197
FaceMapping readFaceMapping(ObjectStreamType &buffer)
Definition: boundaryprojection.hh:212
Base::CoordinateType CoordinateType
Definition: boundaryprojection.hh:142
Definition: boundaryprojection.hh:240
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:242
CircleBoundaryProjection(const double radius=std::sqrt((double) dimworld))
constructor taking radius of circle (default = sqrt( dimworld ) )
Definition: boundaryprojection.hh:245
virtual CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:252
virtual ~CircleBoundaryProjection()
destructor
Definition: boundaryprojection.hh:249
const double radius_
radius of circ
Definition: boundaryprojection.hh:264
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:94
Definition: boundarysegment.hh:41
static std::unique_ptr< BoundarySegment > restore(ObjectStreamType &in)
create an object of BoundarySegment type from a previously registered factory linked to key.
Definition: boundarysegment.hh:59
std::stringstream ObjectStreamType
Definition: boundarysegment.hh:44
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