Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_FFT_OpenBCSolver.H
Go to the documentation of this file.
1#ifndef AMREX_FFT_OPENBC_SOLVER_H_
2#define AMREX_FFT_OPENBC_SOLVER_H_
3
4#include <AMReX_FFT_R2C.H>
5
6namespace amrex::FFT
7{
8
24template <typename T = Real>
26{
27public:
28 using MF = typename R2C<T>::MF;
29 using cMF = typename R2C<T>::cMF;
30
37 explicit OpenBCSolver (Box const& domain, Info const& info = Info{});
38
45 template <class F>
46 void setGreensFunction (F const& greens_function);
47
54 void solve (MF& phi, MF const& rho);
55
61 [[nodiscard]] Box const& Domain () const { return m_domain; }
62
63private:
64 static Box make_grown_domain (Box const& domain, Info const& info);
65
66 Box m_domain;
67 Info m_info;
68 R2C<T> m_r2c;
69 cMF m_G_fft;
70 std::unique_ptr<R2C<T>> m_r2c_green;
71};
72
73template <typename T>
74Box OpenBCSolver<T>::make_grown_domain (Box const& domain, Info const& info)
75{
76 IntVect len = domain.length();
77#if (AMREX_SPACEDIM == 3)
78 if (info.twod_mode) { len[2] = 0; }
79#else
81#endif
82 return Box(domain.smallEnd(), domain.bigEnd()+len, domain.ixType());
83}
84
85template <typename T>
86OpenBCSolver<T>::OpenBCSolver (Box const& domain, Info const& info)
87 : m_domain(domain),
88 m_info(info),
89 m_r2c(OpenBCSolver<T>::make_grown_domain(domain,info),
90 m_info.setDomainStrategy(FFT::DomainStrategy::slab))
91{
93 "FFT::OpenBCSolver does not support FFT::Info::batch_size > 1");
94
95#if (AMREX_SPACEDIM == 3)
96 if (m_info.twod_mode) {
97 auto gdom = make_grown_domain(domain,m_info);
98 gdom.enclosedCells(2);
99 gdom.setSmall(2, 0);
100 int nprocs = std::min({ParallelContext::NProcsSub(),
101 m_info.nprocs,
102 m_domain.length(2)});
103 gdom.setBig(2, nprocs-1);
104 m_r2c_green = std::make_unique<R2C<T>>(gdom,m_info);
105 auto [sd, ord] = m_r2c_green->getSpectralData();
106 m_G_fft = cMF(*sd, amrex::make_alias, 0, 1);
107 } else
108#endif
109 {
110 amrex::ignore_unused(m_r2c_green);
111 auto [sd, ord] = m_r2c.getSpectralData();
113 m_G_fft.define(sd->boxArray(), sd->DistributionMap(), 1, 0);
114 }
115}
116
117template <typename T>
118template <class F>
119void OpenBCSolver<T>::setGreensFunction (F const& greens_function)
120{
121 BL_PROFILE("OpenBCSolver::setGreensFunction");
122
123 auto* infab = m_info.twod_mode ? detail::get_fab(m_r2c_green->m_rx)
124 : detail::get_fab(m_r2c.m_rx);
125 auto const& lo = m_domain.smallEnd();
126 auto const& lo3 = lo.dim3();
127 auto const& len = m_domain.length3d();
128 if (infab) {
129 auto const& a = infab->array();
130 auto box = infab->box();
131 GpuArray<int,3> nimages{1,1,1};
132 int ndims = m_info.twod_mode ? AMREX_SPACEDIM-1 : AMREX_SPACEDIM;
133 for (int idim = 0; idim < ndims; ++idim) {
134 if (box.smallEnd(idim) == lo[idim] && box.length(idim) == 2*len[idim]) {
135 box.growHi(idim, -len[idim]+1); // +1 to include the middle plane
136 nimages[idim] = 2;
137 }
138 }
139 AMREX_ASSERT(nimages[0] == 2);
140 box.shift(-lo);
141 amrex::ParallelForOMP(box, [=] AMREX_GPU_DEVICE (int i, int j, int k)
142 {
143 T G;
144 if (i == len[0] || j == len[1] || k == len[2]) {
145 G = 0;
146 } else {
147 auto ii = i;
148 auto jj = (j > len[1]) ? 2*len[1]-j : j;
149 auto kk = (k > len[2]) ? 2*len[2]-k : k;
150 G = greens_function(ii+lo3.x,jj+lo3.y,kk+lo3.z);
151 }
152 for (int koff = 0; koff < nimages[2]; ++koff) {
153 int k2 = (koff == 0) ? k : 2*len[2]-k;
154 if ((k2 == 2*len[2]) || (koff == 1 && k == len[2])) {
155 continue;
156 }
157 for (int joff = 0; joff < nimages[1]; ++joff) {
158 int j2 = (joff == 0) ? j : 2*len[1]-j;
159 if ((j2 == 2*len[1]) || (joff == 1 && j == len[1])) {
160 continue;
161 }
162 for (int ioff = 0; ioff < nimages[0]; ++ioff) {
163 int i2 = (ioff == 0) ? i : 2*len[0]-i;
164 if ((i2 == 2*len[0]) || (ioff == 1 && i == len[0])) {
165 continue;
166 }
167 a(i2+lo3.x,j2+lo3.y,k2+lo3.z) = G;
168 }
169 }
170 }
171 });
172 }
173
174 if (m_info.twod_mode) {
175 m_r2c_green->forward(m_r2c_green->m_rx);
176 } else {
177 m_r2c.forward(m_r2c.m_rx);
178 }
179
180 if (!m_info.twod_mode) {
181 auto [sd, ord] = m_r2c.getSpectralData();
183 auto const* srcfab = detail::get_fab(*sd);
184 if (srcfab) {
185 auto* dstfab = detail::get_fab(m_G_fft);
186 if (dstfab) {
187 Gpu::dtod_memcpy_async(dstfab->dataPtr(), srcfab->dataPtr(), dstfab->nBytes());
188 } else {
189 amrex::Abort("FFT::OpenBCSolver: how did this happen");
190 }
191 }
192
193 m_r2c.prepare_openbc();
194 }
195}
196
197template <typename T>
198void OpenBCSolver<T>::solve (MF& phi, MF const& rho)
199{
200 BL_PROFILE("OpenBCSolver::solve");
201
202 auto& inmf = m_r2c.m_rx;
203 inmf.setVal(T(0));
204 inmf.ParallelCopy(rho, 0, 0, 1);
205
206 m_r2c.m_openbc_half = !m_info.twod_mode;
207 m_r2c.forward(inmf);
208 m_r2c.m_openbc_half = false;
209
210 auto scaling_factor = m_r2c.scalingFactor();
211
212 auto const* gfab = detail::get_fab(m_G_fft);
213 if (gfab) {
214 auto [sd, ord] = m_r2c.getSpectralData();
216 auto* rhofab = detail::get_fab(*sd);
217 if (rhofab) {
218 auto* pdst = rhofab->dataPtr();
219 auto const* psrc = gfab->dataPtr();
220 Box const& rhobox = rhofab->box();
221#if (AMREX_SPACEDIM == 3)
222 Long leng = gfab->box().numPts();
223 if (m_info.twod_mode) {
224 AMREX_ASSERT(gfab->box().length(2) == 1 &&
225 leng == (rhobox.length(0) * rhobox.length(1)));
226 } else {
227 AMREX_ASSERT(leng == rhobox.numPts());
228 }
229#endif
231 {
232#if (AMREX_SPACEDIM == 3)
233 Long isrc = i % leng;
234#else
235 Long isrc = i;
236#endif
237 pdst[i] *= psrc[isrc] * scaling_factor;
238 });
239 } else {
240 amrex::Abort("FFT::OpenBCSolver::solve: how did this happen?");
241 }
242 }
243
244 m_r2c.m_openbc_half = !m_info.twod_mode;
245 m_r2c.backward_doit(phi, phi.nGrowVect());
246 m_r2c.m_openbc_half = false;
247}
248
249}
250
251#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
Real * pdst
Definition AMReX_HypreMLABecLap.cpp:1140
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:123
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:356
__host__ __device__ IntVectND< dim > length() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:154
__host__ __device__ IndexTypeND< dim > ixType() const noexcept
Return the indexing type.
Definition AMReX_Box.H:135
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:111
Convolution-based solver for open boundary conditions using Green's functions.
Definition AMReX_FFT_OpenBCSolver.H:26
Box const & Domain() const
Access the physical domain this solver was built for.
Definition AMReX_FFT_OpenBCSolver.H:61
typename R2C< T >::MF MF
Definition AMReX_FFT_OpenBCSolver.H:28
void solve(MF &phi, MF const &rho)
Solve for phi given right-hand side rho.
Definition AMReX_FFT_OpenBCSolver.H:198
void setGreensFunction(F const &greens_function)
Populate the spectral Green's function used by subsequent solves.
Definition AMReX_FFT_OpenBCSolver.H:119
typename R2C< T >::cMF cMF
Definition AMReX_FFT_OpenBCSolver.H:29
OpenBCSolver(Box const &domain, Info const &info=Info{})
Build a solver over domain using the FFT Info settings in info.
Definition AMReX_FFT_OpenBCSolver.H:86
Parallel Discrete Fourier Transform.
Definition AMReX_FFT_R2C.H:47
std::conditional_t< C, cMF, std::conditional_t< std::is_same_v< T, Real >, MultiFab, FabArray< BaseFab< T > > > > MF
Definition AMReX_FFT_R2C.H:52
Open Boundary Poisson Solver.
Definition AMReX_OpenBC.H:70
OpenBCSolver()=default
Construct an empty solver; call define() before solving.
amrex_long Long
Definition AMReX_INT.H:30
void ParallelForOMP(T n, L const &f) noexcept
Performance-portable kernel launch function with optional OpenMP threading.
Definition AMReX_GpuLaunch.H:326
Definition AMReX_FFT_Helper.H:52
DomainStrategy
Definition AMReX_FFT_Helper.H:56
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:449
int NProcsSub() noexcept
number of ranks in current frame
Definition AMReX_ParallelContext.H:74
@ make_alias
Definition AMReX_MakeType.H:7
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:241
Definition AMReX_FFT_Helper.H:64
bool twod_mode
Definition AMReX_FFT_Helper.H:75
int batch_size
Batched FFT size. Only support in R2C, not R2X.
Definition AMReX_FFT_Helper.H:87
int nprocs
Max number of processes to use.
Definition AMReX_FFT_Helper.H:90
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:43