https://mooseframework.inl.gov
Classes | Functions
Moose::PetscSupport Namespace Reference

Classes

class  PetscOptions
 A struct for storing the various types of petsc options and values. More...
 

Functions

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. More...
 
void petscSetOptions (const PetscOptions &po, const std::vector< SolverParams > &solver_params, FEProblemBase *problem)
 A function for setting the PETSc options in PETSc from the options supplied to MOOSE. More...
 
void petscSetKSPDefaults (FEProblemBase &problem, KSP ksp)
 Set the default options for a KSP. More...
 
template<typename T >
void setLinearSolverDefaults (FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
 Set the defaults for a libMesh LinearSolver. More...
 
void petscSetDefaults (FEProblemBase &problem)
 Sets the default options for PETSc. More...
 
PetscErrorCode petscSetupOutput (CommandLine *cmd_line)
 
void outputNorm (libMesh::Real old_norm, libMesh::Real norm, bool use_color=false)
 Helper function for outputting the norm values with/without color. More...
 
PetscErrorCode petscLinearMonitor (KSP, PetscInt its, PetscReal rnorm, void *void_ptr)
 Helper function for displaying the linear residual during PETSC solve. More...
 
void processSingletonMooseWrappedOptions (FEProblemBase &fe_problem, const InputParameters &params)
 Process some MOOSE-wrapped PETSc options. More...
 
void storePetscOptions (FEProblemBase &fe_problem, const std::string &prefix, const ParallelParamObject &param_object)
 Stores the PETSc options supplied from the parameter object on the problem. More...
 
void setConvergedReasonFlags (FEProblemBase &fe_problem, std::string prefix)
 Set flags that will instruct the user on the reason their simulation diverged from PETSc's perspective. More...
 
void setSolveTypeFromParams (FEProblemBase &fe_problem, const InputParameters &params)
 Sets the FE problem's solve type from the input params. More...
 
void setLineSearchFromParams (FEProblemBase &fe_problem, const InputParameters &params)
 Sets the FE problem's line search from the input params. More...
 
void setMFFDTypeFromParams (FEProblemBase &fe_problem, const InputParameters &params)
 Sets the FE problem's matrix-free finite difference type from the input params. More...
 
void storePetscOptionsFromParams (FEProblemBase &fe_problem, const InputParameters &params)
 Stores the Petsc flags and pair options fron the input params in the given PetscOptions object. More...
 
void addPetscFlagsToPetscOptions (const MultiMooseEnum &petsc_flags, std::string prefix, const ParallelParamObject &param_object, PetscOptions &petsc_options)
 Populate flags in a given PetscOptions object using a vector of input arguments. More...
 
void addPetscPairsToPetscOptions (const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options, const unsigned int mesh_dimension, std::string prefix, const ParallelParamObject &param_object, PetscOptions &petsc_options)
 Populate name and value pairs in a given PetscOptions object using vectors of input arguments. More...
 
std::set< std::string > getPetscValidLineSearches ()
 Returns the valid petsc line search options as a set of strings. More...
 
InputParameters getPetscValidParams ()
 Returns the PETSc options that are common between Executioners and Preconditioners. More...
 
MultiMooseEnum getCommonPetscFlags ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags) More...
 
MultiMooseEnum getCommonSNESFlags ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) More...
 
MultiMooseEnum getCommonKSPFlags ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags) More...
 
MultiMooseEnum getCommonPetscKeys ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-value pairs) More...
 
MultiMooseEnum getCommonSNESKeys ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys) More...
 
MultiMooseEnum getCommonKSPKeys ()
 A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) More...
 
bool isSNESVI (FEProblemBase &fe_problem)
 check if SNES type is variational inequalities (VI) solver More...
 
void setSinglePetscOption (const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
 A wrapper function for dealing with different versions of PetscOptionsSetValue. More...
 
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 simulation type, e.g. More...
 
void registerPetscCitation (const std::string &bibtex)
 Register a BibTeX entry with PETSc's citation list so that it is printed (alongside the run-specific citations from any PETSc solvers/preconditioners used) when the PETSc -citations option is enabled. More...
 
void addPetscOptionsFromCommandline (FEProblemBase *const problem=nullptr)
 Insert command-line PETSc options into the active PETSc options database. More...
 
void petscSetDefaultPCSide (FEProblemBase &problem, KSP ksp)
 Setup which side we want to apply preconditioner. More...
 
void petscSetDefaultKSPNormType (FEProblemBase &problem, KSP ksp)
 Set norm type. More...
 
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 algorithm to produce a coloring. More...
 
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 be later set unless explicitly specified in input or on the command line. More...
 
void dontAddNonlinearConvergedReason (FEProblemBase &fe_problem)
 Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line. More...
 
void dontAddLinearConvergedReason (FEProblemBase &fe_problem)
 Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line. More...
 
void dontAddCommonKSPOptions (FEProblemBase &fe_problem)
 Function to ensure that common KSP options are not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line. More...
 
void dontAddCommonSNESOptions (FEProblemBase &fe_problem)
 Function to ensure that common SNES options are not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line. More...
 
void dontAddCommonSNESOptions (FEProblemBase &fe_problem, const std::string &prefix)
 Prefixed variant: suppress common SNES options for the system identified by prefix (e.g. More...
 
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. More...
 
std::string stringify (const LineSearchType &t)
 
std::string stringify (const MffdType &t)
 
void setSolverOptions (const SolverParams &solver_params, const MultiMooseEnum &dont_add_these_options)
 
void petscSetOptionsHelper (const PetscOptions &po, FEProblemBase *const problem)
 
PetscErrorCode petscNonlinearConverged (SNES, PetscInt it, PetscReal, PetscReal, PetscReal, SNESConvergedReason *reason, void *ctx)
 
PetscErrorCode petscLinearConverged (KSP, PetscInt it, PetscReal, KSPConvergedReason *reason, void *ctx)
 
PCSide getPetscPCSide (Moose::PCSideType pcs)
 
KSPNormType getPetscKSPNormType (Moose::MooseKSPNormType kspnorm)
 
template<typename T >
void checkUserProvidedPetscOption (const T &option, const ParallelParamObject &param_object)
 

Function Documentation

◆ addPetscFlagsToPetscOptions()

void Moose::PetscSupport::addPetscFlagsToPetscOptions ( const MultiMooseEnum petsc_flags,
std::string  prefix,
const ParallelParamObject param_object,
PetscOptions petsc_options 
)

Populate flags in a given PetscOptions object using a vector of input arguments.

Parameters
petsc_flagsContainer holding the flags of the petsc options
prefixThe prefix to add to the user provided petsc_flags. This should not contain a leading dash per PETSc prefix convention. Note that this function will immediately add said dash at the start of prefix so that later calls to PetscOptionsSetValue work. This is the reason we pass prefix by value
param_objectThe ParallelParamObject adding the PETSc options
petsc_optionsData structure which handles petsc options within moose

"-log_summary" cannot be used in the input file. This option needs to be set when PETSc is initialized which happens before the parser is even created. We'll throw an error if somebody attempts to add this option later.

"-log_summary" cannot be used in the input file. This option needs to be set when PETSc is initialized which happens before the parser is even created. We'll throw an error if somebody attempts to add this option later.

Definition at line 767 of file PetscSupport.C.

Referenced by MFEMPetscNonlinearSolver::ConstructSolver(), and storePetscOptions().

771 {
772  prefix.insert(prefix.begin(), '-');
773  checkPrefix(prefix);
774 
775  // Update the PETSc single flags
776  for (const auto & option : petsc_flags)
777  {
778  checkUserProvidedPetscOption(option, param_object);
779 
780  const std::string & string_option = option.name();
781 
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");
792 
793  // Update the stored items, but do not create duplicates
794  const std::string prefixed_option = prefix + string_option.substr(1);
795  if (!po.flags.isValueSet(prefixed_option))
796  {
797  po.flags.setAdditionalValue(prefixed_option);
798  po.user_set_options.setAdditionalValue(prefixed_option);
799  }
800  }
801 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
void checkUserProvidedPetscOption(const T &option, const ParallelParamObject &param_object)
Definition: PetscSupport.C:759

◆ addPetscOptionsFromCommandline()

void Moose::PetscSupport::addPetscOptionsFromCommandline ( FEProblemBase *const  problem = nullptr)

Insert command-line PETSc options into the active PETSc options database.

When problem is provided, this also reapplies vector and matrix type options that may have been consumed before the options database was rebuilt, which preserves PETSc's used-option bookkeeping for command-line '-vec_type' and '*mat_type' options.

Definition at line 309 of file PetscSupport.C.

Referenced by petscSetOptionsHelper(), and Moose::SlepcSupport::slepcSetOptions().

310 {
311  // commandline options always win
312  // the options from a user commandline will overwrite the existing ones if any conflicts
313  int argc;
314  char ** args;
315 
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());
319 
320  ::PetscOptions command_line_options;
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()));
326 
327  if (!problem)
328  {
329  LibmeshPetscCallA(PETSC_COMM_WORLD, PetscOptionsDestroy(&command_line_options));
330  return;
331  }
332 
333  errorOnUnprefixedMatTypeOption(command_line_options, *problem);
334 
335  // Some vector/matrix-type options may have been consumed before the PETSc database rebuild.
336  // Replay only the command-line-controlled applications so input-file options handled through
337  // setSinglePetscOption() do not pay the cost twice.
338  const bool have_vec_type = petscOptionsHasName(command_line_options, "-vec_type");
339  bool have_mat_type = false;
340 
341  for (const auto sys_index : make_range(problem->numSolverSystems()))
342  {
343  have_mat_type = petscOptionsHasName(
344  command_line_options, "-mat_type", problem->getSolverSystem(sys_index).name() + "_");
345  if (have_mat_type)
346  break;
347  }
348 
349  if (have_vec_type)
350  applyVectorTypeOptions(*problem);
351  if (have_mat_type)
352  applyMatrixTypeOptions(*problem);
353 
354  LibmeshPetscCallA(PETSC_COMM_WORLD, PetscOptionsDestroy(&command_line_options));
355 }
virtual const std::string & name() const
Definition: SystemBase.C:1342
IntRange< T > make_range(T beg, T end)
SolverSystem & getSolverSystem(unsigned int sys_num)
Get non-constant reference to a solver system.
virtual std::size_t numSolverSystems() const override

