https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
25class FEProblemBase;
27class CommandLine;
28class InputParameters;
30
31namespace libMesh
32{
33class DofMapBase;
34}
35
36namespace Moose
37{
38namespace PetscSupport
39{
40
45{
46public:
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{
73public:
74 explicit PetscOptionsScope(FEProblemBase & problem);
76
79
80private:
83
85 bool _pushed;
86};
87
92void petscSetOptions(const PetscOptions & po,
93 const SolverParams & solver_params,
94 FEProblemBase * const problem = nullptr);
95
102void petscSetOptions(const PetscOptions & po,
103 const std::vector<SolverParams> & solver_params,
104 FEProblemBase * problem);
105
109void petscSetKSPDefaults(FEProblemBase & problem, KSP ksp);
110
116template <typename T>
117void
119{
120 petscSetKSPDefaults(problem,
121 libMesh::cast_ref<libMesh::PetscLinearSolver<T> &>(linear_solver).ksp());
122}
123
127void petscSetDefaults(FEProblemBase & problem);
128
129PetscErrorCode petscSetupOutput(CommandLine * cmd_line);
130
134void outputNorm(libMesh::Real old_norm, libMesh::Real norm, bool use_color = false);
135
139PetscErrorCode petscLinearMonitor(KSP /*ksp*/, PetscInt its, PetscReal rnorm, void * void_ptr);
140
146 const InputParameters & params);
147
156void storePetscOptions(FEProblemBase & fe_problem,
157 const std::string & prefix,
158 const ParallelParamObject & param_object);
159
169void setConvergedReasonFlags(FEProblemBase & fe_problem, std::string prefix);
170
174void setSolveTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
175
179void setLineSearchFromParams(FEProblemBase & fe_problem, const InputParameters & params);
180
184void setMFFDTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
185
190
201void addPetscFlagsToPetscOptions(const MultiMooseEnum & petsc_flags,
202 std::string prefix,
203 const ParallelParamObject & param_object,
204 PetscOptions & petsc_options);
205
218 const std::vector<std::pair<MooseEnumItem, std::string>> & petsc_pair_options,
219 const unsigned int mesh_dimension,
220 std::string prefix,
221 const ParallelParamObject & param_object,
222 PetscOptions & petsc_options);
223
227std::set<std::string> getPetscValidLineSearches();
228
237
240
243
246
249
252
255
257bool isSNESVI(FEProblemBase & fe_problem);
258
265void setSinglePetscOption(const std::string & name,
266 const std::string & value = "",
267 FEProblemBase * const problem = nullptr);
268
273void setSinglePetscOptionIfAppropriate(const MultiMooseEnum & dont_add_these_options,
274 const std::string & name,
275 const std::string & value = "",
276 FEProblemBase * const problem = nullptr);
277
283void registerPetscCitation(const std::string & bibtex);
284
292void addPetscOptionsFromCommandline(FEProblemBase * const problem = nullptr);
293
297void petscSetDefaultPCSide(FEProblemBase & problem, KSP ksp);
298
302void petscSetDefaultKSPNormType(FEProblemBase & problem, KSP ksp);
303
310void colorAdjacencyMatrix(PetscScalar * adjacency_matrix,
311 unsigned int size,
312 unsigned int colors,
313 std::vector<unsigned int> & vertex_colors,
314 const char * coloring_algorithm);
315
320void dontAddPetscFlag(const std::string & flag, PetscOptions & petsc_options);
321
327
333
338void dontAddCommonKSPOptions(FEProblemBase & fe_problem);
339
344void dontAddCommonSNESOptions(FEProblemBase & fe_problem);
345
351void dontAddCommonSNESOptions(FEProblemBase & fe_problem, const std::string & prefix);
352
361std::unique_ptr<PetscMatrix<Number>>
363 Mat & petsc_mat,
364 const std::string & binary_mat_file,
365 unsigned int mat_number_to_load = 1);
366
367#define SNESGETLINESEARCH SNESGetLineSearch
368}
369}
This class wraps provides and tracks access to command line parameters.
Definition CommandLine.h:30
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Activates a problem's PETSc options database for the lifetime of this scope.
bool _pushed
Whether a database was pushed and therefore needs to be popped.
PetscOptionsScope & operator=(const PetscOptionsScope &)=delete
FEProblemBase & _problem
Problem whose PETSc options database is activated.
PetscOptionsScope(const PetscOptionsScope &)=delete
A struct for storing the various types of petsc options and values.
MultiMooseEnum dont_add_these_options
Flags to explicitly not set, even if they are specified programmatically.
std::string pc_description
Preconditioner description.
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
MultiMooseEnum user_set_options
Options that are set by the user at the input level.
MultiMooseEnum flags
Single value PETSc options (flags)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
Nonlinear system to be solved.
Base class shared by both Action and MooseObject.
void dontAddLinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be l...
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.
PetscErrorCode petscSetupOutput(CommandLine *cmd_line)
void petscSetDefaultKSPNormType(FEProblemBase &problem, KSP ksp)
Set norm type.
void setConvergedReasonFlags(FEProblemBase &fe_problem, std::string prefix)
Set flags that will instruct the user on the reason their simulation diverged from PETSc's perspectiv...
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...
void setMFFDTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem's matrix-free finite difference type from the input params.
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc 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 ...
void setLinearSolverDefaults(FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
Set the defaults for a libMesh LinearSolver.
MultiMooseEnum getCommonKSPKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys)
void processSingletonMooseWrappedOptions(FEProblemBase &fe_problem, const InputParameters &params)
Process some MOOSE-wrapped PETSc options.
bool isSNESVI(FEProblemBase &fe_problem)
check if SNES type is variational inequalities (VI) solver
void petscSetDefaults(FEProblemBase &problem)
Sets the default options for PETSc.
void dontAddCommonKSPOptions(FEProblemBase &fe_problem)
Function to ensure that common KSP options are not added to the PetscOptions storage object to be lat...
void petscSetKSPDefaults(FEProblemBase &problem, KSP ksp)
Set the default options for a KSP.
MultiMooseEnum getCommonSNESKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys)
void addPetscOptionsFromCommandline(FEProblemBase *const problem=nullptr)
Insert command-line PETSc options into the active PETSc options database.
MultiMooseEnum getCommonKSPFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags)
void outputNorm(libMesh::Real old_norm, libMesh::Real norm, bool use_color=false)
Helper function for outputting the norm values with/without color.
void petscSetDefaultPCSide(FEProblemBase &problem, KSP ksp)
Setup which side we want to apply preconditioner.
InputParameters getPetscValidParams()
Returns the PETSc options that are common between Executioners and Preconditioners.
void setSinglePetscOptionIfAppropriate(const MultiMooseEnum &dont_add_these_options, const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
Same as setSinglePetscOption, but does not set the option if it doesn't make sense for the current si...
void registerPetscCitation(const std::string &bibtex)
Register a BibTeX entry with PETSc's citation list so that it is printed (alongside the run-specific ...
void setSinglePetscOption(const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
A wrapper function for dealing with different versions of PetscOptionsSetValue.
std::set< std::string > getPetscValidLineSearches()
Returns the valid petsc line search options as a set of strings.
MultiMooseEnum getCommonSNESFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags)
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 setSolveTypeFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem's solve type from the input params.
void dontAddNonlinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be ...
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.
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
void setLineSearchFromParams(FEProblemBase &fe_problem, const InputParameters &params)
Sets the FE problem's line search from the input params.
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.
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 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.
PetscErrorCode petscLinearMonitor(KSP, PetscInt its, PetscReal rnorm, void *void_ptr)
Helper function for displaying the linear residual during PETSC solve.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Tnew cast_ref(Told &oldvar)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real