MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
sparsesmoothers.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12// Implementation of data types for sparse matrix smoothers
13
14#include "vector.hpp"
15#include "matrix.hpp"
16#include "sparsemat.hpp"
17#include "sparsesmoothers.hpp"
18#include <iostream>
19
20namespace mfem
21{
22
24{
25 oper = dynamic_cast<const SparseMatrix*>(&a);
26 MFEM_VERIFY(oper != nullptr, "Operator must be a SparseMatrix");
27 height = oper->Height();
28 width = oper->Width();
29
30 At.reset();
31 oper_T = nullptr;
32}
33
35{
36 if (oper_T) { return; }
37
38 const real_t tol = 1e-14;
39 if (oper->IsSymmetric() > tol * oper->MaxNorm())
40 {
41 At.reset(Transpose(*oper));
42 oper_T = At.get();
43 }
44 else
45 {
46 At.reset();
47 oper_T = oper;
48 }
49}
50
51void GSSmoother::Mult(const Vector &x, Vector &y) const
52{
53 if (!iterative_mode)
54 {
55 y = 0.0;
56 }
57 for (int i = 0; i < iterations; i++)
58 {
59 if (type != 2)
60 {
62 }
63 if (type != 1)
64 {
66 }
67 }
68}
69
70void GSSmoother::MultTranspose(const Vector &x, Vector &y) const
71{
73
74 if (!iterative_mode)
75 {
76 y = 0.0;
77 }
78
79 for (int i = 0; i < iterations; i++)
80 {
81 if (type != 1)
82 {
84 }
85 if (type != 2)
86 {
88 }
89 }
90}
91
92void DSmoother::Mult_(const SparseMatrix &A, const Vector &x, Vector &y) const
93{
94 if (!iterative_mode && type == 0 && iterations == 1)
95 {
97 return;
98 }
99
100 z.SetSize(width);
101
102 Vector *r = &y, *p = &z;
103
104 if (iterations % 2 == 0)
105 {
106 Swap<Vector*>(r, p);
107 }
108
109 if (!iterative_mode)
110 {
111 *p = 0.0;
112 }
113 else if (iterations % 2)
114 {
115 *p = y;
116 }
117 for (int i = 0; i < iterations; i++)
118 {
119 if (type == 0)
120 {
121 A.Jacobi(x, *p, *r, scale, use_abs_diag);
122 }
123 else if (type == 1)
124 {
125 A.Jacobi2(x, *p, *r, scale);
126 }
127 else if (type == 2)
128 {
129 A.Jacobi3(x, *p, *r, scale);
130 }
131 else
132 {
133 MFEM_ABORT("Invalid type.");
134 }
135 Swap<Vector*>(r, p);
136 }
137}
138
139void DSmoother::Mult(const Vector &x, Vector &y) const
140{
141 Mult_(*oper, x, y);
142}
143
144void DSmoother::MultTranspose(const Vector &x, Vector &y) const
145{
146 if (iterations == 1 && !iterative_mode)
147 {
148 Mult_(*oper, x, y);
149 return;
150 }
151
153 MFEM_VERIFY(type == 0 || !At, "l1 or lumped Jacobi transpose not implemented"
154 " for non-symmetric matrices");
155 Mult_(*oper_T, x, y);
156}
157
158}
JacobiType type
Type of diagonal scaling, see DSmoother::JacobiType.
bool use_abs_diag
Uses abs values of the diagonal entries. Relevant only with type JacobiType::JACOBI.
void Mult_(const SparseMatrix &A, const Vector &x, Vector &y) const
Apply the Jacobi smoother (used internally by Mult() and MultTranspose())
int iterations
Number of stationary iterations to perform.
void Mult(const Vector &x, Vector &y) const override
Apply the Jacobi smoother.
real_t scale
Scaling (damping) factor.
Vector z
Temporary work vector.
void MultTranspose(const Vector &x, Vector &y) const override
Apply the transpose of the Jacobi smoother.
int iterations
Number of stationary iterations.
void Mult(const Vector &x, Vector &y) const override
Application of the Gauss-Seidel smoother.
void MultTranspose(const Vector &x, Vector &y) const override
Application of the transpose of the Gauss-Seidel smoother.
GSType type
Type of Gauss-Seidel, see GSSmoother::GSType.
Abstract operator.
Definition operator.hpp:27
int width
Dimension of the input / number of columns in the matrix.
Definition operator.hpp:30
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition operator.hpp:68
int height
Dimension of the output / number of rows in the matrix.
Definition operator.hpp:29
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
Definition operator.hpp:74
bool iterative_mode
If true, use the second argument of Mult() as an initial guess.
Definition operator.hpp:858
Data type sparse matrix.
Definition sparsemat.hpp:51
real_t MaxNorm() const
void Gauss_Seidel_back(const Vector &x, Vector &y) const
void Jacobi3(const Vector &b, const Vector &x0, Vector &x1, real_t sc=1.0) const
void Jacobi(const Vector &b, const Vector &x0, Vector &x1, real_t sc, bool use_abs_diag=false) const
void Gauss_Seidel_forw(const Vector &x, Vector &y) const
Gauss-Seidel forward and backward iterations over a vector x.
void DiagScale(const Vector &b, Vector &x, real_t sc=1.0, bool use_abs_diag=false) const
x = sc b / A_ii. When use_abs_diag = true, |A_ii| is used.
real_t IsSymmetric() const
Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix.
void Jacobi2(const Vector &b, const Vector &x0, Vector &x1, real_t sc=1.0) const
void SetOperator(const Operator &a) override
Sets the underlying matrix. a must be a SparseMatrix.
std::unique_ptr< SparseMatrix > At
Transpose of A, if needed.
void EnsureTranspose() const
Ensure that the transpose is set.
const SparseMatrix * oper
The underlying matrix.
const SparseMatrix * oper_T
Vector data type.
Definition vector.hpp:82
void SetSize(int s)
Resize the vector to size s.
Definition vector.hpp:633
real_t a
Definition lissajous.cpp:41
void Transpose(const Table &A, Table &At, int ncols_A_)
Transpose a Table.
Definition table.cpp:443
void Swap(T &a, T &b)
Swap objects of type T. The operation is performed using the most specialized swap function from the ...
Definition array.hpp:767
float real_t
Definition config.hpp:46
real_t p(const Vector &x, real_t t)