3#ifndef DUNE_DGF_PROJECTIONBLOCK_HH
4#define DUNE_DGF_PROJECTIONBLOCK_HH
8#include <dune/grid/common/boundaryprojection.hh>
9#include <dune/grid/io/file/dgfparser/blocks/basic.hh>
25 friend std::ostream &operator<< ( std::ostream &,
const Token & );
30 defaultKeyword, functionKeyword, segmentKeyword,
31 sqrtKeyword, sinKeyword, cosKeyword, piKeyword,
34 openingParen, closingParen, openingBracket, closingBracket, normDelim,
35 additiveOperator, multiplicativeOperator, powerOperator,
44 void setSymbol (
const Type &t,
char c )
51 friend std::ostream &operator<< ( std::ostream &,
const Token & );
56 typedef std::shared_ptr< Expression > ExpressionPointer;
57 typedef std::pair< ExpressionPointer, std::string > ExpressionPair ;
60 template<
int dimworld >
61 class BoundaryProjection;
63 void registerProjectionFactory(
const int dimworld );
65 static const char* blockId() {
return "Projection"; }
67 ProjectionBlock ( std::istream &in,
int dimworld );
69 template<
int dimworld >
70 const DuneBoundaryProjection< dimworld > *defaultProjection ()
const
72 if( defaultFunction_.first )
74 return new BoundaryProjection< dimworld >( defaultFunction_ );
80 size_t numBoundaryProjections ()
const
82 return boundaryFunctions_.size();
85 const std::vector< unsigned int > &boundaryFace (
const size_t i )
const
87 assert( i < numBoundaryProjections() );
88 return boundaryFunctions_[ i ].first;
91 template<
int dimworld >
92 const DuneBoundaryProjection< dimworld > *boundaryProjection (
const size_t i )
const
94 assert( i < numBoundaryProjections() );
95 return new BoundaryProjection< dimworld >( boundaryFunctions_[ i ].second );
98 ExpressionPointer function (
const std::string &name )
const
100 const FunctionMap::const_iterator it = functions_.find( name );
101 return (it != functions_.end() ? it->second.first : 0);
104 ExpressionPair lastFunctionInserted ()
const
106 assert( ! functions_.empty() );
107 return functions_.begin()->second;
110 static ProjectionBlock::ExpressionPair createExpression(
const std::string& funcexpr,
const int dimworld )
112 std::stringstream str;
113 str << blockId() << std::endl;
114 str << funcexpr << std::endl;
115 str <<
"#" << std::endl;
116 ProjectionBlock problock( str, dimworld );
117 return problock.lastFunctionInserted();
123 void parseFunction (
const std::string& exprname );
124 ExpressionPointer parseBasicExpression (
const std::string &variableName );
125 ExpressionPointer parsePostfixExpression (
const std::string &variableName );
126 ExpressionPointer parseUnaryExpression (
const std::string &variableName );
127 ExpressionPointer parsePowerExpression (
const std::string &variableName );
128 ExpressionPointer parseMultiplicativeExpression (
const std::string &variableName );
129 ExpressionPointer parseExpression (
const std::string &variableName );
130 void parseDefault ();
131 void parseSegment ();
133 void matchToken (
const Token::Type &type,
const std::string &message );
136 static char lowerCase (
char c )
138 return ((c >=
'A') && (c <=
'Z') ? c + (
'a' -
'A') : c);
142 typedef std::map< std::string, ExpressionPair > FunctionMap;
143 typedef std::pair< std::vector< unsigned int >, ExpressionPair > BoundaryFunction;
148 FunctionMap functions_;
149 ExpressionPair defaultFunction_;
150 std::vector< BoundaryFunction > boundaryFunctions_;
154 std::ostream &operator<< ( std::ostream &out,
const ProjectionBlock::Token &token );
157 struct ProjectionBlock::Expression
159 typedef std::vector< double > Vector;
161 virtual ~Expression ()
164 virtual void evaluate (
const Vector &argument, Vector &result )
const = 0;
168 template<
int dimworld >
169 class ProjectionBlock::BoundaryProjection
170 :
public DuneBoundaryProjection< dimworld >
172 typedef DuneBoundaryProjection< dimworld > Base;
173 typedef BoundaryProjection < dimworld > This;
174 typedef typename Base :: ObjectStreamType ObjectStreamType;
179 BoundaryProjection (
const ExpressionPair& exprpair )
180 : expression_( exprpair.first ),
181 expressionName_( exprpair.second )
184 BoundaryProjection( ObjectStreamType& buffer )
187 buffer.read( (
char *) &size,
sizeof(
int) );
188 expressionName_.resize( size );
189 buffer.read( (
char *) expressionName_.c_str(), size );
190 expression_ = ProjectionBlock::createExpression( expressionName_, dimworld ).first;
193 virtual CoordinateType operator() (
const CoordinateType &global )
const override
195 std::vector< double > x( dimworld );
196 for(
int i = 0; i < dimworld; ++i )
197 x[ i ] = global[ i ];
198 std::vector< double > y;
199 expression_->evaluate( x, y );
200 CoordinateType result;
201 for(
int i = 0; i < dimworld; ++i )
202 result[ i ] = y[ i ];
207 virtual void backup( std::stringstream& buffer )
const override
209 buffer.write( (
const char *) &key(),
sizeof(
int ));
210 int size = expressionName_.size();
211 buffer.write( (
const char *) &size,
sizeof(
int) );
212 buffer.write( expressionName_.c_str(), size );
215 static void registerFactory()
219 key() = Base::template registerFactory< This >();
230 ExpressionPointer expression_;
231 std::string expressionName_;
234 inline void ProjectionBlock::registerProjectionFactory(
const int dimworld )
236 if( dimworld == 3 ) {
237 BoundaryProjection< 3 > :: registerFactory();
239 else if ( dimworld == 2 ) {
240 BoundaryProjection< 2 > :: registerFactory();
242 else if ( dimworld == 1 ) {
243 BoundaryProjection< 1 > :: registerFactory();
246 DUNE_THROW(NotImplemented,
"ProjectionBlock::registerProjectionFactory not implemented for dimworld = " << dimworld);
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
constexpr GeometryType line
GeometryType representing a line.
Definition: type.hh:803
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:401
Dune namespace.
Definition: alignedallocator.hh:14
FieldVector< double, dimworld > CoordinateType
type of coordinate vector
Definition: boundaryprojection.hh:40