From c3dcc0e121012f06acfba046bd97039b60b80538 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 12 May 2016 14:24:07 +0200
Subject: [PATCH] preprocessor: add option no_2nd_order_params_derivs. #1187

---
 DynareMain.cc  |  8 ++++++--
 DynareMain2.cc |  4 ++--
 ModFile.cc     | 14 +++++++-------
 ModFile.hh     |  3 ++-
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/DynareMain.cc b/DynareMain.cc
index ead3b74a..4d6336f8 100644
--- a/DynareMain.cc
+++ b/DynareMain.cc
@@ -41,7 +41,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool
            bool nograph, bool nointeractive, bool parallel, ConfigFile &config_file,
            WarningConsolidation &warnings_arg, bool nostrict, bool check_model_changes,
            bool minimal_workspace, bool compute_xrefs, FileOutputType output_mode,
-           LanguageOutputType lang
+           LanguageOutputType lang, bool sec_order_param_deriv
 #if defined(_WIN32) || defined(__CYGWIN32__)
            , bool cygwin, bool msvc
 #endif
@@ -57,6 +57,7 @@ usage()
   cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [notmpterms] [nolog] [warn_uninit]"
        << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test]"
        << " [-D<variable>[=<value>]] [-I/path] [nostrict] [fast] [minimal_workspace] [compute_xrefs] [output=dynamic|first|second|third] [language=C|C++|julia]"
+       << " [no_2nd_order_params_derivs]"
 #if defined(_WIN32) || defined(__CYGWIN32__)
        << " [cygwin] [msvc]"
 #endif
@@ -90,6 +91,7 @@ main(int argc, char **argv)
   bool no_line_macro = false;
   bool no_log = false;
   bool no_warn = false;
+  bool sec_order_param_deriv = true;
   bool warn_uninit = false;
   bool console = false;
   bool nograph = false;
@@ -119,6 +121,8 @@ main(int argc, char **argv)
         debug = true;
       else if (!strcmp(argv[arg], "noclearall"))
         clear_all = false;
+      else if (!strcmp(argv[arg], "no_2nd_order_params_derivs"))
+        sec_order_param_deriv = false;
       else if (!strcmp(argv[arg], "onlyclearglobals"))
         {
           clear_all = false;
@@ -318,7 +322,7 @@ main(int argc, char **argv)
   main2(macro_output, basename, debug, clear_all, clear_global,
         no_tmp_terms, no_log, no_warn, warn_uninit, console, nograph, nointeractive,
         parallel, config_file, warnings, nostrict, check_model_changes, minimal_workspace,
-        compute_xrefs, output_mode, language
+        compute_xrefs, output_mode, language, sec_order_param_deriv
 #if defined(_WIN32) || defined(__CYGWIN32__)
         , cygwin, msvc
 #endif
diff --git a/DynareMain2.cc b/DynareMain2.cc
index 4bead835..386f939c 100644
--- a/DynareMain2.cc
+++ b/DynareMain2.cc
@@ -30,7 +30,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
       bool nograph, bool nointeractive, bool parallel, ConfigFile &config_file,
       WarningConsolidation &warnings, bool nostrict, bool check_model_changes,
       bool minimal_workspace, bool compute_xrefs, FileOutputType output_mode,
-      LanguageOutputType language
+      LanguageOutputType language, bool sec_order_param_deriv
 #if defined(_WIN32) || defined(__CYGWIN32__)
       , bool cygwin, bool msvc
 #endif
@@ -51,7 +51,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
   mod_file->evalAllExpressions(warn_uninit);
 
   // Do computations
-  mod_file->computingPass(no_tmp_terms, output_mode, compute_xrefs);
+  mod_file->computingPass(no_tmp_terms, output_mode, compute_xrefs, sec_order_param_deriv);
 
   // Write outputs
   if (output_mode != none)
diff --git a/ModFile.cc b/ModFile.cc
index 972a5985..1a62e800 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -469,7 +469,7 @@ ModFile::transformPass(bool nostrict)
 }
 
 void
-ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xrefs)
+ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xrefs, bool sec_order_param_deriv)
 {
   // Mod file may have no equation (for example in a standalone BVAR estimation)
   if (dynamic_model.equation_number() > 0)
@@ -490,10 +490,10 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr
 	  const bool static_hessian = mod_file_struct.identification_present
 	    || mod_file_struct.estimation_analytic_derivation;
           FileOutputType paramsDerivatives = none;
-          if (mod_file_struct.identification_present)
-              paramsDerivatives = first;
           if (mod_file_struct.estimation_analytic_derivation)
-              paramsDerivatives = third;
+            paramsDerivatives = third;
+          if (mod_file_struct.identification_present || !sec_order_param_deriv)
+            paramsDerivatives = first;
 	  static_model.computingPass(global_eval_context, no_tmp_terms, static_hessian,
 				     false, paramsDerivatives, block, byte_code);
 	}
@@ -527,10 +527,10 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr
 		    || mod_file_struct.estimation_analytic_derivation
 		    || output == third;
                   FileOutputType paramsDerivatives = none;
-                  if (mod_file_struct.identification_present)
-                      paramsDerivatives = first;
                   if (mod_file_struct.estimation_analytic_derivation)
-                      paramsDerivatives = third;
+                    paramsDerivatives = third;
+                  if (mod_file_struct.identification_present || !sec_order_param_deriv)
+                    paramsDerivatives = first;
 		  dynamic_model.computingPass(true, hessian, thirdDerivatives, paramsDerivatives, global_eval_context, no_tmp_terms, block, use_dll, byte_code, compute_xrefs);
 		}
 	    }
diff --git a/ModFile.hh b/ModFile.hh
index c720b45e..8dcb9140 100644
--- a/ModFile.hh
+++ b/ModFile.hh
@@ -132,7 +132,8 @@ public:
   //! Execute computations
   /*! \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */
   /*! \param compute_xrefs if true, equation cross references will be computed */
-  void computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xrefs);
+  /*! \param sec_order_param_deriv if true, compute second order param derivatives*/
+  void computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xrefs, bool sec_order_param_deriv);
   //! Writes Matlab/Octave output files
   /*!
     \param basename The base name used for writing output files. Should be the name of the mod file without its extension
-- 
GitLab