Skip to content

Commit

Permalink
reserve refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Oct 15, 2024
1 parent fffd323 commit 04b1b69
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ jobs:
make -j2 && sudo make install && cd ../.. && rm -rf samrai
cd ${{runner.workspace}}/build && rm -rf *
cmake $GITHUB_WORKSPACE -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON --fresh \
-DCMAKE_BUILD_TYPE=RelWithDebInfo -Dasan=OFF \
-DCMAKE_BUILD_TYPE=Debug -Dasan=OFF \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DlowResourceTests=ON -DdevMode=ON -Dbench=ON \
-DCMAKE_CXX_FLAGS="-DPHARE_DIAG_DOUBLES=1 " -Dphare_configurator=ON
-DCMAKE_CXX_FLAGS="-O3 -DPHARE_DIAG_DOUBLES=1 " -Dphare_configurator=ON
- name: Build
working-directory: ${{runner.workspace}}/build
Expand Down
10 changes: 6 additions & 4 deletions src/amr/data/field/refine/electric_field_refiner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#include "core/def/phare_mpi.hpp"

#include <SAMRAI/hier/Box.h>

#include "amr/amr_constants.hpp"
#include "amr/resources_manager/amr_utils.hpp"

#include "core/utilities/constants.hpp"
#include "core/data/grid/gridlayoutdefs.hpp"
#include "core/utilities/point/point.hpp"

#include <SAMRAI/hier/Box.h>

#include <cstddef>

namespace PHARE::amr
Expand Down Expand Up @@ -43,8 +45,8 @@ class ElectricFieldRefiner
{
TBOX_ASSERT(coarseField.physicalQuantity() == fineField.physicalQuantity());

auto const locFineIdx = AMRToLocal(fineIndex, fineBox_);
auto constexpr refinementRatio = 2;
auto const locFineIdx = AMRToLocal(fineIndex, fineBox_);

auto coarseIdx{fineIndex};
for (auto& idx : coarseIdx)
idx = idx / refinementRatio;
Expand Down
68 changes: 42 additions & 26 deletions src/amr/data/particles/refine/particles_data_split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
#define PHARE_PARTICLES_DATA_SPLIT_HPP


#include "core/def.hpp"
#include "phare_core.hpp"
#include "core/def/phare_mpi.hpp"
#include "core/utilities/constants.hpp"

#include "core/def.hpp"
#include "amr/data/particles/particles_data.hpp"
#include "amr/resources_manager/amr_utils.hpp"
#include "split.hpp"
#include "core/utilities/constants.hpp"
#include "phare_core.hpp"
#include "amr/amr_constants.hpp"
#include "amr/utilities/box/amr_box.hpp"
#include "amr/resources_manager/amr_utils.hpp"
#include "amr/data/particles/particles_data.hpp"

#include <SAMRAI/geom/CartesianPatchGeometry.h>
#include <SAMRAI/hier/Box.h>
#include <SAMRAI/hier/RefineOperator.h>
#include <SAMRAI/pdat/CellOverlap.h>

#include <functional>


namespace PHARE
Expand Down Expand Up @@ -53,6 +53,12 @@ namespace amr
template<typename ParticleArray, ParticlesDataSplitType splitType, typename Splitter>
class ParticlesRefineOperator : public SAMRAI::hier::RefineOperator
{
using Particle_t = typename ParticleArray::value_type;
static constexpr bool putParticlesInCoarseBoundary
= splitType == ParticlesDataSplitType::coarseBoundary
|| splitType == ParticlesDataSplitType::coarseBoundaryOld
|| splitType == ParticlesDataSplitType::coarseBoundaryNew;

public:
static constexpr auto dim = Splitter::dimension;
static constexpr auto interpOrder = Splitter::interp_order;
Expand Down Expand Up @@ -99,7 +105,7 @@ namespace amr
= std::dynamic_pointer_cast<ParticlesData<ParticleArray>>(
source.getPatchData(sourceComponent));

// Finnaly we need the cartesion geometry of both patch.
// Finaly we need the cartesion geometry of both patch.
auto patchGeomDestination
= std::dynamic_pointer_cast<SAMRAI::geom::CartesianPatchGeometry>(
destination.getPatchGeometry());
Expand Down Expand Up @@ -133,9 +139,15 @@ namespace amr
}
}

template<typename Fn>
static void _reserve(ParticleArray& particles, Fn&& counter)
{ // only reserve for regular refinement of domain to start with
if constexpr (!putParticlesInCoarseBoundary)
particles.reserve(counter() * nbRefinedPart);
}