◆ addPetscPairsToPetscOptions()

void Moose::PetscSupport::addPetscPairsToPetscOptions ( const std::vector< std::pair< MooseEnumItem, std::string >> &  petsc_pair_options,
const unsigned int  mesh_dimension,
std::string  prefix,
const ParallelParamObject param_object,
PetscOptions petsc_options 
)

Populate name and value pairs in a given PetscOptions object using vectors of input arguments.

Parameters
petsc_pair_optionsOption-value pairs of petsc settings
mesh_dimensionThe mesh dimension, needed for multigrid settings
prefixThe prefix to add to the user provided petsc_pair_options. This should not contain a leading dash per PETSc prefix convention. Note that this function will immediately add said dash at the start of prefix so that later calls to PetscOptionsSetValue work. This is the reason we pass prefix by value
param_objectThe ParallelParamObject adding the PETSc options
petsc_optionsData structure which handles petsc options within moose

Definition at line 831 of file PetscSupport.C.

Referenced by PhysicsBase::addPetscPairsToPetscOptions(), MFEMPetscNonlinearSolver::ConstructSolver(), and storePetscOptions().

837 {
838  prefix.insert(prefix.begin(), '-');
839  checkPrefix(prefix);
840 
841  // Setup the name value pairs
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;
848 #endif
849  std::string pc_description = "";
850 #if !PETSC_VERSION_LESS_THAN(3, 12, 0)
851  // If users use HMG, we would like to set
852  bool hmg_found = false;
853  bool matptap_found = false;
854  bool hmg_strong_threshold_found = false;
855 #endif
856  std::vector<std::pair<std::string, std::string>> new_options;
857 
858  for (const auto & [option_name, option_value] : petsc_pair_options)
859  {
860  checkUserProvidedPetscOption(option_name, param_object);
861 
862  new_options.clear();
863  const std::string prefixed_option_name =
864  prefix + static_cast<const std::string &>(option_name).substr(1);
865 
866  // Do not add duplicate settings
867  if (auto it =
868  MooseUtils::findPair(po.pairs, po.pairs.begin(), prefixed_option_name, MooseUtils::Any);
869  it == po.pairs.end())
870  {
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);
874 #else
875  if (option_name == "-pc_factor_mat_solver_type")
876  new_options.push_back(prefix + "pc_factor_mat_solver_package", option_value);
877 #endif
878 
879  // Look for a pc description
880  if (option_name == "-pc_type" || option_name == "-sub_pc_type" ||
881  option_name == "-pc_hypre_type")
882  pc_description += option_value + ' ';
883 
884 #if !PETSC_VERSION_LESS_THAN(3, 12, 0)
885  if (option_name == "-pc_type" && option_value == "hmg")
886  hmg_found = true;
887 
888  // MPIAIJ for PETSc 3.12.0: -matptap_via
889  // MAIJ for PETSc 3.12.0: -matmaijptap_via
890  // MPIAIJ for PETSc 3.13 to 3.16: -matptap_via, -matproduct_ptap_via
891  // MAIJ for PETSc 3.13 to 3.16: -matproduct_ptap_via
892  // MPIAIJ for PETSc 3.17 and higher: -matptap_via, -mat_product_algorithm
893  // MAIJ for PETSc 3.17 and higher: -mat_product_algorithm
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);
901 #else
902  if (hmg_found && (option_name == "-matproduct_ptap_via"))
903  {
904  new_options.emplace_back(prefix + "matptap_via", option_value);
905  new_options.emplace_back(prefix + "matmaijptap_via", option_value);
906  }
907 #endif
908 
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;
912 
913  // For 3D problems, we need to set this 0.7
914  if (option_name == "-hmg_inner_pc_hypre_boomeramg_strong_threshold")
915  hmg_strong_threshold_found = true;
916 #endif
917  // This special case is common enough that we'd like to handle it for the user.
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;
931 #endif
932 
933  if (!new_options.empty())
934  {
935  std::copy(new_options.begin(), new_options.end(), std::back_inserter(po.pairs));
936  for (const auto & option : new_options)
937  po.user_set_options.setAdditionalValue(option.first);
938  }
939  else
940  {
941  po.pairs.push_back(std::make_pair(prefixed_option_name, option_value));
942  po.user_set_options.setAdditionalValue(prefixed_option_name);
943  }
944  }
945  else
946  {
947  do
948  {
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());
952  }
953  }
954 
955  // When running a 3D mesh with boomeramg, it is almost always best to supply a strong threshold
956  // value. We will provide that for the user here if they haven't supplied it themselves.
957  if (boomeramg_found && !strong_threshold_found && mesh_dimension == 3)
958  {
959  po.pairs.emplace_back(prefix + "pc_hypre_boomeramg_strong_threshold", "0.7");
960  pc_description += "strong_threshold: 0.7 (auto)";
961  }
962 
963 #if !PETSC_VERSION_LESS_THAN(3, 12, 0)
964  if (hmg_found && !hmg_strong_threshold_found && mesh_dimension == 3)
965  {
966  po.pairs.emplace_back(prefix + "hmg_inner_pc_hypre_boomeramg_strong_threshold", "0.7");
967  pc_description += "strong_threshold: 0.7 (auto)";
968  }
969 
970  // Default PETSc PtAP takes too much memory, and it is not quite useful
971  // Let us switch to use new algorithm
972  if (hmg_found && !matptap_found)
973  {
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");
978 #else
979  po.pairs.emplace_back(prefix + "matptap_via", "allatonce");
980  po.pairs.emplace_back(prefix + "matmaijptap_via", "allatonce");
981 #endif
982  }
983 #endif
984 
985 #if !PETSC_VERSION_LESS_THAN(3, 7, 0)
986  // In PETSc-3.7.{0--4}, there is a bug when using superlu_dist, and we have to use
987  // SamePattern_SameRowPerm, otherwise we use whatever we have in PETSc
988  if (superlu_dist_found && !fact_pattern_found)
989  {
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 ";
994 #else
995  "SamePattern");
996  pc_description += "mat_superlu_dist_fact: SamePattern ";
997 #endif
998  }
999 
1000  // restore this superlu option
1001  if (superlu_dist_found && !tiny_pivot_found)
1002  {
1003  po.pairs.emplace_back(prefix + "mat_superlu_dist_replacetinypivot", "1");
1004  pc_description += " mat_superlu_dist_replacetinypivot: true ";
1005  }
1006 #endif
1007  // Set Preconditioner description
1008  if (!pc_description.empty() && prefix.size() > 1)
1009  po.pc_description += "[" + prefix.substr(1, prefix.size() - 2) + "]: ";
1010  po.pc_description += pc_description;
1011 }
void checkUserProvidedPetscOption(const T &option, const ParallelParamObject &param_object)
Definition: PetscSupport.C:759

