LCOV - code coverage report
Current view: top level - include/utils - PetscSupport.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 8601ad Lines: 7 7 100.0 %
Date: 2025-07-18 13:27:08 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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;
      26             : class NonlinearSystemBase;
      27             : class CommandLine;
      28             : class InputParameters;
      29             : class ParallelParamObject;
      30             : 
      31             : namespace Moose
      32             : {
      33             : namespace PetscSupport
      34             : {
      35             : 
      36             : /**
      37             :  * A struct for storing the various types of petsc options and values
      38             :  */
      39             : class PetscOptions
      40             : {
      41             : public:
      42       58142 :   PetscOptions()
      43       58142 :     : flags("", "", true), dont_add_these_options("", "", true), user_set_options("", "", true)
      44             :   {
      45       58142 :   }
      46             : 
      47             :   /// PETSc key-value pairs
      48             :   std::vector<std::pair<std::string, std::string>> pairs;
      49             : 
      50             :   /// Single value PETSc options (flags)
      51             :   MultiMooseEnum flags;
      52             : 
      53             :   /// Flags to explicitly not set, even if they are specified programmatically
      54             :   MultiMooseEnum dont_add_these_options;
      55             : 
      56             :   /// Options that are set by the user at the input level
      57             :   MultiMooseEnum user_set_options;
      58             : 
      59             :   /// Preconditioner description
      60             :   std::string pc_description;
      61             : };
      62             : 
      63             : /**
      64             :  * A function for setting the PETSc options in PETSc from the options supplied to MOOSE. This
      65             :  * interface function should be used when setting options on a per-system basis
      66             :  */
      67             : void petscSetOptions(const PetscOptions & po,
      68             :                      const SolverParams & solver_params,
      69             :                      FEProblemBase * const problem = nullptr);
      70             : 
      71             : /**
      72             :  * A function for setting the PETSc options in PETSc from the options supplied to MOOSE. This
      73             :  * interface function should be used for setting options all at once for all systems in a
      74             :  * multi-system context. Note that PetscOptions is not a vector because the options database has
      75             :  * prefixes for the different systems
      76             :  */
      77             : void petscSetOptions(const PetscOptions & po,
      78             :                      const std::vector<SolverParams> & solver_params,
      79             :                      FEProblemBase * problem);
      80             : 
      81             : /**
      82             :  * Set the default options for a KSP
      83             :  */
      84             : void petscSetKSPDefaults(FEProblemBase & problem, KSP ksp);
      85             : 
      86             : /**
      87             :  * Set the defaults for a libMesh LinearSolver
      88             :  *
      89             :  * Used in explicit solves
      90             :  */
      91             : template <typename T>
      92             : void
      93         300 : setLinearSolverDefaults(FEProblemBase & problem, libMesh::LinearSolver<T> & linear_solver)
      94             : {
      95         300 :   petscSetKSPDefaults(problem,
      96         300 :                       libMesh::cast_ref<libMesh::PetscLinearSolver<T> &>(linear_solver).ksp());
      97         300 : }
      98             : 
      99             : /**
     100             :  * Sets the default options for PETSc
     101             :  */
     102             : void petscSetDefaults(FEProblemBase & problem);
     103             : 
     104             : /**
     105             :  * Setup the PETSc DM object
     106             :  */
     107             : void petscSetupDM(NonlinearSystemBase & nl, const std::string & dm_name);
     108             : 
     109             : PetscErrorCode petscSetupOutput(CommandLine * cmd_line);
     110             : 
     111             : /**
     112             :  * Helper function for outputting the norm values with/without color
     113             :  */
     114             : void outputNorm(libMesh::Real old_norm, libMesh::Real norm, bool use_color = false);
     115             : 
     116             : /**
     117             :  * Helper function for displaying the linear residual during PETSC solve
     118             :  */
     119             : PetscErrorCode petscLinearMonitor(KSP /*ksp*/, PetscInt its, PetscReal rnorm, void * void_ptr);
     120             : 
     121             : /**
     122             :  * Process some MOOSE-wrapped PETSc options. These options have no support for multi-system as
     123             :  * indicated by the fact that this function takes no prefix nor solver system argument
     124             :  */
     125             : void processSingletonMooseWrappedOptions(FEProblemBase & fe_problem,
     126             :                                          const InputParameters & params);
     127             : 
     128             : /**
     129             :  * Stores the PETSc options supplied from the parameter object on the problem
     130             :  * @param fe_problem The problem on which we will store the parameters
     131             :  * @param prefix A prefix to apply to all the parameter object's PETSc options. This should either
     132             :  * be a single character '-' or a string like "-foo_" where the trailing '_' is required
     133             :  * @param param_object The parameter object potentially holding PETSc options
     134             :  * String prefixes may be used to select the system the parameters is applied to
     135             :  */
     136             : void storePetscOptions(FEProblemBase & fe_problem,
     137             :                        const std::string & prefix,
     138             :                        const ParallelParamObject & param_object);
     139             : 
     140             : /**
     141             :  * Set flags that will instruct the user on the reason their simulation diverged from PETSc's
     142             :  * perspective
     143             :  */
     144             : void setConvergedReasonFlags(FEProblemBase & fe_problem, const std::string & prefix);
     145             : 
     146             : /**
     147             :  * Sets the FE problem's solve type from the input params.
     148             :  */
     149             : void setSolveTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
     150             : 
     151             : /**
     152             :  * Sets the FE problem's line search from the input params.
     153             :  */
     154             : void setLineSearchFromParams(FEProblemBase & fe_problem, const InputParameters & params);
     155             : 
     156             : /**
     157             :  *  Sets the FE problem's matrix-free finite difference type from the input params.
     158             :  */
     159             : void setMFFDTypeFromParams(FEProblemBase & fe_problem, const InputParameters & params);
     160             : 
     161             : /**
     162             :  * Stores the Petsc flags and pair options fron the input params in the given PetscOptions object.
     163             :  */
     164             : void storePetscOptionsFromParams(FEProblemBase & fe_problem, const InputParameters & params);
     165             : 
     166             : /**
     167             :  * Populate flags in a given PetscOptions object using a vector of input arguments
     168             :  * @param petsc_flags Container holding the flags of the petsc options
     169             :  * @param prefix The prefix to add to the user provided \p petsc_flags
     170             :  * @param param_object The \p ParallelParamObject adding the PETSc options
     171             :  * @param petsc_options Data structure which handles petsc options within moose
     172             :  */
     173             : void addPetscFlagsToPetscOptions(const MultiMooseEnum & petsc_flags,
     174             :                                  const std::string & prefix,
     175             :                                  const ParallelParamObject & param_object,
     176             :                                  PetscOptions & petsc_options);
     177             : 
     178             : /**
     179             :  * Populate name and value pairs in a given PetscOptions object using vectors of input arguments
     180             :  * @param petsc_pair_options Option-value pairs of petsc settings
     181             :  * @param mesh_dimension The mesh dimension, needed for multigrid settings
     182             :  * @param prefix The prefix to add to the user provided \p petsc_flags
     183             :  * @param param_object The \p ParallelParamObject adding the PETSc options
     184             :  * @param petsc_options Data structure which handles petsc options within moose
     185             :  */
     186             : void addPetscPairsToPetscOptions(
     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             : 
     193             : /**
     194             :  * Returns the valid petsc line search options as a set of strings
     195             :  */
     196             : std::set<std::string> getPetscValidLineSearches();
     197             : 
     198             : /**
     199             :  * Returns the PETSc options that are common between Executioners and Preconditioners
     200             :  * @return InputParameters object containing the PETSc related parameters
     201             :  *
     202             :  * The output of this function should be added to the the parameters object of the overarching class
     203             :  * @see CreateExecutionerAction
     204             :  */
     205             : InputParameters getPetscValidParams();
     206             : 
     207             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags)
     208             : MultiMooseEnum getCommonPetscFlags();
     209             : 
     210             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc snes single options (flags)
     211             : MultiMooseEnum getCommonSNESFlags();
     212             : 
     213             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc ksp single options (flags)
     214             : MultiMooseEnum getCommonKSPFlags();
     215             : 
     216             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-value pairs)
     217             : MultiMooseEnum getCommonPetscKeys();
     218             : 
     219             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc snes option names (keys)
     220             : MultiMooseEnum getCommonSNESKeys();
     221             : 
     222             : /// A helper function to produce a MultiMooseEnum with commonly used PETSc ksp option names (keys)
     223             : MultiMooseEnum getCommonKSPKeys();
     224             : 
     225             : /// check if SNES type is variational inequalities (VI) solver
     226             : bool isSNESVI(FEProblemBase & fe_problem);
     227             : 
     228             : /**
     229             :  * A wrapper function for dealing with different versions of
     230             :  * PetscOptionsSetValue.  This is not generally called from
     231             :  * MOOSE code, it is instead intended to be called by stuff in
     232             :  * MOOSE::PetscSupport.
     233             :  */
     234             : void setSinglePetscOption(const std::string & name,
     235             :                           const std::string & value = "",
     236             :                           FEProblemBase * const problem = nullptr);
     237             : 
     238             : /**
     239             :  * Same as setSinglePetscOption, but does not set the option if it doesn't make sense for the
     240             :  * current simulation type, e.g. if \p name is contained within \p dont_add_these_options
     241             :  */
     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             : 
     247             : void addPetscOptionsFromCommandline();
     248             : 
     249             : /**
     250             :  * Setup which side we want to apply preconditioner
     251             :  */
     252             : void petscSetDefaultPCSide(FEProblemBase & problem, KSP ksp);
     253             : 
     254             : /**
     255             :  * Set norm type
     256             :  */
     257             : void petscSetDefaultKSPNormType(FEProblemBase & problem, KSP ksp);
     258             : 
     259             : /**
     260             :  * This method takes an adjacency matrix, and a desired number of colors and applies
     261             :  * a graph coloring algorithm to produce a coloring. The coloring is returned as a vector
     262             :  * of unsigned integers indicating which color or group each vextex in the adjacency matrix
     263             :  * belongs to.
     264             :  */
     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             : 
     271             : /**
     272             :  * Function to ensure that a particular petsc option is not added to the PetscOptions
     273             :  * storage object to be later set unless explicitly specified in input or on the command line.
     274             :  */
     275             : void dontAddPetscFlag(const std::string & flag, PetscOptions & petsc_options);
     276             : 
     277             : /**
     278             :  * Function to ensure that -snes_converged_reason is not added to the PetscOptions storage
     279             :  * object to be later set unless explicitly specified in input or on the command line.
     280             :  */
     281             : void dontAddNonlinearConvergedReason(FEProblemBase & fe_problem);
     282             : 
     283             : /**
     284             :  * Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage
     285             :  * object to be later set unless explicitly specified in input or on the command line.
     286             :  */
     287             : void dontAddLinearConvergedReason(FEProblemBase & fe_problem);
     288             : 
     289             : /**
     290             :  * Function to ensure that common KSP options are not added to the PetscOptions storage
     291             :  * object to be later set unless explicitly specified in input or on the command line.
     292             :  */
     293             : void dontAddCommonKSPOptions(FEProblemBase & fe_problem);
     294             : 
     295             : /**
     296             :  * Function to ensure that common SNES options are not added to the PetscOptions storage
     297             :  * object to be later set unless explicitly specified in input or on the command line.
     298             :  */
     299             : void dontAddCommonSNESOptions(FEProblemBase & fe_problem);
     300             : 
     301             : /**
     302             :  * Create a matrix from a binary file. Note that the returned libMesh matrix wrapper will not
     303             :  * destroy the created matrix on destruction. \p petsc_mat must be destroyed manually via \p
     304             :  * MatDestroy
     305             :  * @param mat_number_to_load A binary file may contain multiple writes of a matrix. This parameter
     306             :  * can be used to load a particular matrix from the binary file. By default we load the
     307             :  * first written matrix
     308             :  */
     309             : std::unique_ptr<PetscMatrix<Number>>
     310             : createMatrixFromFile(const libMesh::Parallel::Communicator & comm,
     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             : }

Generated by: LCOV version 1.14