From 42bcf5e446cedae275fa2a8a686bcba70a1cdd7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Tue, 19 Mar 2024 08:57:53 +0000 Subject: [PATCH] Refactor getModuleSizes method and remove commented code --- poet/include/IPhreeqcPOET.hpp | 12 +-- poet/src/IPhreeqcPOET.cpp | 186 ---------------------------------- poet/src/Init.cpp | 52 +++++++++- 3 files changed, 52 insertions(+), 198 deletions(-) delete mode 100644 poet/src/IPhreeqcPOET.cpp diff --git a/poet/include/IPhreeqcPOET.hpp b/poet/include/IPhreeqcPOET.hpp index 1a550242..f31c4ea4 100644 --- a/poet/include/IPhreeqcPOET.hpp +++ b/poet/include/IPhreeqcPOET.hpp @@ -57,8 +57,7 @@ public: }; PhreeqcMat getPhreeqcMat(); - - PhreeqcMat getPhreeqcMat(const std::vector &ids); + // PhreeqcMat getPhreeqcMat(const std::vector &ids); std::map raw_dumps() { std::map dumps; @@ -79,14 +78,7 @@ public: using essential_names = std::array, 5>; using ModulesArray = std::array; - ModulesArray getModuleSizes() const { - ModulesArray module_sizes; - for (std::uint8_t i = 0; i < 5; i++) { - module_sizes[i] = this->initial_names[i].size(); - } - - return module_sizes; - } + ModulesArray getModuleSizes(const std::vector &cell_ids); private: // required only for simulation diff --git a/poet/src/IPhreeqcPOET.cpp b/poet/src/IPhreeqcPOET.cpp deleted file mode 100644 index 099c0301..00000000 --- a/poet/src/IPhreeqcPOET.cpp +++ /dev/null @@ -1,186 +0,0 @@ -#include "IPhreeqcPOET.hpp" - -#include -#include -#include -#include -#include - -static inline std::vector -unionStringVectors(const std::vector &a, - const std::vector &b) { - std::vector result; - - std::set_union(a.begin(), a.end(), b.begin(), b.end(), - std::back_inserter(result)); - - return result; -} - -static std::vector -createConcVector(const std::vector &conc_names, - const std::vector::const_iterator &conc_it, - const std::vector &new_names) { - std::vector conc_vec; - - for (const auto &i : new_names) { - auto it = std::find(conc_names.begin(), conc_names.end(), i); - - auto conc_index = std::distance(conc_names.begin(), it); - - if (it != conc_names.end()) { - conc_vec.push_back(*(conc_it + conc_index)); - } else { - conc_vec.push_back(std::numeric_limits::quiet_NaN()); - } - } - - return conc_vec; -} - -// void IPhreeqcPOET::valuesFromModule(const std::string &module_name, -// int cell_number, essential_names &names, -// std::vector &values) { -// std::size_t dest_module_i = 0; -// std::vector to_insert; -// if (module_name == "exchange") { // 1 -// this->Get_exchange(cell_number)->dump_essential_names(names[POET_EXCH]); -// this->Get_exchange(cell_number)->get_essential_values(to_insert); -// dest_module_i = 1; -// } else if (module_name == "kinetics") { // 2 -// this->Get_kinetic(cell_number)->dump_essential_names(names[POET_KIN]); -// this->Get_kinetic(cell_number)->get_essential_values(to_insert); -// dest_module_i = 2; -// } else if (module_name == "surface") { // 4 -// this->Get_surface(cell_number)->dump_essential_names(names[POET_SURF]); -// this->Get_surface(cell_number)->get_essential_values(to_insert); -// dest_module_i = 4; -// } - -// std::size_t offset = 0; -// for (std::size_t i = 0; i < dest_module_i; i++) { -// offset += names[i].size(); -// } - -// values.insert(values.begin() + offset, to_insert.begin(), to_insert.end()); -// } - -// void IPhreeqcPOET::resolveSolutionUseKW( -// const std::vector &unresolved, -// std::map>> -// &mapped_values) { - -// for (const auto &input : unresolved) { -// if (mapped_values.find(input.module_n) != mapped_values.end()) { -// continue; -// } - -// essential_names new_conc_names; -// new_conc_names[0] = mapped_values[input.sol_n].first[0]; - -// const auto &curr_sol_vec = mapped_values[input.sol_n].second; -// std::vector new_conc_values( -// curr_sol_vec.begin(), curr_sol_vec.begin() + -// new_conc_names[0].size()); - -// valuesFromModule(input.module_name, input.module_n, new_conc_names, -// new_conc_values); - -// mapped_values[input.module_n] = -// std::make_pair(new_conc_names, new_conc_values); -// } -// } - -void IPhreeqcPOET::parseInitValues() { - - std::map>> init_values; - - for (const auto &[id, val] : this->PhreeqcPtr->Get_Rxn_solution_map()) { - // A key less than zero indicates an internal solution - if (id < 0) { - continue; - } - - essential_names curr_conc_names = this->dump_essential_names(id); - - init_values[id] = std::make_pair( - curr_conc_names, this->get_essential_values(id, curr_conc_names[0])); - } - - // const auto unresolved_modules = this->getSolutionMapping(); - // resolveSolutionUseKW(unresolved_modules, init_values); - - std::vector ids_to_erase; - - // loop over found initial values and erase those that are not in the ids - for (const auto &[id, values] : init_values) { - // find key in vector of ids - // auto it = std::find(ids.begin(), ids.end(), id); - - // if (it == ids.end()) { - // ids_to_erase.push_back(id); - // continue; - // } - - // create a union of all known concentration names - for (std::size_t i = 0; i < 5; i++) { - this->initial_names[i] = - unionStringVectors(this->initial_names[i], values.first[i]); - - if (i == 1) { - for (auto &specie : this->initial_names[i]) { - specie = subExchangeName(specie); - } - } - } - } - - for (const auto &key : ids_to_erase) { - init_values.erase(key); - } - - // create a vector of the initial concentrations with NaNs for missing - // concentrations - for (const auto &[key, val] : init_values) { - std::vector combined_conc_vec; - - for (std::size_t i = 0, offset = 0; i < 5; i++) { - std::vector union_vec = createConcVector( - val.first[i], val.second.begin() + offset, this->initial_names[i]); - - combined_conc_vec.insert(combined_conc_vec.end(), union_vec.begin(), - union_vec.end()); - - offset += val.first[i].size(); - } - - this->initial_concentrations.push_back(combined_conc_vec); - this->solution_ids.push_back(key); - } - - // const auto unresolved_modules = this->getSolutionMapping(); - - // std::map>> - // mapped_values; - - // for (const auto &val : unresolved_modules) { - // const auto &module_n = val.module_n; - - // if (init_values.find(module_n) != init_values.end()) { - // break; - // } - - // if (std::find(this->solution_ids.begin(), this->solution_ids.end(), - // module_n) == this->solution_ids.end()) { - // const int solution_index = - // std::find(solution_ids.begin(), solution_ids.end(), val.sol_n) - - // solution_ids.begin(); - - // essential_names solution_only; - // solution_only[0] = init_values[val.sol_n].first[0]; - - // mapped_values[module_n] = std::make_pair( - // solution_only, init_values[val.sol_n].second); - // } - // } -} \ No newline at end of file diff --git a/poet/src/Init.cpp b/poet/src/Init.cpp index 6ff4e80f..2aaa9341 100644 --- a/poet/src/Init.cpp +++ b/poet/src/Init.cpp @@ -90,21 +90,69 @@ IPhreeqcPOET::conc_from_essentials(const RawMap &raws, values.push_back(combined_conc_vec); } + + return values; } IPhreeqcPOET::PhreeqcMat IPhreeqcPOET::getPhreeqcMat() { std::vector> values; - const IPhreeqcPOET::essential_names names = + const IPhreeqcPOET::essential_names ess_names = this->union_raws(this->raw_initials); const std::vector> conc_values = - this->conc_from_essentials(this->raw_initials, names); + this->conc_from_essentials(this->raw_initials, ess_names); + std::vector conc_names; + + for (const auto &i : ess_names) { + conc_names.insert(conc_names.end(), i.begin(), i.end()); + } std::vector ids(this->raw_initials.size()); std::transform(this->raw_initials.begin(), this->raw_initials.end(), ids.begin(), [](const auto &pair) { return pair.first; }); + return {.names = conc_names, .ids = ids, .values = conc_values}; } + +IPhreeqcPOET::ModulesArray +IPhreeqcPOET::getModuleSizes(const std::vector &cell_ids) { + ModulesArray module_sizes; + RawMap raws; + + for (const auto &id : cell_ids) { + raws[id] = this->raw_initials[id]; + } + + const IPhreeqcPOET::essential_names ess_names = this->union_raws(raws); + + std::transform(ess_names.begin(), ess_names.end(), module_sizes.begin(), + [](const auto &mod) { return mod.size(); }); + + return module_sizes; +} + +// IPhreeqcPOET::PhreeqcMat +// IPhreeqcPOET::getPhreeqcMat(const std::vector &use_ids) { +// std::vector> values; +// RawMap raws; + +// for (const auto &id : use_ids) { +// raws[id] = this->raw_initials[id]; +// } + +// const IPhreeqcPOET::essential_names ess_names = this->union_raws(raws); + +// const std::vector> conc_values = +// this->conc_from_essentials(raws, ess_names); + +// std::vector conc_names; + +// for (const auto &i : ess_names) { +// conc_names.insert(conc_names.end(), i.begin(), i.end()); +// } + +// return {.names = conc_names, .ids = use_ids, .values = conc_values}; +// } \ No newline at end of file