From 80cc96b3b4a7a254fd9735b7fda5e065f1dfb029 Mon Sep 17 00:00:00 2001 From: Thomas Howarth Date: Sun, 8 Nov 2026 10:17:05 +0000 Subject: [PATCH 1/7] PLIC: advection_type "PLIC" in HydroUtils HydroUtils::ComputeFluxesOnBoxFromState advects a volume fraction with advection_type = "PLIC", as any other scalar: the edge states come from PLIC::ComputeEdgeStateAndFluxes with the myc_height reconstruction, and HydroUtils::ComputeFluxes forms the fluxes from them, so u_flux, knownFaceState, area weighting and ComputeConvectiveTerm work unchanged. The boundary conditions are carried by the ghost cells of F, filled with PLIC::FillWallGhosts. "PLIC" aborts with embedded boundaries, for velocities and for more than one component, and ExtrapVelToFaces does not accept it. PLIC::ComputeEdgeStateAndFluxes accepts empty flux arrays, for the edge states only. Tests/PLIC advects through HydroUtils with test.use_hydroutils = 1, with the same shape errors as the direct path and the volume conserved to round-off. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TwkdHV9vdS67quGfmN9xjG --- Docs/source/PLIC.rst | 16 +++++++- Docs/source/Schemes.rst | 1 + Docs/source/bcs.rst | 5 ++- PLIC/hydro_plic.H | 4 +- PLIC/hydro_plic_owkes_desjardins.cpp | 5 ++- Tests/PLIC/GNUmakefile | 6 ++- Tests/PLIC/README.md | 7 +++- Tests/PLIC/main.cpp | 5 ++- Tests/PLIC/test_advect.cpp | 38 ++++++++++++++++- Utils/hydro_compute_edgestate_and_flux.cpp | 30 +++++++++++++- Utils/hydro_utils.H | 47 +++++++++++++++++----- Utils/hydro_utils.cpp | 7 +++- 12 files changed, 144 insertions(+), 27 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index 56864f47..87357901 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -15,7 +15,8 @@ Like Godunov, MOL and BDS, the library provides spatial operators and leaves the :math:`F`, its ghost cells and the synchronization of the levels to the solver. It requires Cartesian coordinates and does not support embedded boundaries; ``PLIC::CheckMesh`` aborts on either. The reconstruction is selected by name, as ``advection_type`` is in -``HydroUtils::ComputeFluxesOnBoxFromState``. +``HydroUtils::ComputeFluxesOnBoxFromState``, which also advects :math:`F` with +``advection_type = "PLIC"``. What the solver provides ------------------------ @@ -128,6 +129,19 @@ the loop and pass ``check_courant = false``. No ghost face beyond a domain face condition (an inflow profile in those ghost faces is not seen): the faces on the domain boundary stand in for them. +Through ``HydroUtils`` +~~~~~~~~~~~~~~~~~~~~~~ + +A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState`` can advect +:math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` +and ``is_velocity = false``. It uses the ``myc_height`` reconstruction. The edge states are those of +``PLIC::ComputeEdgeStateAndFluxes``, and ``HydroUtils`` forms the fluxes from them as for any other +scalar, so that ``u_flux``, ``fluxes_are_area_weighted``, ``knownFaceState`` and +``HydroUtils::ComputeConvectiveTerm`` work as usual. :math:`F` needs ``PLIC::nGrowF("myc_height")`` +ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the boundary conditions in place of the +``BCRec`` arguments. ``"PLIC"`` aborts with embedded boundaries, and ``HydroUtils::ExtrapVelToFaces`` +does not accept it. + .. note:: Keep the interface on the finest level, by tagging the cells for which ``PLIC::interface_band`` diff --git a/Docs/source/Schemes.rst b/Docs/source/Schemes.rst index 830b3d51..11197237 100644 --- a/Docs/source/Schemes.rst +++ b/Docs/source/Schemes.rst @@ -6,6 +6,7 @@ Advection schemes ^^^^^^^^^^^^^^^^^ In AMReX-Hydro, the fundamental algorithm is a Method-of-Lines (MOL), Godunov, or Bell-Dawson-Shubin (BDS) approach. +The volume fraction of a two-phase flow can also be advected with geometric PLIC fluxes, see :ref:`plic`. Each method provides functions for two separate usages: 1. **Construct values of the normal velocity at the centroid on each cell face, termed "Pre-MAC" or diff --git a/Docs/source/bcs.rst b/Docs/source/bcs.rst index 232de842..5cd7b190 100644 --- a/Docs/source/bcs.rst +++ b/Docs/source/bcs.rst @@ -38,8 +38,9 @@ does not change which boundary condition is applied. Position-dependent BCs are only read by the (EB)Godunov routines. The MOL, EBMOL and BDS routines use the single BC per face :cpp:`BCRecs` only, so :cpp:`HydroUtils::ExtrapVelToFaces` and :cpp:`HydroUtils::ComputeFluxesOnBoxFromState` -abort if a BC MF (or Array4) is passed with ``advection_type`` set to ``"MOL"`` or ``"BDS"``, -rather than silently dropping it. +abort if a BC MF (or Array4) is passed with ``advection_type`` set to ``"MOL"``, ``"BDS"`` or +``"PLIC"``, rather than silently dropping it. With ``"PLIC"`` the boundary conditions are carried +by the ghost cells (see :ref:`plic`). The MacProjector supports mixed boundary conditions by making use of the underlying linear solver's diff --git a/PLIC/hydro_plic.H b/PLIC/hydro_plic.H index 5e0fb7f3..350f1552 100644 --- a/PLIC/hydro_plic.H +++ b/PLIC/hydro_plic.H @@ -87,7 +87,9 @@ void FitPlanes (amrex::MultiFab& plane, amrex::MultiFab const& F, int ngrow, * face of bx is written, so in a tiled MFIter loop two tiles write the faces * between them (the same values, as with Godunov::ComputeEdgeState): with * OpenMP, give each tile its own face arrays, or loop without tiling. - * \param flux_x output flux on the x faces of bx (likewise y, z) + * \param flux_x output flux on the x faces of bx (likewise y, z), or an empty Array4 for the + * edge states only, as HydroUtils::ComputeFluxesOnBoxFromState asks with + * advection_type "PLIC" * \param face_x output edge state on the x faces of bx (likewise y, z) * \param F volume fraction at t^n, with nGrowF(reconstruction) filled ghost cells * \param umac face velocity at t^{n+1/2}, discretely divergence-free, with one filled ghost diff --git a/PLIC/hydro_plic_owkes_desjardins.cpp b/PLIC/hydro_plic_owkes_desjardins.cpp index 2b413378..2ac1944e 100644 --- a/PLIC/hydro_plic_owkes_desjardins.cpp +++ b/PLIC/hydro_plic_owkes_desjardins.cpp @@ -108,7 +108,7 @@ struct FaceFluxes } else { fc(iv) = upwind_fraction(F, iv, dir, um(iv)); } - fl(iv) = to_flux * liquid; + if (fl) { fl(iv) = to_flux * liquid; } } }; @@ -248,7 +248,8 @@ ComputeEdgeStateAndFluxes (amrex::Box const& bx, amrex::ReduceData reduce_data(reduce_op); for (int d = 0; d < AMREX_SPACEDIM; ++d) { const amrex::Box faces = amrex::surroundingNodes(bx, d); - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(detail::FaceBox(flux[d], d).contains(faces) && + // An empty flux array asks for the edge states only + AMREX_ALWAYS_ASSERT_WITH_MESSAGE((!flux[d] || detail::FaceBox(flux[d], d).contains(faces)) && detail::FaceBox(face[d], d).contains(faces), "PLIC: the fluxes and edge states need the faces of bx"); // The nodes of bx read the faces up to one beyond them (see node_displacement) diff --git a/Tests/PLIC/GNUmakefile b/Tests/PLIC/GNUmakefile index bc59990d..bcc8cd16 100644 --- a/Tests/PLIC/GNUmakefile +++ b/Tests/PLIC/GNUmakefile @@ -19,12 +19,14 @@ Pdirs += Base Pdirs += Boundary Ppack += $(foreach dir, $(Pdirs), $(AMREX_HOME)/Src/$(dir)/Make.package) -Ppack += $(AMREX_HYDRO_HOME)/PLIC/Make.package +Hdirs := Utils Godunov MOL BDS PLIC + +Ppack += $(foreach dir, $(Hdirs), $(AMREX_HYDRO_HOME)/$(dir)/Make.package) include $(Ppack) Blocs := $(foreach dir, $(Pdirs), $(AMREX_HOME)/Src/$(dir)) -Blocs += $(AMREX_HYDRO_HOME)/PLIC +Blocs += $(foreach dir, $(Hdirs), $(AMREX_HYDRO_HOME)/$(dir)) INCLUDE_LOCATIONS += $(Blocs) VPATH_LOCATIONS += $(Blocs) diff --git a/Tests/PLIC/README.md b/Tests/PLIC/README.md index 6a729b4f..ec23009c 100644 --- a/Tests/PLIC/README.md +++ b/Tests/PLIC/README.md @@ -22,8 +22,11 @@ mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex The advection cases return to their initial shape at `t = prob.period`, so the shape error is the L1 difference from the initial volume fraction. `plic.reconstruction` selects the reconstruction (default `myc_height`). Set `amr.max_level = 0` and `amr.n_cell` for a uniform grid. The `test.max_*` -parameters set the checks. `test.plot_file = ` writes the final volume fraction as a plotfile -(component `vof`, every level), e.g. `mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex test.plot_file=plt`. +parameters set the checks. With `test.use_hydroutils = 1` the test advects F as a solver would any +other scalar, through `HydroUtils::ComputeFluxesOnBoxFromState` with `advection_type = "PLIC"`, +which gives the same results. `test.plot_file = ` writes the final volume fraction as a +plotfile (component `vof`, every level), e.g. +`mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex test.plot_file=plt`. `PLICTestUtils.H` holds what the test solver does around the library and a real solver would do its own way: the prescribed velocities, the time step and the refinement tags. diff --git a/Tests/PLIC/main.cpp b/Tests/PLIC/main.cpp index 427c5a7c..5790a491 100644 --- a/Tests/PLIC/main.cpp +++ b/Tests/PLIC/main.cpp @@ -4,8 +4,9 @@ // test.kind = kernels the plane geometry, the normals, the plane fit (test_geometry.cpp), the // wall ghost cells (test_walls.cpp) and the face density (test_density.cpp) // test.kind = advect a prescribed velocity on an AMR hierarchy (see inputs.*), advanced with -// PLIC::ComputeEdgeStateAndFluxes in the solver's own MFIter loop -// (test_advect.cpp) +// PLIC::ComputeEdgeStateAndFluxes in the solver's own MFIter loop, or with +// HydroUtils::ComputeFluxesOnBoxFromState and advection_type "PLIC" if +// test.use_hydroutils = 1 (test_advect.cpp) #include "test_util.H" diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index c9af06b5..ccee76f7 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -1,12 +1,15 @@ // Advection: a prescribed velocity on an AMR hierarchy (see inputs.*), advanced with -// PLIC::ComputeEdgeStateAndFluxes in the solver's own MFIter loop +// PLIC::ComputeEdgeStateAndFluxes in the solver's own MFIter loop, or with +// HydroUtils::ComputeFluxesOnBoxFromState and advection_type "PLIC" if test.use_hydroutils = 1 #include "test_util.H" #include +#include #include #include +#include #include #include #include @@ -140,6 +143,11 @@ class AdvectTest final : public AmrCore ppt.query("max_l1", m_max_l1); ppt.query("max_volume_change", m_max_volume_change); ppt.query("max_f_excursion", m_max_f_excursion); + ppt.query("use_hydroutils", m_use_hydroutils); + if (m_use_hydroutils) { + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_reconstruction == "myc_height", + "test.use_hydroutils needs plic.reconstruction = myc_height"); + } // All the non-periodic domain faces are walls: extrapolated, which FillWallGhosts then extends m_bcrec.resize(1); @@ -149,6 +157,13 @@ class AdvectTest final : public AmrCore m_bcrec[0].setHi(d, periodic ? BCType::int_dir : BCType::foextrap); } + m_bcrec_d.resize(1); + m_iconserv_d.resize(1); + Gpu::copyAsync(Gpu::hostToDevice, m_bcrec.begin(), m_bcrec.end(), m_bcrec_d.begin()); + const int iconserv = 1; + Gpu::copyAsync(Gpu::hostToDevice, &iconserv, &iconserv + 1, m_iconserv_d.begin()); + Gpu::streamSynchronize(); + m_F.resize(max_level + 1); m_umac0.resize(max_level + 1); } @@ -163,7 +178,8 @@ class AdvectTest final : public AmrCore FillLevel(lev, m_F[lev]); } - Print() << " reconstruction " << m_reconstruction << "\n"; + Print() << " reconstruction " << m_reconstruction + << (m_use_hydroutils ? ", through HydroUtils" : "") << "\n"; const Real vol0 = Volume(); Real vol_now = vol0; @@ -332,6 +348,21 @@ class AdvectTest final : public AmrCore #pragma omp parallel if (Gpu::notInLaunchRegion()) #endif for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { + if (m_use_hydroutils) { + // As for any other conservative scalar, with the boundary conditions in the + // ghost cells of F. PLIC reads neither divu nor a forcing. + HydroUtils::ComputeFluxesOnBoxFromState( + mfi.validbox(), 1, mfi, m_F[lev].const_array(mfi), + AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), + AMREX_D_DECL(face[lev][0].array(mfi), face[lev][1].array(mfi), face[lev][2].array(mfi)), + false, + AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), + umac[lev][2].const_array(mfi)), + Array4{}, Array4{}, Geom(lev), dt, + m_bcrec, m_bcrec_d.data(), m_iconserv_d.data(), + false, false, false, false, "PLIC"); + continue; + } PLIC::ComputeEdgeStateAndFluxes( mfi.validbox(), AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), @@ -469,6 +500,9 @@ class AdvectTest final : public AmrCore // The face velocity at t = 0 on each level, with a ghost layer (see Velocity) Vector> m_umac0; Vector m_bcrec; + Gpu::DeviceVector m_bcrec_d; + Gpu::DeviceVector m_iconserv_d; + bool m_use_hydroutils = false; int m_regrid_int = 4; int m_refine_band = 2; int m_max_step = 1000000; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index 6e25ed53..806b2e45 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -124,9 +125,13 @@ namespace { { Abort("BDS is not available with EB"); } + else if (advection_type == "PLIC") + { + Abort("PLIC is not available with EB"); + } else { - Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL or BDS)"); + Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL, BDS or PLIC)"); } } else @@ -175,9 +180,30 @@ namespace { l_dt, h_bcrec, d_bcrec, iconserv, is_velocity, allow_inflow_on_outflow); } + else if (advection_type == "PLIC") + { +#if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) + // The flux volumes are not cut by embedded boundaries, so a level with cut + // cells is rejected as a whole, rather than only its cut boxes. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), + "PLIC is not available with EB"); +#endif + // The volume fraction is one scalar, advected in conservative form. Its ghost + // cells carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs + // are not read here. The edge states are the liquid fractions of the flux + // volumes, which ComputeFluxes turns back into the geometric fluxes. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, + "PLIC advects one volume fraction, not a velocity"); + PLIC::ComputeEdgeStateAndFluxes(bx, + AMREX_D_DECL(Array4{}, Array4{}, Array4{}), + AMREX_D_DECL(face_x,face_y,face_z), + q, + AMREX_D_DECL(u_mac,v_mac,w_mac), + geom, l_dt, "myc_height"); + } else { - Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL or BDS)"); + Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL, BDS or PLIC)"); } } } diff --git a/Utils/hydro_utils.H b/Utils/hydro_utils.H index d0654f3e..5a877c3b 100644 --- a/Utils/hydro_utils.H +++ b/Utils/hydro_utils.H @@ -65,8 +65,13 @@ void enforceInOutSolvability ( * 1. divu must be a valid Array4 in 3D Godunov for every component, and for * BDS whenever any component is conservative. * - * \note advection_type must be one of "Godunov", "MOL" or "BDS". BDS is not - * available with EB. + * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and + * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, + * iconserv = 1, not a velocity) with the geometric fluxes of + * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs + * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which + * carry the boundary conditions in place of the BCRecs, and the face Courant + * number must be below 1. See the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -161,8 +166,13 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * 1. divu must be a valid Array4 in 3D Godunov for every component, and for * BDS whenever any component is conservative. * - * \note advection_type must be one of "Godunov", "MOL" or "BDS". BDS is not - * available with EB. + * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and + * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, + * iconserv = 1, not a velocity) with the geometric fluxes of + * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs + * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which + * carry the boundary conditions in place of the BCRecs, and the face Courant + * number must be below 1. See the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -221,8 +231,13 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * 1. divu must be a valid Array4 in 3D Godunov for every component, and for * BDS whenever any component is conservative. * - * \note advection_type must be one of "Godunov", "MOL" or "BDS". BDS is not - * available with EB. + * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and + * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, + * iconserv = 1, not a velocity) with the geometric fluxes of + * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs + * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which + * carry the boundary conditions in place of the BCRecs, and the face Courant + * number must be below 1. See the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -280,8 +295,13 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * 1. divu must be a valid Array4 in 3D Godunov for every component, and for * BDS whenever any component is conservative. * - * \note advection_type must be one of "Godunov", "MOL" or "BDS". BDS is not - * available with EB. + * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and + * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, + * iconserv = 1, not a velocity) with the geometric fluxes of + * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs + * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which + * carry the boundary conditions in place of the BCRecs, and the face Courant + * number must be below 1. See the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -339,8 +359,13 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * 1. divu must be a valid Array4 in 3D Godunov for every component, and for * BDS whenever any component is conservative. * - * \note advection_type must be one of "Godunov", "MOL" or "BDS". BDS is not - * available with EB. + * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and + * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, + * iconserv = 1, not a velocity) with the geometric fluxes of + * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs + * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which + * carry the boundary conditions in place of the BCRecs, and the face Courant + * number must be below 1. See the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -395,6 +420,7 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * \note advection_type must be one of "Godunov", "MOL" or "BDS", matching * ComputeFluxesOnBoxFromState. BDS extrapolates the velocity to faces with * Godunov PLM, so "BDS" here ignores godunov_ppm, and is not available with EB. + * "PLIC" advects a volume fraction, not the velocity, and is not accepted here. * * \note This convenience overload takes neither velocity_on_eb_inflow nor BC_MF. * Use the general overload below if you need either of them. @@ -426,6 +452,7 @@ ExtrapVelToFaces ( amrex::MultiFab const& vel, * \note advection_type must be one of "Godunov", "MOL" or "BDS", matching * ComputeFluxesOnBoxFromState. BDS extrapolates the velocity to faces with * Godunov PLM, so "BDS" here ignores godunov_ppm, and is not available with EB. + * "PLIC" advects a volume fraction, not the velocity, and is not accepted here. * * \note BC_MF, the position-dependent boundary conditions, is only supported with * (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those kernels diff --git a/Utils/hydro_utils.cpp b/Utils/hydro_utils.cpp index 1ca7eae5..5f64ab33 100644 --- a/Utils/hydro_utils.cpp +++ b/Utils/hydro_utils.cpp @@ -306,8 +306,13 @@ HydroUtils::ComputeConvectiveTerm(Box const& bx, int num_comp, } #endif } + else if (advection_type == "PLIC") + { + // The volume fraction is advected in conservative form only: nothing to add + } else { - amrex::Abort("HydroUtils::ComputeConvectiveTerm: unknown advection_type "); + amrex::Abort("HydroUtils::ComputeConvectiveTerm: unknown advection_type " + advection_type + + " (expected Godunov, MOL, BDS or PLIC)"); } } From abcf6771182962616a3980db72ba1f86569d5c75 Mon Sep 17 00:00:00 2001 From: ThomasHowarth Date: Wed, 18 Nov 2026 11:05:53 +0000 Subject: [PATCH 2/7] PLIC: review fixes to advection_type "PLIC" in HydroUtils - HydroUtils writes the geometric fluxes of PLIC::ComputeEdgeStateAndFluxes when they are formed with u_mac, rather than edge state * u: where shear folds a flux volume the edge state is held to [0,1] and that product is not the liquid that crosses the face. Through HydroUtils the fluxes and edge states are now those of PLIC to the bit (inputs.3d.deform differed at 1e-8). With a corrective u_flux, or knownFaceState, the fluxes are still formed from the edge states. - The PLIC branch is built only with HYDRO_USE_PLIC, which PLIC/Make.package and PLIC/CMakeLists.txt define, so GNUmake codes that do not list PLIC still build; "PLIC" then aborts. - Check iconserv = 1 and that bc_arr is not passed. - One short PLIC note in the ComputeFluxesOnBoxFromState docs, pointing to the PLIC section, rather than five copies. - Tests/PLIC with test.use_hydroutils = 1 checks the HydroUtils fluxes and edge states against PLIC::ComputeEdgeStateAndFluxes, to the bit. Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 17 ++-- PLIC/CMakeLists.txt | 3 + PLIC/Make.package | 3 + PLIC/hydro_plic.H | 3 +- Tests/PLIC/README.md | 8 +- Tests/PLIC/test_advect.cpp | 47 +++++++++- Utils/hydro_compute_edgestate_and_flux.cpp | 100 +++++++++++++++------ Utils/hydro_utils.H | 40 +++------ 8 files changed, 152 insertions(+), 69 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index 87357901..8eab53c1 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -134,13 +134,16 @@ Through ``HydroUtils`` A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState`` can advect :math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` -and ``is_velocity = false``. It uses the ``myc_height`` reconstruction. The edge states are those of -``PLIC::ComputeEdgeStateAndFluxes``, and ``HydroUtils`` forms the fluxes from them as for any other -scalar, so that ``u_flux``, ``fluxes_are_area_weighted``, ``knownFaceState`` and -``HydroUtils::ComputeConvectiveTerm`` work as usual. :math:`F` needs ``PLIC::nGrowF("myc_height")`` -ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the boundary conditions in place of the -``BCRec`` arguments. ``"PLIC"`` aborts with embedded boundaries, and ``HydroUtils::ExtrapVelToFaces`` -does not accept it. +and ``is_velocity = false``. It uses the ``myc_height`` reconstruction, and gives the edge states and +the fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit. With a ``u_flux`` other than the face +velocity (a corrective velocity), or with ``knownFaceState``, the fluxes are formed from the edge +states as for any other scalar. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds +nothing, as :math:`F` is advected in conservative form. :math:`F` needs +``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the +boundary conditions in place of the ``BCRec`` arguments; ``divu``, the forcing ``fq``, +``limiter_type`` and ``allow_inflow_on_outflow`` are not read. ``"PLIC"`` aborts with embedded +boundaries, and ``HydroUtils::ExtrapVelToFaces`` does not accept it. With GNUmake, add the ``PLIC`` +directory to the build (it defines ``HYDRO_USE_PLIC``); without it ``"PLIC"`` aborts. .. note:: diff --git a/PLIC/CMakeLists.txt b/PLIC/CMakeLists.txt index 793594e7..e884a342 100644 --- a/PLIC/CMakeLists.txt +++ b/PLIC/CMakeLists.txt @@ -15,3 +15,6 @@ target_sources( hydro_plic_owkes_desjardins_K.H hydro_plic_reconstruction.cpp ) + +# HydroUtils::ComputeFluxesOnBoxFromState accepts advection_type "PLIC" when PLIC is built +target_compile_definitions(amrex_hydro PUBLIC HYDRO_USE_PLIC) diff --git a/PLIC/Make.package b/PLIC/Make.package index 8e69b701..29b255d1 100644 --- a/PLIC/Make.package +++ b/PLIC/Make.package @@ -5,3 +5,6 @@ CEXE_sources += hydro_plic.cpp CEXE_sources += hydro_plic_reconstruction.cpp CEXE_headers += hydro_plic_owkes_desjardins_K.H CEXE_sources += hydro_plic_owkes_desjardins.cpp + +# HydroUtils::ComputeFluxesOnBoxFromState accepts advection_type "PLIC" when PLIC is built +DEFINES += -DHYDRO_USE_PLIC diff --git a/PLIC/hydro_plic.H b/PLIC/hydro_plic.H index 350f1552..feb8d4e0 100644 --- a/PLIC/hydro_plic.H +++ b/PLIC/hydro_plic.H @@ -88,8 +88,7 @@ void FitPlanes (amrex::MultiFab& plane, amrex::MultiFab const& F, int ngrow, * between them (the same values, as with Godunov::ComputeEdgeState): with * OpenMP, give each tile its own face arrays, or loop without tiling. * \param flux_x output flux on the x faces of bx (likewise y, z), or an empty Array4 for the - * edge states only, as HydroUtils::ComputeFluxesOnBoxFromState asks with - * advection_type "PLIC" + * edge states only * \param face_x output edge state on the x faces of bx (likewise y, z) * \param F volume fraction at t^n, with nGrowF(reconstruction) filled ghost cells * \param umac face velocity at t^{n+1/2}, discretely divergence-free, with one filled ghost diff --git a/Tests/PLIC/README.md b/Tests/PLIC/README.md index ec23009c..750a011b 100644 --- a/Tests/PLIC/README.md +++ b/Tests/PLIC/README.md @@ -23,10 +23,10 @@ The advection cases return to their initial shape at `t = prob.period`, so the s difference from the initial volume fraction. `plic.reconstruction` selects the reconstruction (default `myc_height`). Set `amr.max_level = 0` and `amr.n_cell` for a uniform grid. The `test.max_*` parameters set the checks. With `test.use_hydroutils = 1` the test advects F as a solver would any -other scalar, through `HydroUtils::ComputeFluxesOnBoxFromState` with `advection_type = "PLIC"`, -which gives the same results. `test.plot_file = ` writes the final volume fraction as a -plotfile (component `vof`, every level), e.g. -`mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex test.plot_file=plt`. +other scalar, through `HydroUtils::ComputeFluxesOnBoxFromState` with `advection_type = "PLIC"`, and +checks that it gives the fluxes and edge states of `PLIC::ComputeEdgeStateAndFluxes` to the bit. +`test.plot_file = ` writes the final volume fraction as a plotfile (component `vof`, every +level), e.g. `mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex test.plot_file=plt`. `PLICTestUtils.H` holds what the test solver does around the library and a real solver would do its own way: the prescribed velocities, the time step and the refinement tags. diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index ccee76f7..43cfbd88 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -214,6 +214,8 @@ class AdvectTest final : public AmrCore Real edge_mismatch = m_edge_mismatch; ParallelDescriptor::ReduceRealMax(edge_out); ParallelDescriptor::ReduceRealMax(edge_mismatch); + Real hydroutils_diff = m_hydroutils_diff; + ParallelDescriptor::ReduceRealMax(hydroutils_diff); const Real vol1 = vol_now; const Real f_out = max(0.0_rt - f_min, f_max - 1.0_rt, 0.0_rt); const Real l1 = ShapeError(); @@ -228,6 +230,10 @@ class AdvectTest final : public AmrCore // Exactly: an edge state the clamp misses leaves [0,1] by round-off only check(edge_out == 0.0_rt, "edge state within [0,1]"); check(edge_mismatch <= roundoff_tol, "edge state = flux / u, held to [0,1], on every face with a flow"); + if (m_use_hydroutils) { + Print() << " largest difference from PLIC::ComputeEdgeStateAndFluxes " << hydroutils_diff << "\n"; + check(hydroutils_diff == 0.0_rt, "HydroUtils gives the fluxes and edge states of PLIC, to the bit"); + } if (m_max_l1 > 0.0_rt) { check(l1 <= m_max_l1, "L1 shape error within " + std::to_string(m_max_l1)); } if (m_max_volume_change > 0.0_rt) { @@ -302,6 +308,43 @@ class AdvectTest final : public AmrCore return dt; } + // Through HydroUtils, the fluxes and edge states of one step on one level, on this rank, are + // those of PLIC::ComputeEdgeStateAndFluxes, to the bit + void CompareWithPLIC (Array const& flux, Array const& face, + Array const& umac, int lev, Real dt) + { + Array flux_ref; + Array face_ref; + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + flux_ref[d].define(flux[d].boxArray(), flux[d].DistributionMap(), 1, 0); + face_ref[d].define(face[d].boxArray(), face[d].DistributionMap(), 1, 0); + } +#ifdef AMREX_USE_OMP +#pragma omp parallel if (Gpu::notInLaunchRegion()) +#endif + for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { + PLIC::ComputeEdgeStateAndFluxes( + mfi.validbox(), + AMREX_D_DECL(flux_ref[0].array(mfi), flux_ref[1].array(mfi), flux_ref[2].array(mfi)), + AMREX_D_DECL(face_ref[0].array(mfi), face_ref[1].array(mfi), face_ref[2].array(mfi)), + m_F[lev].const_array(mfi), + AMREX_D_DECL(umac[0].const_array(mfi), umac[1].const_array(mfi), umac[2].const_array(mfi)), + Geom(lev), dt, m_reconstruction); + } + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + auto const& fl = flux[d].const_arrays(); + auto const& fc = face[d].const_arrays(); + auto const& fl_ref = flux_ref[d].const_arrays(); + auto const& fc_ref = face_ref[d].const_arrays(); + const Real diff = ParReduce(TypeList{}, TypeList{}, flux[d], IntVect(0), + [=] AMREX_GPU_DEVICE (int b, int i, int j, int k) noexcept -> GpuTuple { + return {nan_as_max(max(std::abs(fl[b](i, j, k) - fl_ref[b](i, j, k)), + std::abs(fc[b](i, j, k) - fc_ref[b](i, j, k))))}; + }); + m_hydroutils_diff = max(m_hydroutils_diff, diff); + } + } + // The edge states of one step on one level, on this rank: on every face with a flow, the liquid // fraction of the flux volume (the flux over the face velocity), held to [0,1] void CheckEdgeStates (Array const& flux, Array const& face, @@ -350,7 +393,7 @@ class AdvectTest final : public AmrCore for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { if (m_use_hydroutils) { // As for any other conservative scalar, with the boundary conditions in the - // ghost cells of F. PLIC reads neither divu nor a forcing. + // ghost cells of F HydroUtils::ComputeFluxesOnBoxFromState( mfi.validbox(), 1, mfi, m_F[lev].const_array(mfi), AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), @@ -373,6 +416,7 @@ class AdvectTest final : public AmrCore Geom(lev), dt, m_reconstruction, false, false); } CheckEdgeStates(flux[lev], face[lev], umac[lev]); + if (m_use_hydroutils) { CompareWithPLIC(flux[lev], face[lev], umac[lev], lev, dt); } } for (int lev = nlevs - 1; lev > 0; --lev) { average_down_faces(GetArrOfConstPtrs(flux[lev]), GetArrOfPtrs(flux[lev - 1]), refRatio(lev - 1), @@ -515,6 +559,7 @@ class AdvectTest final : public AmrCore // On this rank Real m_edge_out = 0.0_rt; Real m_edge_mismatch = 0.0_rt; + Real m_hydroutils_diff = 0.0_rt; std::string m_plot_file; }; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index 806b2e45..b9f74152 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -5,7 +5,9 @@ #include #include #include +#ifdef HYDRO_USE_PLIC #include +#endif #include #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -125,10 +127,6 @@ namespace { { Abort("BDS is not available with EB"); } - else if (advection_type == "PLIC") - { - Abort("PLIC is not available with EB"); - } else { Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL, BDS or PLIC)"); @@ -180,33 +178,62 @@ namespace { l_dt, h_bcrec, d_bcrec, iconserv, is_velocity, allow_inflow_on_outflow); } - else if (advection_type == "PLIC") - { -#if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) - // The flux volumes are not cut by embedded boundaries, so a level with cut - // cells is rejected as a whole, rather than only its cut boxes. - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), - "PLIC is not available with EB"); -#endif - // The volume fraction is one scalar, advected in conservative form. Its ghost - // cells carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs - // are not read here. The edge states are the liquid fractions of the flux - // volumes, which ComputeFluxes turns back into the geometric fluxes. - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, - "PLIC advects one volume fraction, not a velocity"); - PLIC::ComputeEdgeStateAndFluxes(bx, - AMREX_D_DECL(Array4{}, Array4{}, Array4{}), - AMREX_D_DECL(face_x,face_y,face_z), - q, - AMREX_D_DECL(u_mac,v_mac,w_mac), - geom, l_dt, "myc_height"); - } else { Abort("Unknown advection_type: "+advection_type+" (expected Godunov, MOL, BDS or PLIC)"); } } } + +#ifdef HYDRO_USE_PLIC + // advection_type "PLIC": the edge states of PLIC::ComputeEdgeStateAndFluxes, and its geometric + // fluxes when the fluxes are formed with u_mac. Returns whether it wrote the fluxes. Where shear + // folds a flux volume the edge state is held to [0,1] and edge state * u is not the liquid that + // crosses the face, so the fluxes are not formed from the edge states then. + bool PLICFluxes (Box const& bx, int ncomp, + Array4 const& q, + AMREX_D_DECL(Array4 const& flux_x, + Array4 const& flux_y, + Array4 const& flux_z), + AMREX_D_DECL(Array4 const& face_x, + Array4 const& face_y, + Array4 const& face_z), + bool knownFaceState, + AMREX_D_DECL(Array4 const& u_mac, + Array4 const& v_mac, + Array4 const& w_mac), + AMREX_D_DECL(Array4 const& u_flux, + Array4 const& v_flux, + Array4 const& w_flux), + Geometry const& geom, Real l_dt, int const* iconserv, + bool is_velocity, bool fluxes_are_area_weighted, + Array4 const& bc_arr) + { + // The volume fraction is one scalar, advected in conservative form. Its ghost cells + // carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs are not read. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, + "PLIC advects one volume fraction, not a velocity"); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!bc_arr, "bc_arr is only supported with (EB)Godunov"); + // iconserv is device memory + amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) noexcept + { + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(iconserv[0] == 1, "PLIC needs iconserv = 1"); + }); + if (knownFaceState) { return false; } + const bool flux_is_mac = AMREX_D_TERM(u_flux.dataPtr() == u_mac.dataPtr(), + && v_flux.dataPtr() == v_mac.dataPtr(), + && w_flux.dataPtr() == w_mac.dataPtr()); + PLIC::ComputeEdgeStateAndFluxes(bx, + AMREX_D_DECL(flux_is_mac ? flux_x : Array4{}, + flux_is_mac ? flux_y : Array4{}, + flux_is_mac ? flux_z : Array4{}), + AMREX_D_DECL(face_x,face_y,face_z), + q, + AMREX_D_DECL(u_mac,v_mac,w_mac), + geom, l_dt, "myc_height", fluxes_are_area_weighted); + return flux_is_mac; + } +#endif } // namespace #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -485,6 +512,29 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, bool regular = (flagfab.getType(amrex::grow(bx,3)) == FabType::regular); #endif + if (advection_type == "PLIC") { +#ifdef HYDRO_USE_PLIC +#if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) + // The flux volumes are not cut by embedded boundaries, so a level with cut cells is + // rejected as a whole, rather than only its cut boxes. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), "PLIC is not available with EB"); +#endif + if (PLICFluxes(bx, ncomp, q, + AMREX_D_DECL(flux_x,flux_y,flux_z), + AMREX_D_DECL(face_x,face_y,face_z), + knownFaceState, + AMREX_D_DECL(u_mac,v_mac,w_mac), + AMREX_D_DECL(u_flux,v_flux,w_flux), + geom, l_dt, iconserv, is_velocity, fluxes_are_area_weighted, bc_arr)) { + return; + } + knownFaceState = true; +#else + Abort("advection_type PLIC needs AMReX-Hydro built with PLIC (with GNUmake, add the PLIC " + "directory to the build)"); +#endif + } + // Compute edge state if needed if (!knownFaceState) { ComputeEdgeState(bx, ncomp, mfi, q, qnph, diff --git a/Utils/hydro_utils.H b/Utils/hydro_utils.H index 5a877c3b..95721cd7 100644 --- a/Utils/hydro_utils.H +++ b/Utils/hydro_utils.H @@ -66,12 +66,8 @@ void enforceInOutSolvability ( * BDS whenever any component is conservative. * * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and - * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, - * iconserv = 1, not a velocity) with the geometric fluxes of - * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs - * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which - * carry the boundary conditions in place of the BCRecs, and the face Courant - * number must be below 1. See the PLIC section of the documentation. + * PLIC are not available with EB. For what "PLIC" needs from the caller, see + * "Through HydroUtils" in the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -167,12 +163,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * BDS whenever any component is conservative. * * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and - * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, - * iconserv = 1, not a velocity) with the geometric fluxes of - * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs - * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which - * carry the boundary conditions in place of the BCRecs, and the face Courant - * number must be below 1. See the PLIC section of the documentation. + * PLIC are not available with EB. For what "PLIC" needs from the caller, see + * "Through HydroUtils" in the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -232,12 +224,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * BDS whenever any component is conservative. * * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and - * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, - * iconserv = 1, not a velocity) with the geometric fluxes of - * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs - * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which - * carry the boundary conditions in place of the BCRecs, and the face Courant - * number must be below 1. See the PLIC section of the documentation. + * PLIC are not available with EB. For what "PLIC" needs from the caller, see + * "Through HydroUtils" in the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -296,12 +284,8 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * BDS whenever any component is conservative. * * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and - * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, - * iconserv = 1, not a velocity) with the geometric fluxes of - * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs - * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which - * carry the boundary conditions in place of the BCRecs, and the face Courant - * number must be below 1. See the PLIC section of the documentation. + * PLIC are not available with EB. For what "PLIC" needs from the caller, see + * "Through HydroUtils" in the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -360,12 +344,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * BDS whenever any component is conservative. * * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and - * PLIC are not available with EB. "PLIC" advects one volume fraction (ncomp = 1, - * iconserv = 1, not a velocity) with the geometric fluxes of - * PLIC::ComputeEdgeStateAndFluxes and the myc_height reconstruction: q needs - * PLIC::nGrowF("myc_height") ghost cells, filled with PLIC::FillWallGhosts, which - * carry the boundary conditions in place of the BCRecs, and the face Courant - * number must be below 1. See the PLIC section of the documentation. + * PLIC are not available with EB. For what "PLIC" needs from the caller, see + * "Through HydroUtils" in the PLIC section of the documentation. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and From ff430ed4bfd513689e4c45871b1f598ff322043d Mon Sep 17 00:00:00 2001 From: ThomasHowarth Date: Wed, 18 Nov 2026 11:16:45 +0000 Subject: [PATCH 3/7] PLIC: simplify advection_type "PLIC" in HydroUtils - The PLIC branch inline in ComputeFluxesOnBoxFromState, without a helper of 21 parameters and a return value with two meanings. - The device check of iconserv = 1 only with AMREX_DEBUG, rather than a kernel launch per box in every build. - HYDRO_USE_PLIC a PRIVATE definition in CMake: only the library reads it. - The flux kernel tests the flux Array4 itself rather than a copy of it. - PLIC.rst says the geometric fluxes need u_flux to be the same Array4 as u_mac (or the overloads without u_flux). - Tests/PLIC: iconserv as a Gpu::DeviceScalar, a synchronous copy of the BCRecs, and the two flux paths as if/else. Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 7 +- PLIC/CMakeLists.txt | 2 +- Tests/PLIC/test_advect.cpp | 29 ++++---- Utils/hydro_compute_edgestate_and_flux.cpp | 84 +++++++--------------- 4 files changed, 45 insertions(+), 77 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index 8eab53c1..6c29dc22 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -135,9 +135,10 @@ Through ``HydroUtils`` A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState`` can advect :math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` and ``is_velocity = false``. It uses the ``myc_height`` reconstruction, and gives the edge states and -the fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit. With a ``u_flux`` other than the face -velocity (a corrective velocity), or with ``knownFaceState``, the fluxes are formed from the edge -states as for any other scalar. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds +the fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit, when the fluxes are formed with the +face velocity itself (the overloads without ``u_flux``, or ``u_flux`` the same ``Array4`` as +``u_mac``). With a corrective ``u_flux``, or with ``knownFaceState``, the fluxes are formed from the +edge states as for any other scalar. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds nothing, as :math:`F` is advected in conservative form. :math:`F` needs ``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the boundary conditions in place of the ``BCRec`` arguments; ``divu``, the forcing ``fq``, diff --git a/PLIC/CMakeLists.txt b/PLIC/CMakeLists.txt index e884a342..992fef12 100644 --- a/PLIC/CMakeLists.txt +++ b/PLIC/CMakeLists.txt @@ -17,4 +17,4 @@ target_sources( ) # HydroUtils::ComputeFluxesOnBoxFromState accepts advection_type "PLIC" when PLIC is built -target_compile_definitions(amrex_hydro PUBLIC HYDRO_USE_PLIC) +target_compile_definitions(amrex_hydro PRIVATE HYDRO_USE_PLIC) diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index 43cfbd88..eb765960 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -158,11 +159,7 @@ class AdvectTest final : public AmrCore } m_bcrec_d.resize(1); - m_iconserv_d.resize(1); - Gpu::copyAsync(Gpu::hostToDevice, m_bcrec.begin(), m_bcrec.end(), m_bcrec_d.begin()); - const int iconserv = 1; - Gpu::copyAsync(Gpu::hostToDevice, &iconserv, &iconserv + 1, m_iconserv_d.begin()); - Gpu::streamSynchronize(); + Gpu::copy(Gpu::hostToDevice, m_bcrec.begin(), m_bcrec.end(), m_bcrec_d.begin()); m_F.resize(max_level + 1); m_umac0.resize(max_level + 1); @@ -402,18 +399,18 @@ class AdvectTest final : public AmrCore AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), umac[lev][2].const_array(mfi)), Array4{}, Array4{}, Geom(lev), dt, - m_bcrec, m_bcrec_d.data(), m_iconserv_d.data(), + m_bcrec, m_bcrec_d.data(), m_iconserv_d.dataPtr(), false, false, false, false, "PLIC"); - continue; + } else { + PLIC::ComputeEdgeStateAndFluxes( + mfi.validbox(), + AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), + AMREX_D_DECL(face[lev][0].array(mfi), face[lev][1].array(mfi), face[lev][2].array(mfi)), + m_F[lev].const_array(mfi), + AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), + umac[lev][2].const_array(mfi)), + Geom(lev), dt, m_reconstruction, false, false); } - PLIC::ComputeEdgeStateAndFluxes( - mfi.validbox(), - AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), - AMREX_D_DECL(face[lev][0].array(mfi), face[lev][1].array(mfi), face[lev][2].array(mfi)), - m_F[lev].const_array(mfi), - AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), - umac[lev][2].const_array(mfi)), - Geom(lev), dt, m_reconstruction, false, false); } CheckEdgeStates(flux[lev], face[lev], umac[lev]); if (m_use_hydroutils) { CompareWithPLIC(flux[lev], face[lev], umac[lev], lev, dt); } @@ -545,7 +542,7 @@ class AdvectTest final : public AmrCore Vector> m_umac0; Vector m_bcrec; Gpu::DeviceVector m_bcrec_d; - Gpu::DeviceVector m_iconserv_d; + Gpu::DeviceScalar m_iconserv_d{1}; bool m_use_hydroutils = false; int m_regrid_int = 4; int m_refine_band = 2; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index b9f74152..ffb27950 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -185,55 +185,6 @@ namespace { } } -#ifdef HYDRO_USE_PLIC - // advection_type "PLIC": the edge states of PLIC::ComputeEdgeStateAndFluxes, and its geometric - // fluxes when the fluxes are formed with u_mac. Returns whether it wrote the fluxes. Where shear - // folds a flux volume the edge state is held to [0,1] and edge state * u is not the liquid that - // crosses the face, so the fluxes are not formed from the edge states then. - bool PLICFluxes (Box const& bx, int ncomp, - Array4 const& q, - AMREX_D_DECL(Array4 const& flux_x, - Array4 const& flux_y, - Array4 const& flux_z), - AMREX_D_DECL(Array4 const& face_x, - Array4 const& face_y, - Array4 const& face_z), - bool knownFaceState, - AMREX_D_DECL(Array4 const& u_mac, - Array4 const& v_mac, - Array4 const& w_mac), - AMREX_D_DECL(Array4 const& u_flux, - Array4 const& v_flux, - Array4 const& w_flux), - Geometry const& geom, Real l_dt, int const* iconserv, - bool is_velocity, bool fluxes_are_area_weighted, - Array4 const& bc_arr) - { - // The volume fraction is one scalar, advected in conservative form. Its ghost cells - // carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs are not read. - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, - "PLIC advects one volume fraction, not a velocity"); - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!bc_arr, "bc_arr is only supported with (EB)Godunov"); - // iconserv is device memory - amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) noexcept - { - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(iconserv[0] == 1, "PLIC needs iconserv = 1"); - }); - if (knownFaceState) { return false; } - const bool flux_is_mac = AMREX_D_TERM(u_flux.dataPtr() == u_mac.dataPtr(), - && v_flux.dataPtr() == v_mac.dataPtr(), - && w_flux.dataPtr() == w_mac.dataPtr()); - PLIC::ComputeEdgeStateAndFluxes(bx, - AMREX_D_DECL(flux_is_mac ? flux_x : Array4{}, - flux_is_mac ? flux_y : Array4{}, - flux_is_mac ? flux_z : Array4{}), - AMREX_D_DECL(face_x,face_y,face_z), - q, - AMREX_D_DECL(u_mac,v_mac,w_mac), - geom, l_dt, "myc_height", fluxes_are_area_weighted); - return flux_is_mac; - } -#endif } // namespace #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -519,14 +470,33 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, // rejected as a whole, rather than only its cut boxes. AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), "PLIC is not available with EB"); #endif - if (PLICFluxes(bx, ncomp, q, - AMREX_D_DECL(flux_x,flux_y,flux_z), - AMREX_D_DECL(face_x,face_y,face_z), - knownFaceState, - AMREX_D_DECL(u_mac,v_mac,w_mac), - AMREX_D_DECL(u_flux,v_flux,w_flux), - geom, l_dt, iconserv, is_velocity, fluxes_are_area_weighted, bc_arr)) { - return; + // The volume fraction is one scalar, advected in conservative form. Its ghost cells + // carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs are not read. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, + "PLIC advects one volume fraction, not a velocity"); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!bc_arr, "bc_arr is only supported with (EB)Godunov"); +#ifdef AMREX_DEBUG + amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) noexcept // iconserv is device memory + { + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(iconserv[0] == 1, "PLIC needs iconserv = 1"); + }); +#endif + if (!knownFaceState) { + // PLIC's geometric fluxes, where the fluxes are formed with u_mac itself (the same + // Array4s). Where shear folds a flux volume the edge state is held to [0,1], and + // edge state * u is not the liquid that crosses the face. + const bool flux_is_mac = AMREX_D_TERM(u_flux.dataPtr() == u_mac.dataPtr(), + && v_flux.dataPtr() == v_mac.dataPtr(), + && w_flux.dataPtr() == w_mac.dataPtr()); + PLIC::ComputeEdgeStateAndFluxes(bx, + AMREX_D_DECL(flux_is_mac ? flux_x : Array4{}, + flux_is_mac ? flux_y : Array4{}, + flux_is_mac ? flux_z : Array4{}), + AMREX_D_DECL(face_x,face_y,face_z), + q, + AMREX_D_DECL(u_mac,v_mac,w_mac), + geom, l_dt, "myc_height", fluxes_are_area_weighted); + if (flux_is_mac) { return; } } knownFaceState = true; #else From d0b11570b510ba1ecd421798a2e1f33ca99b98f3 Mon Sep 17 00:00:00 2001 From: ThomasHowarth Date: Fri, 20 Nov 2026 15:39:19 +0000 Subject: [PATCH 4/7] PLIC: second review fixes to advection_type "PLIC" in HydroUtils - Check iconserv on the host in every build: 1, or -1 with knownFaceState. The debug-only device check rejected -1 and accepted 0 in release. - Keep PLIC's geometric fluxes whenever u_flux holds the values of u_mac (compared in one reduction when the arrays differ), not only when it is the same Array4. - Pass the real flux arrays to PLIC::ComputeEdgeStateAndFluxes, and revert its empty-flux mode, so the public API again aborts on a missing flux. - ComputeConvectiveTerm aborts on "PLIC" in builds without PLIC, as ComputeFluxesOnBoxFromState does. - Docs: "PLIC" in the bc_arr notes; the HydroUtils section of PLIC.rst as a list of the flux cases, and a note on rebuilding after adding PLIC. - Tests/PLIC: with test.use_hydroutils, also check a copy of u_mac, area-weighted fluxes, a corrective u_flux and knownFaceState with iconserv 1 and -1, all to the bit; share the flux calls between the step and the comparison. Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 31 ++-- PLIC/hydro_plic.H | 3 +- PLIC/hydro_plic_owkes_desjardins.cpp | 5 +- Tests/PLIC/test_advect.cpp | 179 ++++++++++++++------- Utils/hydro_compute_edgestate_and_flux.cpp | 62 +++++-- Utils/hydro_utils.H | 25 +-- Utils/hydro_utils.cpp | 5 + 7 files changed, 213 insertions(+), 97 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index 6c29dc22..4ceb92f7 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -134,17 +134,26 @@ Through ``HydroUtils`` A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState`` can advect :math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` -and ``is_velocity = false``. It uses the ``myc_height`` reconstruction, and gives the edge states and -the fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit, when the fluxes are formed with the -face velocity itself (the overloads without ``u_flux``, or ``u_flux`` the same ``Array4`` as -``u_mac``). With a corrective ``u_flux``, or with ``knownFaceState``, the fluxes are formed from the -edge states as for any other scalar. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds -nothing, as :math:`F` is advected in conservative form. :math:`F` needs -``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the -boundary conditions in place of the ``BCRec`` arguments; ``divu``, the forcing ``fq``, -``limiter_type`` and ``allow_inflow_on_outflow`` are not read. ``"PLIC"`` aborts with embedded -boundaries, and ``HydroUtils::ExtrapVelToFaces`` does not accept it. With GNUmake, add the ``PLIC`` -directory to the build (it defines ``HYDRO_USE_PLIC``); without it ``"PLIC"`` aborts. +and ``is_velocity = false``. It uses the ``myc_height`` reconstruction: + +- with ``u_flux`` equal to ``u_mac`` (or the overloads without ``u_flux``), it gives the edge states + and the geometric fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit; +- with a corrective ``u_flux``, the fluxes are PLIC's edge states times ``u_flux``; +- with ``knownFaceState``, the face arrays are the edge states (``iconserv = 1``) or the fluxes + (``iconserv = -1``). Any other ``iconserv`` aborts. + +:math:`F` needs ``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, +which carry the boundary conditions in place of the ``BCRec`` arguments; ``divu``, the forcing +``fq``, ``limiter_type`` and ``allow_inflow_on_outflow`` are not read. +``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds nothing, as :math:`F` is advected +in conservative form. ``"PLIC"`` aborts with embedded boundaries, and +``HydroUtils::ExtrapVelToFaces`` does not accept it. + +.. note:: + + With GNUmake, add the ``PLIC`` directory to the build, which defines ``HYDRO_USE_PLIC``; without + it ``"PLIC"`` aborts. The name of the build directory does not depend on it, so after adding + ``PLIC`` to an existing build, run ``make clean`` to rebuild ``Utils``. .. note:: diff --git a/PLIC/hydro_plic.H b/PLIC/hydro_plic.H index feb8d4e0..5e0fb7f3 100644 --- a/PLIC/hydro_plic.H +++ b/PLIC/hydro_plic.H @@ -87,8 +87,7 @@ void FitPlanes (amrex::MultiFab& plane, amrex::MultiFab const& F, int ngrow, * face of bx is written, so in a tiled MFIter loop two tiles write the faces * between them (the same values, as with Godunov::ComputeEdgeState): with * OpenMP, give each tile its own face arrays, or loop without tiling. - * \param flux_x output flux on the x faces of bx (likewise y, z), or an empty Array4 for the - * edge states only + * \param flux_x output flux on the x faces of bx (likewise y, z) * \param face_x output edge state on the x faces of bx (likewise y, z) * \param F volume fraction at t^n, with nGrowF(reconstruction) filled ghost cells * \param umac face velocity at t^{n+1/2}, discretely divergence-free, with one filled ghost diff --git a/PLIC/hydro_plic_owkes_desjardins.cpp b/PLIC/hydro_plic_owkes_desjardins.cpp index 2ac1944e..2b413378 100644 --- a/PLIC/hydro_plic_owkes_desjardins.cpp +++ b/PLIC/hydro_plic_owkes_desjardins.cpp @@ -108,7 +108,7 @@ struct FaceFluxes } else { fc(iv) = upwind_fraction(F, iv, dir, um(iv)); } - if (fl) { fl(iv) = to_flux * liquid; } + fl(iv) = to_flux * liquid; } }; @@ -248,8 +248,7 @@ ComputeEdgeStateAndFluxes (amrex::Box const& bx, amrex::ReduceData reduce_data(reduce_op); for (int d = 0; d < AMREX_SPACEDIM; ++d) { const amrex::Box faces = amrex::surroundingNodes(bx, d); - // An empty flux array asks for the edge states only - AMREX_ALWAYS_ASSERT_WITH_MESSAGE((!flux[d] || detail::FaceBox(flux[d], d).contains(faces)) && + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(detail::FaceBox(flux[d], d).contains(faces) && detail::FaceBox(face[d], d).contains(faces), "PLIC: the fluxes and edge states need the faces of bx"); // The nodes of bx read the faces up to one beyond them (see node_displacement) diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index eb765960..143dc780 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -158,9 +157,6 @@ class AdvectTest final : public AmrCore m_bcrec[0].setHi(d, periodic ? BCType::int_dir : BCType::foextrap); } - m_bcrec_d.resize(1); - Gpu::copy(Gpu::hostToDevice, m_bcrec.begin(), m_bcrec.end(), m_bcrec_d.begin()); - m_F.resize(max_level + 1); m_umac0.resize(max_level + 1); } @@ -305,41 +301,137 @@ class AdvectTest final : public AmrCore return dt; } - // Through HydroUtils, the fluxes and edge states of one step on one level, on this rank, are - // those of PLIC::ComputeEdgeStateAndFluxes, to the bit - void CompareWithPLIC (Array const& flux, Array const& face, - Array const& umac, int lev, Real dt) + using Faces = Array; + + // The fluxes and edge states of F on level lev, with PLIC::ComputeEdgeStateAndFluxes, + // after the PLIC::CheckCourantNumber of the step + void PLICFluxes (int lev, Faces& fl, Faces& fc, Faces const& umac, Real dt, bool area_weighted) { - Array flux_ref; - Array face_ref; - for (int d = 0; d < AMREX_SPACEDIM; ++d) { - flux_ref[d].define(flux[d].boxArray(), flux[d].DistributionMap(), 1, 0); - face_ref[d].define(face[d].boxArray(), face[d].DistributionMap(), 1, 0); - } #ifdef AMREX_USE_OMP #pragma omp parallel if (Gpu::notInLaunchRegion()) #endif for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { PLIC::ComputeEdgeStateAndFluxes( mfi.validbox(), - AMREX_D_DECL(flux_ref[0].array(mfi), flux_ref[1].array(mfi), flux_ref[2].array(mfi)), - AMREX_D_DECL(face_ref[0].array(mfi), face_ref[1].array(mfi), face_ref[2].array(mfi)), + AMREX_D_DECL(fl[0].array(mfi), fl[1].array(mfi), fl[2].array(mfi)), + AMREX_D_DECL(fc[0].array(mfi), fc[1].array(mfi), fc[2].array(mfi)), m_F[lev].const_array(mfi), AMREX_D_DECL(umac[0].const_array(mfi), umac[1].const_array(mfi), umac[2].const_array(mfi)), - Geom(lev), dt, m_reconstruction); + Geom(lev), dt, m_reconstruction, area_weighted, false); } + } + + // The same through HydroUtils, as for any other conservative scalar, with the boundary conditions + // in the ghost cells of F (the BCRecs are not read): with the overload without u_flux if uflux is + // null, and with knownFaceState fc holds the given face arrays + void HydroUtilsFluxes (int lev, Faces& fl, Faces& fc, Faces const& umac, Faces const* uflux, bool known, + int const* iconserv, Real dt, bool area_weighted) + { +#ifdef AMREX_USE_OMP +#pragma omp parallel if (Gpu::notInLaunchRegion()) +#endif + for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { + auto const& F = m_F[lev].const_array(mfi); + AMREX_D_TERM(auto const& flx = fl[0].array(mfi);, auto const& fly = fl[1].array(mfi);, + auto const& flz = fl[2].array(mfi);); + AMREX_D_TERM(auto const& fcx = fc[0].array(mfi);, auto const& fcy = fc[1].array(mfi);, + auto const& fcz = fc[2].array(mfi);); + AMREX_D_TERM(auto const& um = umac[0].const_array(mfi);, auto const& vm = umac[1].const_array(mfi);, + auto const& wm = umac[2].const_array(mfi);); + if (uflux) { + HydroUtils::ComputeFluxesOnBoxFromState( + mfi.validbox(), 1, mfi, F, AMREX_D_DECL(flx, fly, flz), AMREX_D_DECL(fcx, fcy, fcz), known, + AMREX_D_DECL(um, vm, wm), + AMREX_D_DECL((*uflux)[0].const_array(mfi), (*uflux)[1].const_array(mfi), + (*uflux)[2].const_array(mfi)), + Array4{}, Array4{}, Geom(lev), dt, m_bcrec, nullptr, iconserv, + false, false, false, area_weighted, "PLIC"); + } else { + HydroUtils::ComputeFluxesOnBoxFromState( + mfi.validbox(), 1, mfi, F, AMREX_D_DECL(flx, fly, flz), AMREX_D_DECL(fcx, fcy, fcz), known, + AMREX_D_DECL(um, vm, wm), + Array4{}, Array4{}, Geom(lev), dt, m_bcrec, nullptr, iconserv, + false, false, false, area_weighted, "PLIC"); + } + } + } + + // Through HydroUtils, the fluxes and edge states of one step on one level, on this rank, to the bit: + // - the step itself (u_flux the same Array4s as u_mac), a copy of u_mac as u_flux, and both with + // area-weighted fluxes: those of PLIC::ComputeEdgeStateAndFluxes; + // - a corrective u_flux: PLIC's edge states, times u_flux; + // - knownFaceState with iconserv = 1: the given edge states, times u_mac; + // - knownFaceState with iconserv = -1: the face arrays (PLIC's fluxes), copied. + void CompareWithPLIC (Faces const& flux, Faces const& face, Faces const& umac, int lev, Real dt) + { + // On the faces of the boxes, or as a copy of src, with its ghost faces + const auto define = [&] (Faces& mf, Faces const* src = nullptr) { + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + MultiFab const& like = src ? (*src)[d] : flux[d]; + mf[d].define(like.boxArray(), like.DistributionMap(), 1, like.nGrow()); + if (src) { MultiFab::Copy(mf[d], (*src)[d], 0, 0, 1, like.nGrow()); } + } + }; + const auto compare = [&] (Faces const& fl, Faces const& fc, Faces const& fl_ref, Faces const& fc_ref) { + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + auto const& a = fl[d].const_arrays(); + auto const& b = fc[d].const_arrays(); + auto const& a_ref = fl_ref[d].const_arrays(); + auto const& b_ref = fc_ref[d].const_arrays(); + const Real diff = ParReduce(TypeList{}, TypeList{}, fl[d], IntVect(0), + [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept -> GpuTuple { + return {nan_as_max(max(std::abs(a[n](i, j, k) - a_ref[n](i, j, k)), + std::abs(b[n](i, j, k) - b_ref[n](i, j, k))))}; + }); + m_hydroutils_diff = max(m_hydroutils_diff, diff); + } + }; + + // PLIC's own, without and with area weighting + Faces flux_ref, face_ref, flux_ref_aw, face_ref_aw; + for (Faces* mf : {&flux_ref, &face_ref, &flux_ref_aw, &face_ref_aw}) { define(*mf); } + PLICFluxes(lev, flux_ref, face_ref, umac, dt, false); + PLICFluxes(lev, flux_ref_aw, face_ref_aw, umac, dt, true); + + // The step itself + compare(flux, face, flux_ref, face_ref); + + // A copy of u_mac, without and with area weighting + Faces umac_copy, fl, fc; + define(umac_copy, &umac); + define(fl); + define(fc); + HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, false); + compare(fl, fc, flux_ref, face_ref); + HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, true); + compare(fl, fc, flux_ref_aw, face_ref_aw); + HydroUtilsFluxes(lev, fl, fc, umac, &umac, false, m_iconserv_d.dataPtr(), dt, true); + compare(fl, fc, flux_ref_aw, face_ref_aw); + + // A corrective u_flux + Faces ucorr, flux_corr; + define(ucorr, &umac); + define(flux_corr, &face_ref); for (int d = 0; d < AMREX_SPACEDIM; ++d) { - auto const& fl = flux[d].const_arrays(); - auto const& fc = face[d].const_arrays(); - auto const& fl_ref = flux_ref[d].const_arrays(); - auto const& fc_ref = face_ref[d].const_arrays(); - const Real diff = ParReduce(TypeList{}, TypeList{}, flux[d], IntVect(0), - [=] AMREX_GPU_DEVICE (int b, int i, int j, int k) noexcept -> GpuTuple { - return {nan_as_max(max(std::abs(fl[b](i, j, k) - fl_ref[b](i, j, k)), - std::abs(fc[b](i, j, k) - fc_ref[b](i, j, k))))}; - }); - m_hydroutils_diff = max(m_hydroutils_diff, diff); + ucorr[d].mult(0.5_rt); + MultiFab::Multiply(flux_corr[d], ucorr[d], 0, 0, 1, 0); + } + HydroUtilsFluxes(lev, fl, fc, umac, &ucorr, false, m_iconserv_d.dataPtr(), dt, false); + compare(fl, fc, flux_corr, face_ref); + + // knownFaceState: the edge states (iconserv = 1) or the fluxes (iconserv = -1) + Faces flux_known; + define(flux_known, &face_ref); + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + MultiFab::Multiply(flux_known[d], umac[d], 0, 0, 1, 0); } + define(fc, &face_ref); + HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, m_iconserv_d.dataPtr(), dt, false); + compare(fl, fc, flux_known, face_ref); + const Gpu::DeviceScalar iconserv_flux{-1}; + define(fc, &flux_ref); + HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, iconserv_flux.dataPtr(), dt, false); + compare(fl, fc, flux_ref, flux_ref); } // The edge states of one step on one level, on this rank: on every face with a flow, the liquid @@ -384,33 +476,11 @@ class AdvectTest final : public AmrCore umac[lev] = Velocity(lev, time + 0.5_rt * dt); // The Courant numbers of the level in one wait for the GPU, not one per box PLIC::CheckCourantNumber(AMREX_D_DECL(umac[lev][0], umac[lev][1], umac[lev][2]), Geom(lev), dt); -#ifdef AMREX_USE_OMP -#pragma omp parallel if (Gpu::notInLaunchRegion()) -#endif - for (MFIter mfi(m_F[lev]); mfi.isValid(); ++mfi) { - if (m_use_hydroutils) { - // As for any other conservative scalar, with the boundary conditions in the - // ghost cells of F - HydroUtils::ComputeFluxesOnBoxFromState( - mfi.validbox(), 1, mfi, m_F[lev].const_array(mfi), - AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), - AMREX_D_DECL(face[lev][0].array(mfi), face[lev][1].array(mfi), face[lev][2].array(mfi)), - false, - AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), - umac[lev][2].const_array(mfi)), - Array4{}, Array4{}, Geom(lev), dt, - m_bcrec, m_bcrec_d.data(), m_iconserv_d.dataPtr(), - false, false, false, false, "PLIC"); - } else { - PLIC::ComputeEdgeStateAndFluxes( - mfi.validbox(), - AMREX_D_DECL(flux[lev][0].array(mfi), flux[lev][1].array(mfi), flux[lev][2].array(mfi)), - AMREX_D_DECL(face[lev][0].array(mfi), face[lev][1].array(mfi), face[lev][2].array(mfi)), - m_F[lev].const_array(mfi), - AMREX_D_DECL(umac[lev][0].const_array(mfi), umac[lev][1].const_array(mfi), - umac[lev][2].const_array(mfi)), - Geom(lev), dt, m_reconstruction, false, false); - } + if (m_use_hydroutils) { + HydroUtilsFluxes(lev, flux[lev], face[lev], umac[lev], nullptr, false, m_iconserv_d.dataPtr(), dt, + false); + } else { + PLICFluxes(lev, flux[lev], face[lev], umac[lev], dt, false); } CheckEdgeStates(flux[lev], face[lev], umac[lev]); if (m_use_hydroutils) { CompareWithPLIC(flux[lev], face[lev], umac[lev], lev, dt); } @@ -541,7 +611,6 @@ class AdvectTest final : public AmrCore // The face velocity at t = 0 on each level, with a ghost layer (see Velocity) Vector> m_umac0; Vector m_bcrec; - Gpu::DeviceVector m_bcrec_d; Gpu::DeviceScalar m_iconserv_d{1}; bool m_use_hydroutils = false; int m_regrid_int = 4; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index ffb27950..7c1f82fb 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -7,6 +7,7 @@ #include #ifdef HYDRO_USE_PLIC #include +#include #endif #include @@ -185,6 +186,37 @@ namespace { } } +#ifdef HYDRO_USE_PLIC + // Whether u_flux holds the values of u_mac on the faces of bx (the same Array4s, or a copy) + bool SameFaceVelocity (Box const& bx, + AMREX_D_DECL(Array4 const& u_mac, + Array4 const& v_mac, + Array4 const& w_mac), + AMREX_D_DECL(Array4 const& u_flux, + Array4 const& v_flux, + Array4 const& w_flux)) + { + const Array, AMREX_SPACEDIM> mac{AMREX_D_DECL(u_mac, v_mac, w_mac)}; + const Array, AMREX_SPACEDIM> flx{AMREX_D_DECL(u_flux, v_flux, w_flux)}; + // One reduction over the directions whose arrays differ, so one sync + ReduceOps reduce_op; + ReduceData reduce_data(reduce_op); + bool compared = false; + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + if (flx[d].dataPtr() == mac[d].dataPtr()) { continue; } + const Array4 um = mac[d]; + const Array4 uf = flx[d]; + reduce_op.eval(amrex::surroundingNodes(bx, d), reduce_data, + [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept -> GpuTuple + { + return { static_cast(!(uf(i,j,k) == um(i,j,k))) }; + }); + compared = true; + } + return !compared || amrex::get<0>(reduce_data.value(reduce_op)) == 0; + } +#endif + } // namespace #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -475,28 +507,26 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, "PLIC advects one volume fraction, not a velocity"); AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!bc_arr, "bc_arr is only supported with (EB)Godunov"); -#ifdef AMREX_DEBUG - amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) noexcept // iconserv is device memory - { - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(iconserv[0] == 1, "PLIC needs iconserv = 1"); - }); -#endif + // Conservative (1), or the face array already holding the fluxes (-1, with + // knownFaceState). Convective (0) would be silently advected in conservative form, as + // ComputeConvectiveTerm adds nothing for "PLIC". + int h_iconserv = 0; + Gpu::dtoh_memcpy(&h_iconserv, iconserv, sizeof(int)); // iconserv is device memory + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(h_iconserv == 1 || (h_iconserv == -1 && knownFaceState), + "PLIC needs iconserv = 1, or -1 with knownFaceState"); if (!knownFaceState) { - // PLIC's geometric fluxes, where the fluxes are formed with u_mac itself (the same - // Array4s). Where shear folds a flux volume the edge state is held to [0,1], and - // edge state * u is not the liquid that crosses the face. - const bool flux_is_mac = AMREX_D_TERM(u_flux.dataPtr() == u_mac.dataPtr(), - && v_flux.dataPtr() == v_mac.dataPtr(), - && w_flux.dataPtr() == w_mac.dataPtr()); PLIC::ComputeEdgeStateAndFluxes(bx, - AMREX_D_DECL(flux_is_mac ? flux_x : Array4{}, - flux_is_mac ? flux_y : Array4{}, - flux_is_mac ? flux_z : Array4{}), + AMREX_D_DECL(flux_x,flux_y,flux_z), AMREX_D_DECL(face_x,face_y,face_z), q, AMREX_D_DECL(u_mac,v_mac,w_mac), geom, l_dt, "myc_height", fluxes_are_area_weighted); - if (flux_is_mac) { return; } + // Where shear folds a flux volume the edge state is held to [0,1], and edge state * u + // is not the liquid that crosses the face: keep PLIC's geometric fluxes when they are + // formed with u_mac. A corrective u_flux forms its fluxes from the edge states below. + if (SameFaceVelocity(bx, AMREX_D_DECL(u_mac,v_mac,w_mac), AMREX_D_DECL(u_flux,v_flux,w_flux))) { + return; + } } knownFaceState = true; #else diff --git a/Utils/hydro_utils.H b/Utils/hydro_utils.H index 95721cd7..2877291d 100644 --- a/Utils/hydro_utils.H +++ b/Utils/hydro_utils.H @@ -76,8 +76,9 @@ void enforceInOutSolvability ( * regular box and a cut box is computed the same way from both sides. * * \note bc_arr, the position-dependent boundary conditions, is only supported - * with (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those - * kernels would silently use h_bcrec/d_bcrec instead. + * with (EB)Godunov; passing it with "MOL", "BDS" or "PLIC" aborts, because + * those kernels would silently use h_bcrec/d_bcrec instead (with "PLIC" + * the ghost cells of q carry the boundary conditions). */ void ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mfi, @@ -173,8 +174,9 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * regular box and a cut box is computed the same way from both sides. * * \note bc_arr, the position-dependent boundary conditions, is only supported - * with (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those - * kernels would silently use h_bcrec/d_bcrec instead. + * with (EB)Godunov; passing it with "MOL", "BDS" or "PLIC" aborts, because + * those kernels would silently use h_bcrec/d_bcrec instead (with "PLIC" + * the ghost cells of q carry the boundary conditions). */ void ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mfi, @@ -234,8 +236,9 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * regular box and a cut box is computed the same way from both sides. * * \note bc_arr, the position-dependent boundary conditions, is only supported - * with (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those - * kernels would silently use h_bcrec/d_bcrec instead. + * with (EB)Godunov; passing it with "MOL", "BDS" or "PLIC" aborts, because + * those kernels would silently use h_bcrec/d_bcrec instead (with "PLIC" + * the ghost cells of q carry the boundary conditions). */ void ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi, @@ -294,8 +297,9 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * regular box and a cut box is computed the same way from both sides. * * \note bc_arr, the position-dependent boundary conditions, is only supported - * with (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those - * kernels would silently use h_bcrec/d_bcrec instead. + * with (EB)Godunov; passing it with "MOL", "BDS" or "PLIC" aborts, because + * those kernels would silently use h_bcrec/d_bcrec instead (with "PLIC" + * the ghost cells of q carry the boundary conditions). */ #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) void @@ -354,8 +358,9 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * regular box and a cut box is computed the same way from both sides. * * \note bc_arr, the position-dependent boundary conditions, is only supported - * with (EB)Godunov; passing it with "MOL" or "BDS" aborts, because those - * kernels would silently use h_bcrec/d_bcrec instead. + * with (EB)Godunov; passing it with "MOL", "BDS" or "PLIC" aborts, because + * those kernels would silently use h_bcrec/d_bcrec instead (with "PLIC" + * the ghost cells of q carry the boundary conditions). */ void ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi, diff --git a/Utils/hydro_utils.cpp b/Utils/hydro_utils.cpp index 5f64ab33..35213d4a 100644 --- a/Utils/hydro_utils.cpp +++ b/Utils/hydro_utils.cpp @@ -308,7 +308,12 @@ HydroUtils::ComputeConvectiveTerm(Box const& bx, int num_comp, } else if (advection_type == "PLIC") { +#ifdef HYDRO_USE_PLIC // The volume fraction is advected in conservative form only: nothing to add +#else + amrex::Abort("advection_type PLIC needs AMReX-Hydro built with PLIC (with GNUmake, add the PLIC " + "directory to the build)"); +#endif } else { amrex::Abort("HydroUtils::ComputeConvectiveTerm: unknown advection_type " + advection_type From cec4da55657ea0af10605b94a59b9c048c0d1e3f Mon Sep 17 00:00:00 2001 From: Thomas Howarth Date: Tue, 24 Nov 2026 17:59:37 +0000 Subject: [PATCH 5/7] PLIC: third review fixes to advection_type "PLIC" in HydroUtils - Choose between PLIC's geometric flux (u_flux equal to u_mac) and edge state * u_flux face by face, not box by box: boxes sharing a face could write different fluxes on it when u_flux differs on part of a level. This also drops a reduction, and its wait for the GPU, per box. - Abort in R-Z with knownFaceState too. - Docs: the ghost cells "PLIC" needs, the arguments it does not read, its waits for the GPU per box, and the note on the finest level back under Advection. - Tests/PLIC: a u_flux that differs on part of the level, arrays reset before each call, no device lambda inside a lambda, the comparison every test.hydroutils_int steps (default 10), and inputs.2d.wall_vortex through HydroUtils. Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 32 +++--- Tests/PLIC/README.md | 5 +- Tests/PLIC/inputs.2d.wall_vortex | 2 + Tests/PLIC/test_advect.cpp | 125 +++++++++++++++------ Utils/hydro_compute_edgestate_and_flux.cpp | 60 +++++----- Utils/hydro_utils.H | 5 + 6 files changed, 151 insertions(+), 78 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index 4ceb92f7..b4ce701f 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -125,9 +125,15 @@ The face velocities must be at :math:`t^{n+1/2}`, discretely divergence-free and shared by boxes (as after a MAC projection), with one filled ghost layer, and the face Courant number below 1. That is checked in every build, box by box. The check waits for the GPU, so that the boxes of the loop do not overlap: on GPUs, check the level once with ``PLIC::CheckCourantNumber`` before -the loop and pass ``check_courant = false``. No ghost face beyond a domain face that is not periodic is read, whatever its boundary -condition (an inflow profile in those ghost faces is not seen): the faces on the domain boundary stand -in for them. +the loop and pass ``check_courant = false``. No ghost face beyond a domain face that is not periodic +is read, whatever its boundary condition (an inflow profile in those ghost faces is not seen): the +faces on the domain boundary stand in for them. + +.. note:: + + Keep the interface on the finest level, by tagging the cells for which ``PLIC::interface_band`` + holds: flux volumes do not tile across coarse/fine boundaries, which must stay in pure cells. With + subcycling, the reflux corrections then fall in pure cells too. Through ``HydroUtils`` ~~~~~~~~~~~~~~~~~~~~~~ @@ -136,15 +142,19 @@ A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState :math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` and ``is_velocity = false``. It uses the ``myc_height`` reconstruction: -- with ``u_flux`` equal to ``u_mac`` (or the overloads without ``u_flux``), it gives the edge states - and the geometric fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit; -- with a corrective ``u_flux``, the fluxes are PLIC's edge states times ``u_flux``; +- on the faces where ``u_flux`` equals ``u_mac`` (every face, with the overloads without ``u_flux``), + it gives the edge states and the geometric fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the + bit; +- on the faces where a corrective ``u_flux`` differs, the fluxes are PLIC's edge states times + ``u_flux``. The choice is made face by face, so the boxes sharing a face get the same flux; - with ``knownFaceState``, the face arrays are the edge states (``iconserv = 1``) or the fluxes (``iconserv = -1``). Any other ``iconserv`` aborts. :math:`F` needs ``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, -which carry the boundary conditions in place of the ``BCRec`` arguments; ``divu``, the forcing -``fq``, ``limiter_type`` and ``allow_inflow_on_outflow`` are not read. +which carry the boundary conditions in place of the ``BCRec`` arguments; ``qnph``, ``divu``, the +forcing ``fq``, the ``godunov_*`` flags, ``limiter_type`` and ``allow_inflow_on_outflow`` are not +read. On GPUs each box waits for the copy of ``iconserv`` to the host and for its Courant check; +where that matters, call ``PLIC::ComputeEdgeStateAndFluxes`` with ``check_courant = false`` instead. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds nothing, as :math:`F` is advected in conservative form. ``"PLIC"`` aborts with embedded boundaries, and ``HydroUtils::ExtrapVelToFaces`` does not accept it. @@ -155,12 +165,6 @@ in conservative form. ``"PLIC"`` aborts with embedded boundaries, and it ``"PLIC"`` aborts. The name of the build directory does not depend on it, so after adding ``PLIC`` to an existing build, run ``make clean`` to rebuild ``Utils``. -.. note:: - - Keep the interface on the finest level, by tagging the cells for which ``PLIC::interface_band`` - holds: flux volumes do not tile across coarse/fine boundaries, which must stay in pure cells. With - subcycling, the reflux corrections then fall in pure cells too. - Reconstructions --------------- diff --git a/Tests/PLIC/README.md b/Tests/PLIC/README.md index 750a011b..3819d45b 100644 --- a/Tests/PLIC/README.md +++ b/Tests/PLIC/README.md @@ -16,7 +16,7 @@ mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex | `inputs.kernels` | Plane geometry, normals, plane fit, wall ghost cells, face density | | `inputs.2d.vortex` | Circle in the time-reversed single vortex, 3 levels | | `inputs.2d.zalesak` | Zalesak's slotted disk, one revolution, 3 levels | -| `inputs.2d.wall_vortex` | Circle cut by a wall, in the same flow: a free contact line | +| `inputs.2d.wall_vortex` | Circle cut by a wall, in the same flow, through `HydroUtils` | | `inputs.3d.deform` | Sphere in LeVeque's deformation field, 3 levels | The advection cases return to their initial shape at `t = prob.period`, so the shape error is the L1 @@ -24,7 +24,8 @@ difference from the initial volume fraction. `plic.reconstruction` selects the r (default `myc_height`). Set `amr.max_level = 0` and `amr.n_cell` for a uniform grid. The `test.max_*` parameters set the checks. With `test.use_hydroutils = 1` the test advects F as a solver would any other scalar, through `HydroUtils::ComputeFluxesOnBoxFromState` with `advection_type = "PLIC"`, and -checks that it gives the fluxes and edge states of `PLIC::ComputeEdgeStateAndFluxes` to the bit. +checks that it gives the fluxes and edge states of `PLIC::ComputeEdgeStateAndFluxes` to the bit, on +the first step and every `test.hydroutils_int` steps (default 10; 1 checks every step). `test.plot_file = ` writes the final volume fraction as a plotfile (component `vof`, every level), e.g. `mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex test.plot_file=plt`. diff --git a/Tests/PLIC/inputs.2d.wall_vortex b/Tests/PLIC/inputs.2d.wall_vortex index 3d2ead53..e9c0d66c 100644 --- a/Tests/PLIC/inputs.2d.wall_vortex +++ b/Tests/PLIC/inputs.2d.wall_vortex @@ -20,6 +20,8 @@ prob.period = 2.0 amr.fixed_dt = 0.0078125 amr.stop_time = 2.0 +# Advect through HydroUtils, and compare with PLIC::ComputeEdgeStateAndFluxes +test.use_hydroutils = 1 test.max_volume_change = 1.0e-13 test.max_f_excursion = 1.0e-9 diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index 143dc780..a56c4f35 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -144,6 +144,8 @@ class AdvectTest final : public AmrCore ppt.query("max_volume_change", m_max_volume_change); ppt.query("max_f_excursion", m_max_f_excursion); ppt.query("use_hydroutils", m_use_hydroutils); + ppt.query("hydroutils_int", m_hydroutils_int); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_hydroutils_int > 0, "test.hydroutils_int must be positive"); if (m_use_hydroutils) { AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_reconstruction == "myc_height", "test.use_hydroutils needs plic.reconstruction = myc_height"); @@ -187,7 +189,7 @@ class AdvectTest final : public AmrCore } const Real dt = ComputeDt(time, prev_dt, nstep); - Advance(time, dt); + Advance(time, dt, m_use_hydroutils && nstep % m_hydroutils_int == 0); const Real vol_new = Volume(); max_change = max(max_change, std::abs(vol_new - vol_now) / max(vol_now, std::numeric_limits::min())); vol_now = vol_new; @@ -356,10 +358,61 @@ class AdvectTest final : public AmrCore } } + // The largest difference of the fluxes and edge states from the reference ones, on this rank. Not a + // lambda: nvcc does not take a device lambda inside one. + void CompareFluxes (Faces const& fl, Faces const& fc, Faces const& fl_ref, Faces const& fc_ref) + { + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + auto const& a = fl[d].const_arrays(); + auto const& b = fc[d].const_arrays(); + auto const& a_ref = fl_ref[d].const_arrays(); + auto const& b_ref = fc_ref[d].const_arrays(); + const Real diff = ParReduce(TypeList{}, TypeList{}, fl[d], IntVect(0), + [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept -> GpuTuple { + return {nan_as_max(max(std::abs(a[n](i, j, k) - a_ref[n](i, j, k)), + std::abs(b[n](i, j, k) - b_ref[n](i, j, k))))}; + }); + m_hydroutils_diff = max(m_hydroutils_diff, diff); + } + } + + // The fluxes expected with u_flux: the geometric ones where it is u_mac, else edge state * u_flux + void ExpectedFluxes (Faces& expected, Faces const& geometric, Faces const& fc, Faces const& umac, + Faces const& uflux) + { + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + auto const& ex = expected[d].arrays(); + auto const& ge = geometric[d].const_arrays(); + auto const& ed = fc[d].const_arrays(); + auto const& um = umac[d].const_arrays(); + auto const& uf = uflux[d].const_arrays(); + ParallelFor(expected[d], [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept + { + ex[n](i, j, k) = (uf[n](i, j, k) == um[n](i, j, k)) ? ge[n](i, j, k) + : ed[n](i, j, k) * uf[n](i, j, k); + }); + } + Gpu::streamSynchronize(); + } + + // Halve the face velocities in the upper half of the domain in x + void HalveUpperX (Faces& u, int lev) + { + const int mid = (Geom(lev).Domain().smallEnd(0) + Geom(lev).Domain().bigEnd(0) + 1) / 2; + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + auto const& ua = u[d].arrays(); + ParallelFor(u[d], u[d].nGrowVect(), [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept + { + if (i >= mid) { ua[n](i, j, k) *= 0.5_rt; } + }); + } + Gpu::streamSynchronize(); + } + // Through HydroUtils, the fluxes and edge states of one step on one level, on this rank, to the bit: // - the step itself (u_flux the same Array4s as u_mac), a copy of u_mac as u_flux, and both with // area-weighted fluxes: those of PLIC::ComputeEdgeStateAndFluxes; - // - a corrective u_flux: PLIC's edge states, times u_flux; + // - a corrective u_flux, on every face or on some: PLIC's edge states times u_flux where it differs; // - knownFaceState with iconserv = 1: the given edge states, times u_mac; // - knownFaceState with iconserv = -1: the face arrays (PLIC's fluxes), copied. void CompareWithPLIC (Faces const& flux, Faces const& face, Faces const& umac, int lev, Real dt) @@ -372,21 +425,6 @@ class AdvectTest final : public AmrCore if (src) { MultiFab::Copy(mf[d], (*src)[d], 0, 0, 1, like.nGrow()); } } }; - const auto compare = [&] (Faces const& fl, Faces const& fc, Faces const& fl_ref, Faces const& fc_ref) { - for (int d = 0; d < AMREX_SPACEDIM; ++d) { - auto const& a = fl[d].const_arrays(); - auto const& b = fc[d].const_arrays(); - auto const& a_ref = fl_ref[d].const_arrays(); - auto const& b_ref = fc_ref[d].const_arrays(); - const Real diff = ParReduce(TypeList{}, TypeList{}, fl[d], IntVect(0), - [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept -> GpuTuple { - return {nan_as_max(max(std::abs(a[n](i, j, k) - a_ref[n](i, j, k)), - std::abs(b[n](i, j, k) - b_ref[n](i, j, k))))}; - }); - m_hydroutils_diff = max(m_hydroutils_diff, diff); - } - }; - // PLIC's own, without and with area weighting Faces flux_ref, face_ref, flux_ref_aw, face_ref_aw; for (Faces* mf : {&flux_ref, &face_ref, &flux_ref_aw, &face_ref_aw}) { define(*mf); } @@ -394,30 +432,46 @@ class AdvectTest final : public AmrCore PLICFluxes(lev, flux_ref_aw, face_ref_aw, umac, dt, true); // The step itself - compare(flux, face, flux_ref, face_ref); + CompareFluxes(flux, face, flux_ref, face_ref); // A copy of u_mac, without and with area weighting Faces umac_copy, fl, fc; define(umac_copy, &umac); define(fl); define(fc); + // So that a flux or an edge state that is not written shows + const auto reset = [] (Faces& mf) { + for (auto& m : mf) { m.setVal(std::numeric_limits::quiet_NaN()); } + }; + reset(fl); + reset(fc); HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, false); - compare(fl, fc, flux_ref, face_ref); + CompareFluxes(fl, fc, flux_ref, face_ref); + reset(fl); + reset(fc); HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, true); - compare(fl, fc, flux_ref_aw, face_ref_aw); + CompareFluxes(fl, fc, flux_ref_aw, face_ref_aw); + reset(fl); + reset(fc); HydroUtilsFluxes(lev, fl, fc, umac, &umac, false, m_iconserv_d.dataPtr(), dt, true); - compare(fl, fc, flux_ref_aw, face_ref_aw); + CompareFluxes(fl, fc, flux_ref_aw, face_ref_aw); - // A corrective u_flux + // A corrective u_flux on every face, then on part of the level (and of some boxes) only Faces ucorr, flux_corr; define(ucorr, &umac); - define(flux_corr, &face_ref); - for (int d = 0; d < AMREX_SPACEDIM; ++d) { - ucorr[d].mult(0.5_rt); - MultiFab::Multiply(flux_corr[d], ucorr[d], 0, 0, 1, 0); + define(flux_corr); + for (auto& u : ucorr) { u.mult(0.5_rt, u.nGrow()); } + for (const bool partial : {false, true}) { + if (partial) { + define(ucorr, &umac); + HalveUpperX(ucorr, lev); + } + ExpectedFluxes(flux_corr, flux_ref, face_ref, umac, ucorr); + reset(fl); + reset(fc); + HydroUtilsFluxes(lev, fl, fc, umac, &ucorr, false, m_iconserv_d.dataPtr(), dt, false); + CompareFluxes(fl, fc, flux_corr, face_ref); } - HydroUtilsFluxes(lev, fl, fc, umac, &ucorr, false, m_iconserv_d.dataPtr(), dt, false); - compare(fl, fc, flux_corr, face_ref); // knownFaceState: the edge states (iconserv = 1) or the fluxes (iconserv = -1) Faces flux_known; @@ -426,12 +480,14 @@ class AdvectTest final : public AmrCore MultiFab::Multiply(flux_known[d], umac[d], 0, 0, 1, 0); } define(fc, &face_ref); + reset(fl); HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, m_iconserv_d.dataPtr(), dt, false); - compare(fl, fc, flux_known, face_ref); + CompareFluxes(fl, fc, flux_known, face_ref); const Gpu::DeviceScalar iconserv_flux{-1}; define(fc, &flux_ref); + reset(fl); HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, iconserv_flux.dataPtr(), dt, false); - compare(fl, fc, flux_ref, flux_ref); + CompareFluxes(fl, fc, flux_ref, flux_ref); } // The edge states of one step on one level, on this rank: on every face with a flow, the liquid @@ -459,8 +515,8 @@ class AdvectTest final : public AmrCore // One step on all levels with the same time step, as a solver without subcycling would: the fluxes // of each box, the fine fluxes onto the coarse faces under them, the conservative update, and the // ghost cells. A subcycling solver would advance each level on its own and reflux with the same - // fluxes instead. - void Advance (Real time, Real dt) + // fluxes instead. With compare, the fluxes through HydroUtils are checked (see CompareWithPLIC). + void Advance (Real time, Real dt, bool compare) { const int nlevs = finest_level + 1; Vector> umac(nlevs); @@ -483,7 +539,7 @@ class AdvectTest final : public AmrCore PLICFluxes(lev, flux[lev], face[lev], umac[lev], dt, false); } CheckEdgeStates(flux[lev], face[lev], umac[lev]); - if (m_use_hydroutils) { CompareWithPLIC(flux[lev], face[lev], umac[lev], lev, dt); } + if (compare) { CompareWithPLIC(flux[lev], face[lev], umac[lev], lev, dt); } } for (int lev = nlevs - 1; lev > 0; --lev) { average_down_faces(GetArrOfConstPtrs(flux[lev]), GetArrOfPtrs(flux[lev - 1]), refRatio(lev - 1), @@ -613,6 +669,9 @@ class AdvectTest final : public AmrCore Vector m_bcrec; Gpu::DeviceScalar m_iconserv_d{1}; bool m_use_hydroutils = false; + // Steps between the comparisons with PLIC, each about 7 times the fluxes of a step. Not a multiple + // of regrid_int, to compare right after a regrid and between two + int m_hydroutils_int = 10; int m_regrid_int = 4; int m_refine_band = 2; int m_max_step = 1000000; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index 7c1f82fb..dfc7be6a 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -7,7 +7,6 @@ #include #ifdef HYDRO_USE_PLIC #include -#include #endif #include @@ -187,33 +186,34 @@ namespace { } #ifdef HYDRO_USE_PLIC - // Whether u_flux holds the values of u_mac on the faces of bx (the same Array4s, or a copy) - bool SameFaceVelocity (Box const& bx, - AMREX_D_DECL(Array4 const& u_mac, - Array4 const& v_mac, - Array4 const& w_mac), - AMREX_D_DECL(Array4 const& u_flux, - Array4 const& v_flux, - Array4 const& w_flux)) + // The fluxes of the faces where u_flux differs from u_mac: edge state times u_flux. Decided face + // by face, so that the boxes and tiles sharing a face agree on its flux. + void CorrectiveFluxes (Box const& bx, + Array, AMREX_SPACEDIM> const& flux, + Array, AMREX_SPACEDIM> const& face, + Array, AMREX_SPACEDIM> const& mac, + Array, AMREX_SPACEDIM> const& corr, + Geometry const& geom, bool fluxes_are_area_weighted) { - const Array, AMREX_SPACEDIM> mac{AMREX_D_DECL(u_mac, v_mac, w_mac)}; - const Array, AMREX_SPACEDIM> flx{AMREX_D_DECL(u_flux, v_flux, w_flux)}; - // One reduction over the directions whose arrays differ, so one sync - ReduceOps reduce_op; - ReduceData reduce_data(reduce_op); - bool compared = false; for (int d = 0; d < AMREX_SPACEDIM; ++d) { - if (flx[d].dataPtr() == mac[d].dataPtr()) { continue; } - const Array4 um = mac[d]; - const Array4 uf = flx[d]; - reduce_op.eval(amrex::surroundingNodes(bx, d), reduce_data, - [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept -> GpuTuple + if (corr[d].dataPtr() == mac[d].dataPtr()) { continue; } + Real area = Real(1.); + if (fluxes_are_area_weighted) { + for (int t = 0; t < AMREX_SPACEDIM; ++t) { + if (t != d) { area *= geom.CellSize(t); } + } + } + Array4 const fl = flux[d]; + Array4 const fc = face[d]; + Array4 const um = mac[d]; + Array4 const uf = corr[d]; + amrex::ParallelFor(amrex::surroundingNodes(bx, d), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { - return { static_cast(!(uf(i,j,k) == um(i,j,k))) }; + if (uf(i,j,k) != um(i,j,k)) { + fl(i,j,k) = fc(i,j,k) * uf(i,j,k) * area; + } }); - compared = true; } - return !compared || amrex::get<0>(reduce_data.value(reduce_op)) == 0; } #endif @@ -502,6 +502,8 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, // rejected as a whole, rather than only its cut boxes. AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), "PLIC is not available with EB"); #endif + // Also with knownFaceState, which does not reach the check in PLIC + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(geom.IsCartesian(), "PLIC requires Cartesian coordinates"); // The volume fraction is one scalar, advected in conservative form. Its ghost cells // carry the boundary conditions (PLIC::FillWallGhosts), so the BCRecs are not read. AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, @@ -522,13 +524,13 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, AMREX_D_DECL(u_mac,v_mac,w_mac), geom, l_dt, "myc_height", fluxes_are_area_weighted); // Where shear folds a flux volume the edge state is held to [0,1], and edge state * u - // is not the liquid that crosses the face: keep PLIC's geometric fluxes when they are - // formed with u_mac. A corrective u_flux forms its fluxes from the edge states below. - if (SameFaceVelocity(bx, AMREX_D_DECL(u_mac,v_mac,w_mac), AMREX_D_DECL(u_flux,v_flux,w_flux))) { - return; - } + // is not the liquid that crosses the face: keep PLIC's geometric fluxes on the faces + // where u_flux is u_mac. + CorrectiveFluxes(bx, {AMREX_D_DECL(flux_x,flux_y,flux_z)}, {AMREX_D_DECL(face_x,face_y,face_z)}, + {AMREX_D_DECL(u_mac,v_mac,w_mac)}, {AMREX_D_DECL(u_flux,v_flux,w_flux)}, + geom, fluxes_are_area_weighted); + return; } - knownFaceState = true; #else Abort("advection_type PLIC needs AMReX-Hydro built with PLIC (with GNUmake, add the PLIC " "directory to the build)"); diff --git a/Utils/hydro_utils.H b/Utils/hydro_utils.H index 2877291d..810bfab6 100644 --- a/Utils/hydro_utils.H +++ b/Utils/hydro_utils.H @@ -68,6 +68,7 @@ void enforceInOutSolvability ( * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. + * q then needs PLIC::nGrowF("myc_height") filled ghost cells. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -166,6 +167,7 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. + * q then needs PLIC::nGrowF("myc_height") filled ghost cells. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -228,6 +230,7 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. + * q then needs PLIC::nGrowF("myc_height") filled ghost cells. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -289,6 +292,7 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. + * q then needs PLIC::nGrowF("myc_height") filled ghost cells. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -350,6 +354,7 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. + * q then needs PLIC::nGrowF("myc_height") filled ghost cells. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and From 2ed9d8f8e4eff057e49621cf7fd60e33f396d503 Mon Sep 17 00:00:00 2001 From: Thomas Howarth Date: Tue, 1 Dec 2026 11:07:25 +0000 Subject: [PATCH 6/7] PLIC: fourth review fixes to advection_type "PLIC" in HydroUtils - Corrective fluxes: where u_mac is zero or of the other sign to u_flux, take the upwind F of u_flux rather than PLIC's edge state (upwinded by u_mac), so the flux never draws on the downwind cell. Moved to a public PLIC::CorrectiveFluxes, reusing upwind_fraction and FaceArea. - The iconserv check and the per-box Courant check run in CPU and debug builds only; optimized GPU builds rely on PLIC::CheckCourantNumber before the MFIter loop, so the boxes are not serialized. - The EB check for "PLIC" comes before the covered-box early return. - Docs: myc_height is fixed, so FillWallGhosts and SharpFaceDensity must use it too; u_flux is compared with u_mac bit for bit. - Tests: corrective u_flux of the sign of u_mac, reversed, and non-zero where u_mac is zero, without and with area weighting; a NaN edge state no longer hides behind max(); inputs.2d.vortex and inputs.3d.deform also advect through HydroUtils (AMR, periodic, 3D). Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 26 ++++- PLIC/hydro_plic.H | 34 ++++++ PLIC/hydro_plic_owkes_desjardins.cpp | 43 +++++++ Tests/PLIC/README.md | 4 +- Tests/PLIC/inputs.2d.vortex | 3 + Tests/PLIC/inputs.3d.deform | 3 + Tests/PLIC/test_advect.cpp | 127 ++++++++++++--------- Utils/hydro_compute_edgestate_and_flux.cpp | 76 +++++------- Utils/hydro_utils.H | 15 ++- 9 files changed, 218 insertions(+), 113 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index b4ce701f..be507a94 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -142,19 +142,35 @@ A solver that advects its scalars with ``HydroUtils::ComputeFluxesOnBoxFromState :math:`F` with the same call and ``advection_type = "PLIC"``, with ``ncomp = 1``, ``iconserv = 1`` and ``is_velocity = false``. It uses the ``myc_height`` reconstruction: -- on the faces where ``u_flux`` equals ``u_mac`` (every face, with the overloads without ``u_flux``), +- on the faces where ``u_flux`` equals ``u_mac`` bit for bit (every face, with the overloads without + ``u_flux``), it gives the edge states and the geometric fluxes of ``PLIC::ComputeEdgeStateAndFluxes``, to the bit; -- on the faces where a corrective ``u_flux`` differs, the fluxes are PLIC's edge states times - ``u_flux``. The choice is made face by face, so the boxes sharing a face get the same flux; +- on the faces where a corrective ``u_flux`` differs, even by round-off, the fluxes are PLIC's edge states times + ``u_flux``. PLIC's edge states are upwinded by ``u_mac``: where ``u_mac`` is zero or of the other + sign, the flux takes instead the :math:`F` of the upwind cell of ``u_flux``, held to [0,1], so that + it never draws on the downwind cell, unlike the other schemes, as :math:`F` must stay in [0,1]. + The face arrays keep PLIC's edge states. The choice is made face by face, so the boxes sharing a + face get the same flux. A solver that calls PLIC directly gets the same fluxes from + ``PLIC::CorrectiveFluxes``; - with ``knownFaceState``, the face arrays are the edge states (``iconserv = 1``) or the fluxes (``iconserv = -1``). Any other ``iconserv`` aborts. :math:`F` needs ``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the boundary conditions in place of the ``BCRec`` arguments; ``qnph``, ``divu``, the forcing ``fq``, the ``godunov_*`` flags, ``limiter_type`` and ``allow_inflow_on_outflow`` are not -read. On GPUs each box waits for the copy of ``iconserv`` to the host and for its Courant check; -where that matters, call ``PLIC::ComputeEdgeStateAndFluxes`` with ``check_courant = false`` instead. +read. The reconstruction is always ``myc_height``: a solver that advects :math:`F` this way must +pass ``"myc_height"`` to ``PLIC::FillWallGhosts`` and ``PLIC::SharpFaceDensity`` too, or the wall +ghost cells and the face density come from other normals than the fluxes (or ``F`` has too few +ghost cells). To call PLIC directly with another reconstruction, use +``PLIC::ComputeEdgeStateAndFluxes``. + +On GPUs, check the Courant number of each level once with ``PLIC::CheckCourantNumber`` before the +``MFIter`` loop: optimized GPU builds do not check it box by box, as that waits for the GPU, and +neither do they check that ``iconserv`` is 1, or -1 with ``knownFaceState`` (other values are then +treated as 1). A face Courant number above 1 makes the flux volumes read beyond the data, so a GPU +solver that skips ``PLIC::CheckCourantNumber`` must bound its time step some other way. CPU builds +and debug builds check both, box by box. ``HydroUtils::ComputeConvectiveTerm`` accepts ``"PLIC"`` and adds nothing, as :math:`F` is advected in conservative form. ``"PLIC"`` aborts with embedded boundaries, and ``HydroUtils::ExtrapVelToFaces`` does not accept it. diff --git a/PLIC/hydro_plic.H b/PLIC/hydro_plic.H index 5e0fb7f3..7062d254 100644 --- a/PLIC/hydro_plic.H +++ b/PLIC/hydro_plic.H @@ -119,6 +119,40 @@ void ComputeEdgeStateAndFluxes (amrex::Box const& bx, bool fluxes_are_area_weighted = false, bool check_courant = true); +/** + * \brief Overwrite the fluxes from ComputeEdgeStateAndFluxes on the faces where a corrective velocity + * u_flux differs from umac (bit for bit), as in a multilevel synchronization: the edge state + * times u_flux there. The edge states are upwinded by umac: where umac is zero or of the other + * sign, the flux takes instead the F of the upwind cell of u_flux, held to [0,1], so that it + * never draws on the downwind cell. The edge states are not changed. Decided face by face, so + * that the boxes sharing a face agree on its flux; a direction whose u_flux is the umac array + * itself is skipped. + * + * \param flux_x fluxes on the x faces of bx, from ComputeEdgeStateAndFluxes (likewise y, z) + * \param face_x edge states on the x faces of bx, from ComputeEdgeStateAndFluxes (likewise y, z) + * \param F volume fraction at t^n, with one filled ghost cell + * \param umac the face velocity the fluxes were computed with (likewise vmac, wmac) + * \param uflux the corrective face velocity (likewise vflux, wflux) + * \param geom geometry of the level + * \param fluxes_are_area_weighted as passed to ComputeEdgeStateAndFluxes + */ +void CorrectiveFluxes (amrex::Box const& bx, + AMREX_D_DECL(amrex::Array4 const& flux_x, + amrex::Array4 const& flux_y, + amrex::Array4 const& flux_z), + AMREX_D_DECL(amrex::Array4 const& face_x, + amrex::Array4 const& face_y, + amrex::Array4 const& face_z), + amrex::Array4 const& F, + AMREX_D_DECL(amrex::Array4 const& umac, + amrex::Array4 const& vmac, + amrex::Array4 const& wmac), + AMREX_D_DECL(amrex::Array4 const& uflux, + amrex::Array4 const& vflux, + amrex::Array4 const& wflux), + amrex::Geometry const& geom, + bool fluxes_are_area_weighted = false); + /** * \brief Abort unless the face Courant number, |u| dt/dx, is below 1 on every face that * ComputeEdgeStateAndFluxes reads for the boxes of the level: their faces and one ghost face diff --git a/PLIC/hydro_plic_owkes_desjardins.cpp b/PLIC/hydro_plic_owkes_desjardins.cpp index 2b413378..0d2cd221 100644 --- a/PLIC/hydro_plic_owkes_desjardins.cpp +++ b/PLIC/hydro_plic_owkes_desjardins.cpp @@ -271,6 +271,49 @@ ComputeEdgeStateAndFluxes (amrex::Box const& bx, }); } +void +CorrectiveFluxes (amrex::Box const& bx, + AMREX_D_DECL(amrex::Array4 const& flux_x, + amrex::Array4 const& flux_y, + amrex::Array4 const& flux_z), + AMREX_D_DECL(amrex::Array4 const& face_x, + amrex::Array4 const& face_y, + amrex::Array4 const& face_z), + amrex::Array4 const& F, + AMREX_D_DECL(amrex::Array4 const& umac, + amrex::Array4 const& vmac, + amrex::Array4 const& wmac), + AMREX_D_DECL(amrex::Array4 const& uflux, + amrex::Array4 const& vflux, + amrex::Array4 const& wflux), + amrex::Geometry const& geom, + bool fluxes_are_area_weighted) +{ + BL_PROFILE("PLIC::CorrectiveFluxes()"); + const amrex::Array, AMREX_SPACEDIM> flux{AMREX_D_DECL(flux_x, flux_y, flux_z)}; + const amrex::Array, AMREX_SPACEDIM> face{AMREX_D_DECL(face_x, face_y, face_z)}; + const amrex::Array, AMREX_SPACEDIM> vel{AMREX_D_DECL(umac, vmac, wmac)}; + const amrex::Array, AMREX_SPACEDIM> corr{AMREX_D_DECL(uflux, vflux, wflux)}; + for (int d = 0; d < AMREX_SPACEDIM; ++d) { + // The same arrays: every face keeps its geometric flux + if (corr[d].dataPtr() == vel[d].dataPtr()) { continue; } + const amrex::Real area = detail::FaceArea(geom, d, fluxes_are_area_weighted); + amrex::Array4 const fl = flux[d]; + amrex::Array4 const fc = face[d]; + amrex::Array4 const um = vel[d]; + amrex::Array4 const uf = corr[d]; + amrex::ParallelFor(amrex::surroundingNodes(bx, d), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept + { + const amrex::Real u = uf(i, j, k); + if (u != um(i, j, k)) { + const amrex::Real fe = (um(i, j, k) * u > 0.0_rt) + ? fc(i, j, k) : upwind_fraction(F, amrex::IntVect(AMREX_D_DECL(i, j, k)), d, u); + fl(i, j, k) = fe * u * area; + } + }); + } +} + void CheckCourantNumber (AMREX_D_DECL(amrex::MultiFab const& umac, amrex::MultiFab const& vmac, diff --git a/Tests/PLIC/README.md b/Tests/PLIC/README.md index 3819d45b..42f7e124 100644 --- a/Tests/PLIC/README.md +++ b/Tests/PLIC/README.md @@ -14,10 +14,10 @@ mpirun -np 4 ./main2d.gnu.MPI.ex inputs.2d.vortex | Input | Case | |-------------------------|-----------------------------------------------------------------| | `inputs.kernels` | Plane geometry, normals, plane fit, wall ghost cells, face density | -| `inputs.2d.vortex` | Circle in the time-reversed single vortex, 3 levels | +| `inputs.2d.vortex` | Circle in the time-reversed single vortex, 3 levels, through `HydroUtils` | | `inputs.2d.zalesak` | Zalesak's slotted disk, one revolution, 3 levels | | `inputs.2d.wall_vortex` | Circle cut by a wall, in the same flow, through `HydroUtils` | -| `inputs.3d.deform` | Sphere in LeVeque's deformation field, 3 levels | +| `inputs.3d.deform` | Sphere in LeVeque's deformation field, 3 levels, through `HydroUtils` | The advection cases return to their initial shape at `t = prob.period`, so the shape error is the L1 difference from the initial volume fraction. `plic.reconstruction` selects the reconstruction diff --git a/Tests/PLIC/inputs.2d.vortex b/Tests/PLIC/inputs.2d.vortex index 24ca2589..6fbd5051 100644 --- a/Tests/PLIC/inputs.2d.vortex +++ b/Tests/PLIC/inputs.2d.vortex @@ -27,6 +27,9 @@ amr.fixed_dt = 0.001953125 amr.stop_time = 4.0 test.max_l1 = 5.0e-4 +# Advect through HydroUtils, and compare with PLIC::ComputeEdgeStateAndFluxes +test.use_hydroutils = 1 + test.max_volume_change = 1.0e-13 test.max_f_excursion = 1.0e-9 diff --git a/Tests/PLIC/inputs.3d.deform b/Tests/PLIC/inputs.3d.deform index 2d130041..5d2ecabc 100644 --- a/Tests/PLIC/inputs.3d.deform +++ b/Tests/PLIC/inputs.3d.deform @@ -23,6 +23,9 @@ prob.period = 3.0 amr.fixed_dt = 0.001953125 amr.stop_time = 3.0 +# Advect through HydroUtils, and compare with PLIC::ComputeEdgeStateAndFluxes +test.use_hydroutils = 1 + test.max_volume_change = 1.0e-13 test.max_f_excursion = 1.0e-9 diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index a56c4f35..548d0521 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -369,18 +369,28 @@ class AdvectTest final : public AmrCore auto const& b_ref = fc_ref[d].const_arrays(); const Real diff = ParReduce(TypeList{}, TypeList{}, fl[d], IntVect(0), [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept -> GpuTuple { - return {nan_as_max(max(std::abs(a[n](i, j, k) - a_ref[n](i, j, k)), - std::abs(b[n](i, j, k) - b_ref[n](i, j, k))))}; + // Each difference through nan_as_max on its own: max(x, NaN) is x + return {max(nan_as_max(std::abs(a[n](i, j, k) - a_ref[n](i, j, k))), + nan_as_max(std::abs(b[n](i, j, k) - b_ref[n](i, j, k))))}; }); m_hydroutils_diff = max(m_hydroutils_diff, diff); } } - // The fluxes expected with u_flux: the geometric ones where it is u_mac, else edge state * u_flux + // The fluxes expected with u_flux: the geometric ones where it is u_mac, else edge state * u_flux, + // with the edge state of u_mac where u_mac has the sign of u_flux, and the upwind F of u_flux where + // u_mac is zero or of the other sign void ExpectedFluxes (Faces& expected, Faces const& geometric, Faces const& fc, Faces const& umac, - Faces const& uflux) + Faces const& uflux, int lev, bool area_weighted) { + auto const& F = m_F[lev].const_arrays(); for (int d = 0; d < AMREX_SPACEDIM; ++d) { + Real area = 1.0_rt; + if (area_weighted) { + for (int t = 0; t < AMREX_SPACEDIM; ++t) { + if (t != d) { area *= Geom(lev).CellSize(t); } + } + } auto const& ex = expected[d].arrays(); auto const& ge = geometric[d].const_arrays(); auto const& ed = fc[d].const_arrays(); @@ -388,33 +398,55 @@ class AdvectTest final : public AmrCore auto const& uf = uflux[d].const_arrays(); ParallelFor(expected[d], [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept { - ex[n](i, j, k) = (uf[n](i, j, k) == um[n](i, j, k)) ? ge[n](i, j, k) - : ed[n](i, j, k) * uf[n](i, j, k); + const Real u = uf[n](i, j, k); + if (u == um[n](i, j, k)) { + ex[n](i, j, k) = ge[n](i, j, k); + } else if (um[n](i, j, k) * u > 0.0_rt) { + ex[n](i, j, k) = ed[n](i, j, k) * u * area; + } else { + IntVect iv(AMREX_D_DECL(i, j, k)); + if (u >= 0.0_rt) { iv[d] -= 1; } + ex[n](i, j, k) = Clamp(F[n](iv), 0.0_rt, 1.0_rt) * u * area; + } }); } - Gpu::streamSynchronize(); } - // Halve the face velocities in the upper half of the domain in x - void HalveUpperX (Faces& u, int lev) + // A corrective velocity with a different case in each quarter of the domain in x, so that the + // boxes span several: half of u_mac (of the sign of u_mac), u_mac itself (the geometric fluxes), + // u_mac reversed (the upwind F of u_flux), and u_mac shifted (non-zero where u_mac is zero, and of + // the other sign where u_mac is small) + void CorrectiveVelocity (Faces& u, int lev) { - const int mid = (Geom(lev).Domain().smallEnd(0) + Geom(lev).Domain().bigEnd(0) + 1) / 2; + const Box& domain = Geom(lev).Domain(); + const int lo = domain.smallEnd(0); + const int n = domain.length(0); for (int d = 0; d < AMREX_SPACEDIM; ++d) { auto const& ua = u[d].arrays(); - ParallelFor(u[d], u[d].nGrowVect(), [=] AMREX_GPU_DEVICE (int n, int i, int j, int k) noexcept + ParallelFor(u[d], u[d].nGrowVect(), [=] AMREX_GPU_DEVICE (int b, int i, int j, int k) noexcept { - if (i >= mid) { ua[n](i, j, k) *= 0.5_rt; } + const int quarter = Clamp((i - lo) * 4 / n, 0, 3); + Real& v = ua[b](i, j, k); + if (quarter == 0) { + v *= 0.5_rt; + } else if (quarter == 2) { + v = -v; + } else if (quarter == 3) { + v += 0.1_rt; + } }); } - Gpu::streamSynchronize(); } // Through HydroUtils, the fluxes and edge states of one step on one level, on this rank, to the bit: - // - the step itself (u_flux the same Array4s as u_mac), a copy of u_mac as u_flux, and both with - // area-weighted fluxes: those of PLIC::ComputeEdgeStateAndFluxes; - // - a corrective u_flux, on every face or on some: PLIC's edge states times u_flux where it differs; + // - the step itself (u_flux the same Array4s as u_mac), with area-weighted fluxes, and a copy of + // u_mac as u_flux: those of PLIC::ComputeEdgeStateAndFluxes; + // - a corrective u_flux (see CorrectiveVelocity), without and with area weighting: where it + // differs, PLIC's edge states times u_flux, or the upwind F of u_flux times u_flux where u_mac is + // zero or of the other sign; // - knownFaceState with iconserv = 1: the given edge states, times u_mac; // - knownFaceState with iconserv = -1: the face arrays (PLIC's fluxes), copied. + // In all, 6 computations of the PLIC fluxes, besides those of the step. void CompareWithPLIC (Faces const& flux, Faces const& face, Faces const& umac, int lev, Real dt) { // On the faces of the boxes, or as a copy of src, with its ghost faces @@ -422,8 +454,8 @@ class AdvectTest final : public AmrCore for (int d = 0; d < AMREX_SPACEDIM; ++d) { MultiFab const& like = src ? (*src)[d] : flux[d]; mf[d].define(like.boxArray(), like.DistributionMap(), 1, like.nGrow()); - if (src) { MultiFab::Copy(mf[d], (*src)[d], 0, 0, 1, like.nGrow()); } } + if (src) { LocalCopy(mf, *src, 0, 0, 1, (*src)[0].nGrowVect()); } }; // PLIC's own, without and with area weighting Faces flux_ref, face_ref, flux_ref_aw, face_ref_aw; @@ -434,43 +466,34 @@ class AdvectTest final : public AmrCore // The step itself CompareFluxes(flux, face, flux_ref, face_ref); - // A copy of u_mac, without and with area weighting - Faces umac_copy, fl, fc; - define(umac_copy, &umac); + // Through HydroUtils into fl and fc, which start as NaN so that a flux or an edge state that is + // not written shows (with knownFaceState, fc holds the given face arrays and is kept) + Faces fl, fc; define(fl); define(fc); - // So that a flux or an edge state that is not written shows - const auto reset = [] (Faces& mf) { - for (auto& m : mf) { m.setVal(std::numeric_limits::quiet_NaN()); } + const auto run = [&] (Faces const& uflux, bool known, int const* iconserv, bool aw) { + setVal(fl, std::numeric_limits::quiet_NaN()); + if (!known) { setVal(fc, std::numeric_limits::quiet_NaN()); } + HydroUtilsFluxes(lev, fl, fc, umac, &uflux, known, iconserv, dt, aw); }; - reset(fl); - reset(fc); - HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, false); - CompareFluxes(fl, fc, flux_ref, face_ref); - reset(fl); - reset(fc); - HydroUtilsFluxes(lev, fl, fc, umac, &umac_copy, false, m_iconserv_d.dataPtr(), dt, true); - CompareFluxes(fl, fc, flux_ref_aw, face_ref_aw); - reset(fl); - reset(fc); - HydroUtilsFluxes(lev, fl, fc, umac, &umac, false, m_iconserv_d.dataPtr(), dt, true); - CompareFluxes(fl, fc, flux_ref_aw, face_ref_aw); - // A corrective u_flux on every face, then on part of the level (and of some boxes) only - Faces ucorr, flux_corr; + // u_flux the u_mac arrays, with area weighting; a copy of u_mac + run(umac, false, m_iconserv_d.dataPtr(), true); + CompareFluxes(fl, fc, flux_ref_aw, face_ref_aw); + Faces ucorr; define(ucorr, &umac); + run(ucorr, false, m_iconserv_d.dataPtr(), false); + CompareFluxes(fl, fc, flux_ref, face_ref); + + // A corrective u_flux, without and with area weighting + CorrectiveVelocity(ucorr, lev); + Faces flux_corr; define(flux_corr); - for (auto& u : ucorr) { u.mult(0.5_rt, u.nGrow()); } - for (const bool partial : {false, true}) { - if (partial) { - define(ucorr, &umac); - HalveUpperX(ucorr, lev); - } - ExpectedFluxes(flux_corr, flux_ref, face_ref, umac, ucorr); - reset(fl); - reset(fc); - HydroUtilsFluxes(lev, fl, fc, umac, &ucorr, false, m_iconserv_d.dataPtr(), dt, false); - CompareFluxes(fl, fc, flux_corr, face_ref); + for (const bool aw : {false, true}) { + ExpectedFluxes(flux_corr, aw ? flux_ref_aw : flux_ref, aw ? face_ref_aw : face_ref, umac, ucorr, + lev, aw); + run(ucorr, false, m_iconserv_d.dataPtr(), aw); + CompareFluxes(fl, fc, flux_corr, aw ? face_ref_aw : face_ref); } // knownFaceState: the edge states (iconserv = 1) or the fluxes (iconserv = -1) @@ -480,13 +503,11 @@ class AdvectTest final : public AmrCore MultiFab::Multiply(flux_known[d], umac[d], 0, 0, 1, 0); } define(fc, &face_ref); - reset(fl); - HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, m_iconserv_d.dataPtr(), dt, false); + run(umac, true, m_iconserv_d.dataPtr(), false); CompareFluxes(fl, fc, flux_known, face_ref); const Gpu::DeviceScalar iconserv_flux{-1}; define(fc, &flux_ref); - reset(fl); - HydroUtilsFluxes(lev, fl, fc, umac, &umac, true, iconserv_flux.dataPtr(), dt, false); + run(umac, true, iconserv_flux.dataPtr(), false); CompareFluxes(fl, fc, flux_ref, flux_ref); } @@ -669,7 +690,7 @@ class AdvectTest final : public AmrCore Vector m_bcrec; Gpu::DeviceScalar m_iconserv_d{1}; bool m_use_hydroutils = false; - // Steps between the comparisons with PLIC, each about 7 times the fluxes of a step. Not a multiple + // Steps between the comparisons with PLIC, each about 6 times the fluxes of a step. Not a multiple // of regrid_int, to compare right after a regrid and between two int m_hydroutils_int = 10; int m_regrid_int = 4; diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index dfc7be6a..2ab0ce70 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -185,38 +185,6 @@ namespace { } } -#ifdef HYDRO_USE_PLIC - // The fluxes of the faces where u_flux differs from u_mac: edge state times u_flux. Decided face - // by face, so that the boxes and tiles sharing a face agree on its flux. - void CorrectiveFluxes (Box const& bx, - Array, AMREX_SPACEDIM> const& flux, - Array, AMREX_SPACEDIM> const& face, - Array, AMREX_SPACEDIM> const& mac, - Array, AMREX_SPACEDIM> const& corr, - Geometry const& geom, bool fluxes_are_area_weighted) - { - for (int d = 0; d < AMREX_SPACEDIM; ++d) { - if (corr[d].dataPtr() == mac[d].dataPtr()) { continue; } - Real area = Real(1.); - if (fluxes_are_area_weighted) { - for (int t = 0; t < AMREX_SPACEDIM; ++t) { - if (t != d) { area *= geom.CellSize(t); } - } - } - Array4 const fl = flux[d]; - Array4 const fc = face[d]; - Array4 const um = mac[d]; - Array4 const uf = corr[d]; - amrex::ParallelFor(amrex::surroundingNodes(bx, d), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept - { - if (uf(i,j,k) != um(i,j,k)) { - fl(i,j,k) = fc(i,j,k) * uf(i,j,k) * area; - } - }); - } - } -#endif - } // namespace #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -487,6 +455,11 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, EBCellFlagFab const& flagfab = ebfact.getMultiEBCellFlagFab()[mfi]; Array4 const& flag = flagfab.const_array(); + // The flux volumes of PLIC are not cut by embedded boundaries, so a level with cut or covered + // cells is rejected as a whole, its covered boxes included, rather than only its cut boxes. + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(advection_type != "PLIC" || ebfact.isAllRegular(), + "PLIC is not available with EB"); + // If entire box is covered, don't do anything and return if (flagfab.getType(bx) == FabType::covered) return; @@ -497,11 +470,6 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, if (advection_type == "PLIC") { #ifdef HYDRO_USE_PLIC -#if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) - // The flux volumes are not cut by embedded boundaries, so a level with cut cells is - // rejected as a whole, rather than only its cut boxes. - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ebfact.isAllRegular(), "PLIC is not available with EB"); -#endif // Also with knownFaceState, which does not reach the check in PLIC AMREX_ALWAYS_ASSERT_WITH_MESSAGE(geom.IsCartesian(), "PLIC requires Cartesian coordinates"); // The volume fraction is one scalar, advected in conservative form. Its ghost cells @@ -509,26 +477,38 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1 && !is_velocity, "PLIC advects one volume fraction, not a velocity"); AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!bc_arr, "bc_arr is only supported with (EB)Godunov"); - // Conservative (1), or the face array already holding the fluxes (-1, with - // knownFaceState). Convective (0) would be silently advected in conservative form, as - // ComputeConvectiveTerm adds nothing for "PLIC". - int h_iconserv = 0; - Gpu::dtoh_memcpy(&h_iconserv, iconserv, sizeof(int)); // iconserv is device memory - AMREX_ALWAYS_ASSERT_WITH_MESSAGE(h_iconserv == 1 || (h_iconserv == -1 && knownFaceState), - "PLIC needs iconserv = 1, or -1 with knownFaceState"); + // The check of iconserv and the Courant number of each box wait for the GPU, so that the + // boxes of the loop do not overlap: optimized GPU builds skip both, and rely on the caller + // checking each level once with PLIC::CheckCourantNumber before its MFIter loop. +#if defined(AMREX_DEBUG) || !defined(AMREX_USE_GPU) + constexpr bool check_courant = true; +#else + constexpr bool check_courant = false; +#endif + if (check_courant) { + // Conservative (1), or the face array already holding the fluxes (-1, with + // knownFaceState). Convective (0) would be silently advected in conservative form, as + // ComputeConvectiveTerm adds nothing for "PLIC". + int h_iconserv = 0; + Gpu::dtoh_memcpy(&h_iconserv, iconserv, sizeof(int)); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(h_iconserv == 1 || (h_iconserv == -1 && knownFaceState), + "PLIC needs iconserv = 1, or -1 with knownFaceState"); + } if (!knownFaceState) { PLIC::ComputeEdgeStateAndFluxes(bx, AMREX_D_DECL(flux_x,flux_y,flux_z), AMREX_D_DECL(face_x,face_y,face_z), q, AMREX_D_DECL(u_mac,v_mac,w_mac), - geom, l_dt, "myc_height", fluxes_are_area_weighted); + geom, l_dt, "myc_height", fluxes_are_area_weighted, + check_courant); // Where shear folds a flux volume the edge state is held to [0,1], and edge state * u // is not the liquid that crosses the face: keep PLIC's geometric fluxes on the faces // where u_flux is u_mac. - CorrectiveFluxes(bx, {AMREX_D_DECL(flux_x,flux_y,flux_z)}, {AMREX_D_DECL(face_x,face_y,face_z)}, - {AMREX_D_DECL(u_mac,v_mac,w_mac)}, {AMREX_D_DECL(u_flux,v_flux,w_flux)}, - geom, fluxes_are_area_weighted); + PLIC::CorrectiveFluxes(bx, AMREX_D_DECL(flux_x,flux_y,flux_z), + AMREX_D_DECL(face_x,face_y,face_z), q, + AMREX_D_DECL(u_mac,v_mac,w_mac), AMREX_D_DECL(u_flux,v_flux,w_flux), + geom, fluxes_are_area_weighted); return; } #else diff --git a/Utils/hydro_utils.H b/Utils/hydro_utils.H index 810bfab6..8b68ea3d 100644 --- a/Utils/hydro_utils.H +++ b/Utils/hydro_utils.H @@ -68,7 +68,8 @@ void enforceInOutSolvability ( * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. - * q then needs PLIC::nGrowF("myc_height") filled ghost cells. + * On GPUs the caller must check the Courant number of each level with + * PLIC::CheckCourantNumber before its MFIter loop. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -167,7 +168,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. - * q then needs PLIC::nGrowF("myc_height") filled ghost cells. + * On GPUs the caller must check the Courant number of each level with + * PLIC::CheckCourantNumber before its MFIter loop. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -230,7 +232,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. - * q then needs PLIC::nGrowF("myc_height") filled ghost cells. + * On GPUs the caller must check the Courant number of each level with + * PLIC::CheckCourantNumber before its MFIter loop. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -292,7 +295,8 @@ ComputeFluxesOnBoxFromState (amrex::Box const& bx, int ncomp, amrex::MFIter& mfi * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. - * q then needs PLIC::nGrowF("myc_height") filled ghost cells. + * On GPUs the caller must check the Courant number of each level with + * PLIC::CheckCourantNumber before its MFIter loop. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and @@ -354,7 +358,8 @@ ComputeFluxesOnBoxFromState ( amrex::Box const& bx, int ncomp, amrex::MFIter& mf * \note advection_type must be one of "Godunov", "MOL", "BDS" or "PLIC". BDS and * PLIC are not available with EB. For what "PLIC" needs from the caller, see * "Through HydroUtils" in the PLIC section of the documentation. - * q then needs PLIC::nGrowF("myc_height") filled ghost cells. + * On GPUs the caller must check the Courant number of each level with + * PLIC::CheckCourantNumber before its MFIter loop. * * \note On a level that has cut cells, the cut boxes are handled by EBGodunov, which * uses PLM and adds the forces after the transverse terms. godunov_use_ppm and From 5d71f970b264a15fc414774ad3733b469de1b36c Mon Sep 17 00:00:00 2001 From: Thomas Howarth Date: Tue, 1 Dec 2026 21:11:43 +0000 Subject: [PATCH 7/7] PLIC: fifth review fixes to advection_type "PLIC" in HydroUtils - knownFaceState: the fluxes get PLIC::CorrectiveFluxes too, so where a corrective u_flux is zero-crossing or reversed relative to u_mac, the flux takes the upwind F of u_flux rather than the given edge state (upwinded by u_mac), as without knownFaceState. With iconserv = -1 the given fluxes are kept. - PLIC::CorrectiveFluxes takes an optional device pointer to iconserv and reads it in its kernel, so HydroUtils needs no wait for the GPU per box; HydroUtils now calls it once for both paths. - PLIC::CorrectiveFluxes checks that the fluxes, edge states, u_mac and u_flux cover the faces of bx, and that F has one ghost cell: with knownFaceState the other schemes read neither F nor u_mac. - Docs: the knownFaceState correction, and that it needs u_mac and F with one ghost cell. - Tests: knownFaceState with the corrective u_flux, with iconserv = 1 (corrected) and -1 (copied). Co-Authored-By: Claude Opus 5.5 --- Docs/source/PLIC.rst | 5 ++++- PLIC/hydro_plic.H | 11 +++++++--- PLIC/hydro_plic_owkes_desjardins.cpp | 17 +++++++++++++-- Tests/PLIC/test_advect.cpp | 15 +++++++++---- Utils/hydro_compute_edgestate_and_flux.cpp | 25 ++++++++++++++-------- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Docs/source/PLIC.rst b/Docs/source/PLIC.rst index be507a94..28ed1ca4 100644 --- a/Docs/source/PLIC.rst +++ b/Docs/source/PLIC.rst @@ -154,7 +154,10 @@ and ``is_velocity = false``. It uses the ``myc_height`` reconstruction: face get the same flux. A solver that calls PLIC directly gets the same fluxes from ``PLIC::CorrectiveFluxes``; - with ``knownFaceState``, the face arrays are the edge states (``iconserv = 1``) or the fluxes - (``iconserv = -1``). Any other ``iconserv`` aborts. + (``iconserv = -1``, copied). Any other ``iconserv`` aborts. With edge states, the fluxes get the + same correction as above where ``u_flux`` differs from ``u_mac``, so pass the ``u_mac`` that + they were computed with. Unlike the other schemes, a corrective ``u_flux`` then needs ``u_mac`` + and :math:`F` with one filled ghost cell, whatever ``iconserv``. :math:`F` needs ``PLIC::nGrowF("myc_height")`` ghost cells, filled by ``PLIC::FillWallGhosts``, which carry the boundary conditions in place of the ``BCRec`` arguments; ``qnph``, ``divu``, the diff --git a/PLIC/hydro_plic.H b/PLIC/hydro_plic.H index 7062d254..9c158f3b 100644 --- a/PLIC/hydro_plic.H +++ b/PLIC/hydro_plic.H @@ -128,13 +128,17 @@ void ComputeEdgeStateAndFluxes (amrex::Box const& bx, * that the boxes sharing a face agree on its flux; a direction whose u_flux is the umac array * itself is skipped. * - * \param flux_x fluxes on the x faces of bx, from ComputeEdgeStateAndFluxes (likewise y, z) - * \param face_x edge states on the x faces of bx, from ComputeEdgeStateAndFluxes (likewise y, z) + * \param flux_x fluxes on the x faces of bx, from ComputeEdgeStateAndFluxes, or the edge states + * times umac (likewise y, z) + * \param face_x edge states on the x faces of bx, upwinded by umac, as from ComputeEdgeStateAndFluxes + * (likewise y, z) * \param F volume fraction at t^n, with one filled ghost cell * \param umac the face velocity the fluxes were computed with (likewise vmac, wmac) * \param uflux the corrective face velocity (likewise vflux, wflux) * \param geom geometry of the level * \param fluxes_are_area_weighted as passed to ComputeEdgeStateAndFluxes + * \param iconserv optional, on the device: where iconserv[0] is -1 (HydroUtils with + * knownFaceState) the fluxes are given and are kept */ void CorrectiveFluxes (amrex::Box const& bx, AMREX_D_DECL(amrex::Array4 const& flux_x, @@ -151,7 +155,8 @@ void CorrectiveFluxes (amrex::Box const& bx, amrex::Array4 const& vflux, amrex::Array4 const& wflux), amrex::Geometry const& geom, - bool fluxes_are_area_weighted = false); + bool fluxes_are_area_weighted = false, + int const* iconserv = nullptr); /** * \brief Abort unless the face Courant number, |u| dt/dx, is below 1 on every face that diff --git a/PLIC/hydro_plic_owkes_desjardins.cpp b/PLIC/hydro_plic_owkes_desjardins.cpp index 0d2cd221..505234bd 100644 --- a/PLIC/hydro_plic_owkes_desjardins.cpp +++ b/PLIC/hydro_plic_owkes_desjardins.cpp @@ -287,7 +287,8 @@ CorrectiveFluxes (amrex::Box const& bx, amrex::Array4 const& vflux, amrex::Array4 const& wflux), amrex::Geometry const& geom, - bool fluxes_are_area_weighted) + bool fluxes_are_area_weighted, + int const* iconserv) { BL_PROFILE("PLIC::CorrectiveFluxes()"); const amrex::Array, AMREX_SPACEDIM> flux{AMREX_D_DECL(flux_x, flux_y, flux_z)}; @@ -297,13 +298,25 @@ CorrectiveFluxes (amrex::Box const& bx, for (int d = 0; d < AMREX_SPACEDIM; ++d) { // The same arrays: every face keeps its geometric flux if (corr[d].dataPtr() == vel[d].dataPtr()) { continue; } + // Checked here, not only in ComputeEdgeStateAndFluxes: with knownFaceState HydroUtils calls + // this alone, where the other schemes read neither F nor umac. An empty Array4 fails too. + const amrex::Box faces = amrex::surroundingNodes(bx, d); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(detail::FaceBox(flux[d], d).contains(faces) && + detail::FaceBox(face[d], d).contains(faces) && + detail::FaceBox(vel[d], d).contains(faces) && + detail::FaceBox(corr[d], d).contains(faces), + "PLIC: CorrectiveFluxes needs the fluxes, edge states, umac and u_flux on the faces of bx"); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(amrex::Box(F).contains(amrex::grow(bx, d, 1)), + "PLIC: CorrectiveFluxes needs F with one filled ghost cell"); const amrex::Real area = detail::FaceArea(geom, d, fluxes_are_area_weighted); amrex::Array4 const fl = flux[d]; amrex::Array4 const fc = face[d]; amrex::Array4 const um = vel[d]; amrex::Array4 const uf = corr[d]; - amrex::ParallelFor(amrex::surroundingNodes(bx, d), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept + amrex::ParallelFor(faces, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept { + // Read on the device, so that the boxes of an MFIter loop do not wait for the GPU + if (iconserv && iconserv[0] == -1) { return; } const amrex::Real u = uf(i, j, k); if (u != um(i, j, k)) { const amrex::Real fe = (um(i, j, k) * u > 0.0_rt) diff --git a/Tests/PLIC/test_advect.cpp b/Tests/PLIC/test_advect.cpp index 548d0521..6a40d21a 100644 --- a/Tests/PLIC/test_advect.cpp +++ b/Tests/PLIC/test_advect.cpp @@ -444,8 +444,10 @@ class AdvectTest final : public AmrCore // - a corrective u_flux (see CorrectiveVelocity), without and with area weighting: where it // differs, PLIC's edge states times u_flux, or the upwind F of u_flux times u_flux where u_mac is // zero or of the other sign; - // - knownFaceState with iconserv = 1: the given edge states, times u_mac; - // - knownFaceState with iconserv = -1: the face arrays (PLIC's fluxes), copied. + // - knownFaceState with iconserv = 1: the given edge states, times u_mac, and with the corrective + // u_flux the same correction as without knownFaceState; + // - knownFaceState with iconserv = -1: the face arrays (PLIC's fluxes), copied, with u_mac and + // with the corrective u_flux, which is not read. // In all, 6 computations of the PLIC fluxes, besides those of the step. void CompareWithPLIC (Faces const& flux, Faces const& face, Faces const& umac, int lev, Real dt) { @@ -505,10 +507,15 @@ class AdvectTest final : public AmrCore define(fc, &face_ref); run(umac, true, m_iconserv_d.dataPtr(), false); CompareFluxes(fl, fc, flux_known, face_ref); + ExpectedFluxes(flux_corr, flux_known, face_ref, umac, ucorr, lev, false); + run(ucorr, true, m_iconserv_d.dataPtr(), false); + CompareFluxes(fl, fc, flux_corr, face_ref); const Gpu::DeviceScalar iconserv_flux{-1}; define(fc, &flux_ref); - run(umac, true, iconserv_flux.dataPtr(), false); - CompareFluxes(fl, fc, flux_ref, flux_ref); + for (Faces const* uflux : {&umac, static_cast(&ucorr)}) { + run(*uflux, true, iconserv_flux.dataPtr(), false); + CompareFluxes(fl, fc, flux_ref, flux_ref); + } } // The edge states of one step on one level, on this rank: on every face with a flow, the liquid diff --git a/Utils/hydro_compute_edgestate_and_flux.cpp b/Utils/hydro_compute_edgestate_and_flux.cpp index 2ab0ce70..46b998eb 100644 --- a/Utils/hydro_compute_edgestate_and_flux.cpp +++ b/Utils/hydro_compute_edgestate_and_flux.cpp @@ -184,7 +184,6 @@ namespace { } } } - } // namespace #if defined(AMREX_USE_EB) && !defined(HYDRO_NO_EB) @@ -502,15 +501,23 @@ HydroUtils::ComputeFluxesOnBoxFromState (Box const& bx, int ncomp, MFIter& mfi, AMREX_D_DECL(u_mac,v_mac,w_mac), geom, l_dt, "myc_height", fluxes_are_area_weighted, check_courant); - // Where shear folds a flux volume the edge state is held to [0,1], and edge state * u - // is not the liquid that crosses the face: keep PLIC's geometric fluxes on the faces - // where u_flux is u_mac. - PLIC::CorrectiveFluxes(bx, AMREX_D_DECL(flux_x,flux_y,flux_z), - AMREX_D_DECL(face_x,face_y,face_z), q, - AMREX_D_DECL(u_mac,v_mac,w_mac), AMREX_D_DECL(u_flux,v_flux,w_flux), - geom, fluxes_are_area_weighted); - return; + } else { + // The given edge states times u_flux, or the given fluxes (iconserv = -1) + HydroUtils::ComputeFluxes(bx, + AMREX_D_DECL(flux_x,flux_y,flux_z), + AMREX_D_DECL(u_flux,v_flux,w_flux), + AMREX_D_DECL(face_x,face_y,face_z), + geom, ncomp, fluxes_are_area_weighted, + iconserv); } + // On the faces where a corrective u_flux differs from u_mac: see PLIC::CorrectiveFluxes. + // Elsewhere PLIC's geometric fluxes are kept, as edge state * u is not the liquid that + // crosses a face where shear folds the flux volume. + PLIC::CorrectiveFluxes(bx, AMREX_D_DECL(flux_x,flux_y,flux_z), + AMREX_D_DECL(face_x,face_y,face_z), q, + AMREX_D_DECL(u_mac,v_mac,w_mac), AMREX_D_DECL(u_flux,v_flux,w_flux), + geom, fluxes_are_area_weighted, iconserv); + return; #else Abort("advection_type PLIC needs AMReX-Hydro built with PLIC (with GNUmake, add the PLIC " "directory to the build)");