3#include <AMReX_Config.H>
28 :
public std::runtime_error
31 using std::runtime_error::runtime_error;
65 template <
typename AMF>
67 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file =
nullptr);
79 template <
typename AMF>
80 RT solve (std::initializer_list<AMF*> a_sol,
81 std::initializer_list<AMF const*> a_rhs,
82 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file =
nullptr);
94 RT a_tol_rel,
RT a_tol_abs);
102 template <
typename AMF>
104 Location a_loc = Location::FaceCenter);
112 template <
typename AMF>
114 Location a_loc = Location::FaceCenter);
122 template <
typename AMF>
124 Location a_loc = Location::FaceCenter);
132 template <
typename AMF>
134 Location a_loc = Location::FaceCenter);
143 template <
typename AMF>
146 Location a_loc = Location::FaceCenter);
155 template <
typename AMF>
157 std::initializer_list<AMF*> a_sol,
158 Location a_loc = Location::FaceCenter);
166 template <
typename AMF>
168 Location a_loc = Location::CellCenter);
176 template <
typename AMF>
177 void getFluxes (std::initializer_list<AMF*> a_flux,
178 Location a_loc = Location::CellCenter);
187 template <
typename AMF>
190 Location a_loc = Location::CellCenter);
199 template <
typename AMF>
200 void getFluxes (std::initializer_list<AMF*> a_flux,
201 std::initializer_list<AMF*> a_sol,
202 Location a_loc = Location::CellCenter);
285 void setFixedIter (
int nit)
noexcept { do_fixed_number_of_iters = nit; }
358 [[deprecated(
"Use MLMG::setConvergenceNormType() instead.")]]
387 void setNSolve (
int flag)
noexcept { do_nsolve = flag; }
400 void setNoGpuSync (
bool do_not_sync)
noexcept { do_no_sync_gpu = do_not_sync; }
402#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
422 void setHypreOptionsNamespace(
const std::string& prefix)
noexcept
424 hypre_options_namespace = prefix;
428 void setHypreOldDefault (
bool l)
noexcept {hypre_old_default = l;}
430 void setHypreRelaxType (
int n)
noexcept {hypre_relax_type = n;}
432 void setHypreRelaxOrder (
int n)
noexcept {hypre_relax_order = n;}
434 void setHypreNumSweeps (
int n)
noexcept {hypre_num_sweeps = n;}
436 void setHypreStrongThreshold (
Real t)
noexcept {hypre_strong_threshold = t;}
452 template <
typename AMF>
453 void prepareForSolve (Vector<AMF*>
const& a_sol, Vector<AMF const*>
const& a_rhs);
484 void mgVcycle (
int amrlev,
int mglev);
497 void NSolve (MLMGT<MF>& a_solver, MF& a_sol, MF& a_rhs);
587#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
595 template <
class TMF=MF>
596 requires (std::same_as<TMF,MultiFab>)
597 void bottomSolveWithHypre (MF&
x,
const MF& b);
600#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
608 template <
class TMF=MF>
609 requires (std::same_as<TMF,MultiFab>)
610 void bottomSolveWithPETSc (MF&
x,
const MF& b);
630 [[nodiscard]]
int getNumIters () const noexcept {
return m_iter_fine_resnorm0.
size(); }
637 bool precond_mode =
false;
638 bool throw_exception =
false;
642 int do_fixed_number_of_iters = 0;
643 int max_precond_iters = 1;
650 int max_fmg_iters = 0;
654 int bottom_verbose = 0;
655 int bottom_maxiter = 200;
656 RT bottom_reltol = std::is_same<RT,double>() ?
RT(1.e-4) :
RT(1.e-3);
657 RT bottom_abstol =
RT(-1.0);
661 int final_fill_bc = 0;
668 bool linop_prepared =
false;
669 Long solve_called = 0;
672 int do_nsolve =
false;
673 int nsolve_grid_size = 16;
674 std::unique_ptr<MLLinOpT<MF>> ns_linop;
675 std::unique_ptr<MLMGT<MF>> ns_mlmg;
676 std::unique_ptr<MF> ns_sol;
677 std::unique_ptr<MF> ns_rhs;
679 std::string print_ident;
681 bool do_no_sync_gpu =
false;
684#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
689 std::unique_ptr<Hypre> hypre_solver;
690 std::unique_ptr<MLMGBndryT<MF>> hypre_bndry;
691 std::unique_ptr<HypreNodeLap> hypre_node_solver;
693 std::string hypre_options_namespace =
"hypre";
694 bool hypre_old_default =
true;
695 int hypre_relax_type = 6;
696 int hypre_relax_order = 1;
697 int hypre_num_sweeps = 2;
698 Real hypre_strong_threshold = 0.25;
702#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
703 std::unique_ptr<PETScABecLap> petsc_solver;
704 std::unique_ptr<MLMGBndryT<MF>> petsc_bndry;
727 enum timer_types { solve_time=0, iter_time, bottom_time, ntimers };
728 Vector<double> timer;
730 RT m_rhsnorm0 =
RT(-1.0);
731 RT m_init_resnorm0 =
RT(-1.0);
732 RT m_final_resnorm0 =
RT(-1.0);
733 Vector<int> m_niters_cg;
734 Vector<RT> m_iter_fine_resnorm0;
745 void checkPoint (
const Vector<MultiFab*>& a_sol,
746 const Vector<MultiFab const*>& a_rhs,
747 RT a_tol_rel,
RT a_tol_abs,
const char* a_file_name)
const;
751template <
typename MF>
753 : linop(a_lp), ncomp(a_lp.getNComp()), namrlevs(a_lp.NAMRLevels()),
754 finest_amr_lev(a_lp.NAMRLevels()-1)
759template <
typename MF>
770template <
typename MF>
771template <
typename AMF>
774 std::initializer_list<AMF const*> a_rhs,
775 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
779 a_tol_rel, a_tol_abs, checkpoint_file);
782template <
typename MF>
783template <
typename AMF>
786 RT a_tol_rel,
RT a_tol_abs,
const char* checkpoint_file) ->
RT
790 bool prev_in_single_stream_region =
false;
791 bool prev_in_nosync_region =
false;
793 if (do_no_sync_gpu) {
798 if constexpr (std::is_same<AMF,MultiFab>()) {
799 if (checkpoint_file !=
nullptr) {
800 checkPoint(a_sol, a_rhs, a_tol_rel, a_tol_abs, checkpoint_file);
805 bottom_solver = linop.getDefaultBottomSolver();
808#if (defined(AMREX_USE_HYPRE) || defined(AMREX_USE_PETSC)) && (AMREX_SPACEDIM > 1)
809 if constexpr (IsFabArray_v<AMF>) {
811 int mo = linop.getMaxOrder();
812 if (a_sol[0]->hasEBFabFactory()) {
813 linop.setMaxOrder(2);
815 linop.setMaxOrder(std::min(3,mo));
821 bool is_nsolve = linop.m_parent;
825 RT& composite_norminf = m_final_resnorm0;
828 m_iter_fine_resnorm0.clear();
830 prepareForSolve(a_sol, a_rhs);
832 computeMLResidual(finest_amr_lev);
835 RT resnorm0 = MLResNormInf(finest_amr_lev, local);
836 RT rhsnorm0 = MLRhsNormInf(local);
842 amrex::Print() << print_ident <<
"MLMG: Initial rhs = " << rhsnorm0 <<
"\n"
843 << print_ident <<
"MLMG: Initial residual (resid0) = " << resnorm0 <<
"\n";
847 m_init_resnorm0 = resnorm0;
848 m_rhsnorm0 = rhsnorm0;
850 RT max_norm = resnorm0;
851 std::string norm_name =
"resid0";
854 if (rhsnorm0 >= resnorm0) {
858 norm_name =
"resid0";
867 norm_name =
"resid0";
872 const RT res_target = std::max(a_tol_abs, std::max(a_tol_rel,
RT(1.e-16))*max_norm);
874 if (!is_nsolve && resnorm0 <= res_target) {
875 composite_norminf = resnorm0;
877 amrex::Print() << print_ident <<
"MLMG: No iterations needed\n";
881 bool converged =
false;
883 const int niters = do_fixed_number_of_iters ? do_fixed_number_of_iters : max_iters;
884 for (
int iter = 0; iter < niters; ++iter)
893 if (is_nsolve) {
continue; }
895 RT fine_norminf = ResNormInf(finest_amr_lev);
896 m_iter_fine_resnorm0.push_back(fine_norminf);
897 composite_norminf = fine_norminf;
899 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1 <<
" Fine resid/"
900 << norm_name <<
" = " << fine_norminf/max_norm <<
"\n";
902 bool fine_converged = (fine_norminf <= res_target);
904 if (namrlevs == 1 && fine_converged) {
906 }
else if (fine_converged) {
908 computeMLResidual(finest_amr_lev-1);
909 RT crse_norminf = MLResNormInf(finest_amr_lev-1);
911 amrex::Print() << print_ident <<
"MLMG: Iteration " << std::setw(3) << iter+1
912 <<
" Crse resid/" << norm_name <<
" = "
913 << crse_norminf/max_norm <<
"\n";
915 converged = (crse_norminf <= res_target);
916 composite_norminf = std::max(fine_norminf, crse_norminf);
923 amrex::Print() << print_ident <<
"MLMG: Final Iter. " << iter+1
924 <<
" resid, resid/" << norm_name <<
" = "
925 << composite_norminf <<
", "
926 << composite_norminf/max_norm <<
"\n";
930 if (composite_norminf >
RT(1.e20)*max_norm)
933 amrex::Print() << print_ident <<
"MLMG: Failing to converge after " << iter+1 <<
" iterations."
934 <<
" resid, resid/" << norm_name <<
" = "
935 << composite_norminf <<
", "
936 << composite_norminf/max_norm <<
"\n";
939 if ( throw_exception ) {
940 throw error(
"MLMG blew up.");
948 if (!converged && do_fixed_number_of_iters == 0) {
950 amrex::Print() << print_ident <<
"MLMG: Failed to converge after " << max_iters <<
" iterations."
951 <<
" resid, resid/" << norm_name <<
" = "
952 << composite_norminf <<
", "
953 << composite_norminf/max_norm <<
"\n";
956 if ( throw_exception ) {
957 throw error(
"MLMG failed to converge.");
968 if (linop.hasHiddenDimension()) {
969 ng_back[linop.hiddenDirection()] = 0;
971 for (
int alev = 0; alev < namrlevs; ++alev)
973 if (!sol_is_alias[alev]) {
974 LocalCopy(*a_sol[alev], sol[alev], 0, 0, ncomp, ng_back);
980 ParallelReduce::Max<double>(timer.data(), timer.size(), 0,
984 amrex::AllPrint() << print_ident <<
"MLMG: Timers: Solve = " << timer[solve_time]
985 <<
" Iter = " << timer[iter_time]
986 <<
" Bottom = " << timer[bottom_time] <<
"\n";
992 if (do_no_sync_gpu) {
997 return composite_norminf;
1000template <
typename MF>
1003 RT a_tol_rel,
RT a_tol_abs) ->
RT
1005 precond_mode =
true;
1006 std::swap(max_precond_iters, do_fixed_number_of_iters);
1007 linop.beginPrecondBC();
1009 auto r = solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
1011 linop.endPrecondBC();
1012 std::swap(max_precond_iters, do_fixed_number_of_iters);
1013 precond_mode =
false;
1018template <
typename MF>
1022 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1023 const MF* crse_bcdata = (alev > 0) ? a_sol[alev-1] :
nullptr;
1024 linop.prepareForFluxes(alev, crse_bcdata);
1028template <
typename MF>
1029template <
typename AMF>
1034 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1035 if constexpr (std::is_same<AMF,MF>()) {
1036 linop.compGrad(alev, a_grad_sol[alev], sol[alev], a_loc);
1039 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1040 auto const& amf = *(a_grad_sol[alev][idim]);
1043 linop.compGrad(alev,
GetArrOfPtrs(grad_sol), sol[alev], a_loc);
1044 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1045 LocalCopy(*a_grad_sol[alev][idim], grad_sol[idim], 0, 0, ncomp,
IntVect(0));
1051template <
typename MF>
1052template <
typename AMF>
1059template <
typename MF>
1060template <
typename AMF>
1065 if (!linop.isCellCentered()) {
1066 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1071 if constexpr (std::is_same<AMF,MF>()) {
1075 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1076 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1077 auto const& amf = *(a_flux[ilev][idim]);
1082 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1083 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1084 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp,
IntVect(0));
1090template <
typename MF>
1091template <
typename AMF>
1099template <
typename MF>
1100template <
typename AMF>
1107 if (!linop.isCellCentered()) {
1108 amrex::Abort(
"Calling wrong getFluxes for nodal solver");
1111 if constexpr (std::is_same<AMF,MF>()) {
1112 linop.getFluxes(a_flux, a_sol, a_loc);
1115 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1116 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1117 auto const& amf = *(a_flux[ilev][idim]);
1123 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1124 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1125 LocalCopy(*a_flux[ilev][idim], fluxes[ilev][idim], 0, 0, ncomp,
IntVect(0));
1131template <
typename MF>
1132template <
typename AMF>
1135 std::initializer_list<AMF*> a_sol,
Location a_loc)
1141template <
typename MF>
1142template <
typename AMF>
1147 if constexpr (std::is_same<AMF,MF>()) {
1151 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1152 auto const& amf = *a_flux[ilev];
1156 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1162template <
typename MF>
1163template <
typename AMF>
1170template <
typename MF>
1171template <
typename AMF>
1178 if constexpr (! std::is_same<AMF,MF>()) {
1179 for (
int alev = 0; alev < namrlevs; ++alev) {
1184 if (linop.isCellCentered())
1187 for (
int alev = 0; alev < namrlevs; ++alev) {
1188 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
1189 const int mglev = 0;
1191 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1192 ffluxes[alev][idim].define(
amrex::convert(linop.m_grids[alev][mglev],
1194 linop.m_dmap[alev][mglev], ncomp, nghost,
MFInfo(),
1195 *linop.m_factory[alev][mglev]);
1198 if constexpr (std::is_same<AMF,MF>()) {
1203 for (
int alev = 0; alev < namrlevs; ++alev) {
1212 if constexpr (std::is_same<AMF,MF>()) {
1213 linop.getFluxes(a_flux, a_sol);
1216 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1217 auto const& amf = *a_flux[ilev];
1221 for (
int ilev = 0; ilev < namrlevs; ++ilev) {
1228template <
typename MF>
1229template <
typename AMF>
1232 std::initializer_list<AMF*> a_sol,
Location a_loc)
1239template <
typename MF>
1243 if (!linop.isCellCentered()) {
1251template <
typename MF>
1257 if (!linop.isCellCentered()) {
1261 linop.getEBFluxes(a_eb_flux, a_sol);
1265template <
typename MF>
1273 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1276 sol_is_alias.resize(namrlevs,
true);
1277 for (
int alev = 0; alev < namrlevs; ++alev)
1279 if (cf_strategy == CFStrategy::ghostnodes ||
nGrowVect(*a_sol[alev]) == ng_sol)
1281 sol[alev] = linop.makeAlias(*a_sol[alev]);
1282 sol_is_alias[alev] =
true;
1286 if (sol_is_alias[alev])
1288 sol[alev] = linop.make(alev, 0, ng_sol);
1297 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1298 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
1299 const MF* prhs = a_rhs[alev];
1300#if (AMREX_SPACEDIM != 3)
1301 int nghost = (cf_strategy == CFStrategy::ghostnodes) ? linop.getNGrow(alev) : 0;
1303 MFInfo(), *linop.Factory(alev));
1305 linop.applyMetricTerm(alev, 0, rhstmp);
1306 linop.unimposeNeumannBC(alev, rhstmp);
1307 linop.applyInhomogNeumannTerm(alev, rhstmp);
1310 linop.solutionResidual(alev, *a_res[alev], sol[alev], *prhs, crse_bcdata);
1311 if (alev < finest_amr_lev) {
1312 linop.reflux(alev, *a_res[alev], sol[alev], *prhs,
1313 *a_res[alev+1], sol[alev+1], *a_rhs[alev+1]);
1314 if (linop.isCellCentered()) {
1316 EB_average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1318 average_down(*a_res[alev+1], *a_res[alev], 0, ncomp, linop.AMRRefRatioVect(alev));
1325#if (AMREX_SPACEDIM != 3)
1326 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1327 linop.unapplyMetricTerm(alev, 0, *a_res[alev]);
1332template <
typename MF>
1343 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1345 for (
int alev = 0; alev < namrlevs; ++alev)
1347 if (cf_strategy == CFStrategy::ghostnodes)
1349 nghost = linop.getNGrow(alev);
1350 in[alev] = a_in[alev];
1352 else if (
nGrowVect(*a_in[alev]) == ng_sol)
1354 in[alev] = a_in[alev];
1359 if (cf_strategy == CFStrategy::ghostnodes) { ng =
IntVect(nghost); }
1360 in_raii[alev] = linop.make(alev, 0, ng);
1362 in[alev] = &(in_raii[alev]);
1364 rh[alev] = linop.make(alev, 0,
IntVect(nghost));
1370 for (
int alev = 0; alev < namrlevs; ++alev) {
1371 linop.applyInhomogNeumannTerm(alev, rh[alev]);
1375 for (
int alev = finest_amr_lev; alev >= 0; --alev) {
1376 const MF* crse_bcdata = (alev > 0) ? in[alev-1] :
nullptr;
1377 linop.solutionResidual(alev, *out[alev], *in[alev], rh[alev], crse_bcdata);
1378 if (alev < finest_amr_lev) {
1379 linop.reflux(alev, *out[alev], *in[alev], rh[alev],
1380 *out[alev+1], *in[alev+1], rh[alev+1]);
1381 if (linop.isCellCentered()) {
1382 if constexpr (IsMultiFabLike_v<MF>) {
1386 average_down(*out[alev+1], *out[alev], 0,
nComp(*out[alev]), linop.AMRRefRatioVect(alev));
1389 amrex::Abort(
"MLMG: TODO average_down for non-MultiFab");
1395#if (AMREX_SPACEDIM != 3)
1396 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1397 linop.unapplyMetricTerm(alev, 0, *out[alev]);
1401 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
1402 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(alev); }
1403 Scale(*out[alev],
RT(-1), 0,
nComp(*out[alev]), nghost);
1407template <
typename MF>
1411 precond_mode =
true;
1412 linop.beginPrecondBC();
1414 linop.endPrecondBC();
1415 precond_mode =
false;
1418template <
typename MF>
1419template <
typename AMF>
1428 timer.assign(ntimers, 0.0);
1432 if (linop.hasHiddenDimension()) { ng_sol[linop.hiddenDirection()] = 0; }
1434 if (!linop_prepared) {
1435 linop.prepareForSolve();
1436 linop_prepared =
true;
1437 }
else if (linop.needsUpdate()) {
1440#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1441 hypre_solver.reset();
1442 hypre_bndry.reset();
1443 hypre_node_solver.reset();
1446#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1447 petsc_solver.reset();
1448 petsc_bndry.reset();
1453 sol_is_alias.resize(namrlevs,
false);
1454 for (
int alev = 0; alev < namrlevs; ++alev)
1456 if (cf_strategy == CFStrategy::ghostnodes)
1458 if constexpr (std::is_same<AMF,MF>()) {
1459 sol[alev] = linop.makeAlias(*a_sol[alev]);
1460 sol_is_alias[alev] =
true;
1462 amrex::Abort(
"Type conversion not supported for CFStrategy::ghostnodes");
1467 if (
nGrowVect(*a_sol[alev]) == ng_sol) {
1468 if constexpr (std::is_same<AMF,MF>()) {
1469 sol[alev] = linop.makeAlias(*a_sol[alev]);
1470 sol_is_alias[alev] =
true;
1473 if (!sol_is_alias[alev]) {
1474 if (!solve_called) {
1475 sol[alev] = linop.make(alev, 0, ng_sol);
1483 rhs.resize(namrlevs);
1484 for (
int alev = 0; alev < namrlevs; ++alev)
1486 if (cf_strategy == CFStrategy::ghostnodes) { ng_rhs =
IntVect(linop.getNGrow(alev)); }
1487 if (!solve_called) {
1488 rhs[alev] = linop.make(alev, 0, ng_rhs);
1490 LocalCopy(rhs[alev], *a_rhs[alev], 0, 0, ncomp, ng_rhs);
1491 linop.applyMetricTerm(alev, 0, rhs[alev]);
1492 linop.unimposeNeumannBC(alev, rhs[alev]);
1493 linop.applyInhomogNeumannTerm(alev, rhs[alev]);
1494 linop.applyOverset(alev, rhs[alev]);
1495 if ( ! precond_mode) {
1496 bool r = linop.scaleRHS(alev, &(rhs[alev]));
1502 if (factory && !factory->isAllRegular()) {
1503 if constexpr (std::is_same<MF,MultiFab>()) {
1507 amrex::Abort(
"TODO: MLMG with EB only works with MultiFab");
1513 for (
int falev = finest_amr_lev; falev > 0; --falev)
1515 linop.averageDownSolutionRHS(falev-1, sol[falev-1], rhs[falev-1], sol[falev], rhs[falev]);
1519 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1524 IntVect ng = linop.getNGrowVectRestriction();
1525 if (cf_strategy == CFStrategy::ghostnodes) { ng = ng_rhs; }
1526 if (!solve_called) {
1527 linop.make(res, ng);
1528 linop.make(rescor, ng);
1530 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1532 const int nmglevs = linop.NMGLevels(alev);
1533 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1535 setVal(res [alev][mglev],
RT(0.0));
1536 setVal(rescor[alev][mglev],
RT(0.0));
1540 if (cf_strategy != CFStrategy::ghostnodes) { ng = ng_sol; }
1542 for (
int alev = 0; alev <= finest_amr_lev; ++alev)
1544 const int nmglevs = linop.NMGLevels(alev);
1545 cor[alev].resize(nmglevs);
1546 for (
int mglev = 0; mglev < nmglevs; ++mglev)
1548 if (!solve_called) {
1550 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev,mglev)); }
1551 cor[alev][mglev] = linop.make(alev, mglev, _ng);
1557 cor_hold.resize(std::max(namrlevs-1,1));
1560 const int nmglevs = linop.NMGLevels(alev);
1561 cor_hold[alev].resize(nmglevs);
1562 for (
int mglev = 0; mglev < nmglevs-1; ++mglev)
1564 if (!solve_called) {
1566 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev,mglev)); }
1567 cor_hold[alev][mglev] = linop.make(alev, mglev, _ng);
1569 setVal(cor_hold[alev][mglev],
RT(0.0));
1572 for (
int alev = 1; alev < finest_amr_lev; ++alev)
1574 cor_hold[alev].resize(1);
1575 if (!solve_called) {
1577 if (cf_strategy == CFStrategy::ghostnodes) { _ng=
IntVect(linop.getNGrow(alev)); }
1578 cor_hold[alev][0] = linop.make(alev, 0, _ng);
1580 setVal(cor_hold[alev][0],
RT(0.0));
1584 || !linop.supportNSolve())
1589 if (do_nsolve && ns_linop ==
nullptr)
1595 amrex::Print() << print_ident <<
"MLMG: # of AMR levels: " << namrlevs <<
"\n"
1596 << print_ident <<
" # of MG levels on the coarsest AMR level: " << linop.NMGLevels(0)
1599 amrex::Print() << print_ident <<
" # of MG levels in N-Solve: " << ns_linop->NMGLevels(0) <<
"\n"
1600 << print_ident <<
" # of grids in N-Solve: " << ns_linop->m_grids[0][0].size() <<
"\n";
1605template <
typename MF>
1609 if (!linop_prepared) {
1610 linop.prepareForSolve();
1611 linop_prepared =
true;
1612 }
else if (linop.needsUpdate()) {
1615#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1616 hypre_solver.reset();
1617 hypre_bndry.reset();
1618 hypre_node_solver.reset();
1621#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1622 petsc_solver.reset();
1623 petsc_bndry.reset();
1628template <
typename MF>
1633 linop.preparePrecond();
1636template <
typename MF>
1640 if constexpr (IsMultiFabLike_v<MF>) {
1641 ns_linop = linop.makeNLinOp(nsolve_grid_size);
1644 if (cf_strategy == CFStrategy::ghostnodes) { nghost = linop.getNGrow(); }
1646 const BoxArray& ba = (*ns_linop).m_grids[0][0];
1650 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1651 ns_sol = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1653 if (cf_strategy == CFStrategy::ghostnodes) { ng = nghost; }
1654 ns_rhs = std::make_unique<MF>(ba, dm, ncomp, ng,
MFInfo(), *(ns_linop->Factory(0,0)));
1658 ns_linop->setLevelBC(0, ns_sol.get());
1660 ns_mlmg = std::make_unique<MLMGT<MF>>(*ns_linop);
1661 ns_mlmg->setVerbose(0);
1662 ns_mlmg->setFixedIter(1);
1663 ns_mlmg->setMaxFmgIter(20);
1670template <
typename MF>
1675 for (
int alev = finest_amr_lev; alev > 0; --alev)
1680 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(alev)); }
1681 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1684 computeResWithCrseSolFineCor(alev-1,alev);
1686 if (alev != finest_amr_lev) {
1687 std::swap(cor_hold[alev][0], cor[alev][0]);
1694 if (linop.isSingular(0) && linop.getEnforceSingularSolvable())
1696 makeSolvable(0,0,res[0][0]);
1699 if (iter < max_fmg_iters) {
1706 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(0)); }
1707 LocalAdd(sol[0], cor[0][0], 0, 0, ncomp, nghost);
1710 for (
int alev = 1; alev <= finest_amr_lev; ++alev)
1713 interpCorrection(alev);
1716 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(alev)); }
1717 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1719 if (alev != finest_amr_lev) {
1720 LocalAdd(cor_hold[alev][0], cor[alev][0], 0, 0, ncomp, nghost);
1724 computeResWithCrseCorFineCor(alev);
1728 LocalAdd(sol[alev], cor[alev][0], 0, 0, ncomp, nghost);
1730 if (alev != finest_amr_lev) {
1731 LocalAdd(cor[alev][0], cor_hold[alev][0], 0, 0, ncomp, nghost);
1735 linop.averageDownAndSync(sol);
1738template <
typename MF>
1743 const int mglev = 0;
1744 mgVcycle(amrlev, mglev);
1749template <
typename MF>
1755 const int mglev_bottom = linop.NMGLevels(amrlev) - 1;
1757 for (
int mglev = mglev_top; mglev < mglev_bottom; ++mglev)
1759 BL_PROFILE_VAR(
"MLMG::mgVcycle_down::"+std::to_string(mglev), blp_mgv_down_lev);
1764 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1765 <<
" DN: Norm before smooth " <<
norm <<
"\n";
1768 setVal(cor[amrlev][mglev],
RT(0.0));
1769 bool skip_fillboundary =
true;
1770 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev], skip_fillboundary, nu1);
1773 computeResOfCorrection(amrlev, mglev);
1778 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1779 <<
" DN: Norm after smooth " <<
norm <<
"\n";
1783 linop.restriction(amrlev, mglev+1, res[amrlev][mglev+1], rescor[amrlev][mglev]);
1792 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1793 <<
" DN: Norm before bottom " <<
norm <<
"\n";
1798 computeResOfCorrection(amrlev, mglev_bottom);
1800 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1801 <<
" UP: Norm after bottom " <<
norm <<
"\n";
1809 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1810 <<
" Norm before smooth " <<
norm <<
"\n";
1812 setVal(cor[amrlev][mglev_bottom],
RT(0.0));
1813 bool skip_fillboundary =
true;
1814 linop.smooth(amrlev, mglev_bottom, cor[amrlev][mglev_bottom],
1815 res[amrlev][mglev_bottom], skip_fillboundary, nu1);
1818 computeResOfCorrection(amrlev, mglev_bottom);
1820 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev_bottom
1821 <<
" Norm after smooth " <<
norm <<
"\n";
1826 for (
int mglev = mglev_bottom-1; mglev >= mglev_top; --mglev)
1828 BL_PROFILE_VAR(
"MLMG::mgVcycle_up::"+std::to_string(mglev), blp_mgv_up_lev);
1830 addInterpCorrection(amrlev, mglev);
1833 computeResOfCorrection(amrlev, mglev);
1835 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1836 <<
" UP: Norm before smooth " <<
norm <<
"\n";
1838 linop.smooth(amrlev, mglev, cor[amrlev][mglev], res[amrlev][mglev],
false, nu2);
1840 if (cf_strategy == CFStrategy::ghostnodes) { computeResOfCorrection(amrlev, mglev); }
1844 computeResOfCorrection(amrlev, mglev);
1846 amrex::Print() << print_ident <<
"AT LEVEL " << amrlev <<
" " << mglev
1847 <<
" UP: Norm after smooth " <<
norm <<
"\n";
1855template <
typename MF>
1862 auto* pf = linop.Factory(0);
1863 auto is_all_regular = [pf] () {
1872 AMREX_ASSERT(linop.isCellCentered() || is_all_regular());
1875 const int amrlev = 0;
1876 const int mg_bottom_lev = linop.NMGLevels(amrlev) - 1;
1878 if (cf_strategy == CFStrategy::ghostnodes) { nghost =
IntVect(linop.getNGrow(amrlev)); }
1880 for (
int mglev = 1; mglev <= mg_bottom_lev; ++mglev)
1882 linop.avgDownResMG(mglev, res[amrlev][mglev], res[amrlev][mglev-1]);
1887 for (
int mglev = mg_bottom_lev-1; mglev >= 0; --mglev)
1890 interpCorrection(amrlev, mglev);
1893 computeResOfCorrection(amrlev, mglev);
1895 LocalCopy(res[amrlev][mglev], rescor[amrlev][mglev], 0, 0, ncomp, nghost);
1898 std::swap(cor[amrlev][mglev], cor_hold[amrlev][mglev]);
1899 mgVcycle(amrlev, mglev);
1900 LocalAdd(cor[amrlev][mglev], cor_hold[amrlev][mglev], 0, 0, ncomp, nghost);
1907template <
typename MF>
1913 NSolve(*ns_mlmg, *ns_sol, *ns_rhs);
1917 actualBottomSolve();
1921template <
typename MF>
1929 MF
const& res_bottom = res[0].back();
1940 RT(-1.0),
RT(-1.0));
1942 linop.copyNSolveSolution(cor[0].back(), a_sol);
1945template <
typename MF>
1951 if (!linop.isBottomActive()) {
return; }
1957 const int amrlev = 0;
1958 const int mglev = linop.NMGLevels(amrlev) - 1;
1959 auto&
x = cor[amrlev][mglev];
1960 auto& b = res[amrlev][mglev];
1966 bool skip_fillboundary =
true;
1967 linop.smooth(amrlev, mglev,
x, b, skip_fillboundary, nuf);
1973 if (linop.isBottomSingular() && linop.getEnforceSingularSolvable())
1976 raii_b = linop.make(amrlev, mglev, ng);
1980 makeSolvable(amrlev,mglev,*bottom_b);
1985#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
1986 if constexpr (std::is_same<MF,MultiFab>()) {
1987 bottomSolveWithHypre(
x, *bottom_b);
1991 amrex::Abort(
"Using Hypre as bottom solver not supported in this case");
1996#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
1997 if constexpr (std::is_same<MF,MultiFab>()) {
1998 bottomSolveWithPETSc(
x, *bottom_b);
2002 amrex::Abort(
"Using PETSc as bottom solver not supported in this case");
2015 int ret = bottomSolveWithCG(
x, *bottom_b, cg_type);
2025 setVal(cor[amrlev][mglev],
RT(0.0));
2026 ret = bottomSolveWithCG(
x, *bottom_b, cg_type);
2037 if (ret != 0 && ret != 9) {
2038 setVal(cor[amrlev][mglev],
RT(0.0));
2040 const int n = (ret==0) ? nub : nuf;
2041 linop.smooth(amrlev, mglev,
x, b,
false, n);
2047 if (! timer.empty()) {
2052template <
typename MF>
2062 if (cf_strategy == CFStrategy::ghostnodes) { cg_solver.
setNGhost(linop.getNGrow()); }
2064 int ret = cg_solver.
solve(
x, b, bottom_reltol, bottom_abstol);
2065 if (ret != 0 && verbose > 1) {
2066 amrex::Print() << print_ident <<
"MLMG: Bottom solve failed.\n";
2073template <
typename MF>
2079 const int mglev = 0;
2080 for (
int alev = amrlevmax; alev >= 0; --alev) {
2081 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
2082 linop.solutionResidual(alev, res[alev][mglev], sol[alev], rhs[alev], crse_bcdata);
2083 if (alev < finest_amr_lev) {
2084 linop.reflux(alev, res[alev][mglev], sol[alev], rhs[alev],
2085 res[alev+1][mglev], sol[alev+1], rhs[alev+1]);
2091template <
typename MF>
2096 const MF* crse_bcdata = (alev > 0) ? &(sol[alev-1]) :
nullptr;
2097 linop.solutionResidual(alev, res[alev][0], sol[alev], rhs[alev], crse_bcdata);
2101template <
typename MF>
2105 BL_PROFILE(
"MLMG::computeResWithCrseSolFineCor()");
2108 if (cf_strategy == CFStrategy::ghostnodes) {
2109 nghost =
IntVect(std::min(linop.getNGrow(falev),linop.getNGrow(calev)));
2112 MF& crse_sol = sol[calev];
2113 const MF& crse_rhs = rhs[calev];
2114 MF& crse_res = res[calev][0];
2116 MF& fine_sol = sol[falev];
2117 const MF& fine_rhs = rhs[falev];
2118 MF& fine_cor = cor[falev][0];
2119 MF& fine_res = res[falev][0];
2120 MF& fine_rescor = rescor[falev][0];
2122 const MF* crse_bcdata = (calev > 0) ? &(sol[calev-1]) :
nullptr;
2123 linop.solutionResidual(calev, crse_res, crse_sol, crse_rhs, crse_bcdata);
2125 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res, BCMode::Homogeneous);
2126 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2128 linop.reflux(calev, crse_res, crse_sol, crse_rhs, fine_res, fine_sol, fine_rhs);
2130 linop.avgDownResAmr(calev, crse_res, fine_res);
2134template <
typename MF>
2138 BL_PROFILE(
"MLMG::computeResWithCrseCorFineCor()");
2141 if (cf_strategy == CFStrategy::ghostnodes) {
2142 nghost =
IntVect(linop.getNGrow(falev));
2145 const MF& crse_cor = cor[falev-1][0];
2147 MF& fine_cor = cor [falev][0];
2148 MF& fine_res = res [falev][0];
2149 MF& fine_rescor = rescor[falev][0];
2152 linop.correctionResidual(falev, 0, fine_rescor, fine_cor, fine_res,
2153 BCMode::Inhomogeneous, &crse_cor);
2154 LocalCopy(fine_res, fine_rescor, 0, 0, ncomp, nghost);
2158template <
typename MF>
2165 if (cf_strategy == CFStrategy::ghostnodes) {
2166 nghost =
IntVect(linop.getNGrow(alev));
2169 MF & crse_cor = cor[alev-1][0];
2170 MF & fine_cor = cor[alev ][0];
2172 const Geometry& crse_geom = linop.Geom(alev-1,0);
2175 int ng_dst = linop.isCellCentered() ? 1 : 0;
2176 if (cf_strategy == CFStrategy::ghostnodes)
2178 ng_src = linop.getNGrow(alev-1);
2179 ng_dst = linop.getNGrow(alev-1);
2180 if constexpr (IsMultiFabLike_v<MF>) {
2183 amrex::Abort(
"MLMG: CFStrategy::ghostnodes not supported for non-MultiFab like types");
2187 MF cfine = linop.makeCoarseAmr(alev,
IntVect(ng_dst));
2192 linop.interpolationAmr(alev, fine_cor, cfine, nghost);
2198template <
typename MF>
2204 MF& crse_cor = cor[alev][mglev+1];
2205 MF& fine_cor = cor[alev][mglev ];
2206 linop.interpAssign(alev, mglev, fine_cor, crse_cor);
2210template <
typename MF>
2216 const MF& crse_cor = cor[alev][mglev+1];
2217 MF& fine_cor = cor[alev][mglev ];
2222 if (linop.isMFIterSafe(alev, mglev, mglev+1))
2228 cfine = linop.makeCoarseMG(alev, mglev,
IntVect(0));
2233 linop.interpolation(alev, mglev, fine_cor, *cmf);
2240template <
typename MF>
2245 MF &
x = cor[amrlev][mglev];
2246 const MF& b = res[amrlev][mglev];
2247 MF & r = rescor[amrlev][mglev];
2248 linop.correctionResidual(amrlev, mglev, r,
x, b, BCMode::Homogeneous);
2252template <
typename MF>
2257 return linop.normInf(alev, res[alev][0], local);
2261template <
typename MF>
2267 for (
int alev = 0; alev <= alevmax; ++alev)
2269 r = std::max(r, ResNormInf(alev,
true));
2276template <
typename MF>
2282 for (
int alev = 0; alev <= finest_amr_lev; ++alev) {
2283 auto t = linop.normInf(alev, rhs[alev],
true);
2290template <
typename MF>
2294 auto const&
offset = linop.getSolvabilityOffset(0, 0, rhs[0]);
2296 for (
int c = 0; c < ncomp; ++c) {
2297 amrex::Print() << print_ident <<
"MLMG: Subtracting " <<
offset[c] <<
" from rhs component "
2301 for (
int alev = 0; alev < namrlevs; ++alev) {
2302 linop.fixSolvabilityByOffset(alev, 0, rhs[alev],
offset);
2306template <
typename MF>
2310 auto const&
offset = linop.getSolvabilityOffset(amrlev, mglev, mf);
2312 for (
int c = 0; c < ncomp; ++c) {
2314 <<
" from mf component c = " << c
2315 <<
" on level (" << amrlev <<
", " << mglev <<
")\n";
2318 linop.fixSolvabilityByOffset(amrlev, mglev, mf,
offset);
2321#if defined(AMREX_USE_HYPRE) && (AMREX_SPACEDIM > 1)
2322template <
typename MF>
2324requires (std::same_as<TMF,MultiFab>)
2328 const int amrlev = 0;
2329 const int mglev = linop.NMGLevels(amrlev) - 1;
2333 if (linop.isCellCentered())
2335 if (hypre_solver ==
nullptr)
2337 hypre_solver = linop.makeHypre(hypre_interface);
2339 hypre_solver->setVerbose(bottom_verbose);
2341 hypre_solver->setHypreOptionsNamespace(hypre_options_namespace);
2343 hypre_solver->setHypreOldDefault(hypre_old_default);
2344 hypre_solver->setHypreRelaxType(hypre_relax_type);
2345 hypre_solver->setHypreRelaxOrder(hypre_relax_order);
2346 hypre_solver->setHypreNumSweeps(hypre_num_sweeps);
2347 hypre_solver->setHypreStrongThreshold(hypre_strong_threshold);
2350 const BoxArray& ba = linop.m_grids[amrlev].back();
2351 const DistributionMapping& dm = linop.m_dmap[amrlev].back();
2352 const Geometry& geom = linop.m_geom[amrlev].back();
2354 hypre_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
2355 hypre_bndry->setHomogValues();
2356 const Real* dx = linop.m_geom[0][0].CellSize();
2357 IntVect crse_ratio = linop.m_coarse_data_crse_ratio.
allGT(0) ? linop.m_coarse_data_crse_ratio :
IntVect(1);
2359 0.5*dx[1]*crse_ratio[1],
2360 0.5*dx[2]*crse_ratio[2]));
2361 hypre_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc,
IntVect(-1), bclocation,
2362 linop.m_coarse_fine_bc_type);
2368 ? bottom_abstol :
Real(-1.0);
2369 hypre_solver->solve(
2370 x, b, bottom_reltol, hypre_abstol, bottom_maxiter, *hypre_bndry,
2371 linop.getMaxOrder());
2375 if (hypre_node_solver ==
nullptr)
2378 linop.makeHypreNodeLap(bottom_verbose, hypre_options_namespace);
2380 hypre_node_solver->solve(
x, b, bottom_reltol, bottom_abstol, bottom_maxiter);
2385 if (linop.isSingular(amrlev) && linop.getEnforceSingularSolvable())
2387 makeSolvable(amrlev, mglev,
x);
2392#if defined(AMREX_USE_PETSC) && (AMREX_SPACEDIM > 1)
2393template <
typename MF>
2395requires (std::same_as<TMF,MultiFab>)
2397MLMGT<MF>::bottomSolveWithPETSc (MF&
x,
const MF& b)
2401 if(petsc_solver ==
nullptr)
2403 petsc_solver = linop.makePETSc();
2404 petsc_solver->setVerbose(bottom_verbose);
2406 const BoxArray& ba = linop.m_grids[0].back();
2407 const DistributionMapping& dm = linop.m_dmap[0].back();
2408 const Geometry& geom = linop.m_geom[0].back();
2410 petsc_bndry = std::make_unique<MLMGBndryT<MF>>(ba, dm, ncomp, geom);
2411 petsc_bndry->setHomogValues();
2412 const Real* dx = linop.m_geom[0][0].CellSize();
2413 auto crse_ratio = linop.m_coarse_data_crse_ratio.allGT(0) ? linop.m_coarse_data_crse_ratio :
IntVect(1);
2415 0.5*dx[1]*crse_ratio[1],
2416 0.5*dx[2]*crse_ratio[2]));
2417 petsc_bndry->setLOBndryConds(linop.m_lobc, linop.m_hibc,
IntVect(-1), bclocation,
2418 linop.m_coarse_fine_bc_type);
2420 petsc_solver->solve(
x, b, bottom_reltol,
Real(-1.), bottom_maxiter, *petsc_bndry,
2421 linop.getMaxOrder());
2425template <
typename MF>
2427MLMGT<MF>::checkPoint (
const Vector<MultiFab*>& a_sol,
2428 const Vector<MultiFab const*>& a_rhs,
2429 RT a_tol_rel, RT a_tol_abs,
const char* a_file_name)
const
2431 std::string file_name(a_file_name);
2436 std::string HeaderFileName(std::string(a_file_name)+
"/Header");
2437 std::ofstream HeaderFile;
2438 HeaderFile.open(HeaderFileName.c_str(), std::ofstream::out |
2439 std::ofstream::trunc |
2440 std::ofstream::binary);
2441 if( ! HeaderFile.good()) {
2445 HeaderFile.precision(17);
2449 HeaderFile << linop.name() <<
"\n"
2450 <<
"a_tol_rel = " << a_tol_rel <<
"\n"
2451 <<
"a_tol_abs = " << a_tol_abs <<
"\n"
2452 <<
"verbose = " <<
verbose <<
"\n"
2453 <<
"max_iters = " << max_iters <<
"\n"
2454 <<
"nu1 = " << nu1 <<
"\n"
2455 <<
"nu2 = " << nu2 <<
"\n"
2456 <<
"nuf = " << nuf <<
"\n"
2457 <<
"nub = " << nub <<
"\n"
2458 <<
"max_fmg_iters = " << max_fmg_iters <<
"\n"
2459 <<
"bottom_solver = " <<
static_cast<int>(bottom_solver) <<
"\n"
2460 <<
"bottom_verbose = " << bottom_verbose <<
"\n"
2461 <<
"bottom_maxiter = " << bottom_maxiter <<
"\n"
2462 <<
"bottom_reltol = " << bottom_reltol <<
"\n"
2463 <<
"convergence_norm = " << norm_name <<
"\n"
2464 <<
"namrlevs = " << namrlevs <<
"\n"
2465 <<
"finest_amr_lev = " << finest_amr_lev <<
"\n"
2466 <<
"linop_prepared = " << linop_prepared <<
"\n"
2467 <<
"solve_called = " << solve_called <<
"\n";
2469 for (
int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
2476 for (
int ilev = 0; ilev <= finest_amr_lev; ++ilev) {
2477 VisMF::Write(*a_sol[ilev], file_name+
"/Level_"+std::to_string(ilev)+
"/sol");
2478 VisMF::Write(*a_rhs[ilev], file_name+
"/Level_"+std::to_string(ilev)+
"/rhs");
2481 linop.checkPoint(file_name+
"/linop");
2484template <
typename MF>
2488 print_ident.resize(print_ident.size()+4,
' ');
2491template <
typename MF>
2495 if (print_ident.size() > 4) {
2496 print_ident.resize(print_ident.size()-4,
' ');
2498 print_ident.clear();
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define BL_PROFILE_VAR_STOP(vname)
Definition AMReX_BLProfiler.H:563
#define BL_PROFILE_VAR(fname, vname)
Definition AMReX_BLProfiler.H:560
#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:1129
#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:676
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition AMReX_BoxArray.H:1235
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:148
Solve using GMRES with multigrid as preconditioner.
Definition AMReX_GMRES_MLMG.H:28
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:75
Periodicity periodicity() const noexcept
Definition AMReX_Geometry.H:361
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:29
Definition AMReX_MLMG.H:24
void prepareForFluxes(Vector< MF const * > const &a_sol)
Build boundary caches needed by getFluxes()/compFluxes.
Definition AMReX_MLMG.H:1020
void setBottomVerbose(int v) noexcept
Verbosity for the bottom solver (0 silent).
Definition AMReX_MLMG.H:337
void setMaxFmgIter(int n) noexcept
Cap the number of FMG cycles executed.
Definition AMReX_MLMG.H:278
RT MLResNormInf(int alevmax, bool local=false)
Composite infinity norm of the residual up to level alevmax.
Definition AMReX_MLMG.H:2263
RT MLRhsNormInf(bool local=false)
Composite infinity norm of the RHS.
Definition AMReX_MLMG.H:2278
void setNoGpuSync(bool do_not_sync) noexcept
Disable global GPU syncs before returning to the app.
Definition AMReX_MLMG.H:400
MLMGT(MLMGT< MF > &&)=delete
void actualBottomSolve()
Execute the actual bottom solve after pre-smoothing and restriction.
Definition AMReX_MLMG.H:1947
MF MFType
Definition AMReX_MLMG.H:37
BottomSolver getBottomSolver() const noexcept
Definition AMReX_MLMG.H:325
void setPreSmooth(int n) noexcept
Number of pre-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:299
void setBottomToleranceAbs(RT t) noexcept
Absolute tolerance for the bottom solver.
Definition AMReX_MLMG.H:355
RT getFinalResidual() const noexcept
Definition AMReX_MLMG.H:627
void interpCorrection(int alev)
Interpolate corrections onto AMR level alev.
Definition AMReX_MLMG.H:2160
void getEBFluxes(const Vector< MF * > &a_eb_flux)
Flux into the EB wall using the internally stored solution.
Definition AMReX_MLMG.H:1241
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:1031
void setBottomSmooth(int n) noexcept
Additional smoothing passes executed after the bottom solver.
Definition AMReX_MLMG.H:317
void setNSolve(int flag) noexcept
Enable (flag!=0) or disable the N-solve path.
Definition AMReX_MLMG.H:387
int getBottomVerbose() const
Definition AMReX_MLMG.H:246
void computeResOfCorrection(int amrlev, int mglev)
Compute the residual of the correction equation on (amrlev,mglev).
Definition AMReX_MLMG.H:2242
void applyPrecond(const Vector< MF * > &out, const Vector< MF * > &in)
Apply the linear operator as a preconditioner (out = L(in)).
Definition AMReX_MLMG.H:1409
void setCFStrategy(CFStrategy a_cf_strategy) noexcept
Select the coarse-fine synchronization strategy.
Definition AMReX_MLMG.H:331
void computeResWithCrseCorFineCor(int falev)
Residual update using coarse correction / fine correction.
Definition AMReX_MLMG.H:2136
void NSolve(MLMGT< MF > &a_solver, MF &a_sol, MF &a_rhs)
Perform an NSolve using an MLMGT wrapper.
Definition AMReX_MLMG.H:1923
typename MLLinOpT< MF >::Location Location
Definition AMReX_MLMG.H:42
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:1334
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:1062
void setNSolveGridSize(int s) noexcept
Set the tile size used for N-solve builds.
Definition AMReX_MLMG.H:393
void setVerbose(int v) noexcept
Set the main solver verbosity (0 silent).
Definition AMReX_MLMG.H:266
void computeMLResidual(int amrlevmax)
Compute the composite residual norm up to AMR level amrlevmax.
Definition AMReX_MLMG.H:2075
RT getInitResidual() const noexcept
Definition AMReX_MLMG.H:625
int getNumIters() const noexcept
Definition AMReX_MLMG.H:630
void setPostSmooth(int n) noexcept
Number of post-smoothing passes per V-cycle.
Definition AMReX_MLMG.H:305
void mgVcycle(int amrlev, int mglev)
Run a multigrid V-cycle on (amrlev,mglev).
Definition AMReX_MLMG.H:1751
void prepareForNSolve()
Prepare the NSolve path.
Definition AMReX_MLMG.H:1638
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:1002
void makeSolvable()
Adjust RHS/solution to satisfy null-space constraints.
Definition AMReX_MLMG.H:2292
void setBottomSolver(BottomSolver s) noexcept
Select the bottom solver type (e.g., CG, BiCGStab, Hypre, PETSc).
Definition AMReX_MLMG.H:324
void preparePrecond()
Prepare preconditioner-specific caches (e.g., boundary data).
Definition AMReX_MLMG.H:1630
void incPrintIdentation()
Increase the indentation used when printing solver logs.
Definition AMReX_MLMG.H:2486
typename MLLinOpT< MF >::RT RT
Definition AMReX_MLMG.H:39
void setThrowException(bool t) noexcept
Control behavior when the solve fails to converge or blows up.
Definition AMReX_MLMG.H:260
void decPrintIdentation()
Decrease the indentation used when printing solver logs.
Definition AMReX_MLMG.H:2493
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:285
Vector< RT > const & getResidualHistory() const noexcept
Definition AMReX_MLMG.H:629
void prepareLinOp()
Finalize operator-dependent metadata before iterating.
Definition AMReX_MLMG.H:1607
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:292
CFStrategy
Definition AMReX_MLMG.H:45
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:1421
int bottomSolveWithCG(MF &x, const MF &b, typename MLCGSolverT< MF >::Type type)
Bottom solve using CG/BiCGStab implemented in MLCGSolverT.
Definition AMReX_MLMG.H:2054
void setAlwaysUseBNorm(int flag) noexcept
Deprecated flag for forcing B-norm convergence checks.
Definition AMReX_MLMG.H:761
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:1267
void miniCycle(int amrlev)
Execute a per-level mini cycle.
Definition AMReX_MLMG.H:1740
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:378
typename MLLinOpT< MF >::BCMode BCMode
Definition AMReX_MLMG.H:41
void setConvergenceNormType(MLMGNormType norm) noexcept
Choose the norm used for convergence tests.
Definition AMReX_MLMG.H:371
void computeResWithCrseSolFineCor(int calev, int falev)
Residual update using coarse solution / fine correction.
Definition AMReX_MLMG.H:2103
MLMGT< MF > & operator=(MLMGT< MF > const &)=delete
MLMGT(MLLinOpT< MF > &a_lp)
Definition AMReX_MLMG.H:752
void computeResidual(int alev)
Compute the residual on AMR level alev.
Definition AMReX_MLMG.H:2093
MLLinOpT< MF > & getLinOp()
Definition AMReX_MLMG.H:633
typename MLLinOpT< MF >::FAB FAB
Definition AMReX_MLMG.H:38
RT getBottomToleranceAbs() const noexcept
Definition AMReX_MLMG.H:356
int numAMRLevels() const noexcept
Definition AMReX_MLMG.H:380
MLMGT(MLMGT< MF > const &)=delete
void mgFcycle()
Run an FMG cycle starting from the coarsest grid.
Definition AMReX_MLMG.H:1857
RT getInitRHS() const noexcept
Definition AMReX_MLMG.H:623
RT ResNormInf(int alev, bool local=false)
Infinity norm of the residual on level alev.
Definition AMReX_MLMG.H:2254
Vector< int > const & getNumCGIters() const noexcept
Definition AMReX_MLMG.H:631
void bottomSolve()
Execute the configured bottom solver (Hypre, PETSc, CG, etc.).
Definition AMReX_MLMG.H:1909
void setBottomTolerance(RT t) noexcept
Relative tolerance for the bottom solver.
Definition AMReX_MLMG.H:349
void setFinalSmooth(int n) noexcept
Number of smoothing passes when MLMG is used standalone (final smooth).
Definition AMReX_MLMG.H:311
void addInterpCorrection(int alev, int mglev)
Add interpolated corrections to (alev,mglev) data.
Definition AMReX_MLMG.H:2212
int getVerbose() const
Definition AMReX_MLMG.H:245
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:343
void oneIter(int iter)
Execute a single multigrid iteration (FMG or V-cycle).
Definition AMReX_MLMG.H:1671
void setMaxIter(int n) noexcept
Cap the number of MLMG iterations executed.
Definition AMReX_MLMG.H:272
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:79
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
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:260
std::string getEnumNameString(T const &v)
Get the name string of an enum value.
Definition AMReX_Enum.H:180
bool setNoSyncRegion(bool b) noexcept
Definition AMReX_GpuControl.H:154
bool setSingleStreamRegion(bool b) noexcept
Definition AMReX_GpuControl.H:150
void push(MPI_Comm c)
Definition AMReX_ParallelContext.H:102
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:108
bool IOProcessorSub() noexcept
Am IO processor for current frame?
Definition AMReX_ParallelContext.H:80
int verbose
Definition AMReX.cpp:111
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
int nComp(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2852
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:806
__host__ __device__ T norm(const GpuComplex< T > &a_z) noexcept
Return the norm (magnitude squared) of a complex number.
Definition AMReX_GpuComplex.H:349
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2867
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
IntVect nGrowVect(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2857
void Scale(MF &dst, typename MF::value_type val, int scomp, int ncomp, int nghost)
dst *= val
Definition AMReX_FabArrayUtility.H:1875
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
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:1951
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:37
Vector< T * > GetVecOfPtrs(Vector< T > &a)
Definition AMReX_Vector.H:65
void LocalCopy(DMF &dst, SMF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst = src
Definition AMReX_FabArrayUtility.H:1882
void setBndry(MF &dst, typename MF::value_type val, int scomp, int ncomp)
dst = val in ghost cells.
Definition AMReX_FabArrayUtility.H:1868
MF::value_type norminf(MF const &mf, int scomp, int ncomp, IntVect const &nghost, bool local=false)
Definition AMReX_FabArrayUtility.H:1961
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:241
MLMGNormType
Definition AMReX_MLMG.H:19
void LocalAdd(MF &dst, MF const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
dst += src
Definition AMReX_FabArrayUtility.H:1890
BoxArray const & boxArray(FabArrayBase const &fa)
Definition AMReX_FabArrayBase.cpp:2862
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:1861
BCMode
Definition AMReX_MLLinOp.H:117
Location
Definition AMReX_MLLinOp.H:119
FabArray memory allocation information.
Definition AMReX_FabArray.H:68