◆ checkUserProvidedPetscOption()

template<typename T >
void Moose::PetscSupport::checkUserProvidedPetscOption ( const T &  option,
const ParallelParamObject param_object 
)

Definition at line 759 of file PetscSupport.C.

Referenced by addPetscFlagsToPetscOptions(), and addPetscPairsToPetscOptions().

760 {
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 '-'");
764 }
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...
Definition: MooseBase.h:281

◆ colorAdjacencyMatrix()

void Moose::PetscSupport::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 algorithm to produce a coloring.

The coloring is returned as a vector of unsigned integers indicating which color or group each vextex in the adjacency matrix belongs to.

Definition at line 1210 of file PetscSupport.C.

1215 {
1216  // Mat A will be a dense matrix from the incoming data structure
1217  Mat A;
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));
1221  // PETSc requires a non-const data array to populate the matrix
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));
1225 
1226  // Convert A to a sparse matrix
1227 #if PETSC_VERSION_LESS_THAN(3, 7, 0)
1228  LibmeshPetscCallA(PETSC_COMM_SELF, MatConvert(A, MATAIJ, MAT_REUSE_MATRIX, &A));
1229 #else
1230  LibmeshPetscCallA(PETSC_COMM_SELF, MatConvert(A, MATAIJ, MAT_INPLACE_MATRIX, &A));
1231 #endif
1232 
1233  ISColoring iscoloring;
1234  MatColoring mc;
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)));
1238 
1239  // Petsc normally colors by distance two (neighbors of neighbors), we just want one
1240  LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetDistance(mc, 1));
1241  LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringSetFromOptions(mc));
1242  LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringApply(mc, &iscoloring));
1243 
1244  PetscInt nn;
1245  IS * is;
1246 #if PETSC_RELEASE_LESS_THAN(3, 12, 0)
1247  LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringGetIS(iscoloring, &nn, &is));
1248 #else
1249  LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringGetIS(iscoloring, PETSC_USE_POINTER, &nn, &is));
1250 #endif
1251 
1252  if (nn > static_cast<PetscInt>(colors))
1253  throw std::runtime_error("Not able to color with designated number of colors");
1254 
1255  for (int i = 0; i < nn; i++)
1256  {
1257  PetscInt isize;
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++)
1262  {
1263  mooseAssert(indices[j] < static_cast<PetscInt>(vertex_colors.size()), "Index out of bounds");
1264  vertex_colors[indices[j]] = i;
1265  }
1266  LibmeshPetscCallA(PETSC_COMM_SELF, ISRestoreIndices(is[i], &indices));
1267  }
1268 
1269  LibmeshPetscCallA(PETSC_COMM_SELF, MatDestroy(&A));
1270  LibmeshPetscCallA(PETSC_COMM_SELF, MatColoringDestroy(&mc));
1271  LibmeshPetscCallA(PETSC_COMM_SELF, ISColoringDestroy(&iscoloring));
1272 }
PetscErrorCode PetscInt const PetscInt IS * is

◆ createMatrixFromFile()

std::unique_ptr< PetscMatrix< Number > > Moose::PetscSupport::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.

Note that the returned libMesh matrix wrapper will not destroy the created matrix on destruction. petsc_mat must be destroyed manually via MatDestroy

Parameters
mat_number_to_loadA binary file may contain multiple writes of a matrix. This parameter can be used to load a particular matrix from the binary file. By default we load the first written matrix

Definition at line 1320 of file PetscSupport.C.

Referenced by MatrixSymmetryCheck::execute().

1324 {
1325  LibmeshPetscCallA(comm.get(), MatCreate(comm.get(), &mat));
1326  PetscViewer matviewer;
1327  LibmeshPetscCallA(
1328  comm.get(),
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));
1333 
1334  return std::make_unique<PetscMatrix<Number>>(mat, comm);
1335 }

◆ dontAddCommonKSPOptions()

void Moose::PetscSupport::dontAddCommonKSPOptions ( FEProblemBase fe_problem)

Function to ensure that common KSP options are not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line.

Definition at line 1294 of file PetscSupport.C.

Referenced by ExplicitTimeIntegrator::ExplicitTimeIntegrator().

1295 {
1296  auto & petsc_options = fe_problem.getPetscOptions();
1297  for (const auto & flag : getCommonKSPFlags().getNames())
1298  dontAddPetscFlag(flag, petsc_options);
1299  for (const auto & key : getCommonKSPKeys().getNames())
1300  dontAddPetscFlag(key, petsc_options);
1301 }
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) ...
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
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 ...
MultiMooseEnum getCommonKSPFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags) ...

◆ dontAddCommonSNESOptions() [1/2]

void Moose::PetscSupport::dontAddCommonSNESOptions ( FEProblemBase fe_problem)

Function to ensure that common SNES options are not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line.

Definition at line 1304 of file PetscSupport.C.

Referenced by ExplicitTimeIntegrator::ExplicitTimeIntegrator(), and FEProblemSolve::FEProblemSolve().

1305 {
1306  dontAddCommonSNESOptions(fe_problem, "");
1307 }
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...

◆ dontAddCommonSNESOptions() [2/2]

void Moose::PetscSupport::dontAddCommonSNESOptions ( FEProblemBase fe_problem,
const std::string &  prefix 
)

Prefixed variant: suppress common SNES options for the system identified by prefix (e.g.

"v_sys_"). Adds the prefixed flag/key names to the blocklist so that setSolverOptions and setConvergedReasonFlags skip them for that system.

Definition at line 1310 of file PetscSupport.C.

1311 {
1312  auto & petsc_options = fe_problem.getPetscOptions();
1313  for (const auto & flag : getCommonSNESFlags().getNames())
1314  dontAddPetscFlag("-" + prefix + flag.substr(1), petsc_options);
1315  for (const auto & key : getCommonSNESKeys().getNames())
1316  dontAddPetscFlag("-" + prefix + key.substr(1), petsc_options);
1317 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) ...
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::vector< std::string > getNames() const
Method for returning a vector of all valid enumeration names for this instance.
MultiMooseEnum getCommonSNESKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys) ...

◆ dontAddLinearConvergedReason()

void Moose::PetscSupport::dontAddLinearConvergedReason ( FEProblemBase fe_problem)

Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line.

Definition at line 1288 of file PetscSupport.C.

Referenced by CommonOutputAction::act(), and EigenProblemSolve::EigenProblemSolve().

1289 {
1290  dontAddPetscFlag("-ksp_converged_reason", fe_problem.getPetscOptions());
1291 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
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 ...

◆ dontAddNonlinearConvergedReason()

void Moose::PetscSupport::dontAddNonlinearConvergedReason ( FEProblemBase fe_problem)

Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be later set unless explicitly specified in input or on the command line.

Definition at line 1282 of file PetscSupport.C.

Referenced by CommonOutputAction::act(), and EigenProblemSolve::EigenProblemSolve().

1283 {
1284  dontAddPetscFlag("-snes_converged_reason", fe_problem.getPetscOptions());
1285 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
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 ...

◆ dontAddPetscFlag()

void Moose::PetscSupport::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 be later set unless explicitly specified in input or on the command line.

Definition at line 1275 of file PetscSupport.C.

Referenced by dontAddCommonKSPOptions(), dontAddCommonSNESOptions(), dontAddLinearConvergedReason(), dontAddNonlinearConvergedReason(), and EigenProblemSolve::EigenProblemSolve().

1276 {
1277  if (!petsc_options.dont_add_these_options.contains(flag))
1278  petsc_options.dont_add_these_options.setAdditionalValue(flag);
1279 }

◆ getCommonKSPFlags()

MultiMooseEnum Moose::PetscSupport::getCommonKSPFlags ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags)

Definition at line 1066 of file PetscSupport.C.

Referenced by dontAddCommonKSPOptions(), and getCommonPetscFlags().

1067 {
1068  return MultiMooseEnum(
1069  "-ksp_converged_reason -ksp_gmres_modifiedgramschmidt -ksp_monitor", "", true);
1070 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getCommonKSPKeys()

MultiMooseEnum Moose::PetscSupport::getCommonKSPKeys ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys)

Definition at line 1091 of file PetscSupport.C.

Referenced by dontAddCommonKSPOptions(), and getCommonPetscKeys().

1092 {
1093  return MultiMooseEnum("-ksp_atol -ksp_gmres_restart -ksp_max_it -ksp_pc_side -ksp_rtol "
1094  "-ksp_type -sub_ksp_type",
1095  "",
1096  true);
1097 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getCommonPetscFlags()

MultiMooseEnum Moose::PetscSupport::getCommonPetscFlags ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags)

Definition at line 1073 of file PetscSupport.C.

Referenced by getPetscValidParams(), AddFieldSplitAction::validParams(), MFEMPetscNonlinearSolver::validParams(), and Split::validParams().

1074 {
1075  auto options = MultiMooseEnum("-dm_moose_print_embedding -dm_view", "", true);
1076  options.addValidName(getCommonKSPFlags());
1077  options.addValidName(getCommonSNESFlags());
1078  return options;
1079 }
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) ...
MultiMooseEnum getCommonKSPFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags) ...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getCommonPetscKeys()

