Block-Structured AMR Software Framework
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 
17 template <class Derived, class WeightType>
18 struct Base
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), val);
78  }
79  }
80  }
81  }
82  }
83 
138  template <typename P, typename V, typename F, typename G>
140  void MeshToParticle (P& p,
141  amrex::Array4<const V> const& arr,
142  int src_comp, int dst_comp, int num_comps, F const& f, G const& g)
143  {
144  static constexpr int stencil_width = Derived::stencil_width;
145  for (int ic=0; ic < num_comps; ++ic) {
146  for (int kk = 0; kk <= Derived::nz; ++kk) {
147  for (int jj = 0; jj <= Derived::ny; ++jj) {
148  for (int ii = 0; ii <= Derived::nx; ++ii) {
149  const auto mval = f(arr,index[0]+ii,index[1]+jj,index[2]+kk,src_comp+ic);
150  const auto val = w[0*stencil_width+ii] *
151  w[1*stencil_width+jj] *
152  w[2*stencil_width+kk] * mval;
153  g(p, ic + dst_comp, val);
154  }
155  }
156  }
157  }
158  }
159 };
160 
180 struct Nearest : public Base<Nearest, int>
181 {
182  static constexpr int stencil_width = 1;
184 
185  static constexpr int nx = (AMREX_SPACEDIM >= 1) ? stencil_width - 1 : 0;
186  static constexpr int ny = (AMREX_SPACEDIM >= 2) ? stencil_width - 1 : 0;
187  static constexpr int nz = (AMREX_SPACEDIM >= 3) ? stencil_width - 1 : 0;
188 
189  template <typename P>
191  Nearest (const P& p,
194  {
195  w = &weights[0];
196  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
197  amrex::Real l = (p.pos(i) - plo[i]) * dxi[i] + 0.5;
198  index[i] = static_cast<int>(amrex::Math::floor(l));
199  w[i] = 1;
200  }
201  for (int i = AMREX_SPACEDIM; i < 3; ++i) {
202  index[i] = 0;
203  w[i] = 1;
204  }
205  }
206 };
207 
221 struct Linear : public Base<Linear, amrex::Real>
222 {
223  static constexpr int stencil_width = 2;
224 
225  static constexpr int nx = (AMREX_SPACEDIM >= 1) ? stencil_width - 1 : 0;
226  static constexpr int ny = (AMREX_SPACEDIM >= 2) ? stencil_width - 1 : 0;
227  static constexpr int nz = (AMREX_SPACEDIM >= 3) ? stencil_width - 1 : 0;
228 
229  amrex::Real weights[3*stencil_width];
230 
231  template <typename P>
233  Linear (const P& p,
236  {
237  w = &weights[0];
238  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
239  amrex::Real l = (p.pos(i) - plo[i]) * dxi[i] + 0.5;
240  index[i] = static_cast<int>(amrex::Math::floor(l)) - 1;
241  amrex::Real lint = l - (index[i] + 1);
242  w[stencil_width*i + 0] = 1.-lint;
243  w[stencil_width*i + 1] = lint;
244  }
245  for (int i = AMREX_SPACEDIM; i < 3; ++i) {
246  index[i] = 0;
247  w[stencil_width*i + 0] = 1.;
248  w[stencil_width*i + 1] = 0.;
249  }
250  }
251 };
252 }
253 
254 #endif // include guard
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_GPU_DEVICE
Definition: AMReX_GpuQualifiers.H:18
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition: AMReX_GpuAtomic.H:281
Definition: AMReX_ParticleInterpolators.H:10
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
static constexpr int P
Definition: AMReX_OpenBC.H:14
Definition: AMReX_Array4.H:61
Definition: AMReX_Array.H:34
A base class for doing general particle/mesh interpolation operations.
Definition: AMReX_ParticleInterpolators.H:19
WeightType * w
Definition: AMReX_ParticleInterpolators.H:21
AMREX_GPU_DEVICE AMREX_FORCE_INLINE 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:140
int index[3]
Definition: AMReX_ParticleInterpolators.H:20
AMREX_GPU_DEVICE AMREX_FORCE_INLINE 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
A class the implements linear (CIC) particle/mesh interpolation.
Definition: AMReX_ParticleInterpolators.H:222
static constexpr int nx
Definition: AMReX_ParticleInterpolators.H:225
static constexpr int ny
Definition: AMReX_ParticleInterpolators.H:226
AMREX_GPU_DEVICE AMREX_FORCE_INLINE Linear(const P &p, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &plo, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &dxi)
Definition: AMReX_ParticleInterpolators.H:233
static constexpr int stencil_width
Definition: AMReX_ParticleInterpolators.H:223
static constexpr int nz
Definition: AMReX_ParticleInterpolators.H:227
amrex::Real weights[3 *stencil_width]
Definition: AMReX_ParticleInterpolators.H:229
A class the implements nearest grid point particle/mesh interpolation.
Definition: AMReX_ParticleInterpolators.H:181
int weights[3 *stencil_width]
Definition: AMReX_ParticleInterpolators.H:183
static constexpr int nz
Definition: AMReX_ParticleInterpolators.H:187
static constexpr int ny
Definition: AMReX_ParticleInterpolators.H:186
static constexpr int nx
Definition: AMReX_ParticleInterpolators.H:185
AMREX_GPU_DEVICE AMREX_FORCE_INLINE Nearest(const P &p, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &plo, amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > const &dxi)
Definition: AMReX_ParticleInterpolators.H:191
static constexpr int stencil_width
Definition: AMReX_ParticleInterpolators.H:182