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

JSON: simplification of method for writing temporary terms

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