From 162cbb1f47939043be08d244f72f5cfadf2241d8 Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <github@naumov.de> Date: Thu, 31 Dec 2020 15:02:26 +0100 Subject: [PATCH] [CL] Extract common average amount computation. --- ChemistryLib/PhreeqcIO.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/ChemistryLib/PhreeqcIO.cpp b/ChemistryLib/PhreeqcIO.cpp index 8149ec0a32e..3094cc50ec1 100644 --- a/ChemistryLib/PhreeqcIO.cpp +++ b/ChemistryLib/PhreeqcIO.cpp @@ -531,33 +531,33 @@ std::vector<std::string> const PhreeqcIO::getComponentList() const return component_names; } +template <typename Reactant> +static double averageReactantAmount( + Reactant const& reactant, + std::vector<GlobalIndexType> const& chemical_system_indices) +{ + double const sum = std::accumulate( + chemical_system_indices.begin(), chemical_system_indices.end(), 0.0, + [&](double const s, GlobalIndexType const id) { + return s + (*reactant.amount)[id]; + }); + return sum / chemical_system_indices.size(); +} + void PhreeqcIO::computeSecondaryVariable( std::size_t const ele_id, std::vector<GlobalIndexType> const& chemical_system_indices) { - auto const num_chemical_systems = chemical_system_indices.size(); for (auto& kinetic_reactant : _chemical_system->kinetic_reactants) { - double amount_avg = std::accumulate( - chemical_system_indices.begin(), chemical_system_indices.end(), 0.0, - [&](double amount, auto const& id) { - return amount + (*kinetic_reactant.amount)[id]; - }); - - amount_avg /= num_chemical_systems; - (*kinetic_reactant.amount_avg)[ele_id] = amount_avg; + (*kinetic_reactant.amount_avg)[ele_id] = + averageReactantAmount(kinetic_reactant, chemical_system_indices); } for (auto& equilibrium_reactant : _chemical_system->equilibrium_reactants) { - double amount_avg = std::accumulate( - chemical_system_indices.begin(), chemical_system_indices.end(), 0.0, - [&](double amount, auto const& id) { - return amount + (*equilibrium_reactant.amount)[id]; - }); - - amount_avg /= num_chemical_systems; - (*equilibrium_reactant.amount_avg)[ele_id] = amount_avg; + (*equilibrium_reactant.amount_avg)[ele_id] = averageReactantAmount( + equilibrium_reactant, chemical_system_indices); } } } // namespace PhreeqcIOData -- GitLab