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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "LUscalarMatrix.H"
29 #include "lduMatrix.H"
30 #include "procLduMatrix.H"
31 #include "procLduInterface.H"
32 #include "cyclicLduInterface.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(LUscalarMatrix, 0);
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 :
46  comm_(Pstream::worldComm)
47 {}
48 
49 
51 :
52  scalarSquareMatrix(matrix),
53  comm_(Pstream::worldComm),
54  pivotIndices_(m())
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 (Pstream::parRun())
70  {
71  PtrList<procLduMatrix> lduMatrices(Pstream::nProcs(comm_));
72 
73  label lduMatrixi = 0;
74 
75  lduMatrices.set
76  (
77  lduMatrixi++,
78  new procLduMatrix
79  (
80  ldum,
81  interfaceCoeffs,
82  interfaces
83  )
84  );
85 
86  if (Pstream::master(comm_))
87  {
88  for
89  (
90  int slave=Pstream::firstSlave();
91  slave<=Pstream::lastSlave(comm_);
92  slave++
93  )
94  {
95  lduMatrices.set
96  (
97  lduMatrixi++,
98  new procLduMatrix
99  (
100  IPstream
101  (
103  slave,
104  0, // bufSize
106  comm_
107  )()
108  )
109  );
110  }
111  }
112  else
113  {
114  OPstream toMaster
115  (
118  0, // bufSize
120  comm_
121  );
122  procLduMatrix cldum
123  (
124  ldum,
125  interfaceCoeffs,
126  interfaces
127  );
128  toMaster<< cldum;
129 
130  }
131 
132  if (Pstream::master(comm_))
133  {
134  label nCells = 0;
135  forAll(lduMatrices, i)
136  {
137  nCells += lduMatrices[i].size();
138  }
139 
140  scalarSquareMatrix m(nCells, 0.0);
141  transfer(m);
142  convert(lduMatrices);
143  }
144  }
145  else
146  {
147  label nCells = ldum.lduAddr().size();
148  scalarSquareMatrix m(nCells, Zero);
149  transfer(m);
150  convert(ldum, interfaceCoeffs, interfaces);
151  }
152 
153  if (Pstream::master(comm_))
154  {
155  if (debug)
156  {
157  const label numRows = m();
158  const label numCols = n();
159 
160  Pout<< "LUscalarMatrix : size:" << numRows << endl;
161  for (label rowi = 0; rowi < numRows; ++rowi)
162  {
163  const scalar* row = operator[](rowi);
164 
165  Pout<< "cell:" << rowi << " diagCoeff:" << row[rowi] << endl;
166 
167  Pout<< " connects to upper cells :";
168  for (label coli = rowi+1; coli < numCols; ++coli)
169  {
170  if (mag(row[coli]) > SMALL)
171  {
172  Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
173  }
174  }
175  Pout<< endl;
176  Pout<< " connects to lower cells :";
177  for (label coli = 0; coli < rowi; ++coli)
178  {
179  if (mag(row[coli]) > SMALL)
180  {
181  Pout<< ' ' << coli << " (coeff:" << row[coli] << ')';
182  }
183  }
184  Pout<< nl;
185  }
186  Pout<< nl;
187  }
188 
189  pivotIndices_.setSize(m());
190  LUDecompose(*this, pivotIndices_);
191  }
192 }
193 
194 
195 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
196 
197 void Foam::LUscalarMatrix::convert
198 (
199  const lduMatrix& ldum,
200  const FieldField<Field, scalar>& interfaceCoeffs,
201  const lduInterfaceFieldPtrsList& interfaces
202 )
203 {
204  const label* __restrict__ uPtr = ldum.lduAddr().upperAddr().begin();
205  const label* __restrict__ lPtr = ldum.lduAddr().lowerAddr().begin();
206 
207  const scalar* __restrict__ diagPtr = ldum.diag().begin();
208  const scalar* __restrict__ upperPtr = ldum.upper().begin();
209  const scalar* __restrict__ lowerPtr = ldum.lower().begin();
210 
211  const label nCells = ldum.diag().size();
212  const label nFaces = ldum.upper().size();
213 
214  for (label cell=0; cell<nCells; cell++)
215  {
216  operator[](cell)[cell] = diagPtr[cell];
217  }
218 
219  for (label face=0; face<nFaces; face++)
220  {
221  label uCell = uPtr[face];
222  label lCell = lPtr[face];
223 
224  operator[](uCell)[lCell] = lowerPtr[face];
225  operator[](lCell)[uCell] = upperPtr[face];
226  }
227 
228  forAll(interfaces, inti)
229  {
230  if (interfaces.set(inti))
231  {
232  const lduInterface& interface = interfaces[inti].interface();
233 
234  // Assume any interfaces are cyclic ones
235 
236  const label* __restrict__ lPtr = interface.faceCells().begin();
237 
238  const cyclicLduInterface& cycInterface =
239  refCast<const cyclicLduInterface>(interface);
240  label nbrInt = cycInterface.neighbPatchID();
241  const label* __restrict__ uPtr =
242  interfaces[nbrInt].interface().faceCells().begin();
243 
244  const scalar* __restrict__ nbrUpperLowerPtr =
245  interfaceCoeffs[nbrInt].begin();
246 
247  label inFaces = interface.faceCells().size();
248 
249  for (label face=0; face<inFaces; face++)
250  {
251  label uCell = lPtr[face];
252  label lCell = uPtr[face];
253 
254  operator[](uCell)[lCell] -= nbrUpperLowerPtr[face];
255  }
256  }
257  }
258 }
259 
260 
261 void Foam::LUscalarMatrix::convert
262 (
263  const PtrList<procLduMatrix>& lduMatrices
264 )
265 {
266  procOffsets_.setSize(lduMatrices.size() + 1);
267  procOffsets_[0] = 0;
268 
269  forAll(lduMatrices, ldumi)
270  {
271  procOffsets_[ldumi+1] = procOffsets_[ldumi] + lduMatrices[ldumi].size();
272  }
273 
274  forAll(lduMatrices, ldumi)
275  {
276  const procLduMatrix& lduMatrixi = lduMatrices[ldumi];
277  label offset = procOffsets_[ldumi];
278 
279  const label* __restrict__ uPtr = lduMatrixi.upperAddr_.begin();
280  const label* __restrict__ lPtr = lduMatrixi.lowerAddr_.begin();
281 
282  const scalar* __restrict__ diagPtr = lduMatrixi.diag_.begin();
283  const scalar* __restrict__ upperPtr = lduMatrixi.upper_.begin();
284  const scalar* __restrict__ lowerPtr = lduMatrixi.lower_.begin();
285 
286  const label nCells = lduMatrixi.size();
287  const label nFaces = lduMatrixi.upper_.size();
288 
289  for (label cell=0; cell<nCells; cell++)
290  {
291  label globalCell = cell + offset;
292  operator[](globalCell)[globalCell] = diagPtr[cell];
293  }
294 
295  for (label face=0; face<nFaces; face++)
296  {
297  label uCell = uPtr[face] + offset;
298  label lCell = lPtr[face] + offset;
299 
300  operator[](uCell)[lCell] = lowerPtr[face];
301  operator[](lCell)[uCell] = upperPtr[face];
302  }
303 
304  const PtrList<procLduInterface>& interfaces =
305  lduMatrixi.interfaces_;
306 
307  forAll(interfaces, inti)
308  {
309  const procLduInterface& interface = interfaces[inti];
310 
311  if (interface.myProcNo_ == interface.neighbProcNo_)
312  {
313  const label* __restrict__ ulPtr = interface.faceCells_.begin();
314 
315  const scalar* __restrict__ upperLowerPtr =
316  interface.coeffs_.begin();
317 
318  label inFaces = interface.faceCells_.size()/2;
319 
320  for (label face=0; face<inFaces; face++)
321  {
322  label uCell = ulPtr[face] + offset;
323  label lCell = ulPtr[face + inFaces] + offset;
324 
325  operator[](uCell)[lCell] -= upperLowerPtr[face + inFaces];
326  operator[](lCell)[uCell] -= upperLowerPtr[face];
327  }
328  }
329  else if (interface.myProcNo_ < interface.neighbProcNo_)
330  {
331  // Interface to neighbour proc. Find on neighbour proc the
332  // corresponding interface. The problem is that there can
333  // be multiple interfaces between two processors (from
334  // processorCyclics) so also compare the communication tag
335 
336  const PtrList<procLduInterface>& neiInterfaces =
337  lduMatrices[interface.neighbProcNo_].interfaces_;
338 
339  label neiInterfacei = -1;
340 
341  forAll(neiInterfaces, ninti)
342  {
343  if
344  (
345  (
346  neiInterfaces[ninti].neighbProcNo_
347  == interface.myProcNo_
348  )
349  && (neiInterfaces[ninti].tag_ == interface.tag_)
350  )
351  {
352  neiInterfacei = ninti;
353  break;
354  }
355  }
356 
357  if (neiInterfacei == -1)
358  {
360  }
361 
362  const procLduInterface& neiInterface =
363  neiInterfaces[neiInterfacei];
364 
365  const label* __restrict__ uPtr = interface.faceCells_.begin();
366  const label* __restrict__ lPtr =
367  neiInterface.faceCells_.begin();
368 
369  const scalar* __restrict__ upperPtr = interface.coeffs_.begin();
370  const scalar* __restrict__ lowerPtr =
371  neiInterface.coeffs_.begin();
372 
373  label inFaces = interface.faceCells_.size();
374  label neiOffset = procOffsets_[interface.neighbProcNo_];
375 
376  for (label face=0; face<inFaces; face++)
377  {
378  label uCell = uPtr[face] + offset;
379  label lCell = lPtr[face] + neiOffset;
380 
381  operator[](uCell)[lCell] -= lowerPtr[face];
382  operator[](lCell)[uCell] -= upperPtr[face];
383  }
384  }
385  }
386  }
387 }
388 
389 
390 void Foam::LUscalarMatrix::printDiagonalDominance() const
391 {
392  for (label i=0; i<m(); i++)
393  {
394  scalar sum = 0.0;
395  for (label j=0; j<m(); j++)
396  {
397  if (i != j)
398  {
399  sum += operator[](i)[j];
400  }
401  }
402  Info<< mag(sum)/mag(operator[](i)[i]) << endl;
403  }
404 }
405 
406 
408 {
410  pivotIndices_.setSize(m());
411  LUDecompose(*this, pivotIndices_);
412 }
413 
414 
416 {
417  scalarField source(m());
418 
419  for (label j=0; j<m(); j++)
420  {
421  source = Zero;
422  source[j] = 1;
423  LUBacksubstitute(*this, pivotIndices_, source);
424  for (label i=0; i<m(); i++)
425  {
426  M(i, j) = source[i];
427  }
428  }
429 }
430 
431 
432 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::lduMatrix::lduAddr
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition: lduMatrix.H:572
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master.
Definition: UPstream.H:432
Foam::FieldField
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:53
Foam::UPtrList::begin
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:600
Foam::LUscalarMatrix::LUscalarMatrix
LUscalarMatrix()
Construct null.
Definition: LUscalarMatrix.C:44
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::SquareMatrix< scalar >::operator=
void operator=(const zero)
Assign all elements to zero.
Definition: SquareMatrixI.H:301
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:52
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::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:426
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:414
Foam::UPtrList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: UPtrListI.H:176
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
lduMatrix.H
LUscalarMatrix.H
cyclicLduInterface.H
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
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.
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::procLduMatrix
I/O for lduMatrix and interface values.
Definition: procLduMatrix.H:63
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field< scalar >
Foam::UPstream::lastSlave
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:467
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::LUscalarMatrix::inv
void inv(scalarSquareMatrix &M) const
Set M to the inverse of this square matrix.
Definition: LUscalarMatrix.C:415
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:65
Foam::UPstream::commsTypes::scheduled
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: PtrListI.H:143
Foam::lduMatrix::diag
scalarField & diag()
Definition: lduMatrix.C:192
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
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::UPstream::firstSlave
static constexpr int firstSlave() noexcept
Process index of first slave.
Definition: UPstream.H:461
procLduInterface.H
Foam::SquareMatrix< scalar >
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:491
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::lduAddressing::lowerAddr
virtual const labelUList & lowerAddr() const =0
Return lower addressing.
Foam::Pstream
Inter-processor communications stream.
Definition: Pstream.H:56
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:407
Foam::lduMesh::comm
virtual label comm() const =0
Return communicator used for parallel communication.
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:52
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