Commit 43906691 authored by Sébastien Villemot's avatar Sébastien Villemot

JSON: simplification of method for writing temporary terms

parent 489b4e84
......@@ -6785,24 +6785,18 @@ DynamicModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) c
ostringstream third_derivatives_output; // Used for storing third order derivatives equations
deriv_node_temp_terms_t tef_terms;
temporary_terms_t temp_term_empty;
temporary_terms_t temp_term_union = temporary_terms_derivatives[0];
temporary_terms_t temp_term_union_m_1;
temporary_terms_t temp_term_union;
string concat = "";
int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr;
writeJsonModelLocalVariables(model_local_vars_output, tef_terms);
writeJsonTemporaryTerms(temporary_terms_derivatives[0], temp_term_union_m_1, model_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[0], temp_term_union, model_output, tef_terms, "");
model_output << ", ";
writeJsonModelEquations(model_output, true);
// Writing Jacobian
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[1].begin(), temporary_terms_derivatives[1].end());
concat = "jacobian";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, jacobian_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[1], temp_term_union, jacobian_output, tef_terms, "jacobian");
jacobian_output << R"(, "jacobian": {)"
<< R"( "nrows": )" << equations.size()
<< R"(, "ncols": )" << dynJacobianColsNbr
......@@ -6836,10 +6830,7 @@ DynamicModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) c
jacobian_output << "]}";
// Writing Hessian
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[2].begin(), temporary_terms_derivatives[2].end());
concat = "hessian";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, hessian_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[2], temp_term_union, hessian_output, tef_terms, "hessian");
hessian_output << R"(, "hessian": {)"
<< R"( "nrows": )" << equations.size()
<< R"(, "ncols": )" << hessianColsNbr
......@@ -6881,10 +6872,7 @@ DynamicModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) c
hessian_output << "]}";
// Writing third derivatives
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[3].begin(), temporary_terms_derivatives[3].end());
concat = "third_derivatives";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, third_derivatives_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[3], temp_term_union, third_derivatives_output, tef_terms, "third_derivatives");
third_derivatives_output << R"(, "third_derivative": {)"
<< R"( "nrows": )" << equations.size()
<< R"(, "ncols": )" << hessianColsNbr * dynJacobianColsNbr
......@@ -6968,9 +6956,8 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails)
writeJsonModelLocalVariables(model_local_vars_output, tef_terms);
temporary_terms_t temp_term_union;
string concat = "all";
for (const auto &it : params_derivs_temporary_terms)
writeJsonTemporaryTerms(it.second, temp_term_union, model_output, tef_terms, concat);
writeJsonTemporaryTerms(it.second, temp_term_union, model_output, tef_terms, "all");
jacobian_output << R"("deriv_wrt_params": {)"
<< R"( "neqs": )" << equations.size()
......
......@@ -1433,55 +1433,52 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt,
}
void
ModelTree::writeJsonTemporaryTerms(const temporary_terms_t &tt, const temporary_terms_t &ttm1, ostream &output,
deriv_node_temp_terms_t &tef_terms, string &concat) const
ModelTree::writeJsonTemporaryTerms(const temporary_terms_t &tt,
temporary_terms_t &temp_term_union,
ostream &output,
deriv_node_temp_terms_t &tef_terms, const string &concat) const
{
// Local var used to keep track of temp nodes already written
bool wrote_term = false;
temporary_terms_t tt2 = ttm1;
temporary_terms_t tt2 = temp_term_union;
output << R"("external_functions_temporary_terms_)" << concat << R"(": [)";
for (auto it : tt)
if (ttm1.find(it) == ttm1.end())
{
if (dynamic_cast<AbstractExternalFunctionNode *>(it) != nullptr)
{
if (wrote_term)
output << ", ";
vector<string> efout;
it->writeJsonExternalFunctionOutput(efout, tt2, tef_terms);
for (vector<string>::const_iterator it1 = efout.begin(); it1 != efout.end(); it1++)
{
if (it1 != efout.begin())
output << ", ";
output << *it1;
}
wrote_term = true;
}
tt2.insert(it);
}
{
if (dynamic_cast<AbstractExternalFunctionNode *>(it) != nullptr)
{
if (wrote_term)
output << ", ";
vector<string> efout;
it->writeJsonExternalFunctionOutput(efout, tt2, tef_terms);
for (auto it1 = efout.begin(); it1 != efout.end(); ++it1)
{
if (it1 != efout.begin())
output << ", ";
output << *it1;
}
wrote_term = true;
}
tt2.insert(it);
}
tt2 = ttm1;
wrote_term = false;
output << "]"
<< R"(, "temporary_terms_)" << concat << R"(": [)";
for (auto it = tt.begin();
it != tt.end(); it++)
if (ttm1.find(*it) == ttm1.end())
{
if (wrote_term)
output << ", ";
output << R"({"temporary_term": ")";
(*it)->writeJsonOutput(output, tt, tef_terms);
output << R"(")"
<< R"(, "value": ")";
(*it)->writeJsonOutput(output, tt2, tef_terms);
output << R"("})" << endl;
wrote_term = true;
for (const auto &it : tt)
{
if (wrote_term)
output << ", ";
output << R"({"temporary_term": ")";
it->writeJsonOutput(output, tt, tef_terms);
output << R"(")"
<< R"(, "value": ")";
it->writeJsonOutput(output, temp_term_union, tef_terms);
output << R"("})" << endl;
wrote_term = true;
// Insert current node into tt2
tt2.insert(*it);
}
temp_term_union.insert(it);
}
output << "]";
}
......
......@@ -162,7 +162,7 @@ protected:
void computeParamsDerivativesTemporaryTerms();
//! Writes temporary terms
void writeTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, 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;
void writeJsonTemporaryTerms(const temporary_terms_t &tt, temporary_terms_t &temp_term_union, ostream &output, deriv_node_temp_terms_t &tef_terms, const 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;
//! Adds informations for simulation in a binary file
......
......@@ -2815,14 +2815,11 @@ StaticModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) co
ostringstream third_derivatives_output; // Used for storing third order derivatives equations
deriv_node_temp_terms_t tef_terms;
temporary_terms_t temp_term_union = temporary_terms_derivatives[0];
temporary_terms_t temp_term_union_m_1;
string concat = "";
temporary_terms_t temp_term_union;
writeJsonModelLocalVariables(model_local_vars_output, tef_terms);
writeJsonTemporaryTerms(temporary_terms_derivatives[0], temp_term_union_m_1, model_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[0], temp_term_union, model_output, tef_terms, "");
model_output << ", ";
writeJsonModelEquations(model_output, true);
......@@ -2831,10 +2828,7 @@ StaticModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) co
int hessianColsNbr = JacobianColsNbr*JacobianColsNbr;
// Write Jacobian w.r. to endogenous only
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[1].begin(), temporary_terms_derivatives[1].end());
concat = "jacobian";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, jacobian_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[1], temp_term_union, jacobian_output, tef_terms, "jacobian");
jacobian_output << R"(, "jacobian": {)"
<< R"( "nrows": )" << nrows
<< R"(, "ncols": )" << JacobianColsNbr
......@@ -2868,10 +2862,7 @@ StaticModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) co
jacobian_output << "]}";
// Write Hessian w.r. to endogenous only (only if 2nd order derivatives have been computed)
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[2].begin(), temporary_terms_derivatives[2].end());
concat = "hessian";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, hessian_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[2], temp_term_union, hessian_output, tef_terms, "hessian");
hessian_output << R"(, "hessian": {)"
<< R"( "nrows": )" << equations.size()
<< R"(, "ncols": )" << hessianColsNbr
......@@ -2915,10 +2906,7 @@ StaticModel::writeJsonComputingPassOutput(ostream &output, bool writeDetails) co
hessian_output << "]}";
// Writing third derivatives
temp_term_union_m_1 = temp_term_union;
temp_term_union.insert(temporary_terms_derivatives[3].begin(), temporary_terms_derivatives[3].end());
concat = "third_derivatives";
writeJsonTemporaryTerms(temp_term_union, temp_term_union_m_1, third_derivatives_output, tef_terms, concat);
writeJsonTemporaryTerms(temporary_terms_derivatives[3], temp_term_union, third_derivatives_output, tef_terms, "third_derivatives");
third_derivatives_output << R"(, "third_derivative": {)"
<< R"( "nrows": )" << equations.size()
<< R"(, "ncols": )" << hessianColsNbr * JacobianColsNbr
......@@ -2999,9 +2987,8 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails)
writeJsonModelLocalVariables(model_local_vars_output, tef_terms);
temporary_terms_t temp_term_union;
string concat = "all";
for (const auto &it : params_derivs_temporary_terms)
writeJsonTemporaryTerms(it.second, temp_term_union, model_output, tef_terms, concat);
writeJsonTemporaryTerms(it.second, temp_term_union, model_output, tef_terms, "all");
jacobian_output << R"("deriv_wrt_params": {)"
<< R"( "neqs": )" << equations.size()
......
Markdown is supported
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