From 9263a02a9b881d971325fc6c58a036ce5e84ece1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 27 Mar 2020 18:14:17 +0100
Subject: [PATCH] =?UTF-8?q?Skip=20constant=20equations=20replacement=20onl?=
 =?UTF-8?q?y=20when=20there=20is=20an=20=E2=80=9Cmcp=E2=80=9D=20tag?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Since bef537d40accd597c19f112471974b726c9c9890, constant equations were not
simplified as soon as they had a tag attached.

But this is too wide a restriction. In particular, this breaks the trend
component models which have a target that is set to a constant.

So we now only skip the replacement in the case where there is an “mcp” tag.

Ref. dynare#1697
---
 src/ModelTree.cc | 11 ++++++-----
 src/ModelTree.hh |  7 +++----
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index ec93b9d6..e39f190a 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1964,23 +1964,24 @@ ModelTree::simplifyEquations()
 {
   size_t last_subst_table_size = 0;
   map<VariableNode *, NumConstNode *> subst_table;
-  // Equations with tags are excluded, in particular because of MCPs, see dynare#1697
-  findConstantEquationsWithoutTags(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 &equation : equations)
         equation = dynamic_cast<BinaryOpNode *>(equation->replaceVarsInEquation(subst_table));
       subst_table.clear();
-      findConstantEquationsWithoutTags(subst_table);
+      findConstantEquationsWithoutMcpTag(subst_table);
     }
 }
 
 void
-ModelTree::findConstantEquationsWithoutTags(map<VariableNode *, NumConstNode *> &subst_table) const
+ModelTree::findConstantEquationsWithoutMcpTag(map<VariableNode *, NumConstNode *> &subst_table) const
 {
   for (size_t i = 0; i < equations.size(); i++)
-    if (getEquationTags(i).empty())
+    if (auto tags = getEquationTags(i);
+        tags.find("mcp") == tags.end())
       equations[i]->findConstantEquations(subst_table);
 }
 
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index b6dfbc49..8ae42108 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -412,14 +412,13 @@ public:
   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 tags are excluded, in particular because of MCPs, see
-      dynare#1697 */
+  /*! 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();
-  //! Find equations of the form “variable=constant”, excluding equations with tags
-  void findConstantEquationsWithoutTags(map<VariableNode *, NumConstNode *> &subst_table) const;
+  //! Find equations of the form “variable=constant”, excluding equations with “mcp” tag (see dynare#1697)
+  void findConstantEquationsWithoutMcpTag(map<VariableNode *, NumConstNode *> &subst_table) const;
   //! Helper for writing the Jacobian elements in MATLAB and C
   /*! Writes either (i+1,j+1) or [i+j*no_eq] */
   void jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const;
-- 
GitLab