https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SingleMatrixPreconditioner.C
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
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}
char ** vars
registerMooseObjectAliased("MooseApp", SingleMatrixPreconditioner, "SMP")
unsigned int n_vars
void trustUserCouplingMatrix()
Whether to trust the user coupling matrix even if we want to do things like be paranoid and create a ...
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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 mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
Base class for MOOSE preconditioners.
FEProblemBase & _fe_problem
Subproblem this preconditioner is part of.
void setCouplingMatrix(std::unique_ptr< libMesh::CouplingMatrix > cm)
Setup the coupling matrix on the finite element problem.
static InputParameters validParams()
const unsigned int _nl_sys_num
The nonlinear system number whose linearization this preconditioner should be applied to.
Nonlinear system to be solved.
virtual libMesh::System & system() override
Get the reference to the libMesh system.
Single matrix preconditioner.
static InputParameters validParams()
SingleMatrixPreconditioner(const InputParameters &params)
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition SystemBase.C:890
const std::vector< VariableName > & getVariableNames() const
Definition SystemBase.h:881
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 ...