dune-grid  2.9.0
io/file/dgfparser/blocks/projection.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_DGF_PROJECTIONBLOCK_HH
6 #define DUNE_DGF_PROJECTIONBLOCK_HH
7 
8 #include <map>
9 
12 
13 namespace Dune
14 {
15 
16  namespace dgf
17  {
18 
19  // ProjectionBlock
20  // ---------------
21 
23  : public BasicBlock
24  {
25  struct Token
26  {
27  friend std::ostream &operator<< ( std::ostream &, const Token & );
28 
29  enum Type
30  {
31  string, number,
32  defaultKeyword, functionKeyword, segmentKeyword,
33  sqrtKeyword, sinKeyword, cosKeyword, piKeyword,
34  comma,
35  equals,
36  openingParen, closingParen, openingBracket, closingBracket, normDelim,
37  additiveOperator, multiplicativeOperator, powerOperator,
38  endOfLine
39  };
40 
41  Type type;
42  char symbol;
43  std::string literal;
44  double value;
45 
46  void setSymbol ( const Type &t, char c )
47  {
48  type = t;
49  symbol = c;
50  }
51  };
52 
53  friend std::ostream &operator<< ( std::ostream &, const Token & );
54 
55  public:
56  struct Expression;
57 
58  typedef std::shared_ptr< Expression > ExpressionPointer;
59  typedef std::pair< ExpressionPointer, std::string > ExpressionPair ;
60 
61  private:
62  template< int dimworld >
63  class BoundaryProjection;
64 
65  void registerProjectionFactory( const int dimworld );
66 
67  static const char* blockId() { return "Projection"; }
68  public:
69  ProjectionBlock ( std::istream &in, int dimworld );
70 
71  template< int dimworld >
73  {
74  if( defaultFunction_.first )
75  {
76  return new BoundaryProjection< dimworld >( defaultFunction_ );
77  }
78  else
79  return 0;
80  }
81 
82  size_t numBoundaryProjections () const
83  {
84  return boundaryFunctions_.size();
85  }
86 
87  const std::vector< unsigned int > &boundaryFace ( const size_t i ) const
88  {
89  assert( i < numBoundaryProjections() );
90  return boundaryFunctions_[ i ].first;
91  }
92 
93  template< int dimworld >
95  {
96  assert( i < numBoundaryProjections() );
97  return new BoundaryProjection< dimworld >( boundaryFunctions_[ i ].second );
98  }
99 
100  ExpressionPointer function ( const std::string &name ) const
101  {
102  const FunctionMap::const_iterator it = functions_.find( name );
103  return (it != functions_.end() ? it->second.first : 0);
104  }
105 
107  {
108  assert( ! functions_.empty() );
109  return functions_.begin()->second;
110  }
111 
112  static ProjectionBlock::ExpressionPair createExpression( const std::string& funcexpr, const int dimworld )
113  {
114  std::stringstream str;
115  str << blockId() << std::endl;
116  str << funcexpr << std::endl;
117  str << "#" << std::endl;
118  ProjectionBlock problock( str, dimworld );
119  return problock.lastFunctionInserted();
120  }
121 
122 
123 
124  private:
125  void parseFunction ( const std::string& exprname );
126  ExpressionPointer parseBasicExpression ( const std::string &variableName );
127  ExpressionPointer parsePostfixExpression ( const std::string &variableName );
128  ExpressionPointer parseUnaryExpression ( const std::string &variableName );
129  ExpressionPointer parsePowerExpression ( const std::string &variableName );
130  ExpressionPointer parseMultiplicativeExpression ( const std::string &variableName );
131  ExpressionPointer parseExpression ( const std::string &variableName );
132  void parseDefault ();
133  void parseSegment ();
134 
135  void matchToken ( const Token::Type &type, const std::string &message );
136  void nextToken ();
137 
138  static char lowerCase ( char c )
139  {
140  return ((c >= 'A') && (c <= 'Z') ? c + ('a' - 'A') : c);
141  }
142 
143  protected:
144  typedef std::map< std::string, ExpressionPair > FunctionMap;
145  typedef std::pair< std::vector< unsigned int >, ExpressionPair > BoundaryFunction;
146 
147  using BasicBlock::line;
148 
149  Token token;
152  std::vector< BoundaryFunction > boundaryFunctions_;
153  };
154 
155 
156  std::ostream &operator<< ( std::ostream &out, const ProjectionBlock::Token &token );
157 
158 
160  {
161  typedef std::vector< double > Vector;
162 
163  virtual ~Expression ()
164  {}
165 
166  virtual void evaluate ( const Vector &argument, Vector &result ) const = 0;
167  };
168 
169 
170  template< int dimworld >
171  class ProjectionBlock::BoundaryProjection
172  : public DuneBoundaryProjection< dimworld >
173  {
175  typedef BoundaryProjection < dimworld > This;
176  typedef typename Base :: ObjectStreamType ObjectStreamType;
177 
178  public:
179  typedef typename Base::CoordinateType CoordinateType;
180 
181  BoundaryProjection ( const ExpressionPair& exprpair )
182  : expression_( exprpair.first ),
183  expressionName_( exprpair.second )
184  {}
185 
186  BoundaryProjection( ObjectStreamType& buffer )
187  {
188  int size = 0;
189  buffer.read( (char *) &size, sizeof(int) );
190  expressionName_.resize( size );
191  buffer.read( (char *) expressionName_.c_str(), size );
192  expression_ = ProjectionBlock::createExpression( expressionName_, dimworld ).first;
193  }
194 
195  virtual CoordinateType operator() ( const CoordinateType &global ) const override
196  {
197  std::vector< double > x( dimworld );
198  for( int i = 0; i < dimworld; ++i )
199  x[ i ] = global[ i ];
200  std::vector< double > y;
201  expression_->evaluate( x, y );
202  CoordinateType result;
203  for( int i = 0; i < dimworld; ++i )
204  result[ i ] = y[ i ];
205  return result;
206  }
207 
208  // backup name of expression that should allow to recreate this class
209  virtual void backup( std::stringstream& buffer ) const override
210  {
211  buffer.write( (const char *) &key(), sizeof( int ));
212  int size = expressionName_.size();
213  buffer.write( (const char *) &size, sizeof(int) );
214  buffer.write( expressionName_.c_str(), size );
215  }
216 
217  static void registerFactory()
218  {
219  if( key() < 0 )
220  {
221  key() = Base::template registerFactory< This >();
222  }
223  }
224 
225  protected:
226  static int& key ()
227  {
228  static int k = -1;
229  return k;
230  }
231 
232  ExpressionPointer expression_;
233  std::string expressionName_;
234  };
235 
236  inline void ProjectionBlock::registerProjectionFactory( const int dimworld )
237  {
238  if( dimworld == 3 ) {
239  BoundaryProjection< 3 > :: registerFactory();
240  }
241  else if ( dimworld == 2 ) {
242  BoundaryProjection< 2 > :: registerFactory();
243  }
244  else if ( dimworld == 1 ) {
245  BoundaryProjection< 1 > :: registerFactory();
246  }
247  else {
248  DUNE_THROW(NotImplemented,"ProjectionBlock::registerProjectionFactory not implemented for dimworld = " << dimworld);
249  }
250  }
251 
252  }
253 
254 }
255 
256 #endif // #ifndef DUNE_DGF_PROJECTIONBLOCK_HH
Include standard header files.
Definition: agrid.hh:60
std::ostream & operator<<(std::ostream &out, const IntervalBlock::Interval &interval)
Definition: interval.hh:123
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:33
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:42
BaseType ::ObjectStreamType ObjectStreamType
Definition: boundaryprojection.hh:36
Definition: basic.hh:31
std::stringstream line
Definition: basic.hh:47
Definition: io/file/dgfparser/blocks/projection.hh:24
static ProjectionBlock::ExpressionPair createExpression(const std::string &funcexpr, const int dimworld)
Definition: io/file/dgfparser/blocks/projection.hh:112
const DuneBoundaryProjection< dimworld > * boundaryProjection(const size_t i) const
Definition: io/file/dgfparser/blocks/projection.hh:94
size_t numBoundaryProjections() const
Definition: io/file/dgfparser/blocks/projection.hh:82
friend std::ostream & operator<<(std::ostream &, const Token &)
Definition: projection.cc:838
std::shared_ptr< Expression > ExpressionPointer
Definition: io/file/dgfparser/blocks/projection.hh:56
ProjectionBlock(std::istream &in, int dimworld)
Definition: projection.cc:447
FunctionMap functions_
Definition: io/file/dgfparser/blocks/projection.hh:150
ExpressionPair lastFunctionInserted() const
Definition: io/file/dgfparser/blocks/projection.hh:106
std::pair< ExpressionPointer, std::string > ExpressionPair
Definition: io/file/dgfparser/blocks/projection.hh:59
std::pair< std::vector< unsigned int >, ExpressionPair > BoundaryFunction
Definition: io/file/dgfparser/blocks/projection.hh:145
Token token
Definition: io/file/dgfparser/blocks/projection.hh:149
const DuneBoundaryProjection< dimworld > * defaultProjection() const
Definition: io/file/dgfparser/blocks/projection.hh:72
std::vector< BoundaryFunction > boundaryFunctions_
Definition: io/file/dgfparser/blocks/projection.hh:152
const std::vector< unsigned int > & boundaryFace(const size_t i) const
Definition: io/file/dgfparser/blocks/projection.hh:87
std::map< std::string, ExpressionPair > FunctionMap
Definition: io/file/dgfparser/blocks/projection.hh:144
ExpressionPair defaultFunction_
Definition: io/file/dgfparser/blocks/projection.hh:151
Definition: io/file/dgfparser/blocks/projection.hh:160
virtual ~Expression()
Definition: io/file/dgfparser/blocks/projection.hh:163
virtual void evaluate(const Vector &argument, Vector &result) const =0
std::vector< double > Vector
Definition: io/file/dgfparser/blocks/projection.hh:161