dune-grid  2.2.1
io/file/dgfparser/blocks/projection.hh
Go to the documentation of this file.
1 #ifndef DUNE_DGF_PROJECTIONBLOCK_HH
2 #define DUNE_DGF_PROJECTIONBLOCK_HH
3 
4 #include <map>
5 
8 
9 namespace Dune
10 {
11 
12  namespace dgf
13  {
14 
15  // ProjectionBlock
16  // ---------------
17 
19  : public BasicBlock
20  {
21  struct Token
22  {
23  friend std::ostream &operator<< ( std::ostream &, const Token & );
24 
25  enum Type
26  {
27  string, number,
28  defaultKeyword, functionKeyword, segmentKeyword,
29  sqrtKeyword, sinKeyword, cosKeyword, piKeyword,
30  comma,
31  equals,
32  openingParen, closingParen, openingBracket, closingBracket, normDelim,
33  additiveOperator, multiplicativeOperator, powerOperator,
34  endOfLine
35  };
36 
37  Type type;
38  char symbol;
39  std::string literal;
40  double value;
41 
42  void setSymbol ( const Type &t, char c )
43  {
44  type = t;
45  symbol = c;
46  }
47  };
48 
49  friend std::ostream &operator<< ( std::ostream &, const Token & );
50 
51  public:
52  struct Expression;
53 
54  private:
55  template< int dimworld >
56  class BoundaryProjection;
57 
58  public:
59  ProjectionBlock ( std::istream &in, int dimworld );
60 
61  template< int dimworld >
63  {
64  if( defaultFunction_ != 0 )
66  else
67  return 0;
68  }
69 
70  size_t numBoundaryProjections () const
71  {
72  return boundaryFunctions_.size();
73  }
74 
75  const std::vector< unsigned int > &boundaryFace ( const size_t i ) const
76  {
77  assert( i < numBoundaryProjections() );
78  return boundaryFunctions_[ i ].first;
79  }
80 
81  template< int dimworld >
83  {
84  assert( i < numBoundaryProjections() );
85  return new BoundaryProjection< dimworld >( boundaryFunctions_[ i ].second );
86  }
87 
88  const Expression *function ( const std::string &name ) const
89  {
90  const FunctionMap::const_iterator it = functions_.find( name );
91  return (it != functions_.end() ? it->second : 0);
92  }
93 
94  private:
95  void parseFunction ();
96  const Expression *parseBasicExpression ( const std::string &variableName );
97  const Expression *parsePostfixExpression ( const std::string &variableName );
98  const Expression *parseUnaryExpression ( const std::string &variableName );
99  const Expression *parsePowerExpression ( const std::string &variableName );
100  const Expression *parseMultiplicativeExpression ( const std::string &variableName );
101  const Expression *parseExpression ( const std::string &variableName );
102  void parseDefault ();
103  void parseSegment ();
104 
105  void matchToken ( const Token::Type &type, const std::string &message );
106  void nextToken ();
107 
108  static char lowerCase ( char c )
109  {
110  return ((c >= 'A') && (c <= 'Z') ? c + ('a' - 'A') : c);
111  }
112 
113  protected:
114  typedef std::map< std::string, const Expression * > FunctionMap;
115  typedef std::pair< std::vector< unsigned int >, const Expression * > BoundaryFunction;
116 
117  using BasicBlock::line;
118 
119  Token token;
122  std::vector< BoundaryFunction > boundaryFunctions_;
123  };
124 
125 
126  std::ostream &operator<< ( std::ostream &out, const ProjectionBlock::Token &token );
127 
128 
130  {
131  typedef std::vector< double > Vector;
132 
133  virtual ~Expression ()
134  {}
135 
136  virtual void evaluate ( const Vector &argument, Vector &result ) const = 0;
137  };
138 
139 
140  template< int dimworld >
142  : public DuneBoundaryProjection< dimworld >
143  {
145 
146  public:
148 
149  BoundaryProjection ( const Expression *expression )
150  : expression_( expression )
151  {}
152 
153  virtual CoordinateType operator() ( const CoordinateType &global ) const
154  {
155  std::vector< double > x( dimworld );
156  for( int i = 0; i < dimworld; ++i )
157  x[ i ] = global[ i ];
158  std::vector< double > y;
159  expression_->evaluate( x, y );
160  CoordinateType result;
161  for( int i = 0; i < dimworld; ++i )
162  result[ i ] = y[ i ];
163  return result;
164  }
165 
166  private:
167  const Expression *expression_;
168  };
169 
170  }
171 
172 }
173 
174 #endif // #ifndef DUNE_DGF_PROJECTIONBLOCK_HH