DUNE PDELab (unstable)

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
20namespace Dune
21{
24 template <int dimworld>
25 struct DuneBoundaryProjection;
26
29 template <int dimworld>
31 : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
32 {
34 typedef BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType;
35 typedef typename BaseType :: ObjectStreamType ObjectStreamType;
36
37 using BaseType :: restore;
38 using BaseType :: registerFactory;
39
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>
64 void toBuffer( MessageBufferIF< BufferImp > & buffer ) const
65 {
66 ObjectStreamType str;
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
93 ObjectStreamType str;
94 str.write( data.c_str(), size );
95 return BaseType::restore( str );
96 }
97 };
98
99 template < int dimworld >
100 class BoundaryProjectionWrapper
101 : public DuneBoundaryProjection< dimworld >
102 {
103 protected:
104 typedef DuneBoundaryProjection< dimworld > BaseType;
105 const BaseType& proj_;
106 public:
108 typedef typename BaseType :: CoordinateType CoordinateType;
109
110 // constructor taking other projection
111 BoundaryProjectionWrapper( const BaseType& proje )
112 : proj_( proje )
113 {}
114
116 ~BoundaryProjectionWrapper () {}
117
119 CoordinateType operator() (const CoordinateType& global) const
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
139
140 public:
141 typedef typename Base::CoordinateType CoordinateType;
143
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
170 const BoundarySegment &boundarySegment () const
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>
238 struct CircleBoundaryProjection : public DuneBoundaryProjection< dimworld >
239 {
241 typedef FieldVector< double, dimworld> CoordinateType;
242
244 CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
245 : radius_( radius ) {}
246
248 virtual ~CircleBoundaryProjection() {}
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
Base class for grid boundary segments of arbitrary geometry.
Definition: boundaryprojection.hh:132
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
BoundarySegmentWrapper(const GeometryType &type, const std::vector< CoordinateType > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
Definition: boundaryprojection.hh:152
typename Base::value_type value_type
The type of the elements stored in the vector.
Definition: fvector.hh:112
static constexpr int dimension
The size of this vector.
Definition: fvector.hh:106
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:357
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,...)
Definition: exceptions.hh:314
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:32
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:51
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
virtual ~DuneBoundaryProjection()
destructor
Definition: boundaryprojection.hh:43
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:41
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Oct 20, 22:40, 2025)