Dune Core Modules (2.5.0)

projection.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_DGF_PROJECTIONBLOCK_HH
4#define DUNE_DGF_PROJECTIONBLOCK_HH
5
6#include <map>
7
8#include <dune/grid/common/boundaryprojection.hh>
9#include <dune/grid/io/file/dgfparser/blocks/basic.hh>
10
11namespace Dune
12{
13
14 namespace dgf
15 {
16
17 // ProjectionBlock
18 // ---------------
19
20 class ProjectionBlock
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 >
64 const DuneBoundaryProjection< dimworld > *defaultProjection () const
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 >
84 const DuneBoundaryProjection< dimworld > *boundaryProjection ( const size_t i ) const
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_;
123 const Expression *defaultFunction_;
124 std::vector< BoundaryFunction > boundaryFunctions_;
125 };
126
127
128 std::ostream &operator<< ( std::ostream &out, const ProjectionBlock::Token &token );
129
130
131 struct ProjectionBlock::Expression
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 {
146 typedef DuneBoundaryProjection< dimworld > Base;
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
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:441
Dune namespace.
Definition: alignment.hh:11
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:26
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)