DUNE-FEM (unstable)

projection.hh
1// SPDX-FileCopyrightText: Copyright © 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
10#include <dune/grid/common/boundaryprojection.hh>
11#include <dune/grid/io/file/dgfparser/blocks/basic.hh>
12
13namespace Dune
14{
15
16 namespace dgf
17 {
18
19 // ProjectionBlock
20 // ---------------
21
22 class ProjectionBlock
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 >
72 const DuneBoundaryProjection< dimworld > *defaultProjection () const
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 >
94 const DuneBoundaryProjection< dimworld > *boundaryProjection ( const size_t i ) const
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
106 ExpressionPair lastFunctionInserted () const
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;
150 FunctionMap functions_;
151 ExpressionPair defaultFunction_;
152 std::vector< BoundaryFunction > boundaryFunctions_;
153 };
154
155
156 std::ostream &operator<< ( std::ostream &out, const ProjectionBlock::Token &token );
157
158
159 struct ProjectionBlock::Expression
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 {
174 typedef DuneBoundaryProjection< dimworld > Base;
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
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:498
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:587
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:42
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)