From 789d881cca9f63c153027382850f9dfb02d22cc0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani <houtan@dynare.org> Date: Tue, 28 Jul 2015 17:27:56 +0200 Subject: [PATCH] write steady state model --- ExprNode.cc | 12 ++++++++---- ExprNode.hh | 5 +++-- ModFile.cc | 13 ++++++++++--- SteadyStateModel.cc | 44 +++++++++++++++++++++++++++++++------------- SteadyStateModel.hh | 2 +- 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/ExprNode.cc b/ExprNode.cc index c40f2b6c..7f704691 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -687,8 +687,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oCDynamicSteadyStateOperator: output << "steady_state[" << tsid << "]"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "ys_(" << tsid + 1 << ")"; + output << "ys_" << LEFT_ARRAY_SUBSCRIPT(output_type) << tsid + 1 << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "ys_[" << tsid << "]"; @@ -737,8 +738,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oMatlabDynamicSteadyStateOperator: output << "oo_.exo_steady_state(" << i << ")"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "exo_(" << i << ")"; + output << "exo_" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "exo_[" << i - 1 << "]"; @@ -787,8 +789,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case oMatlabDynamicSteadyStateOperator: output << "oo_.exo_det_steady_state(" << tsid + 1 << ")"; break; + case oJuliaSteadyStateFile: case oSteadyStateFile: - output << "exo_(" << i << ")"; + output << "exo_" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type); break; case oCSteadyStateFile: output << "exo_[" << i - 1 << "]"; @@ -4819,7 +4822,8 @@ ExternalFunctionNode::writeOutput(ostream &output, ExprNodeOutputType output_typ deriv_node_temp_terms_t &tef_terms) const { if (output_type == oMatlabOutsideModel || output_type == oSteadyStateFile - || output_type == oCSteadyStateFile || IS_LATEX(output_type)) + || output_type == oCSteadyStateFile || output_type == oJuliaSteadyStateFile + || IS_LATEX(output_type)) { string name = IS_LATEX(output_type) ? datatree.symbol_table.getTeXName(symb_id) : datatree.symbol_table.getName(symb_id); diff --git a/ExprNode.hh b/ExprNode.hh index c01bb2f9..e979fb07 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -76,8 +76,9 @@ enum ExprNodeOutputType oMatlabDynamicSparseSteadyStateOperator, //!< Matlab code, dynamic block decomposed model, inside a steady state operator oCDynamicSteadyStateOperator, //!< C code, dynamic model, inside a steady state operator oJuliaDynamicSteadyStateOperator, //!< Julia code, dynamic model, inside a steady state operator - oSteadyStateFile, //!< Matlab code, in the generated steady state file - oCSteadyStateFile //!< C code, in the generated steady state file + oSteadyStateFile, //!< Matlab code, in the generated steady state file + oCSteadyStateFile, //!< C code, in the generated steady state file + oJuliaSteadyStateFile //!< Julia code, in the generated steady state file }; #define IS_MATLAB(output_type) ((output_type) == oMatlabStaticModel \ diff --git a/ModFile.cc b/ModFile.cc index 13dabaef..2c6fdb38 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -819,7 +819,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo } // Create steady state file - steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present); + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, false); } cout << "done" << endl; @@ -1092,7 +1092,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << "try" << endl << " using " << basename << "DynamicParamsDerivs" << endl << "catch" << endl - << "end" << endl << endl + << "end" << endl << "export model__" << endl << endl << "model__ = model()" << endl << "model__.fname = \"" << basename << "\"" << endl @@ -1133,6 +1133,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) mod_file_struct.order_option, true); dynamic_model.writeParamsDerivativesFile(basename, true); } + steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present, true); jlOutputFile << "model__.static = " << basename << "Static.getStaticFunction()" << endl << "model__.dynamic = " << basename << "Dynamic.getDynamicFunction()" << endl @@ -1147,7 +1148,13 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output) << " " << basename << "DynamicParamsDerivs.getParamsDerivsFunction()" << endl << " catch" << endl << " function()end" << endl - << " end" << endl << endl + << " end" << endl + << "try" << endl + << " using " << basename << "SteadyState2" << endl + << " model__.steady_state = " << basename + << "SteadyState2.getSteadyStateFunction()" << endl + << "catch" << endl + << "end" << endl << "end" << endl; jlOutputFile.close(); cout << "done" << endl; diff --git a/SteadyStateModel.cc b/SteadyStateModel.cc index fe1a773b..88614ffc 100644 --- a/SteadyStateModel.cc +++ b/SteadyStateModel.cc @@ -105,13 +105,12 @@ SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) c } void -SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model) const +SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const { if (def_table.size() == 0) return; - string filename = basename + "_steadystate2.m"; - + string filename = julia ? basename + "SteadyState2.jl" : basename + "_steadystate2.m"; ofstream output; output.open(filename.c_str(), ios::out | ios::binary); if (!output.is_open()) @@ -120,10 +119,21 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model exit(EXIT_FAILURE); } - output << "function [ys_, params, info] = " << basename << "_steadystate2(" - << "ys_, exo_, params)" << endl - << "% Steady state generated by Dynare preprocessor" << endl - << " info = 0;" << endl; + ExprNodeOutputType output_type = (julia ? oJuliaSteadyStateFile : oSteadyStateFile); + + if (!julia) + output << "function [ys_, params, info] = " << basename << "_steadystate2(" + << "ys_, exo_, params)" << endl + << "% Steady state generated by Dynare preprocessor" << endl + << " info = 0;" << endl; + else + output << "module " << basename << "SteadyState2" << endl << endl + << "export getSteadyStateFunction" << endl << endl + << "function getSteadyStateFunction()" << endl + << " steady_state" << endl + << "end" << endl << endl + << "function steady_state(ys_, exo_, params)" << endl + << " info = 0" << endl; for (size_t i = 0; i < def_table.size(); i++) { @@ -135,7 +145,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model { variable_node_map_t::const_iterator it = variable_node_map.find(make_pair(symb_ids[j], 0)); assert(it != variable_node_map.end()); - dynamic_cast<ExprNode *>(it->second)->writeOutput(output, oSteadyStateFile); + dynamic_cast<ExprNode *>(it->second)->writeOutput(output, output_type); if (j < symb_ids.size()-1) output << ","; } @@ -143,13 +153,21 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model output << "]"; output << "="; - def_table[i].second->writeOutput(output, oSteadyStateFile); + def_table[i].second->writeOutput(output, output_type); output << ";" << endl; } - output << " % Auxiliary equations" << endl; - static_model.writeAuxVarInitval(output, oSteadyStateFile); - output << " check_=0;" << endl - << "end" << endl; + if (!julia) + output << " % Auxiliary equations" << endl; + else + output << " # Auxiliary equations" << endl; + static_model.writeAuxVarInitval(output, output_type); + output << " check_=0;" << endl; + + if (julia) + output << " (ys_, params, info)" << endl; + output << "end" << endl; + if (julia) + output << "end" << endl; } void diff --git a/SteadyStateModel.hh b/SteadyStateModel.hh index 2dd752ba..f79b358f 100644 --- a/SteadyStateModel.hh +++ b/SteadyStateModel.hh @@ -48,7 +48,7 @@ public: /*! \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) const; + void writeSteadyStateFile(const string &basename, bool ramsey_model, bool julia) const; void writeSteadyStateFileC(const string &basename, bool ramsey_model) const; }; -- GitLab