From 349834eecaa003d05c89f207fa6c738bfc3a37fc Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 30 Aug 2017 11:32:01 +0200 Subject: [PATCH] preprocessor: add write_latex_steady_steade_model command. Closes #1496 --- doc/dynare.texi | 34 ++++++++++++++++++--- preprocessor/ComputingTasks.cc | 17 +++++++++++ preprocessor/ComputingTasks.hh | 11 +++++++ preprocessor/DynareBison.yy | 7 ++++- preprocessor/DynareFlex.ll | 1 + preprocessor/ParsingDriver.cc | 6 ++++ preprocessor/ParsingDriver.hh | 2 ++ preprocessor/StaticModel.cc | 17 +++++++++++ preprocessor/StaticModel.hh | 1 + preprocessor/SteadyStateModel.cc | 51 ++++++++++++++++++++++++++++++++ preprocessor/SteadyStateModel.hh | 4 ++- 11 files changed, 145 insertions(+), 6 deletions(-) diff --git a/doc/dynare.texi b/doc/dynare.texi index 63c0bc2e9..394caa78e 100644 --- a/doc/dynare.texi +++ b/doc/dynare.texi @@ -2257,10 +2257,11 @@ Note that the model written in the @TeX{} file will differ from the model declared by the user in the some dimensions (@pxref{write_latex_dynamic_model} for details). -Also note that this command will not output the contents of the -optional @code{steady_state_model} block (@pxref{steady_state_model}); -it will rather output a static version (@i{i.e.} without leads and -lags) of the dynamic model declared in the @code{model} block. +Also note that this command will not output the contents of the optional +@code{steady_state_model} block (@pxref{steady_state_model}); it will rather +output a static version (@i{i.e.} without leads and lags) of the dynamic model +declared in the @code{model} block. To write the @LaTeX{} contents of the +@code{steady_state_model} @pxref{write_latex_steady_state_model}. For the required @LaTeX{} packages, @pxref{write_latex_original_model}. @@ -2275,6 +2276,31 @@ For the required @LaTeX{} packages, @pxref{write_latex_original_model}. @end deffn +@anchor{write_latex_steady_state_model} +@deffn Command write_latex_steady_state_model ; + +@descriptionhead + +This command creates two @LaTeX{} files: one containing the steady state model +and one containing the @LaTeX{} document header information. + +If your @file{.mod} file is @file{@var{FILENAME}.mod}, then Dynare will create +a file called @file{@var{FILENAME}_steady_state.tex}, which includes a file +called @file{@var{FILENAME}_steady_state_content.tex} (also created by Dynare) +containing the list of all the steady state model equations. + +If @LaTeX{} names were given for variables and parameters (@pxref{Variable +declarations}), then those will be used; otherwise, the plain text names will +be used. + +Note that the model written in the @TeX{} file will differ from the model +declared by the user in the some dimensions (@pxref{write_latex_dynamic_model} +for details). + +For the required @LaTeX{} packages, @pxref{write_latex_original_model}. + +@end deffn + @node Auxiliary variables @section Auxiliary variables diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 400630bbc..c3390c274 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -2377,6 +2377,23 @@ 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::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/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index 2e5a69700..e371284f3 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -27,6 +27,7 @@ #include "Statement.hh" #include "StaticModel.hh" #include "DynamicModel.hh" +#include "SteadyStateModel.hh" class SteadyStatement : public Statement { @@ -625,6 +626,16 @@ 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 writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; + virtual void writeJsonOutput(ostream &output) const; +}; + class ShockDecompositionStatement : public Statement { private: diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 0c1e6f2b1..df616c753 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -130,7 +130,7 @@ class ParsingDriver; %token 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 PREDETERMINED_VARIABLES PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE -%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL +%token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL WRITE_LATEX_STEADY_STATE_MODEL %token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER COLORMAP QOQ YOY AOA %left COMMA %left EQUAL_EQUAL EXCLAMATION_EQUAL @@ -261,6 +261,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 @@ -2142,6 +2143,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/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 092d39d9b..76e9686c9 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -131,6 +131,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 write_latex_dynamic_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_DYNAMIC_MODEL;} write_latex_static_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STATIC_MODEL;} write_latex_original_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_ORIGINAL_MODEL;} +write_latex_steady_state_model {BEGIN DYNARE_STATEMENT; return token::WRITE_LATEX_STEADY_STATE_MODEL;} steady {BEGIN DYNARE_STATEMENT; return token::STEADY;} check {BEGIN DYNARE_STATEMENT; return token::CHECK;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index f2615caee..565db4b1a 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1978,6 +1978,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/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 74ee8ff66..2504b8084 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -568,6 +568,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/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 97f4f38b5..37133f8a3 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/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(aux_equations[i])->containsExternalFunction()) + dynamic_cast(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(aux_equations[i]->substituteStaticAuxiliaryDefinition())->writeOutput(output, oLatexStaticModel); + output << endl << "\\end{dmath}" << endl; + } +} + void StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) const { diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index 8c0e9d443..09abf2067 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/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/preprocessor/SteadyStateModel.cc b/preprocessor/SteadyStateModel.cc index 0a6e1536d..227929838 100644 --- a/preprocessor/SteadyStateModel.cc +++ b/preprocessor/SteadyStateModel.cc @@ -104,6 +104,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, expr_t> >::const_iterator it = def_table.begin(); + it != def_table.end(); it++) + for (vector::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/preprocessor/SteadyStateModel.hh b/preprocessor/SteadyStateModel.hh index a7e0a5424..f0782bc1f 100644 --- a/preprocessor/SteadyStateModel.hh +++ b/preprocessor/SteadyStateModel.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2015 Dynare Team + * Copyright (C) 2010-2017 Dynare Team * * This file is part of Dynare. * @@ -50,6 +50,8 @@ public: */ 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