MultiMooseEnum Moose::PetscSupport::getCommonPetscKeys ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-value pairs)

Definition at line 1099 of file PetscSupport.C.

Referenced by getPetscValidParams(), AddFieldSplitAction::validParams(), MFEMPetscNonlinearSolver::validParams(), and Split::validParams().

1100 {
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 "
1106  "-sub_pc_type",
1107  "",
1108  true);
1109  options.addValidName(getCommonKSPKeys());
1110  options.addValidName(getCommonSNESKeys());
1111  return options;
1112 }
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) ...
MultiMooseEnum getCommonSNESKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys) ...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getCommonSNESFlags()

MultiMooseEnum Moose::PetscSupport::getCommonSNESFlags ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags)

Definition at line 1055 of file PetscSupport.C.

Referenced by dontAddCommonSNESOptions(), and getCommonPetscFlags().

1056 {
1057  return MultiMooseEnum(
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",
1061  "",
1062  true);
1063 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getCommonSNESKeys()

MultiMooseEnum Moose::PetscSupport::getCommonSNESKeys ( )

A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys)

Definition at line 1082 of file PetscSupport.C.

Referenced by dontAddCommonSNESOptions(), and getCommonPetscKeys().

1083 {
1084  return MultiMooseEnum("-snes_atol -snes_linesearch_type -snes_ls -snes_max_it -snes_rtol "
1085  "-snes_divergence_tolerance -snes_type",
1086  "",
1087  true);
1088 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ getPetscKSPNormType()

KSPNormType Moose::PetscSupport::getPetscKSPNormType ( Moose::MooseKSPNormType  kspnorm)

Definition at line 531 of file PetscSupport.C.

Referenced by petscSetDefaultKSPNormType().

532 {
533  switch (kspnorm)
534  {
535  case Moose::KSPN_NONE:
536  return KSP_NORM_NONE;
538  return KSP_NORM_PRECONDITIONED;
540  return KSP_NORM_UNPRECONDITIONED;
541  case Moose::KSPN_NATURAL:
542  return KSP_NORM_NATURAL;
543  case Moose::KSPN_DEFAULT:
544  return KSP_NORM_DEFAULT;
545  default:
546  mooseError("Unknown KSP norm type requested.");
547  break;
548  }
549 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
Use whatever we have in PETSc.
Definition: MooseTypes.h:885

◆ getPetscPCSide()

PCSide Moose::PetscSupport::getPetscPCSide ( Moose::PCSideType  pcs)

Definition at line 514 of file PetscSupport.C.

Referenced by petscSetDefaultPCSide().

515 {
516  switch (pcs)
517  {
518  case Moose::PCS_LEFT:
519  return PC_LEFT;
520  case Moose::PCS_RIGHT:
521  return PC_RIGHT;
523  return PC_SYMMETRIC;
524  default:
525  mooseError("Unknown PC side requested.");
526  break;
527  }
528 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311

◆ getPetscValidLineSearches()

std::set< std::string > Moose::PetscSupport::getPetscValidLineSearches ( )

Returns the valid petsc line search options as a set of strings.

Definition at line 1014 of file PetscSupport.C.

Referenced by FEProblemSolve::validParams().

1015 {
1016  return {"default", "shell", "none", "basic", "l2", "bt", "cp"};
1017 }

◆ getPetscValidParams()

InputParameters Moose::PetscSupport::getPetscValidParams ( )

Returns the PETSc options that are common between Executioners and Preconditioners.

Returns
InputParameters object containing the PETSc related parameters

The output of this function should be added to the the parameters object of the overarching class

See also
CreateExecutionerAction

Definition at line 1020 of file PetscSupport.C.

Referenced by FEProblemSolve::validParams(), and MoosePreconditioner::validParams().

1021 {
1023 
1024  MooseEnum solve_type("PJFNK JFNK NEWTON FD LINEAR");
1025  params.addParam<MooseEnum>("solve_type",
1026  solve_type,
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");
1032 
1033  MooseEnum mffd_type("wp ds", "wp");
1034  params.addParam<MooseEnum>("mffd_type",
1035  mffd_type,
1036  "Specifies the finite differencing type for "
1037  "Jacobian-free solve types. Note that the "
1038  "default is wp (for Walker and Pernice).");
1039 
1040  params.addParam<MultiMooseEnum>(
1041  "petsc_options", getCommonPetscFlags(), "Singleton PETSc options");
1042  params.addParam<MultiMooseEnum>(
1043  "petsc_options_iname", getCommonPetscKeys(), "Names of PETSc name/value pairs");
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 "
1048  "mffd_type",
1049  "PETSc");
1050 
1051  return params;
1052 }
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters emptyInputParameters()
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags) ...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...

◆ isSNESVI()

bool Moose::PetscSupport::isSNESVI ( FEProblemBase fe_problem)

check if SNES type is variational inequalities (VI) solver

Definition at line 1115 of file PetscSupport.C.

Referenced by BoundsBase::initialSetup().

1116 {
1117  const PetscOptions & petsc = fe_problem.getPetscOptions();
1118 
1119  int argc;
1120  char ** args;
1121  LibmeshPetscCallA(fe_problem.comm().get(), PetscGetArgs(&argc, &args));
1122 
1123  std::vector<std::string> cml_arg;
1124  for (int i = 0; i < argc; i++)
1125  cml_arg.push_back(args[i]);
1126 
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())
1133  return false;
1134 
1135  return true;
1136 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
const Parallel::Communicator & comm() const

◆ outputNorm()

void Moose::PetscSupport::outputNorm ( libMesh::Real  old_norm,
libMesh::Real  norm,
bool  use_color = false 
)

Helper function for outputting the norm values with/without color.

◆ petscLinearConverged()

PetscErrorCode Moose::PetscSupport::petscLinearConverged ( KSP  ,
PetscInt  it,
PetscReal  ,
KSPConvergedReason *  reason,
void ctx 
)

Definition at line 464 of file PetscSupport.C.

Referenced by petscSetDefaults().

466 {
468  FEProblemBase & problem = *static_cast<FEProblemBase *>(ctx);
469 
470  // execute objects that may be used in convergence check
471  // Right now, setting objects to execute on this flag would be ignored except in the
472  // linear-system-only use case.
474 
475  // perform the convergence check
477  if (problem.getFailNextSystemConvergenceCheck())
478  {
481  }
482  else
483  {
484  auto & convergence = problem.getConvergence(
485  problem.getLinearConvergenceNames()[problem.currentLinearSystem().number()]);
486  status = convergence.checkConvergence(it);
487  }
488 
489  // convert convergence status to PETSc converged reason
490  switch (status)
491  {
493  *reason = KSP_CONVERGED_ITERATING;
494  break;
495 
496  // TODO: find a KSP code that works better for this case
498 #if PETSC_VERSION_LESS_THAN(3, 24, 0)
499  *reason = KSP_CONVERGED_RTOL_NORMAL;
500 #else
501  *reason = KSP_CONVERGED_RTOL_NORMAL_EQUATIONS;
502 #endif
503  break;
504 
506  *reason = KSP_DIVERGED_DTOL;
507  break;
508  }
509 
510  PetscFunctionReturn(PETSC_SUCCESS);
511 }
const ExecFlagType EXEC_LINEAR_CONVERGENCE
Definition: Moose.C:32
virtual MooseConvergenceStatus checkConvergence(unsigned int iter)=0
Returns convergence status.
PetscFunctionBegin
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
MPI_Status status
virtual void execute(const ExecFlagType &exec_type)
Convenience function for performing execution of MOOSE systems.
virtual Convergence & getConvergence(const std::string &name, const THREAD_ID tid=0) const
Gets a Convergence object.
PetscFunctionReturn(PETSC_SUCCESS)
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1158
bool getFailNextSystemConvergenceCheck() const
Whether it will fail the next system convergence check(s), triggering failed step behavior...
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition: Convergence.h:33
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.
void * ctx
const std::vector< ConvergenceName > & getLinearConvergenceNames() const
Gets the linear convergence object name(s).

◆ petscLinearMonitor()

PetscErrorCode Moose::PetscSupport::petscLinearMonitor ( KSP  ,
PetscInt  its,
PetscReal  rnorm,
void void_ptr 
)

Helper function for displaying the linear residual during PETSC solve.

◆ petscNonlinearConverged()

PetscErrorCode Moose::PetscSupport::petscNonlinearConverged ( SNES  ,
PetscInt  it,
PetscReal  ,
PetscReal  ,
PetscReal  ,
SNESConvergedReason *  reason,
void ctx 
)

Definition at line 416 of file PetscSupport.C.

Referenced by petscSetDefaults().

423 {
425  FEProblemBase & problem = *static_cast<FEProblemBase *>(ctx);
426 
427  // execute objects that may be used in convergence check
429 
430  // perform the convergence check
433  {
436  }
437  else
438  {
439  auto & convergence = problem.getConvergence(
441  status = convergence.checkConvergence(it);
442  }
443 
444  // convert convergence status to PETSc converged reason
445  switch (status)
446  {
448  *reason = SNES_CONVERGED_ITERATING;
449  break;
450 
452  *reason = SNES_CONVERGED_FNORM_ABS;
453  break;
454 
456  *reason = SNES_DIVERGED_DTOL;
457  break;
458  }
459 
460  PetscFunctionReturn(PETSC_SUCCESS);
461 }
void resetFailNextNonlinearConvergenceCheck()
Tell the problem that the nonlinear convergence check(s) may proceed as normal.
const std::vector< ConvergenceName > & getNonlinearConvergenceNames() const
Gets the nonlinear system convergence object name(s).
const ExecFlagType EXEC_NONLINEAR_CONVERGENCE
Definition: Moose.C:34
virtual MooseConvergenceStatus checkConvergence(unsigned int iter)=0
Returns convergence status.
PetscFunctionBegin
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
MPI_Status status
virtual void execute(const ExecFlagType &exec_type)
Convenience function for performing execution of MOOSE systems.
virtual Convergence & getConvergence(const std::string &name, const THREAD_ID tid=0) const
Gets a Convergence object.
NonlinearSystemBase & currentNonlinearSystem()
PetscFunctionReturn(PETSC_SUCCESS)
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1158
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition: Convergence.h:33
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check(s) ...
void * ctx

◆ petscSetDefaultKSPNormType()

void Moose::PetscSupport::petscSetDefaultKSPNormType ( FEProblemBase problem,
KSP  ksp 
)

Set norm type.

Definition at line 552 of file PetscSupport.C.

Referenced by Moose::SlepcSupport::mooseSlepcEPSSNESKSPSetPCSide(), and petscSetKSPDefaults().

553 {
554  for (const auto i : make_range(problem.numSolverSystems()))
555  {
556  SolverSystem & sys = problem.getSolverSystem(i);
557  LibmeshPetscCallA(problem.comm().get(),
558  KSPSetNormType(ksp, getPetscKSPNormType(sys.getMooseKSPNormType())));
559  }
560 }
KSPNormType getPetscKSPNormType(Moose::MooseKSPNormType kspnorm)
Definition: PetscSupport.C:531
const Parallel::Communicator & comm() const
IntRange< T > make_range(T beg, T end)
SolverSystem & getSolverSystem(unsigned int sys_num)
Get non-constant reference to a solver system.
Moose::MooseKSPNormType getMooseKSPNormType()
Get the norm in which the linear convergence is measured.
Definition: SolverSystem.h:102
virtual std::size_t numSolverSystems() const override

◆ petscSetDefaultPCSide()

void Moose::PetscSupport::petscSetDefaultPCSide ( FEProblemBase problem,
KSP  ksp 
)

Setup which side we want to apply preconditioner.

Definition at line 563 of file PetscSupport.C.

Referenced by Moose::SlepcSupport::mooseSlepcEPSSNESKSPSetPCSide(), and petscSetKSPDefaults().

564 {
565  for (const auto i : make_range(problem.numSolverSystems()))
566  {
567  SolverSystem & sys = problem.getSolverSystem(i);
568 
569  // PETSc 3.2.x+
570  if (sys.getPCSide() != Moose::PCS_DEFAULT)
571  LibmeshPetscCallA(problem.comm().get(), KSPSetPCSide(ksp, getPetscPCSide(sys.getPCSide())));
572  }
573 }
const Parallel::Communicator & comm() const
PCSide getPetscPCSide(Moose::PCSideType pcs)
Definition: PetscSupport.C:514
Use whatever we have in PETSc.
Definition: MooseTypes.h:873
IntRange< T > make_range(T beg, T end)
SolverSystem & getSolverSystem(unsigned int sys_num)
Get non-constant reference to a solver system.
virtual std::size_t numSolverSystems() const override
Moose::PCSideType getPCSide()
Get the current preconditioner side.
Definition: SolverSystem.h:91

◆ petscSetDefaults()

void Moose::PetscSupport::petscSetDefaults ( FEProblemBase problem)

Sets the default options for PETSc.

Definition at line 598 of file PetscSupport.C.

Referenced by FEProblemBase::initPetscOutputAndSomeSolverSettings(), Moose::setSolverDefaults(), and FEProblemBase::solveLinearSystem().

599 {
600  // Apply matrix-type options once the per-system matrix prefixes are known. This is different
601  // from vectors: libMesh/PETSc vector construction already sees a global '-vec_type' option,
602  // but prefixed matrix options such as '-nl0_mat_type' cannot match anything until we set the
603  // matrix prefix here. Without this, a matrix may be constructed with the default type and keep
604  // that type for the rest of the solve, unless we not only set the options prefix but also apply
605  // the options to the matrix in this function call.
606  applyMatrixTypeOptions(problem);
607 
608  for (const auto nl_index : make_range(problem.numNonlinearSystems()))
609  {
610  NonlinearSystemBase & nl = problem.getNonlinearSystemBase(nl_index);
611 
612  // dig out PETSc solver
613  auto * const petsc_solver = cast_ptr<PetscNonlinearSolver<Number> *>(nl.nonlinearSolver());
614 
615  // Ensure we properly prefix SNES which in turn prefixes its KSP
616  const char * snes_prefix = nullptr;
617  std::string snes_prefix_str;
618  if (nl.system().prefix_with_name())
619  {
620  snes_prefix_str = nl.system().prefix();
621  snes_prefix = snes_prefix_str.c_str();
622  }
623  SNES snes = petsc_solver->snes(snes_prefix);
624  KSP ksp;
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));
628  LibmeshPetscCallA(
629  nl.comm().get(),
630  SNESSetConvergenceTest(snes, petscNonlinearConverged, &problem, LIBMESH_PETSC_NULLPTR));
631 
632  petscSetKSPDefaults(problem, ksp);
633  }
634 
635  for (auto sys_index : make_range(problem.numLinearSystems()))
636  {
637  // dig out PETSc solver
638  LinearSystem & lin_sys = problem.getLinearSystem(sys_index);
639  auto & lm_lin_sys = lin_sys.linearImplicitSystem();
640  auto * const petsc_solver =
641  dynamic_cast<PetscLinearSolver<Number> *>(lm_lin_sys.get_linear_solver());
642  // Ensure we properly prefix KSP
643  if (lm_lin_sys.prefix_with_name())
644  petsc_solver->init(lm_lin_sys.prefix().c_str());
645  else
646  petsc_solver->init();
647  // The KSP call here would initialize without a prefix if we hadn't "manually" performed
648  // initialization above
649  KSP ksp = petsc_solver->ksp();
650 
651  if (problem.hasLinearConvergenceObjects())
652  LibmeshPetscCallA(
653  lin_sys.comm().get(),
654  KSPSetConvergenceTest(ksp, petscLinearConverged, &problem, LIBMESH_PETSC_NULLPTR));
655 
656  // We dont set the KSP defaults here because they seem to clash with the linear solve parameters
657  // set in FEProblemBase::solveLinearSystem
658  }
659 }
virtual std::size_t numNonlinearSystems() const override
libMesh::LinearImplicitSystem & linearImplicitSystem()
Return a reference to the stored linear implicit system.
Definition: LinearSystem.h:93
virtual void init(const char *name=nullptr) override
const Parallel::Communicator & comm() const
void petscSetKSPDefaults(FEProblemBase &problem, KSP ksp)
Set the default options for a KSP.
Definition: PetscSupport.C:576
virtual libMesh::NonlinearSolver< Number > * nonlinearSolver()=0
bool hasLinearConvergenceObjects() const
Whether we have linear convergence objects.
Nonlinear system to be solved.
PetscErrorCode petscLinearConverged(KSP, PetscInt it, PetscReal, KSPConvergedReason *reason, void *ctx)
Definition: PetscSupport.C:464
PetscErrorCode petscNonlinearConverged(SNES, PetscInt it, PetscReal, PetscReal, PetscReal, SNESConvergedReason *reason, void *ctx)
Definition: PetscSupport.C:416
std::string prefix() const
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
LinearSystem & getLinearSystem(unsigned int sys_num)
Get non-constant reference to a linear system.
IntRange< T > make_range(T beg, T end)
Linear system to be solved.
Definition: LinearSystem.h:39
virtual std::size_t numLinearSystems() const override
void prefix_with_name(bool value)
virtual libMesh::System & system() override
Get the reference to the libMesh system.

◆ petscSetKSPDefaults()

void Moose::PetscSupport::petscSetKSPDefaults ( FEProblemBase problem,
KSP  ksp 
)

Set the default options for a KSP.

Definition at line 576 of file PetscSupport.C.

Referenced by petscSetDefaults(), and setLinearSolverDefaults().

577 {
578  auto & es = problem.es();
579 
580  PetscReal rtol = es.parameters.get<Real>("linear solver tolerance");
581  PetscReal atol = es.parameters.get<Real>("linear solver absolute tolerance");
582 
583  // MOOSE defaults this to -1 for some dumb reason
584  if (atol < 0)
585  atol = 1e-50;
586 
587  PetscReal maxits = es.parameters.get<unsigned int>("linear solver maximum iterations");
588 
589  // 1e100 is because we don't use divtol currently
590  LibmeshPetscCallA(problem.comm().get(), KSPSetTolerances(ksp, rtol, atol, 1e100, maxits));
591 
592  petscSetDefaultPCSide(problem, ksp);
593 
594  petscSetDefaultKSPNormType(problem, ksp);
595 }
const Parallel::Communicator & comm() const
void petscSetDefaultKSPNormType(FEProblemBase &problem, KSP ksp)
Set norm type.
Definition: PetscSupport.C:552
const T & get(std::string_view) const
virtual libMesh::EquationSystems & es() override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void petscSetDefaultPCSide(FEProblemBase &problem, KSP ksp)
Setup which side we want to apply preconditioner.
Definition: PetscSupport.C:563

◆ petscSetOptions() [1/2]

void Moose::PetscSupport::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.

This interface function should be used when setting options on a per-system basis

Definition at line 378 of file PetscSupport.C.

Referenced by Moose::SlepcSupport::slepcSetOptions(), FEProblemBase::solve(), and FEProblemBase::solveLinearSystem().

381 {
382  PetscCallAbort(PETSC_COMM_WORLD, PetscOptionsClear(LIBMESH_PETSC_NULLPTR));
383  setSolverOptions(solver_params, po.dont_add_these_options);
384  petscSetOptionsHelper(po, problem);
385 }
void setSolverOptions(const SolverParams &solver_params, const MultiMooseEnum &dont_add_these_options)
Definition: PetscSupport.C:263
void petscSetOptionsHelper(const PetscOptions &po, FEProblemBase *const problem)
Definition: PetscSupport.C:358

◆ petscSetOptions() [2/2]

void Moose::PetscSupport::petscSetOptions ( const PetscOptions po,
const std::vector< SolverParams > &  solver_params,
FEProblemBase problem 
)

A function for setting the PETSc options in PETSc from the options supplied to MOOSE.

This interface function should be used for setting options all at once for all systems in a multi-system context. Note that PetscOptions is not a vector because the options database has prefixes for the different systems

Definition at line 388 of file PetscSupport.C.

391 {
392  PetscCallAbort(PETSC_COMM_WORLD, PetscOptionsClear(LIBMESH_PETSC_NULLPTR));
393  for (const auto & solver_params : solver_params_vec)
394  setSolverOptions(solver_params, po.dont_add_these_options);
395  petscSetOptionsHelper(po, problem);
396 }
void setSolverOptions(const SolverParams &solver_params, const MultiMooseEnum &dont_add_these_options)
Definition: PetscSupport.C:263
void petscSetOptionsHelper(const PetscOptions &po, FEProblemBase *const problem)
Definition: PetscSupport.C:358

◆ petscSetOptionsHelper()

void Moose::PetscSupport::petscSetOptionsHelper ( const PetscOptions po,
FEProblemBase *const  problem 
)

Definition at line 358 of file PetscSupport.C.

Referenced by petscSetOptions().

359 {
360  // Add any additional options specified in the input file
361  for (const auto & flag : po.flags)
362  // Need to use name method here to pass a str instead of an EnumItem because
363  // we don't care if the id attributes match
364  if (!po.dont_add_these_options.contains(flag.name()) ||
365  po.user_set_options.contains(flag.name()))
366  setSinglePetscOption(flag.rawName().c_str());
367 
368  // Add option pairs
369  for (auto & option : po.pairs)
370  if (!po.dont_add_these_options.contains(option.first) ||
371  po.user_set_options.contains(option.first))
372  setSinglePetscOption(option.first, option.second, problem);
373 
375 }
void addPetscOptionsFromCommandline(FEProblemBase *const problem=nullptr)
Insert command-line PETSc options into the active PETSc options database.
Definition: PetscSupport.C:309
void setSinglePetscOption(const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
A wrapper function for dealing with different versions of PetscOptionsSetValue.

◆ petscSetupOutput()

PetscErrorCode Moose::PetscSupport::petscSetupOutput ( CommandLine cmd_line)

Definition at line 399 of file PetscSupport.C.

Referenced by MooseApp::executeExecutioner().

400 {
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)
405  {
406  if (arg.compare(code) == 0)
407  {
409  break;
410  }
411  }
412  PetscFunctionReturn(PETSC_SUCCESS);
413 }
static void petscSetupOutput()
Output string for setting up PETSC output.
Definition: Console.C:855
PetscFunctionBegin
PetscFunctionReturn(PETSC_SUCCESS)
const std::vector< std::string > & getArguments()
Definition: CommandLine.h:138

◆ processSingletonMooseWrappedOptions()

void Moose::PetscSupport::processSingletonMooseWrappedOptions ( FEProblemBase fe_problem,
const InputParameters params 
)

Process some MOOSE-wrapped PETSc options.

These options have no support for multi-system as indicated by the fact that this function takes no prefix nor solver system argument

Definition at line 662 of file PetscSupport.C.

Referenced by storePetscOptions().

663 {
664  setSolveTypeFromParams(fe_problem, params);
665  setLineSearchFromParams(fe_problem, params);
666  setMFFDTypeFromParams(fe_problem, params);
667 }
void setMFFDTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s matrix-free finite difference type from the input params.
Definition: PetscSupport.C:747
void setSolveTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s solve type from the input params.
Definition: PetscSupport.C:702
void setLineSearchFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s line search from the input params.
Definition: PetscSupport.C:715

◆ registerPetscCitation()

void Moose::PetscSupport::registerPetscCitation ( const std::string &  bibtex)

Register a BibTeX entry with PETSc's citation list so that it is printed (alongside the run-specific citations from any PETSc solvers/preconditioners used) when the PETSc -citations option is enabled.

The list is printed at PetscFinalize.

Definition at line 1338 of file PetscSupport.C.

Referenced by MooseApp::requestCitations().

1339 {
1340  // PETSc concatenates registered entries verbatim, so ensure a trailing newline to keep entries
1341  // separated. A null "set" flag registers unconditionally; callers deduplicate by citation key.
1342  const std::string entry = (!bibtex.empty() && bibtex.back() == '\n') ? bibtex : bibtex + "\n";
1343  LibmeshPetscCallA(PETSC_COMM_WORLD, PetscCitationsRegister(entry.c_str(), nullptr));
1344 }

◆ setConvergedReasonFlags()

void Moose::PetscSupport::setConvergedReasonFlags ( FEProblemBase fe_problem,
std::string  prefix 
)

Set flags that will instruct the user on the reason their simulation diverged from PETSc's perspective.

Parameters
fe_problemThe problem from which to retrieve the PETSc options
prefixThe prefix to add to the convergence flags. This should not contain a leading dash per PETSc prefix convention. Note that this function will immediately add said dash at the start of prefix so that calls to PetscOptionsSetValue work. This is the reason we pass prefix by value

Definition at line 804 of file PetscSupport.C.

Referenced by FEProblemSolve::FEProblemSolve().

805 {
806  prefix.insert(prefix.begin(), '-');
807  checkPrefix(prefix);
808  libmesh_ignore(fe_problem); // avoid unused warnings for old PETSc
809 
810 #if !PETSC_VERSION_LESS_THAN(3, 14, 0)
811  // the boolean in these pairs denote whether the user has specified any of the reason flags in the
812  // input file
813  std::array<std::string, 2> reason_flags = {{"snes_converged_reason", "ksp_converged_reason"}};
814 
815  auto & po = fe_problem.getPetscOptions();
816 
817  for (const auto & reason_flag : reason_flags)
818  {
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(),
822  po.pairs.end(),
823  [&full_flag](auto & pair)
824  { return pair.first == (full_flag); }) == po.pairs.end()))
825  po.pairs.emplace_back(full_flag, "::failed");
826  }
827 #endif
828 }
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
Definition: PetscSupport.h:53
void libmesh_ignore(const Args &...)

