Commit 2ab8e2a1 authored by Houtan Bastani's avatar Houtan Bastani

preprocessor: external functions bug fix: fixes the case where an external...

preprocessor: external functions bug fix: fixes the case where an external function is contained in a local variable that’s used in an equation with leads/lags that require auxiliary equations. Closes #916
parent 5ac2683b
...@@ -308,6 +308,12 @@ NumConstNode::writeOutput(ostream &output, ExprNodeOutputType output_type, ...@@ -308,6 +308,12 @@ NumConstNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << datatree.num_constants.get(id); output << datatree.num_constants.get(id);
} }
bool
NumConstNode::containsExternalFunction() const
{
return false;
}
double double
NumConstNode::eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException) NumConstNode::eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException)
{ {
...@@ -571,6 +577,12 @@ VariableNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, t ...@@ -571,6 +577,12 @@ VariableNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, t
datatree.local_variables_table[symb_id]->collectTemporary_terms(temporary_terms, temporary_terms_inuse, Curr_Block); datatree.local_variables_table[symb_id]->collectTemporary_terms(temporary_terms, temporary_terms_inuse, Curr_Block);
} }
bool
VariableNode::containsExternalFunction() const
{
return false;
}
void void
VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
...@@ -1714,6 +1726,12 @@ UnaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, te ...@@ -1714,6 +1726,12 @@ UnaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, te
arg->collectTemporary_terms(temporary_terms, temporary_terms_inuse, Curr_Block); arg->collectTemporary_terms(temporary_terms, temporary_terms_inuse, Curr_Block);
} }
bool
UnaryOpNode::containsExternalFunction() const
{
return arg->containsExternalFunction();
}
void void
UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
...@@ -2885,6 +2903,13 @@ BinaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, t ...@@ -2885,6 +2903,13 @@ BinaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, t
} }
} }
bool
BinaryOpNode::containsExternalFunction() const
{
return arg1->containsExternalFunction()
|| arg2->containsExternalFunction();
}
void void
BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
...@@ -3963,6 +3988,14 @@ TrinaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, ...@@ -3963,6 +3988,14 @@ TrinaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms,
} }
} }
bool
TrinaryOpNode::containsExternalFunction() const
{
return arg1->containsExternalFunction()
|| arg2->containsExternalFunction()
|| arg3->containsExternalFunction();
}
void void
TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
...@@ -4626,6 +4659,12 @@ AbstractExternalFunctionNode::writePrhs(ostream &output, ExprNodeOutputType outp ...@@ -4626,6 +4659,12 @@ AbstractExternalFunctionNode::writePrhs(ostream &output, ExprNodeOutputType outp
} }
} }
bool
AbstractExternalFunctionNode::containsExternalFunction() const
{
return true;
}
ExternalFunctionNode::ExternalFunctionNode(DataTree &datatree_arg, ExternalFunctionNode::ExternalFunctionNode(DataTree &datatree_arg,
int symb_id_arg, int symb_id_arg,
const vector<expr_t> &arguments_arg) : const vector<expr_t> &arguments_arg) :
......
/* /*
* Copyright (C) 2007-2014 Dynare Team * Copyright (C) 2007-2015 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
...@@ -187,6 +187,9 @@ public: ...@@ -187,6 +187,9 @@ public:
*/ */
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0;
//! returns true if the expr node contains an external function
virtual bool containsExternalFunction() const = 0;
//! Writes output of node (with no temporary terms and with "outside model" output type) //! Writes output of node (with no temporary terms and with "outside model" output type)
void writeOutput(ostream &output) const; void writeOutput(ostream &output) const;
...@@ -441,6 +444,7 @@ public: ...@@ -441,6 +444,7 @@ public:
}; };
virtual void prepareForDerivation(); virtual void prepareForDerivation();
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const;
virtual bool containsExternalFunction() const;
virtual void collectDynamicVariables(SymbolType type_arg, set<pair<int, int> > &result) const; virtual void collectDynamicVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const; virtual void collectTemporary_terms(const temporary_terms_t &temporary_terms, temporary_terms_inuse_t &temporary_terms_inuse, int Curr_Block) const;
virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException); virtual double eval(const eval_context_t &eval_context) const throw (EvalException, EvalExternalFunctionException);
...@@ -486,6 +490,7 @@ public: ...@@ -486,6 +490,7 @@ public:
VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg); VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg);
virtual void prepareForDerivation(); virtual void prepareForDerivation();
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const;
virtual bool containsExternalFunction() const;
virtual void collectDynamicVariables(SymbolType type_arg, set<pair<int, int> > &result) const; virtual void collectDynamicVariables(SymbolType type_arg, set<pair<int, int> > &result) const;
virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, virtual void computeTemporaryTerms(map<expr_t, int> &reference_count,
temporary_terms_t &temporary_terms, temporary_terms_t &temporary_terms,
...@@ -552,6 +557,7 @@ public: ...@@ -552,6 +557,7 @@ public:
virtual void prepareForDerivation(); virtual void prepareForDerivation();
virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const;
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const;
virtual bool containsExternalFunction() const;
virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
deriv_node_temp_terms_t &tef_terms) const; deriv_node_temp_terms_t &tef_terms) const;
...@@ -630,6 +636,7 @@ public: ...@@ -630,6 +636,7 @@ public:
virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const;
virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const;
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const;
virtual bool containsExternalFunction() const;
virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
deriv_node_temp_terms_t &tef_terms) const; deriv_node_temp_terms_t &tef_terms) const;
...@@ -724,6 +731,7 @@ public: ...@@ -724,6 +731,7 @@ public:
virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const;
virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const;
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const;
virtual bool containsExternalFunction() const;
virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
deriv_node_temp_terms_t &tef_terms) const; deriv_node_temp_terms_t &tef_terms) const;
...@@ -795,6 +803,7 @@ public: ...@@ -795,6 +803,7 @@ public:
virtual void prepareForDerivation(); virtual void prepareForDerivation();
virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const = 0; virtual void computeTemporaryTerms(map<expr_t, int> &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const = 0;
virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0;
virtual bool containsExternalFunction() const;
virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type,
const temporary_terms_t &temporary_terms, const temporary_terms_t &temporary_terms,
deriv_node_temp_terms_t &tef_terms) const = 0; deriv_node_temp_terms_t &tef_terms) const = 0;
......
/* /*
* Copyright (C) 2003-2014 Dynare Team * Copyright (C) 2003-2015 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
...@@ -1927,9 +1927,16 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const ...@@ -1927,9 +1927,16 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const
<< "% from model file (.mod)" << endl << "% from model file (.mod)" << endl
<< endl; << endl;
deriv_node_temp_terms_t tef_terms;
temporary_terms_t temporary_terms;
for (int i = 0; i < (int) aux_equations.size(); i++)
if (dynamic_cast<ExprNode *>(aux_equations[i])->containsExternalFunction())
dynamic_cast<ExprNode *>(aux_equations[i])->writeExternalFunctionOutput(output, oMatlabStaticModel,
temporary_terms, tef_terms);
for (int i = 0; i < (int) aux_equations.size(); i++) for (int i = 0; i < (int) aux_equations.size(); i++)
{ {
dynamic_cast<ExprNode *>(aux_equations[i])->writeOutput(output, oMatlabStaticModel); dynamic_cast<ExprNode *>(aux_equations[i])->writeOutput(output, oMatlabStaticModel, temporary_terms, tef_terms);
output << ";" << endl; output << ";" << endl;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment