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 {
39  defineTypeNameAndDebug(LUscalarMatrix, 0);
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++,
79  new procLduMatrix
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++,
94  new procLduMatrix
95  (
96  IPstream
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 
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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::lduMatrix::lduAddr
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition: lduMatrix.H:578
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
Foam::FieldField
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:53
Foam::LUscalarMatrix::LUscalarMatrix
LUscalarMatrix()
Construct null.
Definition: LUscalarMatrix.C:45
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:53
Foam::UPtrList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:600
Foam::lduMatrix::upper
scalarField & upper()
Definition: lduMatrix.C:203
interface
interfaceProperties interface(alpha1, U, thermo->transportPropertiesDict())
Foam::lduMatrix
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:83
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrList.H:138
lduMatrix.H
LUscalarMatrix.H
cyclicLduInterface.H
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::lduAddressing::size
label size() const
Return number of equations.
Definition: lduAddressing.H:171
Foam::lduAddressing::upperAddr
virtual const labelUList & upperAddr() const =0
Return upper addressing.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::procLduMatrix
I/O for lduMatrix and interface values.
Definition: procLduMatrix.H:63
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UPstream::subProcs
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:515
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::LUscalarMatrix::inv
void inv(scalarSquareMatrix &M) const
Set M to the inverse of this square matrix.
Definition: LUscalarMatrix.C:411
Foam::UPtrList< const lduInterfaceField >
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::lduMatrix::diag
scalarField & diag()
Definition: lduMatrix.C:192
Foam::SquareMatrix< scalar >::operator=
SquareMatrix & operator=(const SquareMatrix &)=default
Copy assignment.
Foam::LUBacksubstitute
void LUBacksubstitute(const scalarSquareMatrix &luMmatrix, const labelList &pivotIndices, List< Type > &source)
Definition: scalarMatricesTemplates.C:120
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::LUDecompose
void LUDecompose(scalarSquareMatrix &matrix, labelList &pivotIndices)
LU decompose the matrix with pivoting.
Definition: scalarMatrices.C:34
procLduInterface.H
Foam::SquareMatrix< scalar >
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::UPtrList::set
const T * set(const label i) const
Definition: UPtrList.H:176
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::UPstream::commsTypes::scheduled
Foam::lduAddressing::lowerAddr
virtual const labelUList & lowerAddr() const =0
Return lower addressing.
Foam::Pstream
Inter-processor communications stream.
Definition: Pstream.H:56
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
M
#define M(I)
Foam::lduMatrix::mesh
const lduMesh & mesh() const
Return the LDU mesh from which the addressing is obtained.
Definition: lduMatrix.H:566
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::LUscalarMatrix::decompose
void decompose(const scalarSquareMatrix &M)
Perform the LU decomposition of the matrix M.
Definition: LUscalarMatrix.C:403
Foam::lduMesh::comm
virtual label comm() const =0
Return communicator used for parallel communication.
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:53
procLduMatrix.H
Foam::lduMatrix::lower
scalarField & lower()
Definition: lduMatrix.C:174
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::cell
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:54
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445