lduMatrixSolver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "lduMatrix.H"
30 #include "diagonalSolver.H"
31 #include "PrecisionAdaptor.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineRunTimeSelectionTable(lduMatrix::solver, symMatrix);
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
45 (
46  const word& fieldName,
47  const lduMatrix& matrix,
48  const FieldField<Field, scalar>& interfaceBouCoeffs,
49  const FieldField<Field, scalar>& interfaceIntCoeffs,
50  const lduInterfaceFieldPtrsList& interfaces,
51  const dictionary& solverControls
52 )
53 {
54  const word name(solverControls.get<word>("solver"));
55 
56  if (matrix.diagonal())
57  {
59  (
60  new diagonalSolver
61  (
62  fieldName,
63  matrix,
66  interfaces,
67  solverControls
68  )
69  );
70  }
71  else if (matrix.symmetric())
72  {
73  auto* ctorPtr = symMatrixConstructorTable(name);
74 
75  if (!ctorPtr)
76  {
78  (
79  solverControls,
80  "symmetric matrix solver",
81  name,
82  *symMatrixConstructorTablePtr_
83  ) << exit(FatalIOError);
84  }
85 
86  return autoPtr<lduMatrix::solver>
87  (
88  ctorPtr
89  (
90  fieldName,
91  matrix,
94  interfaces,
95  solverControls
96  )
97  );
98  }
99  else if (matrix.asymmetric())
100  {
101  auto* ctorPtr = asymMatrixConstructorTable(name);
102 
103  if (!ctorPtr)
104  {
106  (
107  solverControls,
108  "asymmetric matrix solver",
109  name,
110  *asymMatrixConstructorTablePtr_
111  ) << exit(FatalIOError);
112  }
113 
114  return autoPtr<lduMatrix::solver>
115  (
116  ctorPtr
117  (
118  fieldName,
119  matrix,
122  interfaces,
123  solverControls
124  )
125  );
126  }
127 
128  FatalIOErrorInFunction(solverControls)
129  << "cannot solve incomplete matrix, "
130  "no diagonal or off-diagonal coefficient"
131  << exit(FatalIOError);
132 
133  return nullptr;
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
140 (
141  const word& fieldName,
142  const lduMatrix& matrix,
143  const FieldField<Field, scalar>& interfaceBouCoeffs,
144  const FieldField<Field, scalar>& interfaceIntCoeffs,
145  const lduInterfaceFieldPtrsList& interfaces,
146  const dictionary& solverControls
147 )
148 :
149  fieldName_(fieldName),
150  matrix_(matrix),
151  interfaceBouCoeffs_(interfaceBouCoeffs),
152  interfaceIntCoeffs_(interfaceIntCoeffs),
153  interfaces_(interfaces),
154  controlDict_(solverControls),
155 
156  log_(1),
157  minIter_(0),
158  maxIter_(lduMatrix::defaultMaxIter),
159  normType_(lduMatrix::normTypes::DEFAULT_NORM),
160  tolerance_(lduMatrix::defaultTolerance),
161  relTol_(Zero),
162 
163  profiling_("lduMatrix::solver." + fieldName)
164 {
165  readControls();
166 }
167 
168 
169 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
170 
172 {
173  log_ = 1;
174  minIter_ = 0;
175  maxIter_ = lduMatrix::defaultMaxIter;
177  tolerance_ = lduMatrix::defaultTolerance;
178  relTol_ = 0;
179 
180  controlDict_.readIfPresent("log", log_);
181  lduMatrix::normTypesNames_.readIfPresent("norm", controlDict_, normType_);
182  controlDict_.readIfPresent("minIter", minIter_);
183  controlDict_.readIfPresent("maxIter", maxIter_);
184  controlDict_.readIfPresent("tolerance", tolerance_);
185  controlDict_.readIfPresent("relTol", relTol_);
186 }
187 
188 
189 void Foam::lduMatrix::solver::read(const dictionary& solverControls)
190 {
191  controlDict_ = solverControls;
192  readControls();
193 }
194 
195 
197 (
199  const solveScalarField& source,
200  const direction cmpt
201 ) const
202 {
204  return solve
205  (
206  tpsi_s.ref(),
208  cmpt
209  );
210 }
211 
212 
214 (
215  const solveScalarField& psi,
216  const solveScalarField& source,
217  const solveScalarField& Apsi,
218  solveScalarField& tmpField,
219  const lduMatrix::normTypes normType
220 ) const
221 {
222  switch (normType)
223  {
225  {
226  break;
227  }
228 
231  {
232  // --- Calculate A dot reference value of psi
233  matrix_.sumA(tmpField, interfaceBouCoeffs_, interfaces_);
234 
235  tmpField *= gAverage(psi, matrix_.mesh().comm());
236 
237  return
238  gSum
239  (
240  (mag(Apsi - tmpField) + mag(source - tmpField))(),
241  matrix_.mesh().comm()
243 
244  // Equivalent at convergence:
245  // return 2*gSumMag(source) + solverPerformance::small_;
246  break;
247  }
248  }
249 
250  // Fall-through: no norm
251  return solveScalarField::cmptType(1);
252 }
253 
254 
255 // ************************************************************************* //
const FieldField< Field, scalar > & interfaceBouCoeffs() const noexcept
Definition: lduMatrix.H:297
bool symmetric() const noexcept
Definition: lduMatrix.H:766
virtual void read(const dictionary &)
Read and reset the solver parameters from the given stream.
uint8_t direction
Definition: direction.H:46
SolverPerformance< Type > solve(faMatrix< Type > &, const dictionary &)
Solve returning the solution statistics given convergence tolerance.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
static const scalar small_
Small Type for the use in solvers.
pTraits< solveScalar >::cmptType cmptType
Component type.
Definition: Field.H:123
Foam::diagonalSolver.
const lduMatrix & matrix() const noexcept
Definition: lduMatrix.H:292
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
A non-const Field/List wrapper with possible data conversion.
static constexpr const label defaultMaxIter
Default maximum number of iterations for solvers (1000)
Definition: lduMatrix.H:118
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:51
static const Enum< normTypes > normTypesNames_
Names for the normTypes.
Definition: lduMatrix.H:113
Type gSum(const FieldField< Field, Type > &f)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics...
Abstract base-class for lduMatrix solvers.
Definition: lduMatrix.H:129
A const Field/List wrapper with possible data conversion.
const lduInterfaceFieldPtrsList & interfaces() const noexcept
Definition: lduMatrix.H:307
static const scalar defaultTolerance
Default (absolute) tolerance (1e-6)
Definition: lduMatrix.H:123
solver(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Construct solver for given field name, matrix etc.
static autoPtr< solver > New(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Return a new solver.
normTypes
Enumerated matrix normalisation types.
Definition: lduMatrix.H:103
bool asymmetric() const noexcept
Definition: lduMatrix.H:771
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
solveScalarField::cmptType normFactor(const solveScalarField &psi, const solveScalarField &source, const solveScalarField &Apsi, solveScalarField &tmpField, const lduMatrix::normTypes normType) const
Return the matrix norm using the specified norm method.
bool diagonal() const noexcept
Definition: lduMatrix.H:761
"default" norm (== L1_scaled)
lduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: lduMatrix.H:79
"none" norm (returns 1)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
Type gAverage(const FieldField< Field, Type > &f)
bool readIfPresent(const word &key, const dictionary &dict, EnumType &val) const
Find an entry if present, and assign to T val.
Definition: EnumI.H:125
const FieldField< Field, scalar > & interfaceIntCoeffs() const noexcept
Definition: lduMatrix.H:302
virtual solverPerformance scalarSolve(solveScalarField &psi, const solveScalarField &source, const direction cmpt=0) const
Solve with given field and rhs (in solveScalar precision).
const volScalarField & psi
const word & fieldName() const noexcept
Definition: lduMatrix.H:287
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:615
Namespace for OpenFOAM.
virtual void readControls()
Read the control parameters from controlDict_.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157