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 Moose
32 {
33 namespace PetscSupport
34 {
35 
40 {
41 public:
43  : flags("", "", true), dont_add_these_options("", "", true), user_set_options("", "", true)
44  {
45  }
46 
48  std::vector<std::pair<std::string, std::string>> pairs;
49 
52 
55 
58 
60  std::string pc_description;
61 };
62 
67 void petscSetOptions(const PetscOptions & po,
68  const SolverParams & solver_params,
69  FEProblemBase * const problem = nullptr);
70 
77 void petscSetOptions(const PetscOptions & po,
78  const std::vector<SolverParams> & solver_params,
79  FEProblemBase * problem);
80 
84 void petscSetKSPDefaults(FEProblemBase & problem, KSP ksp);
85 
91 template <typename T>
92 void
94 {
95  petscSetKSPDefaults(problem,
96  libMesh::cast_ref<libMesh::PetscLinearSolver<T> &>(linear_solver).ksp());
97 }
98 
102 void petscSetDefaults(FEProblemBase & problem);
103 
107 void petscSetupDM(NonlinearSystemBase & nl, const std::string & dm_name);
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 
144 void setConvergedReasonFlags(FEProblemBase & fe_problem, const std::string & prefix);
145 
149 void setSolveTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
150 
154 void setLineSearchFromParams(FEProblemBase & fe_problem, const InputParameters & params);
155 
159 void setMFFDTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
160 
164 void storePetscOptionsFromParams(FEProblemBase & fe_problem, const InputParameters & params);
165 
173 void addPetscFlagsToPetscOptions(const MultiMooseEnum & petsc_flags,
174  const std::string & prefix,
175  const ParallelParamObject & param_object,
176  PetscOptions & petsc_options);
177 
187  const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options,
188  const unsigned int mesh_dimension,
189  const std::string & prefix,
190  const ParallelParamObject & param_object,
191  PetscOptions & petsc_options);
192 
196 std::set<std::string> getPetscValidLineSearches();
197 
206 
209 
212 
215 
218 
221 
224 
226 bool isSNESVI(FEProblemBase & fe_problem);
227 
234 void setSinglePetscOption(const std::string & name,
235  const std::string & value = "",
236  FEProblemBase * const problem = nullptr);
237 
242 void setSinglePetscOptionIfAppropriate(const MultiMooseEnum & dont_add_these_options,
243  const std::string & name,
244  const std::string & value = "",
245  FEProblemBase * const problem = nullptr);
246 
248 
252 void petscSetDefaultPCSide(FEProblemBase & problem, KSP ksp);
253 
257 void petscSetDefaultKSPNormType(FEProblemBase & problem, KSP ksp);
258 
265 void colorAdjacencyMatrix(PetscScalar * adjacency_matrix,
266  unsigned int size,
267  unsigned int colors,
268  std::vector<unsigned int> & vertex_colors,
269  const char * coloring_algorithm);
270 
275 void dontAddPetscFlag(const std::string & flag, PetscOptions & petsc_options);
276 
282 
288 
293 void dontAddCommonKSPOptions(FEProblemBase & fe_problem);
294 
299 void dontAddCommonSNESOptions(FEProblemBase & fe_problem);
300 
309 std::unique_ptr<PetscMatrix<Number>>
311  Mat & petsc_mat,
312  const std::string & binary_mat_file,
313  unsigned int mat_number_to_load = 1);
314 
315 #define SNESGETLINESEARCH SNESGetLineSearch
316 }
317 }
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys) ...
Definition: PetscSupport.C:996
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
bool isSNESVI(FEProblemBase &fe_problem)
check if SNES type is variational inequalities (VI) solver
void addPetscOptionsFromCommandline()
Definition: PetscSupport.C:233
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:658
std::set< std::string > getPetscValidLineSearches()
Returns the valid petsc line search options as a set of strings.
Definition: PetscSupport.C:919
void setLinearSolverDefaults(FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
Set the defaults for a libMesh LinearSolver.
Definition: PetscSupport.h:93
void petscSetupDM(NonlinearSystemBase &nl, const std::string &dm_name)
Setup the PETSc DM object.
Definition: PetscSupport.C:190
void petscSetDefaults(FEProblemBase &problem)
Sets the default options for PETSc.
Definition: PetscSupport.C:488
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags) ...
Definition: PetscSupport.C:960
Tnew cast_ref(Told &oldvar)
A struct for storing the various types of petsc options and values.
Definition: PetscSupport.h:39
MultiMooseEnum flags
Single value PETSc options (flags)
Definition: PetscSupport.h:51
void setSolveTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s solve type from the input params.
Definition: PetscSupport.C:613
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:442
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:466
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
Definition: PetscSupport.h:48
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:572
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:978
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:272
void setConvergedReasonFlags(FEProblemBase &fe_problem, const std::string &prefix)
Set flags that will instruct the user on the reason their simulation diverged from PETSc&#39;s perspectiv...
Definition: PetscSupport.C:714
void setLineSearchFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem&#39;s line search from the input params.
Definition: PetscSupport.C:626
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:971
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:925
PetscErrorCode petscSetupOutput(CommandLine *cmd_line)
Definition: PetscSupport.C:293
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options, const unsigned int mesh_dimension, const 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:737
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:987
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 addPetscFlagsToPetscOptions(const MultiMooseEnum &petsc_flags, const 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:678
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:588
std::string pc_description
Preconditioner description.
Definition: PetscSupport.h:60
MultiMooseEnum dont_add_these_options
Flags to explicitly not set, even if they are specified programmatically.
Definition: PetscSupport.h:54
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:57
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:453