From 5f43b1b578929f5ab0648ceadb8ddf7f6c2eea23 Mon Sep 17 00:00:00 2001
From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152>
Date: Mon, 27 Oct 2008 11:35:08 +0000
Subject: [PATCH] 4.0: merged r2121 and r2170 changesets (enforce same number
 of equations and endogenous, except when doing optimal policy or standalone
 BVAR estimation)

git-svn-id: https://www.dynare.org/svn/dynare/branches/4.0@2206 ac1d8469-bf42-47a9-8791-bf33cf982152
---
 preprocessor/ComputingTasks.cc         | 24 ++++++++++++++-----
 preprocessor/ModFile.cc                | 33 ++++++++++++++++++++------
 preprocessor/Statement.cc              | 10 ++++++--
 preprocessor/include/ComputingTasks.hh |  3 ++-
 preprocessor/include/ModFile.hh        |  1 +
 preprocessor/include/Statement.hh      | 18 +++++++++++---
 6 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc
index d08d8c0608..5d7a7431b5 100644
--- a/preprocessor/ComputingTasks.cc
+++ b/preprocessor/ComputingTasks.cc
@@ -165,7 +165,7 @@ StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
 void
 StochSimulStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  mod_file_struct.stoch_simul_or_similar_present = true;
+  mod_file_struct.stoch_simul_present = true;
 
   // Fill in option_order of mod_file_struct
   OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
@@ -191,7 +191,7 @@ ForecastStatement::ForecastStatement(const SymbolList &symbol_list_arg,
 void
 ForecastStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  mod_file_struct.stoch_simul_or_similar_present = true;
+  mod_file_struct.forecast_present = true;
 
   // Fill in option_order of mod_file_struct
   OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
@@ -217,7 +217,7 @@ RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
 void
 RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  mod_file_struct.stoch_simul_or_similar_present = true;
+  mod_file_struct.ramsey_policy_present = true;
 
   /* Fill in option_order of mod_file_struct
      Since ramsey policy needs one further order of derivation (for example, for 1st order
@@ -245,7 +245,7 @@ EstimationStatement::EstimationStatement(const SymbolList &symbol_list_arg,
 void
 EstimationStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  mod_file_struct.stoch_simul_or_similar_present = true;
+  mod_file_struct.estimation_present = true;
 
   // Fill in option_order of mod_file_struct
   OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
@@ -718,7 +718,7 @@ OsrStatement::OsrStatement(const SymbolList &symbol_list_arg,
 void
 OsrStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  mod_file_struct.stoch_simul_or_similar_present = true;
+  mod_file_struct.osr_present = true;
 
   // Fill in option_order of mod_file_struct
   OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
@@ -854,7 +854,7 @@ PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct)
 {
   if (model_tree->equation_number() != 1)
     {
-      cerr << "Error: planer_objective: should have only one equation!" << endl;
+      cerr << "ERROR: planer_objective: should have only one equation!" << endl;
       exit(-1);
     }
 }
@@ -878,6 +878,12 @@ BVARDensityStatement::BVARDensityStatement(int maxnlags_arg, const OptionsList &
 {
 }
 
+void
+BVARDensityStatement::checkPass(ModFileStructure &mod_file_struct)
+{
+  mod_file_struct.bvar_density_present = true;
+}
+
 void
 BVARDensityStatement::writeOutput(ostream &output, const string &basename) const
 {
@@ -891,6 +897,12 @@ BVARForecastStatement::BVARForecastStatement(int nlags_arg, const OptionsList &o
 {
 }
 
+void
+BVARForecastStatement::checkPass(ModFileStructure &mod_file_struct)
+{
+  mod_file_struct.bvar_forecast_present = true;
+}
+
 void
 BVARForecastStatement::writeOutput(ostream &output, const string &basename) const
 {
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index d90a704d7c..6b832720c1 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -53,20 +53,39 @@ ModFile::checkPass()
   if (!mod_file_struct.order_option)
     mod_file_struct.order_option = 2;
 
+  bool stochastic_statement_present = mod_file_struct.stoch_simul_present
+    || mod_file_struct.estimation_present
+    || mod_file_struct.forecast_present
+    || mod_file_struct.osr_present
+    || mod_file_struct.ramsey_policy_present;
+
   // Allow empty model only when doing a standalone BVAR estimation
   if (model_tree.equation_number() == 0
       && (mod_file_struct.check_present
           || mod_file_struct.simul_present
-          || mod_file_struct.stoch_simul_or_similar_present))
+          || stochastic_statement_present))
+    {
+      cerr << "ERROR: At least one model equation must be declared!" << endl;
+      exit(-1);
+    }
+
+  if (mod_file_struct.simul_present && stochastic_statement_present)
     {
-      cerr << "Error: you must declare at least one model equation!" << endl;
+      cerr << "ERROR: A .mod file cannot contain both a simul command and one of {stoch_simul, estimation, forecast, osr, ramsey_policy}" << endl;
       exit(-1);
     }
 
-  if (mod_file_struct.simul_present
-      && mod_file_struct.stoch_simul_or_similar_present)
+  /*
+    Enforce the same number of equations and endogenous, except in two cases:
+    - ramsey_policy is used
+    - a BVAR command is used and there is no equation (standalone BVAR estimation)
+  */
+  if (!mod_file_struct.ramsey_policy_present
+      && !((mod_file_struct.bvar_density_present || mod_file_struct.bvar_forecast_present)
+           && model_tree.equation_number() == 0)
+      && (model_tree.equation_number() != symbol_table.endo_nbr))
     {
-      cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, osr, ramsey_policy}" << endl;
+      cerr << "ERROR: There are " << model_tree.equation_number() << " equations but " << symbol_table.endo_nbr << " endogenous variables!" << endl;
       exit(-1);
     }
 }
