From d39fca02b477e289d5cf95fd7693c64fb4524f66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 5 Jun 2020 17:11:29 +0200
Subject: [PATCH] In the driver file, provide mapping between model local
 variables and indices in the temporary terms vector

The structures are M_.model_local_variables_{dynamic,static}_tt_idxs.

Ref. dynare#1722

(cherry picked from commit b402e35683f36d7accefc78f1a3ca1a5722a456d)
---
 src/DynamicModel.cc | 8 ++++++++
 src/ModelTree.cc    | 2 +-
 src/ModelTree.hh    | 2 +-
 src/StaticModel.cc  | 8 ++++++++
 4 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 3bedee10..75f272f8 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -3177,6 +3177,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
       for (size_t i = 0; i < temporary_terms_derivatives.size(); i++)
         output << temporary_terms_derivatives[i].size() + (i == 0 ? temporary_terms_mlv.size() : 0) << "; ";
       output << "];" << endl;
+
+      /* Write mapping between model local variables and indices in the temporary
+         terms vector (dynare#1722) */
+      output << modstruct << "model_local_variables_dynamic_tt_idxs = {" << endl;
+      for (auto [mlv, value] : temporary_terms_mlv)
+        output << "  '" << symbol_table.getName(mlv->symb_id) << "', "
+               << temporary_terms_idxs.at(mlv)+1 << ';' << endl;
+      output << "};" << endl;
     }
 
   // Write equation tags
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index a4cef4dc..f9c757a1 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -74,7 +74,7 @@ ModelTree::copyHelper(const ModelTree &m)
   for (const auto &it : m.temporary_terms)
     temporary_terms.insert(f(it));
   for (const auto &it : m.temporary_terms_mlv)
-    temporary_terms_mlv[f(it.first)] = f(it.second);
+    temporary_terms_mlv[dynamic_cast<VariableNode *>(f(it.first))] = f(it.second);
   for (const auto &it : m.temporary_terms_derivatives)
     temporary_terms_derivatives.push_back(convert_temporary_terms_t(it));
   for (const auto &it : m.temporary_terms_idxs)
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index b3502c36..2b0c5191 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -136,7 +136,7 @@ protected:
 
   //! Used model local variables, that will be treated as temporary terms
   /*! See the comments in ModelTree::computeTemporaryTerms() */
-  map<expr_t, expr_t, ExprNodeLess> temporary_terms_mlv;
+  map<VariableNode *, expr_t, ExprNodeLess> temporary_terms_mlv;
 
   //! Temporary terms for residuals and derivatives
   /*! Index 0 is temp. terms of residuals, index 1 for first derivatives, ... */
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 7609e156..e4ac7212 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -2107,6 +2107,14 @@ StaticModel::writeOutput(ostream &output, bool block) const
     output << temporary_terms_derivative.size() << "; ";
   output << "];" << endl;
 
+  /* Write mapping between model local variables and indices in the temporary
+     terms vector (dynare#1722) */
+  output << "M_.model_local_variables_static_tt_idxs = {" << endl;
+  for (auto [mlv, value] : temporary_terms_mlv)
+    output << "  '" << symbol_table.getName(mlv->symb_id) << "', "
+           << temporary_terms_idxs.at(mlv)+1 << ';' << endl;
+  output << "};" << endl;
+
   if (!block)
     return;
 
-- 
GitLab