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-------------------------------------------------------------------------------
10License
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
32template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
34(
35 const Foam::zero
36)
37:
38 MatrixSpace::vsType(Zero)
39{}
40
41
42template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
43template<class Form2, class Cmpt2>
45(
47)
48:
50{}
51
52
53template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
54template
55<
56 template<class, Foam::direction, Foam::direction> class Block2,
57 Foam::direction BRowStart,
58 Foam::direction BColStart
59>
61(
62 const Block2<Form, BRowStart, BColStart>& block
63)
64{
65 for (direction i=0; i<Mrows; ++i)
66 {
67 for (direction j=0; j<Ncols; ++j)
68 {
69 operator()(i, j) = block(i, j);
70 }
71 }
72}
73
74
75template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
77:
79{}
80
81
82template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
83template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
86ConstBlock(const msType& matrix)
87:
88 matrix_(matrix)
89{
90 static_assert
91 (
92 msType::mRows >= BRowStart + mRows,
93 "Rows in block > rows in matrix"
94 );
95 static_assert
96 (
97 msType::nCols >= BColStart + nCols,
98 "Columns in block > columns in matrix"
99 );
100}
101
102
103template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
104template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
107Block(msType& matrix)
108:
109 matrix_(matrix)
110{
111 static_assert
112 (
113 msType::mRows >= BRowStart + mRows,
114 "Rows in block > rows in matrix"
115 );
116 static_assert
117 (
118 msType::nCols >= BColStart + nCols,
119 "Columns in block > columns in matrix"
120 );
122
123
124// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125
126template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
127template<Foam::direction Row, Foam::direction Col>
129{
130 static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
131 return this->v_[Row*Ncols + Col];
132}
133
134
135template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
136template<Foam::direction Row, Foam::direction Col>
138{
139 static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
140 return this->v_[Row*Ncols + Col];
141}
142
143
144template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
146{
147 return elmt<0, 0>();
148}
149
150
151template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
153{
154 return elmt<0, 0>();
155}
156
157
158template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
160{
161 return elmt<0,1>();
162}
163
164
165template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
167{
168 return elmt<0,1>();
170
171
172template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
174{
175 return elmt<0,2>();
176}
177
178
179template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
181{
182 return elmt<0,2>();
183}
184
185
186template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
189 return elmt<1,0>();
190}
192
193template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
195{
196 return elmt<1,0>();
197}
199
200template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
202{
203 return elmt<1,1>();
204}
205
206
207template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
209{
210 return elmt<1,1>();
212
213
214template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
216{
217 return elmt<1,2>();
218}
219
220
221template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
223{
224 return elmt<1,2>();
225}
226
228template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
230{
231 return elmt<2,0>();
232}
234
235template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
237{
238 return elmt<2,0>();
239}
241
242template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
245 return elmt<2,1>();
246}
247
248
249template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
252 return elmt<2,1>();
256template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
258{
259 return elmt<2,2>();
260}
261
262template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
265 return elmt<2,2>();
268template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
271{
272 static_assert(Mrows == Ncols, "Matrix is not square");
273 msType result(Zero);
274
275 for (direction i=0; i<Ncols; ++i)
276 {
277 result(i, i) = 1;
278 }
279
280 return result;
281}
282
283
284template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
287{
288 typename typeOfTranspose<Cmpt, Form>::type result;
289
290 for (direction i=0; i<Mrows; ++i)
291 {
292 for (direction j=0; j<Ncols; ++j)
294 result(j,i) = operator()(i, j);
295 }
296 }
297
298 return result;
300
301
302template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
303template
304<
305 class SubTensor,
306 Foam::direction BRowStart,
307 Foam::direction BColStart
312{
313 return *this;
314}
316
317template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
318template
319<
320 class SubTensor,
321 Foam::direction BRowStart,
322 Foam::direction BColStart
323>
324inline
326 Block<SubTensor, BRowStart, BColStart>
328{
329 return *this;
330}
331
332
333// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
334
335template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
337(
338 const direction& i,
339 const direction& j
340) const
341{
342 #ifdef FULLDEBUG
343 if (i >= Mrows || j >= Ncols)
344 {
346 << "indices out of range"
347 << abort(FatalError);
348 }
349 #endif
350
351 return this->v_[i*Ncols + j];
352}
353
354
355template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
357(
358 const direction& i,
359 const direction& j
360)
361{
362 #ifdef FULLDEBUG
363 if (i >= Mrows || j >= Ncols)
364 {
366 << "indices out of range"
367 << abort(FatalError);
368 }
369 #endif
370
371 return this->v_[i*Ncols + j];
372}
373
374
375template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
376template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
377inline SubTensor
380operator()() const
381{
382 return *this;
383}
384
385
386template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
387template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
388inline const Cmpt&
391operator()(const direction i, const direction j) const
392{
393 return matrix_(BRowStart + i, BColStart + j);
394}
395
396
397template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
398template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
399inline SubTensor
402operator()() const
403{
404 SubTensor st;
405
406 for (direction i=0; i<SubTensor::mRows; ++i)
407 {
408 for (direction j=0; j<SubTensor::nCols; ++j)
409 {
410 st[i*SubTensor::nCols + j] = operator()(i, j);
411 }
412 }
413
414 return st;
415}
416
417
418template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
419template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
420inline const Cmpt&
423operator()(const direction i, const direction j) const
424{
425 return matrix_(BRowStart + i, BColStart + j);
426}
427
428
429template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
430template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
431inline Cmpt&
434operator()(const direction i, const direction j)
435{
436 return matrix_(BRowStart + i, BColStart + j);
437}
438
439
440template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
442(
443 const Foam::zero
444)
445{
447}
448
449
450template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
451template<class Form2>
453(
455)
456{
457 *this = *this & matrix;
458}
459
460
461template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
462template
463<
464 template<class, Foam::direction, Foam::direction> class Block2,
465 Foam::direction BRowStart,
466 Foam::direction BColStart
467>
469(
470 const Block2<Form, BRowStart, BColStart>& block
471)
472{
473 for (direction i = 0; i < Mrows; ++i)
474 {
475 for (direction j = 0; j < Ncols; ++j)
476 {
477 operator()(i, j) = block(i, j);
478 }
479 }
480}
481
482
483template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
484template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
485template<class Form2>
486inline void
488Block<SubTensor, BRowStart, BColStart>::
490(
492)
493{
494 for (direction i=0; i<mRows; ++i)
495 {
496 for (direction j=0; j<nCols; ++j)
497 {
498 operator()(i,j) = matrix(i,j);
499 }
500 }
501}
502
503
504template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
505template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
506template<class VSForm>
507inline void
509Block<SubTensor, BRowStart, BColStart>::
511(
513)
514{
515 static_assert(nCols == 1, "Matrix must have a single column");
516
517 for (direction i=0; i<SubTensor::mRows; ++i)
518 {
519 operator()(i,0) = v[i];
520 }
521}
522
523
524// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
525
526namespace Foam
527{
528
529// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
530
531template<class Form, class Cmpt, direction Mrows, direction Ncols>
533(
535)
536{
537 return matrix.T();
538}
539
540
541template<class Form, class Cmpt, direction Ncmpts>
543(
545)
546{
547 typename typeOfTranspose<Cmpt, Form>::type result;
548
549 for (direction i=0; i<Ncmpts; ++i)
550 {
551 result[i] = v[i];
552 }
553
554 return result;
555}
556
557
558// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
559
560template
561<
562 class Form1,
563 class Form2,
564 class Cmpt,
565 direction Mrows1,
566 direction Ncols1,
567 direction Mrows2,
568 direction Ncols2
569>
571(
574)
575{
576 static_assert
577 (
578 Ncols1 == Mrows2,
579 "Number of columns in matrix 1 != number of rows in matrix 2"
580 );
581
583
584 for (direction i=0; i<Mrows1; ++i)
585 {
586 for (direction j=0; j<Ncols2; ++j)
587 {
588 for (direction k=0; k<Mrows2; k++)
589 {
590 result(i, j) += matrix1(i, k)*matrix2(k, j);
591 }
592 }
593 }
594
595 return result;
596}
597
598
599template<class Form, class VSForm, class Cmpt, direction Mrows, direction Ncols>
601(
604)
605{
607
608 for (direction i=0; i<Mrows; ++i)
609 {
610 for (direction j=0; j<Ncols; ++j)
611 {
612 result[i] += matrix(i, j)*v[j];
613 }
614 }
615
616 return result;
617}
618
619
620template
621<
622 class Form1,
623 class Form2,
624 class Cmpt,
625 direction Ncmpts1,
626 direction Ncmpts2
627>
629(
632)
633{
635
636 for (direction i=0; i<Ncmpts1; ++i)
637 {
638 for (direction j=0; j<Ncmpts2; ++j)
639 {
640 result(i, j) = v1[i]*v2[j];
641 }
642 }
643
644 return result;
645}
646
647
648// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
649
650} // End namespace Foam
651
652// ************************************************************************* //
label k
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
static const direction nCols
Definition: MatrixSpace.H:150
static const direction mRows
Definition: MatrixSpace.H:149
static const direction nCols
Definition: MatrixSpace.H:106
static const direction mRows
Definition: MatrixSpace.H:105
Templated matrix space.
Definition: MatrixSpace.H:61
typeOfTranspose< Cmpt, Form >::type T() const
Return the transpose of the matrix.
Definition: MatrixSpaceI.H:286
static constexpr direction nCols
Definition: MatrixSpace.H:73
const Cmpt & xx() const
Definition: MatrixSpaceI.H:145
const Cmpt & yx() const
Definition: MatrixSpaceI.H:187
const Cmpt & yz() const
Definition: MatrixSpaceI.H:215
static msType identity()
An identity matrix for square matrix-spaces.
Definition: MatrixSpaceI.H:270
const Cmpt & xz() const
Definition: MatrixSpaceI.H:173
const Cmpt & zz() const
Definition: MatrixSpaceI.H:257
const Cmpt & elmt() const
Fast const element access using compile-time addressing.
Definition: MatrixSpaceI.H:128
static constexpr direction mRows
Definition: MatrixSpace.H:72
ConstBlock< SubTensor, BRowStart, BColStart > block() const
Return a const sub-block corresponding to the specified type.
const Cmpt & xy() const
Definition: MatrixSpaceI.H:159
MatrixSpace()=default
Default construct.
const Cmpt & zx() const
Definition: MatrixSpaceI.H:229
const Cmpt & zy() const
Definition: MatrixSpaceI.H:243
const Cmpt & operator()(const direction &i, const direction &j) const
(i, j) const element access operator
Definition: MatrixSpaceI.H:337
const Cmpt & yy() const
Definition: MatrixSpaceI.H:201
Const sub-block type.
Definition: VectorSpace.H:128
Templated vector space.
Definition: VectorSpace.H:79
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:338
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:83
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:61
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
type
Volume classification types.
Definition: volumeType.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
errorManip< error > abort(error &err)
Definition: errorManip.H:144
uint8_t direction
Definition: direction.H:56
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError