Dune Core Modules (2.7.1)

boundaryprojection.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_BOUNDARYPROJECTION_HH
4 #define DUNE_BOUNDARYPROJECTION_HH
5 
6 //- system includes
7 #include <cmath>
8 #include <memory>
9 
10 //- Dune includes
11 #include <dune/common/fvector.hh>
12 
13 #include <dune/geometry/multilineargeometry.hh>
14 
17 #include <dune/grid/io/file/gmshreader.hh>
18 
19 namespace Dune
20 {
23  template <int dimworld>
24  struct DuneBoundaryProjection;
25 
28  template <int dimworld>
30  : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
31  {
33  typedef BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType;
34  typedef typename BaseType :: ObjectStreamType ObjectStreamType;
35 
36  using BaseType :: restore;
37  using BaseType :: registerFactory;
38 
43 
45  virtual CoordinateType operator() (const CoordinateType& global) const = 0;
46 
50  virtual void backup( ObjectStreamType& buffer ) const
51  {
52  DUNE_THROW(NotImplemented,"DuneBoundaryProjection::backup not overloaded!");
53  }
54 
55  template <class BufferImp>
56  void toBuffer( BufferImp& buffer ) const
57  {
58  MessageBufferIF< BufferImp > buf( buffer );
59  toBuffer( buf );
60  }
61 
62  template <class BufferImp>
63  void toBuffer( MessageBufferIF< BufferImp > & buffer ) const
64  {
65  ObjectStreamType str;
66  // call virtual interface backup
67  backup( str );
68  std::string data = str.str();
69  const size_t size = data.size();
70  buffer.write( size );
71  for( size_t i=0; i<size; ++i )
72  buffer.write( data[ i ] );
73  }
74 
75  template <class BufferImp>
76  static std::unique_ptr< ThisType > restoreFromBuffer( BufferImp & buffer )
77  {
78  MessageBufferIF< BufferImp > buf( buffer );
79  return restoreFromBuffer( buf );
80  }
81 
82  template <class BufferImp>
83  static std::unique_ptr< ThisType > restoreFromBuffer( MessageBufferIF< BufferImp > & buffer )
84  {
85  std::string data;
86  size_t size = 0;
87  buffer.read( size );
88  data.resize( size );
89  for( size_t i=0; i<size; ++i )
90  buffer.read( data[ i ] );
91 
92  ObjectStreamType str;
93  str.write( data.c_str(), size );
94  return BaseType::restore( str );
95  }
96  };
97 
98  template < int dimworld >
99  class BoundaryProjectionWrapper
100  : public DuneBoundaryProjection< dimworld >
101  {
102  protected:
103  typedef DuneBoundaryProjection< dimworld > BaseType;
104  const BaseType& proj_;
105  public:
107  typedef typename BaseType :: CoordinateType CoordinateType;
108 
109  // constructor taking other projection
110  BoundaryProjectionWrapper( const BaseType& proje )
111  : proj_( proje )
112  {}
113 
115  ~BoundaryProjectionWrapper () {}
116 
118  CoordinateType operator() (const CoordinateType& global) const
119  {
120  return proj_( global );
121  }
122  };
123 
124  // BoundarySegmentWrapper
125  // ----------------------
126 
128  template< int dim, int dimworld >
130  : public DuneBoundaryProjection< dimworld >
131  {
134 
135  typedef typename Base :: ObjectStreamType ObjectStreamType;
136 
137  typedef MultiLinearGeometry<typename Base::CoordinateType::value_type,dim-1,dimworld> FaceMapping;
138 
139  public:
140  typedef typename Base::CoordinateType CoordinateType;
142 
152  const std::vector< CoordinateType > &vertices,
153  const std::shared_ptr< BoundarySegment > &boundarySegment )
154  : faceMapping_( FaceMapping( type, vertices ) ),
155  boundarySegment_( boundarySegment )
156  {}
157 
158  BoundarySegmentWrapper( ObjectStreamType& buffer )
159  : faceMapping_( readFaceMapping( buffer ) ),
160  boundarySegment_( BoundarySegment::restore( buffer ).release() )
161  {
162  }
163 
165  {
166  return boundarySegment() ( faceMapping_.local( global ) );
167  }
168 
169  const BoundarySegment &boundarySegment () const
170  {
171  return *boundarySegment_;
172  }
173 
174  void backup( ObjectStreamType& buffer ) const
175  {
176  // write identifier key first
177  buffer.write( (const char *) &key(), sizeof(int));
178  // now all data
179  GeometryType type = faceMapping_.type();
180  buffer.write( (const char *) &type, sizeof(GeometryType) );
181 
182  int corners = faceMapping_.corners() ;
183  buffer.write( (const char *) &corners, sizeof(int) );
184 
185  CoordinateType corner( 0 );
186  for( int i=0; i<corners; ++i )
187  {
188  corner = faceMapping_.corner( i );
189  buffer.write( (const char *) &corner[ 0 ], sizeof(double)*CoordinateType::dimension );
190  }
191 
192  boundarySegment_->backup( buffer );
193  }
194 
195  static void registerFactory()
196  {
197  if( key() < 0 )
198  {
199  key() = Base::template registerFactory< ThisType >();
200  }
201  }
202 
203  protected:
204  static int& key()
205  {
206  static int k = -1;
207  return k;
208  }
209 
210  FaceMapping readFaceMapping( ObjectStreamType& buffer )
211  {
212  GeometryType type;
213  buffer.read( (char *) &type, sizeof(GeometryType) );
214  int corners = 0;
215  buffer.read( (char *) &corners, sizeof(int) );
216  std::vector< CoordinateType > vertices( corners, CoordinateType(0) );
217  for( int i=0; i<corners; ++i )
218  {
219  buffer.read( (char *) &vertices[ i ][ 0 ], sizeof(double)*CoordinateType::dimension );
220  }
221  return FaceMapping( type, vertices );
222  }
223 
224  private:
225  FaceMapping faceMapping_;
226  const std::shared_ptr< BoundarySegment > boundarySegment_;
227  };
228 
229 
230 
232  //
233  // Example of boundary projection projection to a circle
234  //
236  template <int dimworld>
237  struct CircleBoundaryProjection : public DuneBoundaryProjection< dimworld >
238  {
240  typedef FieldVector< double, dimworld> CoordinateType;
241 
243  CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
244  : radius_( radius ) {}
245 
247  virtual ~CircleBoundaryProjection() {}
248 
250  virtual CoordinateType operator() (const CoordinateType& global) const
251  {
252  CoordinateType prj( global );
253  // get adjustment factor
254  const double factor = radius_ / global.two_norm();
255  // adjust
256  prj *= factor;
257  return prj;
258  }
259 
260  protected:
262  const double radius_;
263  };
264 
265 } // end namespace
266 
267 #endif // #ifndef DUNE_BOUNDARYPROJECTION_HH
Base class for grid boundary segments of arbitrary geometry.
Definition: boundaryprojection.hh:131
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:164
void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:174
BoundarySegmentWrapper(const GeometryType &type, const std::vector< CoordinateType > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
Definition: boundaryprojection.hh:151
@ dimension
The size of this vector.
Definition: fvector.hh:103
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:279
Communication message buffer interface. This class describes the interface for reading and writing da...
Definition: datahandleif.hh:31
generic geometry implementation based on corner coordinates
Definition: multilineargeometry.hh:179
Dune::GeometryType type() const
obtain the name of the reference element
Definition: multilineargeometry.hh:261
GlobalCoordinate corner(int i) const
obtain coordinates of the i-th corner
Definition: multilineargeometry.hh:267
int corners() const
obtain number of corners of the corresponding reference element
Definition: multilineargeometry.hh:264
Default exception for dummy implementations.
Definition: exceptions.hh:261
Describes the parallel communication interface class for MessageBuffers and DataHandles.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:180
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:216
Dune namespace.
Definition: alignedallocator.hh:14
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:92
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:31
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:50
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
virtual ~DuneBoundaryProjection()
destructor
Definition: boundaryprojection.hh:42
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:40
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)