diff --git a/ComputingTasks.cc b/ComputingTasks.cc index e9cc254b9d4c678967172463f7587be7d0fc6894..865502e98e1836a66ebb30c2f37c522540c95b32 100644 --- a/ComputingTasks.cc +++ b/ComputingTasks.cc @@ -2640,6 +2640,29 @@ WriteLatexOriginalModelStatement::writeJsonOutput(ostream &output) const output << "{\"statementName\": \"write_latex_original_model\"}"; } +WriteLatexSteadyStateModelStatement::WriteLatexSteadyStateModelStatement(const SteadyStateModel &steady_state_model_arg) : + steady_state_model(steady_state_model_arg) +{ +} + +void +WriteLatexSteadyStateModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + mod_file_struct.write_latex_steady_state_model_present = true; +} + +void +WriteLatexSteadyStateModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + steady_state_model.writeLatexSteadyStateFile(basename); +} + +void +WriteLatexSteadyStateModelStatement::writeJsonOutput(ostream &output) const +{ + output << "{\"statementName\": \"write_latex_steady_state_model\"}"; +} + ShockDecompositionStatement::ShockDecompositionStatement(const SymbolList &symbol_list_arg, const OptionsList &options_list_arg) : symbol_list(symbol_list_arg), diff --git a/ComputingTasks.hh b/ComputingTasks.hh index 81bb5ef2d5b0aa8c983fc6aec0db84e5201e6835..09e25f4ebb091c7eac368d1a0200ad542523e0e4 100644 --- a/ComputingTasks.hh +++ b/ComputingTasks.hh @@ -27,6 +27,7 @@ #include "Statement.hh" #include "StaticModel.hh" #include "DynamicModel.hh" +#include "SteadyStateModel.hh" class SteadyStatement : public Statement { @@ -678,6 +679,17 @@ public: virtual void writeJsonOutput(ostream &output) const; }; +class WriteLatexSteadyStateModelStatement : public Statement +{ +private: + const SteadyStateModel &steady_state_model; +public: + WriteLatexSteadyStateModelStatement(const SteadyStateModel &steady_state_model_arg); + virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); + virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; + virtual void writeJsonOutput(ostream &output) const; +}; + class ShockDecompositionStatement : public Statement { private: diff --git a/DynareBison.yy b/DynareBison.yy index 7b68dcd330efbc8f1afb59e7e25ab65bb9d5f2e3..6b8007c493c6ac7e348be8c8bf2fa4dbd0539011 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -129,8 +129,8 @@ class ParsingDriver; %token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL %token <string_val> TEX_NAME %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED -%token VALUES VAR VAREXO VAREXO_DET VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECTATION PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE -%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL CROSSEQUATIONS COVARIANCE +%token VALUES VAR VAREXO VAREXO_DET VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECATATION PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE +%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL CROSSEQUATIONS COVARIANCE WRITE_LATEX_STEADY_STATE_MODEL %token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER COLORMAP VAR_MODEL QOQ YOY AOA %left COMMA %left EQUAL_EQUAL EXCLAMATION_EQUAL @@ -265,6 +265,7 @@ statement : parameters | write_latex_dynamic_model | write_latex_static_model | write_latex_original_model + | write_latex_steady_state_model | shock_decomposition | realtime_shock_decomposition | plot_shock_decomposition @@ -2277,6 +2278,10 @@ write_latex_original_model : WRITE_LATEX_ORIGINAL_MODEL ';' { driver.write_latex_original_model(true); } ; +write_latex_steady_state_model : WRITE_LATEX_STEADY_STATE_MODEL ';' + { driver.write_latex_steady_state_model(); } + ; + shock_decomposition : SHOCK_DECOMPOSITION ';' {driver.shock_decomposition(); } | SHOCK_DECOMPOSITION '(' shock_decomposition_options_list ')' ';' diff --git a/DynareFlex.ll b/DynareFlex.ll index e1f405e52110921d9c9ab34a6b68b929723db015..034bda4aed15d0e1fafdde452e7c7862e10e19bf 100644 --- a/DynareFlex.ll +++ b/DynareFlex.ll @@ -133,6 +133,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 <INITIAL>write_latex_dynamic_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_DYNAMIC_MODEL;} <INITIAL>write_latex_static_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STATIC_MODEL;} <INITIAL>write_latex_original_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_ORIGINAL_MODEL;} +<INITIAL>write_latex_steady_state_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STEADY_STATE_MODEL;} <INITIAL>steady {BEGIN DYNARE_STATEMENT; return token::STEADY;} <INITIAL>check {BEGIN DYNARE_STATEMENT; return token::CHECK;} diff --git a/DynareMain.cc b/DynareMain.cc index bee2ce3a3c9b88a476784ca55d38bc44bd98cb61..8176d6f99a5435e336332157d33f6b1b94ca186f 100644 --- a/DynareMain.cc +++ b/DynareMain.cc @@ -39,7 +39,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear_global, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, ConfigFile &config_file, - WarningConsolidation &warnings_arg, bool nostrict, bool check_model_changes, + WarningConsolidation &warnings_arg, bool nostrict, bool stochastic, bool check_model_changes, bool minimal_workspace, bool compute_xrefs, FileOutputType output_mode, LanguageOutputType lang, int params_derivs_order #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) @@ -57,7 +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]" + << " [-D<variable>[=<value>]] [-I/path] [nostrict] [stochastic] [fast] [minimal_workspace] [compute_xrefs] [output=dynamic|first|second|third] [language=C|C++|julia]" << " [params_derivs_order=0|1|2]" #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) << " [cygwin] [msvc] [mingw]" @@ -109,6 +109,7 @@ main(int argc, char **argv) bool parallel_slave_open_mode = false; bool parallel_test = false; bool nostrict = false; + bool stochastic = false; bool check_model_changes = false; bool minimal_workspace = false; bool compute_xrefs = false; @@ -197,6 +198,8 @@ main(int argc, char **argv) parallel_test = true; else if (!strcmp(argv[arg], "nostrict")) nostrict = true; + else if (!strcmp(argv[arg], "stochastic")) + stochastic = true; else if (!strcmp(argv[arg], "fast")) check_model_changes = true; else if (!strcmp(argv[arg], "minimal_workspace")) @@ -365,7 +368,7 @@ main(int argc, char **argv) // Do the rest 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, + parallel, config_file, warnings, nostrict, stochastic, check_model_changes, minimal_workspace, compute_xrefs, output_mode, language, params_derivs_order #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) , cygwin, msvc, mingw diff --git a/DynareMain2.cc b/DynareMain2.cc index e0a9e5392912400927aed3d722131639b7e808d1..6f0e113917835db3c9d1263904e38398ae7db017 100644 --- a/DynareMain2.cc +++ b/DynareMain2.cc @@ -28,7 +28,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear_global, bool no_tmp_terms, bool no_log, bool no_warn, bool warn_uninit, bool console, bool nograph, bool nointeractive, bool parallel, ConfigFile &config_file, - WarningConsolidation &warnings, bool nostrict, bool check_model_changes, + WarningConsolidation &warnings, bool nostrict, bool stochastic, bool check_model_changes, bool minimal_workspace, bool compute_xrefs, FileOutputType output_mode, LanguageOutputType language, int params_derivs_order #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) @@ -45,12 +45,12 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson); // Run checking pass - mod_file->checkPass(nostrict); + mod_file->checkPass(nostrict, stochastic); if (json == checkpass) mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson); // Perform transformations on the model (creation of auxiliary vars and equations) - mod_file->transformPass(nostrict, compute_xrefs || json == transformpass); + mod_file->transformPass(nostrict, stochastic, compute_xrefs || json == transformpass); if (json == transformpass) mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson); diff --git a/ModFile.cc b/ModFile.cc index 9306e32caca4e3d98f59e6cf84b8327497d23e0e..c7b45ac3a19b22c85fd298089e1bbfd09809eca3 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -107,14 +107,21 @@ ModFile::addStatementAtFront(Statement *st) } void -ModFile::checkPass(bool nostrict) +ModFile::checkPass(bool nostrict, bool stochastic) { for (vector<Statement *>::iterator it = statements.begin(); it != statements.end(); it++) (*it)->checkPass(mod_file_struct, warnings); // Check the steady state block - steady_state_model.checkPass(mod_file_struct.ramsey_model_present, warnings); + steady_state_model.checkPass(mod_file_struct, warnings); + + if (mod_file_struct.write_latex_steady_state_model_present && + !mod_file_struct.steady_state_model_present) + { + cerr << "ERROR: You cannot have a write_latex_steady_state_model statement without a steady_state_model block." << endl; + exit(EXIT_FAILURE); + } // If order option has not been set, default to 2 if (!mod_file_struct.order_option) @@ -129,7 +136,8 @@ ModFile::checkPass(bool nostrict) || mod_file_struct.osr_present || mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present - || mod_file_struct.calib_smoother_present; + || mod_file_struct.calib_smoother_present + || stochastic; // Allow empty model only when doing a standalone BVAR estimation if (dynamic_model.equation_number() == 0 @@ -332,7 +340,7 @@ ModFile::checkPass(bool nostrict) } void -ModFile::transformPass(bool nostrict, bool compute_xrefs) +ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs) { // Save the original model (must be done before any model transformations by preprocessor) // - except adl and diff which we always want expanded @@ -410,7 +418,8 @@ ModFile::transformPass(bool nostrict, bool compute_xrefs) || mod_file_struct.osr_present || mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present - || mod_file_struct.calib_smoother_present) + || mod_file_struct.calib_smoother_present + || stochastic ) { // In stochastic models, create auxiliary vars for leads and lags greater than 2, on both endos and exos dynamic_model.substituteEndoLeadGreaterThanTwo(false); diff --git a/ModFile.hh b/ModFile.hh index 7744d92815196d9aefa75a267f707391a5b21ccf..216542cbfa14c212734e94a2dc37fbf20438c945 100644 --- a/ModFile.hh +++ b/ModFile.hh @@ -131,10 +131,10 @@ public: void evalAllExpressions(bool warn_uninit); //! Do some checking and fills mod_file_struct /*! \todo add check for number of equations and endogenous if ramsey_policy is present */ - void checkPass(bool nostrict); + void checkPass(bool nostrict, bool stochastic); //! Perform some transformations on the model (creation of auxiliary vars and equations) /*! \param compute_xrefs if true, equation cross references will be computed */ - void transformPass(bool nostrict, bool compute_xrefs); + void transformPass(bool nostrict, bool stochastic, bool compute_xrefs); //! Execute computations /*! \param no_tmp_terms if true, no temporary terms will be computed in the static and dynamic files */ /*! \param params_derivs_order compute this order of derivs wrt parameters */ diff --git a/ParsingDriver.cc b/ParsingDriver.cc index ae2585f3d9cdcf99845e8413871a333892f3c6fa..e95db642c91c81a2d7d3f0b93d79d68437d1888f 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2175,6 +2175,12 @@ ParsingDriver::write_latex_original_model(bool write_equation_tags) mod_file->addStatement(new WriteLatexOriginalModelStatement(mod_file->original_model, write_equation_tags)); } +void +ParsingDriver::write_latex_steady_state_model() +{ + mod_file->addStatement(new WriteLatexSteadyStateModelStatement(mod_file->steady_state_model)); +} + void ParsingDriver::bvar_density(string *maxnlags) { diff --git a/ParsingDriver.hh b/ParsingDriver.hh index 7ca27e1c29d1e92fc80037f3edab194c177cba26..1ebfff5cab3588f30d814a6e28f9e88dc1bc489b 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -596,6 +596,8 @@ public: void write_latex_static_model(bool write_equation_tags); //! Adds a write_latex_original_model statement void write_latex_original_model(bool write_equation_tags); + //! Adds a write_latex_steady_state_model statement + void write_latex_steady_state_model(); //! BVAR marginal density void bvar_density(string *maxnlags); //! BVAR forecast diff --git a/Statement.cc b/Statement.cc index 14bffcc3bcc197f264062525060dc86605a370b3..8a86f75a96b34ee234a195e99831bdda6d35c701 100644 --- a/Statement.cc +++ b/Statement.cc @@ -59,7 +59,9 @@ ModFileStructure::ModFileStructure() : ms_dsge_present(false), occbin_option(false), orig_eq_nbr(0), - ramsey_eq_nbr(0) + ramsey_eq_nbr(0), + steady_state_model_present(false), + write_latex_steady_state_model_present(false) { } diff --git a/Statement.hh b/Statement.hh index 7e77e12e28a39fe3074263e7b02405ac89f145f7..db7d5606b1bdb6411f96ad14a4a9dc8195cbab3c 100644 --- a/Statement.hh +++ b/Statement.hh @@ -119,6 +119,10 @@ public: int orig_eq_nbr; //! Stores the number of equations added to the Ramsey model int ramsey_eq_nbr; + //! Whether there was a steady_state_model block + bool steady_state_model_present; + //! Whether there is a write_latex_steady_state_model statement present + bool write_latex_steady_state_model_present; }; class Statement diff --git a/StaticModel.cc b/StaticModel.cc index 97f4f38b5e87ea846b5ba6030985c3e8c2472d16..37133f8a3de9d08e996181593c6b69867ce89f66 100644 --- a/StaticModel.cc +++ b/StaticModel.cc @@ -2173,6 +2173,23 @@ StaticModel::writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType } } +void +StaticModel::writeLatexAuxVarRecursiveDefinitions(ostream &output) const +{ + deriv_node_temp_terms_t tef_terms; + temporary_terms_t temporary_terms; + for (int i = 0; i < (int) aux_equations.size(); i++) + if (dynamic_cast<ExprNode *>(aux_equations[i])->containsExternalFunction()) + dynamic_cast<ExprNode *>(aux_equations[i])->writeExternalFunctionOutput(output, oLatexStaticModel, + temporary_terms, tef_terms); + for (int i = 0; i < (int) aux_equations.size(); i++) + { + output << "\\begin{dmath}" << endl; + dynamic_cast<ExprNode *>(aux_equations[i]->substituteStaticAuxiliaryDefinition())->writeOutput(output, oLatexStaticModel); + output << endl << "\\end{dmath}" << endl; + } +} + void StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const { diff --git a/StaticModel.hh b/StaticModel.hh index 8c0e9d4430ba0becd781b85a942e65c8153bb57b..09abf2067c52f27166df8ed6e389117e429d9c0c 100644 --- a/StaticModel.hh +++ b/StaticModel.hh @@ -191,6 +191,7 @@ public: //! Writes definition of the auxiliary variables in a .m or .jl file void writeSetAuxiliaryVariables(const string &basename, const bool julia) const; void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const; + void writeLatexAuxVarRecursiveDefinitions(ostream &output) const; //! To ensure that no exogenous is present in the planner objective //! See #1264 diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index 0a6e1536d0c49e329a1004518550b1e7e409bdf6..a68b2b1c3ddfe9e8c5f5a2c77f3d8c90cba37240 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -56,11 +56,12 @@ SteadyStateModel::addMultipleDefinitions(const vector<int> &symb_ids, expr_t exp } void -SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) const +SteadyStateModel::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) const { if (def_table.size() == 0) return; + mod_file_struct.steady_state_model_present = true; vector<int> so_far_defined; for (size_t i = 0; i < def_table.size(); i++) @@ -74,7 +75,7 @@ SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) c warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl; // Check that expression has no undefined symbol - if (!ramsey_model) + if (!mod_file_struct.ramsey_model_present) { set<int> used_symbols; const expr_t &expr = def_table[i].second; @@ -104,6 +105,57 @@ SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) c } } +void +SteadyStateModel::writeLatexSteadyStateFile(const string &basename) const +{ + ofstream output, content_output; + string filename = basename + "_steady_state.tex"; + string content_basename = basename + "_steady_state_content"; + string content_filename = content_basename + ".tex"; + + output.open(filename.c_str(), ios::out | ios::binary); + if (!output.is_open()) + { + cerr << "ERROR: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + content_output.open(content_filename.c_str(), ios::out | ios::binary); + if (!content_output.is_open()) + { + cerr << "ERROR: Can't open file " << content_filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + + output << "\\documentclass[10pt,a4paper]{article}" << endl + << "\\usepackage[landscape]{geometry}" << endl + << "\\usepackage{fullpage}" << endl + << "\\usepackage{amsfonts}" << endl + << "\\usepackage{breqn}" << endl + << "\\begin{document}" << endl + << "\\footnotesize" << endl; + + for (vector<pair<vector<int>, expr_t> >::const_iterator it = def_table.begin(); + it != def_table.end(); it++) + for (vector<int>::const_iterator it1 = it->first.begin(); it1 != it->first.end(); it1++) + { + int id = *it1; + expr_t value = it->second; + content_output << "\\begin{dmath}" << endl + << symbol_table.getTeXName(id) << " = "; + value->writeOutput(content_output, oLatexStaticModel); + content_output << endl << "\\end{dmath}" << endl; + } + + static_model.writeLatexAuxVarRecursiveDefinitions(content_output); + + output << "\\include{" << content_basename << "}" << endl + << "\\end{document}" << endl; + + output.close(); + content_output.close(); +} + void SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const { diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index a7e0a5424fb7b01bbd9b15cc71e1abe676f91d07..a4b879dc0bd2463bbf6d2a1a64c87530fbd68d55 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2015 Dynare Team + * Copyright (C) 2010-2017 Dynare Team * * This file is part of Dynare. * @@ -21,6 +21,7 @@ #define _STEADY_STATE_MODEL_HH #include "DataTree.hh" +#include "Statement.hh" #include "StaticModel.hh" #include "WarningConsolidation.hh" @@ -43,13 +44,15 @@ public: /*! \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then disable the check on the recursivity of the declarations */ - void checkPass(bool ramsey_model, WarningConsolidation &warnings) const; + void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) const; //! Write the steady state file /*! \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values */ void writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const; void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; + //! Writes LaTeX file with the equations of the dynamic model (for the steady state model) + void writeLatexSteadyStateFile(const string &basename) const; }; #endif