Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_FFT_Poisson.H
Go to the documentation of this file.
1#ifndef AMREX_FFT_POISSON_H_
2#define AMREX_FFT_POISSON_H_
3
4#include <AMReX_FFT.H>
5#include <AMReX_Geometry.H>
6
7namespace amrex::FFT
8{
9
17namespace detail {
18template <typename MF>
19void fill_physbc (MF& mf, Geometry const& geom,
20 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> const& bc);
21
22inline Geometry shift_geom (Geometry const& geom)
23{
24 auto const& dom = geom.Domain();
25 auto const& dlo = dom.smallEnd();
26 if (dlo == 0) {
27 return geom;
28 } else {
29 return Geometry(amrex::shift(dom,-dlo), geom.ProbDomain(), geom.CoordInt(),
30 geom.isPeriodic());
31 }
32}
33
34template <typename MF>
35void shift_mfs (IntVect const& domain_lo, MF const& mf1, MF const& mf2,
36 MF& new_mf1, MF& new_mf2)
37{
38 AMREX_ALWAYS_ASSERT(mf1.boxArray() == mf2.boxArray() &&
39 mf1.DistributionMap() == mf2.DistributionMap());
40 BoxArray ba = mf1.boxArray();
41 ba.shift(-domain_lo);
42 new_mf1.define(ba, mf1.DistributionMap(), 1, mf1.nGrowVect(),
43 MFInfo().SetAlloc(false));
44 new_mf2.define(ba, mf2.DistributionMap(), 1, mf2.nGrowVect(),
45 MFInfo().SetAlloc(false));
46 for (MFIter mfi(mf1); mfi.isValid(); ++mfi) {
47 typename MF::fab_type fab1(mf1[mfi], amrex::make_alias, 0, 1);
48 fab1.shift(-domain_lo);
49 new_mf1.setFab(mfi, std::move(fab1));
50
51 typename MF::fab_type fab2(mf2[mfi], amrex::make_alias, 0, 1);
52 fab2.shift(-domain_lo);
53 new_mf2.setFab(mfi, std::move(fab2));
54 }
55}
56
57}
59
65template <typename MF = MultiFab>
67{
68public:
69
76 Poisson (Geometry const& geom,
77 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> const& bc)
78 requires (IsFabArray_v<MF>)
79 : m_domain_lo(geom.Domain().smallEnd()),
80 m_geom(detail::shift_geom(geom)),
81 m_bc(bc)
82 {
83 bool all_periodic = true;
84 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
85 all_periodic = all_periodic
86 && (bc[idim].first == Boundary::periodic)
87 && (bc[idim].second == Boundary::periodic);
88 }
89 if (all_periodic) {
90 m_r2c = std::make_unique<R2C<typename MF::value_type>>(m_geom.Domain());
91 } else {
92 m_r2x = std::make_unique<R2X<typename MF::value_type>> (m_geom.Domain(), m_bc);
93 }
94 }
95
101 explicit Poisson (Geometry const& geom)
102 requires (IsFabArray_v<MF>)
103 : m_domain_lo(geom.Domain().smallEnd()),
104 m_geom(detail::shift_geom(geom)),
105 m_bc{AMREX_D_DECL(std::make_pair(Boundary::periodic,Boundary::periodic),
106 std::make_pair(Boundary::periodic,Boundary::periodic),
107 std::make_pair(Boundary::periodic,Boundary::periodic))}
108 {
109 if (m_geom.isAllPeriodic()) {
110 m_r2c = std::make_unique<R2C<typename MF::value_type>>(m_geom.Domain());
111 } else {
112 amrex::Abort("FFT::Poisson: wrong BC");
113 }
114 }
115
126 void solve (MF& a_soln, MF const& a_rhs);
127
128private:
129 IntVect m_domain_lo;
130 Geometry m_geom;
131 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> m_bc;
132 std::unique_ptr<R2X<typename MF::value_type>> m_r2x;
133 std::unique_ptr<R2C<typename MF::value_type>> m_r2c;
134};
135
136#if (AMREX_SPACEDIM == 3)
141template <typename MF = MultiFab>
143{
144public:
145
153 template <typename FA=MF>
154 requires (IsFabArray_v<FA>)
155 explicit PoissonOpenBC (Geometry const& geom,
157 IntVect const& ngrow = IntVect(0));
158
165 void solve (MF& soln, MF const& rhs);
166
170 void define_doit (); // has to be public for cuda
171
172private:
173 Geometry m_geom;
174 Box m_grown_domain;
175 IntVect m_ngrow;
177};
178#endif
179
186template <typename MF = MultiFab>
188{
189public:
190 using T = typename MF::value_type;
191
199 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> const& bc)
200 requires (IsFabArray_v<MF>)
201 : m_domain_lo(geom.Domain().smallEnd()),
202 m_geom(detail::shift_geom(geom)),
203 m_bc(bc)
204 {
205#if (AMREX_SPACEDIM < 3)
206 amrex::Abort("FFT::PoissonHybrid: 1D & 2D todo");
207 return;
208#endif
209 bool periodic_xy = true;
210 for (int idim = 0; idim < 2; ++idim) {
211 if (m_geom.Domain().length(idim) > 1) {
212 periodic_xy = periodic_xy && (bc[idim].first == Boundary::periodic);
213 AMREX_ALWAYS_ASSERT((bc[idim].first == Boundary::periodic &&
214 bc[idim].second == Boundary::periodic) ||
215 (bc[idim].first != Boundary::periodic &&
216 bc[idim].second != Boundary::periodic));
217 }
218 }
220 bc[2].second != Boundary::periodic);
221 Info info{};
222 info.setTwoDMode(true);
223 if (m_geom.Domain().length(0) == 1 || m_geom.Domain().length(1) == 1) {
224 info.setOneDMode(true);
225 }
226 if (periodic_xy) {
227 m_r2c = std::make_unique<R2C<typename MF::value_type>>(m_geom.Domain(),
228 info);
229 } else {
230 m_r2x = std::make_unique<R2X<typename MF::value_type>> (m_geom.Domain(),
231 m_bc, info);
232 }
233 build_spmf();
234 }
235
245 void solve (MF& soln, MF const& rhs);
253 void solve (MF& soln, MF const& rhs, Vector<T> const& dz);
261 void solve (MF& soln, MF const& rhs, Gpu::DeviceVector<T> const& dz);
262
269 void solve_2d (MF& a_soln, MF const& a_rhs);
270
279 template <typename TRIA, typename TRIC>
280 void solve (MF& a_soln, MF const& a_rhs, TRIA const& tria, TRIC const& tric);
281
282 // This is public for cuda
290 template <typename FA, typename TRIA, typename TRIC>
291 void solve_z (FA& spmf, TRIA const& tria, TRIC const& tric);
292
298 [[nodiscard]] std::pair<BoxArray,DistributionMapping> getSpectralDataLayout () const;
299
300private:
301
302 void build_spmf ();
303
304 IntVect m_domain_lo;
305 Geometry m_geom;
306 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> m_bc;
307 std::unique_ptr<R2X<typename MF::value_type>> m_r2x;
308 std::unique_ptr<R2C<typename MF::value_type>> m_r2c;
309 MF m_spmf_r;
310 using cMF = FabArray<BaseFab<GpuComplex<T>>>;
311 cMF m_spmf_c;
312};
313
314template <typename MF>
315void Poisson<MF>::solve (MF& a_soln, MF const& a_rhs)
316{
317 BL_PROFILE("FFT::Poisson::solve");
318
319 MF* soln = &a_soln;
320 MF const* rhs = &a_rhs;
321 MF solntmp, rhstmp;
322 if (m_domain_lo != 0) {
323 detail::shift_mfs(m_domain_lo, a_soln, a_rhs, solntmp, rhstmp);
324 soln = &solntmp;
325 rhs = &rhstmp;
326 }
327
328 AMREX_ASSERT(soln->is_cell_centered() && rhs->is_cell_centered());
329
330 using T = typename MF::value_type;
331
333 {AMREX_D_DECL(Math::pi<T>()/T(m_geom.Domain().length(0)),
334 Math::pi<T>()/T(m_geom.Domain().length(1)),
335 Math::pi<T>()/T(m_geom.Domain().length(2)))};
336 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
337 if (m_bc[idim].first == Boundary::periodic) {
338 fac[idim] *= T(2);
339 }
340 }
342 {AMREX_D_DECL(T(2)/T(m_geom.CellSize(0)*m_geom.CellSize(0)),
343 T(2)/T(m_geom.CellSize(1)*m_geom.CellSize(1)),
344 T(2)/T(m_geom.CellSize(2)*m_geom.CellSize(2)))};
345 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
346 if (m_geom.Domain().length(idim) == 1) {
347 dxfac[idim] = 0;
348 }
349 }
350 auto scale = (m_r2x) ? m_r2x->scalingFactor() : m_r2c->scalingFactor();
351
353 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
354 if (m_bc[idim].first == Boundary::odd &&
355 m_bc[idim].second == Boundary::odd)
356 {
357 offset[idim] = T(1);
358 }
359 else if ((m_bc[idim].first == Boundary::odd &&
360 m_bc[idim].second == Boundary::even) ||
361 (m_bc[idim].first == Boundary::even &&
362 m_bc[idim].second == Boundary::odd))
363 {
364 offset[idim] = T(0.5);
365 }
366 }
367
368 auto f = [=] AMREX_GPU_DEVICE (int i, int j, int k, auto& spectral_data)
369 {
371 AMREX_D_TERM(T a = fac[0]*(i+offset[0]);,
372 T b = fac[1]*(j+offset[1]);,
373 T c = fac[2]*(k+offset[2]));
374 T k2 = AMREX_D_TERM(dxfac[0]*(std::cos(a)-T(1)),
375 +dxfac[1]*(std::cos(b)-T(1)),
376 +dxfac[2]*(std::cos(c)-T(1)));
377 if (k2 != T(0)) {
378 spectral_data /= k2;
379 }
380 spectral_data *= scale;
381 };
382
383 IntVect const& ng = amrex::elemwiseMin(soln->nGrowVect(), IntVect(1));
384
385 if (m_r2x) {
386 m_r2x->forwardThenBackward_doit_0(*rhs, *soln, f, ng, m_geom.periodicity());
387 detail::fill_physbc(*soln, m_geom, m_bc);
388 } else {
389 m_r2c->forward(*rhs);
390 m_r2c->post_forward_doit_0(f);
391 m_r2c->backward_doit(*soln, ng, m_geom.periodicity());
392 }
393}
394
395#if (AMREX_SPACEDIM == 3)
396
397template <typename MF>
398template <typename FA>
399requires (IsFabArray_v<FA>)
401 IntVect const& ngrow)
402 : m_geom(geom),
403 m_grown_domain(amrex::grow(amrex::convert(geom.Domain(),ixtype),ngrow)),
404 m_ngrow(ngrow),
405 m_solver(m_grown_domain)
406{
407 define_doit();
408}
409
410template <typename MF>
412{
413 using T = typename MF::value_type;
414 auto const& lo = m_grown_domain.smallEnd();
415 auto const dx = T(m_geom.CellSize(0));
416 auto const dy = T(m_geom.CellSize(1));
417 auto const dz = T(m_geom.CellSize(2));
418 auto const gfac = T(1)/T(std::sqrt(T(12)));
419 // 0.125 comes from that there are 8 Gauss quadrature points
420 auto const fac = T(-0.125) * (dx*dy*dz) / (T(4)*Math::pi<T>());
421 m_solver.setGreensFunction([=] AMREX_GPU_DEVICE (int i, int j, int k) -> T
422 {
423 auto x = (T(i-lo[0]) - gfac) * dx; // first Gauss quadrature point
424 auto y = (T(j-lo[1]) - gfac) * dy;
425 auto z = (T(k-lo[2]) - gfac) * dz;
426 T r = 0;
427 for (int gx = 0; gx < 2; ++gx) {
428 for (int gy = 0; gy < 2; ++gy) {
429 for (int gz = 0; gz < 2; ++gz) {
430 auto xg = x + 2*gx*gfac*dx;
431 auto yg = y + 2*gy*gfac*dy;
432 auto zg = z + 2*gz*gfac*dz;
433 r += T(1)/std::sqrt(xg*xg+yg*yg+zg*zg);
434 }}}
435 return fac * r;
436 });
437}
438
439template <typename MF>
440void PoissonOpenBC<MF>::solve (MF& soln, MF const& rhs)
441{
442 AMREX_ASSERT(m_grown_domain.ixType() == soln.ixType() && m_grown_domain.ixType() == rhs.ixType());
443 m_solver.solve(soln, rhs);
444}
445
446#endif /* AMREX_SPACEDIM == 3 */
447
449namespace fft_poisson_detail {
450 template <typename T>
451 struct Tri_Zero {
452 [[nodiscard]] constexpr T operator() (int, int, int) const
453 {
454 return 0;
455 }
456 };
457
458 template <typename T>
459 struct Tri_Uniform {
461 T operator() (int, int, int) const
462 {
463 return m_dz2inv;
464 }
465 T m_dz2inv;
466 };
467
468 template <typename T>
469 struct TriA {
471 T operator() (int, int, int k) const
472 {
473 return (k > 0) ? T(2.0) / (m_dz[k]*(m_dz[k]+m_dz[k-1]))
474 : T(1.0) / (m_dz[k]* m_dz[k]);
475 }
476 T const* m_dz;
477 };
478
479 template <typename T>
480 struct TriC {
482 T operator() (int, int, int k) const
483 {
484 return (k < m_size-1) ? T(2.0) / (m_dz[k]*(m_dz[k]+m_dz[k+1]))
485 : T(1.0) / (m_dz[k]* m_dz[k]);
486 }
487 T const* m_dz;
488 int m_size;
489 };
490}
492
493template <typename MF>
494std::pair<BoxArray,DistributionMapping>
496{
497 if (!m_spmf_r.empty()) {
498 return std::make_pair(m_spmf_r.boxArray(), m_spmf_r.DistributionMap());
499 } else {
500 return std::make_pair(m_spmf_c.boxArray(), m_spmf_c.DistributionMap());
501 }
502}
503
504template <typename MF>
506{
507#if (AMREX_SPACEDIM == 3)
508 AMREX_ALWAYS_ASSERT(m_geom.Domain().length(2) > 1 &&
509 (m_geom.Domain().length(0) > 1 ||
510 m_geom.Domain().length(1) > 1));
511
512 if (m_r2c) {
513 Box cdomain = m_geom.Domain();
514 if (cdomain.length(0) > 1) {
515 cdomain.setBig(0,cdomain.length(0)/2);
516 } else {
517 cdomain.setBig(1,cdomain.length(1)/2);
518 }
519 auto cba = amrex::decompose(cdomain, ParallelContext::NProcsSub(),
520 {AMREX_D_DECL(true,true,false)});
521 DistributionMapping dm = detail::make_iota_distromap(cba.size());
522 m_spmf_c.define(cba, dm, 1, 0);
523 } else if (m_geom.Domain().length(0) > 1 &&
524 m_geom.Domain().length(1) > 1) {
525 if (m_r2x->m_cy.empty()) { // spectral data is real
526 auto sba = amrex::decompose(m_geom.Domain(),ParallelContext::NProcsSub(),
527 {AMREX_D_DECL(true,true,false)});
528 DistributionMapping dm = detail::make_iota_distromap(sba.size());
529 m_spmf_r.define(sba, dm, 1, 0);
530 } else { // spectral data is complex. one of the first two dimensions is periodic.
531 Box cdomain = m_geom.Domain();
532 if (m_bc[0].first == Boundary::periodic) {
533 cdomain.setBig(0,cdomain.length(0)/2);
534 } else {
535 cdomain.setBig(1,cdomain.length(1)/2);
536 }
537 auto cba = amrex::decompose(cdomain, ParallelContext::NProcsSub(),
538 {AMREX_D_DECL(true,true,false)});
539 DistributionMapping dm = detail::make_iota_distromap(cba.size());
540 m_spmf_c.define(cba, dm, 1, 0);
541 }
542 } else {
543 // spectral data is real
544 auto sba = amrex::decompose(m_geom.Domain(),ParallelContext::NProcsSub(),
545 {AMREX_D_DECL(true,true,false)});
546 DistributionMapping dm = detail::make_iota_distromap(sba.size());
547 m_spmf_r.define(sba, dm, 1, 0);
548 }
549#else
551#endif
552}
553
554template <typename MF>
555void PoissonHybrid<MF>::solve (MF& soln, MF const& rhs)
556{
557 auto delz = T(m_geom.CellSize(AMREX_SPACEDIM-1));
558 solve(soln, rhs,
559 fft_poisson_detail::Tri_Uniform<T>{T(1)/(delz*delz)},
560 fft_poisson_detail::Tri_Uniform<T>{T(1)/(delz*delz)});
561}
562
563template <typename MF>
564void PoissonHybrid<MF>::solve (MF& soln, MF const& rhs, Gpu::DeviceVector<T> const& dz)
565{
567 int(dz.size()) == m_geom.Domain().length(2),
568 "FFT::PoissonHybrid: dz.size() must equal domain length in z");
569
570 auto const* pdz = dz.dataPtr();
571 solve(soln, rhs,
572 fft_poisson_detail::TriA<T>{pdz},
573 fft_poisson_detail::TriC<T>{pdz,int(dz.size())});
574}
575
576template <typename MF>
577void PoissonHybrid<MF>::solve (MF& soln, MF const& rhs, Vector<T> const& dz)
578{
579 AMREX_ASSERT(soln.is_cell_centered() && rhs.is_cell_centered());
581 int(dz.size()) == m_geom.Domain().length(2),
582 "FFT::PoissonHybrid: dz.size() must equal domain length in z");
583
584#ifdef AMREX_USE_GPU
585 Gpu::DeviceVector<T> d_dz(dz.size());
586 Gpu::htod_memcpy_async(d_dz.data(), dz.data(), dz.size()*sizeof(T));
587 auto const* pdz = d_dz.data();
588#else
589 auto const* pdz = dz.data();
590#endif
591 solve(soln, rhs,
592 fft_poisson_detail::TriA<T>{pdz},
593 fft_poisson_detail::TriC<T>{pdz,int(dz.size())});
594}
595
596template <typename MF>
597void PoissonHybrid<MF>::solve_2d (MF& soln, MF const& rhs)
598{
599 solve(soln, rhs, fft_poisson_detail::Tri_Zero<T>{}, fft_poisson_detail::Tri_Zero<T>{});
600}
601
602template <typename MF>
603template <typename TRIA, typename TRIC>
604void PoissonHybrid<MF>::solve (MF& a_soln, MF const& a_rhs, TRIA const& tria,
605 TRIC const& tric)
606{
607 BL_PROFILE("FFT::PoissonHybrid::solve");
608
609 AMREX_ASSERT(a_soln.is_cell_centered() && a_rhs.is_cell_centered());
610
611#if (AMREX_SPACEDIM < 3)
612 amrex::ignore_unused(a_soln, a_rhs, tria, tric);
613#else
614
615 MF* soln = &a_soln;
616 MF const* rhs = &a_rhs;
617 MF solntmp, rhstmp;
618 if (m_domain_lo != 0) {
619 detail::shift_mfs(m_domain_lo, a_soln, a_rhs, solntmp, rhstmp);
620 soln = &solntmp;
621 rhs = &rhstmp;
622 }
623
624 IntVect const& ng = amrex::elemwiseMin(soln->nGrowVect(), IntVect(1));
625
626 if (m_r2c)
627 {
628 m_r2c->forward(*rhs, m_spmf_c);
629 solve_z(m_spmf_c, tria, tric);
630 m_r2c->backward_doit(m_spmf_c, *soln, ng, m_geom.periodicity());
631 }
632 else
633 {
634 if (m_r2x->m_cy.empty()) { // spectral data is real
635 m_r2x->forward(*rhs, m_spmf_r);
636 solve_z(m_spmf_r, tria, tric);
637 m_r2x->backward(m_spmf_r, *soln, ng, m_geom.periodicity());
638 } else { // spectral data is complex.
639 m_r2x->forward(*rhs, m_spmf_c);
640 solve_z(m_spmf_c, tria, tric);
641 m_r2x->backward(m_spmf_c, *soln, ng, m_geom.periodicity());
642 }
643 }
644
645 detail::fill_physbc(*soln, m_geom, m_bc);
646#endif
647}
648
649template <typename MF>
650template <typename FA, typename TRIA, typename TRIC>
651void PoissonHybrid<MF>::solve_z (FA& spmf, TRIA const& tria, TRIC const& tric)
652{
653 BL_PROFILE("PoissonHybrid::solve_z");
654
655#if (AMREX_SPACEDIM < 3)
656 amrex::ignore_unused(spmf, tria, tric);
657#else
658 auto facx = Math::pi<T>()/T(m_geom.Domain().length(0));
659 auto facy = Math::pi<T>()/T(m_geom.Domain().length(1));
660 if (m_bc[0].first == Boundary::periodic) { facx *= T(2); }
661 if (m_bc[1].first == Boundary::periodic) { facy *= T(2); }
662 auto dxfac = T(2)/T(m_geom.CellSize(0)*m_geom.CellSize(0));
663 auto dyfac = T(2)/T(m_geom.CellSize(1)*m_geom.CellSize(1));
664 auto scale = (m_r2x) ? m_r2x->scalingFactor() : m_r2c->scalingFactor();
665
666 if (m_geom.Domain().length(0) == 1) { dxfac = 0; }
667 if (m_geom.Domain().length(1) == 1) { dyfac = 0; }
668
669 GpuArray<T,AMREX_SPACEDIM-1> offset{T(0),T(0)};
670 for (int idim = 0; idim < AMREX_SPACEDIM-1; ++idim) {
671 if (m_geom.Domain().length(idim) > 1) {
672 if (m_bc[idim].first == Boundary::odd &&
673 m_bc[idim].second == Boundary::odd)
674 {
675 offset[idim] = T(1);
676 }
677 else if ((m_bc[idim].first == Boundary::odd &&
678 m_bc[idim].second == Boundary::even) ||
679 (m_bc[idim].first == Boundary::even &&
680 m_bc[idim].second == Boundary::odd))
681 {
682 offset[idim] = T(0.5);
683 }
684 }
685 }
686
687 if
688#ifndef _WIN32
689 constexpr
690#endif
691 (std::is_same_v<TRIA,fft_poisson_detail::Tri_Zero<T>> &&
692 std::is_same_v<TRIC,fft_poisson_detail::Tri_Zero<T>>) {
693 amrex::ignore_unused(tria,tric);
694#if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
695#pragma omp parallel
696#endif
697 for (MFIter mfi(spmf,TilingIfNotGPU()); mfi.isValid(); ++mfi)
698 {
699 auto const& spectral = spmf.array(mfi);
700 auto const& box = mfi.validbox();
701 amrex::ParallelFor(box, [=] AMREX_GPU_DEVICE (int i, int j, int k)
702 {
703 T a = facx*(i+offset[0]);
704 T b = facy*(j+offset[1]);
705 T k2 = dxfac * (std::cos(a)-T(1))
706 + dyfac * (std::cos(b)-T(1));
707 if (k2 != T(0)) {
708 spectral(i,j,k) /= k2;
709 }
710 spectral(i,j,k) *= scale;
711 });
712 }
713 } else {
714 bool zlo_neumann = m_bc[2].first == Boundary::even;
715 bool zhi_neumann = m_bc[2].second == Boundary::even;
716 bool is_singular = (offset[0] == T(0)) && (offset[1] == T(0))
717 && zlo_neumann && zhi_neumann;
718
719 auto nz = m_geom.Domain().length(2);
720
721#if defined(AMREX_USE_OMP) && !defined(AMREX_USE_GPU)
722#pragma omp parallel
723#endif
724 for (MFIter mfi(spmf); mfi.isValid(); ++mfi)
725 {
726 auto const& spectral = spmf.array(mfi);
727 auto const& box = mfi.validbox();
728 auto const& xybox = amrex::makeSlab(box, 2, 0);
729
730#ifdef AMREX_USE_GPU
731 // xxxxx TODO: We need to explore how to optimize this
732 // function. Maybe we can use cusparse. Maybe we should make
733 // z-direction to be the unit stride direction.
734
735 FArrayBox tridiag_workspace(box,4);
736 auto const& ald = tridiag_workspace.array(0);
737 auto const& bd = tridiag_workspace.array(1);
738 auto const& cud = tridiag_workspace.array(2);
739 auto const& scratch = tridiag_workspace.array(3);
740
741 amrex::ParallelFor(xybox, [=] AMREX_GPU_DEVICE (int i, int j, int)
742 {
743 T a = facx*(i+offset[0]);
744 T b = facy*(j+offset[1]);
745 T k2 = dxfac * (std::cos(a)-T(1))
746 + dyfac * (std::cos(b)-T(1));
747
748 // Tridiagonal solve
749 for(int k=0; k < nz; k++) {
750 if(k==0) {
751 ald(i,j,k) = T(0.);
752 cud(i,j,k) = tric(i,j,k);
753 if (zlo_neumann) {
754 bd(i,j,k) = k2 - cud(i,j,k);
755 } else {
756 bd(i,j,k) = k2 - cud(i,j,k) - T(2.0)*tria(i,j,k);
757 }
758 } else if (k == nz-1) {
759 ald(i,j,k) = tria(i,j,k);
760 cud(i,j,k) = T(0.);
761 if (zhi_neumann) {
762 bd(i,j,k) = k2 - ald(i,j,k);
763 if (i == 0 && j == 0 && is_singular) {
764 bd(i,j,k) *= T(2.0);
765 }
766 } else {
767 bd(i,j,k) = k2 - ald(i,j,k) - T(2.0)*tric(i,j,k);
768 }
769 } else {
770 ald(i,j,k) = tria(i,j,k);
771 cud(i,j,k) = tric(i,j,k);
772 bd(i,j,k) = k2 -ald(i,j,k)-cud(i,j,k);
773 }
774 }
775
776 scratch(i,j,0) = cud(i,j,0)/bd(i,j,0);
777 spectral(i,j,0) = spectral(i,j,0)/bd(i,j,0);
778
779 for (int k = 1; k < nz; k++) {
780 if (k < nz-1) {
781 scratch(i,j,k) = cud(i,j,k) / (bd(i,j,k) - ald(i,j,k) * scratch(i,j,k-1));
782 }
783 spectral(i,j,k) = (spectral(i,j,k) - ald(i,j,k) * spectral(i,j,k - 1))
784 / (bd(i,j,k) - ald(i,j,k) * scratch(i,j,k-1));
785 }
786
787 for (int k = nz - 2; k >= 0; k--) {
788 spectral(i,j,k) -= scratch(i,j,k) * spectral(i,j,k + 1);
789 }
790
791 for (int k = 0; k < nz; ++k) {
792 spectral(i,j,k) *= scale;
793 }
794 });
796
797#else
798
799 Gpu::DeviceVector<T> ald(nz);
801 Gpu::DeviceVector<T> cud(nz);
802 Gpu::DeviceVector<T> scratch(nz);
803
804 amrex::LoopOnCpu(xybox, [&] (int i, int j, int)
805 {
806 T a = facx*(i+offset[0]);
807 T b = facy*(j+offset[1]);
808 T k2 = dxfac * (std::cos(a)-T(1))
809 + dyfac * (std::cos(b)-T(1));
810
811 // Tridiagonal solve
812 for(int k=0; k < nz; k++) {
813 if(k==0) {
814 ald[k] = T(0.);
815 cud[k] = tric(i,j,k);
816 if (zlo_neumann) {
817 bd[k] = k2 - cud[k];
818 } else {
819 bd[k] = k2 - cud[k] - T(2.0)*tria(i,j,k);
820 }
821 } else if (k == nz-1) {
822 ald[k] = tria(i,j,k);
823 cud[k] = T(0.);
824 if (zhi_neumann) {
825 bd[k] = k2 - ald[k];
826 if (i == 0 && j == 0 && is_singular) {
827 bd[k] *= T(2.0);
828 }
829 } else {
830 bd[k] = k2 - ald[k] - T(2.0)*tric(i,j,k);
831 }
832 } else {
833 ald[k] = tria(i,j,k);
834 cud[k] = tric(i,j,k);
835 bd[k] = k2 -ald[k]-cud[k];
836 }
837 }
838
839 scratch[0] = cud[0]/bd[0];
840 spectral(i,j,0) = spectral(i,j,0)/bd[0];
841
842 for (int k = 1; k < nz; k++) {
843 if (k < nz-1) {
844 scratch[k] = cud[k] / (bd[k] - ald[k] * scratch[k-1]);
845 }
846 spectral(i,j,k) = (spectral(i,j,k) - ald[k] * spectral(i,j,k - 1))
847 / (bd[k] - ald[k] * scratch[k-1]);
848 }
849
850 for (int k = nz - 2; k >= 0; k--) {
851 spectral(i,j,k) -= scratch[k] * spectral(i,j,k + 1);
852 }
853
854 for (int k = 0; k < nz; ++k) {
855 spectral(i,j,k) *= scale;
856 }
857 });
858#endif
859 }
860 }
861#endif
862}
863
865namespace detail {
866
867template <class T>
868struct FFTPhysBCTag {
869 Array4<T> dfab;
870 Box dbox;
871 Boundary bc;
872 Orientation face;
873
875 Box const& box () const noexcept { return dbox; }
876};
877
878template <typename MF>
879void fill_physbc (MF& mf, Geometry const& geom,
880 Array<std::pair<Boundary,Boundary>,AMREX_SPACEDIM> const& bc)
881{
882 using T = typename MF::value_type;
883 using Tag = FFTPhysBCTag<T>;
884 Vector<Tag> tags;
885
886 for (MFIter mfi(mf, MFItInfo{}.DisableDeviceSync()); mfi.isValid(); ++mfi)
887 {
888 auto const& box = mfi.fabbox();
889 auto const& arr = mf.array(mfi);
890 for (OrientationIter oit; oit; ++oit) {
891 Orientation face = oit();
892 int idim = face.coordDir();
893 Box b = geom.Domain();
894 Boundary fbc;
895 if (face.isLow()) {
896 b.setRange(idim,geom.Domain().smallEnd(idim)-1);
897 fbc = bc[idim].first;
898 } else {
899 b.setRange(idim,geom.Domain().bigEnd(idim)+1);
900 fbc = bc[idim].second;
901 }
902 b &= box;
903 if (b.ok() && fbc != Boundary::periodic) {
904 tags.push_back(Tag{.dfab = arr, .dbox = b, .bc = fbc, .face = face});
905 }
906 }
907 }
908
909#if defined(AMREX_USE_GPU)
910 amrex::ParallelFor(tags, [=] AMREX_GPU_DEVICE (int i, int j, int k,
911 Tag const& tag) noexcept
912#else
913 auto ntags = int(tags.size());
914#ifdef AMREX_USE_OMP
915#pragma omp parallel for
916#endif
917 for (int itag = 0; itag < ntags; ++itag) {
918 Tag const& tag = tags[itag];
919 amrex::LoopOnCpu(tag.dbox, [&] (int i, int j, int k)
920#endif
921 {
922 int sgn = tag.face.isLow() ? 1 : -1;
923 IntVect siv = IntVect(AMREX_D_DECL(i,j,k))
924 + sgn * IntVect::TheDimensionVector(tag.face.coordDir());
925 if (tag.bc == Boundary::odd) {
926 tag.dfab(i,j,k) = -tag.dfab(siv);
927 } else { // even
928 tag.dfab(i,j,k) = tag.dfab(siv);
929 }
930 });
931#if !defined(AMREX_USE_GPU)
932 }
933#endif
934}
935}
937
938}
939
940#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_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1139
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Array4< T const > array() const noexcept
Definition AMReX_BaseFab.H:387
__host__ __device__ BoxND & setBig(const IntVectND< dim > &bg) noexcept
Redefine the big end of the BoxND.
Definition AMReX_Box.H:495
__host__ __device__ IntVectND< dim > length() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:155
A Fortran Array of REALs.
Definition AMReX_FArrayBox.H:231
Convolution-based solver for open boundary conditions using Green's functions.
Definition AMReX_FFT_OpenBCSolver.H:26
3D Poisson solver for periodic, Dirichlet & Neumann boundaries in the first two dimensions,...
Definition AMReX_FFT_Poisson.H:188
void solve(MF &soln, MF const &rhs)
Solve del dot grad soln = rhs for uniform spacing in all directions.
Definition AMReX_FFT_Poisson.H:555
typename MF::value_type T
Definition AMReX_FFT_Poisson.H:190
PoissonHybrid(Geometry const &geom, Array< std::pair< Boundary, Boundary >, 3 > const &bc)
Construct the hybrid Poisson solver (mixed BCs in z with optional nonuniform spacing).
Definition AMReX_FFT_Poisson.H:198
void solve_z(FA &spmf, TRIA const &tria, TRIC const &tric)
CUDA helper that applies the supplied tridiagonal operator along z.
Definition AMReX_FFT_Poisson.H:651
std::pair< BoxArray, DistributionMapping > getSpectralDataLayout() const
Layout information for spectral storage used by the hybrid solver.
Definition AMReX_FFT_Poisson.H:495
void solve_2d(MF &a_soln, MF const &a_rhs)
Solve an independent 2-D Poisson problem on every z-plane.
Definition AMReX_FFT_Poisson.H:597
Poisson solve for Open BC using FFT.
Definition AMReX_FFT_Poisson.H:143
void solve(MF &soln, MF const &rhs)
Solve the open-boundary Poisson problem.
Definition AMReX_FFT_Poisson.H:440
void define_doit()
Initialize the discretized Green's function cache (public for CUDA kernels).
Definition AMReX_FFT_Poisson.H:411
Poisson solver for periodic, Dirichlet & Neumann boundaries using FFT.
Definition AMReX_FFT_Poisson.H:67
Poisson(Geometry const &geom, Array< std::pair< Boundary, Boundary >, 3 > const &bc)
Construct a Poisson solver with explicit boundary types.
Definition AMReX_FFT_Poisson.H:76
Poisson(Geometry const &geom)
Construct a purely periodic Poisson solver.
Definition AMReX_FFT_Poisson.H:101
void solve(MF &a_soln, MF const &a_rhs)
Solve del dot grad soln = rhs.
Definition AMReX_FFT_Poisson.H:315
An Array of FortranArrayBox(FAB)-like Objects.
Definition AMReX_FabArray.H:344
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:75
const Box & Domain() const noexcept
Returns our rectangular domain.
Definition AMReX_Geometry.H:216
bool isAllPeriodic() const noexcept
Is domain periodic in all directions?
Definition AMReX_Geometry.H:344
__host__ static __device__ constexpr IndexTypeND< dim > TheCellType() noexcept
This static member function returns an IndexTypeND object of value IndexTypeND::CELL....
Definition AMReX_IndexType.H:150
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:88
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:172
Encapsulation of the Orientation of the Faces of a Box.
Definition AMReX_Orientation.H:29
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
size_type size() const noexcept
Definition AMReX_PODVector.H:648
T * dataPtr() noexcept
Definition AMReX_PODVector.H:670
T * data() noexcept
Definition AMReX_PODVector.H:666
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Grow BoxND in all directions by given amount.
Definition AMReX_Box.H:1289
std::array< T, N > Array
Definition AMReX_Array.H:26
Definition AMReX_FFT_Helper.H:52
Boundary
Definition AMReX_FFT_Helper.H:58
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:421
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
__host__ __device__ BoxND< dim > convert(const BoxND< dim > &b, const IntVectND< dim > &typ) noexcept
Return a BoxND with different type.
Definition AMReX_Box.H:1567
__host__ __device__ BoxND< dim > makeSlab(BoxND< dim > const &b, int direction, int slab_index) noexcept
Definition AMReX_Box.H:2140
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
double second() noexcept
Definition AMReX_Utility.cpp:940
__host__ __device__ BoxND< dim > shift(const BoxND< dim > &b, int dir, int nzones) noexcept
Return a BoxND with indices shifted by nzones in dir direction.
Definition AMReX_Box.H:1504
BoxArray decompose(Box const &domain, int nboxes, Array< bool, 3 > const &decomp, bool no_overlap)
Decompose domain box into BoxArray.
Definition AMReX_BoxArray.cpp:1943
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
__host__ __device__ constexpr IntVectND< dim > scale(const IntVectND< dim > &p, int s) noexcept
Returns a IntVectND obtained by multiplying each of the components of this IntVectND by s.
Definition AMReX_IntVect.H:1102
bool TilingIfNotGPU() noexcept
Definition AMReX_MFIter.H:12
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:241
const int[]
Definition AMReX_BLProfiler.cpp:1664
void LoopOnCpu(Dim3 lo, Dim3 hi, F const &f) noexcept
Definition AMReX_Loop.H:365
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Return the element-wise minimum of the given values for types like XDim3.
Definition AMReX_Algorithm.H:63
A multidimensional array accessor.
Definition AMReX_Array4.H:285
Definition AMReX_FFT_Helper.H:64
Info & setOneDMode(bool x)
Flag the degenerate 2-D mode (nx==1 or ny==1) that still batches along z.
Definition AMReX_FFT_Helper.H:123
Info & setTwoDMode(bool x)
Restrict transforms to the first two dimensions (3-D problems only).
Definition AMReX_FFT_Helper.H:112
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:43