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