MatrixSpaceI.H
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 -------------------------------------------------------------------------------
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 <type_traits>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
34 {}
35 
36 
37 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
39 (
40  const Foam::zero
41 )
42 :
44 {}
45 
46 
47 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
48 template<class Form2, class Cmpt2>
50 (
52 )
53 :
55 {}
56 
57 
58 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
59 template
60 <
61  template<class, Foam::direction, Foam::direction> class Block2,
62  Foam::direction BRowStart,
63  Foam::direction BColStart
64 >
66 (
67  const Block2<Form, BRowStart, BColStart>& block
68 )
69 {
70  for (direction i=0; i<Mrows; ++i)
71  {
72  for (direction j=0; j<Ncols; ++j)
73  {
74  operator()(i, j) = block(i, j);
75  }
76  }
77 }
78 
79 
80 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
82 :
83  MatrixSpace::vsType(is)
84 {}
85 
86 
87 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
88 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
91 ConstBlock(const msType& matrix)
92 :
93  matrix_(matrix)
94 {
95  static_assert
96  (
97  msType::mRows >= BRowStart + mRows,
98  "Rows in block > rows in matrix"
99  );
100  static_assert
101  (
102  msType::nCols >= BColStart + nCols,
103  "Columns in block > columns in matrix"
104  );
105 }
106 
107 
108 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
109 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
112 Block(msType& matrix)
113 :
114  matrix_(matrix)
115 {
116  static_assert
117  (
118  msType::mRows >= BRowStart + mRows,
119  "Rows in block > rows in matrix"
120  );
121  static_assert
122  (
123  msType::nCols >= BColStart + nCols,
124  "Columns in block > columns in matrix"
125  );
126 }
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
131 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
132 template<Foam::direction Row, Foam::direction Col>
134 {
135  static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
136  return this->v_[Row*Ncols + Col];
137 }
138 
139 
140 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
141 template<Foam::direction Row, Foam::direction Col>
143 {
144  static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
145  return this->v_[Row*Ncols + Col];
146 }
147 
148 
149 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
151 {
152  return elmt<0, 0>();
153 }
154 
155 
156 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
158 {
159  return elmt<0, 0>();
160 }
161 
162 
163 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
165 {
166  return elmt<0,1>();
167 }
168 
169 
170 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
172 {
173  return elmt<0,1>();
174 }
175 
176 
177 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
179 {
180  return elmt<0,2>();
181 }
182 
183 
184 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
186 {
187  return elmt<0,2>();
188 }
189 
190 
191 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
193 {
194  return elmt<1,0>();
195 }
196 
197 
198 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
200 {
201  return elmt<1,0>();
202 }
203 
204 
205 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
207 {
208  return elmt<1,1>();
209 }
210 
211 
212 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
214 {
215  return elmt<1,1>();
216 }
217 
218 
219 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
221 {
222  return elmt<1,2>();
223 }
224 
225 
226 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
228 {
229  return elmt<1,2>();
230 }
231 
232 
233 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
235 {
236  return elmt<2,0>();
237 }
238 
239 
240 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
242 {
243  return elmt<2,0>();
244 }
245 
246 
247 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
249 {
250  return elmt<2,1>();
251 }
252 
253 
254 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
256 {
257  return elmt<2,1>();
258 }
259 
260 
261 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
263 {
264  return elmt<2,2>();
265 }
266 
267 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
269 {
270  return elmt<2,2>();
271 }
272 
273 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
276 {
277  static_assert(Mrows == Ncols, "Matrix is not square");
278  msType result(Zero);
279 
280  for (direction i=0; i<Ncols; ++i)
281  {
282  result(i, i) = 1;
283  }
284 
285  return result;
286 }
287 
288 
289 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
292 {
293  typename typeOfTranspose<Cmpt, Form>::type result;
294 
295  for (direction i=0; i<Mrows; ++i)
296  {
297  for (direction j=0; j<Ncols; ++j)
298  {
299  result(j,i) = operator()(i, j);
300  }
301  }
302 
303  return result;
304 }
305 
306 
307 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
308 template
309 <
310  class SubTensor,
311  Foam::direction BRowStart,
312  Foam::direction BColStart
313 >
315  ConstBlock<SubTensor, BRowStart, BColStart>
317 {
318  return *this;
319 }
320 
321 
322 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
323 template
324 <
325  class SubTensor,
326  Foam::direction BRowStart,
327  Foam::direction BColStart
328 >
329 inline
331  Block<SubTensor, BRowStart, BColStart>
333 {
334  return *this;
335 }
336 
337 
338 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
339 
340 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
342 (
343  const direction& i,
344  const direction& j
345 ) const
346 {
347  #ifdef FULLDEBUG
348  if (i >= Mrows || j >= Ncols)
349  {
351  << "indices out of range"
352  << abort(FatalError);
353  }
354  #endif
355 
356  return this->v_[i*Ncols + j];
357 }
358 
359 
360 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
362 (
363  const direction& i,
364  const direction& j
365 )
366 {
367  #ifdef FULLDEBUG
368  if (i >= Mrows || j >= Ncols)
369  {
371  << "indices out of range"
372  << abort(FatalError);
373  }
374  #endif
375 
376  return this->v_[i*Ncols + j];
377 }
378 
379 
380 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
381 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
382 inline SubTensor
385 operator()() const
386 {
387  return *this;
388 }
389 
390 
391 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
392 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
393 inline const Cmpt&
396 operator()(const direction i, const direction j) const
397 {
398  return matrix_(BRowStart + i, BColStart + j);
399 }
400 
401 
402 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
403 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
404 inline SubTensor
407 operator()() const
408 {
409  SubTensor st;
410 
411  for (direction i=0; i<SubTensor::mRows; ++i)
412  {
413  for (direction j=0; j<SubTensor::nCols; ++j)
414  {
415  st[i*SubTensor::nCols + j] = operator()(i, j);
416  }
417  }
418 
419  return st;
420 }
421 
422 
423 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
424 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
425 inline const Cmpt&
428 operator()(const direction i, const direction j) const
429 {
430  return matrix_(BRowStart + i, BColStart + j);
431 }
432 
433 
434 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
435 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
436 inline Cmpt&
439 operator()(const direction i, const direction j)
440 {
441  return matrix_(BRowStart + i, BColStart + j);
442 }
443 
444 
445 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
447 (
448  const Foam::zero
449 )
450 {
452 }
453 
454 
455 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
456 template<class Form2>
458 (
460 )
461 {
462  *this = *this & matrix;
463 }
464 
465 
466 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
467 template
468 <
469  template<class, Foam::direction, Foam::direction> class Block2,
470  Foam::direction BRowStart,
471  Foam::direction BColStart
472 >
474 (
475  const Block2<Form, BRowStart, BColStart>& block
476 )
477 {
478  for (direction i = 0; i < Mrows; ++i)
479  {
480  for (direction j = 0; j < Ncols; ++j)
481  {
482  operator()(i, j) = block(i, j);
483  }
484  }
485 }
486 
487 
488 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
489 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
490 template<class Form2>
491 inline void
493 Block<SubTensor, BRowStart, BColStart>::
494 operator=
495 (
497 )
498 {
499  for (direction i=0; i<mRows; ++i)
500  {
501  for (direction j=0; j<nCols; ++j)
502  {
503  operator()(i,j) = matrix(i,j);
504  }
505  }
506 }
507 
508 
509 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
510 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
511 template<class VSForm>
512 inline void
515 operator=
516 (
518 )
519 {
520  static_assert(nCols == 1, "Matrix must have a single column");
521 
522  for (direction i=0; i<SubTensor::mRows; ++i)
523  {
524  operator()(i,0) = v[i];
525  }
526 }
527 
528 
529 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
530 
531 namespace Foam
532 {
533 
534 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
535 
536 template<class Form, class Cmpt, direction Mrows, direction Ncols>
537 inline typename typeOfTranspose<Cmpt, Form>::type T
538 (
540 )
541 {
542  return matrix.T();
543 }
544 
545 
546 template<class Form, class Cmpt, direction Ncmpts>
547 inline typename typeOfTranspose<Cmpt, Form>::type T
548 (
550 )
551 {
552  typename typeOfTranspose<Cmpt, Form>::type result;
553 
554  for (direction i=0; i<Ncmpts; ++i)
555  {
556  result[i] = v[i];
557  }
558 
559  return result;
560 }
561 
562 
563 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
564 
565 template
566 <
567  class Form1,
568  class Form2,
569  class Cmpt,
570  direction Mrows1,
571  direction Ncols1,
572  direction Mrows2,
573  direction Ncols2
574 >
575 inline typename typeOfInnerProduct<Cmpt, Form1, Form2>::type operator&
576 (
579 )
580 {
581  static_assert
582  (
583  Ncols1 == Mrows2,
584  "Number of columns in matrix 1 != number of rows in matrix 2"
585  );
586 
588 
589  for (direction i=0; i<Mrows1; ++i)
590  {
591  for (direction j=0; j<Ncols2; ++j)
592  {
593  for (direction k=0; k<Mrows2; k++)
594  {
595  result(i, j) += matrix1(i, k)*matrix2(k, j);
596  }
597  }
598  }
599 
600  return result;
601 }
602 
603 
604 template<class Form, class VSForm, class Cmpt, direction Mrows, direction Ncols>
605 inline typename typeOfInnerProduct<Cmpt, Form, VSForm>::type operator&
606 (
609 )
610 {
612 
613  for (direction i=0; i<Mrows; ++i)
614  {
615  for (direction j=0; j<Ncols; ++j)
616  {
617  result[i] += matrix(i, j)*v[j];
618  }
619  }
620 
621  return result;
622 }
623 
624 
625 template
626 <
627  class Form1,
628  class Form2,
629  class Cmpt,
630  direction Ncmpts1,
631  direction Ncmpts2
632 >
633 inline typename typeOfOuterProduct<Cmpt, Form1, Form2>::type operator*
634 (
637 )
638 {
640 
641  for (direction i=0; i<Ncmpts1; ++i)
642  {
643  for (direction j=0; j<Ncmpts2; ++j)
644  {
645  result(i, j) = v1[i]*v2[j];
646  }
647  }
648 
649  return result;
650 }
651 
652 
653 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
654 
655 } // End namespace Foam
656 
657 // ************************************************************************* //
Foam::MatrixSpace::zz
const Cmpt & zz() const
Definition: MatrixSpaceI.H:262
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:58
Foam::MatrixSpace::xx
const Cmpt & xx() const
Definition: MatrixSpaceI.H:150
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::MatrixSpace::elmt
const Cmpt & elmt() const
Fast const element access using compile-time addressing.
Definition: MatrixSpaceI.H:133
Foam::typeOfTranspose
Abstract template class to provide the transpose form of a form.
Definition: products.H:62
Foam::MatrixSpace::T
typeOfTranspose< Cmpt, Form >::type T() const
Return the transpose of the matrix.
Definition: MatrixSpaceI.H:291
Foam::MatrixSpace::Block::nCols
static const direction nCols
Definition: MatrixSpace.H:149
Foam::MatrixSpace::ConstBlock::mRows
static const direction mRows
Definition: MatrixSpace.H:104
Foam::MatrixSpace::zy
const Cmpt & zy() const
Definition: MatrixSpaceI.H:248
Foam::MatrixSpace::mRows
static constexpr direction mRows
Definition: MatrixSpace.H:71
Foam::MatrixSpace::ConstBlock::nCols
static const direction nCols
Definition: MatrixSpace.H:105
Foam::MatrixSpace::yy
const Cmpt & yy() const
Definition: MatrixSpaceI.H:206
Foam::VectorSpace< Form, Cmpt, Mrows *Ncols >::v_
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:83
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:56
Foam::MatrixSpace::nCols
static constexpr direction nCols
Definition: MatrixSpace.H:72
Foam::MatrixSpace
Templated matrix space.
Definition: MatrixSpace.H:58
Foam::MatrixSpace::yz
const Cmpt & yz() const
Definition: MatrixSpaceI.H:220
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::MatrixSpace::Block
Sub-block type.
Definition: MatrixSpace.H:141
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::MatrixSpace::block
ConstBlock< SubTensor, BRowStart, BColStart > block() const
Return a const sub-block corresponding to the specified type.
Foam::typeOfOuterProduct
Definition: products.H:57
Foam::typeOfInnerProduct
Definition: products.H:51
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::MatrixSpace::operator()
const Cmpt & operator()(const direction &i, const direction &j) const
(i, j) const element access operator
Definition: MatrixSpaceI.H:342
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::MatrixSpace::zx
const Cmpt & zx() const
Definition: MatrixSpaceI.H:234
Foam::MatrixSpace::identity
static msType identity()
Return the identity matrix for square matrix spaces.
Definition: MatrixSpaceI.H:275
Foam::MatrixSpace::MatrixSpace
MatrixSpace()
Construct null.
Definition: MatrixSpaceI.H:33
Foam::MatrixSpace::yx
const Cmpt & yx() const
Definition: MatrixSpaceI.H:192
Foam::MatrixSpace::xy
const Cmpt & xy() const
Definition: MatrixSpaceI.H:164
Foam::VectorSpace::operator=
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:347
Foam::MatrixSpace::Block::mRows
static const direction mRows
Definition: MatrixSpace.H:148
Foam::VectorSpace< Form, Cmpt, Mrows *Ncols >::operator
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
Foam::MatrixSpace::xz
const Cmpt & xz() const
Definition: MatrixSpaceI.H:178
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61