From 5fe94ed6069da3a7611086d15149322e53f1347d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 6 Dec 2021 16:29:24 +0100
Subject: [PATCH] Simplify constant equations also in equations marked [static]

The simplifyEquations() methods had to be moved to DynamicModel, in order to
access the static_only_equations member.
---
 src/DynamicModel.cc | 21 +++++++++++++++++++++
 src/DynamicModel.hh |  4 ++++
 src/ExprNode.cc     |  2 +-
 src/ModelTree.cc    | 19 -------------------
 src/ModelTree.hh    |  3 ---
 5 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 1bae113b..b32617c5 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -6344,3 +6344,24 @@ DynamicModel::checkNoRemainingPacExpectation() const
         exit(EXIT_FAILURE);
       }
 }
+
+void
+DynamicModel::simplifyEquations()
+{
+  size_t last_subst_table_size = 0;
+  map<VariableNode *, NumConstNode *> subst_table;
+  // Equations with “mcp” tag are excluded, see dynare#1697
+  findConstantEquationsWithoutMcpTag(subst_table);
+  while (subst_table.size() != last_subst_table_size)
+    {
+      last_subst_table_size = subst_table.size();
+      for (auto &[id, definition] : local_variables_table)
+        definition = definition->replaceVarsInEquation(subst_table);
+      for (auto &equation : equations)
+        equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table));
+      for (auto &equation : static_only_equations)
+        equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table));
+      subst_table.clear();
+      findConstantEquationsWithoutMcpTag(subst_table);
+    }
+}
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index 1ce54fb8..09d4d900 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -589,5 +589,9 @@ public:
   bool ParamUsedWithLeadLag() const;
 
   bool isChecksumMatching(const string &basename, bool block) const;
+
+  //! Simplify model equations: if a variable is equal to a constant, replace that variable elsewhere in the model
+  /*! Equations with MCP tags are excluded, see dynare#1697 */
+  void simplifyEquations();
 };
 #endif
diff --git a/src/ExprNode.cc b/src/ExprNode.cc
index ddd42699..c2a35b04 100644
--- a/src/ExprNode.cc
+++ b/src/ExprNode.cc
@@ -1938,7 +1938,7 @@ expr_t
 VariableNode::replaceVarsInEquation(map<VariableNode *, NumConstNode *> &table) const
 {
   /* Do not recurse into model-local variables definitions, since MLVs are
-     already handled by ModelTree::simplifyEquations().
+     already handled by DynamicModel::simplifyEquations().
      This is also necessary because of #65. */
   for (auto &it : table)
     if (it.first->symb_id == symb_id)
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index c6216131..da71df01 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1629,25 +1629,6 @@ ModelTree::includeExcludeEquations(set<pair<string, string>> &eqs, bool exclude_
   return excluded_vars;
 }
 
-void
-ModelTree::simplifyEquations()
-{
-  size_t last_subst_table_size = 0;
-  map<VariableNode *, NumConstNode *> subst_table;
-  // Equations with “mcp” tag are excluded, see dynare#1697
-  findConstantEquationsWithoutMcpTag(subst_table);
-  while (subst_table.size() != last_subst_table_size)
-    {
-      last_subst_table_size = subst_table.size();
-      for (auto &[id, definition] : local_variables_table)
-        definition = definition->replaceVarsInEquation(subst_table);
-      for (auto &equation : equations)
-        equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table));
-      subst_table.clear();
-      findConstantEquationsWithoutMcpTag(subst_table);
-    }
-}
-
 void
 ModelTree::findConstantEquationsWithoutMcpTag(map<VariableNode *, NumConstNode *> &subst_table) const
 {
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index a5e3d1bd..4596df66 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -440,9 +440,6 @@ public:
   //! Is a given variable non-stationary?
   bool isNonstationary(int symb_id) const;
   void set_cutoff_to_zero();
-  //! Simplify model equations: if a variable is equal to a constant, replace that variable elsewhere in the model
-  /*! Equations with MCP tags are excluded, see dynare#1697 */
-  void simplifyEquations();
   /*! Reorder auxiliary variables so that they appear in recursive order in
       set_auxiliary_variables.m and dynamic_set_auxiliary_series.m */
   void reorderAuxiliaryEquations();
-- 
GitLab