OGS
LinAlg.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <cassert>
14
15#include "BaseLib/Error.h"
16#include "LinAlgEnums.h"
17
18#ifdef USE_PETSC
19#include <petscsystypes.h>
20#endif
21
22namespace MathLib
23{
24
35namespace LinAlg
36{
37
38// Matrix or Vector
39
41template<typename MatrixOrVector>
43{
44 y = x;
45}
46
48template <typename MatrixOrVector>
49void scale(MatrixOrVector& x, double const a)
50{
51 x *= a;
52}
53
55template <typename MatrixOrVector>
56void aypx(MatrixOrVector& y, double const a, MatrixOrVector const& x)
57{
58 y = a*y + x;
59}
60
62template <typename MatrixOrVector>
63void axpy(MatrixOrVector& y, double const a, MatrixOrVector const& x)
64{
65 y += a*x;
66}
67
69template <typename MatrixOrVector>
70void axpby(MatrixOrVector& y, double const a, double const b,
71 MatrixOrVector const& x)
72{
73 y = a*x + b*y;
74}
75
77template<typename MatrixOrVector>
79 MatrixOrVector const& x, MatrixOrVector const& y);
80
82template<typename MatrixOrVector>
83double norm1(MatrixOrVector const& x);
84
86template<typename MatrixOrVector>
87double norm2(MatrixOrVector const& x);
88
90template<typename MatrixOrVector>
91double normMax(MatrixOrVector const& x);
92
93template<typename MatrixOrVector>
95{
96 switch (type) {
98 return norm1(x);
100 return norm2(x);
102 return normMax(x);
103 default:
104 OGS_FATAL("Invalid norm type given.");
105 }
106}
107
108template <typename Matrix>
109void finalizeAssembly(Matrix& /*A*/);
110
111// Matrix and Vector
112
119template<typename Matrix, typename Vector>
120void matMult(Matrix const& A, Vector const& x, Vector& y)
121{
122 assert(&x != &y);
123 y = A*x;
124}
125
132template<typename Matrix, typename Vector>
133void matMultAdd(Matrix const& A, Vector const& v1, Vector const& v2, Vector& v3)
134{
135 assert(&v1 != &v3);
136 v3 = v2 + A*v1;
137}
138
139} // namespace LinAlg
140} // namespace MathLib
141
142// Global PETScMatrix/PETScVector //////////////////////////////////////////
143#ifdef USE_PETSC
144
145namespace MathLib {
146
147class PETScMatrix;
148class PETScVector;
149
150namespace LinAlg
151{
152
153// Vector
154
157void setLocalAccessibleVector(PETScVector const& x);
158
159void set(PETScVector& x, PetscScalar const a);
160
161void copy(PETScVector const& x, PETScVector& y);
162
163void scale(PETScVector& x, PetscScalar const a);
164
165// y = a*y + X
166void aypx(PETScVector& y, PetscScalar const a, PETScVector const& x);
167
168// y = a*x + y
169void axpy(PETScVector& y, PetscScalar const a, PETScVector const& x);
170
171// y = a*x + b*y
172void axpby(PETScVector& y, PetscScalar const a, PetscScalar const b,
173 PETScVector const& x);
174
175// Matrix
176
177void copy(PETScMatrix const& A, PETScMatrix& B);
178
179// A = a*A
180void scale(PETScMatrix& A, PetscScalar const a);
181
182// Y = a*Y + X
183void aypx(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
184
185// Y = a*X + Y
186void axpy(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
187
188// Matrix and Vector
189
190// v3 = A*v1 + v2
191void matMult(PETScMatrix const& A, PETScVector const& x, PETScVector& y);
192
193// y = A*x
194void matMultAdd(PETScMatrix const& A, PETScVector const& v1,
195 PETScVector const& v2, PETScVector& v3);
196
197void finalizeAssembly(PETScMatrix& A);
198void finalizeAssembly(PETScVector& x);
199
200}} // namespaces
201
202
203// Sparse global EigenMatrix/EigenVector //////////////////////////////////////////
204#else
205
206namespace MathLib {
207
208class EigenMatrix;
209class EigenVector;
210
211namespace LinAlg
212{
213
214// Vector
215
224void setLocalAccessibleVector(EigenVector const& x);
225
226void set(EigenVector& x, double const a);
227
228void copy(EigenVector const& x, EigenVector& y);
229
230void scale(EigenVector& x, double const a);
231
232// y = a*y + X
233void aypx(EigenVector& y, double const a, EigenVector const& x);
234
235// y = a*x + y
236void axpy(EigenVector& y, double const a, EigenVector const& x);
237
238// y = a*x + b*y
239void axpby(EigenVector& y, double const a, double const b, EigenVector const& x);
240
241
242// Matrix
243
244void copy(EigenMatrix const& A, EigenMatrix& B);
245
246// A = a*A
247void scale(EigenMatrix& A, double const a);
248
249// Y = a*Y + X
250void aypx(EigenMatrix& Y, double const a, EigenMatrix const& X);
251
252// Y = a*X + Y
253void axpy(EigenMatrix& Y, double const a, EigenMatrix const& X);
254
255
256// Matrix and Vector
257
258// y = A*x
259void matMult(EigenMatrix const& A, EigenVector const& x, EigenVector& y);
260
261// v3 = A*v1 + v2
262void matMultAdd(EigenMatrix const& A, EigenVector const& v1,
263 EigenVector const& v2, EigenVector& v3);
264
265void finalizeAssembly(EigenMatrix& x);
266void finalizeAssembly(EigenVector& A);
267
268} // namespace LinAlg
269
270} // namespace MathLib
271
272#endif
273
274namespace MathLib::LinAlg
275{
276
285template <typename VectorType>
286double computeRelativeNorm(VectorType const& x, VectorType const& y,
288{
290 {
291 OGS_FATAL("An invalid norm type has been passed");
292 }
293
294 // Stores \f$diff = x-y\f$.
295 VectorType diff;
297 MathLib::LinAlg::axpy(diff, -1.0, y);
298
300
301 const double norm_x = MathLib::LinAlg::norm(x, norm_type);
302 if (norm_x > std::numeric_limits<double>::epsilon())
303 {
304 return norm_diff / norm_x;
305 }
306
307 // Both of norm_x and norm_diff are close to zero
308 if (norm_diff < std::numeric_limits<double>::epsilon())
309 {
310 return 1.0;
311 }
312
313 // Only norm_x is close to zero
314 return norm_diff / std::numeric_limits<double>::epsilon();
315}
316} // namespace MathLib::LinAlg
#define OGS_FATAL(...)
Definition Error.h:26
double norm(MatrixOrVector const &x, MathLib::VecNormType type)
Definition LinAlg.h:94
void finalizeAssembly(PETScMatrix &A)
Definition LinAlg.cpp:170
double norm1(PETScVector const &x)
Definition LinAlg.cpp:90
double normMax(PETScVector const &x)
Definition LinAlg.cpp:110
void copy(PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:37
void componentwiseDivide(PETScVector &w, PETScVector const &x, PETScVector const &y)
Definition LinAlg.cpp:81
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:27
void set(PETScVector &x, PetscScalar const a)
Definition LinAlg.cpp:32
void matMult(PETScMatrix const &A, PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:149
double computeRelativeNorm(VectorType const &x, VectorType const &y, MathLib::VecNormType norm_type)
Definition LinAlg.h:286
void matMultAdd(PETScMatrix const &A, PETScVector const &v1, PETScVector const &v2, PETScVector &v3)
Definition LinAlg.cpp:159
double norm2(PETScVector const &x)
Definition LinAlg.cpp:100
void scale(PETScVector &x, PetscScalar const a)
Definition LinAlg.cpp:44
void aypx(PETScVector &y, PetscScalar const a, PETScVector const &x)
Definition LinAlg.cpp:50
void axpy(PETScVector &y, PetscScalar const a, PETScVector const &x)
Definition LinAlg.cpp:57
void axpby(PETScVector &y, PetscScalar const a, PetscScalar const b, PETScVector const &x)
Definition LinAlg.cpp:64
VecNormType
Norm type. Not declared as class type in order to use the members as integers.
Definition LinAlgEnums.h:22