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