MatrixBlock.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) 2016 OpenFOAM Foundation
9  Copyright (C) 2019 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 "MatrixBlock.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 template<class MatrixType>
35 {
36  if (nCols_ != 1)
37  {
39  << "Number of columns " << nCols_ << " != 1"
40  << abort(FatalError);
41  }
42 
43  Field<cmptType> f(mRows_);
44 
45  forAll(f, i)
46  {
47  f[i] = operator()(i, 0);
48  }
49 
50  return f;
51 }
52 
53 
54 template<class MatrixType>
56 {
57  if (nCols_ != 1)
58  {
60  << "Number of columns " << nCols_ << " != 1"
61  << abort(FatalError);
62  }
63 
64  Field<cmptType> f(mRows_);
65 
66  forAll(f, i)
67  {
68  f[i] = operator()(i, 0);
69  }
70 
71  return f;
72 }
73 
74 
75 template<class MatrixType>
77 (
78  const char* what
79 ) const
80 {
82  << "Block addresses " << what
83  << " outside matrix or invalid matrix components"
84  << abort(FatalError);
85  return 0;
86 }
87 
88 
89 template<class MatrixType>
91 (
92  const char* what
93 ) const
94 {
96  << "Block addresses " << what
97  << " outside matrix or invalid matrix components"
98  << abort(FatalError);
99  return 0;
100 }
101 
102 
103 template<class MatrixType> void Foam::ConstMatrixBlock<MatrixType>::checkIndex
104 (
105  const label i,
106  const label j
107 ) const
108 {
109  if (i < 0 || i >= mRows_)
110  {
112  << "Index " << i << " is out of range 0 ... " << mRows_ - 1
113  << abort(FatalError);
114  }
115  else if (j < 0 || j >= nCols_)
116  {
118  << "Index " << j << " is out of range 0 ... " << nCols_ - 1
119  << abort(FatalError);
120  }
121 }
122 
123 
124 template<class MatrixType> void Foam::MatrixBlock<MatrixType>::checkIndex
125 (
126  const label i,
127  const label j
128 ) const
129 {
130  if (i < 0 || i >= mRows_)
131  {
133  << "Index " << i << " is out of range 0 ... " << mRows_ - 1
134  << abort(FatalError);
135  }
136  else if (j < 0 || j >= nCols_)
137  {
139  << "Index " << j << " is out of range 0 ... " << nCols_ - 1
140  << abort(FatalError);
141  }
142 }
143 
144 
145 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
146 
147 template<class MatrixType>
148 template<class Form>
150 (
151  const Matrix<Form, cmptType>& Mb
152 )
153 {
154  if (mRows_ != Mb.m() || nCols_ != Mb.n())
155  {
157  << "Attempt to assign blocks of different sizes: "
158  << mRows_ << "x" << nCols_ << " != "
159  << Mb.m() << "x" << Mb.n()
160  << abort(FatalError);
161  }
162 
163  for (label i = 0; i < mRows_; ++i)
164  {
165  for (label j = 0; j < nCols_; ++j)
166  {
167  (*this)(i, j) = Mb(i, j);
168  }
169  }
170 }
171 
172 
173 template<class MatrixType>
175 (
177 )
178 {
179  if (reinterpret_cast<const ConstMatrixBlock<MatrixType>*>(this) != &Mb)
180  {
181  if (mRows_ != Mb.m() || nCols_ != Mb.n())
182  {
184  << "Attempt to assign blocks of different sizes: "
185  << mRows_ << "x" << nCols_ << " != "
186  << Mb.m() << "x" << Mb.n()
187  << abort(FatalError);
188  }
189 
190  for (label i = 0; i < mRows_; ++i)
191  {
192  for (label j = 0; j < nCols_; ++j)
193  {
194  (*this)(i, j) = Mb(i, j);
195  }
196  }
197  }
198 }
199 
200 
201 template<class MatrixType>
203 (
204  const MatrixBlock<MatrixType>& Mb
205 )
206 {
207  if (this != &Mb)
208  {
209  if (mRows_ != Mb.m() || nCols_ != Mb.n())
210  {
212  << "Attempt to assign blocks of different sizes: "
213  << mRows_ << "x" << nCols_ << " != "
214  << Mb.m() << "x" << Mb.n()
215  << abort(FatalError);
216  }
217 
218  for (label i = 0; i < mRows_; ++i)
219  {
220  for (label j = 0; j < nCols_; ++j)
221  {
222  (*this)(i, j) = Mb(i, j);
223  }
224  }
225  }
226 }
227 
228 
229 template<class MatrixType>
230 template<class MatrixType2>
232 (
234 )
235 {
236  if (reinterpret_cast<const ConstMatrixBlock<MatrixType2>*>(this) != &Mb)
237  {
238  if (mRows_ != Mb.m() || nCols_ != Mb.n())
239  {
241  << "Attempt to assign blocks of different sizes: "
242  << mRows_ << "x" << nCols_ << " != "
243  << Mb.m() << "x" << Mb.n()
244  << abort(FatalError);
245  }
246 
247  for (label i = 0; i < mRows_; ++i)
248  {
249  for (label j = 0; j < nCols_; ++j)
250  {
251  (*this)(i, j) = Mb(i, j);
252  }
253  }
254  }
255 }
256 
257 
258 template<class MatrixType>
259 template<class MatrixType2>
261 (
262  const MatrixBlock<MatrixType2>& Mb
263 )
264 {
265  if (this != &Mb)
266  {
267  if (mRows_ != Mb.m() || nCols_ != Mb.n())
268  {
270  << "Attempt to assign blocks of different sizes: "
271  << mRows_ << "x" << nCols_ << " != "
272  << Mb.m() << "x" << Mb.n()
273  << abort(FatalError);
274  }
275 
276  for (label i = 0; i < mRows_; ++i)
277  {
278  for (label j = 0; j < nCols_; ++j)
279  {
280  (*this)(i, j) = Mb(i, j);
281  }
282  }
283  }
284 }
285 
286 
287 template<class MatrixType>
288 template
289 <
290  template<class, Foam::direction, Foam::direction> class MSBlock,
291  class SubTensor,
292  Foam::direction BRowStart,
293  Foam::direction BColStart
294 >
296 (
297  const MSBlock<SubTensor, BRowStart, BColStart>& Mb
298 )
299 {
300  if (mRows_ != Mb.mRows || nCols_ != Mb.nCols)
301  {
303  << "Attempt to assign blocks of different sizes: "
304  << mRows_ << "x" << nCols_ << " != "
305  << Mb.mRows << "x" << Mb.nCols
306  << abort(FatalError);
307  }
308 
309  for (direction i = 0; i < mRows_; ++i)
310  {
311  for (direction j = 0; j < nCols_; ++j)
312  {
313  operator()(i, j) = Mb(i, j);
314  }
315  }
316 }
317 
318 
319 template<class MatrixType>
320 template
321 <
322  template<class, Foam::direction> class VSBlock,
323  class SubVector,
324  Foam::direction BStart
325 >
327 (
328  const VSBlock<SubVector, BStart>& Mb
329 )
330 {
331  if (mRows_ != Mb.nComponents || nCols_ != 1)
332  {
334  << "Attempt to assign blocks of different sizes: "
335  << mRows_ << "x" << nCols_ << " != "
336  << Mb.nComponents << "x" << 1
337  << abort(FatalError);
338  }
339 
340  for (direction i = 0; i < mRows_; ++i)
341  {
342  operator()(i, 0) = Mb[i];
343  }
344 }
345 
346 
347 template<class MatrixType>
348 template<class MSForm, Foam::direction Nrows, Foam::direction Ncols>
350 (
352 )
353 {
354  if (mRows_ != Nrows || nCols_ != Ncols)
355  {
357  << "Attempt to assign blocks of different sizes: "
358  << mRows_ << "x" << nCols_ << " != "
359  << Nrows << "x" << Ncols
360  << abort(FatalError);
361  }
362 
363  for (label i = 0; i < mRows_; ++i)
364  {
365  for (label j = 0; j < nCols_; ++j)
366  {
367  (*this)(i, j) = ms(i, j);
368  }
369  }
370 }
371 
372 
373 template<class MatrixType>
374 template<class VSForm, Foam::direction Ncmpts>
376 (
378 )
379 {
380  if (mRows_ != Ncmpts || nCols_ != 1)
381  {
383  << "Attempt to assign blocks of different sizes: "
384  << mRows_ << "x" << nCols_ << " != "
385  << Ncmpts << "x" << 1
386  << abort(FatalError);
387  }
388 
389  for (direction i = 0; i < Ncmpts; ++i)
390  {
391  operator()(i, 0) = ms[i];
392  }
393 }
394 
395 
396 template<class MatrixType>
398 {
399  if (mRows_ != f.size() || nCols_ != 1)
400  {
402  << "Error: cannot assign blocks of different size (left is "
403  << mRows_ << "x" << nCols_ << " != "
404  << f.size() << "x" << 1
405  << abort(FatalError);
406  }
407 
408  forAll(f, i)
409  {
410  operator()(i, 0) = f[i];
411  }
412 }
413 
414 
415 // ************************************************************************* //
Foam::Matrix
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: DiagonalMatrix.H:53
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:56
Foam::MatrixSpace
Templated matrix space.
Definition: MatrixSpace.H:58
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::MatrixBlock
A templated block of an (m x n) matrix of type <MatrixType>.
Definition: Matrix.H:66
Foam::FatalError
error FatalError
Foam::MatrixBlock::checkIndex
void checkIndex(const label i, const label j) const
Check if (i, j) is within range of row-column limits.
Definition: MatrixBlock.C:125
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
MatrixBlock.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
f
labelList f(nPoints)
Foam::MatrixBlock::operator=
void operator=(const Matrix< Form, cmptType > &)
Assignment to a compatible matrix.
Definition: MatrixBlock.C:150
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::ConstMatrixBlock
Definition: Matrix.H:65
Foam::ConstMatrixBlock::checkIndex
void checkIndex(const label i, const label j) const
Check if (i, j) is within range of row-column limits.
Definition: MatrixBlock.C:104