From af654f80539973ef5b67a922a2385dd4974e70d3 Mon Sep 17 00:00:00 2001 From: Debojyoti Ghosh Date: Mon, 16 Sep 2024 19:42:17 -0700 Subject: [PATCH] Implemented implicit field solve preconditioner based on the curl-curl MLMG solver --- .../ImplicitSolvers/CMakeLists.txt | 1 + .../ImplicitSolvers/ImplicitSolver.H | 30 +- .../ImplicitSolvers/ImplicitSolver.cpp | 57 +++ .../FieldSolver/ImplicitSolvers/Make.package | 1 + .../ImplicitSolvers/ThetaImplicitEM.H | 2 +- .../ImplicitSolvers/ThetaImplicitEM.cpp | 6 +- .../ImplicitSolvers/WarpXSolverVec.H | 3 +- .../ImplicitSolvers/WarpXSolverVec.cpp | 2 + Source/NonlinearSolvers/CurlCurlMLMGPC.H | 404 ++++++++++++++++++ Source/NonlinearSolvers/JacobianFunctionMF.H | 46 +- Source/NonlinearSolvers/NewtonSolver.H | 20 +- Source/NonlinearSolvers/Preconditioner.H | 94 ++++ Source/WarpX.H | 15 + 13 files changed, 659 insertions(+), 22 deletions(-) create mode 100644 Source/FieldSolver/ImplicitSolvers/ImplicitSolver.cpp create mode 100644 Source/NonlinearSolvers/CurlCurlMLMGPC.H create mode 100644 Source/NonlinearSolvers/Preconditioner.H diff --git a/Source/FieldSolver/ImplicitSolvers/CMakeLists.txt b/Source/FieldSolver/ImplicitSolvers/CMakeLists.txt index 6e16f19084c..04abc9d3e91 100644 --- a/Source/FieldSolver/ImplicitSolvers/CMakeLists.txt +++ b/Source/FieldSolver/ImplicitSolvers/CMakeLists.txt @@ -2,6 +2,7 @@ foreach(D IN LISTS WarpX_DIMS) warpx_set_suffix_dims(SD ${D}) target_sources(lib_${SD} PRIVATE + ImplicitSolver.cpp SemiImplicitEM.cpp ThetaImplicitEM.cpp WarpXImplicitOps.cpp diff --git a/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.H b/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.H index 88ad6a058fd..a571e5814fc 100644 --- a/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.H +++ b/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.H @@ -1,4 +1,4 @@ -/* Copyright 2024 Justin Angus +/* Copyright 2024 Justin Angus, Debojyoti Ghosh * * This file is part of WarpX. * @@ -7,11 +7,13 @@ #ifndef Implicit_Solver_H_ #define Implicit_Solver_H_ -#include "FieldSolver/ImplicitSolvers/WarpXSolverVec.H" -#include "NonlinearSolvers/NonlinearSolverLibrary.H" - #include #include +#include + +#include "FieldSolver/ImplicitSolvers/WarpXSolverVec.H" +#include "NonlinearSolvers/NonlinearSolverLibrary.H" +#include "../../Utils/WarpXAlgorithmSelection.H" /** * \brief Base class for implicit time solvers. The base functions are those @@ -85,6 +87,16 @@ public: int a_nl_iter, bool a_from_jacobian ) = 0; + [[nodiscard]] virtual amrex::Real theta () const { return 1.0; } + + [[nodiscard]] int numAMRLevels () const { return m_num_amr_levels; } + + [[nodiscard]] const amrex::Geometry& GetGeometry (const int) const; + [[nodiscard]] const amrex::Array& GetFieldBoundaryLo () const; + [[nodiscard]] const amrex::Array& GetFieldBoundaryHi () const; + [[nodiscard]] const amrex::Array GetLinOpBCLo () const; + [[nodiscard]] const amrex::Array GetLinOpBCHi () const; + protected: /** @@ -94,6 +106,11 @@ protected: bool m_is_defined = false; + /** + * \brief Number of AMR levels + */ + int m_num_amr_levels = 1; + /** * \brief Nonlinear solver type and object */ @@ -140,6 +157,11 @@ protected: } + /** + * \brief Convert from WarpX FieldBoundaryType to amrex::LinOpBCType + */ + amrex::Array convertFieldBCToLinOpBC ( const amrex::Array& ) const; + }; #endif diff --git a/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.cpp b/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.cpp new file mode 100644 index 00000000000..0b28e8007c7 --- /dev/null +++ b/Source/FieldSolver/ImplicitSolvers/ImplicitSolver.cpp @@ -0,0 +1,57 @@ +#include "ImplicitSolver.H" +#include "WarpX.H" + +using namespace amrex; + +const Geometry& ImplicitSolver::GetGeometry (const int a_lvl) const +{ + AMREX_ASSERT((a_lvl >= 0) && (a_lvl < m_num_amr_levels)); + return m_WarpX->GetGeometry(a_lvl); +} + +const Array& ImplicitSolver::GetFieldBoundaryLo () const +{ + return m_WarpX->GetFieldBoundaryLo(); +} + +const Array& ImplicitSolver::GetFieldBoundaryHi () const +{ + return m_WarpX->GetFieldBoundaryHi(); +} + +const Array ImplicitSolver::GetLinOpBCLo () const +{ + return convertFieldBCToLinOpBC(m_WarpX->GetFieldBoundaryLo()); +} + +const Array ImplicitSolver::GetLinOpBCHi () const +{ + return convertFieldBCToLinOpBC(m_WarpX->GetFieldBoundaryHi()); +} + +Array ImplicitSolver::convertFieldBCToLinOpBC (const Array& a_fbc) const +{ + Array lbc; + for (int i = 0; i < AMREX_SPACEDIM; i++) { + if (a_fbc[i] == FieldBoundaryType::PML) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::Periodic) { + lbc[i] = LinOpBCType::Periodic; + } else if (a_fbc[i] == FieldBoundaryType::PEC) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::PMC) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::Damped) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::Absorbing_SilverMueller) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::Neumann) { + lbc[i] = LinOpBCType::Neumann; + } else if (a_fbc[i] == FieldBoundaryType::None) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } else if (a_fbc[i] == FieldBoundaryType::Open) { + WARPX_ABORT_WITH_MESSAGE("LinOpBCType not set for this FieldBoundaryType"); + } + } + return lbc; +} diff --git a/Source/FieldSolver/ImplicitSolvers/Make.package b/Source/FieldSolver/ImplicitSolvers/Make.package index a4543f94dd3..16cd4003490 100644 --- a/Source/FieldSolver/ImplicitSolvers/Make.package +++ b/Source/FieldSolver/ImplicitSolvers/Make.package @@ -1,3 +1,4 @@ +CEXE_sources += ImplicitSolver.cpp CEXE_sources += SemiImplicitEM.cpp CEXE_sources += ThetaImplicitEM.cpp CEXE_sources += WarpXImplicitOps.cpp diff --git a/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.H b/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.H index 009c2c7e546..ca9325f3553 100644 --- a/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.H +++ b/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.H @@ -79,7 +79,7 @@ public: int a_nl_iter, bool a_from_jacobian ) override; - [[nodiscard]] amrex::Real theta () const { return m_theta; } + [[nodiscard]] amrex::Real theta () const override { return m_theta; } private: diff --git a/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.cpp b/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.cpp index 3d74ddfde69..a0eb8e0f185 100644 --- a/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.cpp +++ b/Source/FieldSolver/ImplicitSolvers/ThetaImplicitEM.cpp @@ -19,15 +19,15 @@ void ThetaImplicitEM::Define ( WarpX* const a_WarpX ) // Retain a pointer back to main WarpX class m_WarpX = a_WarpX; + m_num_amr_levels = 1; // Define E and Eold vectors m_E.Define( m_WarpX, FieldType::Efield_fp ); m_Eold.Define( m_E ); // Define Bold MultiFab - const int num_levels = 1; - m_Bold.resize(num_levels); // size is number of levels - for (int lev = 0; lev < num_levels; ++lev) { + m_Bold.resize(m_num_amr_levels); // size is number of levels + for (int lev = 0; lev < m_num_amr_levels; ++lev) { for (int n=0; n<3; n++) { const amrex::MultiFab& Bfp = m_WarpX->getField( FieldType::Bfield_fp,lev,n); m_Bold[lev][n] = std::make_unique( Bfp.boxArray(), diff --git a/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.H b/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.H index f884f5fa623..88a189a904d 100644 --- a/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.H +++ b/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.H @@ -74,6 +74,7 @@ public: void Define ( const WarpXSolverVec& a_solver_vec ) { assertIsDefined( a_solver_vec ); + m_num_amr_levels = a_solver_vec.m_num_amr_levels; Define( WarpXSolverVec::m_WarpX, a_solver_vec.getArrayVecType(), a_solver_vec.getScalarVecType() ); @@ -291,7 +292,7 @@ private: warpx::fields::FieldType m_scalar_type = warpx::fields::FieldType::None; static constexpr int m_ncomp = 1; - static constexpr int m_num_amr_levels = 1; + int m_num_amr_levels = 1; inline static bool m_warpx_ptr_defined = false; inline static WarpX* m_WarpX = nullptr; diff --git a/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.cpp b/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.cpp index f2a88d82d42..540df46d963 100644 --- a/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.cpp +++ b/Source/FieldSolver/ImplicitSolvers/WarpXSolverVec.cpp @@ -23,6 +23,8 @@ void WarpXSolverVec::Define ( WarpX* a_WarpX, m_warpx_ptr_defined = true; } + m_num_amr_levels = 1; + m_array_type = a_array_type; m_scalar_type = a_scalar_type; diff --git a/Source/NonlinearSolvers/CurlCurlMLMGPC.H b/Source/NonlinearSolvers/CurlCurlMLMGPC.H new file mode 100644 index 00000000000..55b84aa14b6 --- /dev/null +++ b/Source/NonlinearSolvers/CurlCurlMLMGPC.H @@ -0,0 +1,404 @@ +/* Copyright 2024 Debojyoti Ghosh + * + * This file is part of WarpX. + * + * License: BSD-3-Clause-LBNL + */ +#ifndef CURL_CURL_MLMG_PC_H_ +#define CURL_CURL_MLMG_PC_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "FieldSolver/Fields.H" +#include "../Utils/WarpXConst.H" +#include "Preconditioner.H" + +/** + * \brief Curl-curl Preconditioner + * + * Preconditioner that solves the curl-curl equation for the E-field, given + * a RHS. Uses AMReX's curl-curl linear operator and multigrid solver. + * + * This class is templated on a solution-type class T and an operator class Ops. + * + * The Ops class must have the following function: + * + Return number of AMR levels + * + Return the amrex::Geometry object given an AMR level + * + Return the amrex::BoxArray object given an AMR level + * + Return the amrex::DistributionMapping object given an AMR level + * + Return hi and lo linear operator boundaries + * + Return the time step factor (theta) for the time integration scheme + * + * The T class must have the following functions: + * + Return underlying vector of amrex::MultiFab arrays + */ + +template +class CurlCurlMLMGPC : public Preconditioner +{ + public: + + using RT = typename T::value_type; + + /** + * \brief Default constructor + */ + CurlCurlMLMGPC () = default; + + /** + * \brief Default destructor + */ + ~CurlCurlMLMGPC () override = default; + + /** + * \brief Define the preconditioner + */ + virtual void Define (const T&, Ops* const) override; + + /** + * \brief Update the preconditioner + */ + virtual void Update (const T&) override; + + /** + * \brief Apply (solve) the preconditioner given a RHS + */ + virtual void Apply (T&, const T&) override; + + /** + * \brief Print parameters + */ + virtual void printParameters() const override; + + /** + * \brief Check if the nonlinear solver has been defined. + */ + [[nodiscard]] inline bool IsDefined () const override { return m_is_defined; } + + protected: + + using MFArr = amrex::Array; + + bool m_is_defined = false; + + bool m_verbose = true; + bool m_bottom_verbose = false; + bool m_agglomeration = true; + bool m_consolidation = true; + bool m_use_gmres = false; + bool m_use_gmres_pc = true; + + int m_max_iter = 300; + int m_max_coarsening_level = 30; + + RT m_atol = 1.0e-16; + RT m_rtol = 1.0e-10; + + Ops* m_ops; + + int m_num_amr_levels = 0; + amrex::Vector m_geom; + amrex::Vector m_grids; + amrex::Vector m_dmap; + amrex::IntVect m_gv; + + amrex::Array m_bc_lo; + amrex::Array m_bc_hi; + + std::unique_ptr m_info; + std::unique_ptr m_curl_curl; + std::unique_ptr> m_solver; + std::unique_ptr> m_gmres_solver; + + /** + * \brief Read parameters + */ + void readParameters(); + + /** + * \brief Define staggered-grid MultiFabs for AMReX's curl-curl operator given WarpX's fields + */ + void defineMLCCAMFFromWarpXAMF ( amrex::Array&, + const amrex::Array,3>&, + const amrex::IntVect& ); + + /** + * \brief Copy to AMReX's staggered-grid MultiFabs from WarpX's staggered-grid MultiFabs + */ + void copyMLCCAMFFromWarpXAMF ( amrex::Array&, + const amrex::Array,3>&, + const amrex::IntVect& ); + + /** + * \brief Copy to WarpX's staggered-grid MultiFabs from AMReX's staggered-grid MultiFabs + */ + void copyWarpXAMFFromMLCCAMF ( amrex::Array,3>&, + const amrex::Array&, + const amrex::IntVect& ); + + private: + +}; + +template +void CurlCurlMLMGPC::printParameters() const +{ + using namespace amrex; + Print() << PreconditionerTypes::curl_curl_mlmg << " verbose: " << (m_verbose?"true":"false") << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " bottom verbose: " << (m_bottom_verbose?"true":"false") << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " max iter: " << m_max_iter << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " agglomeration: " << m_agglomeration << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " consolidation: " << m_consolidation << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " max_coarsening_level: " << m_max_coarsening_level << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " absolute tolerance: " << m_atol << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " relative tolerance: " << m_rtol << "\n"; + Print() << PreconditionerTypes::curl_curl_mlmg << " use GMRES: " << (m_use_gmres?"true":"false") << "\n"; + if (m_use_gmres) { + Print() << PreconditionerTypes::curl_curl_mlmg + << " use PC for GMRES: " + << (m_use_gmres_pc?"true":"false") << "\n"; + } +} + +template +void CurlCurlMLMGPC::readParameters() +{ + amrex::ParmParse pp(PreconditionerTypes::curl_curl_mlmg); + pp.query("verbose", m_verbose); + pp.query("bottom_verbose", m_bottom_verbose); + pp.query("max_iter", m_max_iter); + pp.query("agglomeration", m_agglomeration); + pp.query("consolidation", m_consolidation); + pp.query("max_coarsening_level", m_max_coarsening_level); + pp.query("absolute_tolerance", m_atol); + pp.query("relative_tolerance", m_rtol); + pp.query("use_gmres", m_use_gmres); + pp.query("use_gmres_pc", m_use_gmres_pc); + + return; +} + +template +void CurlCurlMLMGPC::Define ( const T& a_U, + Ops* const a_ops ) +{ + using namespace amrex; + + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + !IsDefined(), + "CurlCurlMLMGPC::Define() called on defined object" ); + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + (a_ops != nullptr), + "CurlCurlMLMGPC::Define(): a_ops is nullptr" ); + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + a_U.getArrayVecType()==warpx::fields::FieldType::Efield_fp, + "CurlCurlMLMGPC::Define() must be called with Efield_fp type"); + + m_ops = a_ops; + // read preconditioner parameters + readParameters(); + + // create info object for curl-curl op + m_info = std::make_unique(); + m_info->setAgglomeration(m_agglomeration); + m_info->setConsolidation(m_consolidation); + m_info->setMaxCoarseningLevel(m_max_coarsening_level); + + // Get data vectors from a_U + auto& u_mfarrvec = a_U.getArrayVec(); + + // Set number of AMR levels and create geometry, grids, and + // distribution mapping vectors. + m_num_amr_levels = m_ops->numAMRLevels(); + m_geom.resize(m_num_amr_levels); + m_grids.resize(m_num_amr_levels); + m_dmap.resize(m_num_amr_levels); + for (int n = 0; n < m_num_amr_levels; n++) { + m_geom[n] = m_ops->GetGeometry(n); + m_dmap[n] = u_mfarrvec[n][0]->DistributionMap(); + + BoxArray ba = u_mfarrvec[n][0]->boxArray(); + m_grids[n] = ba.enclosedCells(); + } + + // Construct the curl-curl linear operator and set its BCs + m_curl_curl = std::make_unique(m_geom, m_grids, m_dmap, *m_info); + m_curl_curl->setDomainBC(m_ops->GetLinOpBCLo(), m_ops->GetLinOpBCHi()); + m_curl_curl->setScalars(1.0, 1.0); + + // Construct the MLMG solver + m_solver = std::make_unique>(*m_curl_curl); + m_solver->setMaxIter(m_max_iter); + m_solver->setVerbose(static_cast(m_verbose)); + m_solver->setBottomVerbose(static_cast(m_bottom_verbose)); + + // If using GMRES solver, construct it + if (m_use_gmres) { + m_gmres_solver = std::make_unique>(*m_solver); + m_gmres_solver->usePrecond(m_use_gmres_pc); + m_gmres_solver->setPrecondNumIters(m_max_iter); + m_gmres_solver->setVerbose(static_cast(m_verbose)); + } + + m_is_defined = true; + return; +} + +template +void CurlCurlMLMGPC::Update (const T& a_U) +{ + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + IsDefined(), + "CurlCurlMLMGPC::Update() called on undefined object" ); + + // a_U is not needed for a linear operator + amrex::ignore_unused(a_U); + + // set the coefficients alpha and beta for curl-curl op + RT alpha = (m_ops->theta()*this->m_dt*PhysConst::c) * (m_ops->theta()*this->m_dt*PhysConst::c); + RT beta = RT(1.0); + m_curl_curl->setScalars(alpha, beta); + if (m_verbose) { + amrex::Print() << "Updating " << PreconditionerTypes::curl_curl_mlmg + << ": dt = " << this->m_dt << ", " + << " coefficients: " + << "alpha = " << alpha << ", " + << "beta = " << beta << "\n"; + } + + return; +} + +template +void CurlCurlMLMGPC::defineMLCCAMFFromWarpXAMF ( amrex::Array& a_mfarr_mlcc, + const amrex::Array,3>& a_mfarr_warpx, + const amrex::IntVect& ng ) +{ +#if defined(WARPX_DIM_1D_Z) + WARPX_ABORT_WITH_MESSAGE("CurlCurlMLMGPC::defineMLCCAMFFromWarpXAMF not yet implemented for 1D"); +#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ) + // In WarpX, missing dimension is y + // In AMReX, missing dimension is z + { + a_mfarr_mlcc[0].define( a_mfarr_warpx[0]->boxArray(), + a_mfarr_warpx[0]->DistributionMap(), + a_mfarr_warpx[0]->nComp(), + ng ); + } + { + a_mfarr_mlcc[1].define( a_mfarr_warpx[2]->boxArray(), + a_mfarr_warpx[2]->DistributionMap(), + a_mfarr_warpx[2]->nComp(), + ng ); + } + { + a_mfarr_mlcc[2].define( a_mfarr_warpx[1]->boxArray(), + a_mfarr_warpx[1]->DistributionMap(), + a_mfarr_warpx[1]->nComp(), + ng ); + } +#elif defined(WARPX_DIM_3D) + for (int d = 0; d < 3; d++) { + a_mfarr_mlcc[d].define( a_mfarr_warpx[d]->boxArray(), + a_mfarr_warpx[d]->DistributionMap(), + a_mfarr_warpx[d]->nComp(), + ng ); + } +#endif +} + +template +void CurlCurlMLMGPC::copyMLCCAMFFromWarpXAMF ( amrex::Array& a_dst, + const amrex::Array,3>& a_src, + const amrex::IntVect& ng ) +{ +#if defined(WARPX_DIM_1D_Z) + WARPX_ABORT_WITH_MESSAGE("CurlCurlMLMGPC::copyMLCCAMFFromWarpXAMF not yet implemented for 1D"); +#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ) + // In WarpX, missing dimension is y + // In AMReX, missing dimension is z + amrex::MultiFab::Copy( a_dst[0], *(a_src[0]), 0, 0, a_dst[0].nComp(), ng); + amrex::MultiFab::Copy( a_dst[1], *(a_src[2]), 0, 0, a_dst[0].nComp(), ng); + amrex::MultiFab::Copy( a_dst[2], *(a_src[1]), 0, 0, a_dst[0].nComp(), ng); +#elif defined(WARPX_DIM_3D) + for (int d = 0; d < 3; d++) { + amrex::MultiFab::Copy( a_dst[d], *(a_src[d]), 0, 0, ncomp, ng); + } +#endif +} + +template +void CurlCurlMLMGPC::copyWarpXAMFFromMLCCAMF ( amrex::Array,3>& a_dst, + const amrex::Array& a_src, + const amrex::IntVect& ng ) +{ +#if defined(WARPX_DIM_1D_Z) + WARPX_ABORT_WITH_MESSAGE("CurlCurlMLMGPC::copyMLCCAMFFromWarpXAMF not yet implemented for 1D"); +#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ) + // In WarpX, missing dimension is y + // In AMReX, missing dimension is z + amrex::MultiFab::Copy( *(a_dst[0]), a_src[0], 0, 0, a_dst[0]->nComp(), ng); + amrex::MultiFab::Copy( *(a_dst[1]), a_src[2], 0, 0, a_dst[0]->nComp(), ng); + amrex::MultiFab::Copy( *(a_dst[2]), a_src[1], 0, 0, a_dst[0]->nComp(), ng); +#elif defined(WARPX_DIM_3D) + for (int d = 0; d < 3; d++) { + amrex::MultiFab::Copy( *(a_dst[d]), a_src[d], 0, 0, ncomp, ng); + } +#endif +} + +template +void CurlCurlMLMGPC::Apply (T& a_x, const T& a_b) +{ + using namespace amrex; + + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + IsDefined(), + "CurlCurlMLMGPC::Apply() called on undefined object" ); + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + a_x.getArrayVecType()==warpx::fields::FieldType::Efield_fp, + "CurlCurlMLMGPC::Apply() - a_x must be Efield_fp type"); + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + a_b.getArrayVecType()==warpx::fields::FieldType::Efield_fp, + "CurlCurlMLMGPC::Apply() - a_b must be Efield_fp type"); + + // Get the data vectors + auto& b_mfarrvec = a_b.getArrayVec(); + auto& x_mfarrvec = a_x.getArrayVec(); + WARPX_ALWAYS_ASSERT_WITH_MESSAGE( + ((b_mfarrvec.size() == m_num_amr_levels) && (x_mfarrvec.size() == m_num_amr_levels)), + "Error in CurlCurlMLMGPC::Apply() - mismatch in number of levels." ); + + for (int n = 0; n < m_num_amr_levels; n++) { + + // Copy initial guess to local object + Array solution, rhs; + defineMLCCAMFFromWarpXAMF(solution, x_mfarrvec[n], IntVect::TheUnitVector()); + defineMLCCAMFFromWarpXAMF(rhs, b_mfarrvec[n], IntVect::TheZeroVector()); + copyMLCCAMFFromWarpXAMF(solution, x_mfarrvec[n], IntVect::TheZeroVector()); + copyMLCCAMFFromWarpXAMF(rhs, b_mfarrvec[n], IntVect::TheZeroVector()); + + m_curl_curl->prepareRHS({&rhs}); + if (m_use_gmres) { + m_gmres_solver->solve(solution, rhs, m_rtol, m_atol); + } else { + m_solver->solve({&solution}, {&rhs}, m_rtol, m_atol); + } + + // Copy solution in local object to a_x + copyWarpXAMFFromMLCCAMF(x_mfarrvec[n], solution, IntVect::TheZeroVector()); + + } + + return; +} + +#endif diff --git a/Source/NonlinearSolvers/JacobianFunctionMF.H b/Source/NonlinearSolvers/JacobianFunctionMF.H index d5c2b6cbac9..7e13ee9d036 100644 --- a/Source/NonlinearSolvers/JacobianFunctionMF.H +++ b/Source/NonlinearSolvers/JacobianFunctionMF.H @@ -7,6 +7,9 @@ #ifndef JacobianFunctionMF_H_ #define JacobianFunctionMF_H_ +#include +#include "CurlCurlMLMGPC.H" + /** * \brief This is a linear function class for computing the action of a * Jacobian on a vector using a matrix-free finite-difference method. @@ -35,14 +38,18 @@ class JacobianFunctionMF inline void precond ( T& a_U, const T& a_X ) { - if (m_usePreCond) { a_U.zero(); } - else { a_U.Copy(a_X); } + if (m_usePreCond) { + a_U.zero(); + m_preCond->Apply(a_U, a_X); + } else { + a_U.Copy(a_X); + } } inline void updatePreCondMat ( const T& a_X ) { - amrex::ignore_unused(a_X); + if (m_usePreCond) m_preCond->Update(a_X); } inline @@ -133,15 +140,25 @@ class JacobianFunctionMF void curTime ( RT a_time ) { m_cur_time = a_time; + if (m_usePreCond) m_preCond->CurTime(a_time); } inline void curTimeStep ( RT a_dt ) { m_dt = a_dt; + if (m_usePreCond) m_preCond->CurTimeStep(a_dt); + } + + inline + void printParams () const + { + if (m_pc_type != "none") { + m_preCond->printParameters(); + } } - void define( const T&, Ops* ); + void define( const T&, Ops* const, const std::string& ); private: @@ -151,16 +168,18 @@ class JacobianFunctionMF RT m_epsJFNK = RT(1.0e-6); RT m_normY0; RT m_cur_time, m_dt; + std::string m_pc_type; T m_Z, m_Y0, m_R0, m_R; Ops* m_ops; - + Preconditioner* m_preCond; }; template void JacobianFunctionMF::define ( const T& a_U, - Ops* a_ops ) + Ops* a_ops, + const std::string& a_pc_type ) { m_Z.Define(a_U); m_Y0.Define(a_U); @@ -169,6 +188,21 @@ void JacobianFunctionMF::define ( const T& a_U, m_ops = a_ops; + m_preCond = nullptr; + m_usePreCond = (a_pc_type != "none"); + if (m_usePreCond) { + m_pc_type = a_pc_type; + if (m_pc_type == PreconditionerTypes::curl_curl_mlmg) { + m_preCond = new CurlCurlMLMGPC(); + } else { + std::stringstream convergenceMsg; + convergenceMsg << "JacobianFunctionMF::define(): " << m_pc_type << + " is not a valid preconditioner type."; + WARPX_ABORT_WITH_MESSAGE(convergenceMsg.str()); + } + m_preCond->Define(a_U, a_ops); + } + m_is_defined = true; } diff --git a/Source/NonlinearSolvers/NewtonSolver.H b/Source/NonlinearSolvers/NewtonSolver.H index 742e139a5f5..8ec1cee2170 100644 --- a/Source/NonlinearSolvers/NewtonSolver.H +++ b/Source/NonlinearSolvers/NewtonSolver.H @@ -79,6 +79,9 @@ public: amrex::Print() << "GMRES max iterations: " << m_gmres_maxits << "\n"; amrex::Print() << "GMRES relative tolerance: " << m_gmres_rtol << "\n"; amrex::Print() << "GMRES absolute tolerance: " << m_gmres_atol << "\n"; + amrex::Print() << "Preconditioner type: " << m_pc_type << "\n"; + + m_linear_function->printParams(); } private: @@ -138,9 +141,12 @@ private: */ int m_gmres_restart_length = 30; + /** + * \brief Preconditioner type + */ + std::string m_pc_type = "none"; + mutable amrex::Real m_cur_time, m_dt; - mutable bool m_update_pc = false; - mutable bool m_update_pc_init = false; /** * \brief The linear function used by GMRES to compute A*v. @@ -184,7 +190,7 @@ void NewtonSolver::Define ( const Vec& a_U, m_ops = a_ops; m_linear_function = std::make_unique>(); - m_linear_function->define(m_F, m_ops); + m_linear_function->define(m_F, m_ops, m_pc_type); m_linear_solver = std::make_unique>>(); m_linear_solver->define(*m_linear_function); @@ -212,6 +218,9 @@ void NewtonSolver::ParseParameters () pp_gmres.query("absolute_tolerance", m_gmres_atol); pp_gmres.query("relative_tolerance", m_gmres_rtol); pp_gmres.query("max_iterations", m_gmres_maxits); + + const amrex::ParmParse pp_jac("jacobian"); + pp_jac.query("pc_type", m_pc_type); } template @@ -330,10 +339,7 @@ void NewtonSolver::EvalResidual ( Vec& a_F, m_linear_function->setBaseRHS(m_R); // update preconditioner - if (m_update_pc || m_update_pc_init) { - m_linear_function->updatePreCondMat(a_U); - } - m_update_pc_init = false; + m_linear_function->updatePreCondMat(a_U); // Compute residual: F(U) = U - b - R(U) a_F.Copy(a_U); diff --git a/Source/NonlinearSolvers/Preconditioner.H b/Source/NonlinearSolvers/Preconditioner.H new file mode 100644 index 00000000000..9d23c310a65 --- /dev/null +++ b/Source/NonlinearSolvers/Preconditioner.H @@ -0,0 +1,94 @@ +/* Copyright 2024 Debojyoti Ghosh + * + * This file is part of WarpX. + * + * License: BSD-3-Clause-LBNL + */ +#ifndef WARPX_PRECONDITIONER_H_ +#define WARPX_PRECONDITIONER_H_ + +/** + * \brief Types for preconditioners for field solvers + */ +namespace PreconditionerTypes +{ + /** + * MLMG-based solver for the curl-curl operator + */ + const std::string curl_curl_mlmg = "pc_curl_curl_mlmg"; +} + +/** + * \brief Base class for preconditioners + * + * This class is templated on a solution-type class T and an operator class Ops. + * + * The Ops class must have the following function: + * (this will depend on the specific preconditioners inheriting from this class) + * + * The T class must have the following functions: + * (this will depend on the specific preconditioners inheriting from this class) + */ + +template +class Preconditioner +{ + public: + + using RT = typename T::value_type; + + /** + * \brief Default constructor + */ + Preconditioner () = default; + + /** + * \brief Default destructor + */ + virtual ~Preconditioner () = default; + + /** + * \brief Define the preconditioner + */ + virtual void Define (const T&, Ops* const) = 0; + + /** + * \brief Update the preconditioner + */ + virtual void Update ( const T& ) = 0; + + /** + * \brief Apply (solve) the preconditioner given a RHS + */ + virtual void Apply (T&, const T&) = 0; + + /** + * \brief Check if the nonlinear solver has been defined. + */ + virtual bool IsDefined () const = 0; + + /** + * \brief Print parameters + */ + virtual void printParameters() const { } + + /** + * \brief Set the current time. + */ + inline void CurTime (const RT a_time) { m_time = a_time; } + + /** + * \brief Set the current time step size. + */ + inline void CurTimeStep (const RT a_dt) { m_dt = a_dt; } + + protected: + + RT m_time = 0.0; + RT m_dt = 0.0; + + private: + +}; + +#endif diff --git a/Source/WarpX.H b/Source/WarpX.H index a89ffe20573..912d87f986c 100644 --- a/Source/WarpX.H +++ b/Source/WarpX.H @@ -113,6 +113,21 @@ public: [[nodiscard]] int Verbose () const { return verbose; } + [[nodiscard]] const amrex::Geometry& GetGeometry ( const int a_lev ) const + { + return GetInstance().Geom(a_lev); + } + + [[nodiscard]] const amrex::Array& GetFieldBoundaryLo () const + { + return field_boundary_lo; + } + + [[nodiscard]] const amrex::Array& GetFieldBoundaryHi () const + { + return field_boundary_hi; + } + void InitData (); void Evolve (int numsteps = -1);