From 50097bc054b5e5d1fcf19fab2343aa05fd234117 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Mon, 24 Aug 2015 12:37:04 +0200 Subject: [PATCH] adding writeResidualsC() for external files --- DynamicModel.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++--- DynamicModel.hh | 2 ++ ModFile.cc | 3 ++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/DynamicModel.cc b/DynamicModel.cc index 6c506c40..1a6f954b 100644 --- a/DynamicModel.cc +++ b/DynamicModel.cc @@ -4431,6 +4431,55 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d } } +void +DynamicModel::writeResidualsC(const string &basename, bool cuda) const +{ + string filename = basename + "_residuals.c"; + ofstream mDynamicModelFile, mDynamicMexFile; + + mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary); + if (!mDynamicModelFile.is_open()) + { + cerr << "Error: Can't open file " << filename << " for writing" << endl; + exit(EXIT_FAILURE); + } + mDynamicModelFile << "/*" << endl + << " * " << filename << " : Computes residuals of the model for Dynare" << endl + << " *" << endl + << " * Warning : this file is generated automatically by Dynare" << endl + << " * from model " << basename << "(.mod)" << endl + << " */" << endl + << "#include " << endl; + + mDynamicModelFile << "#include " << endl; + + mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl + << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl; + + // Write function definition if oPowerDeriv is used + // even for residuals if doing Ramsey + writePowerDerivCHeader(mDynamicModelFile); + + mDynamicModelFile << "void Residuals(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual)" << endl + << "{" << endl; + + // this is always empty here, but needed by d1->writeOutput + deriv_node_temp_terms_t tef_terms; + + ostringstream model_output; // Used for storing model equations + writeModelEquations(model_output, oCDynamicModel); + + mDynamicModelFile << " double lhs, rhs;" << endl + << endl + << " /* Residual equations */" << endl + << model_output.str() + << "}" << endl; + + writePowerDeriv(mDynamicModelFile, true); + mDynamicModelFile.close(); + +} + void DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const { @@ -4475,14 +4524,16 @@ DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const jacobianHelper(mDynamicModelFile, eq, getDynJacobianCol(var), oCDynamicModel); mDynamicModelFile << "="; - // oCstaticModel makes reference to the static variables - d1->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms); + // oCStaticModel makes reference to the static variables + // oCDynamicModel makes reference to the dynamic variables + d1->writeOutput(mDynamicModelFile, oCDynamicModel, temporary_terms, tef_terms); mDynamicModelFile << ";" << endl; } mDynamicModelFile << "}" << endl; - writePowerDeriv(mDynamicModelFile, true); + // already written in writeResidualsC() + // writePowerDeriv(mDynamicModelFile, true); mDynamicModelFile.close(); } diff --git a/DynamicModel.hh b/DynamicModel.hh index 6f1eefd7..a9655220 100644 --- a/DynamicModel.hh +++ b/DynamicModel.hh @@ -476,6 +476,8 @@ public: void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; //! Writes model initialization and lead/lag incidence matrix to Cpp output void writeCCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const; + //! Writes C file containing residuals + void writeResidualsC(const string &basename, bool cuda) const; //! Writes C file containing first order derivatives of model evaluated at steady state void writeFirstDerivativesC(const string &basename, bool cuda) const; //! Writes C file containing second order derivatives of model evaluated at steady state (compressed sparse column) diff --git a/ModFile.cc b/ModFile.cc index 1b7f286b..a3de63a7 100644 --- a/ModFile.cc +++ b/ModFile.cc @@ -865,7 +865,7 @@ ModFile::writeExternalFilesC(const string &basename, FileOutputType output) cons // static_model.writeParamsDerivativesFileC(basename, cuda); // static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda); - // dynamic_model.writeResidualsC(basename, cuda); + dynamic_model.writeResidualsC(basename, cuda); // dynamic_model.writeParamsDerivativesFileC(basename, cuda); dynamic_model.writeFirstDerivativesC(basename, cuda); @@ -971,6 +971,7 @@ ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) con // dynamic_model.writeResidualsC(basename, cuda); // dynamic_model.writeParamsDerivativesFileC(basename, cuda); + dynamic_model.writeResidualsC(basename, cuda); dynamic_model.writeFirstDerivativesC(basename, cuda); if (output == second)