https://mooseframework.inl.gov
PetscSupport.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "libmesh/libmesh.h"
13 
14 // MOOSE includes
15 #include "SolverParams.h"
16 #include "MultiMooseEnum.h"
17 
18 #include "libmesh/petsc_macro.h"
19 #include "libmesh/linear_solver.h"
20 #include "libmesh/petsc_linear_solver.h"
21 
22 #include <petscksp.h>
23 
24 // Forward declarations
25 class FEProblemBase;
27 class CommandLine;
28 class InputParameters;
30 
31 namespace libMesh
32 {
33 class DofMapBase;
34 }
35 
36 namespace Moose
37 {
38 namespace PetscSupport
39 {
40 
45 {
46 public:
48  : flags("", "", true), dont_add_these_options("", "", true), user_set_options("", "", true)
49  {
50  }
51 
53  std::vector<std::pair<std::string, std::string>> pairs;
54 
57 
60 
63 
65  std::string pc_description;
66 };
67 
72 void petscSetOptions(const PetscOptions & po,
73  const SolverParams & solver_params,
74  FEProblemBase * const problem = nullptr);
75 
82 void petscSetOptions(const PetscOptions & po,
83  const std::vector<SolverParams> & solver_params,
84  FEProblemBase * problem);
85 
89 void petscSetKSPDefaults(FEProblemBase & problem, KSP ksp);
90 
96 template <typename T>
97 void
99 {
100  petscSetKSPDefaults(problem,
101  libMesh::cast_ref<libMesh::PetscLinearSolver<T> &>(linear_solver).ksp());
102 }
103 
107 void petscSetDefaults(FEProblemBase & problem);
108 
109 PetscErrorCode petscSetupOutput(CommandLine * cmd_line);
110 
114 void outputNorm(libMesh::Real old_norm, libMesh::Real norm, bool use_color = false);
115 
119 PetscErrorCode petscLinearMonitor(KSP /*ksp*/, PetscInt its, PetscReal rnorm, void * void_ptr);
120 
126  const InputParameters & params);
127 
136 void storePetscOptions(FEProblemBase & fe_problem,
137  const std::string & prefix,
138  const ParallelParamObject & param_object);
139 
149 void setConvergedReasonFlags(FEProblemBase & fe_problem, std::string prefix);
150 
154 void setSolveTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
155 
159 void setLineSearchFromParams(FEProblemBase & fe_problem, const InputParameters & params);
160 
164 void setMFFDTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
165 
169 void storePetscOptionsFromParams(FEProblemBase & fe_problem, const InputParameters & params);
170 
181 void addPetscFlagsToPetscOptions(const MultiMooseEnum & petsc_flags,
182  std::string prefix,
183  const ParallelParamObject & param_object,
184  PetscOptions & petsc_options);
185 
198  const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options,
199  const unsigned int mesh_dimension,
200  std::string prefix,
201  const ParallelParamObject & param_object,
202  PetscOptions & petsc_options);
203 
207 std::set<std::string> getPetscValidLineSearches();
208 
217 
220 
223 
226 
229 
232 
235 
237 bool isSNESVI(FEProblemBase & fe_problem);
238 
245 void setSinglePetscOption(const std::string & name,
246  const std::string & value = "",
247  FEProblemBase * const problem = nullptr);
248 
253 void setSinglePetscOptionIfAppropriate(const MultiMooseEnum & dont_add_these_options,
254  const std::string & name,
255  const std::string & value = "",
256  FEProblemBase * const problem = nullptr);
257 
259 
263 void petscSetDefaultPCSide(FEProblemBase & problem, KSP ksp);
264 
268 void petscSetDefaultKSPNormType(FEProblemBase & problem, KSP ksp);
269 
276 void colorAdjacencyMatrix(PetscScalar * adjacency_matrix,
277  unsigned int size,
278  unsigned int colors,
279  std::vector<unsigned int> & vertex_colors,
280  const char * coloring_algorithm);
281 
286 void dontAddPetscFlag(const std::string & flag, PetscOptions & petsc_options);
287 
293 
299 
304 void dontAddCommonKSPOptions(FEProblemBase & fe_problem);
305 
310 void dontAddCommonSNESOptions(FEProblemBase & fe_problem);
311 
320 std::unique_ptr<PetscMatrix<Number>>
322  Mat & petsc_mat,
323  const std::string & binary_mat_file,
324  unsigned int mat_number_to_load = 1);
325 
326 #define SNESGETLINESEARCH SNESGetLineSearch
327 }
328 }
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) ...
Definition: PetscSupport.C:956
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
Definition: PetscSupport.C:964
bool isSNESVI(FEProblemBase &fe_problem)
check if SNES type is variational inequalities (VI) solver
Definition: PetscSupport.C:980
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:635
void addPetscOptionsFromCommandline()
Definition: PetscSupport.C:191
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:615
std::set< std::string > getPetscValidLineSearches()
Returns the valid petsc line search options as a set of strings.
Definition: PetscSupport.C:879
void setLinearSolverDefaults(FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
Set the defaults for a libMesh LinearSolver.
Definition: PetscSupport.h:98
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:696
void petscSetDefaults(FEProblemBase &problem)
Sets the default options for PETSc.
Definition: PetscSupport.C:446
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) ...
Definition: PetscSupport.C:920
Tnew cast_ref(Told &oldvar)
A struct for storing the various types of petsc options and values.
Definition: PetscSupport.h:44
MultiMooseEnum flags
Single value PETSc options (flags)
Definition: PetscSupport.h:56
void setSolveTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s solve type from the input params.
Definition: PetscSupport.C:570
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void petscSetDefaultKSPNormType(FEProblemBase &problem, KSP ksp)
Set norm type.
Definition: PetscSupport.C:400
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.
void petscSetKSPDefaults(FEProblemBase &problem, KSP ksp)
Set the default options for a KSP.
Definition: PetscSupport.C:424
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
Definition: PetscSupport.h:53
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:29
Nonlinear system to be solved.
void processSingletonMooseWrappedOptions(FEProblemBase &fe_problem, const InputParameters &params)
Process some MOOSE-wrapped PETSc options.
Definition: PetscSupport.C:530
void setConvergedReasonFlags(FEProblemBase &fe_problem, std::string prefix)
Set flags that will instruct the user on the reason their simulation diverged from PETSc&#39;s perspectiv...
Definition: PetscSupport.C:672
void dontAddCommonKSPOptions(FEProblemBase &fe_problem)
Function to ensure that common KSP options are not added to the PetscOptions storage object to be lat...
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags) ...
Definition: PetscSupport.C:938
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 ...
void outputNorm(libMesh::Real old_norm, libMesh::Real norm, bool use_color=false)
Helper function for outputting the norm values with/without color.
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.
Definition: PetscSupport.C:230
void setLineSearchFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s line search from the input params.
Definition: PetscSupport.C:583
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...
PetscErrorCode petscLinearMonitor(KSP, PetscInt its, PetscReal rnorm, void *void_ptr)
Helper function for displaying the linear residual during PETSC solve.
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) ...
Definition: PetscSupport.C:931
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
InputParameters getPetscValidParams()
Returns the PETSc options that are common between Executioners and Preconditioners.
Definition: PetscSupport.C:885
PetscErrorCode petscSetupOutput(CommandLine *cmd_line)
Definition: PetscSupport.C:251
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...
MultiMooseEnum getCommonSNESKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys) ...
Definition: PetscSupport.C:947
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...
void dontAddLinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be l...
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...
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.
Definition: PetscSupport.C:545
std::string pc_description
Preconditioner description.
Definition: PetscSupport.h:65
MultiMooseEnum dont_add_these_options
Flags to explicitly not set, even if they are specified programmatically.
Definition: PetscSupport.h:59
Base class shared by both Action and MooseObject.
MultiMooseEnum user_set_options
Options that are set by the user at the input level.
Definition: PetscSupport.h:62
void storePetscOptionsFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Stores the Petsc flags and pair options fron the input params in the given PetscOptions object...
void petscSetDefaultPCSide(FEProblemBase &problem, KSP ksp)
Setup which side we want to apply preconditioner.
Definition: PetscSupport.C:411