LduMatrix.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "lduMatrix.H"
27 #include "IOstreams.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Type, class DType, class LUType>
33 :
34  lduMesh_(mesh),
35  diagPtr_(nullptr),
36  upperPtr_(nullptr),
37  lowerPtr_(nullptr),
38  sourcePtr_(nullptr),
39  interfaces_(0),
40  interfacesUpper_(0),
41  interfacesLower_(0)
42 {}
43 
44 
45 template<class Type, class DType, class LUType>
47 :
48  lduMesh_(A.lduMesh_),
49  diagPtr_(nullptr),
50  upperPtr_(nullptr),
51  lowerPtr_(nullptr),
52  sourcePtr_(nullptr),
53  interfaces_(0),
54  interfacesUpper_(0),
55  interfacesLower_(0)
56 {
57  if (A.diagPtr_)
58  {
59  diagPtr_ = new Field<DType>(*(A.diagPtr_));
60  }
61 
62  if (A.upperPtr_)
63  {
64  upperPtr_ = new Field<LUType>(*(A.upperPtr_));
65  }
66 
67  if (A.lowerPtr_)
68  {
69  lowerPtr_ = new Field<LUType>(*(A.lowerPtr_));
70  }
71 
72  if (A.sourcePtr_)
73  {
74  sourcePtr_ = new Field<Type>(*(A.sourcePtr_));
75  }
76 }
77 
78 
79 template<class Type, class DType, class LUType>
81 :
82  lduMesh_(A.lduMesh_),
83  diagPtr_(nullptr),
84  upperPtr_(nullptr),
85  lowerPtr_(nullptr),
86  sourcePtr_(nullptr),
87  interfaces_(0),
88  interfacesUpper_(0),
89  interfacesLower_(0)
90 {
91  if (reuse)
92  {
93  if (A.diagPtr_)
94  {
95  diagPtr_ = A.diagPtr_;
96  A.diagPtr_ = nullptr;
97  }
98 
99  if (A.upperPtr_)
100  {
101  upperPtr_ = A.upperPtr_;
102  A.upperPtr_ = nullptr;
103  }
104 
105  if (A.lowerPtr_)
106  {
107  lowerPtr_ = A.lowerPtr_;
108  A.lowerPtr_ = nullptr;
109  }
110 
111  if (A.sourcePtr_)
112  {
113  sourcePtr_ = A.sourcePtr_;
114  A.sourcePtr_ = nullptr;
115  }
116  }
117  else
118  {
119  if (A.diagPtr_)
120  {
121  diagPtr_ = new Field<DType>(*(A.diagPtr_));
122  }
123 
124  if (A.upperPtr_)
125  {
126  upperPtr_ = new Field<LUType>(*(A.upperPtr_));
127  }
128 
129  if (A.lowerPtr_)
130  {
131  lowerPtr_ = new Field<LUType>(*(A.lowerPtr_));
132  }
133 
134  if (A.sourcePtr_)
135  {
136  sourcePtr_ = new Field<Type>(*(A.sourcePtr_));
137  }
138  }
139 }
140 
141 
142 template<class Type, class DType, class LUType>
144 (
145  const lduMesh& mesh,
146  Istream& is
147 )
148 :
149  lduMesh_(mesh),
150  diagPtr_(new Field<DType>(is)),
151  upperPtr_(new Field<LUType>(is)),
152  lowerPtr_(new Field<LUType>(is)),
153  sourcePtr_(new Field<Type>(is)),
154  interfaces_(0),
155  interfacesUpper_(0),
156  interfacesLower_(0)
157 {}
158 
159 
160 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
161 
162 template<class Type, class DType, class LUType>
164 {
165  if (diagPtr_)
166  {
167  delete diagPtr_;
168  }
169 
170  if (upperPtr_)
171  {
172  delete upperPtr_;
173  }
174 
175  if (lowerPtr_)
176  {
177  delete lowerPtr_;
178  }
179 
180  if (sourcePtr_)
181  {
182  delete sourcePtr_;
183  }
184 }
185 
186 
187 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
188 
189 template<class Type, class DType, class LUType>
191 {
192  if (!diagPtr_)
193  {
194  diagPtr_ = new Field<DType>(lduAddr().size(), Zero);
195  }
196 
197  return *diagPtr_;
198 }
199 
200 
201 template<class Type, class DType, class LUType>
203 {
204  if (!upperPtr_)
205  {
206  if (lowerPtr_)
207  {
208  upperPtr_ = new Field<LUType>(*lowerPtr_);
209  }
210  else
211  {
212  upperPtr_ = new Field<LUType>
213  (
214  lduAddr().lowerAddr().size(),
215  Zero
216  );
217  }
218  }
219 
220  return *upperPtr_;
221 }
222 
223 
224 template<class Type, class DType, class LUType>
226 {
227  if (!lowerPtr_)
228  {
229  if (upperPtr_)
230  {
231  lowerPtr_ = new Field<LUType>(*upperPtr_);
232  }
233  else
234  {
235  lowerPtr_ = new Field<LUType>
236  (
237  lduAddr().lowerAddr().size(),
238  Zero
239  );
240  }
241  }
242 
243  return *lowerPtr_;
244 }
245 
246 
247 template<class Type, class DType, class LUType>
249 {
250  if (!sourcePtr_)
251  {
252  sourcePtr_ = new Field<Type>(lduAddr().size(), Zero);
253  }
254 
255  return *sourcePtr_;
256 }
257 
258 
259 template<class Type, class DType, class LUType>
261 {
262  if (!diagPtr_)
263  {
265  << "diagPtr_ unallocated"
266  << abort(FatalError);
267  }
268 
269  return *diagPtr_;
270 }
271 
272 
273 template<class Type, class DType, class LUType>
275 {
276  if (!lowerPtr_ && !upperPtr_)
277  {
279  << "lowerPtr_ or upperPtr_ unallocated"
280  << abort(FatalError);
281  }
282 
283  if (upperPtr_)
284  {
285  return *upperPtr_;
286  }
287  else
288  {
289  return *lowerPtr_;
290  }
291 }
292 
293 
294 template<class Type, class DType, class LUType>
296 {
297  if (!lowerPtr_ && !upperPtr_)
298  {
300  << "lowerPtr_ or upperPtr_ unallocated"
301  << abort(FatalError);
302  }
303 
304  if (lowerPtr_)
305  {
306  return *lowerPtr_;
307  }
308  else
309  {
310  return *upperPtr_;
311  }
312 }
313 
314 
315 template<class Type, class DType, class LUType>
317 {
318  if (!sourcePtr_)
319  {
321  << "sourcePtr_ unallocated"
322  << abort(FatalError);
323  }
324 
325  return *sourcePtr_;
326 }
327 
328 
329 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
330 
331 template<class Type, class DType, class LUType>
332 Foam::Ostream& Foam::operator<<
333 (
334  Ostream& os,
336 )
337 {
338  if (ldum.diagPtr_)
339  {
340  os << "Diagonal = "
341  << *ldum.diagPtr_
342  << endl << endl;
343  }
344 
345  if (ldum.upperPtr_)
346  {
347  os << "Upper triangle = "
348  << *ldum.upperPtr_
349  << endl << endl;
350  }
351 
352  if (ldum.lowerPtr_)
353  {
354  os << "Lower triangle = "
355  << *ldum.lowerPtr_
356  << endl << endl;
357  }
358 
359  if (ldum.sourcePtr_)
360  {
361  os << "Source = "
362  << *ldum.sourcePtr_
363  << endl << endl;
364  }
365 
366  os.check("Ostream& operator<<(Ostream&, const LduMatrix&");
367 
368  return os;
369 }
370 
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 
374 #include "LduMatrixOperations.C"
375 #include "LduMatrixATmul.C"
377 #include "LduMatrixPreconditioner.C"
378 #include "LduMatrixSmoother.C"
379 #include "LduMatrixSolver.C"
380 
381 // ************************************************************************* //
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Field< Type > & source()
Definition: LduMatrix.C:248
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
LduMatrix(const lduMesh &)
Construct given an LDU addressed mesh.
Definition: LduMatrix.C:32
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Abstract base class for meshes which provide LDU addressing for the construction of lduMatrix and LDU...
Definition: lduMesh.H:59
Field< LUType > & lower()
Definition: LduMatrix.C:225
virtual const labelUList & lowerAddr() const =0
Return lower addressing.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Field< LUType > & upper()
Definition: LduMatrix.C:202
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const lduAddressing & lduAddr() const
Return the LDU addressing.
Definition: LduMatrix.H:498
Field< DType > & diag()
Definition: LduMatrix.C:190
LduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: LduMatrix.H:69
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
label size() const
Return number of equations.