Loading...
Searching...
No Matches
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-2025 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, Foam::zero{})
44{}
45
46
47template<class Type>
48Foam::DiagonalMatrix<Type>::DiagonalMatrix(const label n, const Type& val)
50 List<Type>(n, val)
51{}
52
53
54template<class Type>
55template<class Form>
57:
58 List<Type>(Foam::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, leave untouched
78 // forcing of val=Zero sometimes confuses compiler
79 if (mag(val) > VSMALL)
80 {
81 val = Type(1)/val;
82 }
83 }
84}
85
86
87template<class Type>
88template<class CompOp>
90(
91 const CompOp& compare
92) const
93{
94 List<label> p(this->size());
95 std::iota(p.begin(), p.end(), 0);
96 std::sort
97 (
98 p.begin(),
99 p.end(),
100 [&](label i, label j){ return compare((*this)[i], (*this)[j]); }
101 );
102
103 return p;
104}
105
106
107template<class Type>
109{
110 #ifdef FULLDEBUG
111 if (this->size() != p.size())
112 {
114 << "Attempt to column-reorder according to an uneven list: " << nl
115 << "DiagonalMatrix diagonal size = " << this->size() << nl
116 << "Permutation list size = " << p.size() << nl
117 << abort(FatalError);
118 }
119 #endif
120
121 List<bool> pass(p.size(), false);
122
123 for (label i = 0; i < p.size(); ++i)
124 {
125 if (pass[i])
126 {
127 continue;
128 }
129 pass[i] = true;
130 label prev = i;
131 label j = p[i];
132 while (i != j)
133 {
134 Foam::Swap((*this)[prev], (*this)[j]);
135 pass[j] = true;
136 prev = j;
137 j = p[j];
138 }
139 }
140}
141
142
143// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
144
145namespace Foam
146{
148// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
149
150//- Return the matrix inverse as a DiagonalMatrix
151template<class Type>
153{
154 // Construct with fall-back value conditional calculation
155 // of invert to avoid over-eager compiler optimisation
156 DiagonalMatrix<Type> Ainv(mat.size(), Foam::zero{});
157
158 Type* iter = Ainv.begin();
159
160 for (const Type& val : mat)
161 {
162 if (mag(val) > VSMALL)
163 {
164 *iter = Type(1)/val;
165 }
166
167 ++iter;
168 }
169
170 return Ainv;
171}
172
173
174//- Return Matrix column-reordered according to
175//- a given permutation labelList
176template<class Type>
178(
179 const DiagonalMatrix<Type>& mat,
180 const labelUList& p
181)
182{
183 #ifdef FULLDEBUG
184 if (mat.size() != p.size())
185 {
187 << "Attempt to column-reorder according to an uneven list: " << nl
188 << "DiagonalMatrix diagonal size = " << mat.size() << nl
189 << "Permutation list size = " << p.size() << nl
190 << abort(FatalError);
191 }
192 #endif
193
194 DiagonalMatrix<Type> reordered(mat.size());
195
196 label j = 0;
197 for (const label i : p)
198 {
199 reordered[j] = mat[i];
200 ++j;
201 }
202
203 return reordered;
204}
205
206
207// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208
209} // End namespace Foam
210
211// ************************************************************************* //
label n
A templated (N x N) diagonal matrix of objects of <Type>, effectively containing N elements,...
labelList sortPermutation(const CompOp &compare) const
Return a sort permutation labelList according to a given comparison on the diagonal entries.
DiagonalMatrix()=default
Default construct.
void invert()
Return the matrix inverse into itself.
void applyPermutation(const labelUList &p)
Column-reorder this Matrix according to a given permutation labelList.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition Matrix.H:77
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
void size(const label n)
Definition UList.H:118
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
DiagonalMatrix< Type > applyPermutation(const DiagonalMatrix< Type > &mat, const labelUList &p)
Return Matrix column-reordered according to a given permutation labelList.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
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:26
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
UList< label > labelUList
A UList of labels.
Definition UList.H:75
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50