/home/runner/work/amr-wind/amr-wind/amr-wind/utilities/sampling/SamplingContainer.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/utilities/sampling/SamplingContainer.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
SamplingContainer.H
Go to the documentation of this file.
1#ifndef SAMPLINGCONTAINER_H
2#define SAMPLINGCONTAINER_H
3
4#include <memory>
5
6#include "AMReX_AmrParticles.H"
8
9namespace amr_wind {
10
11class Field;
12
13namespace sampling {
14
15class SamplerBase;
16
17static constexpr int SNStructReal = 0;
18static constexpr int SNStructInt = 3;
19static constexpr int SNArrayReal = 0;
20static constexpr int SNArrayInt = 0;
21
25struct IIx
26{
27 enum Indices {
28 uid = 0,
30 nid
31 };
32};
33
60 : public amrex::AmrParticleContainer<
61 SNStructReal,
62 SNStructInt,
63 SNArrayReal,
64 SNArrayInt>
65{
66public:
67 explicit SamplingContainer(amrex::AmrCore& mesh)
68 : amrex::AmrParticleContainer<
72 SNArrayInt>(&mesh)
73 , m_mesh(mesh)
74 {}
75
78 void setup_container(
79 const int num_real_components, const int num_int_components = 0);
80
84 const amrex::Vector<std::unique_ptr<SamplerBase>>& /*samplers*/);
85
87 template <typename FType>
88 void interpolate_fields(const amrex::Vector<FType>& fields, const int scomp)
89 {
90 BL_PROFILE("amr-wind::SamplingContainer::interpolate_fields");
91
92 const int nlevels = m_mesh.finestLevel() + 1;
93
94 for (int lev = 0; lev < nlevels; ++lev) {
95 for (ParIterType pti(*this, lev); pti.isValid(); ++pti) {
96 int scomp_curr = scomp;
97 for (const auto* fld : fields) {
98 AMREX_ALWAYS_ASSERT(fld->num_grow() > amrex::IntVect{0});
99 const auto farr = (*fld)(lev).const_array(pti);
101 pti, farr, lev, fld->field_location(), fld->num_comp(),
102 scomp_curr);
103
104 scomp_curr += fld->num_comp();
105 }
106 }
107 }
108 }
109
112 const DerivedQtyMgr& derived_mgr,
113 const FieldRepo& repo,
114 const int scomp);
115
117 void populate_buffer(std::vector<double>& buf);
118
120
122
135 template <typename FType>
137 const int np,
138 const int ic,
139 SamplingContainer::ParticleVector& pvec,
140 SamplingContainer::RealVector& pavec,
141 const FType& farr,
142 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& problo,
143 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dxi,
144 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& dx,
145 const amrex::GpuArray<amrex::Real, AMREX_SPACEDIM>& offset)
146 {
147 BL_PROFILE("amr-wind::SamplingContainer::sample_impl");
148
149 auto* pstruct = pvec.data();
150 auto* parr = pavec.data();
151
152 amrex::ParallelFor(np, [=] AMREX_GPU_DEVICE(int ip) noexcept {
153 auto& p = pstruct[ip];
154 // Determine offsets within the containing cell
155 const amrex::Real x =
156 (p.pos(0) - problo[0] - offset[0] * dx[0]) * dxi[0];
157 const amrex::Real y =
158 (p.pos(1) - problo[1] - offset[1] * dx[1]) * dxi[1];
159 const amrex::Real z =
160 (p.pos(2) - problo[2] - offset[2] * dx[2]) * dxi[2];
161
162 // Index of the low corner
163 const int i = static_cast<int>(std::floor(x));
164 const int j = static_cast<int>(std::floor(y));
165 const int k = static_cast<int>(std::floor(z));
166
167 // Interpolation weights in each direction (linear basis)
168 const amrex::Real wx_hi = (x - i);
169 const amrex::Real wy_hi = (y - j);
170 const amrex::Real wz_hi = (z - k);
171
172 const amrex::Real wx_lo = 1.0 - wx_hi;
173 const amrex::Real wy_lo = 1.0 - wy_hi;
174 const amrex::Real wz_lo = 1.0 - wz_hi;
175
176 parr[ip] = wx_lo * wy_lo * wz_lo * farr(i, j, k, ic) +
177 wx_lo * wy_lo * wz_hi * farr(i, j, k + 1, ic) +
178 wx_lo * wy_hi * wz_lo * farr(i, j + 1, k, ic) +
179 wx_lo * wy_hi * wz_hi * farr(i, j + 1, k + 1, ic) +
180 wx_hi * wy_lo * wz_lo * farr(i + 1, j, k, ic) +
181 wx_hi * wy_lo * wz_hi * farr(i + 1, j, k + 1, ic) +
182 wx_hi * wy_hi * wz_lo * farr(i + 1, j + 1, k, ic) +
183 wx_hi * wy_hi * wz_hi * farr(i + 1, j + 1, k + 1, ic);
184 });
185 }
186
187private:
189 template <typename FType>
191 const ParIterType& pti,
192 const FType& farr,
193 const int lev,
194 const FieldLoc floc,
195 const int ncomp,
196 const int scomp)
197 {
198 const auto& geom = m_mesh.Geom(lev);
199 const auto dx = geom.CellSizeArray();
200 const auto dxi = geom.InvCellSizeArray();
201 const auto plo = geom.ProbLoArray();
202 const int np = pti.numParticles();
203 auto& pvec = pti.GetArrayOfStructs()();
204 int fidx = scomp;
205 for (int ic = 0; ic < ncomp; ++ic) {
206 auto& parr = pti.GetStructOfArrays().GetRealData(fidx++);
207
208 switch (floc) {
209 case FieldLoc::NODE: {
210 amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> offset{
211 0.0, 0.0, 0.0};
212 sample_field(np, ic, pvec, parr, farr, plo, dxi, dx, offset);
213 break;
214 }
215
216 case FieldLoc::CELL: {
217 amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> offset{
218 0.5, 0.5, 0.5};
219 sample_field(np, ic, pvec, parr, farr, plo, dxi, dx, offset);
220 break;
221 }
222
223 case FieldLoc::XFACE: {
224 amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> offset{
225 0.0, 0.5, 0.5};
226 sample_field(np, ic, pvec, parr, farr, plo, dxi, dx, offset);
227 break;
228 }
229
230 case FieldLoc::YFACE: {
231 amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> offset{
232 0.5, 0.0, 0.5};
233 sample_field(np, ic, pvec, parr, farr, plo, dxi, dx, offset);
234 break;
235 }
236
237 case FieldLoc::ZFACE: {
238 amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> offset{
239 0.5, 0.5, 0.0};
240 sample_field(np, ic, pvec, parr, farr, plo, dxi, dx, offset);
241 break;
242 }
243 }
244 }
245 }
246
247 const amrex::AmrCore& m_mesh;
248
250};
251
252} // namespace sampling
253} // namespace amr_wind
254
255#endif /* SAMPLINGCONTAINER_H */
Definition DerivedQuantity.H:32
Definition FieldRepo.H:86
Definition SamplingContainer.H:65
long m_total_particles
Definition SamplingContainer.H:249
const amrex::AmrCore & m_mesh
Definition SamplingContainer.H:247
void interpolate(const ParIterType &pti, const FType &farr, const int lev, const FieldLoc floc, const int ncomp, const int scomp)
Interpolate from an array4 onto particles.
Definition SamplingContainer.H:190
void interpolate_fields(const amrex::Vector< FType > &fields, const int scomp)
Perform field interpolation to sampling locations.
Definition SamplingContainer.H:88
SamplingContainer(amrex::AmrCore &mesh)
Definition SamplingContainer.H:67
long & num_sampling_particles()
Definition SamplingContainer.H:121
long num_sampling_particles() const
Definition SamplingContainer.H:119
void initialize_particles(const amrex::Vector< std::unique_ptr< SamplerBase > > &)
Definition SamplingContainer.cpp:28
void populate_buffer(std::vector< double > &buf)
Populate the buffer with data for all the particles.
Definition SamplingContainer.cpp:151
void setup_container(const int num_real_components, const int num_int_components=0)
Definition SamplingContainer.cpp:8
void interpolate_derived_fields(const DerivedQtyMgr &derived_mgr, const FieldRepo &repo, const int scomp)
Perform derived field interpolation to sampling locations.
Definition SamplingContainer.cpp:131
void sample_field(const int np, const int ic, SamplingContainer::ParticleVector &pvec, SamplingContainer::RealVector &pavec, const FType &farr, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &problo, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dxi, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dx, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &offset)
Definition SamplingContainer.H:136
FieldLoc
Definition FieldDescTypes.H:27
@ NODE
Node-centered (e.g., for pressure)
@ ZFACE
Face-centered in z-direction.
@ XFACE
Face-centered in x-direction (e.g., face normal velocity)
@ CELL
Cell-centered (default)
@ YFACE
Face-centered in y-direction.
static constexpr int SNArrayReal
Definition SamplingContainer.H:19
static constexpr int SNStructReal
Definition SamplingContainer.H:17
static constexpr int SNArrayInt
Definition SamplingContainer.H:20
static constexpr int SNStructInt
Definition SamplingContainer.H:18
Definition BCInterface.cpp:7
Definition console_io.cpp:25
Definition SamplingContainer.H:26
Indices
Definition SamplingContainer.H:27
@ nid
Index within the set for this particle.
Definition SamplingContainer.H:30
@ uid
Unique identifier for this particle.
Definition SamplingContainer.H:28
@ sid
Identifier of the set this particle belongs to.
Definition SamplingContainer.H:29