adding writeResidualsC() for external files

issue#70
Michel Juillard 2015-08-24 12:37:04 +02:00
parent 203ddeba3d
commit 50097bc054
3 changed files with 58 additions and 4 deletions

View File

@ -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 <math.h>" << endl;
mDynamicModelFile << "#include <stdlib.h>" << 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();
}

View File

@ -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)

View File

@ -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)