From 98c08fd74b0d3721d8951b43b8ae8ada894a8eea Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Mon, 25 Jul 2016 13:43:34 -0400
Subject: [PATCH] preprocessor: check linear model hessian = 0. closes #419

---
 DynamicModel.hh | 9 +++++++++
 ModFile.cc      | 7 +++++++
 2 files changed, 16 insertions(+)

diff --git a/DynamicModel.hh b/DynamicModel.hh
index 9cef4cb0..9c0e670f 100644
--- a/DynamicModel.hh
+++ b/DynamicModel.hh
@@ -217,6 +217,9 @@ public:
   //! Writes model initialization and lead/lag incidence matrix to output
   void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present, bool compute_xrefs, bool julia) const;
 
+  //! Return true if the hessian is equal to zero
+  inline bool checkHessianZero() const;
+
   //! Adds informations for simulation in a binary file
   void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename,
                                    const int &num, int &u_count_int, bool &file_open, bool is_two_boundaries) const;
@@ -492,6 +495,12 @@ public:
   bool isChecksumMatching(const string &basename) const;
 };
 
+inline bool
+DynamicModel::checkHessianZero() const
+{
+  return second_derivatives.empty();
+}
+
 //! Classes to re-order derivatives for various sparse storage formats 
 class derivative
 {
diff --git a/ModFile.cc b/ModFile.cc
index 05c7bce2..414a5130 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -519,6 +519,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr
 		  bool hessian = mod_file_struct.order_option >= 2 
 		    || mod_file_struct.identification_present 
 		    || mod_file_struct.estimation_analytic_derivation
+                    || linear
 		    || output == second 
 		    || output == third;
 		  bool thirdDerivatives = mod_file_struct.order_option == 3 
@@ -532,6 +533,12 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr
 	    }
 	  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())
+        {
+          cerr << "ERROR: If the model is declared linear the second derivatives must be equal to zero." << endl;
+          exit(EXIT_FAILURE);
+        }
     }
 
   for (vector<Statement *>::iterator it = statements.begin();
-- 
GitLab