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-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
33 template<class Type>
35 :
36  List<Type>(n)
37 {}
38 
39 
40 template<class Type>
42 :
43  List<Type>(n, Foam::zero{})
44 {}
45 
46 
47 template<class Type>
48 Foam::DiagonalMatrix<Type>::DiagonalMatrix(const label n, const Type& val)
49 :
50  List<Type>(n, val)
51 {}
52 
53 
54 template<class Type>
55 template<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 
72 template<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 
87 template<class Type>
88 template<class CompOp>
90 (
91  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 
107 template<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 
145 namespace Foam
146 {
148 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
149 
150 //- Return the matrix inverse as a DiagonalMatrix
151 template<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(), 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
176 template<class Type>
178 (
179  const DiagonalMatrix<Type>& mat,
180  const List<label>& 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 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
DiagonalMatrix()=default
Default construct.
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
void applyPermutation(const List< label > &p)
Column-reorder this Matrix according to a given permutation labelList.
A templated (N x N) diagonal matrix of objects of <Type>, effectively containing N elements...
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
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
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order: ...
void invert()
Return the matrix inverse into itself.
DiagonalMatrix< Type > applyPermutation(const DiagonalMatrix< Type > &mat, const List< label > &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().
Definition: DynamicList.H:692
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
List< label > sortPermutation(CompOp &compare) const
Return a sort permutation labelList according to a given comparison on the diagonal entries...
volScalarField & p
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127