3#include <AMReX_Config.H>
30 :
public std::runtime_error
33 using std::runtime_error::runtime_error;
67 template <
typename AMF>
69 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file =
nullptr);
81 template <
typename AMF>
82 RT solve (std::initializer_list<AMF*> a_sol,
83 std::initializer_list<AMF const*> a_rhs,
84 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file =
nullptr);
96 RT a_tol_rel,
RT a_tol_abs);
104 template <
typename AMF>
106 Location a_loc = Location::FaceCenter);
114 template <
typename AMF>
116 Location a_loc = Location::FaceCenter);
124 template <
typename AMF>
126 Location a_loc = Location::FaceCenter);
134 template <
typename AMF>
136 Location a_loc = Location::FaceCenter);
145 template <
typename AMF>
148 Location a_loc = Location::FaceCenter);
157 template <
typename AMF>
159 std::initializer_list<AMF*> a_sol,
160 Location a_loc = Location::FaceCenter);
168 template <
typename AMF>
170 Location a_loc = Location::CellCenter);
178 template <
typename AMF>
179 void getFluxes (std::initializer_list<AMF*> a_flux,
180 Location a_loc = Location::CellCenter);
189 template <
typename AMF>
192 Location a_loc = Location::CellCenter);
201 template <
typename AMF>
202 void getFluxes (std::initializer_list<AMF*> a_flux,
203 std::initializer_list<AMF*> a_sol,
204 Location a_loc = Location::CellCenter);
287 void setFixedIter (
int nit)
noexcept { do_fixed_number_of_iters = nit; }
360 [[deprecated(
"Use MLMG::setConvergenceNormType() instead.")]]
389 void setNSolve (
int flag)
noexcept { do_nsolve = flag; }
412 void setNoGpuSync (
bool do_not_sync)
noexcept { do_no_sync_gpu = do_not_sync; }
414#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
434 void setHypreOptionsNamespace(
const std::string& prefix)
noexcept
436 hypre_options_namespace = prefix;
440 void setHypreOldDefault (
bool l)
noexcept {hypre_old_default = l;}
442 void setHypreRelaxType (
int n)
noexcept {hypre_relax_type = n;}
444 void setHypreRelaxOrder (
int n)
noexcept {hypre_relax_order = n;}
446 void setHypreNumSweeps (
int n)
noexcept {hypre_num_sweeps = n;}
448 void setHypreStrongThreshold (
Real t)
noexcept {hypre_strong_threshold = t;}
464 template <
typename AMF>
465 void prepareForSolve (Vector<AMF*>
const& a_sol, Vector<AMF const*>
const& a_rhs);
496 void mgVcycle (
int amrlev,
int mglev);
509 void NSolve (MLMGT<MF>& a_solver, MF& a_sol, MF& a_rhs);
521 void postCG (
int ret,
int niters = -1);
609#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
617 template <
class TMF=MF>
618 requires (std::same_as<TMF,MultiFab>)
619 void bottomSolveWithHypre (MF&
x,
const MF& b);
622#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
630 template <
class TMF=MF>
631 requires (std::same_as<TMF,MultiFab>)
632 void bottomSolveWithPETSc (MF&
x,
const MF& b);
652 [[nodiscard]]
int getNumIters () const noexcept {
return m_iter_fine_resnorm0.
size(); }
659 bool precond_mode =
false;
660 bool throw_exception =
false;
664 int do_fixed_number_of_iters = 0;
665 int max_precond_iters = 1;
672 int max_fmg_iters = 0;
676 int bottom_verbose = 0;
677 int bottom_maxiter = 200;
678 RT bottom_reltol = std::is_same<RT,double>() ?
RT(1.e-4) :
RT(1.e-3);
679 RT bottom_abstol =
RT(-1.0);
683 int final_fill_bc = 0;
690 bool linop_prepared =
false;
691 Long solve_called = 0;
694 int do_nsolve =
false;
695 int nsolve_grid_size = 16;
696 std::unique_ptr<MLLinOpT<MF>> ns_linop;
697 std::unique_ptr<MLMGT<MF>> ns_mlmg;
698 std::unique_ptr<MF> ns_sol;
699 std::unique_ptr<MF> ns_rhs;
701 std::string print_ident;
703 bool do_no_sync_gpu =
false;
706#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
711 std::unique_ptr<Hypre> hypre_solver;
712 std::unique_ptr<MLMGBndryT<MF>> hypre_bndry;
713 std::unique_ptr<HypreNodeLap> hypre_node_solver;
715 std::string hypre_options_namespace =
"hypre";
716 bool hypre_old_default =
true;
717 int hypre_relax_type = 6;
718 int hypre_relax_order = 1;
719 int hypre_num_sweeps = 2;
720 Real hypre_strong_threshold = 0.25;
724#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
725 std::unique_ptr<PETScABecLap> petsc_solver;
726 std::unique_ptr<MLMGBndryT<MF>> petsc_bndry;
749 enum timer_types { solve_time=0, iter_time, bottom_time, ntimers };
750 Vector<double> timer;
752 RT m_rhsnorm0 =
RT(-1.0);
753 RT m_init_resnorm0 =
RT(-1.0);
754 RT m_final_resnorm0 =
RT(-1.0);
755 Vector<int> m_niters_cg;
756 Vector<RT> m_iter_fine_resnorm0;
767 void checkPoint (
const Vector<MultiFab*>& a_sol,
768 const Vector<MultiFab const*>& a_rhs,
769 RT a_tol_rel,
RT a_tol_abs,
const char* a_file_name)
const;
773template <
typename MF>
775 : linop(a_lp), ncomp(a_lp.getNComp()), namrlevs(a_lp.NAMRLevels()),
776 finest_amr_lev(a_lp.NAMRLevels()-1)
781template <
typename MF>
792template <
typename MF>
793template <
typename AMF>
796 std::initializer_list<AMF const*> a_rhs,
797 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
801 a_tol_rel, a_tol_abs, checkpoint_file);
804template <
typename MF>
805template <
typename AMF>
808 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
819 std::optional<Gpu::SyncAtExitOnly> no_sync_region;
820 std::optional<Gpu::SingleStreamRegion> single_stream_region;
821 if (do_no_sync_gpu) {
822 no_sync_region.emplace();
823 single_stream_region.emplace();
826 if constexpr (std::is_same<AMF,MultiFab>()) {
827 if (checkpoint_file !=
nullptr) {
828 checkPoint(a_sol, a_rhs, a_tol_rel, a_tol_abs, checkpoint_file);
837 bottom_solver = linop.getDefaultBottomSolver();
840#if (defined(AMREX_USE_HYPRE) || defined(AMREX_USE_PETSC)) && (AMREX_SPACEDIM > 1)
841 if constexpr (IsFabArray_v<AMF>) {
843 int mo = linop.getMaxOrder();
844 if (a_sol[0]->hasEBFabFactory()) {
845 linop.setMaxOrder(2);
847 linop.setMaxOrder(std::min(3,mo));
853 bool is_nsolve = linop.m_parent;
857 RT& composite_norminf = m_final_resnorm0;
860 m_iter_fine_resnorm0.clear();
862 prepareForSolve(a_sol, a_rhs);
864 computeMLResidual(finest_amr_lev);
867 RT resnorm0 = MLResNormInf(finest_amr_lev, local);
868 RT rhsnorm0 = MLRhsNormInf(local);
874 amrex::Print() << print_ident <<
"MLMG: Initial rhs = " << rhsnorm0 <<
"\n"
875 << print_ident <<
"MLMG: Initial residual (resid0) = " << resnorm0 <<
"\n";
879 m_init_resnorm0 = resnorm0;
880 m_rhsnorm0 = rhsnorm0;
882 RT max_norm = resnorm0;
883 std::string norm_name =
"resid0";
886 if (rhsnorm0 >= resnorm0) {
890 norm_name =
"resid0";
899 norm_name =
"resid0";
904 const RT res_target = std::max(a_tol_abs, std::max(a_tol_rel,
RT(1.e-16))*max_norm);
906 if (!is_nsolve && resnorm0 <= res_target) {
907 composite_norminf = resnorm0;
909 amrex::Print() << print_ident <<
"MLMG: No iterations needed\n";
913 bool converged =
false;
915 const int niters = do_fixed_number_of_iters ? do_fixed_number_of_iters : max_iters;
916 for (
int iter = 0; iter < niters; ++iter)
925 if (is_nsolve) {
continue; }
927 RT fine_norminf = ResNormInf(finest_amr_lev);
928 m_iter_fine_resnorm0.push_back(fine_norminf);
929 composite_norminf = fine_norminf;
931 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1 <<
" Fine resid/"
932 << norm_name <<
" = " << fine_norminf/max_norm <<
"\n";
934 bool fine_converged = (fine_norminf <= res_target);
936 if (namrlevs == 1 && fine_converged) {
938 }
else if (fine_converged) {
940 computeMLResidual(finest_amr_lev-1);
941 RT crse_norminf = MLResNormInf(finest_amr_lev-1);
943 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1
944 <<
" Crse resid/" << norm_name <<
" = "
945 << crse_norminf/max_norm <<
"\n";
947 converged = (crse_norminf <= res_target);
948 composite_norminf = std::max(fine_norminf, crse_norminf);
955 amrex::Print() << print_ident <<
"MLMG: Final Iter. " << iter+1
956 <<
" resid, resid/" << norm_name <<
" = "
957 << composite_norminf <<
", "
958 << composite_norminf/max_norm <<
"\n";
962 if (composite_norminf >
RT(1.e20)*max_norm)
965 amrex::Print() << print_ident <<
"MLMG: Failing to converge after " << iter+1 <<
" iterations."
966 <<
" resid, resid/" << norm_name <<
" = "
967 << composite_norminf <<
", "
968 << composite_norminf/max_norm <<
"\n";
971 if ( throw_exception ) {
972 throw error(
"MLMG blew up.");
980 if (!converged && do_fixed_number_of_iters == 0) {
982 amrex::Print() << print_ident <<
"MLMG: Failed to converge after " << max_iters <<
" iterations."
983 <<
" resid, resid/" << norm_name <<
" = "
984 << composite_norminf <<
", "
985 << composite_norminf/max_norm <<
"\n";
988 if ( throw_exception ) {
989 throw error(
"MLMG failed to converge.");
1000 if (linop.hasHiddenDimension()) {
1001 ng_back[linop.hiddenDirection()] = 0;
1003 for (
int alev = 0; alev < namrlevs; ++alev)
1005 if (!sol_is_alias[alev]) {
1006 LocalCopy(*a_sol[alev], sol[alev], 0, 0, ncomp, ng_back);
1012 ParallelReduce::Max<double>(timer.data(), timer.size(), 0,
1016 amrex::AllPrint() << print_ident <<
"MLMG: Timers: Solve = " << timer[solve_time]
1017 <<
" Iter = " << timer[iter_time]
1018 <<
" Bottom = " << timer[bottom_time] <<
"\n";
1024 return composite_norminf;
1027template <
typename MF>
1030 RT a_tol_rel,
RT a_tol_abs) ->
RT
1032 precond_mode =
true;
1033 std::swap(max_precond_iters, do_fixed_number_of_iters);
1034 linop.beginPrecondBC();
1036 auto r = solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
1038 linop.endPrecondBC();
1039 std::swap(max_precond_iters, do_fixed_number_of_iters);
1040 precond_mode =
false;
1045template <
typename MF>
1049 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1050 const MF* crse_bcdata = (alev > 0) ? a_sol[alev-1] :
nullptr;
1051 linop.prepareForFluxes(alev, crse_bcdata);
1055template <
typename MF>
1056template <
typename AMF>
1061 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1062 if constexpr (std::is_same<AMF,MF>()) {
1063 linop.compGrad(alev, a_grad_sol[alev], sol[alev], a_loc);
1066 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1067 auto const& amf = *(a_grad_sol[alev][idim]);
1070 linop.compGrad(alev,
GetArrOfPtrs(grad_sol), sol[alev], a_loc);
1071 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1072 LocalCopy(*a_grad_sol[alev][idim], grad_sol[idim], 0, 0, ncomp,
IntVect(0));
1078template <
typename MF>
1079template <
typename AMF>
1086template <
typename MF>
1087template <
typename AMF>
1092 if (!linop.isCellCentered()) {
1093 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1098 if constexpr (std::is_same<AMF,MF>()) {
1102 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1103 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1104 auto const& amf = *(a_flux[ilev][idim]);
1109 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1110 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1111 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp,
IntVect(0));
1117template <
typename MF>
1118template <
typename AMF>
1126template <
typename MF>
1127template <
typename AMF>
1134 if (!linop.isCellCentered()) {
1135 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1138 if constexpr (std::is_same<AMF,MF>()) {
1139 linop.getFluxes(a_flux, a_sol, a_loc);
1142 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1143 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1144 auto const& amf = *(a_flux[ilev][idim]);
1150 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1151 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1152 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp,
IntVect(0));
1158template <
typename MF>
1159template <
typename AMF>
1162 std::initializer_list<AMF*> a_sol,
Location a_loc)
1168template <
typename MF>
1169template <
typename AMF>
1174 if constexpr (std::is_same<AMF,MF>()) {
1178 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1179 auto const& amf = *a_flux[ilev];
1183 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1189template <
typename MF>
1190template <
typename AMF>
1197template <
typename MF>
1198template <
typename AMF>
1205 if constexpr (! std::is_same<AMF,MF>()) {
1206 for (
int alev = 0; alev < namrlevs; ++alev) {
1211 if (linop.isCellCentered())
1214 for (
int alev = 0; alev < namrlevs; ++alev) {
1215 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1216 const int mglev = 0;
1218 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1219 ffluxes[alev][idim].define(
amrex::convert(linop.m_grids[alev][mglev],
1221 linop.m_dmap[alev][mglev], ncomp, nghost,
MFInfo(),
1222 *linop.m_factory[alev][mglev]);
1225 if constexpr (std::is_same<AMF,MF>()) {
1230 for (
int alev = 0; alev < namrlevs; ++alev) {
1239 if constexpr (std::is_same<AMF,MF>()) {
1240 linop.getFluxes(a_flux, a_sol);
1243 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1244 auto const& amf = *a_flux[ilev];
1248 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1255template <
typename MF>
1256template <
typename AMF>
1259 std::initializer_list<AMF*> a_sol,
Location a_loc)
1266template <
typename MF>
1270 if (!linop.isCellCentered()) {
1278template <
typename MF>
1284 if (!linop.isCellCentered()) {
1288 linop.getEBFluxes(a_eb_flux, a_sol);
1292template <
typename MF>
1300 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1303 sol_is_alias.resize(namrlevs,
true);
1304 for (
int alev = 0; alev < namrlevs; ++alev)
1306 if (cf_strategy == CFStrategy::ghostnodes ||
nGrowVect(*a_sol[alev]) == ng_sol)
1308 sol[alev] = linop.makeAlias(*a_sol[alev]);
1309 sol_is_alias[alev] =
true;
1313 if (sol_is_alias[alev])
1315 sol[alev] = linop.make(alev, 0, ng_sol);
1316 sol_is_alias[alev] =
false;
1325 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1326 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
1327 const MF* prhs = a_rhs[alev];
1328#if (AMREX_SPACEDIM != 3)
1329 int nghost = (cf_strategy == CFStrategy::ghostnodes) ? linop.getNGrow(alev) : 0;
1331 MFInfo(), *linop.Factory(alev));
1333 linop.applyMetricTerm(alev, 0, rhstmp);
1334 linop.unimposeNeumannBC(alev, rhstmp);
1335 linop.applyInhomogNeumannTerm(alev, rhstmp);
1338 linop.solutionResidual(alev, *a_res[alev], sol[alev], *prhs, crse_bcdata);
1339 if (alev < finest_amr_lev) {
1340 linop.reflux(alev, *a_res[alev], sol[alev], *prhs,
1341 *a_res[alev+1], sol[alev+1], *a_rhs[alev+1]);
1342 if (linop.isCellCentered()) {
1344 EB_average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1346 average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1353#if (AMREX_SPACEDIM != 3)
1354 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1355 linop.unapplyMetricTerm(alev, 0, *a_res[alev]);
1360template <
typename MF>
1371 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1373 for (
int alev = 0; alev < namrlevs; ++alev)
1375 if (cf_strategy == CFStrategy::ghostnodes)
1377 nghost = linop.getNGrow(alev);
1378 in[alev] = a_in[alev];
1380 else if (
nGrowVect(*a_in[alev]) == ng_sol)
1382 in[alev] = a_in[alev];
1387 if (cf_strategy == CFStrategy::ghostnodes) { ng =
IntVect(nghost); }
1388 in_raii[alev] = linop.make(alev, 0, ng,
1391 in[alev] = &(in_raii[alev]);
1393 rh[alev] = linop.make(alev, 0,
IntVect(nghost),
1400 for (
int alev = 0; alev < namrlevs; ++alev) {
1401 linop.applyInhomogNeumannTerm(alev, rh[alev]);
1405 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1406 const MF* crse_bcdata = (alev > 0) ? in[alev-1] :
nullptr;
1407 linop.solutionResidual(alev, *out[alev], *in[alev], rh[alev], crse_bcdata);
1408 if (alev < finest_amr_lev) {
1409 linop.reflux(alev, *out[alev], *in[alev], rh[alev],
1410 *out[alev+1], *in[alev+1], rh[alev+1]);
1411 if (linop.isCellCentered()) {
1412 if constexpr (IsMultiFabLike_v<MF>) {
1416 average_down(*out[alev+1], *out[alev], 0,
nComp(*out[alev]), linop.AMRRefRatioVect(alev));
1419 amrex::Abort(
"MLMG: TODO average_down for non-MultiFab");
1425#if (AMREX_SPACEDIM != 3)
1426 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1427 linop.unapplyMetricTerm(alev, 0, *out[alev]);
1431 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1432 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1433 Scale(*out[alev],
RT(-1), 0,
nComp(*out[alev]), nghost);
1437template <
typename MF>
1441 precond_mode =
true;
1442 linop.beginPrecondBC();
1444 linop.endPrecondBC();
1445 precond_mode =
false;
1448template <
typename MF>
1449template <
typename AMF>
1458 timer.assign(ntimers, 0.0);
1462 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1464 if (!linop_prepared) {
1465 linop.prepareForSolve();
1466 linop_prepared =
true;
1467 }
else if (linop.needsUpdate()) {
1470#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1471 hypre_solver.reset();
1472 hypre_bndry.reset();
1473 hypre_node_solver.reset();
1476#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1477 petsc_solver.reset();
1478 petsc_bndry.reset();
1483 sol_is_alias.resize(namrlevs,
false);
1484 for (
int alev = 0; alev < namrlevs; ++alev)
1486 if (cf_strategy == CFStrategy::ghostnodes)
1488 if constexpr (std::is_same<AMF,MF>()) {
1489 sol[alev] = linop.makeAlias(*a_sol[alev]);
1490 sol_is_alias[alev] =
true;
1492 amrex::Abort(
"Type conversion not supported for CFStrategy::ghostnodes");
1497 bool alias_made =
false;
1498 if (
nGrowVect(*a_sol[alev]) == ng_sol) {
1499 if constexpr (std::is_same<AMF,MF>()) {
1500 sol[alev] = linop.makeAlias(*a_sol[alev]);
1501 sol_is_alias[alev] =
true;
1506 if (sol_is_alias[alev] || !solve_called) {
1507 sol[alev] = linop.make(alev, 0, ng_sol);
1508 sol_is_alias[alev] =
false;
1516 rhs.resize(namrlevs);
1517 for (
int alev = 0; alev < namrlevs; ++alev)
1519 if (cf_strategy == CFStrategy::ghostnodes) { ng_rhs =
IntVect(linop.getNGrow(alev)); }
1520 if (!solve_called) {
1521 rhs[alev] = linop.make(alev, 0, ng_rhs);
1523 LocalCopy(rhs[alev], *a_rhs[alev], 0, 0, ncomp, ng_rhs);
1524 linop.applyMetricTerm(alev, 0, rhs[alev]);
1525 linop.unimposeNeumannBC(alev, rhs[alev]);
1526 linop.applyInhomogNeumannTerm(alev, rhs[alev]);
1527 linop.applyOverset(alev, rhs[alev]);
1528 if ( ! precond_mode) {
1529 bool r = linop.scaleRHS(alev, &(rhs[alev]));
1535 if (factory && !factory->isAllRegular()) {
1536 if constexpr (std::is_same<MF,MultiFab>()) {
1540 amrex::Abort(
"TODO: MLMG with EB only works with MultiFab");
1546 for (
int falev = finest_amr_lev; falev > 0; --falev)
1548 linop.averageDownSolutionRHS(falev-1, sol[falev-1], rhs[falev-1], sol[falev], rhs[falev]);
1552 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1557 IntVect ng = linop.getNGrowVectRestriction();
1558 if (cf_strategy == CFStrategy::ghostnodes) { ng = ng_rhs; }
1559 if (!solve_called) {
1560 linop.make(res, ng);
1561 linop.make(rescor, ng);
1563 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1565 const int nmglevs = linop.NMGLevels(alev);
1566 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1568 setVal(res [alev][mglev],
RT(0.0));
1569 setVal(rescor[alev][mglev],
RT(0.0));
1573 if (cf_strategy != CFStrategy::ghostnodes) { ng = ng_sol; }
1575 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1577 const int nmglevs = linop.NMGLevels(alev);
1578 cor[alev].resize(nmglevs);
1579 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1581 if (!solve_called) {
1583 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev,mglev)); }
1584 cor[alev][mglev] = linop.make(alev, mglev, _ng);
1590 cor_hold.resize(std::max(namrlevs-1,1));
1593 const int nmglevs = linop.NMGLevels(alev);
1594 cor_hold[alev].resize(nmglevs);
1595 for (
int mglev = 0; mglev < nmglevs-1; ++mglev)
1597 if (!solve_called) {
1599 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev,mglev)); }
1600 cor_hold[alev][mglev] = linop.make(alev, mglev, _ng);
1602 setVal(cor_hold[alev][mglev],
RT(0.0));
1605 for (
int alev = 1; alev < finest_amr_lev; ++alev)
1607 cor_hold[alev].resize(1);
1608 if (!solve_called) {
1610 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev)); }
1611 cor_hold[alev][0] = linop.make(alev, 0, _ng);
1613 setVal(cor_hold[alev][0],
RT(0.0));
1617 || !linop.supportNSolve())
1622 if (do_nsolve && ns_linop ==
nullptr)
1628 amrex::Print() << print_ident <<
"MLMG: # of AMR levels: " << namrlevs <<
"\n"
1629 << print_ident <<
" # of MG levels on the coarsest AMR level: " << linop.NMGLevels(0)
1632 amrex::Print() << print_ident <<
" # of MG levels in N-Solve: " << ns_linop->NMGLevels(0) <<
"\n"
1633 << print_ident <<
" # of grids in N-Solve: " << ns_linop->m_grids[0][0].size() <<
"\n";
1638template <
typename MF>
1642 if (!linop_prepared) {
1643 linop.prepareForSolve();
1644 linop_prepared =
true;
1645 }
else if (linop.needsUpdate()) {
1648#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1649 hypre_solver.reset();
1650 hypre_bndry.reset();
1651 hypre_node_solver.reset();
1654#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1655 petsc_solver.reset();
1656 petsc_bndry.reset();
1661template <
typename MF>
1666 linop.preparePrecond();
1669template <
typename MF>
1673 if constexpr (IsMultiFabLike_v<MF>) {
1674 ns_linop = linop.makeNLinOp(nsolve_grid_size);
1677 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(); }
1679 const BoxArray& ba = (*ns_linop).m_grids[0][0];
1683 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1684 ns_sol = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1686 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1687 ns_rhs = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1691 ns_linop->setLevelBC(0, ns_sol.get());
1693 ns_mlmg = std::make_unique<MLMGT<MF>>(*ns_linop);
1694 ns_mlmg->setVerbose(0);
1695 ns_mlmg->setFixedIter(1);
1696 ns_mlmg->setMaxFmgIter(20);
1703template <
typename MF>
1708 for (
int alev = finest_amr_lev; alev > 0; --alev)
1713 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(alev)); }
1714 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1717 computeResWithCrseSolFineCor(alev-1,alev);
1719 if (alev != finest_amr_lev) {
1720 std::swap(cor_hold[alev][0], cor[alev][0]);
1727 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1729 makeSolvable(0,0,res[0][0]);
1732 if (iter < max_fmg_iters) {
1739 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(0)); }
1740 LocalAdd(sol[0], cor[0][0], 0, 0, ncomp, nghost);
1743 for (
int alev = 1; alev <= finest_amr_lev; ++alev)
1746 interpCorrection(alev);
1749 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(alev)); }
1750 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1752 if (alev != finest_amr_lev) {
1753 LocalAdd(cor_hold[alev][0], cor[alev][0], 0, 0, ncomp, nghost);
1757 computeResWithCrseCorFineCor(alev);
1761 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1763 if (alev != finest_amr_lev) {
1764 LocalAdd(cor[alev][0], cor_hold[alev][0], 0, 0, ncomp, nghost);
1768 linop.averageDownAndSync(sol);
1771template <
typename MF>
1776 const int mglev = 0;
1777 mgVcycle(amrlev, mglev);
1782template <
typename MF>
1788 const int mglev_bottom = linop.NMGLevels(amrlev) - 1;
1790 for (
int mglev = mglev_top; mglev < mglev_bottom; ++mglev)
1792 BL_PROFILE_VAR(
"MLMG::mgVcycle_down::"+std::to_string(mglev), blp_mgv_down_lev);
1797 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1798 <<
" DN: Norm before smooth " <<
norm <<
"\n";
1801 setVal(cor[amrlev][mglev],
RT(0.0));
1802 bool skip_fillboundary =
true;
1803 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev], skip_fillboundary, nu1);
1806 computeResOfCorrection(amrlev, mglev);
1811 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1812 <<
" DN: Norm after smooth " <<
norm <<
"\n";
1816 linop.restriction(amrlev, mglev+1, res[amrlev][mglev+1], rescor[amrlev][mglev]);
1825 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1826 <<
" DN: Norm before bottom " <<
norm <<
"\n";
1831 computeResOfCorrection(amrlev, mglev_bottom);
1833 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1834 <<
" UP: Norm after bottom " <<
norm <<
"\n";
1842 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1843 <<
" Norm before smooth " <<
norm <<
"\n";
1845 setVal(cor[amrlev][mglev_bottom],
RT(0.0));
1846 bool skip_fillboundary =
true;
1847 linop.smooth(amrlev, mglev_bottom, cor[amrlev][mglev_bottom],
1848 res[amrlev][mglev_bottom], skip_fillboundary, nu1);
1851 computeResOfCorrection(amrlev, mglev_bottom);
1853 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1854 <<
" Norm after smooth " <<
norm <<
"\n";
1859 for (
int mglev = mglev_bottom-1; mglev >= mglev_top; --mglev)
1861 BL_PROFILE_VAR(
"MLMG::mgVcycle_up::"+std::to_string(mglev), blp_mgv_up_lev);
1863 addInterpCorrection(amrlev, mglev);
1866 computeResOfCorrection(amrlev, mglev);
1868 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1869 <<
" UP: Norm before smooth " <<
norm <<
"\n";
1871 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev],
false, nu2);
1873 if (cf_strategy == CFStrategy::ghostnodes) { computeResOfCorrection(amrlev, mglev); }
1877 computeResOfCorrection(amrlev, mglev);
1879 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1880 <<
" UP: Norm after smooth " <<
norm <<
"\n";
1888template <
typename MF>
1895 auto* pf = linop.Factory(0);
1896 auto is_all_regular = [pf] () {
1905 AMREX_ASSERT(linop.isCellCentered() || is_all_regular());
1908 const int amrlev = 0;
1909 const int mg_bottom_lev = linop.NMGLevels(amrlev) - 1;
1911 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(amrlev)); }
1913 for (
int mglev = 1; mglev <= mg_bottom_lev; ++mglev)
1915 linop.avgDownResMG(mglev, res[amrlev][mglev], res[amrlev][mglev-1]);
1920 for (
int mglev = mg_bottom_lev-1; mglev >= 0; --mglev)
1923 interpCorrection(amrlev, mglev);
1926 computeResOfCorrection(amrlev, mglev);
1928 LocalCopy(res[amrlev][mglev], rescor[amrlev][mglev], 0, 0, ncomp, nghost);
1931 std::swap(cor[amrlev][mglev], cor_hold[amrlev][mglev]);
1932 mgVcycle(amrlev, mglev);
1933 LocalAdd(cor[amrlev][mglev], cor_hold[amrlev][mglev], 0, 0, ncomp, nghost);
1940template <
typename MF>
1946 NSolve(*ns_mlmg, *ns_sol, *ns_rhs);
1950 actualBottomSolve();
1954template <
typename MF>
1962 MF
const& res_bottom = res[0].back();
1973 RT(-1.0),
RT(-1.0));
1975 linop.copyNSolveSolution(cor[0].back(), a_sol);
1978template <
typename MF>
1983 m_niters_cg.push_back(niters);
1986 const int amrlev = 0;
1987 const int mglev = linop.NMGLevels(amrlev) - 1;
1988 auto&
x = cor[amrlev][mglev];
1989 auto& b = res[amrlev][mglev];
1990 const int n = (ret==0) ? nub : nuf;
1991 linop.smooth(amrlev, mglev,
x, b,
false, n);
1994template <
typename MF>
2000 if (!linop.isBottomActive()) {
return; }
2006 const int amrlev = 0;
2007 const int mglev = linop.NMGLevels(amrlev) - 1;
2008 auto&
x = cor[amrlev][mglev];
2009 auto& b = res[amrlev][mglev];
2015 bool skip_fillboundary =
true;
2016 linop.smooth(amrlev, mglev,
x, b, skip_fillboundary, nuf);
2022 if (linop.isBottomSingular() && linop.getEnforceSingularSolvable())
2025 raii_b = linop.make(amrlev, mglev, ng,
2030 makeSolvable(amrlev,mglev,*bottom_b);
2035#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2036 if constexpr (std::is_same<MF,MultiFab>()) {
2037 bottomSolveWithHypre(
x, *bottom_b);
2041 amrex::Abort(
"Using Hypre as bottom solver not supported in this case");
2046#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
2047 if constexpr (std::is_same<MF,MultiFab>()) {
2048 bottomSolveWithPETSc(
x, *bottom_b);
2052 amrex::Abort(
"Using PETSc as bottom solver not supported in this case");
2057 linop.customBottomSolve(
this,
x, *bottom_b, bottom_reltol, bottom_abstol,
2070 int ret = bottomSolveWithCG(
x, *bottom_b, cg_type);
2084 ret = bottomSolveWithCG(
x, *bottom_b, cg_type);
2100 if (! timer.empty()) {
2105template <
typename MF>
2115 if (cf_strategy == CFStrategy::ghostnodes) { cg_solver.
setNGhost(linop.getNGrow()); }
2117 int ret = cg_solver.
solve(
x, b, bottom_reltol, bottom_abstol);
2118 if (ret != 0 && verbose > 1) {
2119 amrex::Print() << print_ident <<
"MLMG: Bottom solve failed.\n";
2123 if (ret != 0 && ret != 9) {
2131template <
typename MF>
2137 const int mglev = 0;
2138 for (
int alev = amrlevmax; alev >= 0; --alev) {
2139 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
2140 linop.solutionResidual(alev, res[alev][mglev], sol[alev], rhs[alev], crse_bcdata);
2141 if (alev < finest_amr_lev) {
2142 linop.reflux(alev, res[alev][mglev], sol[alev], rhs[alev],
2143 res[alev+1][mglev], sol[alev+1], rhs[alev+1]);
2149template <
typename MF>
2154 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
2155 linop.solutionResidual(alev, res[alev][0], sol[alev], rhs[alev], crse_bcdata);
2159template <
typename MF>
2163 BL_PROFILE(
"MLMG::computeResWithCrseSolFineCor()");
2166 if (cf_strategy == CFStrategy::ghostnodes) {
2167 nghost =
IntVect(std::min(linop.getNGrow(falev),linop.getNGrow(calev)));
2170 MF& crse_sol = sol[calev];
2171 const MF& crse_rhs = rhs[calev];
2172 MF& crse_res = res[calev][0];
2174 MF& fine_sol = sol[falev];
2175 const MF& fine_rhs = rhs[falev];
2176 MF& fine_cor = cor[falev][0];
2177 MF& fine_res = res[falev][0];
2178 MF& fine_rescor = rescor[falev][0];
2180 const MF* crse_bcdata = (calev > 0) ? &(sol[calev-1]) :
nullptr;
2181 linop.solutionResidual(calev, crse_res, crse_sol, crse_rhs, crse_bcdata);
2183 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res, BCMode::Homogeneous);
2184 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2186 linop.reflux(calev, crse_res, crse_sol, crse_rhs, fine_res, fine_sol, fine_rhs);
2188 linop.avgDownResAmr(calev, crse_res, fine_res);
2192template <
typename MF>
2196 BL_PROFILE(
"MLMG::computeResWithCrseCorFineCor()");
2199 if (cf_strategy == CFStrategy::ghostnodes) {
2200 nghost =
IntVect(linop.getNGrow(falev));
2203 const MF& crse_cor = cor[falev-1][0];
2205 MF& fine_cor = cor [falev][0];
2206 MF& fine_res = res [falev][0];
2207 MF& fine_rescor = rescor[falev][0];
2210 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res,
2211 BCMode::Inhomogeneous, &crse_cor);
2212 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2216template <
typename MF>
2223 if (cf_strategy == CFStrategy::ghostnodes) {
2224 nghost =
IntVect(linop.getNGrow(alev));
2227 MF & crse_cor = cor[alev-1][0];
2228 MF & fine_cor = cor[alev ][0];
2230 const Geometry& crse_geom = linop.Geom(alev-1,0);
2233 int ng_dst = linop.isCellCentered() ? 1 : 0;
2234 if (cf_strategy == CFStrategy::ghostnodes)
2236 ng_src = linop.getNGrow(alev-1);
2237 ng_dst = linop.getNGrow(alev-1);
2238 if constexpr (IsMultiFabLike_v<MF>) {
2241 amrex::Abort(
"MLMG: CFStrategy::ghostnodes not supported for non-MultiFab like types");
2245 MF cfine = linop.makeCoarseAmr(alev,
IntVect(ng_dst),
2251 linop.interpolationAmr(alev, fine_cor, cfine, nghost);
2257template <
typename MF>
2263 MF& crse_cor = cor[alev][mglev+1];
2264 MF& fine_cor = cor[alev][mglev ];
2265 linop.interpAssign(alev, mglev, fine_cor, crse_cor);
2269template <
typename MF>
2275 const MF& crse_cor = cor[alev][mglev+1];
2276 MF& fine_cor = cor[alev][mglev ];
2281 if (linop.isMFIterSafe(alev, mglev, mglev+1))
2287 cfine = linop.makeCoarseMG(alev, mglev,
IntVect(0),
2293 linop.interpolation(alev, mglev, fine_cor, *cmf);
2300template <
typename MF>
2305 MF &
x = cor[amrlev][mglev];
2306 const MF& b = res[amrlev][mglev];
2307 MF & r = rescor[amrlev][mglev];
2308 linop.correctionResidual(amrlev, mglev, r,
x, b, BCMode::Homogeneous);
2312template <
typename MF>
2317 return linop.normInf(alev, res[alev][0], local);
2321template <
typename MF>
2327 for (
int alev = 0; alev <= alevmax; ++alev)
2329 r = std::max(r, ResNormInf(alev,
true));
2336template <
typename MF>
2342 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
2343 auto t = linop.normInf(alev, rhs[alev],
true);
2350template <
typename MF>
2354 auto const&
offset = linop.getSolvabilityOffset(0, 0, rhs[0]);
2356 for (
int c = 0; c < ncomp; ++c) {
2357 amrex::Print() << print_ident <<
"MLMG: Subtracting " <<
offset[c] <<
" from rhs component "
2361 for (
int alev = 0; alev < namrlevs; ++alev) {
2362 linop.fixSolvabilityByOffset(alev, 0, rhs[alev],
offset);
2366template <
typename MF>
2370 auto const&
offset = linop.getSolvabilityOffset(amrlev, mglev, mf);
2372 for (
int c = 0; c < ncomp; ++c) {
2374 <<
" from mf component c = " << c
2375 <<
" on level (" << amrlev <<
", " << mglev <<
")\n";
2378 linop.fixSolvabilityByOffset(amrlev, mglev, mf,
offset);
2381#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2382template <
typename MF>
2384requires (std::same_as<TMF,MultiFab>)
2388 const int amrlev = 0;
2389 const int mglev = linop.NMGLevels(amrlev) - 1;
2393 if (linop.isCellCentered())
2395 if (hypre_solver ==
nullptr)
2397 hypre_solver = linop.makeHypre(hypre_interface);
2399 hypre_solver->setVerbose(bottom_verbose);
2401 hypre_solver->setHypreOptionsNamespace(hypre_options_namespace);
2403 hypre_solver->setHypreOldDefault(hypre_old_default);
2404 hypre_solver->setHypreRelaxType(hypre_relax_type);
2405 hypre_solver->setHypreRelaxOrder(hypre_relax_order);
2406 hypre_solver->setHypreNumSweeps(hypre_num_sweeps);
2407 hypre_solver->setHypreStrongThreshold(hypre_strong_threshold);
2410 const BoxArray& ba = linop.m_grids[amrlev].back();
2411 const DistributionMapping& dm = linop.m_dmap[amrlev].back();
2412 const Geometry& geom = linop.m_geom[amrlev].back();
2414 hypre_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
2415 hypre_bndry->setHomogValues();
2416 const Real* dx = linop.m_geom[0][0].CellSize();
2417 IntVect crse_ratio = linop.m_coarse_data_crse_ratio.
allGT(0) ? linop.m_coarse_data_crse_ratio :
IntVect(1);
2419 0.5*dx[1]*crse_ratio[1],
2420 0.5*dx[2]*crse_ratio[2]));
2421 hypre_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc,
IntVect(-1), bclocation,
2422 linop.m_coarse_fine_bc_type);
2428 ? bottom_abstol :
Real(-1.0);
2429 hypre_solver->solve(
2430 x, b, bottom_reltol, hypre_abstol, bottom_maxiter, *hypre_bndry,
2431 linop.getMaxOrder());
2435 if (hypre_node_solver ==
nullptr)
2438 linop.makeHypreNodeLap(bottom_verbose, hypre_options_namespace);
2440 hypre_node_solver->solve(
x, b, bottom_reltol, bottom_abstol, bottom_maxiter);
2445 if (linop.isSingular(amrlev) && linop.getEnforceSingularSolvable())
2447 makeSolvable(amrlev, mglev,
x);
2452#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
2453template <
typename MF>
2455requires (std::same_as<TMF,MultiFab>)
2457MLMGT<MF>::bottomSolveWithPETSc (MF&
x,
const MF& b)
2461 if(petsc_solver ==
nullptr)
2463 petsc_solver = linop.makePETSc();
2464 petsc_solver->setVerbose(bottom_verbose);
2466 const BoxArray& ba = linop.m_grids[0].back();
2467 const DistributionMapping& dm = linop.m_dmap[0].back();
2468 const Geometry& geom = linop.m_geom[0].back();
2470 petsc_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
2471 petsc_bndry->setHomogValues();
2472 const Real* dx = linop.m_geom[0][0].CellSize();
2473 auto crse_ratio = linop.m_coarse_data_crse_ratio.allGT(0) ? linop.m_coarse_data_crse_ratio :
IntVect(1);
2475 0.5*dx[1]*crse_ratio[1],
2476 0.5*dx[2]*crse_ratio[2]));
2477 petsc_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc,
IntVect(-1), bclocation,
2478 linop.m_coarse_fine_bc_type);
2480 petsc_solver->solve(
x, b, bottom_reltol,
Real(-1.), bottom_maxiter, *petsc_bndry,
2481 linop.getMaxOrder());
2485template <
typename MF>
2487MLMGT<MF>::checkPoint (
const Vector<MultiFab*>& a_sol,
2488 const Vector<MultiFab const*>& a_rhs,
2489 RT a_tol_rel, RT a_tol_abs,
const char* a_file_name)
const
2491 std::string file_name(a_file_name);
2496 std::string HeaderFileName(std::string(a_file_name)+
"/Header");
2497 std::ofstream HeaderFile;
2498 HeaderFile.open(HeaderFileName.c_str(), std::ofstream::out |
2499 std::ofstream::trunc |
2500 std::ofstream::binary);
2501 if( ! HeaderFile.good()) {
2505 HeaderFile.precision(17);
2509 HeaderFile << linop.name() <<
"\n"
2510 <<
"a_tol_rel = " << a_tol_rel <<
"\n"
2511 <<
"a_tol_abs = " << a_tol_abs <<
"\n"
2512 <<
"verbose = " <<
verbose <<
"\n"
2513 <<
"max_iters = " << max_iters <<
"\n"
2514 <<
"nu1 = " << nu1 <<
"\n"
2515 <<
"nu2 = " << nu2 <<
"\n"
2516 <<
"nuf = " << nuf <<
"\n"
2517 <<
"nub = " << nub <<
"\n"
2518 <<
"max_fmg_iters = " << max_fmg_iters <<
"\n"
2519 <<
"bottom_solver = " <<
static_cast<int>(bottom_solver) <<
"\n"
2520 <<
"bottom_verbose = " << bottom_verbose <<
"\n"
2521 <<
"bottom_maxiter = " << bottom_maxiter <<
"\n"
2522 <<
"bottom_reltol = " << bottom_reltol <<
"\n"
2523 <<
"convergence_norm = " << norm_name <<
"\n"
2524 <<
"namrlevs = " << namrlevs <<
"\n"
2525 <<
"finest_amr_lev = " << finest_amr_lev <<
"\n"
2526 <<
"linop_prepared = " << linop_prepared <<
"\n"
2527 <<
"solve_called = " << solve_called <<
"\n";
2529 for (
int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
2536 for (
int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
2537 VisMF::Write(*a_sol[ilev], file_name+
"/Level_"+std::to_string(ilev)+
"/sol");
2538 VisMF::Write(*a_rhs[ilev], file_name+
"/Level_"+std::to_string(ilev)+
"/rhs");
2541 linop.checkPoint(file_name+
"/linop");
2544template <
typename MF>
2548 print_ident.resize(print_ident.size()+4,
' ');
2551template <
typename MF>
2555 if (print_ident.size() > 4) {
2556 print_ident.resize(print_ident.size()-4,
' ');
2558 print_ident.clear();
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:562
#define BL_PROFILE_VAR_STOP(vname)
Definition AMReX_BLProfiler.H:574
#define BL_PROFILE_VAR(fname, vname)
Definition AMReX_BLProfiler.H:571
#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.
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1131
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
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:1251
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:51
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
bool isAllRegular() const noexcept
Definition AMReX_EBFabFactory.cpp:232
Solve using GMRES with multigrid as preconditioner.
Definition AMReX_GMRES_MLMG.H:28
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:85
Periodicity periodicity() const noexcept
Return the Periodicity based on the length of the domain.
Definition AMReX_Geometry.H:424
Interface
HYPRE interface modes supported.
Definition AMReX_Hypre.H:37
__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:517
__host__ __device__ constexpr IntVectND< new_dim > resize(int fill_extra=0) const noexcept
Returns a new IntVectND of size new_dim by either shrinking or expanding this IntVectND.
Definition AMReX_IntVect.H:867
__host__ static __device__ constexpr IntVectND< dim > TheDimensionVector(int d) noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:790
CG-family solvers (BiCGStab or CG) for use as the bottom solver in MLMG.
Definition AMReX_MLCGSolver.H:21
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
void setPrintIdentation(std::string s)
Prefix printed messages (e.g., to indent per level).
Definition AMReX_MLCGSolver.H:90
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
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
Abstract base class for multilevel linear operators used by MLMG and the bottom solvers.
Definition AMReX_MLLinOp.H:137
typename FabDataType< MF >::fab_type FAB
Definition AMReX_MLLinOp.H:147
typename FabDataType< MF >::value_type RT
Definition AMReX_MLLinOp.H:148
Definition AMReX_MLMG.H:31
Definition AMReX_MLMG.H:26
void prepareForFluxes(Vector< MF const * > const &a_sol)
Build boundary caches needed by getFluxes()/compFluxes.
Definition AMReX_MLMG.H:1047
void setBottomVerbose(int v) noexcept
Verbosity for the bottom solver (0 silent).
Definition AMReX_MLMG.H:339
void setMaxFmgIter(int n) noexcept
Cap the number of FMG cycles executed.
Definition AMReX_MLMG.H:280
RT MLResNormInf(int alevmax, bool local=false)
Composite infinity norm of the residual up to level alevmax.
Definition AMReX_MLMG.H:2323
void postCG(int ret, int niters=-1)
Post CG smoothing.
Definition AMReX_MLMG.H:1980
RT MLRhsNormInf(bool local=false)
Composite infinity norm of the RHS.
Definition AMReX_MLMG.H:2338
void setNoGpuSync(bool do_not_sync) noexcept
Control implicit GPU synchronization inside solve().
Definition AMReX_MLMG.H:412
MLMGT(MLMGT< MF > &&)=delete
void actualBottomSolve()
Execute the actual bottom solve after pre-smoothing and restriction.
Definition AMReX_MLMG.H:1996
MF MFType
Definition AMReX_MLMG.H:39
BottomSolver getBottomSolver() const noexcept
Definition AMReX_MLMG.H:327
void setPreSmooth(int n) noexcept
Number of pre-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:301
void setBottomToleranceAbs(RT t) noexcept
Absolute tolerance for the bottom solver.
Definition AMReX_MLMG.H:357
RT getFinalResidual() const noexcept
Definition AMReX_MLMG.H:649
void interpCorrection(int alev)
Interpolate corrections onto AMR level alev.
Definition AMReX_MLMG.H:2218
void getEBFluxes(const Vector< MF * > &a_eb_flux)
Flux into the EB wall using the internally stored solution.
Definition AMReX_MLMG.H:1268
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:1058
void setBottomSmooth(int n) noexcept
Additional smoothing passes executed after the bottom solver.
Definition AMReX_MLMG.H:319
void setNSolve(int flag) noexcept
Enable (flag!=0) or disable the N-solve path.
Definition AMReX_MLMG.H:389
int getBottomVerbose() const
Definition AMReX_MLMG.H:248
void computeResOfCorrection(int amrlev, int mglev)
Compute the residual of the correction equation on (amrlev,mglev).
Definition AMReX_MLMG.H:2302
void applyPrecond(const Vector< MF * > &out, const Vector< MF * > &in)
Apply the linear operator as a preconditioner (out = L(in)).
Definition AMReX_MLMG.H:1439
void setCFStrategy(CFStrategy a_cf_strategy) noexcept
Select the coarse-fine synchronization strategy.
Definition AMReX_MLMG.H:333
void computeResWithCrseCorFineCor(int falev)
Residual update using coarse correction / fine correction.
Definition AMReX_MLMG.H:2194
void NSolve(MLMGT< MF > &a_solver, MF &a_sol, MF &a_rhs)
Perform an NSolve using an MLMGT wrapper.
Definition AMReX_MLMG.H:1956
typename MLLinOpT< MF >::Location Location
Definition AMReX_MLMG.H:44
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:1362
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:1089
void setNSolveGridSize(int s) noexcept
Set the tile size used for N-solve builds.
Definition AMReX_MLMG.H:395
void setVerbose(int v) noexcept
Set the main solver verbosity (0 silent).
Definition AMReX_MLMG.H:268
void computeMLResidual(int amrlevmax)
Compute the composite residual norm up to AMR level amrlevmax.
Definition AMReX_MLMG.H:2133
RT getInitResidual() const noexcept
Definition AMReX_MLMG.H:647
int getNumIters() const noexcept
Definition AMReX_MLMG.H:652
void setPostSmooth(int n) noexcept
Number of post-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:307
void mgVcycle(int amrlev, int mglev)
Run a multigrid V-cycle on (amrlev,mglev).
Definition AMReX_MLMG.H:1784
void prepareForNSolve()
Prepare the NSolve path.
Definition AMReX_MLMG.H:1671
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:1029
void makeSolvable()
Adjust RHS/solution to satisfy null-space constraints.
Definition AMReX_MLMG.H:2352
void setBottomSolver(BottomSolver s) noexcept
Select the bottom solver type (e.g., CG, BiCGStab, Hypre, PETSc).
Definition AMReX_MLMG.H:326
void preparePrecond()
Prepare preconditioner-specific caches (e.g., boundary data).
Definition AMReX_MLMG.H:1663
void incPrintIdentation()
Increase the indentation used when printing solver logs.
Definition AMReX_MLMG.H:2546
typename MLLinOpT< MF >::RT RT
Definition AMReX_MLMG.H:41
void setThrowException(bool t) noexcept
Control behavior when the solve fails to converge or blows up.
Definition AMReX_MLMG.H:262
void decPrintIdentation()
Decrease the indentation used when printing solver logs.
Definition AMReX_MLMG.H:2553
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:287
Vector< RT > const & getResidualHistory() const noexcept
Definition AMReX_MLMG.H:651
void prepareLinOp()
Finalize operator-dependent metadata before iterating.
Definition AMReX_MLMG.H:1640
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:294
CFStrategy
Definition AMReX_MLMG.H:47
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:1451
int bottomSolveWithCG(MF &x, const MF &b, typename MLCGSolverT< MF >::Type type)
Bottom solve using CG/BiCGStab implemented in MLCGSolverT.
Definition AMReX_MLMG.H:2107
void setAlwaysUseBNorm(int flag) noexcept
Deprecated flag for forcing B-norm convergence checks.
Definition AMReX_MLMG.H:783
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:1294
void miniCycle(int amrlev)
Execute a per-level mini cycle.
Definition AMReX_MLMG.H:1773
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 setFinalFillBC(int flag) noexcept
Force a FillBoundary at the end of the solve (nonzero enables).
Definition AMReX_MLMG.H:380
typename MLLinOpT< MF >::BCMode BCMode
Definition AMReX_MLMG.H:43
void setConvergenceNormType(MLMGNormType norm) noexcept
Choose the norm used for convergence tests.
Definition AMReX_MLMG.H:373
void computeResWithCrseSolFineCor(int calev, int falev)
Residual update using coarse solution / fine correction.
Definition AMReX_MLMG.H:2161
MLMGT< MF > & operator=(MLMGT< MF > const &)=delete
MLMGT(MLLinOpT< MF > &a_lp)
Definition AMReX_MLMG.H:774
void computeResidual(int alev)
Compute the residual on AMR level alev.
Definition AMReX_MLMG.H:2151
MLLinOpT< MF > & getLinOp()
Definition AMReX_MLMG.H:655
typename MLLinOpT< MF >::FAB FAB
Definition AMReX_MLMG.H:40
RT getBottomToleranceAbs() const noexcept
Definition AMReX_MLMG.H:358
int numAMRLevels() const noexcept
Definition AMReX_MLMG.H:382
MLMGT(MLMGT< MF > const &)=delete
void mgFcycle()
Run an FMG cycle starting from the coarsest grid.
Definition AMReX_MLMG.H:1890
RT getInitRHS() const noexcept
Definition AMReX_MLMG.H:645
RT ResNormInf(int alev, bool local=false)
Infinity norm of the residual on level alev.
Definition AMReX_MLMG.H:2314
Vector< int > const & getNumCGIters() const noexcept
Definition AMReX_MLMG.H:653
void bottomSolve()
Execute the configured bottom solver (Hypre, PETSc, CG, etc.).
Definition AMReX_MLMG.H:1942
void setBottomTolerance(RT t) noexcept
Relative tolerance for the bottom solver.
Definition AMReX_MLMG.H:351
void setFinalSmooth(int n) noexcept
Number of smoothing passes when MLMG is used standalone (final smooth).
Definition AMReX_MLMG.H:313
void addInterpCorrection(int alev, int mglev)
Add interpolated corrections to (alev,mglev) data.
Definition AMReX_MLMG.H:2271
int getVerbose() const
Definition AMReX_MLMG.H:247
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 setBottomMaxIter(int n) noexcept
Cap the number of iterations inside the bottom solver.
Definition AMReX_MLMG.H:345
void oneIter(int iter)
Execute a single multigrid iteration (FMG or V-cycle).
Definition AMReX_MLMG.H:1704
void setMaxIter(int n) noexcept
Cap the number of MLMG iterations executed.
Definition AMReX_MLMG.H:274
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:980
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:1630
std::array< T, N > Array
Definition AMReX_Array.H:31
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:825
void Max(KeyValuePair< K, V > &vi, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:133
#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 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:112
Definition AMReX_Amr.cpp:50
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:259
void FileOpenFailed(const std::string &file)
Output a message and abort when couldn't open the file.
Definition AMReX_Utility.cpp:116
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:809
__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:359
void Scale(MF &dst, typename MF::value_type val, int scomp, int ncomp, int nghost)
dst *= val
Definition AMReX_FabArrayUtility.H:2175
BoxArray const & boxArray(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.boxArray().
Definition AMReX_FabArrayBase.cpp:2870
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.DistributionMap().
Definition AMReX_FabArrayBase.cpp:2875
void average_face_to_cellcenter(MultiFab &cc, int dcomp, const Vector< const MultiFab * > &fc, IntVect const &ng_vect)
Definition AMReX_MultiFabUtil.cpp:156
void EB_set_covered(MultiFab &mf, Real val)
Fill all covered cells with a single value val.
Definition AMReX_EBMultiFabUtil.cpp:21
double second() noexcept
Definition AMReX_Utility.cpp:919
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:2860
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:2251
void UtilCreateCleanDirectory(const std::string &path, bool callbarrier=true)
Create a new directory, renaming the old one if it exists.
Definition AMReX_Utility.cpp:146
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:331
BottomSolver
Definition AMReX_MLLinOp.H:40
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
RealVectND< 3 > RealVect
Definition AMReX_ParmParse.H:39
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:2865
void LocalCopy(DMF &dst, SMF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst = src
Definition AMReX_FabArrayUtility.H:2182
void setBndry(MF &dst, typename MF::value_type val, int scomp, int ncomp)
dst = val in ghost cells.
Definition AMReX_FabArrayUtility.H:2168
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:2262
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:242
MLMGNormType
Definition AMReX_MLMG.H:21
void LocalAdd(MF &dst, MF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst += src
Definition AMReX_FabArrayUtility.H:2190
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:2161
BCMode
Definition AMReX_MLLinOp.H:117
Location
Definition AMReX_MLLinOp.H:119
FabArray memory allocation information.
Definition AMReX_FabArray.H:73
MFInfo & SetArena(Arena *ar) noexcept
Select the Arena used for FAB storage.
Definition AMReX_FabArray.H:87