From bf23dca409eaee793bf47d7facec5f305a11679b Mon Sep 17 00:00:00 2001 From: deegan Date: Wed, 2 Oct 2024 15:56:46 +0200 Subject: [PATCH] keep particle overallocation on restore state --- src/amr/solvers/solver_ppc.hpp | 28 +++++++++++++------ src/core/numerics/ion_updater/ion_updater.hpp | 12 +++++--- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/amr/solvers/solver_ppc.hpp b/src/amr/solvers/solver_ppc.hpp index eb1f79fe8..3eb79f496 100644 --- a/src/amr/solvers/solver_ppc.hpp +++ b/src/amr/solvers/solver_ppc.hpp @@ -152,6 +152,22 @@ class SolverPPC : public ISolver std::unordered_map tmpDomain; std::unordered_map patchGhost; + template + static void add_to(Map& map, std::string const& key, ParticleArray const& ps) + { + // vector copy drops the capacity (over allocation of the source) + // we want to keep the overallocation somewhat - how much to be assessed + ParticleArray empty{ps.box()}; + + if (!map.count(key)) + map.emplace(key, empty); + else + map.at(key) = empty; + + auto& v = map.at(key); + v.reserve(ps.capacity()); + v.replace_from(ps); + } }; // end solverPPC @@ -206,15 +222,9 @@ void SolverPPC::saveState_(level_t& level, ModelViews_t& ss << state.patch->getGlobalId(); for (auto& pop : state.ions) { - auto key = ss.str() + "_" + pop.name(); - if (!tmpDomain.count(key)) - tmpDomain.emplace(key, pop.domainParticles()); - else - tmpDomain.at(key) = pop.domainParticles(); - if (!patchGhost.count(key)) - patchGhost.emplace(key, pop.patchGhostParticles()); - else - patchGhost.at(key) = pop.patchGhostParticles(); + std::string const key = ss.str() + "_" + pop.name(); + add_to(tmpDomain, key, pop.domainParticles()); + add_to(patchGhost, key, pop.patchGhostParticles()); } } } diff --git a/src/core/numerics/ion_updater/ion_updater.hpp b/src/core/numerics/ion_updater/ion_updater.hpp index dbae99b75..1dad099fa 100644 --- a/src/core/numerics/ion_updater/ion_updater.hpp +++ b/src/core/numerics/ion_updater/ion_updater.hpp @@ -221,7 +221,7 @@ void IonUpdater::updateAndDepositAll_(Ions& ions, auto ghostBox{domainBox}; ghostBox.grow(partGhostWidth); - auto inDomainBox = [&domainBox](auto& particleRange) // + auto inDomainBox = [&domainBox](auto&& particleRange) // { return particleRange.array().partition( [&](auto const& cell) { return isIn(Point{cell}, domainBox); }); @@ -259,9 +259,13 @@ void IonUpdater::updateAndDepositAll_(Ions& ions, interpolator_, layout, inGhostBox, inGhostLayer); auto& particleArray = particleRange.array(); - particleArray.export_particles( - domainParticles, [&](auto const& cell) { return isIn(Point{cell}, domainBox); }); - + { + auto enteredInDomain + = inDomainBox(makeRange(particleArray, 0, inGhostLayerRange.iend())); + domainParticles.reserve(domainParticles.size() + enteredInDomain.size()); + std::copy(enteredInDomain.begin(), enteredInDomain.end(), + std::back_inserter(domainParticles)); + } particleArray.erase( makeRange(particleArray, inGhostLayerRange.iend(), particleArray.size())); };