◆ setLinearSolverDefaults()

template<typename T >
void Moose::PetscSupport::setLinearSolverDefaults ( FEProblemBase problem,
libMesh::LinearSolver< T > &  linear_solver 
)

Set the defaults for a libMesh LinearSolver.

Used in explicit solves

Definition at line 98 of file PetscSupport.h.

Referenced by ExplicitTimeIntegrator::setupSolver().

99 {
100  petscSetKSPDefaults(problem,
101  libMesh::cast_ref<libMesh::PetscLinearSolver<T> &>(linear_solver).ksp());
102 }
Tnew cast_ref(Told &oldvar)
void petscSetKSPDefaults(FEProblemBase &problem, KSP ksp)
Set the default options for a KSP.
Definition: PetscSupport.C:576

◆ setLineSearchFromParams()

void Moose::PetscSupport::setLineSearchFromParams ( FEProblemBase fe_problem,
const InputParameters params 
)

Sets the FE problem's line search from the input params.

Definition at line 715 of file PetscSupport.C.

Referenced by processSingletonMooseWrappedOptions().

716 {
717  // Note: Options set in the Preconditioner block will override those set in the Executioner block
718  if (params.isParamValid("line_search"))
719  {
720  const auto & line_search = params.get<MooseEnum>("line_search");
721  for (const auto i : make_range(fe_problem.numNonlinearSystems()))
722  if (fe_problem.solverParams(i)._line_search == Moose::LS_INVALID || line_search != "default")
723  {
724  Moose::LineSearchType enum_line_search =
725  Moose::stringToEnum<Moose::LineSearchType>(line_search);
726  fe_problem.solverParams(i)._line_search = enum_line_search;
727  if (enum_line_search == LS_CONTACT || enum_line_search == LS_PROJECT)
728  {
729  NonlinearImplicitSystem * nl_system = dynamic_cast<NonlinearImplicitSystem *>(
730  &fe_problem.getNonlinearSystemBase(i).system());
731  if (!nl_system)
732  mooseError("You've requested a line search but you must be solving an EigenProblem. "
733  "These two things are not consistent.");
734  PetscNonlinearSolver<Real> * petsc_nonlinear_solver =
735  dynamic_cast<PetscNonlinearSolver<Real> *>(nl_system->nonlinear_solver.get());
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.");
739  petsc_nonlinear_solver->linesearch_object =
740  std::make_unique<ComputeLineSearchObjectWrapper>(fe_problem);
741  }
742  }
743  }
744 }
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
virtual std::size_t numNonlinearSystems() const override
Moose::LineSearchType _line_search
Definition: SolverParams.h:20
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
LineSearchType
Type of the line search.
Definition: MooseTypes.h:974
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
std::unique_ptr< ComputeLineSearchObject > linesearch_object
IntRange< T > make_range(T beg, T end)
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
means not set
Definition: MooseTypes.h:976
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
virtual libMesh::System & system() override
Get the reference to the libMesh system.

