Commit dd679918 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Fix derivs w.r.t. parameters with temporary terms array

parent 61dc80bb
......@@ -2351,6 +2351,7 @@ DynamicModel::writeDynamicModel(const string &dynamic_basename, ostream &Dynamic
writeTemporaryTerms(temporary_terms_res,
temp_term_union,
temporary_terms_idxs,
model_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_res.begin(), temporary_terms_res.end());
......@@ -2364,6 +2365,7 @@ DynamicModel::writeDynamicModel(const string &dynamic_basename, ostream &Dynamic
{
writeTemporaryTerms(temporary_terms_g1,
temp_term_union,
temporary_terms_idxs,
jacobian_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g1.begin(), temporary_terms_g1.end());
......@@ -2387,6 +2389,7 @@ DynamicModel::writeDynamicModel(const string &dynamic_basename, ostream &Dynamic
{
writeTemporaryTerms(temporary_terms_g2,
temp_term_union,
temporary_terms_idxs,
hessian_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g2.begin(), temporary_terms_g2.end());
......@@ -2457,6 +2460,7 @@ DynamicModel::writeDynamicModel(const string &dynamic_basename, ostream &Dynamic
{
writeTemporaryTerms(temporary_terms_g3,
temp_term_union,
temporary_terms_idxs,
third_derivatives_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g3.begin(), temporary_terms_g3.end());
......@@ -4979,8 +4983,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
deriv_node_temp_terms_t tef_terms;
writeModelLocalVariables(model_local_vars_output, output_type, tef_terms);
temporary_terms_t temp_terms_empty;
writeTemporaryTerms(params_derivs_temporary_terms, temp_terms_empty, model_output, output_type, tef_terms);
writeTemporaryTerms(params_derivs_temporary_terms, {}, params_derivs_temporary_terms_idxs, model_output, output_type, tef_terms);
for (first_derivatives_t::const_iterator it = residuals_params_derivatives.begin();
it != residuals_params_derivatives.end(); it++)
......@@ -4993,7 +4996,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
jacobian_output << "rp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << param_col
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << " = ";
d1->writeOutput(jacobian_output, output_type, params_derivs_temporary_terms, tef_terms);
d1->writeOutput(jacobian_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
jacobian_output << ";" << endl;
}
......@@ -5010,7 +5013,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
hessian_output << "gp" << LEFT_ARRAY_SUBSCRIPT(output_type) << eq+1 << ", " << var_col
<< ", " << param_col << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = ";
d2->writeOutput(hessian_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(hessian_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
hessian_output << ";" << endl;
}
......@@ -5034,7 +5037,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl
<< "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(hessian1_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(hessian1_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
hessian1_output << ";" << endl;
}
......@@ -5062,7 +5065,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl
<< "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(third_derivs_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(third_derivs_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
third_derivs_output << ";" << endl;
}
......@@ -5090,7 +5093,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param_col << ";" << endl
<< "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(third_derivs1_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(third_derivs1_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
third_derivs1_output << ";" << endl;
}
......@@ -5159,6 +5162,7 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con
<< "%" << endl
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl
<< "T = NaN(" << params_derivs_temporary_terms_idxs.size() << ",1);" << endl
<< model_local_vars_output.str()
<< model_output.str()
<< "rp = zeros(" << equations.size() << ", "
......
......@@ -1231,6 +1231,7 @@ ModelTree::writeModelLocalVariableTemporaryTerms(const temporary_terms_t &tto, c
void
ModelTree::writeTemporaryTerms(const temporary_terms_t &tt,
const temporary_terms_t &ttm1,
const temporary_terms_idxs_t &tt_idxs,
ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const
{
// Local var used to keep track of temp nodes already written
......@@ -1239,16 +1240,16 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt,
it != tt.end(); it++)
{
if (dynamic_cast<AbstractExternalFunctionNode *>(*it) != NULL)
(*it)->writeExternalFunctionOutput(output, output_type, tt2, temporary_terms_idxs, tef_terms);
(*it)->writeExternalFunctionOutput(output, output_type, tt2, tt_idxs, tef_terms);
if (IS_C(output_type))
output << "double ";
else if (IS_JULIA(output_type))
output << " @inbounds ";
(*it)->writeOutput(output, output_type, tt, temporary_terms_idxs, tef_terms);
(*it)->writeOutput(output, output_type, tt, tt_idxs, tef_terms);
output << " = ";
(*it)->writeOutput(output, output_type, tt2, temporary_terms_idxs, tef_terms);
(*it)->writeOutput(output, output_type, tt2, tt_idxs, tef_terms);
if (IS_C(output_type) || IS_MATLAB(output_type))
output << ";";
......@@ -2010,6 +2011,22 @@ ModelTree::computeParamsDerivativesTemporaryTerms()
params_derivs_temporary_terms_res2 = temp_terms_map[eResidualsParamsSecondDeriv];
params_derivs_temporary_terms_g12 = temp_terms_map[eJacobianParamsSecondDeriv];
params_derivs_temporary_terms_g2 = temp_terms_map[eHessianParamsDeriv];
int idx = 0;
for (auto tt : params_derivs_temporary_terms_res)
params_derivs_temporary_terms_idxs[tt] = idx++;
for (auto tt : params_derivs_temporary_terms_g1)
params_derivs_temporary_terms_idxs[tt] = idx++;
for (auto tt : params_derivs_temporary_terms_res2)
params_derivs_temporary_terms_idxs[tt] = idx++;
for (auto tt : params_derivs_temporary_terms_g12)
params_derivs_temporary_terms_idxs[tt] = idx++;
for (auto tt : params_derivs_temporary_terms_g2)
params_derivs_temporary_terms_idxs[tt] = idx++;
}
bool
......
......@@ -152,6 +152,8 @@ protected:
temporary_terms_t params_derivs_temporary_terms_g12;
temporary_terms_t params_derivs_temporary_terms_g2;
temporary_terms_idxs_t params_derivs_temporary_terms_idxs;
//! Trend variables and their growth factors
map<int, expr_t> trend_symbols_map;
......@@ -185,7 +187,7 @@ protected:
//! Computes temporary terms for the file containing parameters derivatives
void computeParamsDerivativesTemporaryTerms();
//! Writes temporary terms
void writeTemporaryTerms(const temporary_terms_t &tt, const temporary_terms_t &ttm1, ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;
void writeTemporaryTerms(const temporary_terms_t &tt, const temporary_terms_t &ttm1, const temporary_terms_idxs_t &tt_idxs, ostream &output, ExprNodeOutputType output_type, deriv_node_temp_terms_t &tef_terms) const;
void writeJsonTemporaryTerms(const temporary_terms_t &tt, const temporary_terms_t &ttm1, ostream &output, deriv_node_temp_terms_t &tef_terms, string &concat) const;
//! Compiles temporary terms
void compileTemporaryTerms(ostream &code_file, unsigned int &instruction_number, const temporary_terms_t &tt, map_idx_t map_idx, bool dynamic, bool steady_dynamic) const;
......
......@@ -1364,6 +1364,7 @@ StaticModel::writeStaticModel(const string &basename,
writeTemporaryTerms(temporary_terms_res,
temp_term_union,
temporary_terms_idxs,
model_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_res.begin(), temporary_terms_res.end());
......@@ -1378,6 +1379,7 @@ StaticModel::writeStaticModel(const string &basename,
{
writeTemporaryTerms(temporary_terms_g1,
temp_term_union,
temporary_terms_idxs,
jacobian_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g1.begin(), temporary_terms_g1.end());
}
......@@ -1401,6 +1403,7 @@ StaticModel::writeStaticModel(const string &basename,
{
writeTemporaryTerms(temporary_terms_g2,
temp_term_union,
temporary_terms_idxs,
hessian_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g2.begin(), temporary_terms_g2.end());
......@@ -1470,6 +1473,7 @@ StaticModel::writeStaticModel(const string &basename,
{
writeTemporaryTerms(temporary_terms_g3,
temp_term_union,
temporary_terms_idxs,
third_derivatives_tt_output, output_type, tef_terms);
temp_term_union.insert(temporary_terms_g3.begin(), temporary_terms_g3.end());
......@@ -2517,8 +2521,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
deriv_node_temp_terms_t tef_terms;
writeModelLocalVariables(model_local_vars_output, output_type, tef_terms);
temporary_terms_t temp_terms_empty;
writeTemporaryTerms(params_derivs_temporary_terms, temp_terms_empty, model_output, output_type, tef_terms);
writeTemporaryTerms(params_derivs_temporary_terms, {}, params_derivs_temporary_terms_idxs, model_output, output_type, tef_terms);
for (first_derivatives_t::const_iterator it = residuals_params_derivatives.begin();
it != residuals_params_derivatives.end(); it++)
......@@ -2532,7 +2535,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
jacobian_output << "rp" << LEFT_ARRAY_SUBSCRIPT(output_type)
<< eq+1 << ", " << param_col
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << " = ";
d1->writeOutput(jacobian_output, output_type, params_derivs_temporary_terms, tef_terms);
d1->writeOutput(jacobian_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
jacobian_output << ";" << endl;
}
......@@ -2550,7 +2553,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
hessian_output << "gp" << LEFT_ARRAY_SUBSCRIPT(output_type)
<< eq+1 << ", " << var_col << ", " << param_col
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << " = ";
d2->writeOutput(hessian_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(hessian_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
hessian_output << ";" << endl;
}
......@@ -2574,7 +2577,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl
<< "rpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",4"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(hessian1_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(hessian1_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
hessian1_output << ";" << endl;
}
......@@ -2602,7 +2605,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param2_col << ";" << endl
<< "gpp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(third_derivs_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(third_derivs_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
third_derivs_output << ";" << endl;
}
......@@ -2630,7 +2633,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=" << param_col << ";" << endl
<< "hp" << LEFT_ARRAY_SUBSCRIPT(output_type) << i << ",5"
<< RIGHT_ARRAY_SUBSCRIPT(output_type) << "=";
d2->writeOutput(third_derivs1_output, output_type, params_derivs_temporary_terms, tef_terms);
d2->writeOutput(third_derivs1_output, output_type, params_derivs_temporary_terms, params_derivs_temporary_terms_idxs, tef_terms);
third_derivs1_output << ";" << endl;
}
......@@ -2688,6 +2691,7 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons
<< "%" << endl
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl
<< "T = NaN(" << params_derivs_temporary_terms_idxs.size() << ",1);" << endl
<< model_local_vars_output.str()
<< model_output.str()
<< "rp = zeros(" << equations.size() << ", "
......
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