www.mooseframework.org
SingleMatrixPreconditioner.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
11 
12 // MOOSE includes
13 #include "FEProblem.h"
14 #include "MooseUtils.h"
15 #include "MooseVariableFE.h"
16 #include "NonlinearSystem.h"
17 
18 #include "libmesh/coupling_matrix.h"
19 
21 
24 {
26 
27  params.addClassDescription("Single matrix preconditioner (SMP) builds a preconditioner using "
28  "user defined off-diagonal parts of the Jacobian.");
29 
30  params.addParam<std::vector<NonlinearVariableName>>(
31  "coupled_groups",
32  {},
33  "List multiple space separated groups of comma separated variables. "
34  "Off-diagonal jacobians will be generated for all pairs within a group.");
35  params.addParam<bool>(
36  "trust_my_coupling",
37  false,
38  "Whether to trust my coupling even if the framework wants to be paranoid and create a full "
39  "coupling matrix, which can happen when using global AD indexing for example.");
40 
41  return params;
42 }
43 
45  : MoosePreconditioner(params)
46 {
48  unsigned int n_vars = nl.nVariables();
49  const auto & libmesh_system = nl.system();
50  auto cm = std::make_unique<CouplingMatrix>(n_vars);
51 
52  if (!getParam<bool>("full"))
53  {
54  // put 1s on diagonal
55  for (unsigned int i = 0; i < n_vars; ++i)
56  (*cm)(i, i) = 1;
57 
58  // off-diagonal entries from the off_diag_row and off_diag_column parameters
59  for (const auto & off_diag :
60  getParam<NonlinearVariableName, NonlinearVariableName>("off_diag_row", "off_diag_column"))
61  {
62  const auto row = libmesh_system.variable_number(off_diag.first);
63  const auto column = libmesh_system.variable_number(off_diag.second);
64  (*cm)(row, column) = 1;
65  }
66 
67  // off-diagonal entries from the coupled_groups parameters
68  const auto & all_vars = nl.getVariableNames();
69  for (const auto & group : getParam<std::vector<NonlinearVariableName>>("coupled_groups"))
70  {
71  std::vector<VariableName> vars;
72  MooseUtils::tokenize(group, vars, 1, ",");
73  try
74  {
75  MooseUtils::expandAllMatches(all_vars, vars);
76  }
77  catch (std::invalid_argument const & e)
78  {
79  mooseError("No variable name match found for '", e.what(), "'.");
80  }
81 
82  for (const auto j : index_range(vars))
83  for (unsigned int k = j + 1; k < vars.size(); ++k)
84  {
85  const auto row = libmesh_system.variable_number(vars[j]);
86  const auto column = libmesh_system.variable_number(vars[k]);
87  (*cm)(row, column) = 1;
88  (*cm)(column, row) = 1;
89  }
90  }
91  }
92  else
93  {
94  for (unsigned int i = 0; i < n_vars; ++i)
95  for (unsigned int j = 0; j < n_vars; ++j)
96  (*cm)(i, j) = 1;
97  }
98 
99  setCouplingMatrix(std::move(cm));
100  if (getParam<bool>("trust_my_coupling"))
102 }
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:779
void trustUserCouplingMatrix()
Whether to trust the user coupling matrix even if we want to do things like be paranoid and create a ...
registerMooseObjectAliased("MooseApp", SingleMatrixPreconditioner, "SMP")
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const unsigned int _nl_sys_num
The nonlinear system number whose linearization this preconditioner should be applied to...
FEProblemBase & _fe_problem
Subproblem this preconditioner is part of.
static InputParameters validParams()
Base class for MOOSE preconditioners.
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:840
Nonlinear system to be solved.
unsigned int n_vars
void expandAllMatches(const std::vector< T > &candidates, std::vector< T > &patterns)
Definition: MooseUtils.h:753
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
static InputParameters validParams()
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
virtual System & system() override
Get the reference to the libMesh system.
const std::vector< VariableName > & getVariableNames() const
Definition: SystemBase.h:843
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
Single matrix preconditioner.
SingleMatrixPreconditioner(const InputParameters &params)
void setCouplingMatrix(std::unique_ptr< CouplingMatrix > cm)
Setup the coupling matrix on the finite element problem.
auto index_range(const T &sizable)