From 39e396fe12c7010afd5a2cfa739aba12e635aba4 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan.bastani@ens.fr>
Date: Tue, 12 Apr 2011 16:41:29 +0200
Subject: [PATCH] bug fix: allow use of external functions with model local
 variables

---
 preprocessor/DynamicModel.cc | 10 ++++++----
 preprocessor/ModelTree.cc    | 15 ++++++++-------
 preprocessor/ModelTree.hh    |  4 ++--
 preprocessor/StaticModel.cc  |  5 +++--
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 17da4730ae..9c030b12f9 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -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() << ", "
diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index b6e5fb1531..53ba7d3a54 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -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;
     }
 }
diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh
index 7bc091fc9e..72af80f9ea 100644
--- a/preprocessor/ModelTree.hh
+++ b/preprocessor/ModelTree.hh
@@ -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
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index a1dfd9d1d2..5d74d2d8fc 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -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);
 
-- 
GitLab