Skip to content

Commit

Permalink
Cartesian1D curl-curl (AMReX-Codes#4242)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Weiqun Zhang <[email protected]>
  • Loading branch information
RevathiJambunathan and WeiqunZhang authored Dec 2, 2024
1 parent f27eb69 commit 8c2c27c
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 27 deletions.
8 changes: 7 additions & 1 deletion Src/LinearSolvers/MLMG/AMReX_MLCurlCurl.H
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ public:

// public for cuda

#if (AMREX_SPACEDIM > 1)
void smooth4 (int amrlev, int mglev, MF& sol, MF const& rhs, int color) const;
#else
void smooth1D (int amrlev, int mglev, MF& sol, MF const& rhs, int color) const;
#endif

void compresid (int amrlev, int mglev, MF& resid, MF const& b) const;

Expand All @@ -130,8 +134,10 @@ private:
Array<IntVect,3> m_etype
#if (AMREX_SPACEDIM == 3)
{IntVect(0,1,1), IntVect(1,0,1), IntVect(1,1,0)};
#else
#elif (AMREX_SPACEDIM == 2)
{IntVect(0,1), IntVect(1,0), IntVect(1,1)};
#else
{IntVect(0), IntVect(1), IntVect(1)};
#endif

mutable Vector<Vector<Array<std::unique_ptr<iMultiFab>,3>>> m_dotmask;
Expand Down
85 changes: 83 additions & 2 deletions Src/LinearSolvers/MLMG/AMReX_MLCurlCurl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,23 @@ void MLCurlCurl::setBeta (const Vector<Array<MultiFab const*,3>>& a_bcoefs)
*m_bcoefs[amrlev][mglev ][idim], ratio);
m_bcoefs[amrlev][mglev][idim]->FillBoundary(m_geom[amrlev][mglev].periodicity());
}
#if (AMREX_SPACEDIM == 2)
#if (AMREX_SPACEDIM < 3)
if (m_bcoefs[amrlev][mglev][2] == nullptr) {
m_bcoefs[amrlev][mglev][2] = std::make_unique<MultiFab>
(amrex::convert(m_grids[amrlev][mglev], m_etype[2]),
m_dmap[amrlev][mglev], 1, 0);
}
average_down_nodal(*m_bcoefs[amrlev][mglev-1][2],
*m_bcoefs[amrlev][mglev ][2], ratio);
#endif
#if (AMREX_SPACEDIM == 1)
if (m_bcoefs[amrlev][mglev][1] == nullptr) {
m_bcoefs[amrlev][mglev][1] = std::make_unique<MultiFab>
(amrex::convert(m_grids[amrlev][mglev], m_etype[1]),
m_dmap[amrlev][mglev], 1, 0);
}
average_down_nodal(*m_bcoefs[amrlev][mglev-1][1],
*m_bcoefs[amrlev][mglev ][1], ratio);
#endif
}
}
Expand Down Expand Up @@ -316,15 +325,72 @@ void MLCurlCurl::smooth (int amrlev, int mglev, MF& sol, const MF& rhs,

applyBC(amrlev, mglev, const_cast<MF&>(rhs), CurlCurlStateType::b);

for (int color = 0; color < 4; ++color) {
#if (AMREX_SPACEDIM == 1)
int ncolors = 2;
#else
int ncolors = 4;
#endif

for (int color = 0; color < ncolors; ++color) {
if (!skip_fillboundary) {
applyBC(amrlev, mglev, sol, CurlCurlStateType::x);
}
skip_fillboundary = false;
#if (AMREX_SPACEDIM == 1)
smooth1D(amrlev, mglev, sol, rhs, color);
#else
smooth4(amrlev, mglev, sol, rhs, color);
#endif
}
}

#if (AMREX_SPACEDIM == 1)
void MLCurlCurl::smooth1D (int amrlev, int mglev, MF& sol, MF const& rhs,
int color) const
{
auto const& ex = sol[0].arrays();
auto const& ey = sol[1].arrays();
auto const& ez = sol[2].arrays();
auto const& rhsx = rhs[0].const_arrays();
auto const& rhsy = rhs[1].const_arrays();
auto const& rhsz = rhs[2].const_arrays();

auto b = m_beta;

auto dinfo = getDirichletInfo(amrlev,mglev);
auto adxinv = this->m_geom[amrlev][mglev].InvCellSizeArray();
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
adxinv[idim] *= std::sqrt(m_alpha);
}

MultiFab nmf(amrex::convert(rhs[0].boxArray(),IntVect(1)),
rhs[0].DistributionMap(), 1, 0, MFInfo().SetAlloc(false));

if (m_bcoefs[amrlev][mglev][0]) {
auto const& bcx = m_bcoefs[amrlev][mglev][0]->const_arrays();
auto const& bcy = m_bcoefs[amrlev][mglev][1]->const_arrays();
auto const& bcz = m_bcoefs[amrlev][mglev][2]->const_arrays();
ParallelFor( nmf, [=] AMREX_GPU_DEVICE(int bno, int i, int j, int k)
{
mlcurlcurl_1D(i,j,k,ex[bno],ey[bno],ez[bno],
rhsx[bno],rhsy[bno],rhsz[bno],
bcx[bno],bcy[bno],bcz[bno],
adxinv,color,dinfo);
});
Gpu::streamSynchronize();
} else {
ParallelFor( nmf, [=] AMREX_GPU_DEVICE(int bno, int i, int j, int k)
{
mlcurlcurl_1D(i,j,k,ex[bno],ey[bno],ez[bno],
rhsx[bno],rhsy[bno],rhsz[bno],
b,adxinv,color,dinfo);
});
Gpu::streamSynchronize();
}
}
#endif