◆ setMFFDTypeFromParams()

void Moose::PetscSupport::setMFFDTypeFromParams ( FEProblemBase fe_problem,
const InputParameters params 
)

Sets the FE problem's matrix-free finite difference type from the input params.

Definition at line 747 of file PetscSupport.C.

Referenced by processSingletonMooseWrappedOptions().

748 {
749  if (params.isParamValid("mffd_type"))
750  {
751  const auto & mffd_type = params.get<MooseEnum>("mffd_type");
752  for (const auto i : make_range(fe_problem.numNonlinearSystems()))
753  fe_problem.solverParams(i)._mffd_type = Moose::stringToEnum<Moose::MffdType>(mffd_type);
754  }
755 }
virtual std::size_t numNonlinearSystems() const override
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
Moose::MffdType _mffd_type
Definition: SolverParams.h:21
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
IntRange< T > make_range(T beg, T end)
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

◆ setSinglePetscOption()

void Moose::PetscSupport::setSinglePetscOption ( const std::string &  name,
const std::string &  value = "",
FEProblemBase *const  problem = nullptr 
)

A wrapper function for dealing with different versions of PetscOptionsSetValue.

This is not generally called from MOOSE code, it is instead intended to be called by stuff in MOOSE::PetscSupport.

Definition at line 1139 of file PetscSupport.C.

