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& 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)
97 {
99 return norm1(x);
101 return norm2(x);
103 return normMax(x);
104 default:
105 OGS_FATAL("Invalid norm type given.");
106 }
107}
108
109template <typename Matrix>
110void finalizeAssembly(Matrix& /*A*/);
111
112// Matrix and Vector
113
120template <typename Matrix, typename Vector>
121void matMult(Matrix const& A, Vector const& x, Vector& y)
122{
123 assert(&x != &y);
124 y = A * x;
125}
126
133template <typename Matrix, typename Vector>
134void matMultAdd(Matrix const& A, Vector const& v1, Vector const& v2, Vector& v3)
135{
136 assert(&v1 != &v3);
137 v3 = v2 + A * v1;
138}
139
140} // namespace LinAlg
141} // namespace MathLib
142
143// Global PETScMatrix/PETScVector //////////////////////////////////////////
144#ifdef USE_PETSC
145
146namespace MathLib
147{
148
149class PETScMatrix;
150class PETScVector;
151
152namespace LinAlg
153{
154
155// Vector
156
159void setLocalAccessibleVector(PETScVector const& x);
160
161void set(PETScVector& x, PetscScalar const a);
162
163void copy(PETScVector const& x, PETScVector& y);
164
165void scale(PETScVector& x, PetscScalar const a);
166
167// y = a*y + X
168void aypx(PETScVector& y, PetscScalar const a, PETScVector const& x);
169
170// y = a*x + y
171void axpy(PETScVector& y, PetscScalar const a, PETScVector const& x);
172
173// y = a*x + b*y
174void axpby(PETScVector& y, PetscScalar const a, PetscScalar const b,
175 PETScVector const& x);
176
177// Matrix
178
179void copy(PETScMatrix const& A, PETScMatrix& B);
180
181// A = a*A
182void scale(PETScMatrix& A, PetscScalar const a);
183
184// Y = a*Y + X
185void aypx(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
186
187// Y = a*X + Y
188void axpy(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
189
190// Matrix and Vector
191
192// v3 = A*v1 + v2
193void matMult(PETScMatrix const& A, PETScVector const& x, PETScVector& y);
194
195// y = A*x
196void matMultAdd(PETScMatrix const& A, PETScVector const& v1,
197 PETScVector const& v2, PETScVector& v3);
198
199void finalizeAssembly(PETScMatrix& A);
200void finalizeAssembly(PETScVector& x);
201
202} // namespace LinAlg
203} // namespace MathLib
204
205// Sparse global EigenMatrix/EigenVector
206// //////////////////////////////////////////
207#else
208
209namespace MathLib
210{
211
212class EigenMatrix;
213class EigenVector;
214
215namespace LinAlg
216{
217
218// Vector
219
228void setLocalAccessibleVector(EigenVector const& x);
229
230void set(EigenVector& x, double const a);
231
232void copy(EigenVector const& x, EigenVector& y);
233
234void scale(EigenVector& x, double const a);
235
236// y = a*y + X
237void aypx(EigenVector& y, double const a, EigenVector const& x);
238
239// y = a*x + y
240void axpy(EigenVector& y, double const a, EigenVector const& x);
241
242// y = a*x + b*y
243void axpby(EigenVector& y, double const a, double const b,
244 EigenVector const& x);
245
246// Matrix
247
248void copy(EigenMatrix const& A, EigenMatrix& B);
249
250// A = a*A
251void scale(EigenMatrix& A, double const a);
252
253// Y = a*Y + X
254void aypx(EigenMatrix& Y, double const a, EigenMatrix const& X);
255
256// Y = a*X + Y
257void axpy(EigenMatrix& Y, double const a, EigenMatrix const& X);
258
259// Matrix and Vector
260
261// y = A*x
262void matMult(EigenMatrix const& A, EigenVector const& x, EigenVector& y);
263
264// v3 = A*v1 + v2
265void matMultAdd(EigenMatrix const& A, EigenVector const& v1,
266 EigenVector const& v2, EigenVector& v3);
267
268void finalizeAssembly(EigenMatrix& x);
269void finalizeAssembly(EigenVector& A);
270
271} // namespace LinAlg
272
273} // namespace MathLib
274
275#endif
276
277namespace MathLib::LinAlg
278{
279
288template <typename VectorType>
289double computeRelativeNorm(VectorType const& x, VectorType const& y,
291{
293 {
294 OGS_FATAL("An invalid norm type has been passed");
295 }
296
297 // Stores \f$diff = x-y\f$.
298 VectorType diff;
300 MathLib::LinAlg::axpy(diff, -1.0, y);
301
303
304 const double norm_x = MathLib::LinAlg::norm(x, norm_type);
305 if (norm_x > std::numeric_limits<double>::epsilon())
306 {
307 return norm_diff / norm_x;
308 }
309
310 // Both of norm_x and norm_diff are close to zero
311 if (norm_diff < std::numeric_limits<double>::epsilon())
312 {
313 return 1.0;
314 }
315
316 // Only norm_x is close to zero
317 return norm_diff / std::numeric_limits<double>::epsilon();
318}
319} // 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:289
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