Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_MLMG.H
Go to the documentation of this file.
1#ifndef AMREX_ML_MG_H_
2#define AMREX_ML_MG_H_
3#include <AMReX_Config.H>
4#include <AMReX_Enum.H>
5
6#include <AMReX_MLLinOp.H>
7#include <AMReX_MLCGSolver.H>
8
9#include <functional>
10#include <limits>
11#include <optional>
12
13namespace amrex {
14
21
22// Norm used to evaluate the target convergence criteria
24
35
37template <typename MF>
38class MLMGT
39{
40public:
41
42 class error
43 : public std::runtime_error
44 {
45 public :
46 using std::runtime_error::runtime_error;
47 };
48
49 template <typename T> friend class MLCGSolverT;
50 template <typename M> friend class GMRESMLMGT;
51 template <typename T> friend class MLMGT;
52
53 using MFType = MF;
54 using FAB = typename MLLinOpT<MF>::FAB;
55 using RT = typename MLLinOpT<MF>::RT;
56
57 using BCMode = typename MLLinOpT<MF>::BCMode;
59
62 enum class CFStrategy : int {none,ghostnodes};
63
66
67 MLMGT (MLMGT<MF> const&) = delete;
68 MLMGT (MLMGT<MF> &&) = delete;
69 MLMGT<MF>& operator= (MLMGT<MF> const&) = delete;
71
82 template <typename AMF>
83 RT solve (const Vector<AMF*>& a_sol, const Vector<AMF const*>& a_rhs,
84 RT a_tol_rel, RT a_tol_abs, const char* checkpoint_file = nullptr);
85
96 template <typename AMF>
97 RT solve (std::initializer_list<AMF*> a_sol,
98 std::initializer_list<AMF const*> a_rhs,
99 RT a_tol_rel, RT a_tol_abs, const char* checkpoint_file = nullptr);
100
110 RT precond (Vector<MF*> const& a_sol, Vector<MF const*> const& a_rhs,
111 RT a_tol_rel, RT a_tol_abs);
112
119 template <typename AMF>
121 Location a_loc = Location::FaceCenter);
122
129 template <typename AMF>
130 void getGradSolution (std::initializer_list<Array<AMF*,AMREX_SPACEDIM>> a_grad_sol,
131 Location a_loc = Location::FaceCenter);
132
139 template <typename AMF>
141 Location a_loc = Location::FaceCenter);
142
149 template <typename AMF>
150 void getFluxes (std::initializer_list<Array<AMF*,AMREX_SPACEDIM>> a_flux,
151 Location a_loc = Location::FaceCenter);
152
160 template <typename AMF>
162 const Vector<AMF*> & a_sol,
163 Location a_loc = Location::FaceCenter);
164
172 template <typename AMF>
173 void getFluxes (std::initializer_list<Array<AMF*,AMREX_SPACEDIM>> a_flux,
174 std::initializer_list<AMF*> a_sol,
175 Location a_loc = Location::FaceCenter);
176
183 template <typename AMF>
184 void getFluxes (const Vector<AMF*> & a_flux,
185 Location a_loc = Location::CellCenter);
186
193 template <typename AMF>
194 void getFluxes (std::initializer_list<AMF*> a_flux,
195 Location a_loc = Location::CellCenter);
196
204 template <typename AMF>
205 void getFluxes (const Vector<AMF*> & a_flux,
206 const Vector<AMF*> & a_sol,
207 Location a_loc = Location::CellCenter);
208
216 template <typename AMF>
217 void getFluxes (std::initializer_list<AMF*> a_flux,
218 std::initializer_list<AMF*> a_sol,
219 Location a_loc = Location::CellCenter);
220
228 void compResidual (const Vector<MF*>& a_res, const Vector<MF*>& a_sol,
229 const Vector<MF const*>& a_rhs);
230
231#ifdef AMREX_USE_EB
237 void getEBFluxes (const Vector<MF*>& a_eb_flux);
244 void getEBFluxes (const Vector<MF*>& a_eb_flux, const Vector<MF*> & a_sol);
245#endif
246
252 void apply (const Vector<MF*>& out, const Vector<MF*>& in);
253
260 void applyPrecond (const Vector<MF*>& out, const Vector<MF*>& in);
261
262 [[nodiscard]] int getVerbose () const { return verbose; }
263 [[nodiscard]] int getBottomVerbose () const { return bottom_verbose; }
264
269
277 void setThrowException (bool t) noexcept { throw_exception = t; }
283 void setVerbose (int v) noexcept { verbose = v; }
289 void setMaxIter (int n) noexcept { max_iters = n; }
295 void setMaxFmgIter (int n) noexcept { max_fmg_iters = n; }
302 void setFixedIter (int nit) noexcept { do_fixed_number_of_iters = nit; }
309 void setPrecondIter (int nit) noexcept { max_precond_iters = nit; }
310
316 void setPreSmooth (int n) noexcept { nu1 = n; }
322 void setPostSmooth (int n) noexcept { nu2 = n; }
328 void setFinalSmooth (int n) noexcept { nuf = n; }
334 void setBottomSmooth (int n) noexcept { nub = n; }
335
341 void setBottomSolver (BottomSolver s) noexcept { bottom_solver = s; }
342 [[nodiscard]] BottomSolver getBottomSolver () const noexcept { return bottom_solver; }
343
345 void setMultigridType (MultigridType t) noexcept { multigrid_type = t; }
346 [[nodiscard]] MultigridType getMultigridType () const noexcept { return multigrid_type; }
349 [[nodiscard]] bool usedAlgMG () const noexcept { return m_algmg_active; }
350
361 void setAlgMGOptions (std::function<void(AlgMG<RT>&)> f) {
362 m_algmg_options = std::move(f);
363 m_algmg_bottom.reset();
364 m_algmg_level0.reset();
365 }
366
372 void setHybridStallCriterion (int window, RT rate) noexcept {
373 hybrid_stall_window = window; hybrid_stall_rate = rate;
374 }
375
377 void setHybridDivergenceFactor (RT f) noexcept { hybrid_divergence_factor = f; }
383 void setCFStrategy (CFStrategy a_cf_strategy) noexcept {cf_strategy = a_cf_strategy;}
389 void setBottomVerbose (int v) noexcept { bottom_verbose = v; }
395 void setBottomMaxIter (int n) noexcept { bottom_maxiter = n; }
401 void setBottomTolerance (RT t) noexcept { bottom_reltol = t; bottom_reltol_set = true; }
407 void setBottomToleranceAbs (RT t) noexcept { bottom_abstol = t;}
408 [[nodiscard]] RT getBottomToleranceAbs () const noexcept{ return bottom_abstol; }
409
410 [[deprecated("Use MLMG::setConvergenceNormType() instead.")]]
416 void setAlwaysUseBNorm (int flag) noexcept;
417
423 void setConvergenceNormType (MLMGNormType norm) noexcept { norm_type = norm; }
424
430 void setFinalFillBC (int flag) noexcept { final_fill_bc = flag; }
431
432 [[nodiscard]] int numAMRLevels () const noexcept { return namrlevs; }
433
439 void setNSolve (int flag) noexcept { do_nsolve = flag; }
445 void setNSolveGridSize (int s) noexcept { nsolve_grid_size = s; }
446
462 void setNoGpuSync (bool do_not_sync) noexcept { do_no_sync_gpu = do_not_sync; }
463
464#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
470 void setHypreInterface (Hypre::Interface f) noexcept {
471 // must use ij interface for EB
472#ifndef AMREX_USE_EB
473 hypre_interface = f;
474#else
476#endif
477 }
478
484 void setHypreOptionsNamespace(const std::string& prefix) noexcept
485 {
486 hypre_options_namespace = prefix;
487 }
488
490 void setHypreOldDefault (bool l) noexcept {hypre_old_default = l;}
492 void setHypreRelaxType (int n) noexcept {hypre_relax_type = n;}
494 void setHypreRelaxOrder (int n) noexcept {hypre_relax_order = n;}
496 void setHypreNumSweeps (int n) noexcept {hypre_num_sweeps = n;}
498 void setHypreStrongThreshold (Real t) noexcept {hypre_strong_threshold = t;}
499#endif
500
507
514 template <typename AMF>
515 void prepareForSolve (Vector<AMF*> const& a_sol, Vector<AMF const*> const& a_rhs);
516
518 template <typename AMF>
519 void prepareSolRHS (Vector<AMF*> const& a_sol, Vector<AMF const*> const& a_rhs);
520
523
526
529
532
538 void oneIter (int iter);
539
541 void spIter (int iter);
544
550 void miniCycle (int amrlev);
551
558 void mgVcycle (int amrlev, int mglev);
560 void mgFcycle ();
561
563 void bottomSolve ();
571 void NSolve (MLMGT<MF>& a_solver, MF& a_sol, MF& a_rhs);
574
583 void postCG (int ret, int niters = -1);
584
590 void computeMLResidual (int amrlevmax);
596 void computeResidual (int alev);
603 void computeResWithCrseSolFineCor (int calev, int falev);
615 void interpCorrection (int alev);
622 void interpCorrection (int alev, int mglev);
629 void addInterpCorrection (int alev, int mglev);
630
637 void computeResOfCorrection (int amrlev, int mglev);
638
645 RT ResNormInf (int alev, bool local = false);
652 RT MLResNormInf (int alevmax, bool local = false);
658 RT MLRhsNormInf (bool local = false);
659
669 void makeSolvable (int amrlev, int mglev, MF& mf);
670
671#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
679 template <class TMF=MF>
680 requires (std::same_as<TMF,MultiFab>)
681 void bottomSolveWithHypre (MF& x, const MF& b);
682#endif
683
684#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
692 template <class TMF=MF>
693 requires (std::same_as<TMF,MultiFab>)
694 void bottomSolveWithPETSc (MF& x, const MF& b);
695#endif
696
705 int bottomSolveWithCG (MF& x, const MF& b, typename MLCGSolverT<MF>::Type type);
706
708 template <class TMF=MF>
709 requires (std::same_as<TMF,MultiFab>)
710 void bottomSolveWithAlgMG (MF& x, const MF& b);
711
714 template <class TMF=MF>
715 requires (std::same_as<TMF,MultiFab>)
717
718 template <class TMF=MF>
719 requires (std::same_as<TMF,MultiFab>)
721 void configureAlgMG (MLAlgMG& s, bool singular, bool with_options);
722
723 template <class TMF=MF>
724 requires (std::same_as<TMF,MultiFab>)
726 void algmgSolve (MLAlgMG& s, MF& x, MF const& b);
727
730 [[nodiscard]] bool hybridShouldSwitch (Vector<RT> const& norms, RT max_norm,
731 RT best_norm, std::string& reason) const;
732
733 [[nodiscard]] RT getInitRHS () const noexcept { return m_rhsnorm0; }
734 // Initial composite residual
735 [[nodiscard]] RT getInitResidual () const noexcept { return m_init_resnorm0; }
736 // Final composite residual
737 [[nodiscard]] RT getFinalResidual () const noexcept { return m_final_resnorm0; }
738 // Residuals on the *finest* AMR level after each iteration
739 [[nodiscard]] Vector<RT> const& getResidualHistory () const noexcept { return m_iter_fine_resnorm0; }
740 [[nodiscard]] int getNumIters () const noexcept { return m_iter_fine_resnorm0.size(); }
741 [[nodiscard]] Vector<int> const& getNumCGIters () const noexcept { return m_niters_cg; }
742
743 MLLinOpT<MF>& getLinOp () { return linop; }
744
745private:
746
747 bool precond_mode = false;
748 bool throw_exception = false;
749 int verbose = 1;
750
751 int max_iters = 200;
752 int do_fixed_number_of_iters = 0;
753 int max_precond_iters = 1;
754
755 int nu1 = 2;
756 int nu2 = 2;
757 int nuf = 8;
758 int nub = 0;
759
760 int max_fmg_iters = 0;
761
762 BottomSolver bottom_solver = BottomSolver::Default;
763 CFStrategy cf_strategy = CFStrategy::none;
764 int bottom_verbose = 0;
765 int bottom_maxiter = 200;
766 RT bottom_reltol = std::is_same<RT,double>() ? RT(1.e-4) : RT(1.e-3);
767 bool bottom_reltol_set = false; // by the user, not the default
768 RT bottom_abstol = RT(-1.0);
769
771
773 bool m_algmg_active = false;
774 int hybrid_stall_window = 4;
775 RT hybrid_stall_rate = RT(0.8);
776 RT hybrid_divergence_factor = RT(10.0);
777 RT m_algmg_reltol = RT(0);
778 std::function<void(AlgMG<RT>&)> m_algmg_options;
779 std::unique_ptr<MLAlgMG> m_algmg_bottom;
780 std::unique_ptr<MLAlgMG> m_algmg_level0;
781
782 int final_fill_bc = 0;
783
784 MLLinOpT<MF>& linop;
785 int ncomp;
786 int namrlevs;
787 int finest_amr_lev;
788
789 bool linop_prepared = false;
790 Long solve_called = 0;
791
793 int do_nsolve = false;
794 int nsolve_grid_size = 16;
795 std::unique_ptr<MLLinOpT<MF>> ns_linop;
796 std::unique_ptr<MLMGT<MF>> ns_mlmg;
797 std::unique_ptr<MF> ns_sol;
798 std::unique_ptr<MF> ns_rhs;
799
801 std::unique_ptr<MLMGT<fMultiFab>> m_sp_mlmg;
802 int m_sp_linop_count = 0;
803
804 [[nodiscard]] IntVect ngSol () const {
805 IntVect ng(1);
806 if (linop.hasHiddenDimension()) { ng[linop.hiddenDirection()] = 0; }
807 return ng;
808 }
809
810 std::string print_ident;
811
812 bool do_no_sync_gpu = false;
813
815#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
816 // Hypre::Interface hypre_interface = Hypre::Interface::structed;
817 // Hypre::Interface hypre_interface = Hypre::Interface::semi_structed;
818 Hypre::Interface hypre_interface = Hypre::Interface::ij;
819
820 std::unique_ptr<Hypre> hypre_solver;
821 std::unique_ptr<MLMGBndryT<MF>> hypre_bndry;
822 std::unique_ptr<HypreNodeLap> hypre_node_solver;
823
824 std::string hypre_options_namespace = "hypre";
825 bool hypre_old_default = HypreDefaults::old_default;
826 int hypre_relax_type = HypreDefaults::relax_type;
827 int hypre_relax_order = HypreDefaults::relax_order;
828 int hypre_num_sweeps = 2; // Sweeps on each level
829 Real hypre_strong_threshold = 0.25; // HYPRE default is 0.25
830#endif
831
833#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
834 std::unique_ptr<PETScABecLap> petsc_solver;
835 std::unique_ptr<MLMGBndryT<MF>> petsc_bndry;
836#endif
837
842 Vector<MF> sol;
843 Vector<MF> rhs;
845
846 Vector<int> sol_is_alias;
847
852 Vector<Vector<MF> > res;
853 Vector<Vector<MF> > cor;
854 Vector<Vector<MF> > cor_hold;
855 Vector<Vector<MF> > rescor;
857
858 enum timer_types { solve_time=0, iter_time, bottom_time, ntimers };
859 Vector<double> timer;
860
861 RT m_rhsnorm0 = RT(-1.0);
862 RT m_init_resnorm0 = RT(-1.0);
863 RT m_final_resnorm0 = RT(-1.0);
864 Vector<int> m_niters_cg;
865 Vector<RT> m_iter_fine_resnorm0; // Residual for each iteration at the finest level
866
876 void checkPoint (const Vector<MultiFab*>& a_sol,
877 const Vector<MultiFab const*>& a_rhs,
878 RT a_tol_rel, RT a_tol_abs, const char* a_file_name) const;
879
880};
881
882template <typename MF>
884 : linop(a_lp), ncomp(a_lp.getNComp()), namrlevs(a_lp.NAMRLevels()),
885 finest_amr_lev(a_lp.NAMRLevels()-1)
886{}
887
888template <typename MF> MLMGT<MF>::~MLMGT () = default;
889
890template <typename MF>
891void
893{
894 if (flag) {
895 norm_type = MLMGNormType::bnorm;
896 } else {
897 norm_type = MLMGNormType::greater;
898 }
899}
900
901template <typename MF>
902template <typename AMF>
903auto
904MLMGT<MF>::solve (std::initializer_list<AMF*> a_sol,
905 std::initializer_list<AMF const*> a_rhs,
906 RT a_tol_rel, RT a_tol_abs, const char* checkpoint_file) -> RT
907{
908 return solve(Vector<AMF*>(std::move(a_sol)),
909 Vector<AMF const*>(std::move(a_rhs)),
910 a_tol_rel, a_tol_abs, checkpoint_file);
911}
912
913template <typename MF>
914template <typename AMF>
915auto
917 RT a_tol_rel, RT a_tol_abs, const char* checkpoint_file) -> RT
918{
919 BL_PROFILE("MLMG::solve()");
920
921 // If requested with setNoGpuSync(true), run the whole solve in a
922 // single-stream, no-implicit-sync region. The RAII objects restore the
923 // previous state (also when an exception is thrown), and SyncAtExitOnly
924 // synchronizes the GPU streams on exit unless the caller was already in
925 // a NoSync region. The order matters:
926 // no_sync_region is destroyed last, i.e., after the single stream region
927 // has been popped, so that all streams are synchronized.
928 std::optional<Gpu::SyncAtExitOnly> no_sync_region;
929 std::optional<Gpu::SingleStreamRegion> single_stream_region;
930 if (do_no_sync_gpu) {
931 no_sync_region.emplace();
932 single_stream_region.emplace();
933 }
934
935 if constexpr (std::is_same<AMF,MultiFab>()) {
936 if (checkpoint_file != nullptr) {
937 checkPoint(a_sol, a_rhs, a_tol_rel, a_tol_abs, checkpoint_file);
938 }
939 }
940
941 if ((bottom_solver == BottomSolver::custom) && !linop.supportCustomBottomSolver()) {
942 bottom_solver = BottomSolver::Default;
943 }
944
945 if (bottom_solver == BottomSolver::Default) {
946 bottom_solver = linop.getDefaultBottomSolver();
947 }
948
949 // The assembled EB stencils support at most third order.
950 bool eb_linop = false;
951#ifdef AMREX_USE_EB
952 if constexpr (IsFabArray_v<AMF>) {
953 eb_linop = dynamic_cast<EBFArrayBoxFactory const*>(linop.Factory(0)) != nullptr;
954 }
955#endif
956 amrex::ignore_unused(eb_linop);
957
958#if (defined(AMREX_USE_HYPRE) || defined(AMREX_USE_PETSC)) && (AMREX_SPACEDIM > 1)
959 if constexpr (IsFabArray_v<AMF>) {
960 if (bottom_solver == BottomSolver::hypre || bottom_solver == BottomSolver::petsc) {
961 if (eb_linop) {
962 linop.setMaxOrder(std::min(3, linop.getMaxOrder()));
963 }
964#ifdef AMREX_USE_HYPRE
965 else if (bottom_solver == BottomSolver::hypre && hypre_interface != Hypre::Interface::ij) {
966 linop.setMaxOrder(std::min(3, linop.getMaxOrder())); // 4 needs the IJ interface
967 }
968#endif
969 }
970 }
971#endif
972 if (bottom_solver == BottomSolver::algmg || multigrid_type != MultigridType::geometric) {
973 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(linop.supportsAlgMG() && ncomp == 1,
974 "MLMG: AlgMG is not available for this operator (single-component cell and nodal operators only)");
975 // The algebraic correction has zero ghost cells.
976 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(cf_strategy != CFStrategy::ghostnodes || linop.getNGrow() == 0,
977 "MLMG: AlgMG does not support the ghostnodes strategy with ghost cells");
978 // The matrix must match the operator.
979 if (eb_linop) {
980 linop.setMaxOrder(std::min(3, linop.getMaxOrder()));
981 }
982 }
983
984 bool is_nsolve = linop.m_parent;
985
986 m_algmg_active = (multigrid_type == MultigridType::algebraic);
987 const bool hybrid = (multigrid_type == MultigridType::hybrid) && !is_nsolve;
988 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!(do_nsolve && multigrid_type != MultigridType::geometric),
989 "MLMG: NSolve requires the geometric multigrid type");
990 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!(hybrid && do_fixed_number_of_iters != 0),
991 "MLMG: the hybrid multigrid type needs a convergence test; as a preconditioner or with a fixed number of iterations use geometric or algebraic");
992
993 // Mixed precision: the residual and the solution update are in double,
994 // the multigrid cycles run on the operator's single precision copy.
995 const bool mixed = (linop.m_sp_linop != nullptr);
996 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!linop.info.mixed_precision || (mixed && linop.supportsMixedPrecision()),
997 "MLMG: this operator does not support mixed precision");
998 if (mixed) {
1000 && cf_strategy != CFStrategy::ghostnodes,
1001 "MLMG: mixed precision needs geometric multigrid without ghost nodes");
1003 && bottom_solver != BottomSolver::petsc
1004 && bottom_solver != BottomSolver::algmg
1005 && bottom_solver != BottomSolver::custom,
1006 "MLMG: mixed precision supports the smoother, CG and BiCGStab bottom solvers only");
1007 }
1008
1009 auto solve_start_time = amrex::second();
1010
1011 RT& composite_norminf = m_final_resnorm0;
1012
1013 m_niters_cg.clear();
1014 m_iter_fine_resnorm0.clear();
1015
1016 prepareForSolve(a_sol, a_rhs);
1017
1018 computeMLResidual(finest_amr_lev);
1019
1020 bool local = true;
1021 RT resnorm0 = MLResNormInf(finest_amr_lev, local);
1022 RT rhsnorm0 = MLRhsNormInf(local);
1023 if (!is_nsolve) {
1025
1026 if (verbose >= 1)
1027 {
1028 amrex::Print() << print_ident << "MLMG: Initial rhs = " << rhsnorm0 << "\n"
1029 << print_ident << "MLMG: Initial residual (resid0) = " << resnorm0 << "\n";
1030 }
1031 }
1032
1033 m_init_resnorm0 = resnorm0;
1034 m_rhsnorm0 = rhsnorm0;
1035
1036 if (!is_nsolve && !amrex::isfinite(std::max(resnorm0, rhsnorm0))) {
1037 if ( throw_exception ) {
1038 throw error("MLMG: rhs or initial residual is not finite.");
1039 } else {
1040 amrex::Abort("MLMG: rhs or initial residual is not finite");
1041 }
1042 }
1043
1044 RT max_norm = resnorm0;
1045 std::string norm_name = "resid0";
1046 switch (norm_type) {
1048 if (rhsnorm0 >= resnorm0) {
1049 norm_name = "bnorm";
1050 max_norm = rhsnorm0;
1051 } else {
1052 norm_name = "resid0";
1053 max_norm = resnorm0;
1054 }
1055 break;
1057 norm_name = "bnorm";
1058 max_norm = rhsnorm0;
1059 break;
1061 norm_name = "resid0";
1062 max_norm = resnorm0;
1063 break;
1064 }
1065
1066 const RT res_target = std::max(a_tol_abs, std::max(a_tol_rel,std::numeric_limits<RT>::epsilon())*max_norm);
1067
1068 if (!is_nsolve && resnorm0 <= res_target) {
1069 composite_norminf = resnorm0;
1070 if (verbose >= 1) {
1071 amrex::Print() << print_ident << "MLMG: No iterations needed\n";
1072 }
1073 } else {
1074 auto iter_start_time = amrex::second();
1075 bool converged = false;
1076
1077 // Hybrid: keep the best iterate so far, starting with the initial guess.
1078 // The switch tests use the residual MLMG reports: the finest level
1079 // until it converges, then the composite one.
1080 Vector<MF> best_sol;
1081 Vector<RT> hybrid_norms;
1082 bool hybrid_composite = false; // the reported residual became the composite one
1083 RT best_norm = resnorm0;
1084 if (hybrid) {
1085 if (namrlevs > 1) { best_norm = ResNormInf(finest_amr_lev); }
1086 best_sol.resize(namrlevs);
1087 for (int alev = 0; alev < namrlevs; ++alev) {
1088 const IntVect ng = nGrowVect(sol[alev]);
1089 best_sol[alev] = linop.make(alev, 0, ng);
1090 LocalCopy(best_sol[alev], sol[alev], 0, 0, ncomp, ng);
1091 }
1092 }
1093
1094 int niters = do_fixed_number_of_iters ? do_fixed_number_of_iters : max_iters;
1095 for (int iter = 0; iter < niters; ++iter)
1096 {
1097 // AlgMG solving all of AMR level 0 gets the reduction still needed
1098 // (one AMR level; 2-norm for max-norm, MLMG checks its own residual)
1099 // or 1e-2 (several; the finer levels' V-cycles rarely do better),
1100 // if looser than bottom_reltol.
1101 m_algmg_reltol = bottom_reltol;
1102 if (!is_nsolve && !do_fixed_number_of_iters && (m_algmg_active || linop.NMGLevels(0) == 1)) {
1103 RT const resnorm = (iter == 0) ? resnorm0 : composite_norminf;
1104 m_algmg_reltol = std::max(bottom_reltol, (namrlevs == 1) ? res_target/resnorm : RT(1.e-2));
1105 }
1106
1107 if (mixed) {
1108 spIter(iter);
1109 } else {
1110 oneIter(iter);
1111 }
1112
1113 converged = false;
1114
1115 // Test convergence on the fine amr level
1116 if (mixed) {
1117 computeMLResidual(finest_amr_lev); // the next spIter needs every level
1118 } else {
1119 computeResidual(finest_amr_lev);
1120 }
1121
1122 if (is_nsolve) { continue; }
1123
1124 RT fine_norminf = ResNormInf(finest_amr_lev);
1125 m_iter_fine_resnorm0.push_back(fine_norminf);
1126 composite_norminf = fine_norminf;
1127 if (verbose >= 2) {
1128 amrex::Print() << print_ident << "MLMG: Iteration " << std::setw(3) << iter+1 << " Fine resid/"
1129 << norm_name << " = " << fine_norminf/max_norm << "\n";
1130 }
1131 bool fine_converged = (fine_norminf <= res_target);
1132
1133 if (namrlevs == 1 && fine_converged) {
1134 converged = true;
1135 } else if (fine_converged) {
1136 // finest level is converged, but we still need to test the coarse levels
1137 if (!mixed) { computeMLResidual(finest_amr_lev-1); }
1138 RT crse_norminf = MLResNormInf(finest_amr_lev-1);
1139 if (verbose >= 2) {
1140 amrex::Print() << print_ident << "MLMG: Iteration " << std::setw(3) << iter+1
1141 << " Crse resid/" << norm_name << " = "
1142 << crse_norminf/max_norm << "\n";
1143 }
1144 converged = (crse_norminf <= res_target);
1145 composite_norminf = std::max(fine_norminf, crse_norminf);
1146 } else {
1147 converged = false;
1148 }
1149
1150 if (converged) {
1151 if (verbose >= 1) {
1152 amrex::Print() << print_ident << "MLMG: Final Iter. " << iter+1
1153 << " resid, resid/" << norm_name << " = "
1154 << composite_norminf << ", "
1155 << composite_norminf/max_norm << "\n";
1156 }
1157 break;
1158 } else {
1159 if (hybrid && !m_algmg_active) {
1160 if (fine_converged && !hybrid_composite) {
1161 // The reported residual now includes the coarse levels:
1162 // restart the comparisons from this iterate.
1163 hybrid_composite = true;
1164 hybrid_norms.clear();
1165 best_norm = composite_norminf;
1166 for (int alev = 0; alev < namrlevs; ++alev) {
1167 LocalCopy(best_sol[alev], sol[alev], 0, 0, ncomp, nGrowVect(sol[alev]));
1168 }
1169 }
1170 hybrid_norms.push_back(composite_norminf);
1171 std::string reason;
1172 bool do_switch = hybridShouldSwitch(hybrid_norms, max_norm,
1173 best_norm, reason);
1174 if (!do_switch && iter+1 == niters) {
1175 do_switch = true;
1176 reason = "not converged";
1177 }
1178 if (do_switch) {
1179 if (verbose >= 1) {
1180 amrex::Print() << print_ident << "MLMG: " << reason << " after "
1181 << iter+1 << " iterations, switching to AlgMG on AMR level 0\n";
1182 }
1183 if (!amrex::isfinite(composite_norminf) || composite_norminf > best_norm) {
1184 for (int alev = 0; alev < namrlevs; ++alev) {
1185 LocalCopy(sol[alev], best_sol[alev], 0, 0, ncomp, nGrowVect(sol[alev]));
1186 }
1187 computeResidual(finest_amr_lev);
1188 composite_norminf = best_norm;
1189 if (verbose >= 2) {
1190 amrex::Print() << print_ident << "MLMG: restarting from the best iterate, resid/"
1191 << norm_name << " = " << best_norm/max_norm << "\n";
1192 }
1193 }
1194 m_algmg_active = true;
1195 niters = iter + 1 + max_iters;
1196 continue;
1197 }
1198 if (composite_norminf < best_norm) {
1199 best_norm = composite_norminf;
1200 for (int alev = 0; alev < namrlevs; ++alev) {
1201 LocalCopy(best_sol[alev], sol[alev], 0, 0, ncomp, nGrowVect(sol[alev]));
1202 }
1203 }
1204 }
1205 if (composite_norminf > RT(1.e20)*max_norm || !amrex::isfinite(composite_norminf))
1206 {
1207 if (verbose > 0) {
1208 amrex::Print() << print_ident << "MLMG: Failing to converge after " << iter+1 << " iterations."
1209 << " resid, resid/" << norm_name << " = "
1210 << composite_norminf << ", "
1211 << composite_norminf/max_norm << "\n";
1212 }
1213
1214 if ( throw_exception ) {
1215 throw error("MLMG blew up.");
1216 } else {
1217 amrex::Abort("MLMG failing so lets stop here");
1218 }
1219 }
1220 }
1221 }
1222
1223 if (!converged && do_fixed_number_of_iters == 0) {
1224 if (verbose > 0) {
1225 amrex::Print() << print_ident << "MLMG: Failed to converge after " << niters << " iterations."
1226 << " resid, resid/" << norm_name << " = "
1227 << composite_norminf << ", "
1228 << composite_norminf/max_norm << "\n";
1229 }
1230
1231 if ( throw_exception ) {
1232 throw error("MLMG failed to converge.");
1233 } else {
1234 amrex::Abort("MLMG failed.");
1235 }
1236 }
1237 timer[iter_time] = amrex::second() - iter_start_time;
1238 }
1239
1240 if (mixed) {
1241 timer[bottom_time] = m_sp_mlmg->timer[bottom_time];
1242 m_niters_cg = m_sp_mlmg->m_niters_cg;
1243 }
1244
1245 linop.postSolve(GetVecOfPtrs(sol));
1246
1247 IntVect ng_back = final_fill_bc ? IntVect(1) : IntVect(0);
1248 if (linop.hasHiddenDimension()) {
1249 ng_back[linop.hiddenDirection()] = 0;
1250 }
1251 for (int alev = 0; alev < namrlevs; ++alev)
1252 {
1253 if (!sol_is_alias[alev]) {
1254 LocalCopy(*a_sol[alev], sol[alev], 0, 0, ncomp, ng_back);
1255 }
1256 }
1257
1258 timer[solve_time] = amrex::second() - solve_start_time;
1259 if (verbose >= 1) {
1260 ParallelReduce::Max<double>(timer.data(), timer.size(), 0,
1262 if (ParallelContext::MyProcSub() == 0)
1263 {
1264 amrex::AllPrint() << print_ident << "MLMG: Timers: Solve = " << timer[solve_time]
1265 << " Iter = " << timer[iter_time]
1266 << " Bottom = " << timer[bottom_time] << "\n";
1267 }
1268 }
1269
1270 ++solve_called;
1271
1272 return composite_norminf;
1273}
1274
1275template <typename MF>
1276auto
1278 RT a_tol_rel, RT a_tol_abs) -> RT
1279{
1280 precond_mode = true;
1281 std::swap(max_precond_iters, do_fixed_number_of_iters);
1282 linop.beginPrecondBC();
1283
1284 auto r = solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
1285
1286 linop.endPrecondBC();
1287 std::swap(max_precond_iters, do_fixed_number_of_iters);
1288 precond_mode = false;
1289
1290 return r;
1291}
1292
1293template <typename MF>
1294void
1296{
1297 for (int alev = finest_amr_lev; alev >= 0; --alev) {
1298 const MF* crse_bcdata = (alev > 0) ? a_sol[alev-1] : nullptr;
1299 linop.prepareForFluxes(alev, crse_bcdata);
1300 }
1301}
1302
1303template <typename MF>
1304template <typename AMF>
1305void
1307{
1308 BL_PROFILE("MLMG::getGradSolution()");
1309 for (int alev = 0; alev <= finest_amr_lev; ++alev) {
1310 if constexpr (std::is_same<AMF,MF>()) {
1311 linop.compGrad(alev, a_grad_sol[alev], sol[alev], a_loc);
1312 } else {
1313 Array<MF,AMREX_SPACEDIM> grad_sol;
1314 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1315 auto const& amf = *(a_grad_sol[alev][idim]);
1316 grad_sol[idim].define(boxArray(amf), DistributionMap(amf), ncomp, 0);
1317 }
1318 linop.compGrad(alev, GetArrOfPtrs(grad_sol), sol[alev], a_loc);
1319 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1320 LocalCopy(*a_grad_sol[alev][idim], grad_sol[idim], 0, 0, ncomp, IntVect(0));
1321 }
1322 }
1323 }
1324}
1325
1326template <typename MF>
1327template <typename AMF>
1328void
1329MLMGT<MF>::getGradSolution (std::initializer_list<Array<AMF*,AMREX_SPACEDIM>> a_grad_sol, Location a_loc)
1330{
1331 getGradSolution(Vector<Array<AMF*,AMREX_SPACEDIM>>(std::move(a_grad_sol)), a_loc);
1332}
1333
1334template <typename MF>
1335template <typename AMF>
1336void
1338 Location a_loc)
1339{
1340 if (!linop.isCellCentered()) {
1341 amrex::Abort("Calling wrong getFluxes for nodal solver");
1342 }
1343
1344 AMREX_ASSERT(sol.size() == a_flux.size());
1345
1346 if constexpr (std::is_same<AMF,MF>()) {
1347 getFluxes(a_flux, GetVecOfPtrs(sol), a_loc);
1348 } else {
1349 Vector<Array<MF,AMREX_SPACEDIM>> fluxes(namrlevs);
1350 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1351 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1352 auto const& amf = *(a_flux[ilev][idim]);
1353 fluxes[ilev][idim].define(boxArray(amf), DistributionMap(amf), ncomp, 0);
1354 }
1355 }
1356 getFluxes(GetVecOfArrOfPtrs(fluxes), GetVecOfPtrs(sol), a_loc);
1357 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1358 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1359 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp, IntVect(0));
1360 }
1361 }
1362 }
1363}
1364
1365template <typename MF>
1366template <typename AMF>
1367void
1369 Location a_loc)
1370{
1371 getFluxes(Vector<Array<AMF*,AMREX_SPACEDIM>>(std::move(a_flux)), a_loc);
1372}
1373
1374template <typename MF>
1375template <typename AMF>
1376void
1378 const Vector<AMF*>& a_sol, Location a_loc)
1379{
1380 BL_PROFILE("MLMG::getFluxes()");
1381
1382 if (!linop.isCellCentered()) {
1383 amrex::Abort("Calling wrong getFluxes for nodal solver");
1384 }
1385
1386 if constexpr (std::is_same<AMF,MF>()) {
1387 linop.getFluxes(a_flux, a_sol, a_loc);
1388 } else {
1389 Vector<Array<MF,AMREX_SPACEDIM>> fluxes(namrlevs);
1390 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1391 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1392 auto const& amf = *(a_flux[ilev][idim]);
1393 fluxes[ilev][idim].define(boxArray(amf), DistributionMap(amf), ncomp, 0);
1394 }
1395 LocalCopy(sol[ilev], *a_sol[ilev], 0, 0, ncomp, nGrowVect(sol[ilev]));
1396 }
1397 linop.getFluxes(GetVecOfArrOfPtrs(fluxes), GetVecOfPtrs(sol), a_loc);
1398 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1399 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1400 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp, IntVect(0));
1401 }
1402 }
1403 }
1404}
1405
1406template <typename MF>
1407template <typename AMF>
1408void
1410 std::initializer_list<AMF*> a_sol, Location a_loc)
1411{
1412 getFluxes(Vector<Array<AMF*,AMREX_SPACEDIM>>(std::move(a_flux)),
1413 Vector<AMF*>(std::move(a_sol)), a_loc);
1414}
1415
1416template <typename MF>
1417template <typename AMF>
1418void
1420{
1421 AMREX_ASSERT(sol.size() == a_flux.size());
1422 if constexpr (std::is_same<AMF,MF>()) {
1423 getFluxes(a_flux, GetVecOfPtrs(sol), a_loc);
1424 } else {
1425 Vector<MF> fluxes(namrlevs);
1426 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1427 auto const& amf = *a_flux[ilev];
1428 fluxes[ilev].define(boxArray(amf), DistributionMap(amf), AMREX_SPACEDIM, 0);
1429 }
1430 getFluxes(GetVecOfPtrs(fluxes), GetVecOfPtrs(sol), a_loc);
1431 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1432 LocalCopy(*a_flux[ilev], fluxes[ilev], 0, 0, AMREX_SPACEDIM, IntVect(0));
1433 }
1434 }
1435}
1436
1437template <typename MF>
1438template <typename AMF>
1439void
1440MLMGT<MF>::getFluxes (std::initializer_list<AMF*> a_flux, Location a_loc)
1441{
1442 getFluxes(Vector<AMF*>(std::move(a_flux)), a_loc);
1443}
1444
1445template <typename MF>
1446template <typename AMF>
1447void
1449 const Vector<AMF*>& a_sol, Location /*a_loc*/)
1450{
1451 AMREX_ASSERT(nComp(*a_flux[0]) >= AMREX_SPACEDIM);
1452
1453 if constexpr (! std::is_same<AMF,MF>()) {
1454 for (int alev = 0; alev < namrlevs; ++alev) {
1455 LocalCopy(sol[alev], *a_sol[alev], 0, 0, ncomp, nGrowVect(sol[alev]));
1456 }
1457 }
1458
1459 if (linop.isCellCentered())
1460 {
1461 Vector<Array<MF,AMREX_SPACEDIM> > ffluxes(namrlevs);
1462 for (int alev = 0; alev < namrlevs; ++alev) {
1463 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1464 const int mglev = 0;
1465 int nghost = 0;
1466 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1467 ffluxes[alev][idim].define(amrex::convert(linop.m_grids[alev][mglev],
1469 linop.m_dmap[alev][mglev], ncomp, nghost, MFInfo(),
1470 *linop.m_factory[alev][mglev]);
1471 }
1472 }
1473 if constexpr (std::is_same<AMF,MF>()) {
1474 getFluxes(amrex::GetVecOfArrOfPtrs(ffluxes), a_sol, Location::FaceCenter);
1475 } else {
1476 getFluxes(amrex::GetVecOfArrOfPtrs(ffluxes), GetVecOfPtrs(sol), Location::FaceCenter);
1477 }
1478 for (int alev = 0; alev < namrlevs; ++alev) {
1479#ifdef AMREX_USE_EB
1480 EB_average_face_to_cellcenter(*a_flux[alev], 0, amrex::GetArrOfConstPtrs(ffluxes[alev]));
1481#else
1482 average_face_to_cellcenter(*a_flux[alev], 0, amrex::GetArrOfConstPtrs(ffluxes[alev]));
1483#endif
1484 }
1485
1486 } else {
1487 if constexpr (std::is_same<AMF,MF>()) {
1488 linop.getFluxes(a_flux, a_sol);
1489 } else {
1490 Vector<MF> fluxes(namrlevs);
1491 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1492 auto const& amf = *a_flux[ilev];
1493 fluxes[ilev].define(boxArray(amf), DistributionMap(amf), AMREX_SPACEDIM, 0);
1494 }
1495 linop.getFluxes(GetVecOfPtrs(fluxes), GetVecOfPtrs(sol));
1496 for (int ilev = 0; ilev < namrlevs; ++ilev) {
1497 LocalCopy(*a_flux[ilev], fluxes[ilev], 0, 0, AMREX_SPACEDIM, IntVect(0));
1498 }
1499 }
1500 }
1501}
1502
1503template <typename MF>
1504template <typename AMF>
1505void
1506MLMGT<MF>::getFluxes (std::initializer_list<AMF*> a_flux,
1507 std::initializer_list<AMF*> a_sol, Location a_loc)
1508{
1509 getFluxes(Vector<AMF*>(std::move(a_flux)),
1510 Vector<AMF*>(std::move(a_sol)), a_loc);
1511}
1512
1513#ifdef AMREX_USE_EB
1514template <typename MF>
1515void
1517{
1518 if (!linop.isCellCentered()) {
1519 amrex::Abort("getEBFluxes is for cell-centered only");
1520 }
1521
1522 AMREX_ASSERT(sol.size() == a_eb_flux.size());
1523 getEBFluxes(a_eb_flux, GetVecOfPtrs(sol));
1524}
1525
1526template <typename MF>
1527void
1528MLMGT<MF>::getEBFluxes (const Vector<MF*>& a_eb_flux, const Vector<MF*>& a_sol)
1529{
1530 BL_PROFILE("MLMG::getEBFluxes()");
1531
1532 if (!linop.isCellCentered()) {
1533 amrex::Abort("getEBFluxes is for cell-centered only");
1534 }
1535
1536 linop.getEBFluxes(a_eb_flux, a_sol);
1537}
1538#endif
1539
1540template <typename MF>
1541void
1543 const Vector<MF const*>& a_rhs)
1544{
1545 BL_PROFILE("MLMG::compResidual()");
1546
1547 IntVect ng_sol(1);
1548 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1549
1550 sol.resize(namrlevs);
1551 sol_is_alias.resize(namrlevs,true);
1552 for (int alev = 0; alev < namrlevs; ++alev)
1553 {
1554 if (cf_strategy == CFStrategy::ghostnodes || nGrowVect(*a_sol[alev]) == ng_sol)
1555 {
1556 sol[alev] = linop.makeAlias(*a_sol[alev]);
1557 sol_is_alias[alev] = true;
1558 }
1559 else
1560 {
1561 if (sol_is_alias[alev])
1562 {
1563 sol[alev] = linop.make(alev, 0, ng_sol);
1564 sol_is_alias[alev] = false;
1565 }
1566 LocalCopy(sol[alev], *a_sol[alev], 0, 0, ncomp, IntVect(0));
1567 }
1568 }
1569
1570 prepareLinOp();
1571
1572
1573 for (int alev = finest_amr_lev; alev >= 0; --alev) {
1574 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) : nullptr;
1575 const MF* prhs = a_rhs[alev];
1576#if (AMREX_SPACEDIM != 3)
1577 int nghost = (cf_strategy == CFStrategy::ghostnodes) ? linop.getNGrow(alev) : 0;
1578 MF rhstmp(boxArray(*prhs), DistributionMap(*prhs), ncomp, nghost,
1579 MFInfo(), *linop.Factory(alev));
1580 LocalCopy(rhstmp, *prhs, 0, 0, ncomp, IntVect(nghost));
1581 linop.applyMetricTerm(alev, 0, rhstmp);
1582 linop.unimposeNeumannBC(alev, rhstmp);
1583 linop.applyInhomogNeumannTerm(alev, rhstmp);
1584 prhs = &rhstmp;
1585#endif
1586 linop.solutionResidual(alev, *a_res[alev], sol[alev], *prhs, crse_bcdata);
1587 if (alev < finest_amr_lev) {
1588 linop.reflux(alev, *a_res[alev], sol[alev], *prhs,
1589 *a_res[alev+1], sol[alev+1], *a_rhs[alev+1]);
1590 if (linop.isCellCentered()) {
1591#ifdef AMREX_USE_EB
1592 // EB operators are double only.
1593 if constexpr (std::is_same_v<MF,MultiFab>) {
1594 EB_average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1595 } else
1596#endif
1597 {
1598 average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1599 }
1600 }
1601 }
1602 }
1603
1604
1605#if (AMREX_SPACEDIM != 3)
1606 for (int alev = 0; alev <= finest_amr_lev; ++alev) {
1607 linop.unapplyMetricTerm(alev, 0, *a_res[alev]);
1608 }
1609#endif
1610}
1611
1612template <typename MF>
1613void
1615{
1616 BL_PROFILE("MLMG::apply()");
1617
1618 Vector<MF*> in(namrlevs);
1619 Vector<MF> in_raii(namrlevs);
1620 Vector<MF> rh(namrlevs);
1621 int nghost = 0;
1622 IntVect ng_sol(1);
1623 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1624
1625 for (int alev = 0; alev < namrlevs; ++alev)
1626 {
1627 if (cf_strategy == CFStrategy::ghostnodes)
1628 {
1629 nghost = linop.getNGrow(alev);
1630 in[alev] = a_in[alev];
1631 }
1632 else if (nGrowVect(*a_in[alev]) == ng_sol)
1633 {
1634 in[alev] = a_in[alev];
1635 }
1636 else
1637 {
1638 IntVect ng = ng_sol;
1639 if (cf_strategy == CFStrategy::ghostnodes) { ng = IntVect(nghost); }
1640 in_raii[alev] = linop.make(alev, 0, ng,
1641 MFInfo().SetArena(The_Async_Arena()));
1642 LocalCopy(in_raii[alev], *a_in[alev], 0, 0, ncomp, IntVect(nghost));
1643 in[alev] = &(in_raii[alev]);
1644 }
1645 rh[alev] = linop.make(alev, 0, IntVect(nghost),
1646 MFInfo().SetArena(The_Async_Arena()));
1647 setVal(rh[alev], RT(0.0));
1648 }
1649
1650 prepareLinOp();
1651
1652 for (int alev = 0; alev < namrlevs; ++alev) {
1653 linop.applyInhomogNeumannTerm(alev, rh[alev]);
1654 }
1655
1656
1657 for (int alev = finest_amr_lev; alev >= 0; --alev) {
1658 const MF* crse_bcdata = (alev > 0) ? in[alev-1] : nullptr;
1659 linop.solutionResidual(alev, *out[alev], *in[alev], rh[alev], crse_bcdata);
1660 if (alev < finest_amr_lev) {
1661 linop.reflux(alev, *out[alev], *in[alev], rh[alev],
1662 *out[alev+1], *in[alev+1], rh[alev+1]);
1663 if (linop.isCellCentered()) {
1664 if constexpr (IsMultiFabLike_v<MF>) {
1665#ifdef AMREX_USE_EB
1666 // EB operators are double only.
1667 if constexpr (std::is_same_v<MF,MultiFab>) {
1668 EB_average_down(*out[alev+1], *out[alev], 0, nComp(*out[alev]), linop.AMRRefRatioVect(alev));
1669 } else
1670#endif
1671 {
1672 average_down(*out[alev+1], *out[alev], 0, nComp(*out[alev]), linop.AMRRefRatioVect(alev));
1673 }
1674 } else {
1675 amrex::Abort("MLMG: TODO average_down for non-MultiFab");
1676 }
1677 }
1678 }
1679 }
1680
1681#if (AMREX_SPACEDIM != 3)
1682 for (int alev = 0; alev <= finest_amr_lev; ++alev) {
1683 linop.unapplyMetricTerm(alev, 0, *out[alev]);
1684 }
1685#endif
1686
1687 for (int alev = 0; alev <= finest_amr_lev; ++alev) {
1688 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1689 Scale(*out[alev], RT(-1), 0, nComp(*out[alev]), nghost);
1690 }
1691}
1692
1693template <typename MF>
1694void
1696{
1697 precond_mode = true;
1698 linop.beginPrecondBC();
1699 apply(out, in);
1700 linop.endPrecondBC();
1701 precond_mode = false;
1702}
1703
1704template <typename MF>
1705template <typename AMF>
1706void
1708{
1709 BL_PROFILE("MLMG::prepareForSolve()");
1710
1711 AMREX_ASSERT(namrlevs <= a_sol.size());
1712 AMREX_ASSERT(namrlevs <= a_rhs.size());
1713
1714 timer.assign(ntimers, 0.0);
1715
1716 prepareLinOp();
1717 prepareSolRHS(a_sol, a_rhs);
1719}
1720
1721template <typename MF>
1722template <typename AMF>
1723void
1725{
1726 IntVect ng_rhs(0);
1727 IntVect const ng_sol = ngSol();
1728
1729 sol.resize(namrlevs);
1730 sol_is_alias.resize(namrlevs,false);
1731 for (int alev = 0; alev < namrlevs; ++alev)
1732 {
1733 if (cf_strategy == CFStrategy::ghostnodes)
1734 {
1735 if constexpr (std::is_same<AMF,MF>()) {
1736 sol[alev] = linop.makeAlias(*a_sol[alev]);
1737 sol_is_alias[alev] = true;
1738 } else {
1739 amrex::Abort("Type conversion not supported for CFStrategy::ghostnodes");
1740 }
1741 }
1742 else
1743 {
1744 bool alias_made = false;
1745 if (nGrowVect(*a_sol[alev]) == ng_sol) {
1746 if constexpr (std::is_same<AMF,MF>()) {
1747 sol[alev] = linop.makeAlias(*a_sol[alev]);
1748 sol_is_alias[alev] = true;
1749 alias_made = true;
1750 }
1751 }
1752 if (!alias_made) {
1753 if (sol_is_alias[alev] || !solve_called) {
1754 sol[alev] = linop.make(alev, 0, ng_sol);
1755 sol_is_alias[alev] = false;
1756 }
1757 LocalCopy(sol[alev], *a_sol[alev], 0, 0, ncomp, IntVect(0));
1758 setBndry(sol[alev], RT(0.0), 0, ncomp);
1759 }
1760 }
1761 }
1762
1763 rhs.resize(namrlevs);
1764 for (int alev = 0; alev < namrlevs; ++alev)
1765 {
1766 if (cf_strategy == CFStrategy::ghostnodes) { ng_rhs = IntVect(linop.getNGrow(alev)); }
1767 if (!solve_called) {
1768 rhs[alev] = linop.make(alev, 0, ng_rhs);
1769 }
1770 LocalCopy(rhs[alev], *a_rhs[alev], 0, 0, ncomp, ng_rhs);
1771 linop.applyMetricTerm(alev, 0, rhs[alev]);
1772 linop.unimposeNeumannBC(alev, rhs[alev]);
1773 linop.applyInhomogNeumannTerm(alev, rhs[alev]);
1774 linop.applyOverset(alev, rhs[alev]);
1775 if ( ! precond_mode) {
1776 bool r = linop.scaleRHS(alev, &(rhs[alev]));
1778 }
1779
1780#ifdef AMREX_USE_EB
1781 const auto *factory = dynamic_cast<EBFArrayBoxFactory const*>(linop.Factory(alev));
1782 if (factory && !factory->isAllRegular()) {
1783 if constexpr (std::is_same<MF,MultiFab>()) {
1784 EB_set_covered(rhs[alev], 0, ncomp, 0, RT(0.0));
1785 EB_set_covered(sol[alev], 0, ncomp, 0, RT(0.0));
1786 } else {
1787 amrex::Abort("TODO: MLMG with EB only works with MultiFab");
1788 }
1789 }
1790#endif
1791 }
1792
1793 for (int falev = finest_amr_lev; falev > 0; --falev)
1794 {
1795 linop.averageDownSolutionRHS(falev-1, sol[falev-1], rhs[falev-1], sol[falev], rhs[falev]);
1796 }
1797
1798 // Nodal solvers: the fine coarse/fine nodes from the coarse solution
1799 if (cf_strategy != CFStrategy::ghostnodes) {
1800 for (int falev = 1; falev <= finest_amr_lev; ++falev) {
1801 linop.interpCoarseFineSolution(falev, sol[falev], sol[falev-1]);
1802 }
1803 }
1804
1805 // enforce solvability if appropriate
1806 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1807 {
1808 makeSolvable();
1809 }
1810}
1811
1812template <typename MF>
1813void
1815{
1816 // Mixed precision: this solver needs only res; the single precision
1817 // multigrid has its own work arrays.
1818 const bool mixed = (linop.m_sp_linop != nullptr);
1819
1820 IntVect ng = linop.getNGrowVectRestriction();
1821 if (cf_strategy == CFStrategy::ghostnodes) { ng = IntVect(linop.getNGrow(finest_amr_lev)); }
1822 if (!solve_called) {
1823 linop.make(res, ng);
1824 if (!mixed) { linop.make(rescor, ng); }
1825 }
1826 for (int alev = 0; alev <= finest_amr_lev; ++alev)
1827 {
1828 const int nmglevs = linop.NMGLevels(alev);
1829 for (int mglev = 0; mglev < nmglevs; ++mglev)
1830 {
1831 setVal(res[alev][mglev], RT(0.0));
1832 if (!mixed) { setVal(rescor[alev][mglev], RT(0.0)); }
1833 }
1834 }
1835
1836 if (mixed) {
1838 }
1839
1840 if (cf_strategy != CFStrategy::ghostnodes) { ng = ngSol(); }
1841 cor.resize(namrlevs);
1842 for (int alev = 0; alev <= finest_amr_lev; ++alev)
1843 {
1844 const int nmglevs = mixed ? 0 : linop.NMGLevels(alev);
1845 cor[alev].resize(nmglevs);
1846 for (int mglev = 0; mglev < nmglevs; ++mglev)
1847 {
1848 if (!solve_called) {
1849 IntVect _ng = ng;
1850 if (cf_strategy == CFStrategy::ghostnodes) { _ng=IntVect(linop.getNGrow(alev,mglev)); }
1851 cor[alev][mglev] = linop.make(alev, mglev, _ng);
1852 }
1853 setVal(cor[alev][mglev], RT(0.0));
1854 }
1855 }
1856
1857 cor_hold.resize(std::max(namrlevs-1,1));
1858 {
1859 const int alev = 0;
1860 const int nmglevs = mixed ? 0 : linop.NMGLevels(alev);
1861 cor_hold[alev].resize(nmglevs);
1862 for (int mglev = 0; mglev < nmglevs-1; ++mglev)
1863 {
1864 if (!solve_called) {
1865 IntVect _ng = ng;
1866 if (cf_strategy == CFStrategy::ghostnodes) { _ng=IntVect(linop.getNGrow(alev,mglev)); }
1867 cor_hold[alev][mglev] = linop.make(alev, mglev, _ng);
1868 }
1869 setVal(cor_hold[alev][mglev], RT(0.0));
1870 }
1871 }
1872 for (int alev = 1; alev < finest_amr_lev && !mixed; ++alev)
1873 {
1874 cor_hold[alev].resize(1);
1875 if (!solve_called) {
1876 IntVect _ng = ng;
1877 if (cf_strategy == CFStrategy::ghostnodes) { _ng=IntVect(linop.getNGrow(alev)); }
1878 cor_hold[alev][0] = linop.make(alev, 0, _ng);
1879 }
1880 setVal(cor_hold[alev][0], RT(0.0));
1881 }
1882
1883 if (linop.m_parent // no embedded N-Solve
1884 || !linop.supportNSolve())
1885 {
1886 do_nsolve = false;
1887 }
1888
1889 // Mixed precision: the single precision multigrid does the N-Solve.
1890 if (do_nsolve && ns_linop == nullptr && !mixed)
1891 {
1893 }
1894
1895 if (verbose >= 2) {
1896 amrex::Print() << print_ident << "MLMG: # of AMR levels: " << namrlevs << "\n"
1897 << print_ident << " # of MG levels on the coarsest AMR level: "
1898 << (mixed ? linop.m_sp_linop->NMGLevels(0) : linop.NMGLevels(0))
1899 << (mixed ? " (single precision)\n" : "\n");
1900 if (ns_linop) {
1901 amrex::Print() << print_ident << " # of MG levels in N-Solve: " << ns_linop->NMGLevels(0) << "\n"
1902 << print_ident << " # of grids in N-Solve: " << ns_linop->m_grids[0][0].size() << "\n";
1903 }
1904 }
1905}
1906
1907template <typename MF>
1908void
1910{
1911 linop.setPrintIndentation(print_ident);
1912 if (!linop_prepared) {
1913 linop.prepareForSolve();
1914 AMREX_ASSERT(!linop.m_mg_deferred || linop.m_mg_built);
1915 linop_prepared = true;
1916 } else if (linop.needsUpdate()) {
1917 linop.update();
1918
1919 m_algmg_bottom.reset();
1920 m_algmg_level0.reset();
1921
1922#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1923 hypre_solver.reset();
1924 hypre_bndry.reset();
1925 hypre_node_solver.reset();
1926#endif
1927
1928#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1929 petsc_solver.reset();
1930 petsc_bndry.reset();
1931#endif
1932 }
1933}
1934
1935template <typename MF>
1936void
1938{
1939 prepareLinOp();
1940 linop.preparePrecond();
1941}
1942
1943template <typename MF>
1944void
1946{
1947 if constexpr (IsMultiFabLike_v<MF>) {
1948 ns_linop = linop.makeNLinOp(nsolve_grid_size);
1949
1950 int nghost = 0;
1951 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(); }
1952
1953 const BoxArray& ba = (*ns_linop).m_grids[0][0];
1954 const DistributionMapping& dm =(*ns_linop).m_dmap[0][0];
1955
1956 int ng = 1;
1957 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1958 ns_sol = std::make_unique<MF>(ba, dm, ncomp, ng, MFInfo(), *(ns_linop->Factory(0,0)));
1959 ng = 0;
1960 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1961 ns_rhs = std::make_unique<MF>(ba, dm, ncomp, ng, MFInfo(), *(ns_linop->Factory(0,0)));
1962 setVal(*ns_sol, RT(0.0));
1963 setVal(*ns_rhs, RT(0.0));
1964
1965 ns_linop->setLevelBC(0, ns_sol.get());
1966
1967 ns_mlmg = std::make_unique<MLMGT<MF>>(*ns_linop);
1968 ns_mlmg->setVerbose(0);
1969 ns_mlmg->setFixedIter(1);
1970 ns_mlmg->setMaxFmgIter(20);
1971 ns_mlmg->setBottomSolver(BottomSolver::smoother);
1972 }
1973}
1974
1975// in : Residual (res) on the finest AMR level
1976// out : sol on all AMR levels
1977template <typename MF>
1978void MLMGT<MF>::oneIter (int iter)
1979{
1980 BL_PROFILE("MLMG::oneIter()");
1981
1982 for (int alev = finest_amr_lev; alev > 0; --alev)
1983 {
1984 miniCycle(alev);
1985
1986 IntVect nghost(0);
1987 if (cf_strategy == CFStrategy::ghostnodes) { nghost = IntVect(linop.getNGrow(alev)); }
1988 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1989
1990 // compute residual for the coarse AMR level
1991 computeResWithCrseSolFineCor(alev-1,alev);
1992
1993 if (alev != finest_amr_lev) {
1994 std::swap(cor_hold[alev][0], cor[alev][0]); // save it for the up cycle
1995 }
1996 }
1997
1998 // coarsest amr level
1999 {
2000 // enforce solvability if appropriate
2001 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
2002 {
2003 makeSolvable(0,0,res[0][0]);
2004 }
2005
2006 if (m_algmg_active) {
2007 if constexpr (std::is_same<MF,MultiFab>()) {
2009 } else {
2010 amrex::Abort("MLMG: AlgMG is only supported for MultiFab");
2011 }
2012 } else if (iter < max_fmg_iters) {
2013 mgFcycle();
2014 } else {
2015 mgVcycle(0, 0);
2016 }
2017
2018 IntVect nghost(0);
2019 if (cf_strategy == CFStrategy::ghostnodes) { nghost = IntVect(linop.getNGrow(0)); }
2020 LocalAdd(sol[0], cor[0][0], 0, 0, ncomp, nghost);
2021 }
2022
2023 for (int alev = 1; alev <= finest_amr_lev; ++alev)
2024 {
2025 // (Fine AMR correction) = I(Coarse AMR correction)
2026 interpCorrection(alev);
2027 // Known overset cells must not receive the interpolated correction.
2028 linop.applyOverset(alev, cor[alev][0]);
2029
2030 IntVect nghost(0);
2031 if (cf_strategy == CFStrategy::ghostnodes) { nghost = IntVect(linop.getNGrow(alev)); }
2032 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
2033
2034 if (alev != finest_amr_lev) {
2035 LocalAdd(cor_hold[alev][0], cor[alev][0], 0, 0, ncomp, nghost);
2036 }
2037
2038 // Update fine AMR level correction
2040
2041 miniCycle(alev);
2042
2043 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
2044
2045 if (alev != finest_amr_lev) {
2046 LocalAdd(cor[alev][0], cor_hold[alev][0], 0, 0, ncomp, nghost);
2047 }
2048 }
2049
2050 linop.averageDownAndSync(sol);
2051}
2052
2053// Mixed precision: set up the multigrid solver on the single precision copy
2054// of the operator. Its work arrays are kept across solves.
2055template <typename MF>
2056void
2058{
2060 const bool first = !m_sp_mlmg;
2061 if (first) {
2062 m_sp_mlmg = std::make_unique<MLMGT<fMultiFab>>(*linop.m_sp_linop);
2063 m_sp_linop_count = linop.m_sp_linop_count;
2064 } else if (m_sp_linop_count != linop.m_sp_linop_count) {
2065 amrex::Abort("MLMG: the operator was re-defined after this mixed precision MLMG "
2066 "was used; build a new MLMG for it");
2067 }
2068 auto& sp = *m_sp_mlmg;
2069 sp.setNSolve(do_nsolve);
2070 sp.setNSolveGridSize(nsolve_grid_size);
2071 sp.setVerbose(std::max(0, verbose-2));
2072 sp.setBottomVerbose(bottom_verbose);
2073 sp.setBottomSolver(bottom_solver);
2074 sp.setBottomMaxIter(bottom_maxiter);
2075 // The float default is looser than the double one; a tolerance set
2076 // by the user is forwarded.
2077 if (bottom_reltol_set) { sp.setBottomTolerance(static_cast<float>(bottom_reltol)); }
2078 sp.setPreSmooth(nu1);
2079 sp.setPostSmooth(nu2);
2080 sp.setFinalSmooth(nuf);
2081 sp.setBottomSmooth(nub);
2082 sp.setMaxFmgIter(max_fmg_iters);
2083 sp.setCFStrategy(static_cast<typename MLMGT<fMultiFab>::CFStrategy>(cf_strategy));
2084 sp.setThrowException(throw_exception);
2085 sp.print_ident = print_ident + " ";
2086 // sp.solve is never called
2087 sp.m_niters_cg.clear();
2088 sp.timer.assign(ntimers, 0.0);
2089 sp.prepareLinOp();
2090 if (first) {
2091 // spIter overwrites sol and rhs. The coarse/fine BC data of
2092 // the copy come from its solution, which is zero whenever
2093 // spIter needs them.
2094 sp.sol.resize(namrlevs);
2095 sp.sol_is_alias.resize(namrlevs, false);
2096 sp.rhs.resize(namrlevs);
2097 for (int alev = 0; alev < namrlevs; ++alev) {
2098 sp.sol[alev] = sp.linop.make(alev, 0, ngSol());
2099 sp.sol[alev].setVal(0.0F);
2100 sp.rhs[alev] = sp.linop.make(alev, 0, IntVect(0));
2101 sp.rhs[alev].setVal(0.0F);
2102 }
2103 }
2104 sp.prepareWorkArrays();
2105 if (first) {
2106 sp.solve_called = 1; // keep the work arrays
2107 sp.computeMLResidual(finest_amr_lev); // fills the copy's coarse/fine BC data (zeros)
2108 }
2109 } else {
2110 amrex::Abort("MLMG::prepareForSPSolve: MultiFab only");
2111 }
2112}
2113
2114// Mixed precision: the composite residual (res) in single precision is the
2115// rhs of the correction equation, which the single precision multigrid
2116// solves with one cycle. The residual is scaled to O(1) before the
2117// conversion, and the correction, scaled back and by the copy's operator
2118// scale, is added to sol in double precision.
2119template <typename MF>
2120void
2122{
2123 BL_PROFILE("MLMG::spIter()");
2124
2126 auto& sp = *m_sp_mlmg;
2127
2128 RT s = (iter == 0) ? m_init_resnorm0 : m_final_resnorm0;
2129 if (!(s > RT(0.0))) { s = RT(1.0); }
2130 RT const invs = RT(1.0)/s;
2131 RT const ss = s * linop.m_sp_scale; // the copy solves (m_sp_scale L) c = r/s
2132 sp.setBottomToleranceAbs(static_cast<float>(bottom_abstol * invs)); // in the units of r/s
2133
2134 for (int alev = 0; alev < namrlevs; ++alev) {
2135 setVal(sp.sol[alev], 0.0F);
2136 auto& dmf = sp.rhs[alev];
2137 auto const& smf = res[alev][0];
2138#ifdef AMREX_USE_GPU
2139 if (Gpu::inLaunchRegion() && dmf.isFusingCandidate()) {
2140 auto const& dma = dmf.arrays();
2141 auto const& sma = smf.const_arrays();
2142 ParallelFor(dmf, IntVect(0), ncomp,
2143 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
2144 {
2145 dma[box_no](i,j,k,n) = static_cast<float>(sma[box_no](i,j,k,n) * invs);
2146 });
2147 if (!Gpu::inNoSyncRegion()) {
2149 }
2150 } else
2151#endif
2152 {
2153#ifdef AMREX_USE_OMP
2154#pragma omp parallel if (Gpu::notInLaunchRegion())
2155#endif
2156 for (MFIter mfi(dmf,TilingIfNotGPU()); mfi.isValid(); ++mfi)
2157 {
2158 const Box& bx = mfi.tilebox();
2159 auto const& dfab = dmf.array(mfi);
2160 auto const& sfab = smf.const_array(mfi);
2161 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
2162 {
2163 dfab(i,j,k,n) = static_cast<float>(sfab(i,j,k,n) * invs);
2164 });
2165 }
2166 }
2167 }
2168 // oneIter recomputes the coarse AMR level residuals from sol and rhs.
2169 LocalCopy(sp.res[finest_amr_lev][0], sp.rhs[finest_amr_lev], 0, 0, ncomp, IntVect(0));
2170
2171 sp.oneIter(iter);
2172
2173 for (int alev = 0; alev < namrlevs; ++alev) {
2174 auto& dmf = sol[alev];
2175 auto const& smf = sp.sol[alev];
2176#ifdef AMREX_USE_GPU
2177 if (Gpu::inLaunchRegion() && dmf.isFusingCandidate()) {
2178 auto const& dma = dmf.arrays();
2179 auto const& sma = smf.const_arrays();
2180 ParallelFor(dmf, IntVect(0), ncomp,
2181 [=] AMREX_GPU_DEVICE (int box_no, int i, int j, int k, int n) noexcept
2182 {
2183 dma[box_no](i,j,k,n) += ss * static_cast<RT>(sma[box_no](i,j,k,n));
2184 });
2185 if (!Gpu::inNoSyncRegion()) {
2187 }
2188 } else
2189#endif
2190 {
2191#ifdef AMREX_USE_OMP
2192#pragma omp parallel if (Gpu::notInLaunchRegion())
2193#endif
2194 for (MFIter mfi(dmf,TilingIfNotGPU()); mfi.isValid(); ++mfi)
2195 {
2196 const Box& bx = mfi.tilebox();
2197 auto const& dfab = dmf.array(mfi);
2198 auto const& sfab = smf.const_array(mfi);
2199 AMREX_HOST_DEVICE_PARALLEL_FOR_4D(bx, ncomp, i, j, k, n,
2200 {
2201 dfab(i,j,k,n) += ss * static_cast<RT>(sfab(i,j,k,n));
2202 });
2203 }
2204 }
2205 }
2206
2207 linop.averageDownAndSync(sol);
2208 // Nodal: the fine coarse/fine nodes are not seen by the residual, so
2209 // take them from the double precision coarse solution.
2210 for (int falev = 1; falev <= finest_amr_lev; ++falev) {
2211 linop.interpCoarseFineSolution(falev, sol[falev], sol[falev-1]);
2212 }
2213 } else {
2215 amrex::Abort("MLMG::spIter: MultiFab only");
2216 }
2217}
2218
2219template <typename MF>
2220void
2222{
2223 BL_PROFILE("MLMG::miniCycle()");
2224 const int mglev = 0;
2225 mgVcycle(amrlev, mglev);
2226}
2227
2228// in : Residual (res)
2229// out : Correction (cor) from bottom to this function's local top
2230template <typename MF>
2231void
2232MLMGT<MF>::mgVcycle (int amrlev, int mglev_top)
2233{
2234 BL_PROFILE("MLMG::mgVcycle()");
2235
2236 const int mglev_bottom = linop.NMGLevels(amrlev) - 1;
2237
2238 for (int mglev = mglev_top; mglev < mglev_bottom; ++mglev)
2239 {
2240 BL_PROFILE_VAR("MLMG::mgVcycle_down::"+std::to_string(mglev), blp_mgv_down_lev);
2241
2242 if (verbose >= 4)
2243 {
2244 RT norm = norminf(res[amrlev][mglev],0,ncomp,IntVect(0));
2245 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev
2246 << " DN: Norm before smooth " << norm << "\n";
2247 }
2248
2249 setVal(cor[amrlev][mglev], RT(0.0));
2250 bool skip_fillboundary = true;
2251 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev], skip_fillboundary, nu1);
2252
2253 // rescor = res - L(cor)
2254 computeResOfCorrection(amrlev, mglev);
2255
2256 if (verbose >= 4)
2257 {
2258 RT norm = norminf(rescor[amrlev][mglev],0,ncomp,IntVect(0));
2259 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev
2260 << " DN: Norm after smooth " << norm << "\n";
2261 }
2262
2263 // res_crse = R(rescor_fine); this provides res/b to the level below
2264 linop.restriction(amrlev, mglev+1, res[amrlev][mglev+1], rescor[amrlev][mglev]);
2265 }
2266
2267 BL_PROFILE_VAR("MLMG::mgVcycle_bottom", blp_bottom);
2268 if (amrlev == 0)
2269 {
2270 if (verbose >= 4)
2271 {
2272 RT norm = norminf(res[amrlev][mglev_bottom],0,ncomp,IntVect(0));
2273 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev_bottom
2274 << " DN: Norm before bottom " << norm << "\n";
2275 }
2276 bottomSolve();
2277 if (verbose >= 4)
2278 {
2279 computeResOfCorrection(amrlev, mglev_bottom);
2280 RT norm = norminf(rescor[amrlev][mglev_bottom],0,ncomp,IntVect(0));
2281 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev_bottom
2282 << " UP: Norm after bottom " << norm << "\n";
2283 }
2284 }
2285 else
2286 {
2287 if (verbose >= 4)
2288 {
2289 RT norm = norminf(res[amrlev][mglev_bottom],0,ncomp,IntVect(0));
2290 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev_bottom
2291 << " Norm before smooth " << norm << "\n";
2292 }
2293 setVal(cor[amrlev][mglev_bottom], RT(0.0));
2294 bool skip_fillboundary = true;
2295 linop.smooth(amrlev, mglev_bottom, cor[amrlev][mglev_bottom],
2296 res[amrlev][mglev_bottom], skip_fillboundary, nu1);
2297 if (verbose >= 4)
2298 {
2299 computeResOfCorrection(amrlev, mglev_bottom);
2300 RT norm = norminf(rescor[amrlev][mglev_bottom],0,ncomp,IntVect(0));
2301 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev_bottom
2302 << " Norm after smooth " << norm << "\n";
2303 }
2304 }
2305 BL_PROFILE_VAR_STOP(blp_bottom);
2306
2307 for (int mglev = mglev_bottom-1; mglev >= mglev_top; --mglev)
2308 {
2309 BL_PROFILE_VAR("MLMG::mgVcycle_up::"+std::to_string(mglev), blp_mgv_up_lev);
2310 // cor_fine += I(cor_crse)
2311 addInterpCorrection(amrlev, mglev);
2312 if (verbose >= 4)
2313 {
2314 computeResOfCorrection(amrlev, mglev);
2315 RT norm = norminf(rescor[amrlev][mglev],0,ncomp,IntVect(0));
2316 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev
2317 << " UP: Norm before smooth " << norm << "\n";
2318 }
2319 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev], false, nu2);
2320
2321 if (cf_strategy == CFStrategy::ghostnodes) { computeResOfCorrection(amrlev, mglev); }
2322
2323 if (verbose >= 4)
2324 {
2325 computeResOfCorrection(amrlev, mglev);
2326 RT norm = norminf(rescor[amrlev][mglev],0,ncomp,IntVect(0));
2327 amrex::Print() << print_ident << "AT LEVEL " << amrlev << " " << mglev
2328 << " UP: Norm after smooth " << norm << "\n";
2329 }
2330 }
2331}
2332
2333// FMG cycle on the coarsest AMR level.
2334// in: Residual on the top MG level (i.e., 0)
2335// out: Correction (cor) on all MG levels
2336template <typename MF>
2337void
2339{
2340 BL_PROFILE("MLMG::mgFcycle()");
2341
2342#ifdef AMREX_USE_EB
2343 auto* pf = linop.Factory(0);
2344 auto is_all_regular = [pf] () {
2345 const auto *const f = dynamic_cast<EBFArrayBoxFactory const*>(pf);
2346 if (f) {
2347 return f->isAllRegular();
2348 } else {
2349 return true;
2350 }
2351 };
2352 amrex::ignore_unused(pf, is_all_regular);
2353 AMREX_ASSERT(linop.isCellCentered() || is_all_regular());
2354#endif
2355
2356 const int amrlev = 0;
2357 const int mg_bottom_lev = linop.NMGLevels(amrlev) - 1;
2358 IntVect nghost(0);
2359 if (cf_strategy == CFStrategy::ghostnodes) { nghost = IntVect(linop.getNGrow(amrlev)); }
2360
2361 for (int mglev = 1; mglev <= mg_bottom_lev; ++mglev)
2362 {
2363 linop.avgDownResMG(mglev, res[amrlev][mglev], res[amrlev][mglev-1]);
2364 }
2365
2366 bottomSolve();
2367
2368 for (int mglev = mg_bottom_lev-1; mglev >= 0; --mglev)
2369 {
2370 // cor_fine = I(cor_crse)
2371 interpCorrection(amrlev, mglev);
2372
2373 // rescor = res - L(cor)
2374 computeResOfCorrection(amrlev, mglev);
2375 // res = rescor; this provides b to the vcycle below
2376 LocalCopy(res[amrlev][mglev], rescor[amrlev][mglev], 0, 0, ncomp, nghost);
2377
2378 // save cor; do v-cycle; add the saved to cor
2379 std::swap(cor[amrlev][mglev], cor_hold[amrlev][mglev]);
2380 mgVcycle(amrlev, mglev);
2381 LocalAdd(cor[amrlev][mglev], cor_hold[amrlev][mglev], 0, 0, ncomp, nghost);
2382 }
2383}
2384
2385// At the true bottom of the coarsest AMR level.
2386// in : Residual (res) as b
2387// out : Correction (cor) as x
2388template <typename MF>
2389void
2391{
2392 if (do_nsolve)
2393 {
2394 NSolve(*ns_mlmg, *ns_sol, *ns_rhs);
2395 }
2396 else
2397 {
2399 }
2400}
2401
2402template <typename MF>
2403void
2404MLMGT<MF>::NSolve (MLMGT<MF>& a_solver, MF& a_sol, MF& a_rhs)
2405{
2406 BL_PROFILE("MLMG::NSolve()");
2407
2408 setVal(a_sol, RT(0.0));
2409
2410 MF const& res_bottom = res[0].back();
2411 if (BoxArray::SameRefs(boxArray(a_rhs),boxArray(res_bottom)) &&
2413 {
2414 LocalCopy(a_rhs, res_bottom, 0, 0, ncomp, IntVect(0));
2415 } else {
2416 setVal(a_rhs, RT(0.0));
2417 ParallelCopy(a_rhs, res_bottom, 0, 0, ncomp);
2418 }
2419
2420 a_solver.solve(Vector<MF*>{&a_sol}, Vector<MF const*>{&a_rhs},
2421 RT(-1.0), RT(-1.0));
2422
2423 linop.copyNSolveSolution(cor[0].back(), a_sol);
2424}
2425
2426template <typename MF>
2427void
2428MLMGT<MF>::postCG (int ret, int niters)
2429{
2430 if (niters >= 0) {
2431 m_niters_cg.push_back(niters);
2432 }
2433
2434 const int amrlev = 0;
2435 const int mglev = linop.NMGLevels(amrlev) - 1;
2436 auto& x = cor[amrlev][mglev];
2437 auto& b = res[amrlev][mglev];
2438 const int n = (ret==0) ? nub : nuf;
2439 linop.smooth(amrlev, mglev, x, b, false, n);
2440}
2441
2442template <typename MF>
2443void
2445{
2446 BL_PROFILE("MLMG::actualBottomSolve()");
2447
2448 if (!linop.isBottomActive()) { return; }
2449
2450 auto bottom_start_time = amrex::second();
2451
2452 ParallelContext::push(linop.BottomCommunicator());
2453 struct PopGuard { ~PopGuard () { ParallelContext::pop(); } } pop_guard; // NOLINT(cppcoreguidelines-special-member-functions)
2454
2455 const int amrlev = 0;
2456 const int mglev = linop.NMGLevels(amrlev) - 1;
2457 auto& x = cor[amrlev][mglev];
2458 auto& b = res[amrlev][mglev];
2459
2460 setVal(x, RT(0.0));
2461
2462 if (bottom_solver == BottomSolver::smoother)
2463 {
2464 bool skip_fillboundary = true;
2465 linop.smooth(amrlev, mglev, x, b, skip_fillboundary, nuf);
2466 }
2467 else
2468 {
2469 MF* bottom_b = &b;
2470 MF raii_b;
2471 if (linop.isBottomSingular() && linop.getEnforceSingularSolvable())
2472 {
2473 const IntVect ng = nGrowVect(b);
2474 raii_b = linop.make(amrlev, mglev, ng,
2475 MFInfo().SetArena(The_Async_Arena()));
2476 LocalCopy(raii_b, b, 0, 0, ncomp, ng);
2477 bottom_b = &raii_b;
2478
2479 makeSolvable(amrlev,mglev,*bottom_b);
2480 }
2481
2482 if (bottom_solver == BottomSolver::hypre)
2483 {
2484#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2485 if constexpr (std::is_same<MF,MultiFab>()) {
2486 bottomSolveWithHypre(x, *bottom_b);
2487 } else
2488#endif
2489 {
2490 amrex::Abort("Using Hypre as bottom solver not supported in this case");
2491 }
2492 }
2493 else if (bottom_solver == BottomSolver::petsc)
2494 {
2495#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
2496 if constexpr (std::is_same<MF,MultiFab>()) {
2497 bottomSolveWithPETSc(x, *bottom_b);
2498 } else
2499#endif
2500 {
2501 amrex::Abort("Using PETSc as bottom solver not supported in this case");
2502 }
2503 }
2504 else if (bottom_solver == BottomSolver::algmg)
2505 {
2506 if constexpr (std::is_same<MF,MultiFab>()) {
2507 bottomSolveWithAlgMG(x, *bottom_b);
2508 } else {
2509 amrex::Abort("MLMG: AlgMG is only supported for MultiFab");
2510 }
2511 }
2512 else if (bottom_solver == BottomSolver::custom && linop.supportCustomBottomSolver())
2513 {
2514 linop.customBottomSolve(this, x, *bottom_b, bottom_reltol, bottom_abstol,
2515 bottom_maxiter);
2516 }
2517 else
2518 {
2519 typename MLCGSolverT<MF>::Type cg_type;
2520 if (bottom_solver == BottomSolver::cg ||
2521 bottom_solver == BottomSolver::cgbicg) {
2522 cg_type = MLCGSolverT<MF>::Type::CG;
2523 } else {
2525 }
2526
2527 int ret = bottomSolveWithCG(x, *bottom_b, cg_type);
2528
2529 if (ret != 0 && (bottom_solver == BottomSolver::cgbicg ||
2530 bottom_solver == BottomSolver::bicgcg))
2531 {
2532 if (bottom_solver == BottomSolver::cgbicg) {
2533 cg_type = MLCGSolverT<MF>::Type::BiCGStab; // switch to bicg
2534 } else {
2535 cg_type = MLCGSolverT<MF>::Type::CG; // switch to cg
2536 }
2537 if (ret == 9) {
2538 // if ret != 9 && ret != 0, x has been set to zero in bottomSolveWithCG
2539 setVal(x, RT(0));
2540 }
2541 ret = bottomSolveWithCG(x, *bottom_b, cg_type);
2542 if (ret == 0) { // switch permanently
2543 if (cg_type == MLCGSolverT<MF>::Type::CG) {
2544 bottom_solver = BottomSolver::cg;
2545 } else {
2546 bottom_solver = BottomSolver::bicgstab;
2547 }
2548 }
2549 }
2550
2551 postCG(ret);
2552 }
2553 }
2554
2555 if (! timer.empty()) {
2556 timer[bottom_time] += amrex::second() - bottom_start_time;
2557 }
2558}
2559
2560template <typename MF>
2561int
2563{
2564 MLCGSolverT<MF> cg_solver(linop);
2565 cg_solver.setSolver(type);
2566 cg_solver.setVerbose(bottom_verbose);
2567 cg_solver.setPrintIndentation(print_ident);
2568 cg_solver.setMaxIter(bottom_maxiter);
2569 cg_solver.setInitSolnZeroed(true);
2570 if (cf_strategy == CFStrategy::ghostnodes) { cg_solver.setNGhost(linop.getNGrow()); }
2571
2572 int ret = cg_solver.solve(x, b, bottom_reltol, bottom_abstol);
2573 if (ret != 0 && verbose > 1) {
2574 amrex::Print() << print_ident << "MLMG: Bottom solve failed.\n";
2575 }
2576 m_niters_cg.push_back(cg_solver.getNumIters());
2577
2578 if (ret != 0 && ret != 9) {
2579 setVal(x, RT(0));
2580 }
2581
2582 return ret;
2583}
2584
2585// Compute multi-level Residual (res) up to amrlevmax.
2586template <typename MF>
2587void
2589{
2590 BL_PROFILE("MLMG::computeMLResidual()");
2591
2592 const int mglev = 0;
2593 for (int alev = amrlevmax; alev >= 0; --alev) {
2594 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) : nullptr;
2595 linop.solutionResidual(alev, res[alev][mglev], sol[alev], rhs[alev], crse_bcdata);
2596 if (alev < finest_amr_lev) {
2597 linop.reflux(alev, res[alev][mglev], sol[alev], rhs[alev],
2598 res[alev+1][mglev], sol[alev+1], rhs[alev+1]);
2599 }
2600 }
2601}
2602
2603// Compute single AMR level residual without masking.
2604template <typename MF>
2605void
2607{
2608 BL_PROFILE("MLMG::computeResidual()");
2609 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) : nullptr;
2610 linop.solutionResidual(alev, res[alev][0], sol[alev], rhs[alev], crse_bcdata);
2611}
2612
2613// Compute coarse AMR level composite residual with coarse solution and fine correction
2614template <typename MF>
2615void
2617{
2618 BL_PROFILE("MLMG::computeResWithCrseSolFineCor()");
2619
2620 IntVect nghost(0);
2621 if (cf_strategy == CFStrategy::ghostnodes) {
2622 nghost = IntVect(std::min(linop.getNGrow(falev),linop.getNGrow(calev)));
2623 }
2624
2625 MF& crse_sol = sol[calev];
2626 const MF& crse_rhs = rhs[calev];
2627 MF& crse_res = res[calev][0];
2628
2629 MF& fine_sol = sol[falev];
2630 const MF& fine_rhs = rhs[falev];
2631 MF& fine_cor = cor[falev][0];
2632 MF& fine_res = res[falev][0];
2633 MF& fine_rescor = rescor[falev][0];
2634
2635 const MF* crse_bcdata = (calev > 0) ? &(sol[calev-1]) : nullptr;
2636 linop.solutionResidual(calev, crse_res, crse_sol, crse_rhs, crse_bcdata);
2637
2638 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res, BCMode::Homogeneous);
2639 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2640
2641 linop.reflux(calev, crse_res, crse_sol, crse_rhs, fine_res, fine_sol, fine_rhs);
2642
2643 linop.avgDownResAmr(calev, crse_res, fine_res);
2644}
2645
2646// Compute fine AMR level residual fine_res = fine_res - L(fine_cor) with coarse providing BC.
2647template <typename MF>
2648void
2650{
2651 BL_PROFILE("MLMG::computeResWithCrseCorFineCor()");
2652
2653 IntVect nghost(0);
2654 if (cf_strategy == CFStrategy::ghostnodes) {
2655 nghost = IntVect(linop.getNGrow(falev));
2656 }
2657
2658 const MF& crse_cor = cor[falev-1][0];
2659
2660 MF& fine_cor = cor [falev][0];
2661 MF& fine_res = res [falev][0];
2662 MF& fine_rescor = rescor[falev][0];
2663
2664 // fine_rescor = fine_res - L(fine_cor)
2665 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res,
2666 BCMode::Inhomogeneous, &crse_cor);
2667 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2668}
2669
2670// Interpolate correction from coarse to fine AMR level.
2671template <typename MF>
2672void
2674{
2675 BL_PROFILE("MLMG::interpCorrection_1");
2676
2677 IntVect nghost(0);
2678 if (cf_strategy == CFStrategy::ghostnodes) {
2679 nghost = IntVect(linop.getNGrow(alev));
2680 }
2681
2682 MF & crse_cor = cor[alev-1][0];
2683 MF & fine_cor = cor[alev ][0];
2684
2685 const Geometry& crse_geom = linop.Geom(alev-1,0);
2686
2687 int ng_src = 0;
2688 int ng_dst = linop.isCellCentered() ? 1 : 0;
2689 if (cf_strategy == CFStrategy::ghostnodes)
2690 {
2691 ng_src = linop.getNGrow(alev-1);
2692 ng_dst = linop.getNGrow(alev-1);
2693 if constexpr (IsMultiFabLike_v<MF>) {
2694 crse_cor.FillBoundary(0, ncomp, IntVect(ng_src), crse_geom.periodicity());
2695 } else {
2696 amrex::Abort("MLMG: CFStrategy::ghostnodes not supported for non-MultiFab like types");
2697 }
2698 }
2699
2700 MF cfine = linop.makeCoarseAmr(alev, IntVect(ng_dst),
2701 MFInfo().SetArena(The_Async_Arena()));
2702 setVal(cfine, RT(0.0));
2703 ParallelCopy(cfine, crse_cor, 0, 0, ncomp, IntVect(ng_src), IntVect(ng_dst),
2704 crse_geom.periodicity());
2705
2706 linop.interpolationAmr(alev, fine_cor, cfine, nghost); // NOLINT(readability-suspicious-call-argument)
2707}
2708
2709// Interpolate correction between MG levels
2710// inout: Correction (cor) on coarse MG lev. (out due to FillBoundary)
2711// out : Correction (cor) on fine MG lev.
2712template <typename MF>
2713void
2714MLMGT<MF>::interpCorrection (int alev, int mglev)
2715{
2716 BL_PROFILE("MLMG::interpCorrection_2");
2717
2718 MF& crse_cor = cor[alev][mglev+1];
2719 MF& fine_cor = cor[alev][mglev ];
2720 linop.interpAssign(alev, mglev, fine_cor, crse_cor);
2721}
2722
2723// (Fine MG level correction) += I(Coarse MG level correction)
2724template <typename MF>
2725void
2727{
2728 BL_PROFILE("MLMG::addInterpCorrection()");
2729
2730 const MF& crse_cor = cor[alev][mglev+1];
2731 MF& fine_cor = cor[alev][mglev ];
2732
2733 MF cfine;
2734 const MF* cmf;
2735
2736 if (linop.isMFIterSafe(alev, mglev, mglev+1))
2737 {
2738 cmf = &crse_cor;
2739 }
2740 else
2741 {
2742 cfine = linop.makeCoarseMG(alev, mglev, IntVect(0),
2743 MFInfo().SetArena(The_Async_Arena()));
2744 ParallelCopy(cfine, crse_cor, 0, 0, ncomp);
2745 cmf = &cfine;
2746 }
2747
2748 linop.interpolation(alev, mglev, fine_cor, *cmf);
2749}
2750
2751// Compute rescor = res - L(cor)
2752// in : res
2753// inout: cor (out due to FillBoundary in linop.correctionResidual)
2754// out : rescor
2755template <typename MF>
2756void
2758{
2759 BL_PROFILE("MLMG:computeResOfCorrection()");
2760 MF & x = cor[amrlev][mglev];
2761 const MF& b = res[amrlev][mglev];
2762 MF & r = rescor[amrlev][mglev];
2763 linop.correctionResidual(amrlev, mglev, r, x, b, BCMode::Homogeneous);
2764}
2765
2766// Compute single-level masked inf-norm of Residual (res).
2767template <typename MF>
2768auto
2769MLMGT<MF>::ResNormInf (int alev, bool local) -> RT
2770{
2771 BL_PROFILE("MLMG::ResNormInf()");
2772 return linop.normInf(alev, res[alev][0], local);
2773}
2774
2775// Computes multi-level masked inf-norm of Residual (res).
2776template <typename MF>
2777auto
2778MLMGT<MF>::MLResNormInf (int alevmax, bool local) -> RT
2779{
2780 BL_PROFILE("MLMG::MLResNormInf()");
2781 RT r = RT(0.0);
2782 for (int alev = 0; alev <= alevmax; ++alev)
2783 {
2784 r = std::max(r, ResNormInf(alev,true));
2785 }
2787 return r;
2788}
2789
2790// Compute multi-level masked inf-norm of RHS (rhs).
2791template <typename MF>
2792auto
2794{
2795 BL_PROFILE("MLMG::MLRhsNormInf()");
2796 RT r = RT(0.0);
2797 for (int alev = 0; alev <= finest_amr_lev; ++alev) {
2798 auto t = linop.normInf(alev, rhs[alev], true);
2799 r = std::max(r, t);
2800 }
2802 return r;
2803}
2804
2805template <typename MF>
2806void
2808{
2809 auto const& offset = linop.getSolvabilityOffset(0, 0, rhs[0]);
2810 if (verbose >= 4) {
2811 for (int c = 0; c < ncomp; ++c) {
2812 amrex::Print() << print_ident << "MLMG: Subtracting " << offset[c] << " from rhs component "
2813 << c << "\n";
2814 }
2815 }
2816 for (int alev = 0; alev < namrlevs; ++alev) {
2817 linop.fixSolvabilityByOffset(alev, 0, rhs[alev], offset);
2818 }
2819}
2820
2821template <typename MF>
2822void
2823MLMGT<MF>::makeSolvable (int amrlev, int mglev, MF& mf)
2824{
2825 auto const& offset = linop.getSolvabilityOffset(amrlev, mglev, mf);
2826 if (verbose >= 4) {
2827 for (int c = 0; c < ncomp; ++c) {
2828 amrex::Print() << print_ident << "MLMG: Subtracting " << offset[c]
2829 << " from mf component c = " << c
2830 << " on level (" << amrlev << ", " << mglev << ")\n";
2831 }
2832 }
2833 linop.fixSolvabilityByOffset(amrlev, mglev, mf, offset);
2834}
2835
2836template <typename MF>
2837template <class TMF>
2838requires (std::same_as<TMF,MultiFab>)
2839void
2840MLMGT<MF>::configureAlgMG (MLAlgMG& s, bool singular, bool with_options)
2841{
2842 // Dependent type: AlgMG stays a forward declaration in this header.
2843 AlgMG<RT>& a = s.solver();
2844 a.setVerbose(bottom_verbose);
2845 a.setPrintIndentation(print_ident + " ");
2846 a.setThrowException(throw_exception);
2847 // Settings that select the setup (singular flag, Krylov type, the user's
2848 // options) are applied once per assembled matrix. Reapplying them on
2849 // every solve could toggle a value against the callback and redo the
2850 // expensive AlgMG setup each time.
2851 if (with_options) {
2852 a.setSingular(singular);
2854 if (m_algmg_options) { m_algmg_options(a); }
2855 }
2856}
2857
2858template <typename MF>
2859template <class TMF>
2860requires (std::same_as<TMF,MultiFab>)
2861void
2862MLMGT<MF>::algmgSolve (MLAlgMG& s, MF& x, MF const& b)
2863{
2864 // As a preconditioner MLMG must be a fixed linear operation: one V-cycle.
2865 if (precond_mode) {
2866 s.applyVcycle(x, b);
2867 } else {
2868 try {
2869 s.solve(x, b, m_algmg_reltol, RT(0), bottom_maxiter);
2870 } catch (std::runtime_error const& e) {
2871 throw error(e.what());
2872 }
2873 }
2874}
2875
2876template <typename MF>
2877template <class TMF>
2878requires (std::same_as<TMF,MultiFab>)
2879void
2881{
2882 const int amrlev = 0;
2883 const int mglev = linop.NMGLevels(amrlev) - 1;
2884
2885 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1, "MLMG: AlgMG needs ncomp == 1");
2886
2887 bool const fresh = (m_algmg_bottom == nullptr);
2888 if (fresh) { m_algmg_bottom = linop.makeAlgMG(mglev); }
2889 configureAlgMG(*m_algmg_bottom, linop.isBottomSingular(), fresh);
2890 algmgSolve(*m_algmg_bottom, x, b);
2891
2892 if (linop.isSingular(amrlev) && linop.getEnforceSingularSolvable()) {
2893 makeSolvable(amrlev, mglev, x);
2894 }
2895}
2896
2897template <typename MF>
2898template <class TMF>
2899requires (std::same_as<TMF,MultiFab>)
2900void
2902{
2903 BL_PROFILE("MLMG::algmgLevel0Solve()");
2904
2905 auto t0 = amrex::second();
2906
2907 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1, "MLMG: AlgMG needs ncomp == 1");
2908
2909 bool const fresh = (m_algmg_level0 == nullptr);
2910 if (fresh) { m_algmg_level0 = linop.makeAlgMG(0); }
2911 configureAlgMG(*m_algmg_level0, linop.isSingular(0), fresh);
2912 algmgSolve(*m_algmg_level0, cor[0][0], res[0][0]);
2913
2914 // Known overset cells must not receive a correction; the matrix rows
2915 // there are identities with a zero rhs, so this is a safety pass.
2916 linop.applyOverset(0, cor[0][0]);
2917
2918 if (linop.isSingular(0) && linop.getEnforceSingularSolvable()) {
2919 makeSolvable(0, 0, cor[0][0]);
2920 }
2921
2922 if (! timer.empty()) {
2923 timer[bottom_time] += amrex::second() - t0;
2924 }
2925}
2926
2927template <typename MF>
2928bool
2930 RT best_norm, std::string& reason) const
2931{
2932 RT const rnorm = norms.back();
2933 if (!amrex::isfinite(rnorm)) {
2934 reason = "non-finite residual";
2935 return true;
2936 }
2937 if (rnorm > RT(1.e20)*max_norm ||
2938 rnorm > hybrid_divergence_factor*best_norm) {
2939 reason = "diverging";
2940 return true;
2941 }
2942 const int w = hybrid_stall_window;
2943 if (w > 0 && int(norms.size()) > w) {
2944 const RT r_old = norms[norms.size()-1-w];
2945 if (rnorm > std::pow(hybrid_stall_rate, RT(w)) * r_old) {
2946 reason = "stalling";
2947 return true;
2948 }
2949 }
2950 return false;
2951}
2952
2953#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2954template <typename MF>
2955template <class TMF>
2956requires (std::same_as<TMF,MultiFab>)
2957void
2958MLMGT<MF>::bottomSolveWithHypre (MF& x, const MF& b)
2959{
2960 const int amrlev = 0;
2961 const int mglev = linop.NMGLevels(amrlev) - 1;
2962
2963 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1, "bottomSolveWithHypre doesn't work with ncomp > 1");
2964
2965 if (linop.isCellCentered())
2966 {
2967 if (hypre_solver == nullptr) // We should reuse the setup
2968 {
2969 hypre_solver = linop.makeHypre(hypre_interface);
2970
2971 hypre_solver->setVerbose(bottom_verbose);
2972 if (hypre_interface == amrex::Hypre::Interface::ij) {
2973 hypre_solver->setHypreOptionsNamespace(hypre_options_namespace);
2974 } else {
2975 hypre_solver->setHypreOldDefault(hypre_old_default);
2976 hypre_solver->setHypreRelaxType(hypre_relax_type);
2977 hypre_solver->setHypreRelaxOrder(hypre_relax_order);
2978 hypre_solver->setHypreNumSweeps(hypre_num_sweeps);
2979 hypre_solver->setHypreStrongThreshold(hypre_strong_threshold);
2980 }
2981
2982 const BoxArray& ba = linop.m_grids[amrlev].back();
2983 const DistributionMapping& dm = linop.m_dmap[amrlev].back();
2984 const Geometry& geom = linop.m_geom[amrlev].back();
2985
2986 hypre_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
2987 hypre_bndry->setHomogValues();
2988 const Real* dx = linop.m_geom[0][0].CellSize();
2989 IntVect crse_ratio = linop.m_coarse_data_crse_ratio.allGT(0) ? linop.m_coarse_data_crse_ratio : IntVect(1);
2990 RealVect bclocation(AMREX_D_DECL(0.5*dx[0]*crse_ratio[0],
2991 0.5*dx[1]*crse_ratio[1],
2992 0.5*dx[2]*crse_ratio[2]));
2993 hypre_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc, IntVect(-1), bclocation,
2994 linop.m_coarse_fine_bc_type);
2995 }
2996
2997 // IJ interface understands absolute tolerance API of HYPRE
2998 amrex::Real hypre_abstol =
2999 (hypre_interface == amrex::Hypre::Interface::ij)
3000 ? bottom_abstol : Real(-1.0);
3001 hypre_solver->setThrowException(throw_exception);
3002 try {
3003 hypre_solver->solve(
3004 x, b, bottom_reltol, hypre_abstol, bottom_maxiter, *hypre_bndry,
3005 linop.getMaxOrder());
3006 } catch (std::runtime_error const& e) {
3007 throw error(e.what());
3008 }
3009 }
3010 else
3011 {
3012 if (hypre_node_solver == nullptr)
3013 {
3014 hypre_node_solver =
3015 linop.makeHypreNodeLap(bottom_verbose, hypre_options_namespace);
3016 }
3017 hypre_node_solver->setThrowException(throw_exception);
3018 try {
3019 hypre_node_solver->solve(x, b, bottom_reltol, bottom_abstol, bottom_maxiter);
3020 } catch (std::runtime_error const& e) {
3021 throw error(e.what());
3022 }
3023 }
3024
3025 // For singular problems there may be a large constant added to all values of the solution
3026 // For precision reasons we enforce that the average of the correction from HYPRE is 0
3027 if (linop.isSingular(amrlev) && linop.getEnforceSingularSolvable())
3028 {
3029 makeSolvable(amrlev, mglev, x);
3030 }
3031}
3032#endif
3033
3034#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
3035template <typename MF>
3036template <class TMF>
3037requires (std::same_as<TMF,MultiFab>)
3038void
3039MLMGT<MF>::bottomSolveWithPETSc (MF& x, const MF& b)
3040{
3041 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ncomp == 1, "bottomSolveWithPETSc doesn't work with ncomp > 1");
3042
3043 if(petsc_solver == nullptr)
3044 {
3045 petsc_solver = linop.makePETSc();
3046 petsc_solver->setVerbose(bottom_verbose);
3047
3048 const BoxArray& ba = linop.m_grids[0].back();
3049 const DistributionMapping& dm = linop.m_dmap[0].back();
3050 const Geometry& geom = linop.m_geom[0].back();
3051
3052 petsc_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
3053 petsc_bndry->setHomogValues();
3054 const Real* dx = linop.m_geom[0][0].CellSize();
3055 auto crse_ratio = linop.m_coarse_data_crse_ratio.allGT(0) ? linop.m_coarse_data_crse_ratio : IntVect(1);
3056 RealVect bclocation(AMREX_D_DECL(0.5*dx[0]*crse_ratio[0],
3057 0.5*dx[1]*crse_ratio[1],
3058 0.5*dx[2]*crse_ratio[2]));
3059 petsc_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc, IntVect(-1), bclocation,
3060 linop.m_coarse_fine_bc_type);
3061 }
3062 petsc_solver->solve(x, b, bottom_reltol, Real(-1.), bottom_maxiter, *petsc_bndry,
3063 linop.getMaxOrder());
3064}
3065#endif
3066
3067template <typename MF>
3068void
3069MLMGT<MF>::checkPoint (const Vector<MultiFab*>& a_sol,
3070 const Vector<MultiFab const*>& a_rhs,
3071 RT a_tol_rel, RT a_tol_abs, const char* a_file_name) const
3072{
3073 std::string file_name(a_file_name);
3074 UtilCreateCleanDirectory(file_name, false);
3075
3077 {
3078 std::string HeaderFileName(std::string(a_file_name)+"/Header");
3079 std::ofstream HeaderFile;
3080 HeaderFile.open(HeaderFileName.c_str(), std::ofstream::out |
3081 std::ofstream::trunc |
3082 std::ofstream::binary);
3083 if( ! HeaderFile.good()) {
3084 FileOpenFailed(HeaderFileName);
3085 }
3086
3087 HeaderFile.precision(17);
3088
3089 std::string norm_name = getEnumNameString(norm_type);
3090
3091 HeaderFile << linop.name() << "\n"
3092 << "a_tol_rel = " << a_tol_rel << "\n"
3093 << "a_tol_abs = " << a_tol_abs << "\n"
3094 << "verbose = " << verbose << "\n"
3095 << "max_iters = " << max_iters << "\n"
3096 << "nu1 = " << nu1 << "\n"
3097 << "nu2 = " << nu2 << "\n"
3098 << "nuf = " << nuf << "\n"
3099 << "nub = " << nub << "\n"
3100 << "max_fmg_iters = " << max_fmg_iters << "\n"
3101 << "bottom_solver = " << static_cast<int>(bottom_solver) << "\n"
3102 << "bottom_verbose = " << bottom_verbose << "\n"
3103 << "bottom_maxiter = " << bottom_maxiter << "\n"
3104 << "bottom_reltol = " << bottom_reltol << "\n"
3105 << "convergence_norm = " << norm_name << "\n"
3106 << "namrlevs = " << namrlevs << "\n"
3107 << "finest_amr_lev = " << finest_amr_lev << "\n"
3108 << "linop_prepared = " << linop_prepared << "\n"
3109 << "solve_called = " << solve_called << "\n";
3110
3111 for (int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
3112 UtilCreateCleanDirectory(file_name+"/Level_"+std::to_string(ilev), false);
3113 }
3114 }
3115
3117
3118 for (int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
3119 VisMF::Write(*a_sol[ilev], file_name+"/Level_"+std::to_string(ilev)+"/sol");
3120 VisMF::Write(*a_rhs[ilev], file_name+"/Level_"+std::to_string(ilev)+"/rhs");
3121 }
3122
3123 linop.checkPoint(file_name+"/linop");
3124}
3125
3126template <typename MF>
3127void
3129{
3130 print_ident.resize(print_ident.size()+4, ' ');
3131}
3132
3133template <typename MF>
3134void
3136{
3137 if (print_ident.size() > 4) {
3138 print_ident.resize(print_ident.size()-4, ' ');
3139 } else {
3140 print_ident.clear();
3141 }
3142}
3143
3144extern template class MLMGT<MultiFab>;
3145#ifdef AMREX_MLMG_HAS_FLOAT
3146extern template class MLMGT<fMultiFab>;
3147#endif
3148
3151
3152}
3153
3154#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define BL_PROFILE_VAR_STOP(vname)
Definition AMReX_BLProfiler.H:605
#define BL_PROFILE_VAR(fname, vname)
Definition AMReX_BLProfiler.H:602
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
Enum reflection utilities and the AMREX_ENUM macro.
#define AMREX_HOST_DEVICE_PARALLEL_FOR_4D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:111
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Algebraic multigrid solver for SpMatrix / AlgVector systems.
Definition AMReX_AlgMG.H:254
void setSingular(bool b)
Definition AMReX_AlgMG.H:336
void setPrintIndentation(std::string s)
Prefix of the lines the solver prints, e.g. to nest them in MLMG output.
Definition AMReX_AlgMG.H:292
void setThrowException(bool b)
Definition AMReX_AlgMG.H:376
void setVerbose(int v)
Definition AMReX_AlgMG.H:290
void setKrylovSolver(KrylovSolver a)
Krylov solver with the V-cycle as preconditioner (see KrylovSolver). Default None.
Definition AMReX_AlgMG.H:353
Print on all processors of the default communicator.
Definition AMReX_Print.H:113
Reference-counted collection of Boxes.
Definition AMReX_BoxArray.H:681
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition AMReX_BoxArray.H:1252
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:55
static bool SameRefs(const DistributionMapping &lhs, const DistributionMapping &rhs)
Return true if lhs and rhs share the same underlying Ref.
Definition AMReX_DistributionMapping.H:295
Definition AMReX_EBFabFactory.H:32
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:87
Periodicity periodicity() const noexcept
Return the Periodicity based on the length of the domain.
Definition AMReX_Geometry.H:426
Interface
HYPRE interface modes supported.
Definition AMReX_Hypre.H:70
@ ij
Definition AMReX_Hypre.H:70
__host__ __device__ constexpr bool allGT(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than argument for all components. NOTE: This is NOT a strict weak ord...
Definition AMReX_IntVect.H:524
__host__ static __device__ constexpr IntVectND< dim > TheDimensionVector(int d) noexcept
Definition AMReX_IntVect.H:797
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:88
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:176
Algebraic system of one MG level of AMR level 0 of an MLLinOp, solved with AlgMG.
Definition AMReX_MLAlgMG.H:26
void applyVcycle(MultiFab &soln, MultiFab const &rhs)
One AlgMG V-cycle with zero initial guess: the preconditioner form.
Definition AMReX_MLAlgMG.cpp:145
AlgMG< Real > & solver() noexcept
The solver, for settings such as verbosity and Krylov acceleration.
Definition AMReX_MLAlgMG.cpp:142
void solve(MultiFab &soln, MultiFab const &rhs, Real reltol, Real abstol, int maxiter)
Solve A x = b with zero initial guess.
Definition AMReX_MLAlgMG.cpp:155
void setSolver(Type _typ) noexcept
Switch between BiCGStab and CG after construction.
Definition AMReX_MLCGSolver.H:48
void setVerbose(int _verbose)
Control how much logging is emitted (0 = silent).
Definition AMReX_MLCGSolver.H:72
int getNumIters() const noexcept
Iteration count from the last solve* call (or -1 if unused).
Definition AMReX_MLCGSolver.H:151
void setInitSolnZeroed(bool _sol_zeroed)
Definition AMReX_MLCGSolver.H:99
int solve(MF &solnL, const MF &rhsL, RT eps_rel, RT eps_abs)
Solve Lp(solnL)=rhsL to the requested tolerance.
Definition AMReX_MLCGSolver.H:176
Type
Definition AMReX_MLCGSolver.H:27
@ BiCGStab
Definition AMReX_MLCGSolver.H:27
@ CG
Definition AMReX_MLCGSolver.H:27
void setNGhost(int _nghost)
Set the number of grow cells used when allocating temporaries.
Definition AMReX_MLCGSolver.H:108
void setMaxIter(int _maxiter)
Cap the number of Krylov iterations performed.
Definition AMReX_MLCGSolver.H:81
void setPrintIndentation(std::string s)
Prefix printed messages (e.g., to indent per level).
Definition AMReX_MLCGSolver.H:90
Definition AMReX_MLLinOp.H:156
bool isCellCentered() const noexcept
Definition AMReX_MLLinOp.H:1248
void setVerbose(int v) noexcept
Set verbosity.
Definition AMReX_MLLinOp.H:324
virtual std::string name() const
Definition AMReX_MLLinOp.H:202
Vector< Array< BCType, 3 > > m_hibc
Definition AMReX_MLLinOp.H:966
Vector< Vector< BoxArray > > m_grids
Definition AMReX_MLLinOp.H:1008
int getMaxOrder() const noexcept
Get order of interpolation at coarse/fine boundary.
Definition AMReX_MLLinOp.H:346
typename FabDataType< MF >::fab_type FAB
Definition AMReX_MLLinOp.H:168
Vector< Vector< DistributionMapping > > m_dmap
Definition AMReX_MLLinOp.H:1009
IntVect m_coarse_data_crse_ratio
Definition AMReX_MLLinOp.H:1037
typename FabDataType< MF >::value_type RT
Definition AMReX_MLLinOp.H:169
bool hasHiddenDimension() const noexcept
Definition AMReX_MLLinOp.H:1293
int hiddenDirection() const noexcept
Definition AMReX_MLLinOp.H:1294
Vector< Array< BCType, 3 > > m_lobc
Definition AMReX_MLLinOp.H:965
Vector< Vector< Geometry > > m_geom
first Vector is for amr level and second is mg level
Definition AMReX_MLLinOp.H:1007
LinOpEnumType::Location Location
Definition AMReX_MLLinOp.H:174
int NMGLevels(int amrlev) const noexcept
Return the number of MG levels at given AMR level.
Definition AMReX_MLLinOp.H:959
LinOpEnumType::BCMode BCMode
Definition AMReX_MLLinOp.H:172
static constexpr bool mixed_precision_capable
Definition AMReX_MLLinOp.H:1047
LinOpBCType m_coarse_fine_bc_type
Definition AMReX_MLLinOp.H:1036
virtual bool isSingular(int amrlev) const =0
Is it singular on AMR level amrlev?
bool getEnforceSingularSolvable() const noexcept
Definition AMReX_MLLinOp.H:359
Definition AMReX_MLMG.H:44
Definition AMReX_MLMG.H:39
void prepareForFluxes(Vector< MF const * > const &a_sol)
Build boundary caches needed by getFluxes()/compFluxes.
Definition AMReX_MLMG.H:1295
void setBottomVerbose(int v) noexcept
Verbosity for the bottom solver (0 silent).
Definition AMReX_MLMG.H:389
void setMaxFmgIter(int n) noexcept
Cap the number of FMG cycles executed.
Definition AMReX_MLMG.H:295
RT MLResNormInf(int alevmax, bool local=false)
Composite infinity norm of the residual up to level alevmax.
Definition AMReX_MLMG.H:2778
void postCG(int ret, int niters=-1)
Post CG smoothing.
Definition AMReX_MLMG.H:2428
RT MLRhsNormInf(bool local=false)
Composite infinity norm of the RHS.
Definition AMReX_MLMG.H:2793
void setNoGpuSync(bool do_not_sync) noexcept
Control implicit GPU synchronization inside solve().
Definition AMReX_MLMG.H:462
MLMGT(MLMGT< MF > &&)=delete
bool hybridShouldSwitch(Vector< RT > const &norms, RT max_norm, RT best_norm, std::string &reason) const
Definition AMReX_MLMG.H:2929
void actualBottomSolve()
Execute the actual bottom solve after pre-smoothing and restriction.
Definition AMReX_MLMG.H:2444
MF MFType
Definition AMReX_MLMG.H:53
void interpCorrection(int alev, int mglev)
Interpolate corrections on (alev,mglev).
Definition AMReX_MLMG.H:2714
BottomSolver getBottomSolver() const noexcept
Definition AMReX_MLMG.H:342
void setPreSmooth(int n) noexcept
Number of pre-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:316
void getFluxes(const Vector< AMF * > &a_flux, Location a_loc=Location::CellCenter)
Cell-centered flux helper.
Definition AMReX_MLMG.H:1419
void setBottomToleranceAbs(RT t) noexcept
Absolute tolerance for the bottom solver (not used by AlgMG and PETSc).
Definition AMReX_MLMG.H:407
RT getFinalResidual() const noexcept
Definition AMReX_MLMG.H:737
void interpCorrection(int alev)
Interpolate corrections onto AMR level alev.
Definition AMReX_MLMG.H:2673
void getEBFluxes(const Vector< MF * > &a_eb_flux)
Flux into the EB wall using the internally stored solution.
Definition AMReX_MLMG.H:1516
void getEBFluxes(const Vector< MF * > &a_eb_flux, const Vector< MF * > &a_sol)
Flux into the EB wall using an explicit solution vector.
Definition AMReX_MLMG.H:1528
friend class GMRESMLMGT
Definition AMReX_MLMG.H:50
void getGradSolution(const Vector< Array< AMF *, 3 > > &a_grad_sol, Location a_loc=Location::FaceCenter)
Populate gradient components of the converged solution.
Definition AMReX_MLMG.H:1306
void setBottomSmooth(int n) noexcept
Additional smoothing passes executed after the bottom solver.
Definition AMReX_MLMG.H:334
void setNSolve(int flag) noexcept
Enable (flag!=0) or disable the N-solve path.
Definition AMReX_MLMG.H:439
int getBottomVerbose() const
Definition AMReX_MLMG.H:263
void computeResOfCorrection(int amrlev, int mglev)
Compute the residual of the correction equation on (amrlev,mglev).
Definition AMReX_MLMG.H:2757
void applyPrecond(const Vector< MF * > &out, const Vector< MF * > &in)
Apply the linear operator as a preconditioner (out = L(in)).
Definition AMReX_MLMG.H:1695
void setCFStrategy(CFStrategy a_cf_strategy) noexcept
Select the coarse-fine synchronization strategy.
Definition AMReX_MLMG.H:383
void computeResWithCrseCorFineCor(int falev)
Residual update using coarse correction / fine correction.
Definition AMReX_MLMG.H:2649
void NSolve(MLMGT< MF > &a_solver, MF &a_sol, MF &a_rhs)
Perform an NSolve using an MLMGT wrapper.
Definition AMReX_MLMG.H:2404
void getFluxes(const Vector< Array< AMF *, 3 > > &a_flux, const Vector< AMF * > &a_sol, Location a_loc=Location::FaceCenter)
Face-centered flux helper that accepts an explicit solution vector.
Definition AMReX_MLMG.H:1377
typename MLLinOpT< MF >::Location Location
Definition AMReX_MLMG.H:58
void apply(const Vector< MF * > &out, const Vector< MF * > &in)
out = L(in). Note that, if no actual solve is needed, one could turn off multigrid coarsening by cons...
Definition AMReX_MLMG.H:1614
void getFluxes(const Vector< Array< AMF *, 3 > > &a_flux, Location a_loc=Location::FaceCenter)
Face-centered flux helper (-b grad(phi) for alpha a - beta div(b grad)).
Definition AMReX_MLMG.H:1337
void setNSolveGridSize(int s) noexcept
Set the tile size used for N-solve builds.
Definition AMReX_MLMG.H:445
void setVerbose(int v) noexcept
Set the main solver verbosity (0 silent).
Definition AMReX_MLMG.H:283
MultigridType getMultigridType() const noexcept
Definition AMReX_MLMG.H:346
void computeMLResidual(int amrlevmax)
Compute the composite residual norm up to AMR level amrlevmax.
Definition AMReX_MLMG.H:2588
void prepareSolRHS(Vector< AMF * > const &a_sol, Vector< AMF const * > const &a_rhs)
Set up sol and rhs from the user's data.
Definition AMReX_MLMG.H:1724
RT getInitResidual() const noexcept
Definition AMReX_MLMG.H:735
int getNumIters() const noexcept
Definition AMReX_MLMG.H:740
void setPostSmooth(int n) noexcept
Number of post-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:322
void spIter(int iter)
Mixed precision: one multigrid iteration on the single precision copy.
Definition AMReX_MLMG.H:2121
void mgVcycle(int amrlev, int mglev)
Run a multigrid V-cycle on (amrlev,mglev).
Definition AMReX_MLMG.H:2232
void prepareForNSolve()
Prepare the NSolve path.
Definition AMReX_MLMG.H:1945
RT precond(Vector< MF * > const &a_sol, Vector< MF const * > const &a_rhs, RT a_tol_rel, RT a_tol_abs)
Apply MLMG as a right-preconditioner with relaxed tolerances.
Definition AMReX_MLMG.H:1277
void makeSolvable(int amrlev, int mglev, MF &mf)
Adjust a field on (amrlev,mglev) to satisfy null-space constraints.
Definition AMReX_MLMG.H:2823
void getFluxes(std::initializer_list< Array< AMF *, 3 > > a_flux, std::initializer_list< AMF * > a_sol, Location a_loc=Location::FaceCenter)
Initializer-list convenience overload for the face-centered flux helper above.
Definition AMReX_MLMG.H:1409
void setHybridStallCriterion(int window, RT rate) noexcept
Hybrid mode: switch to AlgMG when the residual has not dropped by rate per iteration on average over ...
Definition AMReX_MLMG.H:372
void makeSolvable()
Adjust RHS/solution to satisfy null-space constraints.
Definition AMReX_MLMG.H:2807
void setBottomSolver(BottomSolver s) noexcept
Select the bottom solver type (e.g., CG, BiCGStab, Hypre, PETSc).
Definition AMReX_MLMG.H:341
void preparePrecond()
Prepare preconditioner-specific caches (e.g., boundary data).
Definition AMReX_MLMG.H:1937
friend class MLCGSolverT
Definition AMReX_MLMG.H:49
void incPrintIdentation()
Increase the indentation used when printing solver logs.
Definition AMReX_MLMG.H:3128
typename MLLinOpT< MF >::RT RT
Definition AMReX_MLMG.H:55
void setThrowException(bool t) noexcept
Control behavior when the solve fails to converge or blows up.
Definition AMReX_MLMG.H:277
void decPrintIdentation()
Decrease the indentation used when printing solver logs.
Definition AMReX_MLMG.H:3135
void setFixedIter(int nit) noexcept
Set the number of fixed MLMG iterations (convergence may still exit early if the residual is already ...
Definition AMReX_MLMG.H:302
void setMultigridType(MultigridType t) noexcept
Select how AMR level 0 is solved. See MultigridType.
Definition AMReX_MLMG.H:345
Vector< RT > const & getResidualHistory() const noexcept
Definition AMReX_MLMG.H:739
void prepareLinOp()
Finalize operator-dependent metadata before iterating.
Definition AMReX_MLMG.H:1909
void setPrecondIter(int nit) noexcept
Set how many MLMG iterations the preconditioner executes per Krylov call (still subject to early-conv...
Definition AMReX_MLMG.H:309
void prepareWorkArrays()
Allocate (first solve) and zero the work arrays.
Definition AMReX_MLMG.H:1814
CFStrategy
Definition AMReX_MLMG.H:62
@ none
Definition AMReX_MLMG.H:62
@ ghostnodes
Definition AMReX_MLMG.H:62
amrex::BottomSolver BottomSolver
Definition AMReX_MLMG.H:60
void prepareForSolve(Vector< AMF * > const &a_sol, Vector< AMF const * > const &a_rhs)
Prepare linear operators, coefficients, and RHS data prior to solving.
Definition AMReX_MLMG.H:1707
int bottomSolveWithCG(MF &x, const MF &b, typename MLCGSolverT< MF >::Type type)
Bottom solve using CG/BiCGStab implemented in MLCGSolverT.
Definition AMReX_MLMG.H:2562
void getGradSolution(std::initializer_list< Array< AMF *, 3 > > a_grad_sol, Location a_loc=Location::FaceCenter)
Initializer-list convenience overload for getGradSolution.
Definition AMReX_MLMG.H:1329
void setAlwaysUseBNorm(int flag) noexcept
Deprecated flag for forcing B-norm convergence checks.
Definition AMReX_MLMG.H:892
bool usedAlgMG() const noexcept
Definition AMReX_MLMG.H:349
void compResidual(const Vector< MF * > &a_res, const Vector< MF * > &a_sol, const Vector< MF const * > &a_rhs)
Compute multilevel residuals a_rhs - L(a_sol) on each AMR level.
Definition AMReX_MLMG.H:1542
void miniCycle(int amrlev)
Execute a per-level mini cycle.
Definition AMReX_MLMG.H:2221
RT solve(std::initializer_list< AMF * > a_sol, std::initializer_list< AMF const * > a_rhs, RT a_tol_rel, RT a_tol_abs, const char *checkpoint_file=nullptr)
Convenience initializer-list overload that forwards to the Vector-based solve.
void getFluxes(const Vector< AMF * > &a_flux, const Vector< AMF * > &a_sol, Location a_loc=Location::CellCenter)
Cell-centered flux helper that accepts an explicit solution vector.
Definition AMReX_MLMG.H:1448
void setFinalFillBC(int flag) noexcept
Force a FillBoundary at the end of the solve (nonzero enables).
Definition AMReX_MLMG.H:430
typename MLLinOpT< MF >::BCMode BCMode
Definition AMReX_MLMG.H:57
void setConvergenceNormType(MLMGNormType norm) noexcept
Choose the norm used for convergence tests.
Definition AMReX_MLMG.H:423
void setHybridDivergenceFactor(RT f) noexcept
Definition AMReX_MLMG.H:377
void computeResWithCrseSolFineCor(int calev, int falev)
Residual update using coarse solution / fine correction.
Definition AMReX_MLMG.H:2616
MLMGT< MF > & operator=(MLMGT< MF > const &)=delete
MLMGT(MLLinOpT< MF > &a_lp)
Definition AMReX_MLMG.H:883
void computeResidual(int alev)
Compute the residual on AMR level alev.
Definition AMReX_MLMG.H:2606
MLLinOpT< MF > & getLinOp()
Definition AMReX_MLMG.H:743
void getFluxes(std::initializer_list< AMF * > a_flux, Location a_loc=Location::CellCenter)
Initializer-list convenience overload for cell-centered fluxes.
Definition AMReX_MLMG.H:1440
typename MLLinOpT< MF >::FAB FAB
Definition AMReX_MLMG.H:54
void prepareForSPSolve()
Mixed precision: set up the single precision multigrid solver.
Definition AMReX_MLMG.H:2057
RT getBottomToleranceAbs() const noexcept
Definition AMReX_MLMG.H:408
friend class MLMGT
Definition AMReX_MLMG.H:51
int numAMRLevels() const noexcept
Definition AMReX_MLMG.H:432
MLMGT(MLMGT< MF > const &)=delete
void getFluxes(std::initializer_list< Array< AMF *, 3 > > a_flux, Location a_loc=Location::FaceCenter)
Initializer-list convenience overload for getFluxes (face-centered).
Definition AMReX_MLMG.H:1368
void mgFcycle()
Run an FMG cycle starting from the coarsest grid.
Definition AMReX_MLMG.H:2338
RT getInitRHS() const noexcept
Definition AMReX_MLMG.H:733
void algmgSolve(MLAlgMG &s, MultiFab &x, MultiFab const &b)
RT ResNormInf(int alev, bool local=false)
Infinity norm of the residual on level alev.
Definition AMReX_MLMG.H:2769
Vector< int > const & getNumCGIters() const noexcept
Definition AMReX_MLMG.H:741
amrex::MultigridType MultigridType
Definition AMReX_MLMG.H:61
void bottomSolveWithAlgMG(MultiFab &x, const MultiFab &b)
void bottomSolve()
Execute the configured bottom solver (Hypre, PETSc, CG, etc.).
Definition AMReX_MLMG.H:2390
void setAlgMGOptions(std::function< void(AlgMG< RT > &)> f)
Adjust the AlgMG solvers MLMG builds (level 0 and bottom).
Definition AMReX_MLMG.H:361
void setBottomTolerance(RT t) noexcept
Relative tolerance for the bottom solver.
Definition AMReX_MLMG.H:401
void setFinalSmooth(int n) noexcept
Number of smoothing passes when MLMG is used standalone (final smooth).
Definition AMReX_MLMG.H:328
void addInterpCorrection(int alev, int mglev)
Add interpolated corrections to (alev,mglev) data.
Definition AMReX_MLMG.H:2726
int getVerbose() const
Definition AMReX_MLMG.H:262
RT solve(const Vector< AMF * > &a_sol, const Vector< AMF const * > &a_rhs, RT a_tol_rel, RT a_tol_abs, const char *checkpoint_file=nullptr)
Solve the multilevel system; optional checkpoint_file is for debugging only.
void getFluxes(std::initializer_list< AMF * > a_flux, std::initializer_list< AMF * > a_sol, Location a_loc=Location::CellCenter)
Initializer-list convenience overload for the cell-centered flux helper above.
Definition AMReX_MLMG.H:1506
void setBottomMaxIter(int n) noexcept
Cap the number of iterations inside the bottom solver.
Definition AMReX_MLMG.H:395
void configureAlgMG(MLAlgMG &s, bool singular, bool with_options)
void oneIter(int iter)
Execute a single multigrid iteration (FMG or V-cycle).
Definition AMReX_MLMG.H:1978
void setMaxIter(int n) noexcept
Cap the number of MLMG iterations executed.
Definition AMReX_MLMG.H:289
This class provides the user with a few print options.
Definition AMReX_Print.H:35
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
static Long Write(const FabArray< FArrayBox > &mf, const std::string &name, VisMF::How how=NFiles, bool set_ghost=false)
Write a FabArray<FArrayBox> to disk in a "smart" way. Returns the total number of bytes written on th...
Definition AMReX_VisMF.cpp:987
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
__host__ __device__ BoxND< dim > convert(const BoxND< dim > &b, const IntVectND< dim > &typ) noexcept
Return a copy of b converted to the nodal flags typ.
Definition AMReX_Box.H:1632
std::array< T, N > Array
Definition AMReX_Array.H:31
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:849
void Max(KeyValuePair< K, V > &vi, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:254
void Max(KeyValuePair< K, V > &vi, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:133
MLMGT< MultiFab > MLMG
Definition AMReX_MLMG.H:3150
#define AMREX_ENUM(CLASS,...)
Declare a scoped enum with reflection support.
Definition AMReX_Enum.H:270
std::string getEnumNameString(T const &v)
Get the name string of an enum value.
Definition AMReX_Enum.H:190
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
bool inNoSyncRegion() noexcept
Definition AMReX_GpuControl.H:148
constexpr int relax_order
Definition AMReX_Hypre.H:45
constexpr int relax_type
Definition AMReX_Hypre.H:44
constexpr bool old_default
Falgout coarsening with modified classical interpolation.
Definition AMReX_Hypre.H:39
void push(MPI_Comm c)
Definition AMReX_ParallelContext.H:105
void BarrierSub() noexcept
Definition AMReX_ParallelContext.H:88
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
void pop()
Note that it's the user's responsibility to free the MPI_Comm.
Definition AMReX_ParallelContext.H:111
bool IOProcessorSub() noexcept
Am IO processor for current frame?
Definition AMReX_ParallelContext.H:80
int verbose
Definition AMReX.cpp:113
Definition AMReX_Amr.cpp:50
MultigridType
How MLMG solves the coarsest AMR level.
Definition AMReX_MLMG.H:34
@ geometric
Definition AMReX_MLMG.H:34
@ algebraic
Definition AMReX_MLMG.H:34
@ hybrid
Definition AMReX_MLMG.H:34
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
void FileOpenFailed(const std::string &file)
Output a message and abort when couldn't open the file.
Definition AMReX_Utility.cpp:62
std::array< T const *, 3 > GetArrOfConstPtrs(const std::array< T, 3 > &a) noexcept
Create an array of const-qualified pointers from an array of objects.
Definition AMReX_Array.H:1079
void EB_average_face_to_cellcenter(MultiFab &ccmf, int dcomp, const Array< MultiFab const *, 3 > &fmf)
Average face-centered values to cell centers.
Definition AMReX_EBMultiFabUtil.cpp:810
__host__ __device__ T norm(const GpuComplex< T > &a_z) noexcept
Return the norm (magnitude squared) of a complex number.
Definition AMReX_GpuComplex.H:349
void average_down(const MultiFab &S_fine, MultiFab &S_crse, const Geometry &fgeom, const Geometry &cgeom, int scomp, int ncomp, int rr)
Definition AMReX_MultiFabUtil.cpp:360
void Scale(MF &dst, typename MF::value_type val, int scomp, int ncomp, int nghost)
dst *= val
Definition AMReX_FabArrayUtility.H:2188
BoxArray const & boxArray(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.boxArray().
Definition AMReX_FabArrayBase.cpp:3038
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.DistributionMap().
Definition AMReX_FabArrayBase.cpp:3043
void average_face_to_cellcenter(MultiFab &cc, int dcomp, const Vector< const MultiFab * > &fc, IntVect const &ng_vect)
Definition AMReX_MultiFabUtil.cpp:157
constexpr bool IsFabArray_v
Definition AMReX_TypeTraits.H:34
void EB_set_covered(MultiFab &mf, Real val)
Fill all covered cells with a single value val.
Definition AMReX_EBMultiFabUtil.cpp:21
constexpr bool IsMultiFabLike_v
Definition AMReX_TypeTraits.H:44
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
double second() noexcept
Definition AMReX_Utility.cpp:874
std::array< T *, 3 > GetArrOfPtrs(std::array< T, 3 > &a) noexcept
Create an array of pointers from an array of objects.
Definition AMReX_Array.H:1033
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:3028
void ParallelCopy(MF &dst, MF const &src, int scomp, int dcomp, int ncomp, IntVect const &ng_src=IntVect(0), IntVect const &ng_dst=IntVect(0), Periodicity const &period=Periodicity::NonPeriodic())
dst = src w/ MPI communication
Definition AMReX_FabArrayUtility.H:2264
void UtilCreateCleanDirectory(const std::string &path, bool callbarrier=true)
Create a new directory, renaming the old one if it exists.
Definition AMReX_Utility.cpp:92
void EB_average_down(const MultiFab &S_fine, MultiFab &S_crse, const MultiFab &vol_fine, const MultiFab &vfrac_fine, int scomp, int ncomp, const IntVect &ratio)
Volume-weighted average-down from fine to coarse using EB volume fractions.
Definition AMReX_EBMultiFabUtil.cpp:336
void computeResidual(AlgVector< T, AllocV > &res, SpMatrix< T, AllocM > const &A, AlgVector< T, AllocV > const &x, AlgVector< T, AllocV > const &b)
Compute the residual res = b - A * x.
Definition AMReX_SpMV.H:345
@ x
Definition AMReX_Orientation.H:14
BottomSolver
Definition AMReX_MLLinOp.H:43
@ algmg
Definition AMReX_MLLinOp.H:44
@ bicgcg
Definition AMReX_MLLinOp.H:44
@ petsc
Definition AMReX_MLLinOp.H:44
@ cg
Definition AMReX_MLLinOp.H:44
@ Default
Definition AMReX_MLLinOp.H:44
@ custom
Definition AMReX_MLLinOp.H:44
@ cgbicg
Definition AMReX_MLLinOp.H:44
@ hypre
Definition AMReX_MLLinOp.H:44
@ smoother
Definition AMReX_MLLinOp.H:44
@ bicgstab
Definition AMReX_MLLinOp.H:44
Vector< T * > GetVecOfPtrs(Vector< T > &a)
Definition AMReX_Vector.H:65
IntVect nGrowVect(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nGrowVect().
Definition AMReX_FabArrayBase.cpp:3033
void LocalCopy(DMF &dst, SMF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst = src
Definition AMReX_FabArrayUtility.H:2195
bool TilingIfNotGPU() noexcept
Definition AMReX_MFIter.H:12
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
void setBndry(MF &dst, typename MF::value_type val, int scomp, int ncomp)
dst = val in ghost cells.
Definition AMReX_FabArrayUtility.H:2181
MF::value_type norminf(MF const &mf, int scomp, int ncomp, IntVect const &nghost, bool local=false)
Return the infinity norm, with an MPI maximum unless local is true.
Definition AMReX_FabArrayUtility.H:2275
Vector< std::array< T *, 3 > > GetVecOfArrOfPtrs(const Vector< std::array< std::unique_ptr< T >, 3 > > &a)
Definition AMReX_Vector.H:142
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
MLMGNormType
Definition AMReX_MLMG.H:23
@ resnorm
Definition AMReX_MLMG.H:23
@ bnorm
Definition AMReX_MLMG.H:23
@ greater
Definition AMReX_MLMG.H:23
RealVectND< 3 > RealVect
Definition AMReX_ParmParse.H:40
__host__ __device__ bool isfinite(T x) noexcept
Return true if x is neither NaN nor infinity. Works under fast math.
Definition AMReX_Math.H:155
void LocalAdd(MF &dst, MF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst += src
Definition AMReX_FabArrayUtility.H:2203
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:2174
FabArray memory allocation information.
Definition AMReX_FabArray.H:73