v4 parser:

* added a computing pass
* ModelTree:
  - separated checking pass, computing pass, and output writing
  - reorganized static and dynamic model file output code


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1121 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
sebastien 2006-12-15 17:31:31 +00:00
parent 73d3e17540
commit c445ce3a98
5 changed files with 293 additions and 327 deletions

View File

@ -49,6 +49,9 @@ main(int argc, char** argv)
// Run checking pass // Run checking pass
mod_file->checkPass(); mod_file->checkPass();
// Do computations
mod_file->computingPass();
// FIXME // FIXME
string basename = argv[1]; string basename = argv[1];
basename.erase(basename.size() - 4, 4); basename.erase(basename.size() - 4, 4);

View File

@ -23,6 +23,8 @@ ModFile::addStatement(Statement *st)
void void
ModFile::checkPass() ModFile::checkPass()
{ {
model_tree.checkPass();
for(vector<Statement *>::iterator it = statements.begin(); for(vector<Statement *>::iterator it = statements.begin();
it != statements.end(); it++) it != statements.end(); it++)
(*it)->checkPass(mod_file_struct); (*it)->checkPass(mod_file_struct);
@ -40,7 +42,11 @@ ModFile::checkPass()
cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, olr, osr}" << endl; cerr << "Error: a mod file cannot contain both a simul command and one of {stoch_simul, estimation, olr, osr}" << endl;
exit(-1); exit(-1);
} }
}
void
ModFile::computingPass()
{
// Set things to compute // Set things to compute
if (mod_file_struct.simul_present) if (mod_file_struct.simul_present)
model_tree.computeJacobian = true; model_tree.computeJacobian = true;
@ -49,6 +55,8 @@ ModFile::checkPass()
model_tree.computeJacobianExo = true; model_tree.computeJacobianExo = true;
model_tree.computeHessian = true; model_tree.computeHessian = true;
} }
model_tree.computingPass();
} }
void void
@ -123,7 +131,12 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all)
if (linear == 1) if (linear == 1)
mOutputFile << "options_.linear = 1;" << endl; mOutputFile << "options_.linear = 1;" << endl;
model_tree.writeOutput(mOutputFile, basename); model_tree.writeOutput(mOutputFile);
cout << "Processing outputs ..." << endl;
model_tree.writeStaticFile(basename);
model_tree.writeDynamicFile(basename);
// Print statements // Print statements
for(vector<Statement *>::iterator it = statements.begin(); for(vector<Statement *>::iterator it = statements.begin();

View File

@ -46,250 +46,218 @@ ModelTree::ModelTree(SymbolTable &symbol_table_arg,
{ {
} }
//------------------------------------------------------------------------------ void
ModelTree::~ModelTree() ModelTree::writeStaticMFile(const string &static_basename)
{ {
// Empty string filename = static_basename + interfaces::function_file_extension();
}
//------------------------------------------------------------------------------ ofstream mStaticModelFile;
void ModelTree::OpenMFiles(string iModelFileName1, string iModelFileName2) mStaticModelFile.open(filename.c_str(), ios::out | ios::binary);
{ if (!mStaticModelFile.is_open())
if (iModelFileName1.size())
{ {
iModelFileName1 += interfaces::function_file_extension(); cerr << "Error: Can't open file " << filename << " for writing" << endl;
mStaticModelFile.open(iModelFileName1.c_str(),ios::out|ios::binary);
if (!mStaticModelFile.is_open())
{
cout << "ModelTree::Open : Error : Can't open file " << iModelFileName1
<< " for writing\n";
exit(-1);
}
iModelFileName1.erase(iModelFileName1.end()-2,iModelFileName1.end());
//Writing comments and function definition command
mStaticModelFile << "function [residual, g1] = " << iModelFileName1 << "( y, x )\n";
mStaticModelFile << interfaces::comment()+"\n"+interfaces::comment();
mStaticModelFile << "Status : Computes static model for Dynare\n" << interfaces::comment() << "\n";
mStaticModelFile << interfaces::comment();
mStaticModelFile << "Warning : this file is generated automatically by Dynare\n";
mStaticModelFile << interfaces::comment();
mStaticModelFile << " from model file (.mod)\n\n";
if (iModelFileName2.size() && (computeJacobian||computeJacobianExo||computeHessian))
{
iModelFileName2 += interfaces::function_file_extension();
mDynamicModelFile.open(iModelFileName2.c_str(),ios::out|ios::binary);
if (!mDynamicModelFile.is_open())
{
cout << "ModelTree::Open : Error : Can't open file " << iModelFileName2
<< " for writing\n";
exit(-1);
}
iModelFileName2.erase(iModelFileName2.end()-2,iModelFileName2.end());
mDynamicModelFile << "function [residual, g1, g2] = " << iModelFileName2 << "(y, x)\n";
mDynamicModelFile << interfaces::comment()+"\n"+interfaces::comment();
mDynamicModelFile << "Status : Computes dynamic model for Dynare\n" << interfaces::comment() << "\n";
mDynamicModelFile << interfaces::comment();
mDynamicModelFile << "Warning : this file is generated automatically by Dynare\n";
mDynamicModelFile << interfaces::comment();
mDynamicModelFile << " from model file (.mod)\n\n";
}
}
else
{
cout << "ModelTree::Open : Error : Missing file name\n";
exit(-1); exit(-1);
} }
// Writing comments and function definition command
mStaticModelFile << "function [residual, g1] = " << static_basename << "( y, x )\n";
mStaticModelFile << interfaces::comment()+"\n"+interfaces::comment();
mStaticModelFile << "Status : Computes static model for Dynare\n" << interfaces::comment() << "\n";
mStaticModelFile << interfaces::comment();
mStaticModelFile << "Warning : this file is generated automatically by Dynare\n";
mStaticModelFile << interfaces::comment();
mStaticModelFile << " from model file (.mod)\n\n";
writeStaticModel(mStaticModelFile);
interfaces::function_close();
mStaticModelFile.close();
} }
//------------------------------------------------------------------------------
void ModelTree::OpenCFiles(string iModelFileName1, string iModelFileName2) void
ModelTree::writeDynamicMFile(const string &dynamic_basename)
{ {
if (iModelFileName1.size()) string filename = dynamic_basename + interfaces::function_file_extension();
ofstream mDynamicModelFile;
mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary);
if (!mDynamicModelFile.is_open())
{ {
iModelFileName1 += ".c"; cerr << "Error: Can't open file " << filename << " for writing" << endl;
mStaticModelFile.open(iModelFileName1.c_str(),ios::out|ios::binary);
if (!mStaticModelFile.is_open())
{
cout << "ModelTree::Open : Error : Can't open file " << iModelFileName1
<< " for writing\n";
exit(-1);
}
iModelFileName1.erase(iModelFileName1.end()-2,iModelFileName1.end());
mStaticModelFile << "/*\n";
mStaticModelFile << " *" << iModelFileName1 << ".c : Computes static model for Dynare\n";
mStaticModelFile << " * Warning : this file is generated automatically by Dynare\n";
mStaticModelFile << " * from model file (.mod)\n\n";
mStaticModelFile << " */\n";
mStaticModelFile << "#include <math.h>\n";
mStaticModelFile << "#include \"mex.h\"\n";
// A flobal variable for model parameters
mStaticModelFile << "double *params;\n";
if (iModelFileName2.size() && (computeJacobian||computeJacobianExo||computeHessian))
{
iModelFileName2 += ".c";
mDynamicModelFile.open(iModelFileName2.c_str(),ios::out|ios::binary);
if (!mDynamicModelFile.is_open())
{
cout << "ModelTree::Open : Error : Can't open file " << iModelFileName2
<< " for writing\n";
exit(-1);
}
iModelFileName2.erase(iModelFileName2.end()-2,iModelFileName2.end());
mDynamicModelFile << "/*\n";
mDynamicModelFile << " *" << iModelFileName2 << ".c : Computes dynamic model for Dynare\n";
mDynamicModelFile << " *\n";
mDynamicModelFile << " * Warning : this file is generated automatically by Dynare\n";
mDynamicModelFile << " * from model file (.mod)\n\n";
mDynamicModelFile << " */\n";
mDynamicModelFile << "#include <math.h>\n";
mDynamicModelFile << "#include \"mex.h\"\n";
// A flobal variable for model parameters
mDynamicModelFile << "double *params;\n";
// A global variable for it_
mDynamicModelFile << "int it_;\n";
mDynamicModelFile << "int nb_row_x;\n";
}
}
else
{
cout << "ModelTree::Open : Error : Missing file name\n";
exit(-1); exit(-1);
} }
mDynamicModelFile << "function [residual, g1, g2] = " << dynamic_basename << "(y, x)\n";
mDynamicModelFile << interfaces::comment()+"\n"+interfaces::comment();
mDynamicModelFile << "Status : Computes dynamic model for Dynare\n" << interfaces::comment() << "\n";
mDynamicModelFile << interfaces::comment();
mDynamicModelFile << "Warning : this file is generated automatically by Dynare\n";
mDynamicModelFile << interfaces::comment();
mDynamicModelFile << " from model file (.mod)\n\n";
writeDynamicModel(mDynamicModelFile);
interfaces::function_close();
mDynamicModelFile.close();
} }
//------------------------------------------------------------------------------ void
void ModelTree::SaveMFiles() ModelTree::writeStaticCFile(const string &static_basename)
{ {
if (mStaticModelFile.is_open()) string filename = static_basename + ".c";
ofstream mStaticModelFile;
mStaticModelFile.open(filename.c_str(), ios::out | ios::binary);
if (!mStaticModelFile.is_open())
{ {
mStaticModelFile << StaticOutput.str(); cerr << "Error: Can't open file " << filename << " for writing" << endl;
interfaces::function_close(); exit(-1);
mStaticModelFile.close();
}
if (mDynamicModelFile.is_open() && (computeJacobian||computeJacobianExo||computeHessian))
{
mDynamicModelFile << DynamicOutput.str();
interfaces::function_close();
mDynamicModelFile.close();
} }
mStaticModelFile << "/*\n";
mStaticModelFile << " * " << filename << " : Computes static model for Dynare\n";
mStaticModelFile << " * Warning : this file is generated automatically by Dynare\n";
mStaticModelFile << " * from model file (.mod)\n\n";
mStaticModelFile << " */\n";
mStaticModelFile << "#include <math.h>\n";
mStaticModelFile << "#include \"mex.h\"\n";
// A flobal variable for model parameters
mStaticModelFile << "double *params;\n";
// Writing the function Static
writeStaticModel(mStaticModelFile);
// Writing the gateway routine
mStaticModelFile << "/* The gateway routine */\n";
mStaticModelFile << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])\n";
mStaticModelFile << "{\n";
mStaticModelFile << " double *y, *x;\n";
mStaticModelFile << " double *residual, *g1;\n";
mStaticModelFile << " mxArray *M_;\n";
mStaticModelFile << "\n";
mStaticModelFile << " /* Create a pointer to the input matrix y. */\n";
mStaticModelFile << " y = mxGetPr(prhs[0]);\n";
mStaticModelFile << "\n";
mStaticModelFile << " /* Create a pointer to the input matrix x. */\n";
mStaticModelFile << " x = mxGetPr(prhs[1]);\n";
mStaticModelFile << "\n";
mStaticModelFile << " residual = NULL;\n";
mStaticModelFile << " if (nlhs >= 1)\n";
mStaticModelFile << " {\n";
mStaticModelFile << " /* Set the output pointer to the output matrix residual. */\n";
mStaticModelFile << " plhs[0] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ",1, mxREAL);\n";
mStaticModelFile << " /* Create a C pointer to a copy of the output matrix residual. */\n";
mStaticModelFile << " residual = mxGetPr(plhs[0]);\n";
mStaticModelFile << " }\n\n";
mStaticModelFile << " g1 = NULL;\n";
mStaticModelFile << " if (nlhs >= 2)\n";
mStaticModelFile << " {\n";
mStaticModelFile << " /* Set the output pointer to the output matrix g1. */\n";
mStaticModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << mod_param.endo_nbr << ", mxREAL);\n";
mStaticModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mStaticModelFile << " g1 = mxGetPr(plhs[1]);\n";
mStaticModelFile << " }\n\n";
mStaticModelFile << " /* Gets model parameters from global workspace of Matlab */\n";
mStaticModelFile << " M_ = mexGetVariable(\"global\",\"M_\");\n";
mStaticModelFile << " if (M_ == NULL ){\n";
mStaticModelFile << " mexPrintf(\"Global variable not found : \");\n";
mStaticModelFile << " mexErrMsgTxt(\"M_ \\n\");\n";
mStaticModelFile << " }\n";
mStaticModelFile << " params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,\"params\")));\n";
mStaticModelFile << " /* Call the C Static. */\n";
mStaticModelFile << " Static(y, x, residual, g1);\n";
mStaticModelFile << "}\n";
mStaticModelFile.close();
} }
//------------------------------------------------------------------------------
void ModelTree::SaveCFiles() void
ModelTree::writeDynamicCFile(const string &dynamic_basename)
{ {
if (mStaticModelFile.is_open()) string filename = dynamic_basename + ".c";
{
// Writing the function Static
mStaticModelFile << StaticOutput.str();
// Writing the gateway routine
mStaticModelFile << "/* The gateway routine */\n";
mStaticModelFile << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])\n";
mStaticModelFile << "{\n";
mStaticModelFile << " double *y, *x;\n";
mStaticModelFile << " double *residual, *g1;\n";
mStaticModelFile << " mxArray *M_;\n";
mStaticModelFile << "\n";
mStaticModelFile << " /* Create a pointer to the input matrix y. */\n";
mStaticModelFile << " y = mxGetPr(prhs[0]);\n";
mStaticModelFile << "\n";
mStaticModelFile << " /* Create a pointer to the input matrix x. */\n";
mStaticModelFile << " x = mxGetPr(prhs[1]);\n";
mStaticModelFile << "\n";
mStaticModelFile << " residual = NULL;\n"; ofstream mDynamicModelFile;
mStaticModelFile << " if (nlhs >= 1)\n"; mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary);
mStaticModelFile << " {\n"; if (!mDynamicModelFile.is_open())
mStaticModelFile << " /* Set the output pointer to the output matrix residual. */\n";
mStaticModelFile << " plhs[0] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ",1, mxREAL);\n";
mStaticModelFile << " /* Create a C pointer to a copy of the output matrix residual. */\n";
mStaticModelFile << " residual = mxGetPr(plhs[0]);\n";
mStaticModelFile << " }\n\n";
mStaticModelFile << " g1 = NULL;\n";
mStaticModelFile << " if (nlhs >= 2)\n";
mStaticModelFile << " {\n";
mStaticModelFile << " /* Set the output pointer to the output matrix g1. */\n";
mStaticModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << mod_param.endo_nbr << ", mxREAL);\n";
mStaticModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mStaticModelFile << " g1 = mxGetPr(plhs[1]);\n";
mStaticModelFile << " }\n\n";
mStaticModelFile << " /* Gets model parameters from global workspace of Matlab */\n";
mStaticModelFile << " M_ = mexGetVariable(\"global\",\"M_\");\n";
mStaticModelFile << " if (M_ == NULL ){\n";
mStaticModelFile << " mexPrintf(\"Global variable not found : \");\n";
mStaticModelFile << " mexErrMsgTxt(\"M_ \\n\");\n";
mStaticModelFile << " }\n";
mStaticModelFile << " params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,\"params\")));\n";
mStaticModelFile << " /* Call the C Static. */\n";
mStaticModelFile << " Static(y, x, residual, g1);\n";
mStaticModelFile << "}\n";
mStaticModelFile.close();
}
if (mDynamicModelFile.is_open() && (computeJacobian||computeJacobianExo||computeHessian))
{ {
// Writing the function body cerr << "Error: Can't open file " << filename << " for writing" << endl;
mDynamicModelFile << DynamicOutput.str(); exit(-1);
// Writing the gateway routine
mDynamicModelFile << "/* The gateway routine */\n";
mDynamicModelFile << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])\n";
mDynamicModelFile << "{\n";
mDynamicModelFile << " double *y, *x;\n";
mDynamicModelFile << " double *residual, *g1, *g2;\n";
mDynamicModelFile << " mxArray *M_;\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " /* Create a pointer to the input matrix y. */\n";
mDynamicModelFile << " y = mxGetPr(prhs[0]);\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " /* Create a pointer to the input matrix x. */\n";
mDynamicModelFile << " x = mxGetPr(prhs[1]);\n";
mDynamicModelFile << " /* Gets number of rows of matrix x. */\n";
mDynamicModelFile << " nb_row_x = mxGetM(prhs[1]);\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " residual = NULL;\n";
mDynamicModelFile << " if (nlhs >= 1)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix residual. */\n";
mDynamicModelFile << " plhs[0] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ",1, mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix residual. */\n";
mDynamicModelFile << " residual = mxGetPr(plhs[0]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " g1 = NULL;\n";
mDynamicModelFile << " if (nlhs >= 2)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix g1. */\n";
if (computeJacobianExo)
mDynamicModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << variable_table.size() << ", mxREAL);\n";
else if (computeJacobian)
mDynamicModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << mod_param.var_endo_nbr << ", mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mDynamicModelFile << " g1 = mxGetPr(plhs[1]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " g2 = NULL;\n";
mDynamicModelFile << " if (nlhs >= 3)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix g2. */\n";
mDynamicModelFile << " plhs[2] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << variable_table.size()*variable_table.size() << ", mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mDynamicModelFile << " g2 = mxGetPr(plhs[2]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " /* Gets model parameters from global workspace of Matlab */\n";
mDynamicModelFile << " M_ = mexGetVariable(\"global\",\"M_\");\n";
mDynamicModelFile << " if (M_ == NULL )\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " mexPrintf(\"Global variable not found : \");\n";
mDynamicModelFile << " mexErrMsgTxt(\"M_ \\n\");\n";
mDynamicModelFile << " }\n";
mDynamicModelFile << " params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,\"params\")));\n";
mDynamicModelFile << " /* Gets it_ from global workspace of Matlab */\n";
mDynamicModelFile << " it_ = (int) floor(mxGetScalar(mexGetVariable(\"global\", \"it_\")))-1;\n";
mDynamicModelFile << " /* Call the C subroutines. */\n";
mDynamicModelFile << " Dynamic(y, x, residual, g1, g2);\n";
mDynamicModelFile << "}\n";
mDynamicModelFile.close();
} }
mDynamicModelFile << "/*\n";
mDynamicModelFile << " * " << filename << " : Computes dynamic model for Dynare\n";
mDynamicModelFile << " *\n";
mDynamicModelFile << " * Warning : this file is generated automatically by Dynare\n";
mDynamicModelFile << " * from model file (.mod)\n\n";
mDynamicModelFile << " */\n";
mDynamicModelFile << "#include <math.h>\n";
mDynamicModelFile << "#include \"mex.h\"\n";
// A flobal variable for model parameters
mDynamicModelFile << "double *params;\n";
// A global variable for it_
mDynamicModelFile << "int it_;\n";
mDynamicModelFile << "int nb_row_x;\n";
// Writing the function body
writeDynamicModel(mDynamicModelFile);
// Writing the gateway routine
mDynamicModelFile << "/* The gateway routine */\n";
mDynamicModelFile << "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])\n";
mDynamicModelFile << "{\n";
mDynamicModelFile << " double *y, *x;\n";
mDynamicModelFile << " double *residual, *g1, *g2;\n";
mDynamicModelFile << " mxArray *M_;\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " /* Create a pointer to the input matrix y. */\n";
mDynamicModelFile << " y = mxGetPr(prhs[0]);\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " /* Create a pointer to the input matrix x. */\n";
mDynamicModelFile << " x = mxGetPr(prhs[1]);\n";
mDynamicModelFile << " /* Gets number of rows of matrix x. */\n";
mDynamicModelFile << " nb_row_x = mxGetM(prhs[1]);\n";
mDynamicModelFile << "\n";
mDynamicModelFile << " residual = NULL;\n";
mDynamicModelFile << " if (nlhs >= 1)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix residual. */\n";
mDynamicModelFile << " plhs[0] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ",1, mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix residual. */\n";
mDynamicModelFile << " residual = mxGetPr(plhs[0]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " g1 = NULL;\n";
mDynamicModelFile << " if (nlhs >= 2)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix g1. */\n";
if (computeJacobianExo)
mDynamicModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << variable_table.size() << ", mxREAL);\n";
else if (computeJacobian)
mDynamicModelFile << " plhs[1] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << mod_param.var_endo_nbr << ", mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mDynamicModelFile << " g1 = mxGetPr(plhs[1]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " g2 = NULL;\n";
mDynamicModelFile << " if (nlhs >= 3)\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " /* Set the output pointer to the output matrix g2. */\n";
mDynamicModelFile << " plhs[2] = mxCreateDoubleMatrix(" << mod_param.eq_nbr << ", " << variable_table.size()*variable_table.size() << ", mxREAL);\n";
mDynamicModelFile << " /* Create a C pointer to a copy of the output matrix g1. */\n";
mDynamicModelFile << " g2 = mxGetPr(plhs[2]);\n";
mDynamicModelFile << " }\n\n";
mDynamicModelFile << " /* Gets model parameters from global workspace of Matlab */\n";
mDynamicModelFile << " M_ = mexGetVariable(\"global\",\"M_\");\n";
mDynamicModelFile << " if (M_ == NULL )\n";
mDynamicModelFile << " {\n";
mDynamicModelFile << " mexPrintf(\"Global variable not found : \");\n";
mDynamicModelFile << " mexErrMsgTxt(\"M_ \\n\");\n";
mDynamicModelFile << " }\n";
mDynamicModelFile << " params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,\"params\")));\n";
mDynamicModelFile << " /* Gets it_ from global workspace of Matlab */\n";
mDynamicModelFile << " it_ = (int) floor(mxGetScalar(mexGetVariable(\"global\", \"it_\")))-1;\n";
mDynamicModelFile << " /* Call the C subroutines. */\n";
mDynamicModelFile << " Dynamic(y, x, residual, g1, g2);\n";
mDynamicModelFile << "}\n";
mDynamicModelFile.close();
} }
//------------------------------------------------------------------------------
void ModelTree::derive(int iOrder) void ModelTree::derive(int iOrder)
{ {
@ -662,8 +630,8 @@ inline NodeID ModelTree::DeriveArgument(NodeID iArg, Type iType, int iVarID)
} }
//------------------------------------------------------------------------------ void
string ModelTree::setStaticModel(void) ModelTree::writeStaticModel(ostream &StaticOutput)
{ {
TreeIterator tree_it; TreeIterator tree_it;
int lEquationNBR = 0; int lEquationNBR = 0;
@ -803,11 +771,10 @@ string ModelTree::setStaticModel(void)
StaticOutput << "}\n\n"; StaticOutput << "}\n\n";
} }
current_order = d; current_order = d;
return StaticOutput.str();
} }
//------------------------------------------------------------------------------ void
string ModelTree::setDynamicModel(void) ModelTree::writeDynamicModel(ostream &DynamicOutput)
{ {
TreeIterator tree_it; TreeIterator tree_it;
int lEquationNBR = 0; int lEquationNBR = 0;
@ -1032,7 +999,6 @@ string ModelTree::setDynamicModel(void)
DynamicOutput << "}\n\n"; DynamicOutput << "}\n\n";
} }
current_order = d; current_order = d;
return DynamicOutput.str();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -1155,7 +1121,7 @@ inline string ModelTree::getArgument(NodeID id, Type type, EquationType iEquatio
if (type == eParameter) if (type == eParameter)
{ {
argument << param_name << lpar << (long int)id+offset << rpar; argument << "params" << lpar << (long int)id+offset << rpar;
} }
else if (type == eLocalParameter) else if (type == eLocalParameter)
{ {
@ -1254,34 +1220,8 @@ inline string ModelTree::getArgument(NodeID id, Type type, EquationType iEquatio
} }
void void
ModelTree::ModelInitialization(ostream &output) ModelTree::writeOutput(ostream &output) const
{ {
// Exit if there is no equation in model file*/
if (mod_param.eq_nbr == 0)
{
(* error) ("no equations found in model file");
}
cout << mod_param.eq_nbr << " equation(s) found \n";
// Sorting variable table
variable_table.Sort();
// Setting number of equations in ModelParameters class
// Here no derivative are computed
BeginModel++;
min_cost = 40 * operator_table.cost(token::PLUS, offset);
// Setting format of parentheses
if (offset == 1)
{
lpar = '(';
rpar = ')';
param_name = "params";
}
else
{
lpar = '[';
rpar = ']';
param_name = "params";
}
/* Writing initialisation for M_.lead_lag_incidence matrix /* Writing initialisation for M_.lead_lag_incidence matrix
M_.lead_lag_incidence is a matrix with as many columns as there are M_.lead_lag_incidence is a matrix with as many columns as there are
endogenous variables and as many rows as there are periods in the endogenous variables and as many rows as there are periods in the
@ -1290,13 +1230,7 @@ ModelTree::ModelInitialization(ostream &output)
The matrix elements are equal to zero if a variable isn't present in the The matrix elements are equal to zero if a variable isn't present in the
model at a given period. model at a given period.
*/ */
// Initializing matrix to zero
output << "M_.lead_lag_incidence = ["; output << "M_.lead_lag_incidence = [";
/*
zeros(" <<
mod_param.max_lag+mod_param.max_lead+1 << ", " <<
mod_param.endo_nbr << ");\n";
*/
// Loop on endogenous variables // Loop on endogenous variables
for (int endoID = 0; endoID < mod_param.endo_nbr; endoID++) for (int endoID = 0; endoID < mod_param.endo_nbr; endoID++)
{ {
@ -1308,16 +1242,10 @@ ModelTree::ModelInitialization(ostream &output)
string name = symbol_table.getNameByID(eEndogenous, endoID); string name = symbol_table.getNameByID(eEndogenous, endoID);
// and its variableID if exists with current period // and its variableID if exists with current period
int varID = variable_table.getID(name, lag); int varID = variable_table.getID(name, lag);
//cout << name << " " << varID << " " << lag << " " << variable_table.getPrintIndex(varID)+1 << " " << variable_table.getSortID(varID)+1 << endl; if (varID >= 0)
output << " " << variable_table.getPrintIndex(varID) + 1;
if (varID >=0)
{
output << " " << variable_table.getPrintIndex(varID) + 1;
}
else else
{ output << " 0";
output << " 0";
}
} }
output << ";"; output << ";";
} }
@ -1352,9 +1280,7 @@ ModelTree::ModelInitialization(ostream &output)
output << "oo_.recur_steady_state = zeros(" << mod_param.recur_nbr << ", 1);\n"; output << "oo_.recur_steady_state = zeros(" << mod_param.recur_nbr << ", 1);\n";
} }
if (mod_param.parameter_nbr) if (mod_param.parameter_nbr)
{ output << "M_.params = zeros(" << mod_param.parameter_nbr << ", 1);\n";
output << "M_.params = zeros(" << mod_param.parameter_nbr << ", 1);\n";
}
} }
inline int ModelTree::optimize(NodeID node) inline int ModelTree::optimize(NodeID node)
@ -1389,27 +1315,59 @@ inline int ModelTree::optimize(NodeID node)
} }
void void
ModelTree::writeOutput(ostream &output, const string &basename) ModelTree::checkPass() const
{ {
ModelInitialization(output); // Exit if there is no equation in model file
if (mod_param.eq_nbr == 0)
{
cerr << "No equation found in model file" << endl;
exit(-1);
}
}
void
ModelTree::computingPass()
{
cout << mod_param.eq_nbr << " equation(s) found \n";
// Sorting variable table
variable_table.Sort();
// Setting number of equations in ModelParameters class
// Here no derivative are computed
BeginModel++;
min_cost = 40 * operator_table.cost(token::PLUS, offset);
// Setting format of parentheses
if (offset == 1)
{
lpar = '(';
rpar = ')';
}
else
{
lpar = '[';
rpar = ']';
}
if (computeHessian) if (computeHessian)
derive(2); derive(2);
else else
derive(1); derive(1);
}
cout << "Processing outputs ..." << endl;
setStaticModel(); void
setDynamicModel(); ModelTree::writeStaticFile(const string &basename)
{
if (offset == 0) if (offset)
{ writeStaticMFile(basename + "_static");
OpenCFiles(basename + "_static", basename + "_dynamic"); else
SaveCFiles(); writeStaticCFile(basename + "_static");
} }
else
{ void
OpenMFiles(basename + "_static", basename + "_dynamic"); ModelTree::writeDynamicFile(const string &basename)
SaveMFiles(); {
} if (offset)
writeDynamicMFile(basename + "_dynamic");
else
writeDynamicCFile(basename + "_dynamic");
} }