#if (AMREX_SPACEDIM > 1)
void MLCurlCurl::smooth4 (int amrlev, int mglev, MF& sol, MF const& rhs,
int color) const
{
Expand Down Expand Up @@ -386,6 +452,7 @@ void MLCurlCurl::smooth4 (int amrlev, int mglev, MF& sol, MF const& rhs,
}
Gpu::streamSynchronize();
}
#endif

void MLCurlCurl::solutionResidual (int amrlev, MF& resid, MF& x, const MF& b,
const MF* /*crse_bcdata*/)
Expand Down Expand Up @@ -453,6 +520,7 @@ void MLCurlCurl::compresid (int amrlev, int mglev, MF& resid, MF const& b) const

void MLCurlCurl::prepareForSolve ()
{
#if (AMREX_SPACEDIM > 1)
if (m_bcoefs[0][0][0] == nullptr) {
for (int amrlev = 0; amrlev < m_num_amr_levels; ++amrlev) {
for (int mglev = 0; mglev < m_num_mg_levels[amrlev]; ++mglev) {
Expand Down Expand Up @@ -536,6 +604,7 @@ void MLCurlCurl::prepareForSolve ()
}
}
}
#endif
}

Real MLCurlCurl::xdoty (int amrlev, int mglev, const MF& x, const MF& y,
Expand Down Expand Up @@ -634,6 +703,10 @@ void MLCurlCurl::applyBC (int amrlev, int mglev, MF& in, CurlCurlStateType type)
if (CurlCurlStateType::b == type) {
nmfs = 2; // no need to applyBC on Ez
}
#elif (AMREX_SPACEDIM == 1)
if (CurlCurlStateType::b == type) {
nmfs = 1; // no need to applyBC on Ey and Ez
}
#endif
Vector<MultiFab*> mfs(nmfs);
for (int imf = 0; imf < nmfs; ++imf) {
Expand Down Expand Up @@ -798,6 +871,10 @@ CurlCurlDirichletInfo MLCurlCurl::getDirichletInfo (int amrlev, int mglev) const
if (idim == 2) {
return std::numeric_limits<int>::lowest();
}
#elif (AMREX_SPACEDIM == 1)
if (idim > 0) {
return std::numeric_limits<int>::lowest();
}
#endif

if (face == 0) {
Expand Down Expand Up @@ -832,6 +909,10 @@ CurlCurlSymmetryInfo MLCurlCurl::getSymmetryInfo (int amrlev, int mglev) const
if (idim == 2) {
return std::numeric_limits<int>::lowest();
}
#elif (AMREX_SPACEDIM == 1)
if (idim > 0) {
return std::numeric_limits<int>::lowest();
}
#endif

if (face == 0) {
Expand Down
Loading

0 comments on commit 8c2c27c

Please sign in to comment.