Referenced by Moose::SlepcSupport::clearFreeNonlinearPowerIterations(), MFEMPetscNonlinearSolver::ConstructSolver(), FEProblemBase::FEProblemBase(), petscSetOptionsHelper(), CommandLine::printUsage(), MooseApp::requestCitations(), Moose::SlepcSupport::setEigenSolverOptions(), Moose::SlepcSupport::setFreeNonlinearPowerIterations(), Moose::SlepcSupport::setNewtonPetscOptions(), Moose::SlepcSupport::setNonlinearPowerOptions(), and setSinglePetscOptionIfAppropriate().

1142 {
1143  static const TIMPI::Communicator comm_world(PETSC_COMM_WORLD);
1144  const TIMPI::Communicator & comm = problem ? problem->comm() : comm_world;
1145  LibmeshPetscCallA(comm.get(),
1146  PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR,
1147  name.c_str(),
1148  value == "" ? LIBMESH_PETSC_NULLPTR : value.c_str()));
1149  // Create a single option data base so that we can use PETSC's internal option checking which
1150  // is case insensitive. This is better than re-implementing case-insensitive checks here in this
1151  // TU
1152  ::PetscOptions single_option;
1153  LibmeshPetscCallA(comm.get(), PetscOptionsCreate(&single_option));
1154  LibmeshPetscCallA(comm.get(),
1155  PetscOptionsSetValue(single_option,
1156  name.c_str(),
1157  value == "" ? LIBMESH_PETSC_NULLPTR : value.c_str()));
1158  auto check_problem = [problem, &name]()
1159  {
1160  if (!problem)
1161  mooseError(
1162  "Setting the option '",
1163  name,
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 "
1166  "discussions");
1167  };
1168 
1169  // Select vector type from user-passed PETSc options
1170  if (petscOptionsHasName(single_option, "-vec_type"))
1171  {
1172  check_problem();
1173  applyVectorTypeOptions(*problem);
1174  }
1175  // First do a cheap suffix check so unrelated PETSc options do not pay for looping over every
1176  // solver system. Once we know the name looks like a matrix-type option, rely on PETSc's
1177  // option lookup for the actual case-insensitive and prefix-aware matching.
1178  else if (problem && mightBeMatTypeOption(name))
1179  {
1180  errorOnUnprefixedMatTypeOption(single_option, *problem);
1181 
1182  for (const auto i : index_range(problem->_solver_systems))
1183  {
1184  const auto & solver_sys_name = problem->_solver_sys_names[i];
1185  if (!petscOptionsHasName(single_option, "-mat_type", solver_sys_name + "_"))
1186  continue;
1187 
1188  if (problem->solverParams(i)._type == Moose::ST_JFNK)
1189  mooseError("Setting option '", name, "' is incompatible with a JFNK 'solve_type'");
1190 
1191  applyMatrixTypeOptions(*problem, i);
1192  break;
1193  }
1194  }
1195 
1196  LibmeshPetscCallA(comm.get(), PetscOptionsDestroy(&single_option));
1197 }
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
const Parallel::Communicator & comm() const
std::vector< std::shared_ptr< SolverSystem > > _solver_systems
Combined container to base pointer of every solver system.
std::vector< SolverSystemName > _solver_sys_names
The union of nonlinear and linear system names.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Jacobian-Free Newton Krylov.
Definition: MooseTypes.h:894
Moose::SolveType _type
Definition: SolverParams.h:19
communicator & get()
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
auto index_range(const T &sizable)

