DUNE PDELab (git)

boundaryprojection.hh
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
14
15#include <dune/geometry/multilineargeometry.hh>
16
19#include <dune/grid/io/file/gmshreader.hh>
20
21namespace Dune
22{
25 template <int dimworld>
26 struct DuneBoundaryProjection;
27
30 template <int dimworld>
32 : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
33 {
35 typedef BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType;
36 typedef typename BaseType :: ObjectStreamType ObjectStreamType;
37
38 using BaseType :: restore;
39 using BaseType :: registerFactory;
40
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>
65 void toBuffer( MessageBufferIF< BufferImp > & buffer ) const
66 {
67 ObjectStreamType str;
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
94 ObjectStreamType str;
95 str.write( data.c_str(), size );
96 return BaseType::restore( str );
97 }
98 };
99
100 template < int dimworld >
101 class BoundaryProjectionWrapper
102 : public DuneBoundaryProjection< dimworld >
103 {
104 protected:
105 typedef DuneBoundaryProjection< dimworld > BaseType;
106 const BaseType& proj_;
107 public:
109 typedef typename BaseType :: CoordinateType CoordinateType;
110
111 // constructor taking other projection
112 BoundaryProjectionWrapper( const BaseType& proje )
113 : proj_( proje )
114 {}
115
117 ~BoundaryProjectionWrapper () {}
118
120 CoordinateType operator() (const CoordinateType& global) const
121 {
122 return proj_( global );
123 }
124 };
125
126 // BoundarySegmentWrapper
127 // ----------------------
128
130 template< int dim, int dimworld >
132 : public DuneBoundaryProjection< dimworld >
133 {
136
137 typedef typename Base :: ObjectStreamType ObjectStreamType;
138
139 typedef MultiLinearGeometry<typename Base::CoordinateType::value_type,dim-1,dimworld> FaceMapping;
140
141 public:
142 typedef typename Base::CoordinateType CoordinateType;
144
154 const std::vector< CoordinateType > &vertices,
155 const std::shared_ptr< BoundarySegment > &boundarySegment )
156 : faceMapping_( FaceMapping( type, vertices ) ),
157 boundarySegment_( boundarySegment )
158 {}
159
160 BoundarySegmentWrapper( ObjectStreamType& buffer )
161 : faceMapping_( readFaceMapping( buffer ) ),
162 boundarySegment_( BoundarySegment::restore( buffer ).release() )
163 {
164 }
165
167 {
168 return boundarySegment() ( faceMapping_.local( global ) );
169 }
170
171 const BoundarySegment &boundarySegment () const
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>
239 struct CircleBoundaryProjection : public DuneBoundaryProjection< dimworld >
240 {
242 typedef FieldVector< double, dimworld> CoordinateType;
243
245 CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
246 : radius_( radius ) {}
247
249 virtual ~CircleBoundaryProjection() {}
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
Base class for grid boundary segments of arbitrary geometry.
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
BoundarySegmentWrapper(const GeometryType &type, const std::vector< CoordinateType > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
Definition: boundaryprojection.hh:153
static constexpr int dimension
The size of this vector.
Definition: fvector.hh:96
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
Communication message buffer interface. This class describes the interface for reading and writing da...
Definition: datahandleif.hh:33
generic geometry implementation based on corner coordinates
Definition: multilineargeometry.hh:181
Dune::GeometryType type() const
obtain the name of the reference element
Definition: multilineargeometry.hh:269
GlobalCoordinate corner(int i) const
obtain coordinates of the i-th corner
Definition: multilineargeometry.hh:275
int corners() const
obtain number of corners of the corresponding reference element
Definition: multilineargeometry.hh:272
Default exception for dummy implementations.
Definition: exceptions.hh:263
Describes the parallel communication interface class for MessageBuffers and DataHandles.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:94
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:33
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:52
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
virtual ~DuneBoundaryProjection()
destructor
Definition: boundaryprojection.hh:44
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:42
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)