Commit 96bdf8c3 authored by Michel Juillard's avatar Michel Juillard
Browse files

make sure that 2nd and 3rd order derivatives that haven't been computed are not taken to be zero.

parent 42310f38
......@@ -172,7 +172,8 @@ extern "C" {
mxFldp = mxGetField(M_, 0, "NNZDerivatives");
dparams = (double *) mxGetData(mxFldp);
Vector NNZD(dparams, (int) mxGetM(mxFldp));
if (NNZD[kOrder-1] == -1)
mexErrMsgTxt("The derivatives were not computed for the required order. Make sure that you used the right order option inside the stoch_simul command");
const int jcols = nExog+nEndo+nsPred+nsForw; // Num of Jacobian columns
mxFldp = mxGetField(M_, 0, "var_order_endo_names");
......
......@@ -1146,7 +1146,7 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const
}
void
DynamicModel::writeDynamicCFile(const string &dynamic_basename) const
DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) const
{
string filename = dynamic_basename + ".c";
ofstream mDynamicModelFile;
......@@ -1181,6 +1181,9 @@ DynamicModel::writeDynamicCFile(const string &dynamic_basename) const
<< " double *residual, *g1, *v2, *v3;" << endl
<< " int nb_row_x, it_;" << endl
<< endl
<< " /* Check that no derivatives of higher order than computed are being requested */ " << endl
<< " if (nlhs > " << order + 1 << ") " << endl
<< " mexErrMsgTxt(\"Derivatives of higher order than computed have been requested\"); " << endl
<< " /* Create a pointer to the input matrix y. */" << endl
<< " y = mxGetPr(prhs[0]);" << endl
<< endl
......@@ -1868,7 +1871,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const
}
void
DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll) const
DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order) const
{
/* Writing initialisation for M_.lead_lag_incidence matrix
M_.lead_lag_incidence is a matrix with as many columns as there are
......@@ -2062,10 +2065,21 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
output << "M_.params = repmat(NaN," << symbol_table.param_nbr() << ", 1);" << endl;
// Write number of non-zero derivatives
// Use -1 if the derivatives have not been computed
output << "M_.NNZDerivatives = zeros(3, 1);" << endl
<< "M_.NNZDerivatives(1) = " << NNZDerivatives[0] << ";" << endl
<< "M_.NNZDerivatives(2) = " << NNZDerivatives[1] << ";" << endl
<< "M_.NNZDerivatives(3) = " << NNZDerivatives[2] << ";" << endl;
<< "M_.NNZDerivatives(1) = " << NNZDerivatives[0] << ";" << endl;
if (order > 1)
{
output << "M_.NNZDerivatives(2) = " << NNZDerivatives[1] << ";" << endl;
if (order > 2)
output << "M_.NNZDerivatives(3) = " << NNZDerivatives[2] << ";" << endl;
else
output << "M_.NNZDerivatives(3) = -1;" << endl;
}
else
output << "M_.NNZDerivatives(2) = -1;" << endl
<< "M_.NNZDerivatives(3) = -1;" << endl;
}
map<pair<int, pair<int, int > >, NodeID>
......@@ -2422,7 +2436,7 @@ DynamicModel::collect_block_first_order_derivatives()
}
void
DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll) const
DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const
{
int r;
string t_basename = basename + "_dynamic";
......@@ -2445,7 +2459,7 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode
writeSparseDynamicMFile(t_basename, basename);
}
else if (use_dll)
writeDynamicCFile(t_basename);
writeDynamicCFile(t_basename, order);
else
writeDynamicMFile(t_basename);
}
......
......@@ -108,7 +108,7 @@ private:
void writeDynamicMFile(const string &dynamic_basename) const;
//! Writes dynamic model file (C version)
/*! \todo add third derivatives handling */
void writeDynamicCFile(const string &dynamic_basename) const;
void writeDynamicCFile(const string &dynamic_basename, const int order) const;
//! Writes dynamic model file when SparseDLL option is on
void writeSparseDynamicMFile(const string &dynamic_basename, const string &basename) const;
//! Writes the dynamic model equations and its derivatives
......@@ -247,13 +247,13 @@ public:
void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
const eval_context_type &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode);
//! Writes model initialization and lead/lag incidence matrix to output
void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll) const;
void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order) const;
//! Adds informations for simulation in a binary file
void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename,
const int &num, int &u_count_int, bool &file_open, bool is_two_boundaries) const;
//! Writes dynamic model file
void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll) const;
void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const;
//! Writes file containing parameters derivatives
void writeParamsDerivativesFile(const string &basename) const;
//! Converts to static model (only the equations)
......
......@@ -482,9 +482,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all
if (dynamic_model.equation_number() > 0)
{
if (dynamic_model_needed)
dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll);
dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option);
else
dynamic_model.writeOutput(mOutputFile, basename, false, false, false);
dynamic_model.writeOutput(mOutputFile, basename, false, false, false, mod_file_struct.order_option);
if (!byte_code && !no_static)
static_model.writeOutput(mOutputFile, block);
}
......@@ -527,12 +527,12 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all
if (dynamic_model_needed)
{
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll);
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
dynamic_model.writeParamsDerivativesFile(basename);
}
else
{
dynamic_model.writeDynamicFile(basename, false, false, false);
dynamic_model.writeDynamicFile(basename, false, false, false, mod_file_struct.order_option);
dynamic_model.writeParamsDerivativesFile(basename);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment