www.mooseframework.org
FiniteDifferencePreconditioner.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 "MooseVariable.h"
20 #include "NonlinearSystem.h"
21 
22 #include "libmesh/coupling_matrix.h"
23 
24 template <>
27 {
29 
30  params.addParam<std::vector<std::string>>(
31  "off_diag_row",
32  "The off diagonal row you want to add into the matrix, it will be associated "
33  "with an off diagonal column from the same position in off_diag_colum.");
34  params.addParam<std::vector<std::string>>("off_diag_column",
35  "The off diagonal column you want to add into the "
36  "matrix, it will be associated with an off diagonal "
37  "row from the same position in off_diag_row.");
38  params.addParam<bool>("full",
39  false,
40  "Set to true if you want the full set of couplings. Simply "
41  "for convenience so you don't have to set every "
42  "off_diag_row and off_diag_column combination.");
43  params.addParam<bool>("implicit_geometric_coupling",
44  false,
45  "Set to true if you want to add entries into the "
46  "matrix for degrees of freedom that might be coupled "
47  "by inspection of the geometric search objects.");
48 
49  MooseEnum finite_difference_type("standard coloring", "coloring");
50  params.addParam<MooseEnum>("finite_difference_type",
51  finite_difference_type,
52  "standard: standard finite difference"
53  "coloring: finite difference based on coloring");
54 
55  return params;
56 }
57 
59  : MoosePreconditioner(params),
60  _finite_difference_type(getParam<MooseEnum>("finite_difference_type"))
61 {
62  if (n_processors() > 1)
63  mooseError("Can't use the Finite Difference Preconditioner in parallel yet!");
64 
66  unsigned int n_vars = nl.nVariables();
67 
68  std::unique_ptr<CouplingMatrix> cm = libmesh_make_unique<CouplingMatrix>(n_vars);
69 
70  bool full = getParam<bool>("full");
71 
72  // standard finite difference method will add off-diagonal entries
73  if (_finite_difference_type == "standard")
74  full = true;
75 
76  if (!full)
77  {
78  // put 1s on diagonal
79  for (unsigned int i = 0; i < n_vars; i++)
80  (*cm)(i, i) = 1;
81 
82  // off-diagonal entries
83  std::vector<std::vector<unsigned int>> off_diag(n_vars);
84  for (unsigned int i = 0; i < getParam<std::vector<std::string>>("off_diag_row").size(); i++)
85  {
86  unsigned int row =
87  nl.getVariable(0, getParam<std::vector<std::string>>("off_diag_row")[i]).number();
88  unsigned int column =
89  nl.getVariable(0, getParam<std::vector<std::string>>("off_diag_column")[i]).number();
90  (*cm)(row, column) = 1;
91  }
92 
93  // TODO: handle coupling entries between NL-vars and SCALAR-vars
94  }
95  else
96  {
97  for (unsigned int i = 0; i < n_vars; i++)
98  for (unsigned int j = 0; j < n_vars; j++)
99  (*cm)(i, j) = 1;
100  }
101 
102  _fe_problem.setCouplingMatrix(std::move(cm));
103 
104  bool implicit_geometric_coupling = getParam<bool>("implicit_geometric_coupling");
105 
106  nl.addImplicitGeometricCouplingEntriesToJacobian(implicit_geometric_coupling);
107 
108  // Set the jacobian to null so that libMesh won't override our finite differenced jacobian
110 }
NonlinearSystemBase & getNonlinearSystemBase()
void addImplicitGeometricCouplingEntriesToJacobian(bool add=true)
If called with true this will add entries into the jacobian to link together degrees of freedom that ...
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 >()
FiniteDifferencePreconditioner(const InputParameters &params)
Base class for MOOSE preconditioners.
NonlinearSystemBase * nl
Nonlinear system to be solved.
void useFiniteDifferencedPreconditioner(bool use=true)
If called with true this system will use a finite differenced form of the Jacobian as the preconditio...
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
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
InputParameters validParams< FiniteDifferencePreconditioner >()
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
virtual unsigned int nVariables()
Get the number of variables in this system.
Definition: SystemBase.C:580