From ad81129a9b90dab50799bd4589b635420918cf1f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 30 Nov 2015 15:06:57 +0100 Subject: [PATCH] preprocessor: write auxiliary variable definitions to matlab function --- preprocessor/DynamicModel.cc | 41 +++++++++++++++++++++++++++++++++++- preprocessor/DynamicModel.hh | 3 ++- preprocessor/ModelTree.cc | 20 ++++++++++++++++++ preprocessor/ModelTree.hh | 3 ++- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index cdc8f22d6..a91bba7e8 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1515,6 +1515,42 @@ DynamicModel::writeModelEquationsCode_Block(string &file_name, const string &bin code_file.close(); } +void +DynamicModel::writeDynamicAuxMFile(const string &dynamic_basename) const +{ + string filename = dynamic_basename + "_aux.m"; + ofstream mDynamicAuxFile; + mDynamicAuxFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicAuxFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicAuxFile << "function auxvars = " << dynamic_basename << "_aux(y, x, params, steady_state, it_)" + << endl + << "%" << endl + << "% Status : Computes auxiliary variables for Dynare" << endl + << "%" << endl + << "% Inputs :" << endl + << "% y [#dynamic variables by 1] double vector of endogenous variables in the order stored" << endl + << "% in M_.lead_lag_incidence; see the Manual" << endl + << "% x [nperiods by M_.exo_nbr] double matrix of exogenous variables (in declaration order)" << endl + << "% for all simulation periods" << endl + << "% params [M_.param_nbr by 1] double vector of parameter values in declaration order" << endl + << "% it_ scalar double time period for exogenous variables for which to evaluate the model" << endl + << "%" << endl + << "% Outputs:" << endl + << "% auxvars [length(M_.aux_vars) by 1] definitions of auxiliary variables" << endl + << "%" << endl + << "%" << endl + << "% Warning : this file is generated automatically by Dynare" << endl + << "% from the model file (.mod)" << endl << endl; + + writeModelAuxEquations(mDynamicAuxFile, oMatlabDynamicModel); + mDynamicAuxFile << "end" << endl; + mDynamicAuxFile.close(); +} + void DynamicModel::writeDynamicMFile(const string &dynamic_basename) const { @@ -3523,7 +3559,10 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode else if (julia) writeDynamicJuliaFile(basename); else - writeDynamicMFile(t_basename); + { + writeDynamicMFile(t_basename); + writeDynamicAuxMFile(t_basename); + } } void diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh index fbb18b222..e2672800a 100644 --- a/preprocessor/DynamicModel.hh +++ b/preprocessor/DynamicModel.hh @@ -73,7 +73,8 @@ private: //! Store the derivatives or the chainrule derivatives:map, expr_t> typedef map< pair< int, pair< int, int> >, expr_t> first_chain_rule_derivatives_t; first_chain_rule_derivatives_t first_chain_rule_derivatives; - + //! Writes auxiliary equations file (Matlab version) + void writeDynamicAuxMFile(const string &dynamic_basename) const; //! Writes dynamic model file (Matlab version) void writeDynamicMFile(const string &dynamic_basename) const; //! Writes dynamic model file (Julia version) diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 21c2d509e..f3704e110 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1264,6 +1264,26 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) } } +void +ModelTree::writeModelAuxEquations(ostream &output, ExprNodeOutputType output_type) const +{ + int eq_ind = 0, av_ind = 0; + for (vector::const_iterator it = equations_lineno.begin(); + it != equations_lineno.end(); it++, eq_ind++) + if (*it == -1) + { + expr_t rhs = equations[eq_ind]->get_arg2(); + if (IS_JULIA(output_type)) + output << " @inbounds "; + output << "auxvars" << LEFT_ARRAY_SUBSCRIPT(output_type) + << av_ind++ + ARRAY_SUBSCRIPT_OFFSET(output_type) + << RIGHT_ARRAY_SUBSCRIPT(output_type) + << " = "; + rhs->writeOutput(output, output_type, temporary_terms_t()); + output << ";" << endl; + } +} + void ModelTree::compileModelEquations(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, const map_idx_t &map_idx, bool dynamic, bool steady_dynamic) const { diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh index ea799585c..a485bcd8a 100644 --- a/preprocessor/ModelTree.hh +++ b/preprocessor/ModelTree.hh @@ -173,10 +173,11 @@ protected: void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, map_idx_t map_idx, bool dynamic, bool steady_dynamic) const; //! Adds informations for simulation in a binary file void Write_Inf_To_Bin_File(const string &basename, int &u_count_int, bool &file_open, bool is_two_boundaries, int block_mfs) const; - //! Writes model local variables /*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */ void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const; + //! Writes model auxiliary equations + void writeModelAuxEquations(ostream &output, ExprNodeOutputType output_type) const; //! Writes model equations void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const; //! Compiles model equations