/** @brief given two ParticlesData (destination and source),
* an overlap , a ratio and the geometry of both patches, perform the
* an overlap, a ratio and the geometry of both patches, perform the
* splitting of coarse particles onto the destination patch
*/
void refine_(ParticlesData<ParticleArray>& destParticlesData,
Expand Down Expand Up @@ -166,43 +178,47 @@ namespace amr
// same for destinationGhostBox and destinationDomainBox the later will allow to get an
// index relative to the interior

std::array const particlesArrays{&srcInteriorParticles, &srcGhostParticles};

auto const count_expected = [&]() {
std::size_t incoming_estimate = 0;
for (auto const& destinationBox : destBoxes)
{
auto const coarseSplitBox
= coarsen(phare_box_from<dim>(getSplitBox(destinationBox)));
for (auto const& sourceParticlesArray : particlesArrays)
incoming_estimate += sourceParticlesArray->nbr_particles_in(coarseSplitBox);
}
return incoming_estimate;
};
_reserve(destDomainParticles, count_expected);

Splitter split;

// The PatchLevelFillPattern had compute boxes that correspond to the expected filling.
// In case of a coarseBoundary it will most likely give multiple boxes
// in case of interior, this will be just one box usually
for (auto const& destinationBox : destBoxes)
{
std::array particlesArrays{&srcInteriorParticles, &srcGhostParticles};
auto splitBox = getSplitBox(destinationBox);
auto const splitBox = getSplitBox(destinationBox);

auto isInDest = [&destinationBox](auto const& particle) //
auto const isInDest = [&destinationBox](auto const& particle) //
{ return isInBox(destinationBox, particle); };


for (auto const& sourceParticlesArray : particlesArrays)
{
for (auto const& particle : *sourceParticlesArray)
{
std::array<typename ParticleArray::value_type, nbRefinedPart>
refinedParticles;
auto particleRefinedPos = toFineGrid<interpOrder>(particle);
auto const particleRefinedPos = toFineGrid<interpOrder>(particle);

if (isInBox(splitBox, particleRefinedPos))
{
std::array<Particle_t, nbRefinedPart> refinedParticles;
split(particleRefinedPos, refinedParticles);


// we need to know in which of interior or levelGhostParticlesXXXX
// arrays we must put particles

bool constexpr putParticlesInCoarseBoundary
= splitType == ParticlesDataSplitType::coarseBoundary
|| splitType == ParticlesDataSplitType::coarseBoundaryOld
|| splitType == ParticlesDataSplitType::coarseBoundaryNew;



if constexpr (putParticlesInCoarseBoundary)
{
if constexpr (splitType == ParticlesDataSplitType::coarseBoundary)
Expand Down Expand Up @@ -243,9 +259,9 @@ namespace amr
std::back_inserter(destDomainParticles), isInDest);
}
} // end is candidate for split
} // end loop on particles
} // end loop on source particle arrays
} // loop on destination box
} // end loop on particles
} // end loop on source particle arrays
} // loop on destination box
}


Expand Down
22 changes: 19 additions & 3 deletions src/amr/utilities/box/amr_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#define PHARE_AMR_UTILITIES_BOX_BOX_HPP


#include "core/def.hpp"
#include "core/def/phare_mpi.hpp"
#include "core/utilities/box/box.hpp"

#include "amr/amr_constants.hpp"

#include "SAMRAI/hier/Box.h"
#include "core/utilities/box/box.hpp"
#include "core/def.hpp"


namespace PHARE::amr
{
Expand Down Expand Up @@ -85,6 +85,22 @@ struct Box : public PHARE::core::Box<Type, dim>
}
};

template<std::size_t dim>
auto refine(core::Box<int, dim> box)
{
for (std::uint8_t di = 0; di < dim; ++di)
box.lower[di] *= refinementRatio, box.upper[di] *= refinementRatio;
return box;
}
template<std::size_t dim>
auto coarsen(core::Box<int, dim> box)
{
for (std::uint8_t di = 0; di < dim; ++di)
box.lower[di] /= refinementRatio, box.upper[di] /= refinementRatio;

return box;
}

} // namespace PHARE::amr

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray,
std::uint32_t const& nbrParticlesPerCell, std::optional<std::size_t> seed = {},
Basis const basis = Basis::Cartesian,
std::array<InputFunction, 3> const& magneticField = {nullptr, nullptr, nullptr},
double densityCutOff = 1e-5)
double const densityCutOff = 1e-5, double const over_alloc_factor = .1)
: density_{density}
, bulkVelocity_{bulkVelocity}
, thermalVelocity_{thermalVelocity}
Expand All @@ -54,6 +54,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray,
, nbrParticlePerCell_{nbrParticlesPerCell}
, basis_{basis}
, rngSeed_{seed}
, over_alloc_factor_{over_alloc_factor}
{
}

Expand Down Expand Up @@ -92,6 +93,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray,
std::uint32_t nbrParticlePerCell_;
Basis basis_;
std::optional<std::size_t> rngSeed_;
double over_alloc_factor_ = .1;
};


Expand Down Expand Up @@ -178,20 +180,16 @@ void MaxwellianParticleInitializer<ParticleArray, GridLayout>::loadParticles(
auto randGen = getRNG(rngSeed_);
ParticleDeltaDistribution<double> deltaDistrib;

std::size_t const expected_size
= [&](auto const& _n /* or error: reference to local binding 'n' declared*/) {
std::size_t size = 0;
for (std::size_t flatCellIdx = 0; flatCellIdx < ndCellIndices.size(); ++flatCellIdx)
if (_n[flatCellIdx] >= densityCutOff_)
++size;
return size * nbrParticlePerCell_;
}(n);

double const over_alloc_factor = .1; // todo from dict
std::size_t const allocate_size
= expected_size
+ (layout.AMRBox().surface_cell_count() * (nbrParticlePerCell_ * over_alloc_factor));
particles.reserve(allocate_size);
auto const expected_size = std::accumulate(n, n + ndCellIndices.size(), std::size_t{0},
[&](auto const& sum, auto const& density_value) {
return (density_value > densityCutOff_)
? sum + nbrParticlePerCell_
: sum;
});

auto const incoming_estimate
= (layout.AMRBox().surface_cell_count() * (nbrParticlePerCell_ * over_alloc_factor_));
particles.reserve(expected_size + incoming_estimate);

for (std::size_t flatCellIdx = 0; flatCellIdx < ndCellIndices.size(); ++flatCellIdx)
{
Expand Down

0 comments on commit 04b1b69

Please sign in to comment.