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 -------------------------------------------------------------------------------
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 "LUscalarMatrix.H"
30 #include "lduMatrix.H"
31 #include "procLduMatrix.H"
32 #include "procLduInterface.H"
33 #include "cyclicLduInterface.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 :
47  comm_(UPstream::worldComm)
48 {}
49 
50 
52 :
53  scalarSquareMatrix(mat),
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)
79  new procLduMatrix
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 
158 void 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());
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 =
204  refCast<const cyclicLduInterface>(interface);
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 
226 void 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
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 
367 void 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];
377  }
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 // ************************************************************************* //
UPtrList< const lduInterfaceField > lduInterfaceFieldPtrsList
List of coupled interface fields to be used in coupling.
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
LUscalarMatrix() noexcept
Default construct.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
Class to perform the LU decomposition on a symmetric matrix.
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1061
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1252
interfaceProperties interface(alpha1, U, thermo->transportPropertiesDict())
const scalar * operator[](const label irow) const
Return const pointer to data in the specified row - rowData().
Definition: MatrixI.H:587
iterator begin() noexcept
Return an iterator to begin traversing a Matrix.
Definition: MatrixI.H:508
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:51
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
label nCols() const noexcept
The number of columns.
Definition: Matrix.H:257
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:1077
static void recv(Type &value, const int fromProcNo, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Receive and deserialize a value. Uses operator>> for de-serialization.
Definition: IPstream.H:81
dynamicFvMesh & mesh
label nRows() const noexcept
The number of rows.
Definition: Matrix.H:247
void resize_nocopy(const label n)
Resize the matrix without preserving existing content.
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition: UPstream.H:1071
const direction noexcept
Definition: Scalar.H:258
int debug
Static debugging option.
defineTypeNameAndDebug(combustionModel, 0)
lduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: lduMatrix.H:80
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
void decompose(const scalarSquareMatrix &mat)
Perform the LU decomposition of the matrix.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
messageStream Info
Information stream (stdout output on master, null elsewhere)
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:1197
I/O for lduMatrix and interface values.
Definition: procLduMatrix.H:58
bool send()
Send buffer contents now and not in destructor [advanced usage]. Returns true on success.
Definition: OPstreams.C:84
#define M(I)
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Inter-processor communications stream.
Definition: UPstream.H:65
Namespace for OpenFOAM.
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
LU back-substitution with given source, returning the solution in the source.
void inv(scalarSquareMatrix &M) const
Set M to the inverse of this square matrix.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127