Skip to content

Commit

Permalink
WarpX class: move AllocateCenteringCoefficients to anonymous namespac…
Browse files Browse the repository at this point in the history
…e in WarpX.cpp (#5666)

`AllocateCenteringCoefficients` is a pure function used only inside
`WarpX.cpp`. This PR moves it from a private member function of the
WarpX class to an anonymous namespace inside `WarpX.cpp`. The PR also
makes the function more compact.
This is done to simplify the `WarpX.H` header.
  • Loading branch information
lucafedeli88 authored Mar 7, 2025
1 parent 14a4e8f commit ceaf29c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 90 deletions.
20 changes: 0 additions & 20 deletions Source/WarpX.H
Original file line number Diff line number Diff line change
Expand Up @@ -1215,26 +1215,6 @@ private:
return gather_buffer_masks[lev].get();
}

/**
* \brief Allocates and initializes the stencil coefficients used for the finite-order centering
* of fields and currents, and stores them in the given device vectors.
*
* \param[in,out] device_centering_stencil_coeffs_x device vector where the stencil coefficients along x will be stored
* \param[in,out] device_centering_stencil_coeffs_y device vector where the stencil coefficients along y will be stored
* \param[in,out] device_centering_stencil_coeffs_z device vector where the stencil coefficients along z will be stored
* \param[in] centering_nox order of the finite-order centering along x
* \param[in] centering_noy order of the finite-order centering along y
* \param[in] centering_noz order of the finite-order centering along z
* \param[in] a_grid_type type of grid (collocated or not)
*/
void AllocateCenteringCoefficients (amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_x,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_y,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_z,
int centering_nox,
int centering_noy,
int centering_noz,
ablastr::utils::enums::GridType a_grid_type);

void AllocLevelMFs (int lev, const amrex::BoxArray& ba, const amrex::DistributionMapping& dm,
const amrex::IntVect& ngEB, amrex::IntVect& ngJ,
const amrex::IntVect& ngRho, const amrex::IntVect& ngF,
Expand Down
132 changes: 62 additions & 70 deletions Source/WarpX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ WarpX* WarpX::m_instance = nullptr;

namespace
{

[[nodiscard]] bool
isAnyBoundaryPML(
const amrex::Array<FieldBoundaryType,AMREX_SPACEDIM>& field_boundary_lo,
Expand All @@ -201,6 +200,66 @@ namespace
return is_any_pml;
}

/**
* \brief Allocates and initializes the stencil coefficients used for the finite-order centering
* of fields and currents, and stores them in the given device vectors.
*
* \param[in,out] device_centering_stencil_coeffs_x device vector where the stencil coefficients along x will be stored
* \param[in,out] device_centering_stencil_coeffs_y device vector where the stencil coefficients along y will be stored
* \param[in,out] device_centering_stencil_coeffs_z device vector where the stencil coefficients along z will be stored
* \param[in] centering_nox order of the finite-order centering along x
* \param[in] centering_noy order of the finite-order centering along y
* \param[in] centering_noz order of the finite-order centering along z
* \param[in] a_grid_type type of grid (collocated or not)
*/
void AllocateCenteringCoefficients (amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_x,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_y,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_z,
int centering_nox,
int centering_noy,
int centering_noz,
ablastr::utils::enums::GridType a_grid_type)
{
// Vectors of Fornberg stencil coefficients
const auto Fornberg_stencil_coeffs_x = ablastr::math::getFornbergStencilCoefficients(centering_nox, a_grid_type);
const auto Fornberg_stencil_coeffs_y = ablastr::math::getFornbergStencilCoefficients(centering_noy, a_grid_type);
const auto Fornberg_stencil_coeffs_z = ablastr::math::getFornbergStencilCoefficients(centering_noz, a_grid_type);

// Host vectors of stencil coefficients used for finite-order centering
auto host_centering_stencil_coeffs_x = amrex::Vector<amrex::Real>(centering_nox);
auto host_centering_stencil_coeffs_y = amrex::Vector<amrex::Real>(centering_noy);
auto host_centering_stencil_coeffs_z = amrex::Vector<amrex::Real>(centering_noz);

// Re-order Fornberg stencil coefficients:
// example for order 6: (c_0,c_1,c_2) becomes (c_2,c_1,c_0,c_0,c_1,c_2)
ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_x,
Fornberg_stencil_coeffs_x,
centering_nox);

ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_y,
Fornberg_stencil_coeffs_y,
centering_noy);

ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_z,
Fornberg_stencil_coeffs_z,
centering_noz);

// Device vectors of stencil coefficients used for finite-order centering
const auto copy_to_device = [](const auto& src, auto& dst){
dst.resize(src.size());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, src.begin(), src.end(), dst.begin());
};

