34 #include "libmesh/equation_systems.h" 35 #include "libmesh/linear_implicit_system.h" 36 #include "libmesh/nonlinear_implicit_system.h" 37 #include "libmesh/petsc_linear_solver.h" 38 #include "libmesh/petsc_matrix.h" 39 #include "libmesh/petsc_nonlinear_solver.h" 40 #include "libmesh/petsc_preconditioner.h" 41 #include "libmesh/petsc_vector.h" 42 #include "libmesh/sparse_matrix.h" 43 #include "libmesh/petsc_solver_exception.h" 44 #include "libmesh/simple_range.h" 48 #include <petscsnes.h> 53 #include <petscoptions.h> 71 LibmeshPetscCallA(vector.
comm().
get(), VecView(petsc_vec.
vec(), 0));
78 LibmeshPetscCallA(mat.
comm().
get(), MatView(petsc_mat.
mat(), 0));
86 LibmeshPetscCallA(vector.
comm().
get(), VecView(petsc_vec.
vec(), 0));
94 LibmeshPetscCallA(mat.
comm().
get(), MatView(petsc_mat.
mat(), 0));
99 namespace PetscSupport
110 auto *
const petsc_vec = cast_ptr<PetscVector<Number> *>(vec.get());
111 LibmeshPetscCallA(problem.
comm().
get(), VecSetFromOptions(petsc_vec->vec()));
115 auto * petsc_vec = cast_ptr<PetscVector<Number> *>(lm_sys.
solution.get());
116 LibmeshPetscCallA(problem.
comm().
get(), VecSetFromOptions(petsc_vec->vec()));
118 LibmeshPetscCallA(problem.
comm().
get(), VecSetFromOptions(petsc_vec->vec()));
131 petscOptionsHasName(::PetscOptions options,
132 const std::string & name,
133 const std::string & prefix =
"")
135 PetscBool found = PETSC_FALSE;
136 const char *
const prefix_ptr = prefix.empty() ? nullptr : prefix.c_str();
137 LibmeshPetscCallA(PETSC_COMM_WORLD,
138 PetscOptionsHasName(options, prefix_ptr,
name.c_str(), &found));
154 mightBeMatTypeOption(
const std::string & name)
156 static constexpr std::string_view mat_type_suffix =
"mat_type";
157 return name.size() >= mat_type_suffix.size() &&
158 std::equal(mat_type_suffix.rbegin(),
159 mat_type_suffix.rend(),
161 [](
const char left,
const char right)
164 return static_cast<int>(left) ==
165 std::tolower(libMesh::cast_int<unsigned char>(right));
170 errorOnUnprefixedMatTypeOption(::PetscOptions options,
FEProblemBase & problem)
172 if (!petscOptionsHasName(options,
"-mat_type"))
175 std::string error_string =
176 "Setting option '-mat_type' is not supported without a solver-system prefix. Use an option " 179 if (hasMatrixFreeSolveType(problem))
181 " Attempting to change the matrix " 182 "type for the MFFD matrix type used to represent the Jacobian for (P)JFNK solve " 183 "types is not supported.";
191 const std::optional<std::size_t> system_index = std::nullopt)
193 const auto begin = system_index.value_or(0);
196 for (
const auto sys_index :
make_range(begin, end))
199 auto & lm_sys = solver_system.
system();
210 MatSetOptionsPrefix(petsc_mat->mat(), (solver_system.name() +
"_").c_str()));
211 LibmeshPetscCallA(problem.
comm().
get(), MatSetFromOptions(petsc_mat->mat()));
265 const auto prefix_with_dash =
'-' + solver_params.
_prefix;
267 switch (solver_params.
_type)
271 prefix_with_dash +
"snes_mf_operator");
273 prefix_with_dash +
"mat_mffd_type",
280 prefix_with_dash +
"mat_mffd_type",
293 dont_add_these_options, prefix_with_dash +
"snes_type",
"ksponly");
295 prefix_with_dash +
"snes_monitor_cancel");
305 dont_add_these_options, prefix_with_dash +
"snes_linesearch_type",
stringify(ls_type));
316 LibmeshPetscCallA(PETSC_COMM_WORLD, PetscGetArgs(&argc, &args));
317 std::vector<const char *> cl_args(args + 1, args + argc);
318 const auto cl_argc = libMesh::cast_int<int>(cl_args.size());
321 LibmeshPetscCallA(PETSC_COMM_WORLD, PetscOptionsCreate(&command_line_options));
322 LibmeshPetscCallA(PETSC_COMM_WORLD,
323 PetscOptionsInsertArgs(command_line_options, cl_argc, cl_args.data()));
324 LibmeshPetscCallA(PETSC_COMM_WORLD,
325 PetscOptionsInsertArgs(LIBMESH_PETSC_NULLPTR, cl_argc, cl_args.data()));
329 LibmeshPetscCallA(PETSC_COMM_WORLD, PetscOptionsDestroy(&command_line_options));
333 errorOnUnprefixedMatTypeOption(command_line_options, *problem);
338 const bool have_vec_type = petscOptionsHasName(command_line_options,
"-vec_type");
339 bool have_mat_type =
false;
343 have_mat_type = petscOptionsHasName(
350 applyVectorTypeOptions(*problem);
352 applyMatrixTypeOptions(*problem);
354 LibmeshPetscCallA(PETSC_COMM_WORLD, PetscOptionsDestroy(&command_line_options));
361 for (
const auto & flag : po.
flags)
369 for (
auto & option : po.
pairs)
382 PetscCallAbort(PETSC_COMM_WORLD, PetscOptionsClear(LIBMESH_PETSC_NULLPTR));
389 const std::vector<SolverParams> & solver_params_vec,
392 PetscCallAbort(PETSC_COMM_WORLD, PetscOptionsClear(LIBMESH_PETSC_NULLPTR));
393 for (
const auto & solver_params : solver_params_vec)
402 char code[10] = {45, 45, 109, 111, 111, 115, 101};
403 const std::vector<std::string> argv = cmd_line->
getArguments();
404 for (
const auto & arg : argv)
406 if (arg.compare(code) == 0)
421 SNESConvergedReason * reason,
448 *reason = SNES_CONVERGED_ITERATING;
452 *reason = SNES_CONVERGED_FNORM_ABS;
456 *reason = SNES_DIVERGED_DTOL;
465 KSP , PetscInt it, PetscReal , KSPConvergedReason * reason,
void *
ctx)
493 *reason = KSP_CONVERGED_ITERATING;
498 #if PETSC_VERSION_LESS_THAN(3, 24, 0) 499 *reason = KSP_CONVERGED_RTOL_NORMAL;
501 *reason = KSP_CONVERGED_RTOL_NORMAL_EQUATIONS;
506 *reason = KSP_DIVERGED_DTOL;
536 return KSP_NORM_NONE;
538 return KSP_NORM_PRECONDITIONED;
540 return KSP_NORM_UNPRECONDITIONED;
542 return KSP_NORM_NATURAL;
544 return KSP_NORM_DEFAULT;
546 mooseError(
"Unknown KSP norm type requested.");
557 LibmeshPetscCallA(problem.
comm().
get(),
578 auto & es = problem.
es();
581 PetscReal atol = es.parameters.get<
Real>(
"linear solver absolute tolerance");
587 PetscReal maxits = es.parameters.get<
unsigned int>(
"linear solver maximum iterations");
590 LibmeshPetscCallA(problem.
comm().
get(), KSPSetTolerances(ksp, rtol, atol, 1e100, maxits));
606 applyMatrixTypeOptions(problem);
613 auto *
const petsc_solver = cast_ptr<PetscNonlinearSolver<Number> *>(nl.
nonlinearSolver());
616 const char * snes_prefix =
nullptr;
617 std::string snes_prefix_str;
621 snes_prefix = snes_prefix_str.c_str();
623 SNES snes = petsc_solver->snes(snes_prefix);
625 LibmeshPetscCallA(nl.
comm().
get(), SNESGetKSP(snes, &ksp));
626 LibmeshPetscCallA(nl.
comm().
get(), SNESSetMaxLinearSolveFailures(snes, 1000000));
627 LibmeshPetscCallA(nl.
comm().
get(), SNESSetCheckJacobianDomainError(snes, PETSC_TRUE));
640 auto *
const petsc_solver =
643 if (lm_lin_sys.prefix_with_name())
644 petsc_solver->
init(lm_lin_sys.prefix().c_str());
646 petsc_solver->init();
649 KSP ksp = petsc_solver->ksp();
669 #define checkPrefix(prefix) \ 670 mooseAssert(prefix[0] == '-', \ 671 "Leading prefix character must be a '-'. Current prefix is '" << prefix << "'"); \ 672 mooseAssert((prefix.size() == 1) || (prefix.back() == '_'), \ 673 "Terminating prefix character must be a '_'. Current prefix is '" << prefix << "'"); \ 674 mooseAssert(MooseUtils::isAllLowercase(prefix), "PETSc prefixes should be all lower-case") 678 const std::string & prefix,
681 const auto & params = param_object.
parameters();
685 const auto & petsc_options = params.get<
MultiMooseEnum>(
"petsc_options");
686 const auto & petsc_pair_options =
687 params.
get<
MooseEnumItem, std::string>(
"petsc_options_iname",
"petsc_options_value");
698 petsc_pair_options, fe_problem.
mesh().
dimension(), prefix, param_object, po);
708 const std::string & solve_type = params.
get<
MooseEnum>(
"solve_type");
710 fe_problem.
solverParams(i).
_type = Moose::stringToEnum<Moose::SolveType>(solve_type);
720 const auto & line_search = params.
get<
MooseEnum>(
"line_search");
725 Moose::stringToEnum<Moose::LineSearchType>(line_search);
732 mooseError(
"You've requested a line search but you must be solving an EigenProblem. " 733 "These two things are not consistent.");
736 if (!petsc_nonlinear_solver)
737 mooseError(
"Currently the MOOSE line searches all use Petsc, so you " 738 "must use Petsc as your non-linear solver.");
740 std::make_unique<ComputeLineSearchObjectWrapper>(fe_problem);
751 const auto & mffd_type = params.
get<
MooseEnum>(
"mffd_type");
757 template <
typename T>
761 const auto & string_option =
static_cast<const std::string &
>(option);
762 if (string_option[0] !=
'-')
763 param_object.
mooseError(
"PETSc option '", string_option,
"' does not begin with '-'");
772 prefix.insert(prefix.begin(),
'-');
776 for (
const auto & option : petsc_flags)
780 const std::string & string_option = option.name();
788 if (option ==
"-log_summary" || option ==
"-log_view")
789 mooseError(
"The PETSc option \"-log_summary\" or \"-log_view\" can only be used on the " 790 "command line. Please " 791 "remove it from the input file");
794 const std::string prefixed_option = prefix + string_option.substr(1);
806 prefix.insert(prefix.begin(),
'-');
810 #if !PETSC_VERSION_LESS_THAN(3, 14, 0) 813 std::array<std::string, 2> reason_flags = {{
"snes_converged_reason",
"ksp_converged_reason"}};
817 for (
const auto & reason_flag : reason_flags)
819 const auto full_flag = prefix + reason_flag;
820 if (!po.flags.isValueSet(full_flag) && !po.dont_add_these_options.contains(full_flag) &&
821 (std::find_if(po.pairs.begin(),
823 [&full_flag](
auto & pair)
824 {
return pair.first == (full_flag); }) == po.pairs.end()))
825 po.
pairs.emplace_back(full_flag,
"::failed");
832 const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options,
833 const unsigned int mesh_dimension,
838 prefix.insert(prefix.begin(),
'-');
842 bool boomeramg_found =
false;
843 bool strong_threshold_found =
false;
844 #if !PETSC_VERSION_LESS_THAN(3, 7, 0) 845 bool superlu_dist_found =
false;
846 bool fact_pattern_found =
false;
847 bool tiny_pivot_found =
false;
849 std::string pc_description =
"";
850 #if !PETSC_VERSION_LESS_THAN(3, 12, 0) 852 bool hmg_found =
false;
853 bool matptap_found =
false;
854 bool hmg_strong_threshold_found =
false;
856 std::vector<std::pair<std::string, std::string>> new_options;
858 for (
const auto & [option_name, option_value] : petsc_pair_options)
863 const std::string prefixed_option_name =
864 prefix +
static_cast<const std::string &
>(option_name).substr(1);
868 MooseUtils::findPair(po.
pairs, po.
pairs.begin(), prefixed_option_name, MooseUtils::Any);
869 it == po.
pairs.end())
871 #if !PETSC_VERSION_LESS_THAN(3, 9, 0) 872 if (option_name ==
"-pc_factor_mat_solver_package")
873 new_options.emplace_back(prefix +
"pc_factor_mat_solver_type", option_value);
875 if (option_name ==
"-pc_factor_mat_solver_type")
876 new_options.push_back(prefix +
"pc_factor_mat_solver_package", option_value);
880 if (option_name ==
"-pc_type" || option_name ==
"-sub_pc_type" ||
881 option_name ==
"-pc_hypre_type")
882 pc_description += option_value +
' ';
884 #if !PETSC_VERSION_LESS_THAN(3, 12, 0) 885 if (option_name ==
"-pc_type" && option_value ==
"hmg")
894 #if !PETSC_VERSION_LESS_THAN(3, 17, 0) 895 if (hmg_found && (option_name ==
"-matptap_via" || option_name ==
"-matmaijptap_via" ||
896 option_name ==
"-matproduct_ptap_via"))
897 new_options.emplace_back(prefix +
"mat_product_algorithm", option_value);
898 #elif !PETSC_VERSION_LESS_THAN(3, 13, 0) 899 if (hmg_found && (option_name ==
"-matptap_via" || option_name ==
"-matmaijptap_via"))
900 new_options.emplace_back(prefix +
"matproduct_ptap_via", option_value);
902 if (hmg_found && (option_name ==
"-matproduct_ptap_via"))
904 new_options.emplace_back(prefix +
"matptap_via", option_value);
905 new_options.emplace_back(prefix +
"matmaijptap_via", option_value);
909 if (option_name ==
"-matptap_via" || option_name ==
"-matmaijptap_via" ||
910 option_name ==
"-matproduct_ptap_via" || option_name ==
"-mat_product_algorithm")
911 matptap_found =
true;
914 if (option_name ==
"-hmg_inner_pc_hypre_boomeramg_strong_threshold")
915 hmg_strong_threshold_found =
true;
918 if (option_name ==
"-pc_hypre_type" && option_value ==
"boomeramg")
919 boomeramg_found =
true;
920 if (option_name ==
"-pc_hypre_boomeramg_strong_threshold")
921 strong_threshold_found =
true;
922 #if !PETSC_VERSION_LESS_THAN(3, 7, 0) 923 if ((option_name ==
"-pc_factor_mat_solver_package" ||
924 option_name ==
"-pc_factor_mat_solver_type") &&
925 option_value ==
"superlu_dist")
926 superlu_dist_found =
true;
927 if (option_name ==
"-mat_superlu_dist_fact")
928 fact_pattern_found =
true;
929 if (option_name ==
"-mat_superlu_dist_replacetinypivot")
930 tiny_pivot_found =
true;
933 if (!new_options.empty())
935 std::copy(new_options.begin(), new_options.end(), std::back_inserter(po.
pairs));
936 for (
const auto & option : new_options)
941 po.
pairs.push_back(std::make_pair(prefixed_option_name, option_value));
949 it->second = option_value;
950 it = MooseUtils::findPair(po.
pairs, std::next(it), prefixed_option_name, MooseUtils::Any);
951 }
while (it != po.
pairs.end());
957 if (boomeramg_found && !strong_threshold_found && mesh_dimension == 3)
959 po.
pairs.emplace_back(prefix +
"pc_hypre_boomeramg_strong_threshold",
"0.7");
960 pc_description +=
"strong_threshold: 0.7 (auto)";
963 #if !PETSC_VERSION_LESS_THAN(3, 12, 0) 964 if (hmg_found && !hmg_strong_threshold_found && mesh_dimension == 3)
966 po.
pairs.emplace_back(prefix +
"hmg_inner_pc_hypre_boomeramg_strong_threshold",
"0.7");
967 pc_description +=
"strong_threshold: 0.7 (auto)";
972 if (hmg_found && !matptap_found)
974 #if !PETSC_VERSION_LESS_THAN(3, 17, 0) 975 po.
pairs.emplace_back(prefix +
"mat_product_algorithm",
"allatonce");
976 #elif !PETSC_VERSION_LESS_THAN(3, 13, 0) 977 po.
pairs.emplace_back(prefix +
"matproduct_ptap_via",
"allatonce");
979 po.
pairs.emplace_back(prefix +
"matptap_via",
"allatonce");
980 po.
pairs.emplace_back(prefix +
"matmaijptap_via",
"allatonce");
985 #if !PETSC_VERSION_LESS_THAN(3, 7, 0) 988 if (superlu_dist_found && !fact_pattern_found)
990 po.
pairs.emplace_back(prefix +
"mat_superlu_dist_fact",
991 #
if PETSC_VERSION_LESS_THAN(3, 7, 5)
992 "SamePattern_SameRowPerm");
993 pc_description +=
"mat_superlu_dist_fact: SamePattern_SameRowPerm ";
996 pc_description +=
"mat_superlu_dist_fact: SamePattern ";
1001 if (superlu_dist_found && !tiny_pivot_found)
1003 po.
pairs.emplace_back(prefix +
"mat_superlu_dist_replacetinypivot",
"1");
1004 pc_description +=
" mat_superlu_dist_replacetinypivot: true ";
1008 if (!pc_description.empty() && prefix.size() > 1)
1009 po.
pc_description +=
"[" + prefix.substr(1, prefix.size() - 2) +
"]: ";
1013 std::set<std::string>
1016 return {
"default",
"shell",
"none",
"basic",
"l2",
"bt",
"cp"};
1024 MooseEnum solve_type(
"PJFNK JFNK NEWTON FD LINEAR");
1027 "PJFNK: Preconditioned Jacobian-Free Newton Krylov " 1028 "JFNK: Jacobian-Free Newton Krylov " 1029 "NEWTON: Full Newton Solve " 1030 "FD: Use finite differences to compute Jacobian " 1031 "LINEAR: Solving a linear problem");
1036 "Specifies the finite differencing type for " 1037 "Jacobian-free solve types. Note that the " 1038 "default is wp (for Walker and Pernice).");
1044 params.
addParam<std::vector<std::string>>(
1045 "petsc_options_value",
1046 "Values of PETSc name/value pairs (must correspond with \"petsc_options_iname\"");
1047 params.
addParamNamesToGroup(
"solve_type petsc_options petsc_options_iname petsc_options_value " 1058 "-ksp_monitor_snes_lg -snes_ksp_ew -snes_converged_reason " 1059 "-snes_ksp -snes_linesearch_monitor -snes_mf -snes_mf_operator -snes_monitor " 1060 "-snes_test_display -snes_view -snes_monitor_cancel",
1069 "-ksp_converged_reason -ksp_gmres_modifiedgramschmidt -ksp_monitor",
"",
true);
1075 auto options =
MultiMooseEnum(
"-dm_moose_print_embedding -dm_view",
"",
true);
1084 return MultiMooseEnum(
"-snes_atol -snes_linesearch_type -snes_ls -snes_max_it -snes_rtol " 1085 "-snes_divergence_tolerance -snes_type",
1093 return MultiMooseEnum(
"-ksp_atol -ksp_gmres_restart -ksp_max_it -ksp_pc_side -ksp_rtol " 1094 "-ksp_type -sub_ksp_type",
1101 auto options =
MultiMooseEnum(
"-mat_fd_coloring_err -mat_fd_type -mat_mffd_type " 1102 "-pc_asm_overlap -pc_factor_levels " 1103 "-pc_factor_mat_ordering_type -pc_hypre_boomeramg_grid_sweeps_all " 1104 "-pc_hypre_boomeramg_max_iter " 1105 "-pc_hypre_boomeramg_strong_threshold -pc_hypre_type -pc_type " 1121 LibmeshPetscCallA(fe_problem.
comm().
get(), PetscGetArgs(&argc, &args));
1123 std::vector<std::string> cml_arg;
1124 for (
int i = 0; i < argc; i++)
1125 cml_arg.push_back(args[i]);
1127 if (MooseUtils::findPair(petsc.
pairs, petsc.
pairs.begin(), MooseUtils::Any,
"vinewtonssls") ==
1128 petsc.
pairs.end() &&
1129 MooseUtils::findPair(petsc.
pairs, petsc.
pairs.begin(), MooseUtils::Any,
"vinewtonrsls") ==
1130 petsc.
pairs.end() &&
1131 std::find(cml_arg.begin(), cml_arg.end(),
"vinewtonssls") == cml_arg.end() &&
1132 std::find(cml_arg.begin(), cml_arg.end(),
"vinewtonrsls") == cml_arg.end())
1140 const std::string & value ,
1145 LibmeshPetscCallA(comm.
get(),
1146 PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR,
1148 value ==
"" ? LIBMESH_PETSC_NULLPTR :
value.c_str()));
1153 LibmeshPetscCallA(comm.
get(), PetscOptionsCreate(&single_option));
1154 LibmeshPetscCallA(comm.
get(),
1155 PetscOptionsSetValue(single_option,
1157 value ==
"" ? LIBMESH_PETSC_NULLPTR :
value.c_str()));
1158 auto check_problem = [problem, &
name]()
1162 "Setting the option '",
1164 "' requires passing a 'problem' parameter. Contact a developer of your application " 1165 "to have them update their code. If in doubt, reach out to the MOOSE team on Github " 1170 if (petscOptionsHasName(single_option,
"-vec_type"))
1173 applyVectorTypeOptions(*problem);
1178 else if (problem && mightBeMatTypeOption(
name))
1180 errorOnUnprefixedMatTypeOption(single_option, *problem);
1185 if (!petscOptionsHasName(single_option,
"-mat_type", solver_sys_name +
"_"))
1189 mooseError(
"Setting option '",
name,
"' is incompatible with a JFNK 'solve_type'");
1191 applyMatrixTypeOptions(*problem, i);
1196 LibmeshPetscCallA(comm.
get(), PetscOptionsDestroy(&single_option));
1201 const std::string & name,
1202 const std::string & value ,
1212 unsigned int colors,
1213 std::vector<unsigned int> & vertex_colors,
1214 const char * coloring_algorithm)
1218 LibmeshPetscCallA(PETSC_COMM_SELF, MatCreate(PETSC_COMM_SELF, &A));
1219 LibmeshPetscCallA(PETSC_COMM_SELF, MatSetSizes(A, size, size, size, size));
1220 LibmeshPetscCallA(PETSC_COMM_SELF, MatSetType(A, MATSEQDENSE));
1222 LibmeshPetscCallA(PETSC_COMM_SELF, MatSeqDenseSetPreallocation(A, adjacency_matrix));
1223 LibmeshPetscCallA(PETSC_COMM_SELF, MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1224 LibmeshPetscCallA(PETSC_COMM_SELF, MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1227 #if PETSC_VERSION_LESS_THAN(3, 7, 0) 1228 LibmeshPetscCallA(PETSC_COMM_SELF, MatConvert(A, MATAIJ, MAT_REUSE_MATRIX, &A));
1230 LibmeshPetscCallA(PETSC_COMM_SELF, MatConvert(A, MATAIJ, MAT_INPLACE_MATRIX, &A));
1233 ISColoring iscoloring;
1235 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringCreate(A, &mc));
1236 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetType(mc, coloring_algorithm));
1237 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetMaxColors(mc, static_cast<PetscInt>(colors)));
1240 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetDistance(mc, 1));
1241 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetFromOptions(mc));
1242 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringApply(mc, &iscoloring));
1246 #if PETSC_RELEASE_LESS_THAN(3, 12, 0) 1247 LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringGetIS(iscoloring, &nn, &
is));
1249 LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringGetIS(iscoloring, PETSC_USE_POINTER, &nn, &
is));
1252 if (nn > static_cast<PetscInt>(colors))
1253 throw std::runtime_error(
"Not able to color with designated number of colors");
1255 for (
int i = 0; i < nn; i++)
1258 const PetscInt * indices;
1259 LibmeshPetscCallA(PETSC_COMM_SELF, ISGetLocalSize(
is[i], &isize));
1260 LibmeshPetscCallA(PETSC_COMM_SELF, ISGetIndices(
is[i], &indices));
1261 for (
int j = 0; j < isize; j++)
1263 mooseAssert(indices[j] < static_cast<PetscInt>(vertex_colors.size()),
"Index out of bounds");
1264 vertex_colors[indices[j]] = i;
1266 LibmeshPetscCallA(PETSC_COMM_SELF, ISRestoreIndices(
is[i], &indices));
1269 LibmeshPetscCallA(PETSC_COMM_SELF, MatDestroy(&A));
1270 LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringDestroy(&mc));
1271 LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringDestroy(&iscoloring));
1319 std::unique_ptr<PetscMatrix<Number>>
1322 const std::string & binary_mat_file,
1323 const unsigned int mat_number_to_load)
1325 LibmeshPetscCallA(comm.
get(), MatCreate(comm.
get(), &mat));
1326 PetscViewer matviewer;
1329 PetscViewerBinaryOpen(comm.
get(), binary_mat_file.c_str(), FILE_MODE_READ, &matviewer));
1330 for (
unsigned int i = 0; i < mat_number_to_load; ++i)
1331 LibmeshPetscCallA(comm.
get(), MatLoad(mat, matviewer));
1332 LibmeshPetscCallA(comm.
get(), PetscViewerDestroy(&matviewer));
1334 return std::make_unique<PetscMatrix<Number>>(mat, comm);
std::string name(const ElemQuality q)
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) ...
matrices_iterator matrices_begin()
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
void resetFailNextNonlinearConvergenceCheck()
Tell the problem that the nonlinear convergence check(s) may proceed as normal.
bool isSNESVI(FEProblemBase &fe_problem)
check if SNES type is variational inequalities (VI) solver
vectors_iterator vectors_end()
const std::vector< ConvergenceName > & getNonlinearConvergenceNames() const
Gets the nonlinear system convergence object name(s).
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
void addPetscFlagsToPetscOptions(const MultiMooseEnum &petsc_flags, std::string prefix, const ParallelParamObject ¶m_object, PetscOptions &petsc_options)
Populate flags in a given PetscOptions object using a vector of input arguments.
KSPNormType getPetscKSPNormType(Moose::MooseKSPNormType kspnorm)
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
void setMFFDTypeFromParams(FEProblemBase &fe_problem, const InputParameters ¶ms)
Sets the FE problem's matrix-free finite difference type from the input params.
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
const ExecFlagType EXEC_NONLINEAR_CONVERGENCE
static void petscSetupOutput()
Output string for setting up PETSC output.
virtual std::size_t numNonlinearSystems() const override
std::set< std::string > getPetscValidLineSearches()
Returns the valid petsc line search options as a set of strings.
Moose::LineSearchType _line_search
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
void setAdditionalValue(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
const ExecFlagType EXEC_LINEAR_CONVERGENCE
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options, const unsigned int mesh_dimension, std::string prefix, const ParallelParamObject ¶m_object, PetscOptions &petsc_options)
Populate name and value pairs in a given PetscOptions object using vectors of input arguments...
void petscSetDefaults(FEProblemBase &problem)
Sets the default options for PETSc.
libMesh::LinearImplicitSystem & linearImplicitSystem()
Return a reference to the stored linear implicit system.
virtual MooseConvergenceStatus checkConvergence(unsigned int iter)=0
Returns convergence status.
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) ...
const InputParameters & parameters() const
Get the parameters of the object.
virtual void init(const char *name=nullptr) override
A struct for storing the various types of petsc options and values.
MultiMooseEnum flags
Single value PETSc options (flags)
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
void setSolveTypeFromParams(FEProblemBase &fe_problem, const InputParameters ¶ms)
Sets the FE problem's solve type from the input params.
const Parallel::Communicator & comm() const
void petscSetDefaultKSPNormType(FEProblemBase &problem, KSP ksp)
Set norm type.
std::unique_ptr< PetscMatrix< Number > > createMatrixFromFile(const libMesh::Parallel::Communicator &comm, Mat &petsc_mat, const std::string &binary_mat_file, unsigned int mat_number_to_load=1)
Create a matrix from a binary file.
Solving a linear problem.
void petscSetKSPDefaults(FEProblemBase &problem, KSP ksp)
Set the default options for a KSP.
std::vector< std::shared_ptr< SolverSystem > > _solver_systems
Combined container to base pointer of every solver system.
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
virtual libMesh::NonlinearSolver< Number > * nonlinearSolver()=0
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
vectors_iterator vectors_begin()
MffdType
Type of the matrix-free finite-differencing parameter.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool hasLinearConvergenceObjects() const
Whether we have linear convergence objects.
This class wraps provides and tracks access to command line parameters.
MooseKSPNormType
Norm type for converge test.
Nonlinear system to be solved.
virtual const std::string & name() const
PCSide getPetscPCSide(Moose::PCSideType pcs)
void processSingletonMooseWrappedOptions(FEProblemBase &fe_problem, const InputParameters ¶ms)
Process some MOOSE-wrapped PETSc options.
void checkUserProvidedPetscOption(const T &option, const ParallelParamObject ¶m_object)
void setConvergedReasonFlags(FEProblemBase &fe_problem, std::string prefix)
Set flags that will instruct the user on the reason their simulation diverged from PETSc's perspectiv...
bool contains(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
std::vector< SolverSystemName > _solver_sys_names
The union of nonlinear and linear system names.
virtual void execute(const ExecFlagType &exec_type)
Convenience function for performing execution of MOOSE systems.
void dontAddCommonKSPOptions(FEProblemBase &fe_problem)
Function to ensure that common KSP options are not added to the PetscOptions storage object to be lat...
PetscErrorCode petscLinearConverged(KSP, PetscInt it, PetscReal, KSPConvergedReason *reason, void *ctx)
void libmesh_ignore(const Args &...)
Use whatever we have in PETSc.
PetscErrorCode petscNonlinearConverged(SNES, PetscInt it, PetscReal, PetscReal, PetscReal, SNESConvergedReason *reason, void *ctx)
const T & get(std::string_view) const
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags) ...
Moose::MffdType _mffd_type
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
LineSearchType
Type of the line search.
virtual Convergence & getConvergence(const std::string &name, const THREAD_ID tid=0) const
Gets a Convergence object.
void MooseMatView(SparseMatrix< Number > &mat)
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
std::string prefix() const
Use whatever we have in PETSc.
void dontAddPetscFlag(const std::string &flag, PetscOptions &petsc_options)
Function to ensure that a particular petsc option is not added to the PetscOptions storage object to ...
std::string stringify(const MffdType &t)
NonlinearSystemBase & currentNonlinearSystem()
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Jacobian-Free Newton Krylov.
virtual libMesh::EquationSystems & es() override
void setSolverOptions(const SolverParams &solver_params, const MultiMooseEnum &dont_add_these_options)
std::unique_ptr< NumericVector< Number > > solution
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
PetscErrorCode PetscInt const PetscInt IS * is
Use finite differences to compute Jacobian.
void addPetscOptionsFromCommandline(FEProblemBase *const problem=nullptr)
Insert command-line PETSc options into the active PETSc options database.
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
bool isValueSet(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
unsigned int number() const
Gets the number of this system.
AuxiliarySystem & getAuxiliarySystem()
void petscSetOptions(const PetscOptions &po, const SolverParams &solver_params, FEProblemBase *const problem=nullptr)
A function for setting the PETSc options in PETSc from the options supplied to MOOSE.
std::vector< std::string > getNames() const
Method for returning a vector of all valid enumeration names for this instance.
bool getFailNextSystemConvergenceCheck() const
Whether it will fail the next system convergence check(s), triggering failed step behavior...
LinearSystem & getLinearSystem(unsigned int sys_num)
Get non-constant reference to a linear system.
MooseConvergenceStatus
Status returned by calls to checkConvergence.
void setLineSearchFromParams(FEProblemBase &fe_problem, const InputParameters ¶ms)
Sets the FE problem's line search from the input params.
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve the id of an item from the MultiMooseEnum. ...
void setSinglePetscOption(const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
A wrapper function for dealing with different versions of PetscOptionsSetValue.
MultiMooseEnum getCommonKSPFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags) ...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
InputParameters getPetscValidParams()
Returns the PETSc options that are common between Executioners and Preconditioners.
PetscErrorCode petscSetupOutput(CommandLine *cmd_line)
std::unique_ptr< ComputeLineSearchObject > linesearch_object
Class for containing MooseEnum item information.
void petscSetOptionsHelper(const PetscOptions &po, FEProblemBase *const problem)
void dontAddNonlinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be ...
void colorAdjacencyMatrix(PetscScalar *adjacency_matrix, unsigned int size, unsigned int colors, std::vector< unsigned int > &vertex_colors, const char *coloring_algorithm)
This method takes an adjacency matrix, and a desired number of colors and applies a graph coloring al...
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check(s) ...
IntRange< T > make_range(T beg, T end)
virtual MooseMesh & mesh() override
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
LinearSystem & currentLinearSystem()
Get a non-constant reference to the current linear system.
void resetFailNextSystemConvergenceCheck()
Tell the problem that the system convergence check(s) may proceed as normal.
PCSideType
Preconditioning side.
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
SolverSystem & getSolverSystem(unsigned int sys_num)
Get non-constant reference to a solver system.
std::unique_ptr< NumericVector< Number > > current_local_solution
MultiMooseEnum getCommonSNESKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys) ...
Linear system to be solved.
void setSinglePetscOptionIfAppropriate(const MultiMooseEnum &dont_add_these_options, const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
Same as setSinglePetscOption, but does not set the option if it doesn't make sense for the current si...
void dontAddLinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be l...
Preconditioned Jacobian-Free Newton Krylov.
virtual std::size_t numLinearSystems() const override
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
virtual libMesh::System & system() override
Get the reference to the libMesh system.
void storePetscOptions(FEProblemBase &fe_problem, const std::string &prefix, const ParallelParamObject ¶m_object)
Stores the PETSc options supplied from the parameter object on the problem.
std::string pc_description
Preconditioner description.
MultiMooseEnum dont_add_these_options
Flags to explicitly not set, even if they are specified programmatically.
Moose::MooseKSPNormType getMooseKSPNormType()
Get the norm in which the linear convergence is measured.
void prefix_with_name(bool value)
Base class shared by both Action and MooseObject.
PetscFunctionReturn(LIBMESH_PETSC_SUCCESS)
void MooseVecView(NumericVector< Number > &vector)
virtual std::size_t numSolverSystems() const override
MultiMooseEnum user_set_options
Options that are set by the user at the input level.
matrices_iterator matrices_end()
auto index_range(const T &sizable)
const std::vector< std::string > & getArguments()
Moose::PCSideType getPCSide()
Get the current preconditioner side.
void petscSetDefaultPCSide(FEProblemBase &problem, KSP ksp)
Setup which side we want to apply preconditioner.
const std::vector< ConvergenceName > & getLinearConvergenceNames() const
Gets the linear convergence object name(s).
virtual libMesh::System & system() override
Get the reference to the libMesh system.