From b9ee0bb2049e85d8514ed8b360d18072c9201176 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 27 Jul 2018 14:19:59 +0200
Subject: [PATCH] aux_equations may diverge from those in the main model

aux_equations only contain the definition of auxiliary variables, and
may diverge from those in the main model (equations), if other model
transformations applied subsequently. This is not a problem, since
aux_equations is only used for regenerating the values of auxiliaries
given the others.

For example, such a divergence appears when there is an expectation
operator in a ramsey model, see
tests/optimal_policy/nk_ramsey_expectation.mod */
---
 src/DynamicModel.cc | 33 ---------------------------------
 src/ModelTree.hh    |  9 +++++++++
 2 files changed, 9 insertions(+), 33 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 54d0af45..43544888 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -5229,39 +5229,6 @@ DynamicModel::substituteLeadLagInternal(AuxVarType type, bool deterministic_mode
       equation = substeq;
     }
 
-  // Substitute in aux_equations
-  // Without this loop, the auxiliary equations in equations
-  // will diverge from those in aux_equations
-  for (auto & aux_equation : aux_equations)
-    {
-      expr_t subst;
-      switch (type)
-        {
-        case AuxVarType::endoLead:
-          subst = aux_equation->substituteEndoLeadGreaterThanTwo(subst_table,
-                                                                     neweqs, deterministic_model);
-          break;
-        case AuxVarType::endoLag:
-          subst = aux_equation->substituteEndoLagGreaterThanTwo(subst_table, neweqs);
-          break;
-        case AuxVarType::exoLead:
-          subst = aux_equation->substituteExoLead(subst_table, neweqs, deterministic_model);
-          break;
-        case AuxVarType::exoLag:
-          subst = aux_equation->substituteExoLag(subst_table, neweqs);
-          break;
-        case AuxVarType::diffForward:
-          subst = aux_equation->differentiateForwardVars(subset, subst_table, neweqs);
-          break;
-        default:
-          cerr << "DynamicModel::substituteLeadLagInternal: impossible case" << endl;
-          exit(EXIT_FAILURE);
-        }
-      auto *substeq = dynamic_cast<BinaryOpNode *>(subst);
-      assert(substeq != nullptr);
-      aux_equation = substeq;
-    }
-
   // Add new equations
   for (auto & neweq : neweqs)
     addEquation(neweq, -1);
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 682fe0e1..bee2defd 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -59,6 +59,15 @@ protected:
   vector<int> equations_lineno;
 
   //! Only stores generated auxiliary equations, in an order meaningful for evaluation
+  /*! These equations only contain the definition of auxiliary variables, and
+      may diverge from those in the main model (equations), if other model
+      transformations applied subsequently. This is not a problem, since
+      aux_equations is only used for regenerating the values of auxiliaries
+      given the others.
+
+      For example, such a divergence appears when there is an expectation
+      operator in a ramsey model, see
+      tests/optimal_policy/nk_ramsey_expectation.mod */
   deque<BinaryOpNode *> aux_equations;
 
   //! Stores equation tags
-- 
GitLab