copy_to_device(host_centering_stencil_coeffs_x, device_centering_stencil_coeffs_x);
copy_to_device(host_centering_stencil_coeffs_y, device_centering_stencil_coeffs_y);
copy_to_device(host_centering_stencil_coeffs_z, device_centering_stencil_coeffs_z);

amrex::Gpu::synchronize();
}

/**
* \brief
* Set the dotMask container
Expand Down Expand Up @@ -1069,7 +1128,7 @@ WarpX::ReadParameters ()
utils::parser::queryWithParser(
pp_warpx, "current_centering_noz", current_centering_noz);

AllocateCenteringCoefficients(device_current_centering_stencil_coeffs_x,
::AllocateCenteringCoefficients(device_current_centering_stencil_coeffs_x,
device_current_centering_stencil_coeffs_y,
device_current_centering_stencil_coeffs_z,
current_centering_nox,
Expand Down Expand Up @@ -1412,7 +1471,7 @@ WarpX::ReadParameters ()
utils::parser::queryWithParser(
pp_warpx, "field_centering_noz", field_centering_noz);

AllocateCenteringCoefficients(device_field_centering_stencil_coeffs_x,
::AllocateCenteringCoefficients(device_field_centering_stencil_coeffs_x,
device_field_centering_stencil_coeffs_y,
device_field_centering_stencil_coeffs_z,
field_centering_nox,
Expand Down Expand Up @@ -3175,73 +3234,6 @@ WarpX::BuildBufferMasksInBox ( const amrex::Box tbx, amrex::IArrayBox &buffer_ma
});
}

void WarpX::AllocateCenteringCoefficients (amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_x,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_y,
amrex::Gpu::DeviceVector<amrex::Real>& device_centering_stencil_coeffs_z,
const int centering_nox,
const int centering_noy,
const int centering_noz,
ablastr::utils::enums::GridType a_grid_type)
{
// Vectors of Fornberg stencil coefficients
amrex::Vector<amrex::Real> Fornberg_stencil_coeffs_x;
amrex::Vector<amrex::Real> Fornberg_stencil_coeffs_y;
amrex::Vector<amrex::Real> Fornberg_stencil_coeffs_z;

// Host vectors of stencil coefficients used for finite-order centering
amrex::Vector<amrex::Real> host_centering_stencil_coeffs_x;
amrex::Vector<amrex::Real> host_centering_stencil_coeffs_y;
amrex::Vector<amrex::Real> host_centering_stencil_coeffs_z;

Fornberg_stencil_coeffs_x = ablastr::math::getFornbergStencilCoefficients(centering_nox, a_grid_type);
Fornberg_stencil_coeffs_y = ablastr::math::getFornbergStencilCoefficients(centering_noy, a_grid_type);
Fornberg_stencil_coeffs_z = ablastr::math::getFornbergStencilCoefficients(centering_noz, a_grid_type);

host_centering_stencil_coeffs_x.resize(centering_nox);
host_centering_stencil_coeffs_y.resize(centering_noy);
host_centering_stencil_coeffs_z.resize(centering_noz);

// Re-order Fornberg stencil coefficients:
// example for order 6: (c_0,c_1,c_2) becomes (c_2,c_1,c_0,c_0,c_1,c_2)
ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_x,
Fornberg_stencil_coeffs_x,
centering_nox
);
ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_y,
Fornberg_stencil_coeffs_y,
centering_noy
);
ablastr::math::ReorderFornbergCoefficients(
host_centering_stencil_coeffs_z,
Fornberg_stencil_coeffs_z,
centering_noz
);

// Device vectors of stencil coefficients used for finite-order centering

device_centering_stencil_coeffs_x.resize(host_centering_stencil_coeffs_x.size());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
host_centering_stencil_coeffs_x.begin(),
host_centering_stencil_coeffs_x.end(),
device_centering_stencil_coeffs_x.begin());

device_centering_stencil_coeffs_y.resize(host_centering_stencil_coeffs_y.size());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
host_centering_stencil_coeffs_y.begin(),
host_centering_stencil_coeffs_y.end(),
device_centering_stencil_coeffs_y.begin());

device_centering_stencil_coeffs_z.resize(host_centering_stencil_coeffs_z.size());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
host_centering_stencil_coeffs_z.begin(),
host_centering_stencil_coeffs_z.end(),
device_centering_stencil_coeffs_z.begin());

amrex::Gpu::synchronize();
}

const iMultiFab*
WarpX::CurrentBufferMasks (int lev)
{
Expand Down

0 comments on commit ceaf29c

Please sign in to comment.