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