DiagonalMatrix.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2019-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "DiagonalMatrix.H"
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<class Type>
35:
36 List<Type>(n)
37{}
38
39
40template<class Type>
42:
43 List<Type>(n, Zero)
44{}
45
46
47template<class Type>
48Foam::DiagonalMatrix<Type>::DiagonalMatrix(const label n, const Type& val)
49:
50 List<Type>(n, val)
51{}
52
53
54template<class Type>
55template<class Form>
57:
58 List<Type>(min(mat.m(), mat.n()))
59{
60 label i = 0;
61
62 for (Type& val : *this)
63 {
64 val = mat(i, i);
65 ++i;
66 }
67}
68
69
70// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
71
72template<class Type>
74{
75 for (Type& val : *this)
76 {
77 if (mag(val) < VSMALL)
78 {
79 val = Zero;
80 }
81 else
82 {
83 val = Type(1)/val;
84 }
85 }
86}
87
88
89template<class Type>
90template<class CompOp>
92(
93 CompOp& compare
94) const
95{
96 List<label> p(this->size());
97 std::iota(p.begin(), p.end(), 0);
98 std::sort
99 (
100 p.begin(),
101 p.end(),
102 [&](label i, label j){ return compare((*this)[i], (*this)[j]); }
103 );
104
105 return p;
107
108
109template<class Type>
111{
112 #ifdef FULLDEBUG
113 if (this->size() != p.size())
114 {
116 << "Attempt to column-reorder according to an uneven list: " << nl
117 << "DiagonalMatrix diagonal size = " << this->size() << nl
118 << "Permutation list size = " << p.size() << nl
119 << abort(FatalError);
120 }
121 #endif
122
123 List<bool> pass(p.size(), false);
124
125 for (label i = 0; i < p.size(); ++i)
126 {
127 if (pass[i])
128 {
129 continue;
130 }
131 pass[i] = true;
132 label prev = i;
133 label j = p[i];
134 while (i != j)
135 {
136 Swap((*this)[prev], (*this)[j]);
137 pass[j] = true;
138 prev = j;
139 j = p[j];
140 }
141 }
142}
143
144
145// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146
147namespace Foam
148{
149
150// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
151
152//- Return the matrix inverse as a DiagonalMatrix if no elem is equal to zero
153template<class Type>
155{
156 DiagonalMatrix<Type> Ainv(mat.size());
157
158 Type* iter = Ainv.begin();
159
160 for (const Type& val : mat)
161 {
162 if (mag(val) < VSMALL)
163 {
164 *iter = Zero;
165 }
166 else
167 {
168 *iter = Type(1)/val;
169 }
170
171 ++iter;
172 }
173
174 return Ainv;
175}
176
177
178//- Return Matrix column-reordered according to
179//- a given permutation labelList
180template<class Type>
182(
183 const DiagonalMatrix<Type>& mat,
184 const List<label>& p
185)
186{
187 #ifdef FULLDEBUG
188 if (mat.size() != p.size())
189 {
191 << "Attempt to column-reorder according to an uneven list: " << nl
192 << "DiagonalMatrix diagonal size = " << mat.size() << nl
193 << "Permutation list size = " << p.size() << nl
194 << abort(FatalError);
195 }
196 #endif
197
198 DiagonalMatrix<Type> reordered(mat.size());
199
200 label j = 0;
201 for (const label i : p)
202 {
203 reordered[j] = mat[i];
204 ++j;
205 }
206
207 return reordered;
208}
209
210
211// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212
213} // End namespace Foam
214
215// ************************************************************************* //
label n
A templated (N x N) diagonal matrix of objects of <Type>, effectively containing N elements,...
List< label > sortPermutation(CompOp &compare) const
DiagonalMatrix()=default
Default construct.
void applyPermutation(const List< label > &p)
void invert()
Return the matrix inverse into itself if no elem is equal to zero.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: Matrix.H:81
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
errorManip< error > abort(error &err)
Definition: errorManip.H:144
DiagonalMatrix< Type > applyPermutation(const DiagonalMatrix< Type > &mat, const List< label > &p)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53