Loading...
Searching...
No Matches
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-2024 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"
33#include "cyclicLduInterface.H"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
42
43// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46:
47 comm_(UPstream::worldComm)
48{}
49
50
52:
54 comm_(UPstream::worldComm)
55{
56 LUDecompose(*this, pivotIndices_);
57}
58
59
61(
62 const lduMatrix& ldum,
63 const FieldField<Field, scalar>& interfaceCoeffs,
64 const lduInterfaceFieldPtrsList& interfaces
65)
66:
67 comm_(ldum.mesh().comm())
68{
69 if (UPstream::parRun())
70 {
71 PtrList<procLduMatrix> lduMatrices
72 (
73 UPstream::master(comm_) ? UPstream::nProcs(comm_) : 1
74 );
75
76 lduMatrices.set
77 (
78 0, // rank-local matrix (and/or master)
80 (
81 ldum,
82 interfaceCoeffs,
83 interfaces
84 )
85 );
86
87 if (UPstream::master(comm_))
88 {
89 for (const int proci : UPstream::subProcs(comm_))
90 {
91 auto& mat = lduMatrices.emplace_set(proci);
92
93 IPstream::recv(mat, proci, UPstream::msgType(), comm_);
94 }
95
96 convert(lduMatrices);
97 }
98 else
99 {
101 (
102 lduMatrices[0], // rank-local matrix
105 comm_
106 );
107 }
108 }
109 else
110 {
111 convert(ldum, interfaceCoeffs, interfaces);
112 }
113
114
115 if (debug && UPstream::master(comm_))
116 {
117 const label numRows = nRows();
118 const label numCols = nCols();
119
120 Pout<< "LUscalarMatrix : size:" << numRows << endl;
121 for (label rowi = 0; rowi < numRows; ++rowi)
122 {
123 const scalar* row = operator[](rowi);
124
125 Pout<< "cell:" << rowi << " diagCoeff:" << row[rowi] << nl;
126
127 Pout<< " connects to upper cells :";
128 for (label coli = rowi+1; coli < numCols; ++coli)
129 {
130 if (mag(row[coli]) > SMALL)
131 {
132 Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
133 }
134 }
135 Pout<< nl;
136 Pout<< " connects to lower cells :";
137 for (label coli = 0; coli < rowi; ++coli)
138 {
139 if (mag(row[coli]) > SMALL)
140 {
141 Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
142 }
143 }
144 Pout<< nl;
145 }
146 Pout<< endl;
147 }
148
149 if (UPstream::master(comm_))
150 {
151 LUDecompose(*this, pivotIndices_);
152 }
153}
154
155
156// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157
158void Foam::LUscalarMatrix::convert
159(
160 const lduMatrix& ldum,
161 const FieldField<Field, scalar>& interfaceCoeffs,
162 const lduInterfaceFieldPtrsList& interfaces
163)
164{
165 // Resize and fill with zero
166 scalarSquareMatrix::resize_nocopy(ldum.lduAddr().size());
167 scalarSquareMatrix::operator=(Foam::zero{});
168
169 const label* __restrict__ uPtr = ldum.lduAddr().upperAddr().begin();
170 const label* __restrict__ lPtr = ldum.lduAddr().lowerAddr().begin();
171
172 const scalar* __restrict__ diagPtr = ldum.diag().begin();
173 const scalar* __restrict__ upperPtr = ldum.upper().begin();
174 const scalar* __restrict__ lowerPtr = ldum.lower().begin();
175
176 const label nCells = ldum.diag().size();
177 const label nFaces = ldum.upper().size();
178
179 for (label cell=0; cell<nCells; cell++)
180 {
181 operator[](cell)[cell] = diagPtr[cell];
182 }
183
184 for (label face=0; face<nFaces; face++)
185 {
186 label uCell = uPtr[face];
187 label lCell = lPtr[face];
188
189 operator[](uCell)[lCell] = lowerPtr[face];
190 operator[](lCell)[uCell] = upperPtr[face];
191 }
192
193 forAll(interfaces, inti)
194 {
195 if (interfaces.set(inti))
196 {
197 const lduInterface& interface = interfaces[inti].interface();
198
199 // Assume any interfaces are cyclic ones
200
201 const label* __restrict__ lPtr = interface.faceCells().begin();
202
203 const cyclicLduInterface& cycInterface =
205 label nbrInt = cycInterface.neighbPatchID();
206 const label* __restrict__ uPtr =
207 interfaces[nbrInt].interface().faceCells().begin();
208
209 const scalar* __restrict__ nbrUpperLowerPtr =
210 interfaceCoeffs[nbrInt].begin();
211
212 label inFaces = interface.faceCells().size();
213
214 for (label face=0; face<inFaces; face++)
215 {
216 label uCell = lPtr[face];
217 label lCell = uPtr[face];
218
219 operator[](uCell)[lCell] -= nbrUpperLowerPtr[face];
220 }
221 }
222 }
223}
224
225
226void Foam::LUscalarMatrix::convert
227(
228 const PtrList<procLduMatrix>& lduMatrices
229)
230{
231 procOffsets_.resize_nocopy(lduMatrices.size() + 1);
232
233 {
234 auto iter = procOffsets_.begin();
235
236 label nCellsTotal = 0;
237 *iter++ = nCellsTotal;
238
239 for (const auto& mat : lduMatrices)
240 {
241 nCellsTotal += mat.size();
242 *iter++ = nCellsTotal;
243 }
244
245 // Resize and fill with zero
247 scalarSquareMatrix::operator=(Foam::zero{});
248 }
249
250
251 forAll(lduMatrices, ldumi)
252 {
253 const procLduMatrix& lduMatrixi = lduMatrices[ldumi];
254 label offset = procOffsets_[ldumi];
255
256 const label* __restrict__ uPtr = lduMatrixi.upperAddr_.begin();
257 const label* __restrict__ lPtr = lduMatrixi.lowerAddr_.begin();
258
259 const scalar* __restrict__ diagPtr = lduMatrixi.diag_.begin();
260 const scalar* __restrict__ upperPtr = lduMatrixi.upper_.begin();
261 const scalar* __restrict__ lowerPtr = lduMatrixi.lower_.begin();
262
263 const label nCells = lduMatrixi.size();
264 const label nFaces = lduMatrixi.upper_.size();
265
266 for (label cell=0; cell<nCells; cell++)
267 {
268 label globalCell = cell + offset;
269 operator[](globalCell)[globalCell] = diagPtr[cell];
270 }
271
272 for (label face=0; face<nFaces; face++)
273 {
274 label uCell = uPtr[face] + offset;
275 label lCell = lPtr[face] + offset;
276
277 operator[](uCell)[lCell] = lowerPtr[face];
278 operator[](lCell)[uCell] = upperPtr[face];
279 }
280
281 const PtrList<procLduInterface>& interfaces =
282 lduMatrixi.interfaces_;
283
284 forAll(interfaces, inti)
285 {
286 const procLduInterface& interface = interfaces[inti];
287
288 if (interface.myProcNo_ == interface.neighbProcNo_)
289 {
290 const label* __restrict__ ulPtr = interface.faceCells_.begin();
291
292 const scalar* __restrict__ upperLowerPtr =
293 interface.coeffs_.begin();
294
295 label inFaces = interface.faceCells_.size()/2;
296
297 for (label face=0; face<inFaces; face++)
298 {
299 label uCell = ulPtr[face] + offset;
300 label lCell = ulPtr[face + inFaces] + offset;
301
302 operator[](uCell)[lCell] -= upperLowerPtr[face + inFaces];
303 operator[](lCell)[uCell] -= upperLowerPtr[face];
304 }
305 }
306 else if (interface.myProcNo_ < interface.neighbProcNo_)
307 {
308 // Interface to neighbour proc. Find on neighbour proc the
309 // corresponding interface. The problem is that there can
310 // be multiple interfaces between two processors (from
311 // processorCyclics) so also compare the communication tag
312
313 const PtrList<procLduInterface>& neiInterfaces =
314 lduMatrices[interface.neighbProcNo_].interfaces_;
315
316 label neiInterfacei = -1;
317
318 forAll(neiInterfaces, ninti)
319 {
320 if
321 (
322 (
323 neiInterfaces[ninti].neighbProcNo_
324 == interface.myProcNo_
325 )
326 && (neiInterfaces[ninti].tag_ == interface.tag_)
327 )
328 {
329 neiInterfacei = ninti;
330 break;
331 }
332 }
333
334 if (neiInterfacei == -1)
335 {
337 }
338
339 const procLduInterface& neiInterface =
340 neiInterfaces[neiInterfacei];
341
342 const label* __restrict__ uPtr = interface.faceCells_.begin();
343 const label* __restrict__ lPtr =
344 neiInterface.faceCells_.begin();
345
346 const scalar* __restrict__ upperPtr = interface.coeffs_.begin();
347 const scalar* __restrict__ lowerPtr =
348 neiInterface.coeffs_.begin();
349
350 label inFaces = interface.faceCells_.size();
351 label neiOffset = procOffsets_[interface.neighbProcNo_];
352
353 for (label face=0; face<inFaces; face++)
354 {
355 label uCell = uPtr[face] + offset;
356 label lCell = lPtr[face] + neiOffset;
357
358 operator[](uCell)[lCell] -= lowerPtr[face];
359 operator[](lCell)[uCell] -= upperPtr[face];
360 }
361 }
362 }
363 }
364}
365
366
367void Foam::LUscalarMatrix::printDiagonalDominance() const
368{
369 for (label i=0; i<m(); i++)
370 {
371 scalar sum = 0.0;
372 for (label j=0; j<m(); j++)
373 {
374 if (i != j)
375 {
376 sum += operator[](i)[j];
378 }
379 Info<< mag(sum)/mag(operator[](i)[i]) << endl;
380 }
381}
382
383
385{
387 LUDecompose(*this, pivotIndices_);
388}
389
390
392{
393 scalarField source(m());
394
395 for (label j=0; j<m(); j++)
396 {
397 source = Zero;
398 source[j] = 1;
399 LUBacksubstitute(*this, pivotIndices_, source);
400 for (label i=0; i<m(); i++)
401 {
402 M(i, j) = source[i];
403 }
404 }
405}
406
407
408// ************************************************************************* //
#define M(I)
A field of fields is a PtrList of fields with reference counting.
Definition FieldField.H:77
static void recv(Type &value, const int fromProcNo, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Receive and deserialize a value. Uses operator>> for de-serialization.
Definition IPstream.H:80
Class to perform the LU decomposition on a symmetric matrix.
LUscalarMatrix() noexcept
Default construct.
void decompose(const scalarSquareMatrix &mat)
Perform the LU decomposition of the matrix.
void inv(scalarSquareMatrix &M) const
Set M to the inverse of this square matrix.
label m() const noexcept
The number of rows.
Definition Matrix.H:261
iterator begin() noexcept
Return an iterator to begin traversing a Matrix.
Definition MatrixI.H:509
label nCols() const noexcept
The number of columns.
Definition Matrix.H:266
label nRows() const noexcept
The number of rows.
Definition Matrix.H:256
const Type * operator[](const label irow) const
Return const pointer to data in the specified row - rowData().
Definition MatrixI.H:588
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition PtrList.H:171
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location).
Definition PtrListI.H:191
void resize_nocopy(const label n)
SquareMatrix & operator=(const SquareMatrix &)=default
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
bool send()
Send buffer contents now and not in destructor [advanced usage]. Returns true on success.
Definition OPstreams.C:84
Inter-processor communications stream.
Definition UPstream.H:69
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
static int & msgType() noexcept
Message tag of standard messages.
Definition UPstream.H:1926
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition UPstream.H:1691
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run.
Definition UPstream.H:1697
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition UPstream.H:1866
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrList.H:366
iterator begin()
Return iterator to begin traversal of non-nullptr entries.
Definition UPtrListI.H:416
A cell is defined as a list of faces with extra functionality.
Definition cell.H:56
An abstract base class for cyclic coupled interfaces.
A face is a list of labels corresponding to mesh vertices.
Definition face.H:71
virtual const labelUList & upperAddr() const =0
Return upper addressing.
virtual const labelUList & lowerAddr() const =0
Return lower addressing.
label size() const noexcept
Return number of equations.
An abstract base class for implicitly-coupled interfaces e.g. processor and cyclic patches.
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition lduMatrix.H:81
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition lduMatrix.H:769
const scalarField & diag() const
Definition lduMatrix.C:195
const scalarField & upper() const
Definition lduMatrix.C:235
const scalarField & lower() const
Definition lduMatrix.C:306
IO interface for processorLduInterface.
I/O for lduMatrix and interface values.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
Namespace for handling debugging switches.
Definition debug.C:45
Namespace for OpenFOAM.
Type & refCast(U &obj)
A dynamic_cast (for references) to Type reference.
Definition typeInfo.H:172
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution in the source.
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
SquareMatrix< scalar > scalarSquareMatrix
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
UPtrList< const lduInterfaceField > lduInterfaceFieldPtrsList
List of coupled interface fields to be used in coupling.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
interfaceProperties interface(alpha1, U, thermo->transportPropertiesDict())
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299