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 -------------------------------------------------------------------------------
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(matrix),
54  comm_(UPstream::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 (UPstream::parRun())
71  {
72  PtrList<procLduMatrix> lduMatrices(UPstream::nProcs(comm_));
73 
74  label lduMatrixi = 0;
75 
76  lduMatrices.set
77  (
78  lduMatrixi++,
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  lduMatrices.set
92  (
93  lduMatrixi++,
94  new procLduMatrix
95  (
96  IPstream
97  (
99  proci,
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 (UPstream::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 (UPstream::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 
193 void 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 
257 void 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 
386 void 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 // ************************************************************************* //
UPtrList< const lduInterfaceField > lduInterfaceFieldPtrsList
List of coupled interface fields to be used in coupling.
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
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:598
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:1049
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1229
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:594
void transfer(Matrix< SquareMatrix< scalar >, scalar > &mat)
Transfer the contents of the argument Matrix into this Matrix and annul the argument Matrix...
Definition: Matrix.C:295
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
Input inter-processor communications stream.
Definition: IPstream.H:49
void decompose(const scalarSquareMatrix &M)
Perform the LU decomposition of the matrix M.
label n() const noexcept
The number of columns.
Definition: Matrix.H:258
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:1065
void setSize(const label n)
Alias for resize()
Definition: List.H:316
dynamicFvMesh & mesh
"scheduled" : (MPI_Send, MPI_Recv)
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition: UPstream.H:1059
LUscalarMatrix()
Construct null.
int debug
Static debugging option.
defineTypeNameAndDebug(combustionModel, 0)
label m() const noexcept
The number of rows.
Definition: Matrix.H:248
lduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: lduMatrix.H:79
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition: lduMatrix.H:734
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
messageStream Info
Information stream (stdout output on master, null elsewhere)
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:1185
I/O for lduMatrix and interface values.
Definition: procLduMatrix.H:59
SquareMatrix< scalar > scalarSquareMatrix
#define M(I)
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Inter-processor communications stream.
Definition: UPstream.H:60
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.
label size() const
Return number of equations.
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