View File

@ -40,10 +40,13 @@ public:
void addStatement(Statement *st); void addStatement(Statement *st);
//! Do some checking and fills mod_file_struct //! Do some checking and fills mod_file_struct
void checkPass(); void checkPass();
//! Execute computations
void computingPass();
//! Writes Matlab/Scilab output files //! Writes Matlab/Scilab output files
/*! /*!
\param basename The base name used for writing output files. Should be the name of the mod file without its extension \param basename The base name used for writing output files. Should be the name of the mod file without its extension
\param clear_all Should a "clear all" instruction be written to output ? \param clear_all Should a "clear all" instruction be written to output ?
\todo make this method const
*/ */
void writeOutputFiles(const string &basename, bool clear_all); void writeOutputFiles(const string &basename, bool clear_all);
}; };

View File

@ -30,15 +30,6 @@ class ModelTree : public DataTree
private : private :
/*! Stores ID of equations and their derivatives */ /*! Stores ID of equations and their derivatives */
std::vector<std::vector<DerivativeIndex> > mDerivativeIndex; std::vector<std::vector<DerivativeIndex> > mDerivativeIndex;
/*! Output for static model */
std::ostringstream StaticOutput;
/*! Output for dynamic model */
std::ostringstream DynamicOutput;
/*! Output file stream for static model */
std::ofstream mStaticModelFile;
/*! Output file stream for dynamic model */
std::ofstream mDynamicModelFile;
/*! /*!
A token is writen as a temporary expression A token is writen as a temporary expression
if its cost is greater than min_cost if its cost is greater than min_cost
@ -46,8 +37,6 @@ private :
int min_cost; int min_cost;
/*! left and right parentheses can be (,[ or ),] */ /*! left and right parentheses can be (,[ or ),] */
char lpar, rpar; char lpar, rpar;
/*! Name of parameter variables ("params" for C output, and M_.params for Matlab) */
std::string param_name;
//! Reference to model parameters //! Reference to model parameters
ModelParameters &mod_param; ModelParameters &mod_param;
//! Reference to numerical constants table //! Reference to numerical constants table
@ -60,44 +49,44 @@ private :
/*! Gets expression of part of model tree */ /*! Gets expression of part of model tree */
inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1); inline std::string getExpression(NodeID StartID, EquationType iEquationType, int iEquationID = -1);
inline int optimize(NodeID id); inline int optimize(NodeID id);
/*! Opens output M files (1st and 2nd derivatives) */
void OpenMFiles(std::string iModelFileName1, std::string iModelFileName2 = "");
/*! Opens output C files (1st and 2nd derivatives) */
void OpenCFiles(std::string iModelFileName1, std::string iModelFileName2 = "");
/*! Saves output string into output M files */
void SaveMFiles();
/*! Saves output string into output C files */
void SaveCFiles();
/*! Computes derivatives of ModelTree */ /*! Computes derivatives of ModelTree */
void derive(int iOrder); void derive(int iOrder);
/*! //! Writes the static model equations and its derivatives
Writes output file for static model : void writeStaticModel(std::ostream &StaticOutput);
- equations //! Writes the dynamic model equations and its derivatives
- 1st order derivatives with respect to endogenous variables (without lags) void writeDynamicModel(std::ostream &DynamicOutput);
*/ //! Writes static model file (Matlab version)
std::string setStaticModel(void); void writeStaticMFile(const std::string &static_basename);
/*! //! Writes static model file (C version)
Writes output file for dynamic stochastic model : void writeStaticCFile(const std::string &static_basename);
- equations //! Writes dynamic model file (Matlab version)
- 1st order and 2nd order derivatives with respect to endogenous, exogenous, exogenous_det (in specific order) void writeDynamicMFile(const std::string &dynamic_basename);
*/ //! Writes dynamic model file (C version)
std::string setDynamicModel(void); void writeDynamicCFile(const std::string &dynamic_basename);
/*! Writes initialization of various Matlab variables */
void ModelInitialization(std::ostream &output);
public: public:
//! Constructor //! Constructor
ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants); ModelTree(SymbolTable &symbol_table_arg, ModelParameters &mod_param_arg, const NumericalConstants &num_constants);
//! Destructor //! Do some checking
~ModelTree(); void checkPass() const;
//! Whether Jacobian (vs endogenous) should be written //! Whether Jacobian (vs endogenous) should be written
bool computeJacobian; bool computeJacobian;
//! Whether Jacobian (vs endogenous and exogenous) should be written //! Whether Jacobian (vs endogenous and exogenous) should be written
bool computeJacobianExo; bool computeJacobianExo;
//! Whether Hessian (vs endogenous and exogenous) should be written //! Whether Hessian (vs endogenous and exogenous) should be written
bool computeHessian; bool computeHessian;
//! Writes model initialization to output and uses basename for dumping model static/dynamic files //! Execute computations (variable sorting + derivation)
void writeOutput(std::ostream &output, const std::string &basename); /*! You must set computeJacobian, computeJacobianExo and computeHessian to correct values before
calling this function */
void computingPass();
//! Writes model initialization and lead/lag incidence matrix to output
void writeOutput(std::ostream &output) const;
//! Writes static model file
/*! \todo make this method const */
void writeStaticFile(const std::string &basename);
//! Writes dynamic model file
/*! \todo make this method const */
void writeDynamicFile(const std::string &basename);
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#endif #endif