Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_ParticleInterpolators.H
Go to the documentation of this file.
1#ifndef AMREX_PARTICLEINTERPOLATORS_H_
2#define AMREX_PARTICLEINTERPOLATORS_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_IntVect.H>
6#include <AMReX_Gpu.H>
7#include <AMReX_Print.H>
8
10{
11
17template <class Derived, class WeightType>
18struct Base // NOLINT(bugprone-crtp-constructor-accessibility)
19{
20 int index[3];
21 WeightType* w;
22
62 template <typename P, typename V, typename F>
64 void ParticleToMesh (const P& p,
65 amrex::Array4<V> const& arr,
66 int src_comp, int dst_comp, int num_comps, F const& f)
67 {
68 static constexpr int stencil_width = Derived::stencil_width;
69 for (int ic=0; ic < num_comps; ++ic) {
70 for (int kk = 0; kk <= Derived::nz; ++kk) {
71 for (int jj = 0; jj <= Derived::ny; ++jj) {
72 for (int ii = 0; ii <= Derived::nx; ++ii) {
73 const auto pval = f(p, src_comp+ic);
74 const auto val = w[0*stencil_width+ii] *
75 w[1*stencil_width+jj] *
76 w[2*stencil_width+kk] * pval;
77 Gpu::Atomic::AddNoRet(&arr(index[0]+ii, index[1]+jj, index[2]+kk, ic+dst_comp),
78 static_cast<V>(val));
79 }
80 }
81 }
82 }
83 }
84
139 template <typename P, typename V, typename F, typename G>
141 void MeshToParticle (P& p,
142 amrex::Array4<const V> const& arr,
143 int src_comp, int dst_comp, int num_comps, F const& f, G const& g)
144 {
145 static constexpr int stencil_width = Derived::stencil_width;
146 for (int ic=0; ic < num_comps; ++ic) {
147 for (int kk = 0; kk <= Derived::nz; ++kk) {
148 for (int jj = 0; jj <= Derived::ny; ++jj) {
149 for (int ii = 0; ii <= Derived::nx; ++ii) {
150 const auto mval = f(arr,index[0]+ii,index[1]+jj,index[2]+kk,src_comp+ic);
151 const auto val = w[0*stencil_width+ii] *
152 w[1*stencil_width+jj] *
153 w[2*stencil_width+kk] * mval;
154 g(p, ic + dst_comp, val);
155 }
156 }
157 }
158 }
159 }
160};
161
181struct Nearest : public Base<Nearest, int>
182{
183 static constexpr int stencil_width = 1;
185
186 static constexpr int nx = (AMREX_SPACEDIM >= 1) ? stencil_width - 1 : 0;
187 static constexpr int ny = (AMREX_SPACEDIM >= 2) ? stencil_width - 1 : 0;
188 static constexpr int nz = (AMREX_SPACEDIM >= 3) ? stencil_width - 1 : 0;
189
190 template <typename P>
192 Nearest (const P& p,
195 {
196 w = &weights[0];
197 // Compute in the wider of Real and ParticleReal; round when storing.
198 using WT = amrex::ParticleMeshReal;
199 for (int i = 0; i < AMREX_SPACEDIM; ++i) {
200 WT l = (p.pos(i) - plo[i]) * dxi[i] + WT(0.5);
201 index[i] = static_cast<int>(amrex::Math::floor(l));
202 w[i] = 1;
203 }
204 for (int i = AMREX_SPACEDIM; i < 3; ++i) {
205 index[i] = 0;
206 w[i] = 1;
207 }
208 }
209};
210
224struct Linear : public Base<Linear, amrex::ParticleMeshReal>
225{
226 static constexpr int stencil_width = 2;
227
228 static constexpr int nx = (AMREX_SPACEDIM >= 1) ? stencil_width - 1 : 0;
229 static constexpr int ny = (AMREX_SPACEDIM >= 2) ? stencil_width - 1 : 0;
230 static constexpr int nz = (AMREX_SPACEDIM >= 3) ? stencil_width - 1 : 0;
231
233
234 template <typename P>
236 Linear (const P& p,
239 {
240 w = &weights[0];
241 // Compute in the wider of Real and ParticleReal; round when storing.
242 using WT = amrex::ParticleMeshReal;
243 for (int i = 0; i < AMREX_SPACEDIM; ++i) {
244 WT l = (p.pos(i) - plo[i]) * dxi[i] + WT(0.5);
245 index[i] = static_cast<int>(amrex::Math::floor(l)) - 1;
246 WT lint = l - static_cast<WT>(index[i] + 1);
247 w[stencil_width*i + 0] = WT(1.) - lint;
248 w[stencil_width*i + 1] = lint;
249 }
250 for (int i = AMREX_SPACEDIM; i < 3; ++i) {
251 index[i] = 0;
252 w[stencil_width*i + 0] = WT(1.);
253 w[stencil_width*i + 1] = WT(0.);
254 }
255 }
256};
257}
258
259#endif // include guard
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Convenience header for the core AMReX GPU facilities.
std::common_type_t< Real, ParticleReal > ParticleMeshReal
Floating Point Type for Particle-Mesh Operations.
Definition AMReX_REAL.H:102
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:283
Definition AMReX_ParticleInterpolators.H:10
A multidimensional array accessor.
Definition AMReX_Array4.H:289
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:52
A base class for doing general particle/mesh interpolation operations.
Definition AMReX_ParticleInterpolators.H:19
__device__ void ParticleToMesh(const P &p, amrex::Array4< V > const &arr, int src_comp, int dst_comp, int num_comps, F const &f)
A general function for doing particle-to-mesh interpolation for one particle.
Definition AMReX_ParticleInterpolators.H:64
WeightType * w
Definition AMReX_ParticleInterpolators.H:21
int index[3]
Definition AMReX_ParticleInterpolators.H:20
__device__ void MeshToParticle(P &p, amrex::Array4< const V > const &arr, int src_comp, int dst_comp, int num_comps, F const &f, G const &g)
A general function for doing mesh-to-particle interpolation for one particle.
Definition AMReX_ParticleInterpolators.H:141
A class the implements linear (CIC) particle/mesh interpolation.
Definition AMReX_ParticleInterpolators.H:225
static constexpr int nx
Definition AMReX_ParticleInterpolators.H:228
static constexpr int ny
Definition AMReX_ParticleInterpolators.H:229
static constexpr int stencil_width
Definition AMReX_ParticleInterpolators.H:226
__device__ Linear(const P &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi)
Definition AMReX_ParticleInterpolators.H:236
amrex::ParticleMeshReal weights[3 *stencil_width]
Definition AMReX_ParticleInterpolators.H:232
static constexpr int nz
Definition AMReX_ParticleInterpolators.H:230
A class the implements nearest grid point particle/mesh interpolation.
Definition AMReX_ParticleInterpolators.H:182
__device__ Nearest(const P &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi)
Definition AMReX_ParticleInterpolators.H:192
int weights[3 *stencil_width]
Definition AMReX_ParticleInterpolators.H:184
static constexpr int nz
Definition AMReX_ParticleInterpolators.H:188
static constexpr int ny
Definition AMReX_ParticleInterpolators.H:187
static constexpr int nx
Definition AMReX_ParticleInterpolators.H:186
static constexpr int stencil_width
Definition AMReX_ParticleInterpolators.H:183