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 
22 template <>
25 {
27 
28  params.addParam<std::vector<NonlinearVariableName>>(
29  "off_diag_row",
30  "The off diagonal row you want to add into the matrix, it will be associated "
31  "with an off diagonal column from the same position in off_diag_colum.");
32  params.addParam<std::vector<NonlinearVariableName>>(
33  "off_diag_column",
34  "The off diagonal column you want to add into the matrix, it will be "
35  "associated with an off diagonal row from the same position in "
36  "off_diag_row.");
37  params.addParam<std::vector<NonlinearVariableName>>(
38  "coupled_groups",
39  "List multiple space separated groups of comma separated variables. "
40  "Off-diagonal jacobians will be generated for all pairs within a group.");
41  params.addParam<bool>("full",
42  false,
43  "Set to true if you want the full set of couplings. Simply "
44  "for convenience so you don't have to set every "
45  "off_diag_row and off_diag_column combination.");
46 
47  return params;
48 }
49 
51  : MoosePreconditioner(params)
52 {
54  unsigned int n_vars = nl.nVariables();
55 
56  std::unique_ptr<CouplingMatrix> cm = libmesh_make_unique<CouplingMatrix>(n_vars);
57  bool full = getParam<bool>("full");
58 
59  if (!full)
60  {
61  // put 1s on diagonal
62  for (unsigned int i = 0; i < n_vars; i++)
63  (*cm)(i, i) = 1;
64 
65  // off-diagonal entries from the off_diag_row and off_diag_column parameters
66  std::vector<std::vector<unsigned int>> off_diag(n_vars);
67  for (unsigned int i = 0;
68  i < getParam<std::vector<NonlinearVariableName>>("off_diag_row").size();
69  i++)
70  {
71  unsigned int row =
72  nl.getVariable(0, getParam<std::vector<NonlinearVariableName>>("off_diag_row")[i])
73  .number();
74  unsigned int column =
75  nl.getVariable(0, getParam<std::vector<NonlinearVariableName>>("off_diag_column")[i])
76  .number();
77  (*cm)(row, column) = 1;
78  }
79 
80  // off-diagonal entries from the coupled_groups parameters
81  std::vector<NonlinearVariableName> groups =
82  getParam<std::vector<NonlinearVariableName>>("coupled_groups");
83  for (unsigned int i = 0; i < groups.size(); ++i)
84  {
85  std::vector<NonlinearVariableName> vars;
86  MooseUtils::tokenize<NonlinearVariableName>(groups[i], vars, 1, ",");
87  for (unsigned int j = 0; j < vars.size(); ++j)
88  for (unsigned int k = j + 1; k < vars.size(); ++k)
89  {
90  unsigned int row = nl.getVariable(0, vars[j]).number();
91  unsigned int column = nl.getVariable(0, vars[k]).number();
92  (*cm)(row, column) = 1;
93  (*cm)(column, row) = 1;
94  }
95  }
96  }
97  else
98  {
99  for (unsigned int i = 0; i < n_vars; i++)
100  for (unsigned int j = 0; j < n_vars; j++)
101  (*cm)(i, j) = 1;
102  }
103 
104  _fe_problem.setCouplingMatrix(std::move(cm));
105 }
NonlinearSystemBase & getNonlinearSystemBase()
unsigned int number() const
Get variable number coming from libMesh.
InputParameters validParams< SingleMatrixPreconditioner >()
registerMooseObjectAliased("MooseApp", SingleMatrixPreconditioner, "SMP")
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void setCouplingMatrix(std::unique_ptr< CouplingMatrix > cm)
Set custom coupling matrix.
FEProblemBase & _fe_problem
Subproblem this preconditioner is part of.
InputParameters validParams< MoosePreconditioner >()
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
Base class for MOOSE preconditioners.
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:692
NonlinearSystemBase * nl
Nonlinear system to be solved.
MooseVariableFEBase & getVariable(THREAD_ID tid, const std::string &var_name)
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:105
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)