Commit 77590c85 authored by sebastien's avatar sebastien
Browse files

Some restructurings in the preprocessor:

* StaticModel: dropped creation of C file in USE_DLL mode
* StaticModel: added computation of derivatives for block MFS mode
* DynamicModel: catch error codes of mkdir()
* ModelTree: moved "mode" member variable to DynamicModel
* ModFile::writeOutput(): moved manipulations relative to dynamic model file to DynamicModel


git-svn-id: https://www.dynare.org/svn/dynare/trunk@2818 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 95953586
......@@ -81,7 +81,7 @@ void ModelInfoStatement::writeOutput(ostream &output, const string &basename) co
}
SimulStatement::SimulStatement(const OptionsList &options_list_arg, ModelTreeMode mode_arg) :
SimulStatement::SimulStatement(const OptionsList &options_list_arg, DynamicModel::mode_t mode_arg) :
options_list(options_list_arg), mode(mode_arg)
{
}
......@@ -96,7 +96,7 @@ void
SimulStatement::writeOutput(ostream &output, const string &basename) const
{
options_list.writeOutput(output);
if (mode == eStandardMode || mode == eDLLMode)
if (mode == DynamicModel::eStandardMode || mode == DynamicModel::eDLLMode)
output << "simul(oo_.dr);\n";
else
{
......@@ -108,7 +108,7 @@ SimulStatement::writeOutput(ostream &output, const string &basename) const
<< " read_data_;" << endl
<< " end" << endl
<< "end" << endl;
if (mode == eSparseDLLMode)
if (mode == DynamicModel::eSparseDLLMode)
output << "oo_.endo_simul=simulate;" << endl;
else
output << basename << "_dynamic;" << endl;
......@@ -118,7 +118,7 @@ SimulStatement::writeOutput(ostream &output, const string &basename) const
StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
const OptionsList &options_list_arg,
ModelTreeMode mode_arg) :
DynamicModel::mode_t mode_arg) :
symbol_list(symbol_list_arg),
options_list(options_list_arg),
mode(mode_arg)
......@@ -149,7 +149,7 @@ StochSimulStatement::writeOutput(ostream &output, const string &basename) const
{
options_list.writeOutput(output);
symbol_list.writeOutput("var_list_", output);
if (mode == eStandardMode || mode == eDLLMode)
if (mode == DynamicModel::eStandardMode || mode == DynamicModel::eDLLMode)
output << "info = stoch_simul(var_list_);" << endl;
else
output << "info = stoch_simul_sparse(var_list_);" << endl;
......
......@@ -52,9 +52,9 @@ class SimulStatement : public Statement
{
private:
const OptionsList options_list;
const ModelTreeMode mode;
const DynamicModel::mode_t mode;
public:
SimulStatement(const OptionsList &options_list_arg, ModelTreeMode mode_arg);
SimulStatement(const OptionsList &options_list_arg, DynamicModel::mode_t mode_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output, const string &basename) const;
};
......@@ -74,11 +74,11 @@ class StochSimulStatement : public Statement
private:
const SymbolList symbol_list;
const OptionsList options_list;
const ModelTreeMode mode;
const DynamicModel::mode_t mode;
public:
StochSimulStatement(const SymbolList &symbol_list_arg,
const OptionsList &options_list_arg,
ModelTreeMode mode_arg);
DynamicModel::mode_t mode_arg);
virtual void checkPass(ModFileStructure &mod_file_struct);
virtual void writeOutput(ostream &output, const string &basename) const;
};
......
......@@ -21,6 +21,8 @@
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <cerrno>
#include "DynamicModel.hh"
......@@ -41,6 +43,7 @@ DynamicModel::DynamicModel(SymbolTable &symbol_table_arg,
max_exo_lag(0), max_exo_lead(0),
max_exo_det_lag(0), max_exo_det_lead(0),
dynJacobianColsNbr(0),
mode(eStandardMode),
cutoff(1e-15),
markowitz(0.7),
block_triangular(symbol_table_arg, num_constants_arg)
......@@ -2119,8 +2122,22 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput) const
}
void
DynamicModel::writeOutput(ostream &output) const
DynamicModel::writeOutput(ostream &output, const string &basename) const
{
output << "options_.model_mode = " << mode << ";" << endl;
// Erase possible remnants of previous runs
if (mode != eStandardMode)
output << "delete('" << basename << "_dynamic.m');" << endl;
if (mode != eDLLMode)
output << "erase_compiled_function('" + basename + "_dynamic');" << endl;
// Special setup for DLL or Sparse modes
if (mode == eDLLMode)
output << "mex -O LDFLAGS='-pthread -shared -Wl,--no-undefined' " << basename << "_dynamic.c" << endl;
if (mode == eSparseMode || mode == eSparseDLLMode)
output << "addpath " << basename << ";" << endl;
/* Writing initialisation for M_.lead_lag_incidence matrix
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
......@@ -2352,6 +2369,13 @@ DynamicModel::writeOutput(ostream &output) const
<< "M_.NNZDerivatives(3) = " << NNZDerivatives[2] << ";" << endl;
}
void
DynamicModel::writeOutputPostComputing(ostream &output, const string &basename) const
{
if (mode == eSparseMode || mode == eSparseDLLMode)
output << "rmpath " << basename << ";" << endl;
}
void
DynamicModel::evaluateJacobian(const eval_context_type &eval_context, jacob_map *j_m)
{
......@@ -2576,12 +2600,13 @@ DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivative
}
else
if (!no_tmp_terms)
computeTemporaryTerms();
computeTemporaryTerms(mode == eStandardMode);
}
void
DynamicModel::writeDynamicFile(const string &basename) const
{
int r;
switch (mode)
{
case eStandardMode:
......@@ -2589,10 +2614,15 @@ DynamicModel::writeDynamicFile(const string &basename) const
break;
case eSparseMode:
#ifdef _WIN32
mkdir(basename.c_str());
r = mkdir(basename.c_str());
#else
mkdir(basename.c_str(), 0777);
r = mkdir(basename.c_str(), 0777);
#endif
if (r < 0 && errno != EEXIST)
{
perror("ERROR");
exit(EXIT_FAILURE);
}
writeSparseDynamicMFile(basename + "_dynamic", basename, mode);
block_triangular.Free_Block(block_triangular.ModelBlock);
block_triangular.incidencematrix.Free_IM();
......@@ -2604,10 +2634,15 @@ DynamicModel::writeDynamicFile(const string &basename) const
case eSparseDLLMode:
// create a directory to store all the files
#ifdef _WIN32
mkdir(basename.c_str());
r = mkdir(basename.c_str());
#else
mkdir(basename.c_str(), 0777);
r = mkdir(basename.c_str(), 0777);
#endif
if (r < 0 && errno != EEXIST)
{
perror("ERROR");
exit(EXIT_FAILURE);
}
writeModelEquationsCodeOrdered(basename + "_dynamic", block_triangular.ModelBlock, basename, map_idx);
block_triangular.Free_Block(block_triangular.ModelBlock);
block_triangular.incidencematrix.Free_IM();
......@@ -2619,8 +2654,6 @@ DynamicModel::writeDynamicFile(const string &basename) const
void
DynamicModel::toStatic(StaticModel &static_model) const
{
static_model.mode = mode;
// Convert model local variables (need to be done first)
for (map<int, NodeID>::const_iterator it = local_variables_table.begin();
it != local_variables_table.end(); it++)
......@@ -2871,3 +2904,25 @@ DynamicModel::writeLatexFile(const string &basename) const
{
writeLatexModelFile(basename + "_dynamic.tex", oLatexDynamicModel);
}
void
DynamicModel::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const
{
output << LEFT_ARRAY_SUBSCRIPT(output_type);
if (IS_MATLAB(output_type))
output << eq_nb + 1 << ", " << col_nb + 1;
else
output << eq_nb + col_nb * equations.size();
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
}
void
DynamicModel::hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const
{
output << LEFT_ARRAY_SUBSCRIPT(output_type);
if (IS_MATLAB(output_type))
output << row_nb + 1 << ", " << col_nb + 1;
else
output << row_nb + col_nb * NNZDerivatives[1];
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
}
......@@ -30,6 +30,15 @@ using namespace std;
//! Stores a dynamic model
class DynamicModel : public ModelTree
{
public:
//! The modes in which DynamicModel can work
enum mode_t
{
eStandardMode, //!< Standard mode (dynamic file in Matlab)
eSparseMode, //!< Sparse mode (dynamic file in Matlab with block decomposition)
eDLLMode, //!< DLL mode (dynamic file in C)
eSparseDLLMode //!< Sparse DLL mode (dynamic file in C with block decomposition plus a binary file)
};
private:
typedef map<pair<int, int>, int> deriv_id_table_t;
//! Maps a pair (symbol_id, lag) to a deriv ID
......@@ -114,8 +123,18 @@ private:
//! Collect only the first derivatives
map<pair<int, pair<int, int> >, NodeID> collect_first_order_derivatives_endogenous();
//! Helper for writing the Jacobian elements in MATLAB and C
/*! Writes either (i+1,j+1) or [i+j*no_eq] */
void jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const;
//! Helper for writing the sparse Hessian elements in MATLAB and C
/*! Writes either (i+1,j+1) or [i+j*NNZDerivatives[1]] */
void hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const;
public:
DynamicModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants);
//! Mode in which the ModelTree is supposed to work (Matlab, DLL or SparseDLL)
mode_t mode;
//! Adds a variable node
/*! This implementation allows for non-zero lag */
virtual NodeID AddVariable(const string &name, int lag = 0);
......@@ -137,7 +156,9 @@ public:
void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
const eval_context_type &eval_context, bool no_tmp_terms);
//! Writes model initialization and lead/lag incidence matrix to output
void writeOutput(ostream &output) const;
void writeOutput(ostream &output, const string &basename) const;
//! Write statements to be added to the main M-file, after computational tasks
void writeOutputPostComputing(ostream &output, const string &basename) const;
//! Complete set to block decompose the model
BlockTriangular block_triangular;
//! Adds informations for simulation in a binary file
......
......@@ -329,7 +329,6 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
break;
case oMatlabStaticModel:
case oMatlabStaticModelSparse:
case oCStaticModel:
i = tsid + ARRAY_SUBSCRIPT_OFFSET(output_type);
output << "y" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
......@@ -373,7 +372,6 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
break;
case oMatlabStaticModel:
case oMatlabStaticModelSparse:
case oCStaticModel:
output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case oMatlabOutsideModel:
......@@ -408,7 +406,6 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
break;
case oMatlabStaticModel:
case oMatlabStaticModelSparse:
case oCStaticModel:
output << "x" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << RIGHT_ARRAY_SUBSCRIPT(output_type);
break;
case oMatlabOutsideModel:
......
......@@ -55,7 +55,6 @@ enum ExprNodeOutputType
oMatlabDynamicModel, //!< Matlab code, dynamic model declarations
oMatlabStaticModelSparse, //!< Matlab code, static block decomposed mode declaration
oMatlabDynamicModelSparse,//!< Matlab code, dynamic block decomposed mode declaration
oCStaticModel, //!< C code, static model declarations
oCDynamicModel, //!< C code, dynamic model declarations
oMatlabOutsideModel, //!< Matlab code, outside model block (for example in initval)
oLatexStaticModel, //!< LaTeX code, static model declarations
......@@ -68,8 +67,7 @@ enum ExprNodeOutputType
|| (output_type) == oMatlabStaticModelSparse \
|| (output_type) == oMatlabDynamicModelSparse)
#define IS_C(output_type) ((output_type) == oCStaticModel \
|| (output_type) == oCDynamicModel)
#define IS_C(output_type) ((output_type) == oCDynamicModel)
#define IS_LATEX(output_type) ((output_type) == oLatexStaticModel \
|| (output_type) == oLatexDynamicModel)
......
......@@ -216,29 +216,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
<< "delete " << basename << ".log;" << endl
<< "warning warning_old_state" << endl
<< "logname_ = '" << basename << ".log';" << endl
<< "diary " << basename << ".log" << endl
<< "options_.model_mode = " << dynamic_model.mode << ";" << endl;
if (dynamic_model.mode == eSparseMode || dynamic_model.mode == eSparseDLLMode)
mOutputFile << "addpath " << basename << ";" << endl
<< "delete('" << basename << "_dynamic.m');" << endl;
if (dynamic_model.equation_number() > 0)
{
if (dynamic_model.mode == eDLLMode)
mOutputFile << "if exist('" << basename << "_static.c')" << endl
<< " clear " << basename << "_static" << endl
<< " mex -O " << basename << "_static.c" << endl
<< "end" << endl
<< "if exist('" << basename << "_dynamic.c')" << endl
<< " clear " << basename << "_dynamic" << endl
<< " mex -O LDFLAGS='-pthread -shared -Wl,--no-undefined' " << basename << "_dynamic.c" << endl
<< "end" << endl;
else
mOutputFile << "erase_compiled_function('" + basename + "_static');" << endl
<< "erase_compiled_function('" + basename + "_dynamic');" << endl;
}
<< "diary " << basename << ".log" << endl;
cout << "Processing outputs ...";
......@@ -249,10 +227,8 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeOutput(mOutputFile);
static_model.writeStaticFile(basename);
dynamic_model.writeDynamicFile(basename);
dynamic_model.writeParamsDerivativesFile(basename);
dynamic_model.writeOutput(mOutputFile, basename);
static_model.writeOutput(mOutputFile);
}
// Print statements
......@@ -260,14 +236,22 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all) const
it != statements.end(); it++)
(*it)->writeOutput(mOutputFile, basename);
mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl
<< "diary off" << endl;
if (dynamic_model.equation_number() > 0)
dynamic_model.writeOutputPostComputing(mOutputFile, basename);
if (dynamic_model.mode == eSparseMode || dynamic_model.mode == eSparseDLLMode)
mOutputFile << "rmpath " << basename << ";" << endl;
mOutputFile << "save('" << basename << "_results.mat', 'oo_', 'M_', 'options_');" << endl
<< "diary off" << endl
<< endl << "disp(['Total computing time : ' dynsec2hms(toc) ]);" << endl;
mOutputFile << endl << "disp(['Total computing time : ' dynsec2hms(toc) ]);" << endl;
mOutputFile.close();
// Create static and dynamic files
if (dynamic_model.equation_number() > 0)
{
static_model.writeStaticFile(basename);
dynamic_model.writeDynamicFile(basename);
dynamic_model.writeParamsDerivativesFile(basename);
}
cout << "done" << endl;
}
......@@ -26,8 +26,7 @@
ModelTree::ModelTree(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg) :
DataTree(symbol_table_arg, num_constants_arg),
mode(eStandardMode)
DataTree(symbol_table_arg, num_constants_arg)
{
for(int i=0; i < 3; i++)
NNZDerivatives[i] = 0;
......@@ -133,13 +132,11 @@ ModelTree::computeThirdDerivatives(const set<int> &vars)
}
void
ModelTree::computeTemporaryTerms()
ModelTree::computeTemporaryTerms(bool is_matlab)
{
map<NodeID, int> reference_count;
temporary_terms.clear();
bool is_matlab = (mode != eDLLMode);
for (vector<BinaryOpNode *>::iterator it = equations.begin();
it != equations.end(); it++)
(*it)->computeTemporaryTerms(reference_count, temporary_terms, is_matlab);
......@@ -280,25 +277,3 @@ ModelTree::addEquation(NodeID eq)
equations.push_back(beq);
}
void
ModelTree::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const
{
output << LEFT_ARRAY_SUBSCRIPT(output_type);
if (IS_MATLAB(output_type))
output << eq_nb + 1 << ", " << col_nb + 1;
else
output << eq_nb + col_nb * equations.size();
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
}
void
ModelTree::hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const
{
output << LEFT_ARRAY_SUBSCRIPT(output_type);
if (IS_MATLAB(output_type))
output << row_nb + 1 << ", " << col_nb + 1;
else
output << row_nb + col_nb * NNZDerivatives[1];
output << RIGHT_ARRAY_SUBSCRIPT(output_type);
}
......@@ -29,15 +29,6 @@ using namespace std;
#include "DataTree.hh"
//! The three in which ModelTree can work
enum ModelTreeMode
{
eStandardMode, //!< Standard mode (static and dynamic files in Matlab)
eSparseMode, //!< Sparse mode (static file in Matlab, dynamic file in Matlab with block decomposition)
eDLLMode, //!< DLL mode (static and dynamic files in C)
eSparseDLLMode //!< Sparse DLL mode (static file in Matlab, dynamic file in C with block decomposition plus a binary file)
};
//! Shared code for static and dynamic models
class ModelTree : public DataTree
{
......@@ -90,7 +81,7 @@ protected:
//! Write derivative of an equation w.r. to a variable
void writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
//! Computes temporary terms (for all equations and derivatives)
void computeTemporaryTerms();
void computeTemporaryTerms(bool is_matlab);
//! Writes temporary terms
void writeTemporaryTerms(const temporary_terms_type &tt, ostream &output, ExprNodeOutputType output_type) const;
//! Writes model local variables
......@@ -99,21 +90,11 @@ protected:
//! Writes model equations
void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const;
//! Helper for writing the Jacobian elements in MATLAB and C
/*! Writes either (i+1,j+1) or [i+j*no_eq] */
void jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutputType output_type) const;
//! Helper for writing the sparse Hessian elements in MATLAB and C
/*! Writes either (i+1,j+1) or [i+j*NNZDerivatives[1]] */
void hessianHelper(ostream &output, int row_nb, int col_nb, ExprNodeOutputType output_type) const;
//! Writes LaTeX model file
void writeLatexModelFile(const string &filename, ExprNodeOutputType output_type) const;
public:
ModelTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants);
//! Mode in which the ModelTree is supposed to work (Matlab, DLL or SparseDLL)
ModelTreeMode mode;
//! Declare a node as an equation of the model
void addEquation(NodeID eq);
//! Returns the number of equations in the model
......
......@@ -47,6 +47,7 @@ ParsingDriver::set_current_data_tree(DataTree *data_tree_arg)
{
data_tree = data_tree_arg;
model_tree = dynamic_cast<ModelTree *>(data_tree_arg);
dynamic_model = dynamic_cast<DynamicModel *>(data_tree_arg);
}
void
......@@ -182,7 +183,7 @@ ParsingDriver::add_model_variable(string *name, string *olag)
if (type == eUnknownFunction)
error("Symbol " + *name + " is a function name unknown to Dynare. It cannot be used inside model.");
if (type == eExogenous && lag != 0 && (model_tree->mode != eSparseDLLMode && model_tree->mode != eSparseMode))
if (type == eExogenous && lag != 0 && (dynamic_model->mode != DynamicModel::eSparseDLLMode && dynamic_model->mode != DynamicModel::eSparseMode))
warning("Exogenous variable " + *name + " has lead/lag " + *olag);
if (type == eModelLocalVariable && lag != 0)
......@@ -346,23 +347,19 @@ ParsingDriver::forecast()
void
ParsingDriver::use_dll()
{
model_tree->mode = eDLLMode;
dynamic_model->mode = DynamicModel::eDLLMode;
}
void
ParsingDriver::sparse_dll()
{
model_tree->mode = eSparseDLLMode;
/*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr);
model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/
dynamic_model->mode = DynamicModel::eSparseDLLMode;
}
void
ParsingDriver::sparse()
{
model_tree->mode = eSparseMode;
/*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr);
model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/
dynamic_model->mode = DynamicModel::eSparseMode;
}
void
......@@ -620,7 +617,7 @@ ParsingDriver::option_num(const string &name_option, const string &opt)
&& (options_list.num_options.find(name_option) != options_list.num_options.end()))
error("option " + name_option + " declared twice");
if ((name_option == "periods") && (mod_file->dynamic_model.mode == eSparseDLLMode || mod_file->dynamic_model.mode == eSparseMode))
if ((name_option == "periods") && (mod_file->dynamic_model.mode == DynamicModel::eSparseDLLMode || mod_file->dynamic_model.mode == DynamicModel::eSparseMode))
mod_file->dynamic_model.block_triangular.periods = atoi(opt.c_str());
else if (name_option == "cutoff")
mod_file->dynamic_model.cutoff = atof(opt.c_str());
......
......@@ -39,7 +39,7 @@ class ParsingDriver;
#include "Shocks.hh"
#include "SigmaeInitialization.hh"
#include "NumericalInitialization.hh"
#include "ModelTree.hh"
#include "DynamicModel.hh"
using namespace std;
......@@ -94,6 +94,10 @@ private:
/*! It is only a dynamic cast of data_tree pointer, and is therefore null if data_tree is not a ModelTree instance */
ModelTree *model_tree;
//! The dynamic model tree in which to add expressions currently parsed
/*! It is only a dynamic cast of data_tree pointer, and is therefore null if data_tree is not a DynamicModel instance */
DynamicModel *dynamic_model;
//! Sets data_tree and model_tree pointers
void set_current_data_tree(DataTree *data_tree_arg);
......
......@@ -22,6 +22,7 @@
#include <deque>
#include <algorithm>
#include <iterator>
#include <functional>
#ifdef DEBUG
......@@ -41,114 +42,45 @@ using namespace boost;
StaticModel::StaticModel(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg) :
ModelTree(symbol_table_arg, num_constants_arg)
ModelTree(symbol_table_arg, num_constants_arg),
block_mfs(false)
{
}
void
StaticModel::writeStaticMFile(const string &static_basename) const
StaticModel::writeStaticMFile(ostream &output, const string &func_name) const
{
string filename = static_basename + ".m";
ofstream mStaticModelFile;
mStaticModelFile.open(filename.c_str(), ios::out | ios::binary);
if (!mStaticModelFile.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
exit(EXIT_FAILURE);
}
// Writing comments and function definition command
mStaticModelFile << "function [residual, g1, g2] = " << static_basename << "(y, x, params)" << endl
<< "%" << endl
<< "% Status : Computes static model for Dynare" << endl
<< "%" << endl
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl;
writeStaticModel(mStaticModelFile);
mStaticModelFile.close();
}
void
StaticModel::writeStaticCFile(const string &static_basename) const
{
string filename = static_basename + ".c";
ofstream mStaticModelFile;
mStaticModelFile.open(filename.c_str(), ios::out | ios::binary);
if (!mStaticModelFile.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
exit(EXIT_FAILURE);
}
mStaticModelFile << "/*" << endl
<< " * " << filename << " : Computes static model for Dynare" << endl
<< " * Warning : this file is generated automatically by Dynare" << endl
<< " * from model file (.mod)" << endl
<< endl
<< " */" << endl
<< "#include <math.h>" << endl
<< "#include \"mex.h\"" << endl;
// Writing the function Static
writeStaticModel(mStaticModelFile);
// Writing the gateway routine
mStaticModelFile << "/* The gateway routine */" << endl
<< "void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])" << endl
<< "{" << endl
<< " double *y, *x, *params;" << endl
<< " double *residual, *g1;" << endl
<< endl
<< " /* Create a pointer to the input matrix y. */" << endl
<< " y = mxGetPr(prhs[0]);" << endl
<< endl
<< " /* Create a pointer to the input matrix x. */" << endl
<< " x = mxGetPr(prhs[1]);" << endl
<< endl
<< " /* Create a pointer to the input matrix params. */" << endl
<< " params = mxGetPr(prhs[2]);" << endl
<