Commit 39e396fe authored by Houtan Bastani's avatar Houtan Bastani
Browse files

bug fix: allow use of external functions with model local variables

parent 26d55343
......@@ -2050,9 +2050,10 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const
ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : oMatlabDynamicModel);
writeModelLocalVariables(model_output, output_type);
deriv_node_temp_terms_t tef_terms;
writeModelLocalVariables(model_output, output_type, tef_terms);
writeTemporaryTerms(temporary_terms, model_output, output_type);
writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms);
writeModelEquations(model_output, output_type);
......@@ -3430,9 +3431,10 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl;
writeModelLocalVariables(paramsDerivsFile, oMatlabDynamicModel);
deriv_node_temp_terms_t tef_terms;
writeModelLocalVariables(paramsDerivsFile, oMatlabDynamicModel, tef_terms);
writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, oMatlabDynamicModel);
writeTemporaryTerms(params_derivs_temporary_terms, paramsDerivsFile, oMatlabDynamicModel, tef_terms);
// Write parameter derivative
paramsDerivsFile << "rp = zeros(" << equation_number() << ", "
......
......@@ -1087,14 +1087,11 @@ ModelTree::computeTemporaryTerms(bool is_matlab)
void
ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, ostream &output,
ExprNodeOutputType output_type) const
ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const
{
// Local var used to keep track of temp nodes already written
temporary_terms_t tt2;
// To store the functions that have already been written in the form TEF* = ext_fun();
deriv_node_temp_terms_t tef_terms;
for (temporary_terms_t::const_iterator it = tt.begin();
it != tt.end(); it++)
{
......@@ -1153,12 +1150,16 @@ ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_n
}
void
ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const
ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const
{
/* Collect all model local variables appearing in equations, and print only
them. Printing unused model local variables can lead to a crash (see
ticket #101). */
set<int> used_local_vars;
// Use an empty set for the temporary terms
const temporary_terms_t tt;
for (size_t i = 0; i < equations.size(); i++)
equations[i]->collectModelLocalVariables(used_local_vars);
......@@ -1167,6 +1168,7 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t
{
int id = *it;
expr_t value = local_variables_table.find(id)->second;
value->writeExternalFunctionOutput(output, output_type, tt, tef_terms);
if (IS_C(output_type))
output << "double ";
......@@ -1174,8 +1176,7 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t
/* We append underscores to avoid name clashes with "g1" or "oo_" (see
also VariableNode::writeOutput) */
output << symbol_table.getName(id) << "__ = ";
// Use an empty set for the temporary terms
value->writeOutput(output, output_type);
value->writeOutput(output, output_type, tt, tef_terms);
output << ";" << endl;
}
}
......
......@@ -114,7 +114,7 @@ protected:
//! Computes temporary terms (for all equations and derivatives)
void computeTemporaryTerms(bool is_matlab);
//! Writes temporary terms
void writeTemporaryTerms(const temporary_terms_t &tt, ostream &output, ExprNodeOutputType output_type) const;
void writeTemporaryTerms(const temporary_terms_t &tt, ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;
//! Compiles temporary terms
void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, map_idx_t map_idx, bool dynamic, bool steady_dynamic) const;
//! Adds informations for simulation in a binary file
......@@ -122,7 +122,7 @@ protected:
//! Writes model local variables
/*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */
void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const;
void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;
//! Writes model equations
void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const;
//! Compiles model equations
......
......@@ -1161,9 +1161,10 @@ StaticModel::writeStaticMFile(const string &func_name) const
<< "%" << endl
<< endl;
writeModelLocalVariables(output, oMatlabStaticModel);
deriv_node_temp_terms_t tef_terms;
writeModelLocalVariables(output, oMatlabStaticModel, tef_terms);
writeTemporaryTerms(temporary_terms, output, oMatlabStaticModel);
writeTemporaryTerms(temporary_terms, output, oMatlabStaticModel, tef_terms);
writeModelEquations(output, oMatlabStaticModel);
......
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