@@ -114,14 +133,14 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
       mOutputFile.open(fname.c_str(), ios::out | ios::binary);
       if (!mOutputFile.is_open())
         {
-          cerr << "Error: Can't open file " << fname
+          cerr << "ERROR: Can't open file " << fname
                << " for writing" << endl;
           exit(-1);
         }
     }
   else
     {
-      cerr << "Error: Missing file name" << endl;
+      cerr << "ERROR: Missing file name" << endl;
       exit(-1);
     }
 
diff --git a/preprocessor/Statement.cc b/preprocessor/Statement.cc
index dc629b49f2..17adf591bc 100644
--- a/preprocessor/Statement.cc
+++ b/preprocessor/Statement.cc
@@ -22,8 +22,14 @@
 ModFileStructure::ModFileStructure() :
   check_present(false),
   simul_present(false),
-  stoch_simul_or_similar_present(false),
-  order_option(0)
+  stoch_simul_present(false),
+  estimation_present(false),
+  forecast_present(false),
+  osr_present(false),
+  ramsey_policy_present(false),
+  order_option(0),
+  bvar_density_present(false),
+  bvar_forecast_present(false)
 {
 }
 
diff --git a/preprocessor/include/ComputingTasks.hh b/preprocessor/include/ComputingTasks.hh
index 1d5a749b55..cb2170e6cb 100644
--- a/preprocessor/include/ComputingTasks.hh
+++ b/preprocessor/include/ComputingTasks.hh
@@ -447,6 +447,7 @@ private:
   const OptionsList options_list;
 public:
   BVARDensityStatement(int maxnlags_arg, const OptionsList &options_list_arg);
+  virtual void checkPass(ModFileStructure &mod_file_struct);
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
@@ -457,8 +458,8 @@ private:
   const OptionsList options_list;
 public:
   BVARForecastStatement(int nlags_arg, const OptionsList &options_list_arg);
+  virtual void checkPass(ModFileStructure &mod_file_struct);
   virtual void writeOutput(ostream &output, const string &basename) const;
 };
 
-
 #endif
diff --git a/preprocessor/include/ModFile.hh b/preprocessor/include/ModFile.hh
index aecd9c1ecb..6dde145229 100644
--- a/preprocessor/include/ModFile.hh
+++ b/preprocessor/include/ModFile.hh
@@ -60,6 +60,7 @@ public:
   //! Add a statement
   void addStatement(Statement *st);
   //! Do some checking and fills mod_file_struct
+  /*! \todo add check for number of equations and endogenous if ramsey_policy is present */
   void checkPass();
   //! Execute computations
   void computingPass();
diff --git a/preprocessor/include/Statement.hh b/preprocessor/include/Statement.hh
index 77dd87449d..d86129f7a3 100644
--- a/preprocessor/include/Statement.hh
+++ b/preprocessor/include/Statement.hh
@@ -36,12 +36,24 @@ public:
   bool check_present;
   //! Whether a simul statement is present
   bool simul_present;
-  //! Whether a stoch_simul, estimation, osr, ramsey_policy statement is present
-  bool stoch_simul_or_similar_present;
-  //! The value of the "order" option of stoch_simul, estimation, osr, ramsey_policy
+  //! Whether a stoch_simul statement is present
+  bool stoch_simul_present;
+  //! Whether an estimation statement is present
+  bool estimation_present;
+  //! Whether a forecast statement is present
+  bool forecast_present;
+  //! Whether an osr statement is present
+  bool osr_present;
+  //! Whether a ramsey_policy statement is present
+  bool ramsey_policy_present;
+  //! The value of the "order" option of stoch_simul, estimation, forecast, osr, ramsey_policy
   //! Derivation order
   /*! First initialized to zero. If user sets order option somewhere in the MOD file, it will be equal to the maximum of order options. Otherwise will default to 2 */
   int order_option;
+  //! Whether a bvar_density statement is present
+  bool bvar_density_present;
+  //! Whether a bvar_forecast statement is present
+  bool bvar_forecast_present;
 };
 
 class Statement
-- 
GitLab