diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 31663c14c21f448ab957a39a458637d0991318db..5acaeae498482ade782013dd5293c688a8508501 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -5388,7 +5388,8 @@ DynamicModel::writeParamsDerivativesFile(const string &basename, bool julia) con deriv_node_temp_terms_t tef_terms; writeModelLocalVariableTemporaryTerms(temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); - writeTemporaryTerms(params_derivs_temporary_terms, temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); + for (auto it : { make_pair(0,1), make_pair(1,1), make_pair(0,2), make_pair(1,2), make_pair(2,1) }) + writeTemporaryTerms(params_derivs_temporary_terms.find(it)->second, temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); for (const auto & residuals_params_derivative : params_derivatives.find({ 0, 1 })->second) { @@ -6541,9 +6542,11 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) deriv_node_temp_terms_t tef_terms; writeJsonModelLocalVariables(model_local_vars_output, tef_terms); - temporary_terms_t temp_terms_empty; + temporary_terms_t temp_term_union; string concat = "all"; - writeJsonTemporaryTerms(params_derivs_temporary_terms, temp_terms_empty, model_output, tef_terms, concat); + for (auto it : { make_pair(0,1), make_pair(1,1), make_pair(0,2), make_pair(1,2), make_pair(2,1) }) + writeJsonTemporaryTerms(params_derivs_temporary_terms.find(it)->second, temp_term_union, model_output, tef_terms, concat); + jacobian_output << "\"deriv_wrt_params\": {" << " \"neqs\": " << equations.size() << ", \"nparamcols\": " << symbol_table.param_nbr() @@ -6571,7 +6574,7 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) jacobian_output << ", \"param\": \"" << symbol_table.getName(getSymbIDByDerivID(param)) << "\""; jacobian_output << ", \"val\": \""; - d1->writeJsonOutput(jacobian_output, params_derivs_temporary_terms, tef_terms); + d1->writeJsonOutput(jacobian_output, temp_term_union, tef_terms); jacobian_output << "\"}" << endl; } jacobian_output << "]}"; @@ -6608,7 +6611,7 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param\": \"" << symbol_table.getName(getSymbIDByDerivID(param)) << "\""; hessian_output << ", \"val\": \""; - d2->writeJsonOutput(hessian_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(hessian_output, temp_term_union, tef_terms); hessian_output << "\"}" << endl; } hessian_output << "]}"; @@ -6643,7 +6646,7 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param2\": \"" << symbol_table.getName(getSymbIDByDerivID(param2)) << "\""; hessian1_output << ", \"val\": \""; - d2->writeJsonOutput(hessian1_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(hessian1_output, temp_term_union, tef_terms); hessian1_output << "\"}" << endl; } hessian1_output << "]}"; @@ -6684,7 +6687,7 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param2\": \"" << symbol_table.getName(getSymbIDByDerivID(param2)) << "\""; third_derivs_output << ", \"val\": \""; - d2->writeJsonOutput(third_derivs_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(third_derivs_output, temp_term_union, tef_terms); third_derivs_output << "\"}" << endl; } third_derivs_output << "]}" << endl; @@ -6726,7 +6729,7 @@ DynamicModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param\": \"" << symbol_table.getName(getSymbIDByDerivID(param)) << "\""; third_derivs1_output << ", \"val\": \""; - d2->writeJsonOutput(third_derivs1_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(third_derivs1_output, temp_term_union, tef_terms); third_derivs1_output << "\"}" << endl; } third_derivs1_output << "]}" << endl; diff --git a/src/ModelTree.cc b/src/ModelTree.cc index 22741cc86a447af71cc4ca83a0eba947c36948de..0216cb614571e5f69faa16135dc28dd01afb0a1d 100644 --- a/src/ModelTree.cc +++ b/src/ModelTree.cc @@ -75,9 +75,7 @@ ModelTree::copyHelper(const ModelTree &m) for (const auto & it : m.temporary_terms_idxs) temporary_terms_idxs[f(it.first)] = it.second; for (const auto & it : m.params_derivs_temporary_terms) - params_derivs_temporary_terms.insert(f(it)); - for (const auto & it : m.params_derivs_temporary_terms_split) - params_derivs_temporary_terms_split[it.first] = convert_temporary_terms_t(it.second); + params_derivs_temporary_terms[it.first] = convert_temporary_terms_t(it.second); for (const auto & it : m.params_derivs_temporary_terms_idxs) params_derivs_temporary_terms_idxs[f(it.first)] = it.second; @@ -126,7 +124,6 @@ ModelTree::operator=(const ModelTree &m) temporary_terms_mlv.clear(); temporary_terms_derivatives.clear(); params_derivs_temporary_terms.clear(); - params_derivs_temporary_terms_split.clear(); params_derivs_temporary_terms_idxs.clear(); trend_symbols_map.clear(); @@ -2141,13 +2138,15 @@ void ModelTree::computeParamsDerivativesTemporaryTerms() { map<expr_t, pair<int, NodeTreeReference >> reference_count; - params_derivs_temporary_terms.clear(); map<NodeTreeReference, temporary_terms_t> temp_terms_map; - temp_terms_map[NodeTreeReference::residualsParamsDeriv] = params_derivs_temporary_terms_split[{ 0, 1 }]; - temp_terms_map[NodeTreeReference::jacobianParamsDeriv] = params_derivs_temporary_terms_split[{ 1, 1 }]; - temp_terms_map[NodeTreeReference::residualsParamsSecondDeriv] = params_derivs_temporary_terms_split[{ 0, 2 }]; - temp_terms_map[NodeTreeReference::jacobianParamsSecondDeriv] = params_derivs_temporary_terms_split[{ 1, 2 }]; - temp_terms_map[NodeTreeReference::hessianParamsDeriv] = params_derivs_temporary_terms_split[{ 2, 1}]; + temp_terms_map[NodeTreeReference::residualsParamsDeriv] = params_derivs_temporary_terms[{ 0, 1 }]; + temp_terms_map[NodeTreeReference::jacobianParamsDeriv] = params_derivs_temporary_terms[{ 1, 1 }]; + temp_terms_map[NodeTreeReference::residualsParamsSecondDeriv] = params_derivs_temporary_terms[{ 0, 2 }]; + temp_terms_map[NodeTreeReference::jacobianParamsSecondDeriv] = params_derivs_temporary_terms[{ 1, 2 }]; + temp_terms_map[NodeTreeReference::hessianParamsDeriv] = params_derivs_temporary_terms[{ 2, 1}]; + + /* The temp terms should be constructed in the same order as the for loops in + {Static,Dynamic}Model::write{Json,}ParamsDerivativesFile() */ for (const auto &residuals_params_derivative : params_derivatives[{ 0, 1 }]) residuals_params_derivative.second->computeTemporaryTerms(reference_count, @@ -2174,33 +2173,29 @@ ModelTree::computeParamsDerivativesTemporaryTerms() temp_terms_map, true, NodeTreeReference::hessianParamsDeriv); - for (map<NodeTreeReference, temporary_terms_t>::const_iterator it = temp_terms_map.begin(); - it != temp_terms_map.end(); it++) - params_derivs_temporary_terms.insert(it->second.begin(), it->second.end()); - - params_derivs_temporary_terms_split[{ 0, 1 }] = temp_terms_map[NodeTreeReference::residualsParamsDeriv]; - params_derivs_temporary_terms_split[{ 1, 1 }] = temp_terms_map[NodeTreeReference::jacobianParamsDeriv]; - params_derivs_temporary_terms_split[{ 0, 2 }] = temp_terms_map[NodeTreeReference::residualsParamsSecondDeriv]; - params_derivs_temporary_terms_split[{ 1, 2 }] = temp_terms_map[NodeTreeReference::jacobianParamsSecondDeriv]; - params_derivs_temporary_terms_split[{ 2, 1 }] = temp_terms_map[NodeTreeReference::hessianParamsDeriv]; + params_derivs_temporary_terms[{ 0, 1 }] = temp_terms_map[NodeTreeReference::residualsParamsDeriv]; + params_derivs_temporary_terms[{ 1, 1 }] = temp_terms_map[NodeTreeReference::jacobianParamsDeriv]; + params_derivs_temporary_terms[{ 0, 2 }] = temp_terms_map[NodeTreeReference::residualsParamsSecondDeriv]; + params_derivs_temporary_terms[{ 1, 2 }] = temp_terms_map[NodeTreeReference::jacobianParamsSecondDeriv]; + params_derivs_temporary_terms[{ 2, 1 }] = temp_terms_map[NodeTreeReference::hessianParamsDeriv]; int idx = 0; for (auto &it : temporary_terms_mlv) params_derivs_temporary_terms_idxs[it.first] = idx++; - for (auto tt : params_derivs_temporary_terms_split[{ 0, 1 }]) + for (auto tt : params_derivs_temporary_terms[{ 0, 1 }]) params_derivs_temporary_terms_idxs[tt] = idx++; - for (auto tt : params_derivs_temporary_terms_split[{ 1, 1 }]) + for (auto tt : params_derivs_temporary_terms[{ 1, 1 }]) params_derivs_temporary_terms_idxs[tt] = idx++; - for (auto tt : params_derivs_temporary_terms_split[{ 0, 2 }]) + for (auto tt : params_derivs_temporary_terms[{ 0, 2 }]) params_derivs_temporary_terms_idxs[tt] = idx++; - for (auto tt : params_derivs_temporary_terms_split[{ 1, 2 }]) + for (auto tt : params_derivs_temporary_terms[{ 1, 2 }]) params_derivs_temporary_terms_idxs[tt] = idx++; - for (auto tt : params_derivs_temporary_terms_split[{ 2, 1 }]) + for (auto tt : params_derivs_temporary_terms[{ 2, 1 }]) params_derivs_temporary_terms_idxs[tt] = idx++; } diff --git a/src/ModelTree.hh b/src/ModelTree.hh index 0560f4731425b4f5222fba069ec0ae877722498e..2b2c076a4ea62ebaebb0008492f98634561bd9fb 100644 --- a/src/ModelTree.hh +++ b/src/ModelTree.hh @@ -120,15 +120,9 @@ protected: temporary_terms_idxs_t temporary_terms_idxs; - //! Temporary terms for the file containing parameters derivatives - /*! However does not contain the TT related to model local variables. - TODO: this variable should probably be removed, it is essentially the - same information as in params_derivs_temporary_terms_split */ - temporary_terms_t params_derivs_temporary_terms; - //! Temporary terms for parameter derivatives, under a disaggregated form /*! The pair of integers is to be interpreted as in param_derivatives */ - map<pair<int,int>, temporary_terms_t> params_derivs_temporary_terms_split; + map<pair<int,int>, temporary_terms_t> params_derivs_temporary_terms; temporary_terms_idxs_t params_derivs_temporary_terms_idxs; diff --git a/src/StaticModel.cc b/src/StaticModel.cc index 4d621f3d14f2c21f52c94fba5532cfd55c318745..75106657962debc909bcc3a63e704bc6b2d17e45 100644 --- a/src/StaticModel.cc +++ b/src/StaticModel.cc @@ -2653,7 +2653,8 @@ StaticModel::writeParamsDerivativesFile(const string &basename, bool julia) cons deriv_node_temp_terms_t tef_terms; writeModelLocalVariableTemporaryTerms(temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); - writeTemporaryTerms(params_derivs_temporary_terms, temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); + for (auto it : { make_pair(0,1), make_pair(1,1), make_pair(0,2), make_pair(1,2), make_pair(2,1) }) + writeTemporaryTerms(params_derivs_temporary_terms.find(it)->second, temp_term_union, params_derivs_temporary_terms_idxs, tt_output, output_type, tef_terms); for (const auto & residuals_params_derivative : params_derivatives.find({ 0, 1 })->second) { @@ -3065,9 +3066,11 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) deriv_node_temp_terms_t tef_terms; writeJsonModelLocalVariables(model_local_vars_output, tef_terms); - temporary_terms_t temp_terms_empty; + temporary_terms_t temp_term_union; string concat = "all"; - writeJsonTemporaryTerms(params_derivs_temporary_terms, temp_terms_empty, model_output, tef_terms, concat); + for (auto it : { make_pair(0,1), make_pair(1,1), make_pair(0,2), make_pair(1,2), make_pair(2,1) }) + writeJsonTemporaryTerms(params_derivs_temporary_terms.find(it)->second, temp_term_union, model_output, tef_terms, concat); + jacobian_output << "\"deriv_wrt_params\": {" << " \"neqs\": " << equations.size() << ", \"nparamcols\": " << symbol_table.param_nbr() @@ -3095,7 +3098,7 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) jacobian_output << ", \"param\": \"" << symbol_table.getName(getSymbIDByDerivID(param)) << "\""; jacobian_output << ", \"val\": \""; - d1->writeJsonOutput(jacobian_output, params_derivs_temporary_terms, tef_terms); + d1->writeJsonOutput(jacobian_output, temp_term_union, tef_terms); jacobian_output << "\"}" << endl; } jacobian_output << "]}"; @@ -3130,7 +3133,7 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) hessian_output << ", \"var_col\": " << var_col << ", \"param_col\": " << param_col << ", \"val\": \""; - d2->writeJsonOutput(hessian_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(hessian_output, temp_term_union, tef_terms); hessian_output << "\"}" << endl; } hessian_output << "]}"; @@ -3166,7 +3169,7 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param2\": \"" << symbol_table.getName(getSymbIDByDerivID(param2)) << "\""; hessian1_output << ", \"val\": \""; - d2->writeJsonOutput(hessian1_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(hessian1_output, temp_term_union, tef_terms); hessian1_output << "\"}" << endl; } hessian1_output << "]}"; @@ -3205,7 +3208,7 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param2\": \"" << symbol_table.getName(getSymbIDByDerivID(param2)) << "\""; third_derivs_output << ", \"val\": \""; - d2->writeJsonOutput(third_derivs_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(third_derivs_output, temp_term_union, tef_terms); third_derivs_output << "\"}" << endl; } third_derivs_output << "]}" << endl; @@ -3245,7 +3248,7 @@ StaticModel::writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) << ", \"param1\": \"" << symbol_table.getName(getSymbIDByDerivID(param)) << "\""; third_derivs1_output << ", \"val\": \""; - d2->writeJsonOutput(third_derivs1_output, params_derivs_temporary_terms, tef_terms); + d2->writeJsonOutput(third_derivs1_output, temp_term_union, tef_terms); third_derivs1_output << "\"}" << endl; } third_derivs1_output << "]}" << endl;