5#ifndef DUNE_DGF_PROJECTIONBLOCK_HH
6#define DUNE_DGF_PROJECTIONBLOCK_HH
10#include <dune/grid/common/boundaryprojection.hh>
11#include <dune/grid/io/file/dgfparser/blocks/basic.hh>
27 friend std::ostream &operator<< ( std::ostream &,
const Token & );
32 defaultKeyword, functionKeyword, segmentKeyword,
33 sqrtKeyword, sinKeyword, cosKeyword, piKeyword,
36 openingParen, closingParen, openingBracket, closingBracket, normDelim,
37 additiveOperator, multiplicativeOperator, powerOperator,
46 void setSymbol (
const Type &t,
char c )
53 friend std::ostream &operator<< ( std::ostream &,
const Token & );
58 typedef std::shared_ptr< Expression > ExpressionPointer;
59 typedef std::pair< ExpressionPointer, std::string > ExpressionPair ;
62 template<
int dimworld >
63 class BoundaryProjection;
65 void registerProjectionFactory(
const int dimworld );
67 static const char* blockId() {
return "Projection"; }
69 ProjectionBlock ( std::istream &in,
int dimworld );
71 template<
int dimworld >
72 const DuneBoundaryProjection< dimworld > *defaultProjection ()
const
74 if( defaultFunction_.first )
76 return new BoundaryProjection< dimworld >( defaultFunction_ );
82 size_t numBoundaryProjections ()
const
84 return boundaryFunctions_.size();
87 const std::vector< unsigned int > &boundaryFace (
const size_t i )
const
89 assert( i < numBoundaryProjections() );
90 return boundaryFunctions_[ i ].first;
93 template<
int dimworld >
94 const DuneBoundaryProjection< dimworld > *boundaryProjection (
const size_t i )
const
96 assert( i < numBoundaryProjections() );
97 return new BoundaryProjection< dimworld >( boundaryFunctions_[ i ].second );
100 ExpressionPointer function (
const std::string &name )
const
102 const FunctionMap::const_iterator it = functions_.find( name );
103 return (it != functions_.end() ? it->second.first : 0);
106 ExpressionPair lastFunctionInserted ()
const
108 assert( ! functions_.empty() );
109 return functions_.begin()->second;
112 static ProjectionBlock::ExpressionPair createExpression(
const std::string& funcexpr,
const int dimworld )
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();
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 ();
135 void matchToken (
const Token::Type &type,
const std::string &message );
138 static char lowerCase (
char c )
140 return ((c >=
'A') && (c <=
'Z') ? c + (
'a' -
'A') : c);
144 typedef std::map< std::string, ExpressionPair > FunctionMap;
145 typedef std::pair< std::vector< unsigned int >, ExpressionPair > BoundaryFunction;
150 FunctionMap functions_;
151 ExpressionPair defaultFunction_;
152 std::vector< BoundaryFunction > boundaryFunctions_;
156 std::ostream &operator<< ( std::ostream &out,
const ProjectionBlock::Token &token );
159 struct ProjectionBlock::Expression
161 typedef std::vector< double > Vector;
163 virtual ~Expression ()
166 virtual void evaluate (
const Vector &argument, Vector &result )
const = 0;
170 template<
int dimworld >
171 class ProjectionBlock::BoundaryProjection
172 :
public DuneBoundaryProjection< dimworld >
174 typedef DuneBoundaryProjection< dimworld > Base;
175 typedef BoundaryProjection < dimworld > This;
176 typedef typename Base :: ObjectStreamType ObjectStreamType;
181 BoundaryProjection (
const ExpressionPair& exprpair )
182 : expression_( exprpair.first ),
183 expressionName_( exprpair.second )
186 BoundaryProjection( ObjectStreamType& buffer )
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;
195 virtual CoordinateType operator() (
const CoordinateType &global )
const override
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 ];
209 virtual void backup( std::stringstream& buffer )
const override
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 );
217 static void registerFactory()
221 key() = Base::template registerFactory< This >();
232 ExpressionPointer expression_;
233 std::string expressionName_;
236 inline void ProjectionBlock::registerProjectionFactory(
const int dimworld )
238 if( dimworld == 3 ) {
239 BoundaryProjection< 3 > :: registerFactory();
241 else if ( dimworld == 2 ) {
242 BoundaryProjection< 2 > :: registerFactory();
244 else if ( dimworld == 1 ) {
245 BoundaryProjection< 1 > :: registerFactory();
248 DUNE_THROW(NotImplemented,
"ProjectionBlock::registerProjectionFactory not implemented for dimworld = " << dimworld);
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:513
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:402
Dune namespace.
Definition: alignedallocator.hh:13
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:42