3#include <AMReX_Config.H>
43 :
public std::runtime_error
46 using std::runtime_error::runtime_error;
51 template <
typename T>
friend class MLMGT;
82 template <
typename AMF>
84 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file =
nullptr);
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);
111 RT a_tol_rel,
RT a_tol_abs);
119 template <
typename AMF>
121 Location a_loc = Location::FaceCenter);
129 template <
typename AMF>
131 Location a_loc = Location::FaceCenter);
139 template <
typename AMF>
141 Location a_loc = Location::FaceCenter);
149 template <
typename AMF>
151 Location a_loc = Location::FaceCenter);
160 template <
typename AMF>
163 Location a_loc = Location::FaceCenter);
172 template <
typename AMF>
174 std::initializer_list<AMF*> a_sol,
175 Location a_loc = Location::FaceCenter);
183 template <
typename AMF>
185 Location a_loc = Location::CellCenter);
193 template <
typename AMF>
195 Location a_loc = Location::CellCenter);
204 template <
typename AMF>
207 Location a_loc = Location::CellCenter);
216 template <
typename AMF>
218 std::initializer_list<AMF*> a_sol,
219 Location a_loc = Location::CellCenter);
302 void setFixedIter (
int nit)
noexcept { do_fixed_number_of_iters = nit; }
349 [[nodiscard]]
bool usedAlgMG () const noexcept {
return m_algmg_active; }
362 m_algmg_options = std::move(f);
363 m_algmg_bottom.reset();
364 m_algmg_level0.reset();
373 hybrid_stall_window = window; hybrid_stall_rate = rate;
410 [[deprecated(
"Use MLMG::setConvergenceNormType() instead.")]]
439 void setNSolve (
int flag)
noexcept { do_nsolve = flag; }
462 void setNoGpuSync (
bool do_not_sync)
noexcept { do_no_sync_gpu = do_not_sync; }
464#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
484 void setHypreOptionsNamespace(
const std::string& prefix)
noexcept
486 hypre_options_namespace = prefix;
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;}
514 template <
typename AMF>
518 template <
typename AMF>
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);
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);
708 template <
class TMF=MF>
709 requires (std::same_as<TMF,MultiFab>)
714 template <
class TMF=MF>
715 requires (std::same_as<TMF,MultiFab>)
718 template <
class TMF=MF>
719 requires (std::same_as<TMF,MultiFab>)
723 template <
class TMF=MF>
724 requires (std::same_as<TMF,MultiFab>)
731 RT best_norm, std::string& reason)
const;
740 [[nodiscard]]
int getNumIters () const noexcept {
return m_iter_fine_resnorm0.size(); }
747 bool precond_mode =
false;
748 bool throw_exception =
false;
752 int do_fixed_number_of_iters = 0;
753 int max_precond_iters = 1;
760 int max_fmg_iters = 0;
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;
768 RT bottom_abstol =
RT(-1.0);
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;
782 int final_fill_bc = 0;
789 bool linop_prepared =
false;
790 Long solve_called = 0;
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;
801 std::unique_ptr<MLMGT<fMultiFab>> m_sp_mlmg;
802 int m_sp_linop_count = 0;
804 [[nodiscard]]
IntVect ngSol ()
const {
810 std::string print_ident;
812 bool do_no_sync_gpu =
false;
815#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
820 std::unique_ptr<Hypre> hypre_solver;
821 std::unique_ptr<MLMGBndryT<MF>> hypre_bndry;
822 std::unique_ptr<HypreNodeLap> hypre_node_solver;
824 std::string hypre_options_namespace =
"hypre";
828 int hypre_num_sweeps = 2;
829 Real hypre_strong_threshold = 0.25;
833#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
834 std::unique_ptr<PETScABecLap> petsc_solver;
835 std::unique_ptr<MLMGBndryT<MF>> petsc_bndry;
846 Vector<int> sol_is_alias;
852 Vector<Vector<MF> > res;
853 Vector<Vector<MF> > cor;
854 Vector<Vector<MF> > cor_hold;
855 Vector<Vector<MF> > rescor;
858 enum timer_types { solve_time=0, iter_time, bottom_time, ntimers };
859 Vector<double> timer;
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;
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;
882template <
typename MF>
884 : linop(a_lp), ncomp(a_lp.getNComp()), namrlevs(a_lp.NAMRLevels()),
885 finest_amr_lev(a_lp.NAMRLevels()-1)
890template <
typename MF>
901template <
typename MF>
902template <
typename AMF>
905 std::initializer_list<AMF const*> a_rhs,
906 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
910 a_tol_rel, a_tol_abs, checkpoint_file);
913template <
typename MF>
914template <
typename AMF>
917 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
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();
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);
946 bottom_solver = linop.getDefaultBottomSolver();
950 bool eb_linop =
false;
958#if (defined(AMREX_USE_HYPRE) || defined(AMREX_USE_PETSC)) && (AMREX_SPACEDIM > 1)
962 linop.setMaxOrder(std::min(3, linop.getMaxOrder()));
964#ifdef AMREX_USE_HYPRE
966 linop.setMaxOrder(std::min(3, linop.getMaxOrder()));
974 "MLMG: AlgMG is not available for this operator (single-component cell and nodal operators only)");
977 "MLMG: AlgMG does not support the ghostnodes strategy with ghost cells");
980 linop.setMaxOrder(std::min(3, linop.getMaxOrder()));
984 bool is_nsolve = linop.m_parent;
989 "MLMG: NSolve requires the geometric multigrid type");
991 "MLMG: the hybrid multigrid type needs a convergence test; as a preconditioner or with a fixed number of iterations use geometric or algebraic");
995 const bool mixed = (linop.m_sp_linop !=
nullptr);
997 "MLMG: this operator does not support mixed precision");
1000 && cf_strategy != CFStrategy::ghostnodes,
1001 "MLMG: mixed precision needs geometric multigrid without ghost nodes");
1006 "MLMG: mixed precision supports the smoother, CG and BiCGStab bottom solvers only");
1011 RT& composite_norminf = m_final_resnorm0;
1013 m_niters_cg.clear();
1014 m_iter_fine_resnorm0.clear();
1028 amrex::Print() << print_ident <<
"MLMG: Initial rhs = " << rhsnorm0 <<
"\n"
1029 << print_ident <<
"MLMG: Initial residual (resid0) = " << resnorm0 <<
"\n";
1033 m_init_resnorm0 = resnorm0;
1034 m_rhsnorm0 = rhsnorm0;
1037 if ( throw_exception ) {
1038 throw error(
"MLMG: rhs or initial residual is not finite.");
1040 amrex::Abort(
"MLMG: rhs or initial residual is not finite");
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;
1052 norm_name =
"resid0";
1053 max_norm = resnorm0;
1057 norm_name =
"bnorm";
1058 max_norm = rhsnorm0;
1061 norm_name =
"resid0";
1062 max_norm = resnorm0;
1066 const RT res_target = std::max(a_tol_abs, std::max(a_tol_rel,std::numeric_limits<RT>::epsilon())*max_norm);
1068 if (!is_nsolve && resnorm0 <= res_target) {
1069 composite_norminf = resnorm0;
1071 amrex::Print() << print_ident <<
"MLMG: No iterations needed\n";
1075 bool converged =
false;
1082 bool hybrid_composite =
false;
1083 RT best_norm = resnorm0;
1085 if (namrlevs > 1) { best_norm =
ResNormInf(finest_amr_lev); }
1086 best_sol.resize(namrlevs);
1087 for (
int alev = 0; alev < namrlevs; ++alev) {
1089 best_sol[alev] = linop.make(alev, 0, ng);
1090 LocalCopy(best_sol[alev], sol[alev], 0, 0, ncomp, ng);
1094 int niters = do_fixed_number_of_iters ? do_fixed_number_of_iters : max_iters;
1095 for (
int iter = 0; iter < niters; ++iter)
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));
1122 if (is_nsolve) {
continue; }
1125 m_iter_fine_resnorm0.push_back(fine_norminf);
1126 composite_norminf = fine_norminf;
1128 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1 <<
" Fine resid/"
1129 << norm_name <<
" = " << fine_norminf/max_norm <<
"\n";
1131 bool fine_converged = (fine_norminf <= res_target);
1133 if (namrlevs == 1 && fine_converged) {
1135 }
else if (fine_converged) {
1140 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1
1141 <<
" Crse resid/" << norm_name <<
" = "
1142 << crse_norminf/max_norm <<
"\n";
1144 converged = (crse_norminf <= res_target);
1145 composite_norminf = std::max(fine_norminf, crse_norminf);
1152 amrex::Print() << print_ident <<
"MLMG: Final Iter. " << iter+1
1153 <<
" resid, resid/" << norm_name <<
" = "
1154 << composite_norminf <<
", "
1155 << composite_norminf/max_norm <<
"\n";
1159 if (
hybrid && !m_algmg_active) {
1160 if (fine_converged && !hybrid_composite) {
1163 hybrid_composite =
true;
1164 hybrid_norms.clear();
1165 best_norm = composite_norminf;
1166 for (
int alev = 0; alev < namrlevs; ++alev) {
1170 hybrid_norms.push_back(composite_norminf);
1174 if (!do_switch && iter+1 == niters) {
1176 reason =
"not converged";
1180 amrex::Print() << print_ident <<
"MLMG: " << reason <<
" after "
1181 << iter+1 <<
" iterations, switching to AlgMG on AMR level 0\n";
1183 if (!
amrex::isfinite(composite_norminf) || composite_norminf > best_norm) {
1184 for (
int alev = 0; alev < namrlevs; ++alev) {
1188 composite_norminf = best_norm;
1190 amrex::Print() << print_ident <<
"MLMG: restarting from the best iterate, resid/"
1191 << norm_name <<
" = " << best_norm/max_norm <<
"\n";
1194 m_algmg_active =
true;
1195 niters = iter + 1 + max_iters;
1198 if (composite_norminf < best_norm) {
1199 best_norm = composite_norminf;
1200 for (
int alev = 0; alev < namrlevs; ++alev) {
1205 if (composite_norminf >
RT(1.e20)*max_norm || !
amrex::isfinite(composite_norminf))
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";
1214 if ( throw_exception ) {
1215 throw error(
"MLMG blew up.");
1223 if (!converged && do_fixed_number_of_iters == 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";
1231 if ( throw_exception ) {
1232 throw error(
"MLMG failed to converge.");
1241 timer[bottom_time] = m_sp_mlmg->timer[bottom_time];
1242 m_niters_cg = m_sp_mlmg->m_niters_cg;
1248 if (linop.hasHiddenDimension()) {
1249 ng_back[linop.hiddenDirection()] = 0;
1251 for (
int alev = 0; alev < namrlevs; ++alev)
1253 if (!sol_is_alias[alev]) {
1254 LocalCopy(*a_sol[alev], sol[alev], 0, 0, ncomp, ng_back);
1264 amrex::AllPrint() << print_ident <<
"MLMG: Timers: Solve = " << timer[solve_time]
1265 <<
" Iter = " << timer[iter_time]
1266 <<
" Bottom = " << timer[bottom_time] <<
"\n";
1272 return composite_norminf;
1275template <
typename MF>
1278 RT a_tol_rel,
RT a_tol_abs) ->
RT
1280 precond_mode =
true;
1281 std::swap(max_precond_iters, do_fixed_number_of_iters);
1282 linop.beginPrecondBC();
1284 auto r =
solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
1286 linop.endPrecondBC();
1287 std::swap(max_precond_iters, do_fixed_number_of_iters);
1288 precond_mode =
false;
1293template <
typename MF>
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);
1303template <
typename MF>
1304template <
typename AMF>
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);
1314 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1315 auto const& amf = *(a_grad_sol[alev][idim]);
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));
1326template <
typename MF>
1327template <
typename AMF>
1334template <
typename MF>
1335template <
typename AMF>
1340 if (!linop.isCellCentered()) {
1341 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1346 if constexpr (std::is_same<AMF,MF>()) {
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]);
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));
1365template <
typename MF>
1366template <
typename AMF>
1374template <
typename MF>
1375template <
typename AMF>
1382 if (!linop.isCellCentered()) {
1383 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1386 if constexpr (std::is_same<AMF,MF>()) {
1387 linop.getFluxes(a_flux, a_sol, a_loc);
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]);
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));
1406template <
typename MF>
1407template <
typename AMF>
1410 std::initializer_list<AMF*> a_sol,
Location a_loc)
1416template <
typename MF>
1417template <
typename AMF>
1422 if constexpr (std::is_same<AMF,MF>()) {
1426 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1427 auto const& amf = *a_flux[ilev];
1431 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1437template <
typename MF>
1438template <
typename AMF>
1445template <
typename MF>
1446template <
typename AMF>
1453 if constexpr (! std::is_same<AMF,MF>()) {
1454 for (
int alev = 0; alev < namrlevs; ++alev) {
1459 if (linop.isCellCentered())
1462 for (
int alev = 0; alev < namrlevs; ++alev) {
1463 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1464 const int mglev = 0;
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]);
1473 if constexpr (std::is_same<AMF,MF>()) {
1478 for (
int alev = 0; alev < namrlevs; ++alev) {
1487 if constexpr (std::is_same<AMF,MF>()) {
1488 linop.getFluxes(a_flux, a_sol);
1491 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1492 auto const& amf = *a_flux[ilev];
1496 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1503template <
typename MF>
1504template <
typename AMF>
1507 std::initializer_list<AMF*> a_sol,
Location a_loc)
1514template <
typename MF>
1518 if (!linop.isCellCentered()) {
1526template <
typename MF>
1532 if (!linop.isCellCentered()) {
1536 linop.getEBFluxes(a_eb_flux, a_sol);
1540template <
typename MF>
1548 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1550 sol.resize(namrlevs);
1551 sol_is_alias.resize(namrlevs,
true);
1552 for (
int alev = 0; alev < namrlevs; ++alev)
1556 sol[alev] = linop.makeAlias(*a_sol[alev]);
1557 sol_is_alias[alev] =
true;
1561 if (sol_is_alias[alev])
1563 sol[alev] = linop.make(alev, 0, ng_sol);
1564 sol_is_alias[alev] =
false;
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)
1579 MFInfo(), *linop.Factory(alev));
1581 linop.applyMetricTerm(alev, 0, rhstmp);
1582 linop.unimposeNeumannBC(alev, rhstmp);
1583 linop.applyInhomogNeumannTerm(alev, rhstmp);
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()) {
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));
1598 average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1605#if (AMREX_SPACEDIM != 3)
1606 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1607 linop.unapplyMetricTerm(alev, 0, *a_res[alev]);
1612template <
typename MF>
1623 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1625 for (
int alev = 0; alev < namrlevs; ++alev)
1629 nghost = linop.getNGrow(alev);
1630 in[alev] = a_in[alev];
1632 else if (
nGrowVect(*a_in[alev]) == ng_sol)
1634 in[alev] = a_in[alev];
1640 in_raii[alev] = linop.make(alev, 0, ng,
1643 in[alev] = &(in_raii[alev]);
1645 rh[alev] = linop.make(alev, 0,
IntVect(nghost),
1652 for (
int alev = 0; alev < namrlevs; ++alev) {
1653 linop.applyInhomogNeumannTerm(alev, rh[alev]);
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()) {
1667 if constexpr (std::is_same_v<MF,MultiFab>) {
1672 average_down(*out[alev+1], *out[alev], 0,
nComp(*out[alev]), linop.AMRRefRatioVect(alev));
1675 amrex::Abort(
"MLMG: TODO average_down for non-MultiFab");
1681#if (AMREX_SPACEDIM != 3)
1682 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1683 linop.unapplyMetricTerm(alev, 0, *out[alev]);
1687 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1689 Scale(*out[alev],
RT(-1), 0,
nComp(*out[alev]), nghost);
1693template <
typename MF>
1697 precond_mode =
true;
1698 linop.beginPrecondBC();
1700 linop.endPrecondBC();
1701 precond_mode =
false;
1704template <
typename MF>
1705template <
typename AMF>
1714 timer.assign(ntimers, 0.0);
1721template <
typename MF>
1722template <
typename AMF>
1727 IntVect const ng_sol = ngSol();
1729 sol.resize(namrlevs);
1730 sol_is_alias.resize(namrlevs,
false);
1731 for (
int alev = 0; alev < namrlevs; ++alev)
1735 if constexpr (std::is_same<AMF,MF>()) {
1736 sol[alev] = linop.makeAlias(*a_sol[alev]);
1737 sol_is_alias[alev] =
true;
1739 amrex::Abort(
"Type conversion not supported for CFStrategy::ghostnodes");
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;
1753 if (sol_is_alias[alev] || !solve_called) {
1754 sol[alev] = linop.make(alev, 0, ng_sol);
1755 sol_is_alias[alev] =
false;
1763 rhs.resize(namrlevs);
1764 for (
int alev = 0; alev < namrlevs; ++alev)
1767 if (!solve_called) {
1768 rhs[alev] = linop.make(alev, 0, ng_rhs);
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]));
1782 if (factory && !factory->isAllRegular()) {
1783 if constexpr (std::is_same<MF,MultiFab>()) {
1787 amrex::Abort(
"TODO: MLMG with EB only works with MultiFab");
1793 for (
int falev = finest_amr_lev; falev > 0; --falev)
1795 linop.averageDownSolutionRHS(falev-1, sol[falev-1], rhs[falev-1], sol[falev], rhs[falev]);
1800 for (
int falev = 1; falev <= finest_amr_lev; ++falev) {
1801 linop.interpCoarseFineSolution(falev, sol[falev], sol[falev-1]);
1806 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1812template <
typename MF>
1818 const bool mixed = (linop.m_sp_linop !=
nullptr);
1820 IntVect ng = linop.getNGrowVectRestriction();
1822 if (!solve_called) {
1823 linop.make(res, ng);
1824 if (!mixed) { linop.make(rescor, ng); }
1826 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1828 const int nmglevs = linop.NMGLevels(alev);
1829 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1832 if (!mixed) {
setVal(rescor[alev][mglev],
RT(0.0)); }
1841 cor.resize(namrlevs);
1842 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1844 const int nmglevs = mixed ? 0 : linop.NMGLevels(alev);
1845 cor[alev].resize(nmglevs);
1846 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1848 if (!solve_called) {
1851 cor[alev][mglev] = linop.make(alev, mglev, _ng);
1857 cor_hold.resize(std::max(namrlevs-1,1));
1860 const int nmglevs = mixed ? 0 : linop.NMGLevels(alev);
1861 cor_hold[alev].resize(nmglevs);
1862 for (
int mglev = 0; mglev < nmglevs-1; ++mglev)
1864 if (!solve_called) {
1867 cor_hold[alev][mglev] = linop.make(alev, mglev, _ng);
1869 setVal(cor_hold[alev][mglev],
RT(0.0));
1872 for (
int alev = 1; alev < finest_amr_lev && !mixed; ++alev)
1874 cor_hold[alev].resize(1);
1875 if (!solve_called) {
1878 cor_hold[alev][0] = linop.make(alev, 0, _ng);
1880 setVal(cor_hold[alev][0],
RT(0.0));
1884 || !linop.supportNSolve())
1890 if (do_nsolve && ns_linop ==
nullptr && !mixed)
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");
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";
1907template <
typename MF>
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()) {
1919 m_algmg_bottom.reset();
1920 m_algmg_level0.reset();
1922#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1923 hypre_solver.reset();
1924 hypre_bndry.reset();
1925 hypre_node_solver.reset();
1928#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1929 petsc_solver.reset();
1930 petsc_bndry.reset();
1935template <
typename MF>
1940 linop.preparePrecond();
1943template <
typename MF>
1948 ns_linop = linop.makeNLinOp(nsolve_grid_size);
1953 const BoxArray& ba = (*ns_linop).m_grids[0][0];
1958 ns_sol = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1961 ns_rhs = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1965 ns_linop->setLevelBC(0, ns_sol.get());
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);
1977template <
typename MF>
1982 for (
int alev = finest_amr_lev; alev > 0; --alev)
1988 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1993 if (alev != finest_amr_lev) {
1994 std::swap(cor_hold[alev][0], cor[alev][0]);
2001 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
2006 if (m_algmg_active) {
2007 if constexpr (std::is_same<MF,MultiFab>()) {
2010 amrex::Abort(
"MLMG: AlgMG is only supported for MultiFab");
2012 }
else if (iter < max_fmg_iters) {
2020 LocalAdd(sol[0], cor[0][0], 0, 0, ncomp, nghost);
2023 for (
int alev = 1; alev <= finest_amr_lev; ++alev)
2028 linop.applyOverset(alev, cor[alev][0]);
2032 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
2034 if (alev != finest_amr_lev) {
2035 LocalAdd(cor_hold[alev][0], cor[alev][0], 0, 0, ncomp, nghost);
2043 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
2045 if (alev != finest_amr_lev) {
2046 LocalAdd(cor[alev][0], cor_hold[alev][0], 0, 0, ncomp, nghost);
2050 linop.averageDownAndSync(sol);
2055template <
typename MF>
2060 const bool first = !m_sp_mlmg;
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");
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);
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);
2084 sp.setThrowException(throw_exception);
2085 sp.print_ident = print_ident +
" ";
2087 sp.m_niters_cg.clear();
2088 sp.timer.assign(ntimers, 0.0);
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);
2104 sp.prepareWorkArrays();
2106 sp.solve_called = 1;
2107 sp.computeMLResidual(finest_amr_lev);
2110 amrex::Abort(
"MLMG::prepareForSPSolve: MultiFab only");
2119template <
typename MF>
2126 auto& sp = *m_sp_mlmg;
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;
2132 sp.setBottomToleranceAbs(
static_cast<float>(bottom_abstol * invs));
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];
2140 auto const& dma = dmf.arrays();
2141 auto const& sma = smf.const_arrays();
2145 dma[box_no](i,j,k,n) =
static_cast<float>(sma[box_no](i,j,k,n) * invs);
2154#pragma omp parallel if (Gpu::notInLaunchRegion())
2158 const Box& bx = mfi.tilebox();
2159 auto const& dfab = dmf.array(mfi);
2160 auto const& sfab = smf.const_array(mfi);
2163 dfab(i,j,k,n) =
static_cast<float>(sfab(i,j,k,n) * invs);
2169 LocalCopy(sp.res[finest_amr_lev][0], sp.rhs[finest_amr_lev], 0, 0, ncomp,
IntVect(0));
2173 for (
int alev = 0; alev < namrlevs; ++alev) {
2174 auto& dmf = sol[alev];
2175 auto const& smf = sp.sol[alev];
2178 auto const& dma = dmf.arrays();
2179 auto const& sma = smf.const_arrays();
2183 dma[box_no](i,j,k,n) += ss *
static_cast<RT>(sma[box_no](i,j,k,n));
2192#pragma omp parallel if (Gpu::notInLaunchRegion())
2196 const Box& bx = mfi.tilebox();
2197 auto const& dfab = dmf.array(mfi);
2198 auto const& sfab = smf.const_array(mfi);
2201 dfab(i,j,k,n) += ss *
static_cast<RT>(sfab(i,j,k,n));
2207 linop.averageDownAndSync(sol);
2210 for (
int falev = 1; falev <= finest_amr_lev; ++falev) {
2211 linop.interpCoarseFineSolution(falev, sol[falev], sol[falev-1]);
2219template <
typename MF>
2224 const int mglev = 0;
2230template <
typename MF>
2236 const int mglev_bottom = linop.NMGLevels(amrlev) - 1;
2238 for (
int mglev = mglev_top; mglev < mglev_bottom; ++mglev)
2240 BL_PROFILE_VAR(
"MLMG::mgVcycle_down::"+std::to_string(mglev), blp_mgv_down_lev);
2245 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
2246 <<
" DN: Norm before smooth " <<
norm <<
"\n";
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);
2259 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
2260 <<
" DN: Norm after smooth " <<
norm <<
"\n";
2264 linop.restriction(amrlev, mglev+1, res[amrlev][mglev+1], rescor[amrlev][mglev]);
2273 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
2274 <<
" DN: Norm before bottom " <<
norm <<
"\n";
2281 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
2282 <<
" UP: Norm after bottom " <<
norm <<
"\n";
2290 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
2291 <<
" Norm before smooth " <<
norm <<
"\n";
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);
2301 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
2302 <<
" Norm after smooth " <<
norm <<
"\n";
2307 for (
int mglev = mglev_bottom-1; mglev >= mglev_top; --mglev)
2309 BL_PROFILE_VAR(
"MLMG::mgVcycle_up::"+std::to_string(mglev), blp_mgv_up_lev);
2316 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
2317 <<
" UP: Norm before smooth " <<
norm <<
"\n";
2319 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev],
false, nu2);
2327 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
2328 <<
" UP: Norm after smooth " <<
norm <<
"\n";
2336template <
typename MF>
2343 auto* pf = linop.Factory(0);
2344 auto is_all_regular = [pf] () {
2347 return f->isAllRegular();
2353 AMREX_ASSERT(linop.isCellCentered() || is_all_regular());
2356 const int amrlev = 0;
2357 const int mg_bottom_lev = linop.NMGLevels(amrlev) - 1;
2361 for (
int mglev = 1; mglev <= mg_bottom_lev; ++mglev)
2363 linop.avgDownResMG(mglev, res[amrlev][mglev], res[amrlev][mglev-1]);
2368 for (
int mglev = mg_bottom_lev-1; mglev >= 0; --mglev)
2376 LocalCopy(res[amrlev][mglev], rescor[amrlev][mglev], 0, 0, ncomp, nghost);
2379 std::swap(cor[amrlev][mglev], cor_hold[amrlev][mglev]);
2381 LocalAdd(cor[amrlev][mglev], cor_hold[amrlev][mglev], 0, 0, ncomp, nghost);
2388template <
typename MF>
2394 NSolve(*ns_mlmg, *ns_sol, *ns_rhs);
2402template <
typename MF>
2410 MF
const& res_bottom = res[0].back();
2421 RT(-1.0),
RT(-1.0));
2423 linop.copyNSolveSolution(cor[0].back(), a_sol);
2426template <
typename MF>
2431 m_niters_cg.push_back(niters);
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);
2442template <
typename MF>
2448 if (!linop.isBottomActive()) {
return; }
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];
2464 bool skip_fillboundary =
true;
2465 linop.smooth(amrlev, mglev,
x, b, skip_fillboundary, nuf);
2471 if (linop.isBottomSingular() && linop.getEnforceSingularSolvable())
2474 raii_b = linop.make(amrlev, mglev, ng,
2484#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2485 if constexpr (std::is_same<MF,MultiFab>()) {
2486 bottomSolveWithHypre(
x, *bottom_b);
2490 amrex::Abort(
"Using Hypre as bottom solver not supported in this case");
2495#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
2496 if constexpr (std::is_same<MF,MultiFab>()) {
2497 bottomSolveWithPETSc(
x, *bottom_b);
2501 amrex::Abort(
"Using PETSc as bottom solver not supported in this case");
2506 if constexpr (std::is_same<MF,MultiFab>()) {
2509 amrex::Abort(
"MLMG: AlgMG is only supported for MultiFab");
2514 linop.customBottomSolve(
this,
x, *bottom_b, bottom_reltol, bottom_abstol,
2555 if (! timer.empty()) {
2560template <
typename MF>
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";
2578 if (ret != 0 && ret != 9) {
2586template <
typename MF>
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]);
2604template <
typename MF>
2609 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
2610 linop.solutionResidual(alev, res[alev][0], sol[alev], rhs[alev], crse_bcdata);
2614template <
typename MF>
2618 BL_PROFILE(
"MLMG::computeResWithCrseSolFineCor()");
2622 nghost =
IntVect(std::min(linop.getNGrow(falev),linop.getNGrow(calev)));
2625 MF& crse_sol = sol[calev];
2626 const MF& crse_rhs = rhs[calev];
2627 MF& crse_res = res[calev][0];
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];
2635 const MF* crse_bcdata = (calev > 0) ? &(sol[calev-1]) :
nullptr;
2636 linop.solutionResidual(calev, crse_res, crse_sol, crse_rhs, crse_bcdata);
2638 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res, BCMode::Homogeneous);
2639 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2641 linop.reflux(calev, crse_res, crse_sol, crse_rhs, fine_res, fine_sol, fine_rhs);
2643 linop.avgDownResAmr(calev, crse_res, fine_res);
2647template <
typename MF>
2651 BL_PROFILE(
"MLMG::computeResWithCrseCorFineCor()");
2655 nghost =
IntVect(linop.getNGrow(falev));
2658 const MF& crse_cor = cor[falev-1][0];
2660 MF& fine_cor = cor [falev][0];
2661 MF& fine_res = res [falev][0];
2662 MF& fine_rescor = rescor[falev][0];
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);
2671template <
typename MF>
2679 nghost =
IntVect(linop.getNGrow(alev));
2682 MF & crse_cor = cor[alev-1][0];
2683 MF & fine_cor = cor[alev ][0];
2685 const Geometry& crse_geom = linop.Geom(alev-1,0);
2688 int ng_dst = linop.isCellCentered() ? 1 : 0;
2691 ng_src = linop.getNGrow(alev-1);
2692 ng_dst = linop.getNGrow(alev-1);
2696 amrex::Abort(
"MLMG: CFStrategy::ghostnodes not supported for non-MultiFab like types");
2700 MF cfine = linop.makeCoarseAmr(alev,
IntVect(ng_dst),
2706 linop.interpolationAmr(alev, fine_cor, cfine, nghost);
2712template <
typename MF>
2718 MF& crse_cor = cor[alev][mglev+1];
2719 MF& fine_cor = cor[alev][mglev ];
2720 linop.interpAssign(alev, mglev, fine_cor, crse_cor);
2724template <
typename MF>
2730 const MF& crse_cor = cor[alev][mglev+1];
2731 MF& fine_cor = cor[alev][mglev ];
2736 if (linop.isMFIterSafe(alev, mglev, mglev+1))
2742 cfine = linop.makeCoarseMG(alev, mglev,
IntVect(0),
2748 linop.interpolation(alev, mglev, fine_cor, *cmf);
2755template <
typename MF>
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);
2767template <
typename MF>
2772 return linop.normInf(alev, res[alev][0], local);
2776template <
typename MF>
2782 for (
int alev = 0; alev <= alevmax; ++alev)
2791template <
typename MF>
2797 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
2798 auto t = linop.normInf(alev, rhs[alev],
true);
2805template <
typename MF>
2809 auto const& offset = linop.getSolvabilityOffset(0, 0, rhs[0]);
2811 for (
int c = 0; c < ncomp; ++c) {
2812 amrex::Print() << print_ident <<
"MLMG: Subtracting " << offset[c] <<
" from rhs component "
2816 for (
int alev = 0; alev < namrlevs; ++alev) {
2817 linop.fixSolvabilityByOffset(alev, 0, rhs[alev], offset);
2821template <
typename MF>
2825 auto const& offset = linop.getSolvabilityOffset(amrlev, mglev, mf);
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";
2833 linop.fixSolvabilityByOffset(amrlev, mglev, mf, offset);
2836template <
typename MF>
2838requires (std::same_as<TMF,MultiFab>)
2854 if (m_algmg_options) { m_algmg_options(a); }
2858template <
typename MF>
2860requires (std::same_as<TMF,MultiFab>)
2869 s.
solve(
x, b, m_algmg_reltol,
RT(0), bottom_maxiter);
2870 }
catch (std::runtime_error
const& e) {
2871 throw error(e.what());
2876template <
typename MF>
2878requires (std::same_as<TMF,MultiFab>)
2882 const int amrlev = 0;
2883 const int mglev = linop.NMGLevels(amrlev) - 1;
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);
2892 if (linop.isSingular(amrlev) && linop.getEnforceSingularSolvable()) {
2897template <
typename MF>
2899requires (std::same_as<TMF,MultiFab>)
2909 bool const fresh = (m_algmg_level0 ==
nullptr);
2910 if (fresh) { m_algmg_level0 = linop.makeAlgMG(0); }
2912 algmgSolve(*m_algmg_level0, cor[0][0], res[0][0]);
2916 linop.applyOverset(0, cor[0][0]);
2918 if (linop.isSingular(0) && linop.getEnforceSingularSolvable()) {
2922 if (! timer.empty()) {
2927template <
typename MF>
2930 RT best_norm, std::string& reason)
const
2932 RT const rnorm = norms.back();
2934 reason =
"non-finite residual";
2937 if (rnorm >
RT(1.e20)*max_norm ||
2938 rnorm > hybrid_divergence_factor*best_norm) {
2939 reason =
"diverging";
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";
2953#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2954template <
typename MF>
2956requires (std::same_as<TMF,MultiFab>)
2960 const int amrlev = 0;
2961 const int mglev = linop.
NMGLevels(amrlev) - 1;
2967 if (hypre_solver ==
nullptr)
2969 hypre_solver = linop.makeHypre(hypre_interface);
2973 hypre_solver->setHypreOptionsNamespace(hypre_options_namespace);
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);
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();
2991 0.5*dx[1]*crse_ratio[1],
2992 0.5*dx[2]*crse_ratio[2]));
3000 ? bottom_abstol :
Real(-1.0);
3001 hypre_solver->setThrowException(throw_exception);
3003 hypre_solver->solve(
3004 x, b, bottom_reltol, hypre_abstol, bottom_maxiter, *hypre_bndry,
3006 }
catch (std::runtime_error
const& e) {
3007 throw error(e.what());
3012 if (hypre_node_solver ==
nullptr)
3015 linop.makeHypreNodeLap(bottom_verbose, hypre_options_namespace);
3017 hypre_node_solver->setThrowException(throw_exception);
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());
3029 makeSolvable(amrlev, mglev,
x);
3034#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
3035template <
typename MF>
3037requires (std::same_as<TMF,MultiFab>)
3043 if(petsc_solver ==
nullptr)
3045 petsc_solver = linop.makePETSc();
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();
3057 0.5*dx[1]*crse_ratio[1],
3058 0.5*dx[2]*crse_ratio[2]));
3062 petsc_solver->solve(
x, b, bottom_reltol,
Real(-1.), bottom_maxiter, *petsc_bndry,
3067template <
typename MF>
3071 RT a_tol_rel, RT a_tol_abs,
const char* a_file_name)
const
3073 std::string file_name(a_file_name);
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()) {
3087 HeaderFile.precision(17);
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";
3111 for (
int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
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");
3123 linop.checkPoint(file_name+
"/linop");
3126template <
typename MF>
3130 print_ident.resize(print_ident.size()+4,
' ');
3133template <
typename MF>
3137 if (print_ident.size() > 4) {
3138 print_ident.resize(print_ident.size()-4,
' ');
3140 print_ident.clear();
3145#ifdef AMREX_MLMG_HAS_FLOAT
#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