LUscalarMatrix.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) 2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "LUscalarMatrix.H"
30#include "lduMatrix.H"
31#include "procLduMatrix.H"
32#include "procLduInterface.H"
33#include "cyclicLduInterface.H"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
40}
41
42
43// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44
46:
47 comm_(Pstream::worldComm)
48{}
49
50
52:
53 scalarSquareMatrix(matrix),
54 comm_(Pstream::worldComm),
55 pivotIndices_(m())
56{
57 LUDecompose(*this, pivotIndices_);
58}
59
60
62(
63 const lduMatrix& ldum,
64 const FieldField<Field, scalar>& interfaceCoeffs,
65 const lduInterfaceFieldPtrsList& interfaces
66)
67:
68 comm_(ldum.mesh().comm())
69{
70 if (Pstream::parRun())
71 {
72 PtrList<procLduMatrix> lduMatrices(Pstream::nProcs(comm_));
73
74 label lduMatrixi = 0;
75
76 lduMatrices.set
77 (
78 lduMatrixi++,
80 (
81 ldum,
82 interfaceCoeffs,
83 interfaces
84 )
85 );
86
87 if (Pstream::master(comm_))
88 {
89 for (const int slave : Pstream::subProcs(comm_))
90 {
91 lduMatrices.set
92 (
93 lduMatrixi++,
95 (
97 (
99 slave,
100 0, // bufSize
102 comm_
103 )()
104 )
105 );
106 }
107 }
108 else
109 {
110 OPstream toMaster
111 (
114 0, // bufSize
116 comm_
117 );
118 procLduMatrix cldum
119 (
120 ldum,
121 interfaceCoeffs,
122 interfaces
123 );
124 toMaster<< cldum;
125
126 }
127
128 if (Pstream::master(comm_))
129 {
130 label nCells = 0;
131 forAll(lduMatrices, i)
132 {
133 nCells += lduMatrices[i].size();
134 }
135
136 scalarSquareMatrix m(nCells, 0.0);
137 transfer(m);
138 convert(lduMatrices);
139 }
140 }
141 else
142 {
143 label nCells = ldum.lduAddr().size();
144 scalarSquareMatrix m(nCells, Zero);
145 transfer(m);
146 convert(ldum, interfaceCoeffs, interfaces);
147 }
148
149 if (Pstream::master(comm_))
150 {
151 if (debug)
152 {
153 const label numRows = m();
154 const label numCols = n();
155
156 Pout<< "LUscalarMatrix : size:" << numRows << endl;
157 for (label rowi = 0; rowi < numRows; ++rowi)
158 {
159 const scalar* row = operator[](rowi);
160
161 Pout<< "cell:" << rowi << " diagCoeff:" << row[rowi] << endl;
162
163 Pout<< " connects to upper cells :";
164 for (label coli = rowi+1; coli < numCols; ++coli)
165 {
166 if (mag(row[coli]) > SMALL)
167 {
168 Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
169 }
170 }
171 Pout<< endl;
172 Pout<< " connects to lower cells :";
173 for (label coli = 0; coli < rowi; ++coli)
174 {
175 if (mag(row[coli]) > SMALL)
176 {
177 Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
178 }
179 }
180 Pout<< nl;
181 }
182 Pout<< nl;
183 }
184
185 pivotIndices_.setSize(m());
186 LUDecompose(*this, pivotIndices_);
187 }
188}
189
190
191// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
192
193void Foam::LUscalarMatrix::convert
194(
195 const lduMatrix& ldum,
196 const FieldField<Field, scalar>& interfaceCoeffs,
197 const lduInterfaceFieldPtrsList& interfaces
198)
199{
200 const label* __restrict__ uPtr = ldum.lduAddr().upperAddr().begin();
201 const label* __restrict__ lPtr = ldum.lduAddr().lowerAddr().begin();
202
203 const scalar* __restrict__ diagPtr = ldum.diag().begin();
204 const scalar* __restrict__ upperPtr = ldum.upper().begin();
205 const scalar* __restrict__ lowerPtr = ldum.lower().begin();
206
207 const label nCells = ldum.diag().size();
208 const label nFaces = ldum.upper().size();
209
210 for (label cell=0; cell<nCells; cell++)
211 {
212 operator[](cell)[cell] = diagPtr[cell];
213 }
214
215 for (label face=0; face<nFaces; face++)
216 {
217 label uCell = uPtr[face];
218 label lCell = lPtr[face];
219
220 operator[](uCell)[lCell] = lowerPtr[face];
221 operator[](lCell)[uCell] = upperPtr[face];
222 }
223
224 forAll(interfaces, inti)
225 {
226 if (interfaces.set(inti))
227 {
228 const lduInterface& interface = interfaces[inti].interface();
229
230 // Assume any interfaces are cyclic ones
231
232 const label* __restrict__ lPtr = interface.faceCells().begin();
233
234 const cyclicLduInterface& cycInterface =
235 refCast<const cyclicLduInterface>(interface);
236 label nbrInt = cycInterface.neighbPatchID();
237 const label* __restrict__ uPtr =
238 interfaces[nbrInt].interface().faceCells().begin();
239
240 const scalar* __restrict__ nbrUpperLowerPtr =
241 interfaceCoeffs[nbrInt].begin();
242
243 label inFaces = interface.faceCells().size();
244
245 for (label face=0; face<inFaces; face++)
246 {
247 label uCell = lPtr[face];
248 label lCell = uPtr[face];
249
250 operator[](uCell)[lCell] -= nbrUpperLowerPtr[face];
251 }
252 }
253 }
254}
255
256
257void Foam::LUscalarMatrix::convert
258(
259 const PtrList<procLduMatrix>& lduMatrices
260)
261{
262 procOffsets_.setSize(lduMatrices.size() + 1);
263 procOffsets_[0] = 0;
264
265 forAll(lduMatrices, ldumi)
266 {
267 procOffsets_[ldumi+1] = procOffsets_[ldumi] + lduMatrices[ldumi].size();
268 }
269
270 forAll(lduMatrices, ldumi)
271 {
272 const procLduMatrix& lduMatrixi = lduMatrices[ldumi];
273 label offset = procOffsets_[ldumi];
274
275 const label* __restrict__ uPtr = lduMatrixi.upperAddr_.begin();
276 const label* __restrict__ lPtr = lduMatrixi.lowerAddr_.begin();
277
278 const scalar* __restrict__ diagPtr = lduMatrixi.diag_.begin();
279 const scalar* __restrict__ upperPtr = lduMatrixi.upper_.begin();
280 const scalar* __restrict__ lowerPtr = lduMatrixi.lower_.begin();
281
282 const label nCells = lduMatrixi.size();
283 const label nFaces = lduMatrixi.upper_.size();
284
285 for (label cell=0; cell<nCells; cell++)
286 {
287 label globalCell = cell + offset;
288 operator[](globalCell)[globalCell] = diagPtr[cell];
289 }
290
291 for (label face=0; face<nFaces; face++)
292 {
293 label uCell = uPtr[face] + offset;
294 label lCell = lPtr[face] + offset;
295
296 operator[](uCell)[lCell] = lowerPtr[face];
297 operator[](lCell)[uCell] = upperPtr[face];
298 }
299
300 const PtrList<procLduInterface>& interfaces =
301 lduMatrixi.interfaces_;
302
303 forAll(interfaces, inti)
304 {
305 const procLduInterface& interface = interfaces[inti];
306
307 if (interface.myProcNo_ == interface.neighbProcNo_)
308 {
309 const label* __restrict__ ulPtr = interface.faceCells_.begin();
310
311 const scalar* __restrict__ upperLowerPtr =
312 interface.coeffs_.begin();
313
314 label inFaces = interface.faceCells_.size()/2;
315
316 for (label face=0; face<inFaces; face++)
317 {
318 label uCell = ulPtr[face] + offset;
319 label lCell = ulPtr[face + inFaces] + offset;
320
321 operator[](uCell)[lCell] -= upperLowerPtr[face + inFaces];
322 operator[](lCell)[uCell] -= upperLowerPtr[face];
323 }
324 }
325 else if (interface.myProcNo_ < interface.neighbProcNo_)
326 {
327 // Interface to neighbour proc. Find on neighbour proc the
328 // corresponding interface. The problem is that there can
329 // be multiple interfaces between two processors (from
330 // processorCyclics) so also compare the communication tag
331
332 const PtrList<procLduInterface>& neiInterfaces =
333 lduMatrices[interface.neighbProcNo_].interfaces_;
334
335 label neiInterfacei = -1;
336
337 forAll(neiInterfaces, ninti)
338 {
339 if
340 (
341 (
342 neiInterfaces[ninti].neighbProcNo_
343 == interface.myProcNo_
344 )
345 && (neiInterfaces[ninti].tag_ == interface.tag_)
346 )
347 {
348 neiInterfacei = ninti;
349 break;
350 }
351 }
352
353 if (neiInterfacei == -1)
354 {
356 }
357
358 const procLduInterface& neiInterface =
359 neiInterfaces[neiInterfacei];
360
361 const label* __restrict__ uPtr = interface.faceCells_.begin();
362 const label* __restrict__ lPtr =
363 neiInterface.faceCells_.begin();
364
365 const scalar* __restrict__ upperPtr = interface.coeffs_.begin();
366 const scalar* __restrict__ lowerPtr =
367 neiInterface.coeffs_.begin();
368
369 label inFaces = interface.faceCells_.size();
370 label neiOffset = procOffsets_[interface.neighbProcNo_];
371
372 for (label face=0; face<inFaces; face++)
373 {
374 label uCell = uPtr[face] + offset;
375 label lCell = lPtr[face] + neiOffset;
376
377 operator[](uCell)[lCell] -= lowerPtr[face];
378 operator[](lCell)[uCell] -= upperPtr[face];
379 }
380 }
381 }
382 }
383}
384
385
386void Foam::LUscalarMatrix::printDiagonalDominance() const
387{
388 for (label i=0; i<m(); i++)
389 {
390 scalar sum = 0.0;
391 for (label j=0; j<m(); j++)
392 {
393 if (i != j)
394 {
395 sum += operator[](i)[j];
396 }
397 }
398 Info<< mag(sum)/mag(operator[](i)[i]) << endl;
399 }
400}
401
402
404{
406 pivotIndices_.setSize(m());
407 LUDecompose(*this, pivotIndices_);
408}
409
410
412{
413 scalarField source(m());
414
415 for (label j=0; j<m(); j++)
416 {
417 source = Zero;
418 source[j] = 1;
419 LUBacksubstitute(*this, pivotIndices_, source);
420 for (label i=0; i<m(); i++)
421 {
422 M(i, j) = source[i];
423 }
424 }
425}
426
427
428// ************************************************************************* //
#define M(I)
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:80
Input inter-processor communications stream.
Definition: IPstream.H:57
Class to perform the LU decomposition on a symmetric matrix.
LUscalarMatrix()
Construct null.
void setSize(const label n)
Alias for resize()
Definition: List.H:218
label n() const noexcept
The number of columns.
Definition: MatrixI.H:103
void transfer(Matrix< Form, Type > &mat)
Definition: Matrix.C:304
const Type * operator[](const label irow) const
Return const pointer to data in the specified row - rowData().
Definition: MatrixI.H:614
label m() const noexcept
The number of rows.
Definition: MatrixI.H:96
Output inter-processor communications stream.
Definition: OPstream.H:57
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
UPstream::rangeType subProcs() const noexcept
Range of sub-processes indices associated with PstreamBuffers.
Inter-processor communications stream.
Definition: Pstream.H:63
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
const T * set(const label i) const
Definition: PtrList.H:138
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:556
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
const T * set(const label i) const
Definition: UPtrList.H:248
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
iterator begin() noexcept
Return an iterator to begin of UPtrList traversal.
Definition: UPtrListI.H:620
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:57
gradingDescriptor inv() const
Return the inverse gradingDescriptor with 1/expansionRatio.
virtual const labelUList & upperAddr() const =0
Return upper addressing.
label size() const
Return number of equations.
virtual const labelUList & lowerAddr() const =0
Return lower addressing.
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:84
scalarField & upper()
Definition: lduMatrix.C:203
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition: lduMatrix.H:578
scalarField & lower()
Definition: lduMatrix.C:174
scalarField & diag()
Definition: lduMatrix.C:192
I/O for lduMatrix and interface values.
Definition: procLduMatrix.H:64
splitCell * master() const
Definition: splitCell.H:113
bool decompose() const noexcept
Query the decompose flag (normally off)
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const labelList nFaces(UPstream::listGatherValues< label >(aMesh.nFaces()))
Namespace for OpenFOAM.
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
interfaceProperties interface(alpha1, U, thermo->transportPropertiesDict())
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333