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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "lduMatrix.H"
27 #include "diagonalSolver.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineRunTimeSelectionTable(lduMatrix::solver, symMatrix);
34  defineRunTimeSelectionTable(lduMatrix::solver, asymMatrix);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
41 (
42  const word& fieldName,
43  const lduMatrix& matrix,
44  const FieldField<Field, scalar>& interfaceBouCoeffs,
45  const FieldField<Field, scalar>& interfaceIntCoeffs,
46  const lduInterfaceFieldPtrsList& interfaces,
47  const dictionary& solverControls
48 )
49 {
50  const word name(solverControls.lookup("solver"));
51 
52  if (matrix.diagonal())
53  {
55  (
56  new diagonalSolver
57  (
58  fieldName,
59  matrix,
60  interfaceBouCoeffs,
61  interfaceIntCoeffs,
62  interfaces,
63  solverControls
64  )
65  );
66  }
67  else if (matrix.symmetric())
68  {
69  symMatrixConstructorTable::iterator constructorIter =
70  symMatrixConstructorTablePtr_->find(name);
71 
72  if (constructorIter == symMatrixConstructorTablePtr_->end())
73  {
74  FatalIOErrorInFunction(solverControls)
75  << "Unknown symmetric matrix solver " << name << nl << nl
76  << "Valid symmetric matrix solvers are :" << endl
77  << symMatrixConstructorTablePtr_->sortedToc()
78  << exit(FatalIOError);
79  }
80 
82  (
83  constructorIter()
84  (
85  fieldName,
86  matrix,
87  interfaceBouCoeffs,
88  interfaceIntCoeffs,
89  interfaces,
90  solverControls
91  )
92  );
93  }
94  else if (matrix.asymmetric())
95  {
96  asymMatrixConstructorTable::iterator constructorIter =
97  asymMatrixConstructorTablePtr_->find(name);
98 
99  if (constructorIter == asymMatrixConstructorTablePtr_->end())
100  {
101  FatalIOErrorInFunction(solverControls)
102  << "Unknown asymmetric matrix solver " << name << nl << nl
103  << "Valid asymmetric matrix solvers are :" << endl
104  << asymMatrixConstructorTablePtr_->sortedToc()
105  << exit(FatalIOError);
106  }
107 
109  (
110  constructorIter()
111  (
112  fieldName,
113  matrix,
114  interfaceBouCoeffs,
115  interfaceIntCoeffs,
116  interfaces,
117  solverControls
118  )
119  );
120  }
121  else
122  {
123  FatalIOErrorInFunction(solverControls)
124  << "cannot solve incomplete matrix, "
125  "no diagonal or off-diagonal coefficient"
126  << exit(FatalIOError);
127 
128  return autoPtr<lduMatrix::solver>(NULL);
129  }
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
134 
136 (
137  const word& fieldName,
138  const lduMatrix& matrix,
139  const FieldField<Field, scalar>& interfaceBouCoeffs,
140  const FieldField<Field, scalar>& interfaceIntCoeffs,
141  const lduInterfaceFieldPtrsList& interfaces,
142  const dictionary& solverControls
143 )
144 :
145  fieldName_(fieldName),
146  matrix_(matrix),
147  interfaceBouCoeffs_(interfaceBouCoeffs),
148  interfaceIntCoeffs_(interfaceIntCoeffs),
149  interfaces_(interfaces),
150  controlDict_(solverControls)
151 {
152  readControls();
153 }
154 
155 
156 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157 
159 {
160  maxIter_ = controlDict_.lookupOrDefault<label>("maxIter", 1000);
161  minIter_ = controlDict_.lookupOrDefault<label>("minIter", 0);
162  tolerance_ = controlDict_.lookupOrDefault<scalar>("tolerance", 1e-6);
163  relTol_ = controlDict_.lookupOrDefault<scalar>("relTol", 0);
164 }
165 
166 
167 void Foam::lduMatrix::solver::read(const dictionary& solverControls)
168 {
169  controlDict_ = solverControls;
170  readControls();
171 }
172 
173 
175 (
176  const scalarField& psi,
177  const scalarField& source,
178  const scalarField& Apsi,
179  scalarField& tmpField
180 ) const
181 {
182  // --- Calculate A dot reference value of psi
183  matrix_.sumA(tmpField, interfaceBouCoeffs_, interfaces_);
184 
185  tmpField *= gAverage(psi, matrix_.lduMesh_.comm());
186 
187  return
188  gSum
189  (
190  (mag(Apsi - tmpField) + mag(source - tmpField))(),
191  matrix_.lduMesh_.comm()
192  )
194 
195  // At convergence this simpler method is equivalent to the above
196  // return 2*gSumMag(source) + solverPerformance::small_;
197 }
198 
199 
200 // ************************************************************************* //
Foam::lduMatrix::solver::normFactor
scalar normFactor(const scalarField &psi, const scalarField &source, const scalarField &Apsi, scalarField &tmpField) const
Return the matrix norm used to normalise the residual for the.
Definition: lduMatrixSolver.C:175
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::FieldField
Generic field type.
Definition: FieldField.H:51
Foam::diagonalSolver
Foam::diagonalSolver.
Definition: diagonalSolver.H:48
Foam::gAverage
Type gAverage(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:571
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::lduMatrix
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:77
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::lduMatrix::symmetric
bool symmetric() const
Definition: lduMatrix.H:595
Foam::dictionary::lookupOrDefault
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Definition: dictionaryTemplates.C:33
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
lduMatrix.H
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:564
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::lduMatrix::solver::minIter_
label minIter_
Minimum number of iterations in the solver.
Definition: lduMatrix.H:110
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::lduMatrix::solver::read
virtual void read(const dictionary &)
Read and reset the solver parameters from the given stream.
Definition: lduMatrixSolver.C:167
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::lduMatrix::solver::maxIter_
label maxIter_
Maximum number of iterations in the solver.
Definition: lduMatrix.H:107
diagonalSolver.H
Foam::UPtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:53
Foam::lduMatrix::diagonal
bool diagonal() const
Definition: lduMatrix.H:590
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::lduMatrix::solver::readControls
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: lduMatrixSolver.C:158
Foam::lduMatrix::solver::New
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.
Definition: lduMatrixSolver.C:41
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::lduMatrix::solver::relTol_
scalar relTol_
Convergence tolerance relative to the initial.
Definition: lduMatrix.H:116
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::SolverPerformance::small_
static const scalar small_
Small Type for the use in solvers.
Definition: SolverPerformance.H:102
Foam::autoPtr< Foam::lduMatrix::solver >
Foam::lduMatrix::solver::controlDict_
dictionary controlDict_
Dictionary of controls.
Definition: lduMatrix.H:104
Foam::lduMatrix::solver::tolerance_
scalar tolerance_
Final convergence tolerance.
Definition: lduMatrix.H:113
psi
const volScalarField & psi
Definition: setRegionFluidFields.H:13
Foam::lduMatrix::asymmetric
bool asymmetric() const
Definition: lduMatrix.H:600
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::lduMatrix::solver::solver
solver(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Definition: lduMatrixSolver.C:136