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 -------------------------------------------------------------------------------
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, 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)
78  {
79  val = Zero;
80  }
81  else
82  {
83  val = Type(1)/val;
84  }
85  }
86 }
87 
88 
89 template<class Type>
90 template<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;
106 }
107 
108 
109 template<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 
147 namespace Foam
148 {
149 
150 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
151 
152 //- Return the matrix inverse as a DiagonalMatrix if no elem is equal to zero
153 template<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
180 template<class Type>
181 DiagonalMatrix<Type> applyPermutation
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 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::applyPermutation
DiagonalMatrix< Type > applyPermutation(const DiagonalMatrix< Type > &mat, const List< label > &p)
Definition: DiagonalMatrix.C:182
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
DiagonalMatrix.H
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::Matrix
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: DiagonalMatrix.H:53
Foam::DiagonalMatrix
A templated (N x N) diagonal matrix of objects of <Type>, effectively containing N elements,...
Definition: DiagonalMatrix.H:60
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::inv
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:73
Foam::DiagonalMatrix::sortPermutation
List< label > sortPermutation(CompOp &compare) const
Foam::DiagonalMatrix::invert
void invert()
Return the matrix inverse into itself if no elem is equal to zero.
Definition: DiagonalMatrix.C:73
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::DiagonalMatrix::applyPermutation
void applyPermutation(const List< label > &p)
Definition: DiagonalMatrix.C:110
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix()=default
Default construct.
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62