From 73d3e17540f72c26fc3e44ce9aa8daadfe2d2871 Mon Sep 17 00:00:00 2001 From: sebastien Date: Fri, 15 Dec 2006 11:44:58 +0000 Subject: [PATCH] v4 parser: * added an embryonic checking pass after parsing * modified the decision of which derivatives to compute: - if a simul statement is present, compute the jacobian vs endogenous variables only, and no hessian - if a stoch_simul/estimation/olr/osr statement is present, compute the jacobian vs all variables, plus the hessian - if the two previous conditions are simultaneously fulfilled, abort with an error - if none of these two conditions are fulfilled, abort with an error (nothing to compute) * fixed a minor bug with the size of the jacobian matrix when computing it only vs endogenous variables git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1120 ac1d8469-bf42-47a9-8791-bf33cf982152 --- parser.src/ComputingTasks.cc | 30 ++++++++++++++++++++++++ parser.src/DynareMain.cc | 3 +++ parser.src/ModFile.cc | 35 ++++++++++++++++++++++++++-- parser.src/ModelTree.cc | 23 ++++++------------ parser.src/ParsingDriver.cc | 17 +------------- parser.src/Statement.cc | 11 +++++++++ parser.src/include/ComputingTasks.hh | 5 ++++ parser.src/include/ModFile.hh | 9 ++++--- parser.src/include/ModelTree.hh | 10 ++++---- parser.src/include/Statement.hh | 12 ++++++++++ 10 files changed, 113 insertions(+), 42 deletions(-) diff --git a/parser.src/ComputingTasks.cc b/parser.src/ComputingTasks.cc index 830fffce9..cf5bba8e9 100644 --- a/parser.src/ComputingTasks.cc +++ b/parser.src/ComputingTasks.cc @@ -41,6 +41,12 @@ SimulStatement::SimulStatement(const OptionsList &options_list_arg) : { } +void +SimulStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.simul_present = true; +} + void SimulStatement::writeOutput(ostream &output) const { @@ -55,6 +61,12 @@ StochSimulStatement::StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_ { } +void +StochSimulStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.stoch_simul_or_similar_present = true; +} + void StochSimulStatement::writeOutput(ostream &output) const { @@ -70,6 +82,12 @@ EstimationStatement::EstimationStatement(const TmpSymbolTable &tmp_symbol_table_ { } +void +EstimationStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.stoch_simul_or_similar_present = true; +} + void EstimationStatement::writeOutput(ostream &output) const { @@ -494,6 +512,12 @@ OsrStatement::OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg, { } +void +OsrStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.stoch_simul_or_similar_present = true; +} + void OsrStatement::writeOutput(ostream &output) const { @@ -521,6 +545,12 @@ OlrStatement::OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg, { } +void +OlrStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.stoch_simul_or_similar_present = true; +} + void OlrStatement::writeOutput(ostream &output) const { diff --git a/parser.src/DynareMain.cc b/parser.src/DynareMain.cc index 295be442e..2160a5316 100644 --- a/parser.src/DynareMain.cc +++ b/parser.src/DynareMain.cc @@ -46,6 +46,9 @@ main(int argc, char** argv) // Launch parsing ModFile *mod_file = p.parse(argv[1]); + // Run checking pass + mod_file->checkPass(); + // FIXME string basename = argv[1]; basename.erase(basename.size() - 4, 4); diff --git a/parser.src/ModFile.cc b/parser.src/ModFile.cc index a4e647ec8..c4c342ce2 100644 --- a/parser.src/ModFile.cc +++ b/parser.src/ModFile.cc @@ -3,7 +3,7 @@ ModFile::ModFile() : symbol_table(model_parameters), model_tree(symbol_table, model_parameters, num_constants), - order(-1), linear(-1) + linear(false) { } @@ -20,6 +20,37 @@ ModFile::addStatement(Statement *st) statements.push_back(st); } +void +ModFile::checkPass() +{ + for(vector::iterator it = statements.begin(); + it != statements.end(); it++) + (*it)->checkPass(mod_file_struct); + + if (!mod_file_struct.simul_present + && !mod_file_struct.stoch_simul_or_similar_present) + { + cerr << "Error: nothing to compute! you must use one of {simul, stoch_simul, estimation, olr, osr}" << endl; + exit(-1); + } + + if (mod_file_struct.simul_present + && mod_file_struct.stoch_simul_or_similar_present) + { + cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, olr, osr}" << endl; + exit(-1); + } + + // Set things to compute + if (mod_file_struct.simul_present) + model_tree.computeJacobian = true; + else + { + model_tree.computeJacobianExo = true; + model_tree.computeHessian = true; + } +} + void ModFile::writeOutputFiles(const string &basename, bool clear_all) { @@ -92,7 +123,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) if (linear == 1) mOutputFile << "options_.linear = 1;" << endl; - model_tree.writeOutput(mOutputFile, basename, order, linear); + model_tree.writeOutput(mOutputFile, basename); // Print statements for(vector::iterator it = statements.begin(); diff --git a/parser.src/ModelTree.cc b/parser.src/ModelTree.cc index e5fef8753..84657e6c3 100644 --- a/parser.src/ModelTree.cc +++ b/parser.src/ModelTree.cc @@ -40,9 +40,9 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg, DataTree(symbol_table_arg, mod_param_arg), mod_param(mod_param_arg), num_constants(num_constants_arg), - computeHessian(false), computeJacobian(false), - computeJacobianExo(false) + computeJacobianExo(false), + computeHessian(false) { } @@ -964,7 +964,10 @@ string ModelTree::setDynamicModel(void) cout << "done \n"; } int nrows = mod_param.eq_nbr; - int nvars = mod_param.var_endo_nbr + mod_param.exo_nbr + mod_param.exo_det_nbr; + int nvars = mod_param.var_endo_nbr; + if (computeJacobianExo) + nvars += mod_param.exo_nbr + mod_param.exo_det_nbr; + if (offset == 1) { DynamicOutput << "global M_ it_\n"; @@ -1386,20 +1389,8 @@ inline int ModelTree::optimize(NodeID node) } void -ModelTree::writeOutput(ostream &output, const string &basename, int order, int linear) +ModelTree::writeOutput(ostream &output, const string &basename) { - // Setting flags to compute what is necessary - if (order == 1 || linear == 1) - { - computeJacobianExo = true; - computeJacobian = false; - } - else if (order != -1 && linear != -1) - { - computeHessian = true; - computeJacobianExo = true; - } - ModelInitialization(output); if (computeHessian) diff --git a/parser.src/ParsingDriver.cc b/parser.src/ParsingDriver.cc index 5d91290f9..21c0860bd 100644 --- a/parser.src/ParsingDriver.cc +++ b/parser.src/ParsingDriver.cc @@ -497,7 +497,6 @@ ParsingDriver::steady() { mod_file->addStatement(new SteadyStatement(options_list)); options_list.clear(); - mod_file->model_tree.computeJacobian = true; } void @@ -527,9 +526,6 @@ ParsingDriver::option_num(const string &name_option, const string &opt) error("option " + name_option + " declared twice"); options_list.num_options[name_option] = opt; - - if (name_option == "order") - mod_file->order = atoi(opt.c_str()); } void @@ -552,7 +548,7 @@ ParsingDriver::option_str(const string &name_option, const string &opt) void ParsingDriver::linear() { - mod_file->linear = 1; + mod_file->linear = true; } void @@ -582,13 +578,6 @@ void ParsingDriver::rplot() void ParsingDriver::stoch_simul() { - // If order and linear not set, then set them to default values - if (mod_file->order == -1) - mod_file->order = 2; - - if (mod_file->linear == -1) - mod_file->linear = 0; - mod_file->addStatement(new StochSimulStatement(*tmp_symbol_table, options_list)); tmp_symbol_table->clear(); options_list.clear(); @@ -599,7 +588,6 @@ ParsingDriver::simul() { mod_file->addStatement(new SimulStatement(options_list)); options_list.clear(); - mod_file->model_tree.computeJacobian = true; } void @@ -607,7 +595,6 @@ ParsingDriver::check() { mod_file->addStatement(new CheckStatement(options_list)); options_list.clear(); - mod_file->model_tree.computeJacobian = true; } void @@ -626,7 +613,6 @@ ParsingDriver::estimated_params() { mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table)); estim_params_list.clear(); - mod_file->model_tree.computeJacobianExo = true; } void @@ -784,7 +770,6 @@ ParsingDriver::run_osr() mod_file->addStatement(new OsrStatement(*tmp_symbol_table, options_list)); tmp_symbol_table->clear(); options_list.clear(); - mod_file->model_tree.computeJacobianExo = true; } void diff --git a/parser.src/Statement.cc b/parser.src/Statement.cc index 540ca93d4..22e049f08 100644 --- a/parser.src/Statement.cc +++ b/parser.src/Statement.cc @@ -1,9 +1,20 @@ #include "Statement.hh" +ModFileStructure::ModFileStructure() : + simul_present(false), + stoch_simul_or_similar_present(false) +{ +} + Statement::~Statement() { } +void +Statement::checkPass(ModFileStructure &mod_file_struct) +{ +} + NativeStatement::NativeStatement(const string &native_statement_arg) : native_statement(native_statement_arg) { diff --git a/parser.src/include/ComputingTasks.hh b/parser.src/include/ComputingTasks.hh index 25d5b8fca..afc01db0c 100644 --- a/parser.src/include/ComputingTasks.hh +++ b/parser.src/include/ComputingTasks.hh @@ -31,6 +31,7 @@ private: const OptionsList options_list; public: SimulStatement(const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const; }; @@ -42,6 +43,7 @@ private: public: StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const; }; @@ -92,6 +94,7 @@ private: public: EstimationStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const; }; @@ -156,6 +159,7 @@ private: public: OsrStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const; }; @@ -167,6 +171,7 @@ private: public: OlrStatement(const TmpSymbolTable &tmp_symbol_table_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const; }; diff --git a/parser.src/include/ModFile.hh b/parser.src/include/ModFile.hh index bacbe630b..125e3d779 100644 --- a/parser.src/include/ModFile.hh +++ b/parser.src/include/ModFile.hh @@ -26,17 +26,20 @@ public: NumericalConstants num_constants; //! Model equations and their derivatives ModelTree model_tree; - //! Option order - int order; //! Option linear - int linear; + bool linear; + private: //! List of statements vector statements; + //! Structure of the mod file + ModFileStructure mod_file_struct; public: //! Add a statement void addStatement(Statement *st); + //! Do some checking and fills mod_file_struct + void checkPass(); //! Writes Matlab/Scilab output files /*! \param basename The base name used for writing output files. Should be the name of the mod file without its extension diff --git a/parser.src/include/ModelTree.hh b/parser.src/include/ModelTree.hh index f2ac788e2..ab69aa98e 100644 --- a/parser.src/include/ModelTree.hh +++ b/parser.src/include/ModelTree.hh @@ -60,8 +60,6 @@ private : /*! Gets expression of part of model tree */ inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1); inline int optimize(NodeID id); - /*! When Hessian is writen this flag is set to true */ - bool computeHessian; /*! Opens output M files (1st and 2nd derivatives) */ void OpenMFiles(std::string iModelFileName1, std::string iModelFileName2 = ""); /*! Opens output C files (1st and 2nd derivatives) */ @@ -92,12 +90,14 @@ public: ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants); //! Destructor ~ModelTree(); - //! When Jacobian (vs endogenous) is written this flag is set to true + //! Whether Jacobian (vs endogenous) should be written bool computeJacobian; - //! When Jacobian (vs endogenous and exogenous) is written this flag is set to true + //! Whether Jacobian (vs endogenous and exogenous) should be written bool computeJacobianExo; + //! Whether Hessian (vs endogenous and exogenous) should be written + bool computeHessian; //! Writes model initialization to output and uses basename for dumping model static/dynamic files - void writeOutput(std::ostream &output, const std::string &basename, int order, int linear); + void writeOutput(std::ostream &output, const std::string &basename); }; //------------------------------------------------------------------------------ #endif diff --git a/parser.src/include/Statement.hh b/parser.src/include/Statement.hh index fb170ba49..c2ce403a2 100644 --- a/parser.src/include/Statement.hh +++ b/parser.src/include/Statement.hh @@ -7,10 +7,22 @@ using namespace std; #include #include +class ModFileStructure +{ +public: + ModFileStructure(); + //! Whether a simul statement is present + bool simul_present; + //! Whether a stoch_simul, estimation, olr, osr statement is present + bool stoch_simul_or_similar_present; +}; + class Statement { public: virtual ~Statement(); + //! Do some internal check, and fill the ModFileStructure class + virtual void checkPass(ModFileStructure &mod_file_struct); virtual void writeOutput(ostream &output) const = 0; };