dune-grid  2.7.1
boundaryprojection.hh
Go to the documentation of this file.
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 
18 
19 namespace Dune
20 {
23  template <int dimworld>
24  struct DuneBoundaryProjection;
25 
28  template <int dimworld>
30  : public BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > >
31  {
35 
36  using BaseType :: restore;
38 
40  typedef FieldVector< double, dimworld> CoordinateType;
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 >
100  : public DuneBoundaryProjection< dimworld >
101  {
102  protected:
104  const BaseType& proj_;
105  public:
108 
109  // constructor taking other projection
111  : proj_( proje )
112  {}
113 
116 
119  {
120  return proj_( global );
121  }
122  };
123 
124  // BoundarySegmentWrapper
125  // ----------------------
126 
128  template< int dim, int dimworld >
130  : public DuneBoundaryProjection< dimworld >
131  {
134 
136 
137  typedef MultiLinearGeometry<typename Base::CoordinateType::value_type,dim-1,dimworld> FaceMapping;
138 
139  public:
142 
152  const std::vector< CoordinateType > &vertices,
153  const std::shared_ptr< BoundarySegment > &boundarySegment )
154  : faceMapping_( FaceMapping( type, vertices ) ),
155  boundarySegment_( boundarySegment )
156  {}
157 
159  : faceMapping_( readFaceMapping( buffer ) ),
160  boundarySegment_( BoundarySegment::restore( buffer ).release() )
161  {
162  }
163 
165  {
166  return boundarySegment() ( faceMapping_.local( global ) );
167  }
168 
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>
238  {
240  typedef FieldVector< double, dimworld> CoordinateType;
241 
243  CircleBoundaryProjection(const double radius = std::sqrt( (double)dimworld ))
244  : radius_( radius ) {}
245 
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.
Describes the parallel communication interface class for MessageBuffers and DataHandles.
Include standard header files.
Definition: agrid.hh:59
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:180
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:31
DuneBoundaryProjection< dimworld > ThisType
Definition: boundaryprojection.hh:32
virtual void backup(ObjectStreamType &buffer) const
write DuneBoundaryProjection's data to stream buffer
Definition: boundaryprojection.hh:50
static std::unique_ptr< ThisType > restoreFromBuffer(MessageBufferIF< BufferImp > &buffer)
Definition: boundaryprojection.hh:83
virtual CoordinateType operator()(const CoordinateType &global) const =0
projection operator projection a global coordinate
void toBuffer(BufferImp &buffer) const
Definition: boundaryprojection.hh:56
virtual ~DuneBoundaryProjection()
destructor
Definition: boundaryprojection.hh:42
static std::unique_ptr< ThisType > restoreFromBuffer(BufferImp &buffer)
Definition: boundaryprojection.hh:76
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:40
void toBuffer(MessageBufferIF< BufferImp > &buffer) const
Definition: boundaryprojection.hh:63
BoundarySegmentBackupRestore< DuneBoundaryProjection< dimworld > > BaseType
Definition: boundaryprojection.hh:33
BaseType ::ObjectStreamType ObjectStreamType
Definition: boundaryprojection.hh:34
Definition: boundaryprojection.hh:101
BoundaryProjectionWrapper(const BaseType &proje)
Definition: boundaryprojection.hh:110
const BaseType & proj_
Definition: boundaryprojection.hh:104
DuneBoundaryProjection< dimworld > BaseType
Definition: boundaryprojection.hh:103
CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:118
BaseType ::CoordinateType CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:107
~BoundaryProjectionWrapper()
destructor
Definition: boundaryprojection.hh:115
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
const BoundarySegment & boundarySegment() const
Definition: boundaryprojection.hh:169
BoundarySegmentWrapper(ObjectStreamType &buffer)
Definition: boundaryprojection.hh:158
Dune::BoundarySegment< dim, dimworld > BoundarySegment
Definition: boundaryprojection.hh:141
static void registerFactory()
Definition: boundaryprojection.hh:195
static int & key()
Definition: boundaryprojection.hh:204
FaceMapping readFaceMapping(ObjectStreamType &buffer)
Definition: boundaryprojection.hh:210
Base::CoordinateType CoordinateType
Definition: boundaryprojection.hh:140
Definition: boundaryprojection.hh:238
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:240
CircleBoundaryProjection(const double radius=std::sqrt((double) dimworld))
constructor taking radius of circle (default = sqrt( dimworld ) )
Definition: boundaryprojection.hh:243
virtual CoordinateType operator()(const CoordinateType &global) const
projection operator projection a global coordinate
Definition: boundaryprojection.hh:250
virtual ~CircleBoundaryProjection()
destructor
Definition: boundaryprojection.hh:247
const double radius_
radius of circ
Definition: boundaryprojection.hh:262
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:92
Definition: boundarysegment.hh:39
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:57
std::stringstream ObjectStreamType
Definition: boundarysegment.hh:42
static int registerFactory()
Definition: boundarysegment.hh:68
Communication message buffer interface. This class describes the interface for reading and writing da...
Definition: datahandleif.hh:31
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:43
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:57