◆ setSinglePetscOptionIfAppropriate()

void Moose::PetscSupport::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 simulation type, e.g.

if name is contained within dont_add_these_options

Definition at line 1200 of file PetscSupport.C.

Referenced by Moose::SlepcSupport::setEigenProblemOptions(), Moose::SlepcSupport::setSlepcEigenSolverTolerances(), setSolverOptions(), Moose::SlepcSupport::setWhichEigenPairsOptions(), and Moose::SlepcSupport::slepcSetOptions().

1204 {
1205  if (!dont_add_these_options.contains(name))
1206  setSinglePetscOption(name, value, problem);
1207 }
bool contains(const std::string &value) const
Methods for seeing if a value is set in 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.

◆ setSolverOptions()

void Moose::PetscSupport::setSolverOptions ( const SolverParams solver_params,
const MultiMooseEnum dont_add_these_options 
)

Definition at line 263 of file PetscSupport.C.

Referenced by petscSetOptions().

264 {
265  const auto prefix_with_dash = '-' + solver_params._prefix;
266  // set PETSc options implied by a solve type
267  switch (solver_params._type)
268  {
269  case Moose::ST_PJFNK:
270  setSinglePetscOptionIfAppropriate(dont_add_these_options,
271  prefix_with_dash + "snes_mf_operator");
272  setSinglePetscOptionIfAppropriate(dont_add_these_options,
273  prefix_with_dash + "mat_mffd_type",
274  stringify(solver_params._mffd_type));
275  break;
276 
277  case Moose::ST_JFNK:
278  setSinglePetscOptionIfAppropriate(dont_add_these_options, prefix_with_dash + "snes_mf");
279  setSinglePetscOptionIfAppropriate(dont_add_these_options,
280  prefix_with_dash + "mat_mffd_type",
281  stringify(solver_params._mffd_type));
282  break;
283 
284  case Moose::ST_NEWTON:
285  break;
286 
287  case Moose::ST_FD:
288  setSinglePetscOptionIfAppropriate(dont_add_these_options, prefix_with_dash + "snes_fd");
289  break;
290 
291  case Moose::ST_LINEAR:
293  dont_add_these_options, prefix_with_dash + "snes_type", "ksponly");
294  setSinglePetscOptionIfAppropriate(dont_add_these_options,
295  prefix_with_dash + "snes_monitor_cancel");
296  break;
297  }
298 
299  Moose::LineSearchType ls_type = solver_params._line_search;
300  if (ls_type == Moose::LS_NONE)
301  ls_type = Moose::LS_BASIC;
302 
303  if (ls_type != Moose::LS_DEFAULT && ls_type != Moose::LS_CONTACT && ls_type != Moose::LS_PROJECT)
305  dont_add_these_options, prefix_with_dash + "snes_linesearch_type", stringify(ls_type));
306 }
Full Newton Solve.
Definition: MooseTypes.h:895
std::string _prefix
Definition: SolverParams.h:35
Moose::LineSearchType _line_search
Definition: SolverParams.h:20
Solving a linear problem.
Definition: MooseTypes.h:897
Moose::MffdType _mffd_type
Definition: SolverParams.h:21
LineSearchType
Type of the line search.
Definition: MooseTypes.h:974
std::string stringify(const MffdType &t)
Definition: PetscSupport.C:248
Jacobian-Free Newton Krylov.
Definition: MooseTypes.h:894
Moose::SolveType _type
Definition: SolverParams.h:19
Use finite differences to compute Jacobian.
Definition: MooseTypes.h:896
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&#39;t make sense for the current si...
Preconditioned Jacobian-Free Newton Krylov.
Definition: MooseTypes.h:893

◆ setSolveTypeFromParams()

void Moose::PetscSupport::setSolveTypeFromParams ( FEProblemBase fe_problem,
const InputParameters params 
)

Sets the FE problem's solve type from the input params.

Definition at line 702 of file PetscSupport.C.

Referenced by MoosePreconditioner::MoosePreconditioner(), and processSingletonMooseWrappedOptions().

703 {
704  // Note: Options set in the Preconditioner block will override those set in the Executioner block
705  if (params.isParamValid("solve_type") && !params.isParamValid("_use_eigen_value"))
706  {
707  // Extract the solve type
708  const std::string & solve_type = params.get<MooseEnum>("solve_type");
709  for (const auto i : make_range(fe_problem.numNonlinearSystems()))
710  fe_problem.solverParams(i)._type = Moose::stringToEnum<Moose::SolveType>(solve_type);
711  }
712 }
virtual std::size_t numNonlinearSystems() const override
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
Moose::SolveType _type
Definition: SolverParams.h:19
IntRange< T > make_range(T beg, T end)
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

◆ storePetscOptions()

void Moose::PetscSupport::storePetscOptions ( FEProblemBase fe_problem,
const std::string &  prefix,
const ParallelParamObject param_object 
)

Stores the PETSc options supplied from the parameter object on the problem.

Parameters
fe_problemThe problem on which we will store the parameters
prefixA prefix to apply to all the parameter object's PETSc options. This should either be a single character '-' or a string like "-foo_" where the trailing '_' is required
param_objectThe parameter object potentially holding PETSc options String prefixes may be used to select the system the parameters is applied to

Definition at line 677 of file PetscSupport.C.

Referenced by FEProblemSolve::FEProblemSolve(), MooseStaticCondensationPreconditioner::initialSetup(), MoosePreconditioner::initialSetup(), and Split::setup().

680 {
681  const auto & params = param_object.parameters();
682  processSingletonMooseWrappedOptions(fe_problem, params);
683 
684  // The parameters contained in the Action
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");
688 
689  // A reference to the PetscOptions object that contains the settings that will be used in the
690  // solve
691  auto & po = fe_problem.getPetscOptions();
692 
693  // First process the single petsc options/flags
694  addPetscFlagsToPetscOptions(petsc_options, prefix, param_object, po);
695 
696  // Then process the option-value pairs
698  petsc_pair_options, fe_problem.mesh().dimension(), prefix, param_object, po);
699 }
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 &param_object, PetscOptions &petsc_options)
Populate flags in a given PetscOptions object using a vector of input arguments.
Definition: PetscSupport.C:767
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options, const unsigned int mesh_dimension, std::string prefix, const ParallelParamObject &param_object, PetscOptions &petsc_options)
Populate name and value pairs in a given PetscOptions object using vectors of input arguments...
Definition: PetscSupport.C:831
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
void processSingletonMooseWrappedOptions(FEProblemBase &fe_problem, const InputParameters &params)
Process some MOOSE-wrapped PETSc options.
Definition: PetscSupport.C:662
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition: MooseMesh.C:3012
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve the id of an item from the MultiMooseEnum. ...
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
virtual MooseMesh & mesh() override
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...

◆ storePetscOptionsFromParams()

void Moose::PetscSupport::storePetscOptionsFromParams ( FEProblemBase fe_problem,
const InputParameters params 
)

Stores the Petsc flags and pair options fron the input params in the given PetscOptions object.

◆ stringify() [1/2]

std::string Moose::PetscSupport::stringify ( const LineSearchType t)

Definition at line 219 of file PetscSupport.C.

220 {
221  switch (t)
222  {
223  case LS_BASIC:
224  return "basic";
225  case LS_DEFAULT:
226  return "default";
227  case LS_NONE:
228  return "none";
229  case LS_SHELL:
230  return "shell";
231  case LS_L2:
232  return "l2";
233  case LS_BT:
234  return "bt";
235  case LS_CP:
236  return "cp";
237  case LS_CONTACT:
238  return "contact";
239  case LS_PROJECT:
240  return "project";
241  case LS_INVALID:
242  mooseError("Invalid LineSearchType");
243  }
244  return "";
245 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
means not set
Definition: MooseTypes.h:976

◆ stringify() [2/2]

std::string Moose::PetscSupport::stringify ( const MffdType t)

Definition at line 248 of file PetscSupport.C.

Referenced by setSolverOptions().

249 {
250  switch (t)
251  {
252  case MFFD_WP:
253  return "wp";
254  case MFFD_DS:
255  return "ds";
256  case MFFD_INVALID:
257  mooseError("Invalid MffdType");
258  }
259  return "";
260 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
means not set
Definition: MooseTypes.h:993