41 #include <boost/lexical_cast.hpp>
42 #include <boost/algorithm/string/trim.hpp>
43 #include <boost/random/detail/seed.hpp>
46 #include <boost/preprocessor/cat.hpp>
50 #define NEKTAR_MATH_NAME(x) BOOST_PP_CAT(_, x)
52 #define NEKTAR_MATH_NAME(x) x
55 #if( BOOST_VERSION / 100 % 1000 >= 36 )
56 using namespace boost::spirit::classic;
58 using namespace boost::spirit;
67 namespace LibUtilities
99 return (arg > 0.0) - (arg < 0.0);
109 boost::variate_generator<
111 boost::normal_distribution<>
112 > _normal(rng, boost::normal_distribution<>(0, sigma) );
154 template <
typename ScannerT>
157 expression = logical_or;
159 logical_or = logical_and >> *( (root_node_d[str_p(
"||")] >> logical_and) );
161 logical_and = equality >> *( (root_node_d[str_p(
"&&")] >> equality) );
163 equality = lt_gt >> *( (root_node_d[str_p(
"==")] >> lt_gt) );
166 *( (root_node_d[str_p(
"<=")] >> add_sub)
167 | (root_node_d[str_p(
">=")] >> add_sub)
168 | (root_node_d[ch_p(
'<')] >> add_sub)
169 | (root_node_d[ch_p(
'>')] >> add_sub)
172 add_sub = mult_div >>
173 *( (root_node_d[ch_p(
'+')] >> mult_div)
174 | (root_node_d[ch_p(
'-')] >> mult_div)
177 mult_div = exponential >>
178 *( (root_node_d[ch_p(
'*')] >> exponential)
179 | (root_node_d[ch_p(
'/')] >> exponential)
182 exponential = factor >>
183 *( (root_node_d[ch_p(
'^')] >> factor) );
190 | inner_node_d[ch_p(
'(') >> expression >> ch_p(
')')]
191 | (root_node_d[ch_p(
'-')] >> factor);
193 parameter = leaf_node_d[ lexeme_d[
194 (alpha_p |
'_' |
'$') >> *(alnum_p |
'_' |
'$')
198 infix_node_d[inner_node_d[ch_p(
'(') >> expression >> *(
',' >> expression) >> ch_p(
')')]];
200 variable = leaf_node_d[ lexeme_d[
204 number = leaf_node_d[ lexeme_d[
208 constant = leaf_node_d[ lexeme_d[
212 op = eps_p( end_p |
"||" |
"&&" |
"==" |
"<=" |
">=" |
'<' |
'>' |
'+' |
'-' |
'*' |
'/' |
'^' |
')' |
',' );
220 AnalyticExpressionEvaluator::AnalyticExpressionEvaluator():
315 for (std::map<std::string, NekDouble>::const_iterator it = constants.begin(); it != constants.end(); ++it)
337 std::string errormsg(
"Attempt to add numerically different constants under the same name: ");
339 std::cout << errormsg << std::endl;
350 ASSERTL1(value != NULL,
"Constant variable not found: " + name);
357 for (std::map<std::string, NekDouble>::const_iterator it = params.begin(); it != params.end(); it++)
420 std::vector<std::string> variableNames;
421 parse((
char*) vlist.c_str(), ( *space_p >>
423 +(+graph_p)[push_back_a(variableNames)]
433 node_val_data_factory<NekDouble>,
434 std::string::const_iterator,
438 (expr.begin(), expr.end(), myGrammar, space_p);
440 ASSERTL1(parseInfo.full !=
false,
"Unable to fully parse function. Stopped just before: "
441 + std::string(parseInfo.stop, parseInfo.stop + 15));
444 throw std::runtime_error(
"Unable to fully parse function at: "
445 + std::string(parseInfo.stop, parseInfo.stop + 15));
460 for (
int i = 0; i < variableNames.size(); i++)
462 variableMap[variableNames[i]] = i;
473 ASSERTL1(stack.size() == 0,
"Constant expression yeilds non-empty execution stack. Bug in PrepareExecutionAsYouParse()");
475 int const_index =
AddConstant(std::string(
"EXPRESSION_") + boost::lexical_cast<std::string>(stackId), v.second);
476 stack.push_back ( makeStep<StoreConst>( 0, const_index ) );
495 ASSERTL1(
m_executionStack.size() > expression_id,
"unknown analytic expression, it must first be defined with DefineFunction(...)");
500 for (
int i = 0; i < stack.size(); i++)
502 (*stack[i]).run_once();
512 const int expression_id,
520 ASSERTL1(
m_executionStack.size() > expression_id,
"unknown analytic expression, it must first be defined with DefineFunction(...)");
541 for (
int i = 0; i < stack.size(); i++)
543 (*stack[i]).run_once();
556 ASSERTL1(
m_executionStack.size() > expression_id,
"unknown analytic expression, it must first be defined with DefineFunction(...)");
561 ASSERTL1(point.size() == variableMap.size(),
"The number of variables used to define this expression should match the point dimensionality.");
566 VariableMap::const_iterator it;
567 for (it = variableMap.begin(); it != variableMap.end(); ++it)
572 for (
int i = 0; i < stack.size(); i++)
574 (*stack[i]).run_once();
586 const int expression_id,
587 const Array<OneD, const NekDouble>& x,
588 const Array<OneD, const NekDouble>& y,
589 const Array<OneD, const NekDouble>& z,
590 const Array<OneD, const NekDouble>& t,
591 Array<OneD, NekDouble>& result)
595 std::vector<Array<OneD, const NekDouble> >points;
602 Evaluate(expression_id,points,result);
610 const int expression_id,
611 const std::vector<Array<OneD, const NekDouble> > points,
612 Array<OneD, NekDouble>& result)
616 const int num_points = points[0].num_elements();
618 "unknown analytic expression, it must first be defined "
619 "with DefineFunction(...)");
620 ASSERTL1(result.num_elements() >= num_points,
621 "destination array must have enough capacity to store "
622 "expression values at each given point");
630 const int max_chunk_size = 1024;
631 const int nvals = points.size();
632 const int chunk_size = (std::min)(max_chunk_size, num_points);
642 if (result.num_elements() < num_points)
644 result = Array<OneD, NekDouble>(num_points, 0.0);
648 int work_left = num_points;
651 const int this_chunk_size = (std::min)(work_left, 1024);
652 for (
int i = 0; i < this_chunk_size; i++)
654 for(
int j = 0; j < nvals; ++j)
656 m_variable[i+this_chunk_size*j] = points[j][offset + i];
659 for (
int i = 0; i < stack.size(); i++)
661 (*stack[i]).run_many(this_chunk_size);
663 for (
int i = 0; i < this_chunk_size; i++)
665 result[offset + i] =
m_state[i];
667 work_left -= this_chunk_size;
668 offset += this_chunk_size;
681 std::string valueStr(location->value.begin(), location->value.end());
682 boost::algorithm::trim(valueStr);
684 const parser_id parserID = location->value.id();
685 #if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
686 const int num_children = location->children.size();
691 ASSERTL1(num_children == 0,
"Illegal children under constant node: " + valueStr);
696 return std::make_pair(
true,
m_constant[it->second]);
700 ASSERTL1(num_children == 0,
"Illegal children under number node: " + valueStr);
701 return std::make_pair(
true, boost::lexical_cast<NekDouble>(valueStr.c_str()) );
705 ASSERTL1(num_children == 0,
"Illegal children under variable node: " + valueStr);
707 VariableMap::const_iterator it = variableMap.find(valueStr);
708 ASSERTL1(it != variableMap.end(),
"Unknown variable parsed: " + valueStr);
711 stack.push_back ( makeStep<StoreVar>( stateIndex, it->second ) );
712 return std::make_pair(
false, 0);
716 ASSERTL1(num_children == 0,
"Illegal children under parameter node: " + valueStr);
722 stack.push_back ( makeStep<StorePrm>( stateIndex, it->second ) );
723 return std::make_pair(
false, 0);
729 ASSERTL1(num_children == 1 || num_children == 2,
"Function " + valueStr +
" would like to have too few or too many arguments. This is not implemented yet");
731 if (location->children.size() == 1)
738 int const_index =
AddConstant(std::string(
"SUB_EXPR_") + boost::lexical_cast<std::string>(
m_constant.size()), v.second);
739 stack.push_back ( makeStep<StoreConst>( stateIndex, const_index ) );
740 stack.push_back ( makeStep<EvalAWGN>( stateIndex, stateIndex ) );
741 return std::make_pair(
false,0);
746 return std::make_pair(
true,
m_function[it->second](v.second) );
755 if (
true == v1.first &&
true == v2.first)
757 return std::make_pair(
true,
m_function2[it->second](v1.second, v2.second) );
766 stack.push_back ( makeStep<EvalAbs>( stateIndex, stateIndex) );
767 return std::make_pair(
false,0);
769 stack.push_back ( makeStep<EvalAsin>( stateIndex, stateIndex) );
770 return std::make_pair(
false,0);
772 stack.push_back ( makeStep<EvalAcos>( stateIndex, stateIndex ) );
773 return std::make_pair(
false,0);
775 stack.push_back ( makeStep<EvalAtan>( stateIndex, stateIndex ) );
776 return std::make_pair(
false,0);
778 stack.push_back ( makeStep<EvalAtan2>( stateIndex, stateIndex, stateIndex+1 ) );
779 return std::make_pair(
false,0);
781 stack.push_back ( makeStep<EvalAng>( stateIndex, stateIndex, stateIndex+1 ) );
782 return std::make_pair(
false,0);
784 stack.push_back ( makeStep<EvalCeil>( stateIndex, stateIndex ) );
785 return std::make_pair(
false,0);
787 stack.push_back ( makeStep<EvalCos>( stateIndex, stateIndex ) );
788 return std::make_pair(
false,0);
790 stack.push_back ( makeStep<EvalCosh>( stateIndex, stateIndex ) );
791 return std::make_pair(
false,0);
793 stack.push_back ( makeStep<EvalExp>( stateIndex, stateIndex ) );
794 return std::make_pair(
false,0);
796 stack.push_back ( makeStep<EvalFabs>( stateIndex, stateIndex ) );
797 return std::make_pair(
false,0);
799 stack.push_back ( makeStep<EvalFloor>( stateIndex, stateIndex ) );
800 return std::make_pair(
false,0);
802 stack.push_back ( makeStep<EvalLog>( stateIndex, stateIndex ) );
803 return std::make_pair(
false,0);
805 stack.push_back ( makeStep<EvalLog10>( stateIndex, stateIndex ) );
806 return std::make_pair(
false,0);
808 stack.push_back ( makeStep<EvalRad>( stateIndex, stateIndex, stateIndex+1 ) );
809 return std::make_pair(
false,0);
811 stack.push_back ( makeStep<EvalSin>( stateIndex, stateIndex ) );
812 return std::make_pair(
false,0);
814 stack.push_back ( makeStep<EvalSinh>( stateIndex, stateIndex ) );
815 return std::make_pair(
false,0);
817 stack.push_back ( makeStep<EvalSqrt>( stateIndex, stateIndex ) );
818 return std::make_pair(
false,0);
820 stack.push_back ( makeStep<EvalTan>( stateIndex, stateIndex ) );
821 return std::make_pair(
false,0);
823 stack.push_back ( makeStep<EvalTanh>( stateIndex, stateIndex ) );
824 return std::make_pair(
false,0);
826 stack.push_back ( makeStep<EvalSign>( stateIndex, stateIndex ) );
827 return std::make_pair(
false,0);
829 ASSERTL0(
false,
"Evaluation of " + valueStr +
" is not implemented yet");
831 return std::make_pair(
false,0);
835 ASSERTL1(*valueStr.begin() ==
'-',
"Illegal factor - it can only be '-' and it was: " + valueStr);
836 ASSERTL1(num_children == 1,
"Illegal number of children under factor node: " + valueStr);
843 return std::make_pair(
true, - v.second );
845 stack.push_back (makeStep<EvalNeg>( stateIndex, stateIndex) );
846 return std::make_pair(
false,0);
850 ASSERTL1(num_children == 2,
"Too few or too many arguments for mathematical operator: " + valueStr);
856 if ((
true == left.first) && (
true == right.first))
858 switch(*valueStr.begin())
861 return std::make_pair(
true, left.second + right.second );
863 return std::make_pair(
true, left.second - right.second );
865 return std::make_pair(
true, left.second * right.second );
867 return std::make_pair(
true, left.second / right.second );
869 return std::make_pair(
true, std::pow(left.second, right.second) );
871 return std::make_pair(
true, left.second == right.second );
873 if (*(valueStr.end()-1) ==
'=')
875 return std::make_pair(
true, left.second <= right.second );
879 return std::make_pair(
true, left.second < right.second );
881 return std::make_pair(
false,0);
883 if (*(valueStr.end()-1) ==
'=')
885 return std::make_pair(
true, left.second >= right.second );
889 return std::make_pair(
true, left.second > right.second );
891 return std::make_pair(
false,0);
893 ASSERTL0(
false,
"Invalid operator encountered: " + valueStr);
895 return std::make_pair(
false,0);
900 if (
true == left.first)
902 int const_index =
AddConstant(std::string(
"SUB_EXPR_") + boost::lexical_cast<std::string>(
m_constant.size()), left.second);
903 stack.push_back ( makeStep<StoreConst>( stateIndex, const_index ) );
905 if (
true == right.first)
907 int const_index =
AddConstant(std::string(
"SUB_EXPR_") + boost::lexical_cast<std::string>(
m_constant.size()), right.second);
908 stack.push_back ( makeStep<StoreConst>( stateIndex+1, const_index ) );
912 switch(*valueStr.begin())
915 stack.push_back (makeStep<EvalSum>( stateIndex, stateIndex, stateIndex+1 ) );
916 return std::make_pair(
false,0);
918 stack.push_back (makeStep<EvalSub> (stateIndex, stateIndex, stateIndex+1 ) );
919 return std::make_pair(
false,0);
921 stack.push_back (makeStep<EvalMul>( stateIndex, stateIndex, stateIndex+1 ) );
922 return std::make_pair(
false,0);
924 stack.push_back (makeStep<EvalDiv>( stateIndex, stateIndex, stateIndex+1 ) );
925 return std::make_pair(
false,0);
927 stack.push_back (makeStep<EvalPow>( stateIndex, stateIndex, stateIndex+1 ) );
928 return std::make_pair(
false,0);
930 stack.push_back (makeStep<EvalLogicalEqual>( stateIndex, stateIndex, stateIndex+1 ) );
931 return std::make_pair(
false,0);
933 if (*(valueStr.end()-1) ==
'=')
935 stack.push_back (makeStep<EvalLogicalLeq>( stateIndex, stateIndex, stateIndex+1 ) );
939 stack.push_back (makeStep<EvalLogicalLess>( stateIndex, stateIndex, stateIndex+1 ) );
941 return std::make_pair(
false,0);
944 if (*(valueStr.end()-1) ==
'=')
946 stack.push_back (makeStep<EvalLogicalGeq>( stateIndex, stateIndex, stateIndex+1 ) );
950 stack.push_back (makeStep<EvalLogicalGreater>( stateIndex, stateIndex, stateIndex+1 ) );
952 return std::make_pair(
false,0);
955 ASSERTL0(
false,
"Invalid operator encountered: " + valueStr);
957 return std::make_pair(
false,0);
959 ASSERTL0(
false,
"Illegal expression encountered: " + valueStr);
960 return std::make_pair(
false,0);