From d78f9d668b5f18db122bda1de76aa8933ae92885 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 29 Nov 2016 14:20:53 +0100
Subject: [PATCH] preprocessor: fix bug that caused all models declared linear
 to fail under ramsey. closes #419

---
 ModFile.cc | 14 ++++++++++++--
 ModFile.hh |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/ModFile.cc b/ModFile.cc
index 22e34f07..708a8f69 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -36,6 +36,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg)
     dynamic_model(symbol_table, num_constants, external_functions_table),
     trend_dynamic_model(symbol_table, num_constants, external_functions_table),
     ramsey_FOC_equations_dynamic_model(symbol_table, num_constants, external_functions_table),
+    orig_ramsey_dynamic_model(symbol_table, num_constants, external_functions_table),
     static_model(symbol_table, num_constants, external_functions_table),
     steady_state_model(symbol_table, num_constants, external_functions_table, static_model),
     linear(false), block(false), byte_code(false), use_dll(false), no_static(false), 
@@ -360,6 +361,8 @@ ModFile::transformPass(bool nostrict)
         clone the model then clone the new equations back to the original because
         we have to call computeDerivIDs (in computeRamseyPolicyFOCs and computingPass)
        */
+      if (linear)
+        dynamic_model.cloneDynamic(orig_ramsey_dynamic_model);
       dynamic_model.cloneDynamic(ramsey_FOC_equations_dynamic_model);
       ramsey_FOC_equations_dynamic_model.computeRamseyPolicyFOCs(*planner_objective);
       ramsey_FOC_equations_dynamic_model.replaceMyEquations(dynamic_model);
@@ -529,15 +532,22 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr
                   if (mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation)
                     paramsDerivsOrder = params_derivs_order;
 		  dynamic_model.computingPass(true, hessian, thirdDerivatives, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll, byte_code, compute_xrefs);
+                  if (linear && mod_file_struct.ramsey_model_present)
+                    orig_ramsey_dynamic_model.computingPass(true, true, false, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll, byte_code, compute_xrefs);
 		}
 	    }
 	  else // No computing task requested, compute derivatives up to 2nd order by default
 	    dynamic_model.computingPass(true, true, false, none, global_eval_context, no_tmp_terms, block, use_dll, byte_code, compute_xrefs);
 
-      if (linear && !dynamic_model.checkHessianZero())
+      if ((linear && !mod_file_struct.ramsey_model_present && !dynamic_model.checkHessianZero()) ||
+          (linear && mod_file_struct.ramsey_model_present && !orig_ramsey_dynamic_model.checkHessianZero()))
         {
           map<int, string> eqs;
-          dynamic_model.getNonZeroHessianEquations(eqs);
+          if (mod_file_struct.ramsey_model_present)
+            orig_ramsey_dynamic_model.getNonZeroHessianEquations(eqs);
+          else
+            dynamic_model.getNonZeroHessianEquations(eqs);
+
           cerr << "ERROR: If the model is declared linear the second derivatives must be equal to zero." << endl
                << "       The following equations had non-zero second derivatives:" << endl;
           for (map<int, string >::const_iterator it = eqs.begin(); it != eqs.end(); it++)
diff --git a/ModFile.hh b/ModFile.hh
index 3dcfebc5..506ddacc 100644
--- a/ModFile.hh
+++ b/ModFile.hh
@@ -66,6 +66,8 @@ public:
   DynamicModel trend_dynamic_model;
   //! A model in which to create the FOC for the ramsey problem
   DynamicModel ramsey_FOC_equations_dynamic_model;
+  //! A copy of the original model, used to test model linearity under ramsey problem
+  DynamicModel orig_ramsey_dynamic_model;
   //! Static model, as derived from the "model" block when leads and lags have been removed
   StaticModel static_model;
   //! Static model, as declared in the "steady_state_model" block if present
-- 
GitLab