www.mooseframework.org
SingleMatrixPreconditioner.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
16 
17 // MOOSE includes
18 #include "FEProblem.h"
19 #include "MooseUtils.h"
20 #include "MooseVariable.h"
21 #include "NonlinearSystem.h"
22 
23 #include "libmesh/coupling_matrix.h"
24 
25 template <>
28 {
30 
31  params.addParam<std::vector<NonlinearVariableName>>(
32  "off_diag_row",
33  "The off diagonal row you want to add into the matrix, it will be associated "
34  "with an off diagonal column from the same position in off_diag_colum.");
35  params.addParam<std::vector<NonlinearVariableName>>(
36  "off_diag_column",
37  "The off diagonal column you want to add into the matrix, it will be "
38  "associated with an off diagonal row from the same position in "
39  "off_diag_row.");
40  params.addParam<std::vector<NonlinearVariableName>>(
41  "coupled_groups",
42  "List multiple space separated groups of comma separated variables. "
43  "Off-diagonal jacobians will be generated for all pairs within a group.");
44  params.addParam<bool>("full",
45  false,
46  "Set to true if you want the full set of couplings. Simply "
47  "for convenience so you don't have to set every "
48  "off_diag_row and off_diag_column combination.");
49 
50  return params;
51 }
52 
54  : MoosePreconditioner(params)
55 {
57  unsigned int n_vars = nl.nVariables();
58 
59  std::unique_ptr<CouplingMatrix> cm = libmesh_make_unique<CouplingMatrix>(n_vars);
60  bool full = getParam<bool>("full");
61 
62  if (!full)
63  {
64  // put 1s on diagonal
65  for (unsigned int i = 0; i < n_vars; i++)
66  (*cm)(i, i) = 1;
67 
68  // off-diagonal entries from the off_diag_row and off_diag_column parameters
69  std::vector<std::vector<unsigned int>> off_diag(n_vars);
70  for (unsigned int i = 0;
71  i < getParam<std::vector<NonlinearVariableName>>("off_diag_row").size();
72  i++)
73  {
74  unsigned int row =
75  nl.getVariable(0, getParam<std::vector<NonlinearVariableName>>("off_diag_row")[i])
76  .number();
77  unsigned int column =
78  nl.getVariable(0, getParam<std::vector<NonlinearVariableName>>("off_diag_column")[i])
79  .number();
80  (*cm)(row, column) = 1;
81  }
82 
83  // off-diagonal entries from the coupled_groups parameters
84  std::vector<NonlinearVariableName> groups =
85  getParam<std::vector<NonlinearVariableName>>("coupled_groups");
86  for (unsigned int i = 0; i < groups.size(); ++i)
87  {
88  std::vector<NonlinearVariableName> vars;
89  MooseUtils::tokenize<NonlinearVariableName>(groups[i], vars, 1, ",");
90  for (unsigned int j = 0; j < vars.size(); ++j)
91  for (unsigned int k = j + 1; k < vars.size(); ++k)
92  {
93  unsigned int row = nl.getVariable(0, vars[j]).number();
94  unsigned int column = nl.getVariable(0, vars[k]).number();
95  (*cm)(row, column) = 1;
96  (*cm)(column, row) = 1;
97  }
98  }
99  }
100  else
101  {
102  for (unsigned int i = 0; i < n_vars; i++)
103  for (unsigned int j = 0; j < n_vars; j++)
104  (*cm)(i, j) = 1;
105  }
106 
107  _fe_problem.setCouplingMatrix(std::move(cm));
108 }
NonlinearSystemBase & getNonlinearSystemBase()
InputParameters validParams< SingleMatrixPreconditioner >()
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 >()
Base class for MOOSE preconditioners.
NonlinearSystemBase * nl
Nonlinear system to be solved.
virtual MooseVariable & getVariable(THREAD_ID tid, const std::string &var_name)
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:103
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
unsigned int number() const
Get variable number coming from libMesh.
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...
SingleMatrixPreconditioner(const InputParameters &params)
virtual unsigned int nVariables()
Get the number of variables in this system.
Definition: SystemBase.C:580