schurilu 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- schurilu/__init__.py +37 -0
- schurilu/krylov/__init__.py +10 -0
- schurilu/krylov/fgmres.py +212 -0
- schurilu/krylov/fgmrez.py +218 -0
- schurilu/krylov/pcg.py +146 -0
- schurilu/krylov/planczos.py +142 -0
- schurilu/preconditioners/__init__.py +12 -0
- schurilu/preconditioners/_base.py +325 -0
- schurilu/preconditioners/gemslr.py +566 -0
- schurilu/preconditioners/ilu0.py +392 -0
- schurilu/preconditioners/iluk.py +549 -0
- schurilu/preconditioners/ilut.py +518 -0
- schurilu/reordering/__init__.py +20 -0
- schurilu/reordering/spectral.py +411 -0
- schurilu/utils/__init__.py +12 -0
- schurilu/utils/_helpers.py +212 -0
- schurilu-0.1.0.dist-info/METADATA +276 -0
- schurilu-0.1.0.dist-info/RECORD +21 -0
- schurilu-0.1.0.dist-info/WHEEL +5 -0
- schurilu-0.1.0.dist-info/licenses/LICENSE +21 -0
- schurilu-0.1.0.dist-info/top_level.txt +1 -0
schurilu/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SchurILU - Incomplete LU factorization with Schur complement support
|
|
3
|
+
|
|
4
|
+
A Python package for ILU preconditioners and Krylov solvers.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from schurilu.preconditioners import ilu0, iluk, ilut, ILUResult, GeMSLR, arnoldi
|
|
8
|
+
from schurilu.krylov import fgmres, fgmrez, pcg, planczos
|
|
9
|
+
from schurilu.reordering import (
|
|
10
|
+
multilevel_partition,
|
|
11
|
+
spectral_kway,
|
|
12
|
+
unweighted_laplacian,
|
|
13
|
+
connected_components,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__version__ = "0.1.0"
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
# ILU factorizations
|
|
20
|
+
"ilu0",
|
|
21
|
+
"iluk",
|
|
22
|
+
"ilut",
|
|
23
|
+
"ILUResult",
|
|
24
|
+
# Krylov solvers
|
|
25
|
+
"fgmres",
|
|
26
|
+
"fgmrez",
|
|
27
|
+
"pcg",
|
|
28
|
+
"planczos",
|
|
29
|
+
# GeMSLR preconditioner
|
|
30
|
+
"GeMSLR",
|
|
31
|
+
"arnoldi",
|
|
32
|
+
# Reordering / partitioning
|
|
33
|
+
"multilevel_partition",
|
|
34
|
+
"spectral_kway",
|
|
35
|
+
"unweighted_laplacian",
|
|
36
|
+
"connected_components",
|
|
37
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Krylov solver module.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from schurilu.krylov.fgmres import fgmres
|
|
6
|
+
from schurilu.krylov.fgmrez import fgmrez
|
|
7
|
+
from schurilu.krylov.pcg import pcg
|
|
8
|
+
from schurilu.krylov.planczos import planczos
|
|
9
|
+
|
|
10
|
+
__all__ = ["fgmres", "fgmrez", "pcg", "planczos"]
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Flexible GMRES (real) solver.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional, Union, Callable, Tuple, Any
|
|
6
|
+
import numpy as np
|
|
7
|
+
import numpy.typing as npt
|
|
8
|
+
from scipy.sparse import spmatrix
|
|
9
|
+
from scipy.sparse.linalg import LinearOperator
|
|
10
|
+
|
|
11
|
+
# Type alias for preconditioner
|
|
12
|
+
Preconditioner = Union[
|
|
13
|
+
LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]], Any
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def fgmres(
|
|
18
|
+
A: Union[spmatrix, LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]]],
|
|
19
|
+
b: npt.NDArray[Any],
|
|
20
|
+
x0: Optional[npt.NDArray[Any]] = None,
|
|
21
|
+
M: Optional[Preconditioner] = None,
|
|
22
|
+
tol: float = 1e-6,
|
|
23
|
+
maxiter: int = 1000,
|
|
24
|
+
restart: int = 20,
|
|
25
|
+
atol: Optional[float] = None,
|
|
26
|
+
callback: Optional[Callable[[float], None]] = None,
|
|
27
|
+
) -> Tuple[npt.NDArray[Any], int]:
|
|
28
|
+
"""
|
|
29
|
+
Flexible GMRES for solving Ax = b (real arithmetic).
|
|
30
|
+
|
|
31
|
+
FGMRES allows the preconditioner M to vary at each iteration,
|
|
32
|
+
which is useful for nonlinear preconditioners.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
A : sparse matrix or LinearOperator
|
|
37
|
+
The system matrix.
|
|
38
|
+
b : ndarray
|
|
39
|
+
Right-hand side vector.
|
|
40
|
+
x0 : ndarray, optional
|
|
41
|
+
Initial guess. Default is zero vector.
|
|
42
|
+
M : LinearOperator or callable, optional
|
|
43
|
+
Preconditioner. Can be:
|
|
44
|
+
- LinearOperator with matvec method
|
|
45
|
+
- Callable that takes a vector and returns M^{-1} * v
|
|
46
|
+
- Object with solve() method (like ILUResult)
|
|
47
|
+
Default is identity (no preconditioning).
|
|
48
|
+
tol : float, optional
|
|
49
|
+
Relative tolerance for convergence. Default is 1e-6.
|
|
50
|
+
maxiter : int, optional
|
|
51
|
+
Maximum number of iterations. Default is 1000.
|
|
52
|
+
restart : int, optional
|
|
53
|
+
Number of iterations between restarts. Default is 20.
|
|
54
|
+
atol : float, optional
|
|
55
|
+
Absolute tolerance. If None, uses tol * ||b||.
|
|
56
|
+
callback : callable, optional
|
|
57
|
+
Called after each iteration with the current residual norm.
|
|
58
|
+
|
|
59
|
+
Returns
|
|
60
|
+
-------
|
|
61
|
+
x : ndarray
|
|
62
|
+
The solution vector.
|
|
63
|
+
info : int
|
|
64
|
+
0: successful convergence
|
|
65
|
+
>0: did not converge, info = number of iterations
|
|
66
|
+
"""
|
|
67
|
+
n = len(b)
|
|
68
|
+
|
|
69
|
+
# Get matrix-vector product
|
|
70
|
+
if hasattr(A, 'matvec'):
|
|
71
|
+
matvec = A.matvec
|
|
72
|
+
elif hasattr(A, '__matmul__'):
|
|
73
|
+
matvec = lambda v: A @ v
|
|
74
|
+
else:
|
|
75
|
+
matvec = A
|
|
76
|
+
|
|
77
|
+
# Get preconditioner application
|
|
78
|
+
if M is None:
|
|
79
|
+
psolve = lambda v: v.copy()
|
|
80
|
+
elif hasattr(M, 'solve'):
|
|
81
|
+
psolve = M.solve
|
|
82
|
+
elif hasattr(M, 'matvec'):
|
|
83
|
+
psolve = M.matvec
|
|
84
|
+
elif callable(M):
|
|
85
|
+
psolve = M
|
|
86
|
+
else:
|
|
87
|
+
psolve = lambda v: v.copy()
|
|
88
|
+
|
|
89
|
+
# Initial guess
|
|
90
|
+
if x0 is None:
|
|
91
|
+
x = np.zeros(n, dtype=b.dtype)
|
|
92
|
+
else:
|
|
93
|
+
x = x0.copy()
|
|
94
|
+
|
|
95
|
+
# Compute initial residual
|
|
96
|
+
r = b - matvec(x)
|
|
97
|
+
bnorm = np.linalg.norm(b)
|
|
98
|
+
|
|
99
|
+
if bnorm == 0:
|
|
100
|
+
bnorm = 1.0
|
|
101
|
+
|
|
102
|
+
# Determine tolerance
|
|
103
|
+
if atol is None:
|
|
104
|
+
atol = tol * bnorm
|
|
105
|
+
|
|
106
|
+
# Total iterations counter
|
|
107
|
+
total_iter = 0
|
|
108
|
+
|
|
109
|
+
# Outer loop (restarts)
|
|
110
|
+
while total_iter < maxiter:
|
|
111
|
+
# Compute residual norm
|
|
112
|
+
rnorm = np.linalg.norm(r)
|
|
113
|
+
|
|
114
|
+
# Check convergence (before any matvec in this restart cycle)
|
|
115
|
+
if rnorm <= atol or rnorm <= tol * bnorm:
|
|
116
|
+
return x, 0
|
|
117
|
+
|
|
118
|
+
# Normalize residual
|
|
119
|
+
beta = rnorm
|
|
120
|
+
|
|
121
|
+
# Allocate Arnoldi vectors and preconditioned vectors
|
|
122
|
+
m = min(restart, maxiter - total_iter)
|
|
123
|
+
V = np.zeros((n, m + 1), dtype=b.dtype)
|
|
124
|
+
Z = np.zeros((n, m), dtype=b.dtype)
|
|
125
|
+
H = np.zeros((m + 1, m), dtype=b.dtype)
|
|
126
|
+
|
|
127
|
+
V[:, 0] = r / beta
|
|
128
|
+
|
|
129
|
+
# Givens rotation parameters
|
|
130
|
+
cs = np.zeros(m, dtype=b.dtype)
|
|
131
|
+
sn = np.zeros(m, dtype=b.dtype)
|
|
132
|
+
g = np.zeros(m + 1, dtype=b.dtype)
|
|
133
|
+
g[0] = beta
|
|
134
|
+
|
|
135
|
+
# Inner loop (Arnoldi)
|
|
136
|
+
j = 0
|
|
137
|
+
for j in range(m):
|
|
138
|
+
# Apply preconditioner
|
|
139
|
+
Z[:, j] = psolve(V[:, j])
|
|
140
|
+
|
|
141
|
+
# Matrix-vector product
|
|
142
|
+
w = matvec(Z[:, j])
|
|
143
|
+
|
|
144
|
+
# Modified Gram-Schmidt orthogonalization
|
|
145
|
+
for i in range(j + 1):
|
|
146
|
+
H[i, j] = np.dot(w, V[:, i])
|
|
147
|
+
w = w - H[i, j] * V[:, i]
|
|
148
|
+
|
|
149
|
+
H[j + 1, j] = np.linalg.norm(w)
|
|
150
|
+
|
|
151
|
+
# Check for breakdown
|
|
152
|
+
if H[j + 1, j] < 1e-14:
|
|
153
|
+
m = j + 1
|
|
154
|
+
break
|
|
155
|
+
|
|
156
|
+
V[:, j + 1] = w / H[j + 1, j]
|
|
157
|
+
|
|
158
|
+
# Apply previous Givens rotations to the new column
|
|
159
|
+
for i in range(j):
|
|
160
|
+
temp = cs[i] * H[i, j] + sn[i] * H[i + 1, j]
|
|
161
|
+
H[i + 1, j] = -sn[i] * H[i, j] + cs[i] * H[i + 1, j]
|
|
162
|
+
H[i, j] = temp
|
|
163
|
+
|
|
164
|
+
# Compute new Givens rotation
|
|
165
|
+
denom = np.sqrt(H[j, j]**2 + H[j + 1, j]**2)
|
|
166
|
+
cs[j] = H[j, j] / denom
|
|
167
|
+
sn[j] = H[j + 1, j] / denom
|
|
168
|
+
|
|
169
|
+
# Apply Givens rotation to H and g
|
|
170
|
+
H[j, j] = cs[j] * H[j, j] + sn[j] * H[j + 1, j]
|
|
171
|
+
H[j + 1, j] = 0.0
|
|
172
|
+
|
|
173
|
+
g[j + 1] = -sn[j] * g[j]
|
|
174
|
+
g[j] = cs[j] * g[j]
|
|
175
|
+
|
|
176
|
+
total_iter += 1
|
|
177
|
+
|
|
178
|
+
# Callback with current residual estimate
|
|
179
|
+
if callback is not None:
|
|
180
|
+
callback(abs(g[j + 1]))
|
|
181
|
+
|
|
182
|
+
# Check convergence
|
|
183
|
+
if abs(g[j + 1]) <= atol or abs(g[j + 1]) <= tol * bnorm:
|
|
184
|
+
# Solve the upper triangular system
|
|
185
|
+
y = np.zeros(j + 1, dtype=b.dtype)
|
|
186
|
+
for i in range(j, -1, -1):
|
|
187
|
+
y[i] = g[i]
|
|
188
|
+
for k in range(i + 1, j + 1):
|
|
189
|
+
y[i] -= H[i, k] * y[k]
|
|
190
|
+
y[i] /= H[i, i]
|
|
191
|
+
|
|
192
|
+
# Update solution
|
|
193
|
+
x = x + Z[:, :j + 1] @ y
|
|
194
|
+
return x, 0
|
|
195
|
+
|
|
196
|
+
# Solve the upper triangular system
|
|
197
|
+
j_end = j + 1
|
|
198
|
+
y = np.zeros(j_end, dtype=b.dtype)
|
|
199
|
+
for i in range(j_end - 1, -1, -1):
|
|
200
|
+
y[i] = g[i]
|
|
201
|
+
for k in range(i + 1, j_end):
|
|
202
|
+
y[i] -= H[i, k] * y[k]
|
|
203
|
+
y[i] /= H[i, i]
|
|
204
|
+
|
|
205
|
+
# Update solution
|
|
206
|
+
x = x + Z[:, :j_end] @ y
|
|
207
|
+
|
|
208
|
+
# Compute new residual
|
|
209
|
+
r = b - matvec(x)
|
|
210
|
+
|
|
211
|
+
# Did not converge
|
|
212
|
+
return x, total_iter
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Flexible GMRES (complex) solver.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional, Union, Callable, Tuple, Any
|
|
6
|
+
import numpy as np
|
|
7
|
+
import numpy.typing as npt
|
|
8
|
+
from scipy.sparse import spmatrix
|
|
9
|
+
from scipy.sparse.linalg import LinearOperator
|
|
10
|
+
|
|
11
|
+
# Type alias for preconditioner
|
|
12
|
+
Preconditioner = Union[
|
|
13
|
+
LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]], Any
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def fgmrez(
|
|
18
|
+
A: Union[spmatrix, LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]]],
|
|
19
|
+
b: npt.NDArray[Any],
|
|
20
|
+
x0: Optional[npt.NDArray[Any]] = None,
|
|
21
|
+
M: Optional[Preconditioner] = None,
|
|
22
|
+
tol: float = 1e-6,
|
|
23
|
+
maxiter: int = 1000,
|
|
24
|
+
restart: int = 20,
|
|
25
|
+
atol: Optional[float] = None,
|
|
26
|
+
callback: Optional[Callable[[float], None]] = None,
|
|
27
|
+
) -> Tuple[npt.NDArray[Any], int]:
|
|
28
|
+
"""
|
|
29
|
+
Flexible GMRES for solving Ax = b (complex arithmetic).
|
|
30
|
+
|
|
31
|
+
FGMREZ allows the preconditioner M to vary at each iteration,
|
|
32
|
+
which is useful for nonlinear preconditioners. Uses conjugate
|
|
33
|
+
inner products appropriate for complex vectors.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
A : sparse matrix or LinearOperator
|
|
38
|
+
The system matrix (can be complex).
|
|
39
|
+
b : ndarray
|
|
40
|
+
Right-hand side vector (can be complex).
|
|
41
|
+
x0 : ndarray, optional
|
|
42
|
+
Initial guess. Default is zero vector.
|
|
43
|
+
M : LinearOperator or callable, optional
|
|
44
|
+
Preconditioner. Can be:
|
|
45
|
+
- LinearOperator with matvec method
|
|
46
|
+
- Callable that takes a vector and returns M^{-1} * v
|
|
47
|
+
- Object with solve() method (like ILUResult)
|
|
48
|
+
Default is identity (no preconditioning).
|
|
49
|
+
tol : float, optional
|
|
50
|
+
Relative tolerance for convergence. Default is 1e-6.
|
|
51
|
+
maxiter : int, optional
|
|
52
|
+
Maximum number of iterations. Default is 1000.
|
|
53
|
+
restart : int, optional
|
|
54
|
+
Number of iterations between restarts. Default is 20.
|
|
55
|
+
atol : float, optional
|
|
56
|
+
Absolute tolerance. If None, uses tol * ||b||.
|
|
57
|
+
callback : callable, optional
|
|
58
|
+
Called after each iteration with the current residual norm.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
x : ndarray
|
|
63
|
+
The solution vector.
|
|
64
|
+
info : int
|
|
65
|
+
0: successful convergence
|
|
66
|
+
>0: did not converge, info = number of iterations
|
|
67
|
+
"""
|
|
68
|
+
n = len(b)
|
|
69
|
+
|
|
70
|
+
# Ensure complex dtype
|
|
71
|
+
dtype = np.complex128 if b.dtype in [np.complex64, np.complex128] else np.complex128
|
|
72
|
+
|
|
73
|
+
# Get matrix-vector product
|
|
74
|
+
if hasattr(A, 'matvec'):
|
|
75
|
+
matvec = A.matvec
|
|
76
|
+
elif hasattr(A, '__matmul__'):
|
|
77
|
+
matvec = lambda v: A @ v
|
|
78
|
+
else:
|
|
79
|
+
matvec = A
|
|
80
|
+
|
|
81
|
+
# Get preconditioner application
|
|
82
|
+
if M is None:
|
|
83
|
+
psolve = lambda v: v.copy()
|
|
84
|
+
elif hasattr(M, 'solve'):
|
|
85
|
+
psolve = M.solve
|
|
86
|
+
elif hasattr(M, 'matvec'):
|
|
87
|
+
psolve = M.matvec
|
|
88
|
+
elif callable(M):
|
|
89
|
+
psolve = M
|
|
90
|
+
else:
|
|
91
|
+
psolve = lambda v: v.copy()
|
|
92
|
+
|
|
93
|
+
# Initial guess
|
|
94
|
+
if x0 is None:
|
|
95
|
+
x = np.zeros(n, dtype=dtype)
|
|
96
|
+
else:
|
|
97
|
+
x = x0.astype(dtype).copy()
|
|
98
|
+
|
|
99
|
+
b = b.astype(dtype)
|
|
100
|
+
|
|
101
|
+
# Compute initial residual
|
|
102
|
+
r = b - matvec(x)
|
|
103
|
+
bnorm = np.linalg.norm(b)
|
|
104
|
+
|
|
105
|
+
if bnorm == 0:
|
|
106
|
+
bnorm = 1.0
|
|
107
|
+
|
|
108
|
+
# Determine tolerance
|
|
109
|
+
if atol is None:
|
|
110
|
+
atol = tol * bnorm
|
|
111
|
+
|
|
112
|
+
# Total iterations counter
|
|
113
|
+
total_iter = 0
|
|
114
|
+
|
|
115
|
+
# Outer loop (restarts)
|
|
116
|
+
while total_iter < maxiter:
|
|
117
|
+
# Compute residual norm
|
|
118
|
+
rnorm = np.linalg.norm(r)
|
|
119
|
+
|
|
120
|
+
# Check convergence (before any matvec in this restart cycle)
|
|
121
|
+
if rnorm <= atol or rnorm <= tol * bnorm:
|
|
122
|
+
return x, 0
|
|
123
|
+
|
|
124
|
+
# Normalize residual
|
|
125
|
+
beta = rnorm
|
|
126
|
+
|
|
127
|
+
# Allocate Arnoldi vectors and preconditioned vectors
|
|
128
|
+
m = min(restart, maxiter - total_iter)
|
|
129
|
+
V = np.zeros((n, m + 1), dtype=dtype)
|
|
130
|
+
Z = np.zeros((n, m), dtype=dtype)
|
|
131
|
+
H = np.zeros((m + 1, m), dtype=dtype)
|
|
132
|
+
|
|
133
|
+
V[:, 0] = r / beta
|
|
134
|
+
|
|
135
|
+
# Givens rotation parameters
|
|
136
|
+
cs = np.zeros(m, dtype=dtype)
|
|
137
|
+
sn = np.zeros(m, dtype=dtype)
|
|
138
|
+
g = np.zeros(m + 1, dtype=dtype)
|
|
139
|
+
g[0] = beta
|
|
140
|
+
|
|
141
|
+
# Inner loop (Arnoldi)
|
|
142
|
+
j = 0
|
|
143
|
+
for j in range(m):
|
|
144
|
+
# Apply preconditioner
|
|
145
|
+
Z[:, j] = psolve(V[:, j])
|
|
146
|
+
|
|
147
|
+
# Matrix-vector product
|
|
148
|
+
w = matvec(Z[:, j])
|
|
149
|
+
|
|
150
|
+
# Modified Gram-Schmidt orthogonalization with conjugate inner product
|
|
151
|
+
for i in range(j + 1):
|
|
152
|
+
H[i, j] = np.vdot(V[:, i], w) # Conjugate inner product
|
|
153
|
+
w = w - H[i, j] * V[:, i]
|
|
154
|
+
|
|
155
|
+
H[j + 1, j] = np.linalg.norm(w)
|
|
156
|
+
|
|
157
|
+
# Check for breakdown
|
|
158
|
+
if abs(H[j + 1, j]) < 1e-14:
|
|
159
|
+
m = j + 1
|
|
160
|
+
break
|
|
161
|
+
|
|
162
|
+
V[:, j + 1] = w / H[j + 1, j]
|
|
163
|
+
|
|
164
|
+
# Apply previous Givens rotations to the new column
|
|
165
|
+
for i in range(j):
|
|
166
|
+
temp = np.conj(cs[i]) * H[i, j] + np.conj(sn[i]) * H[i + 1, j]
|
|
167
|
+
H[i + 1, j] = -sn[i] * H[i, j] + cs[i] * H[i + 1, j]
|
|
168
|
+
H[i, j] = temp
|
|
169
|
+
|
|
170
|
+
# Compute new Givens rotation (complex version)
|
|
171
|
+
denom = np.sqrt(abs(H[j, j])**2 + abs(H[j + 1, j])**2)
|
|
172
|
+
cs[j] = H[j, j] / denom
|
|
173
|
+
sn[j] = H[j + 1, j] / denom
|
|
174
|
+
|
|
175
|
+
# Apply Givens rotation to H and g
|
|
176
|
+
H[j, j] = np.conj(cs[j]) * H[j, j] + np.conj(sn[j]) * H[j + 1, j]
|
|
177
|
+
H[j + 1, j] = 0.0
|
|
178
|
+
|
|
179
|
+
g[j + 1] = -sn[j] * g[j]
|
|
180
|
+
g[j] = np.conj(cs[j]) * g[j]
|
|
181
|
+
|
|
182
|
+
total_iter += 1
|
|
183
|
+
|
|
184
|
+
# Callback with current residual estimate
|
|
185
|
+
if callback is not None:
|
|
186
|
+
callback(abs(g[j + 1]))
|
|
187
|
+
|
|
188
|
+
# Check convergence
|
|
189
|
+
if abs(g[j + 1]) <= atol or abs(g[j + 1]) <= tol * bnorm:
|
|
190
|
+
# Solve the upper triangular system
|
|
191
|
+
y = np.zeros(j + 1, dtype=dtype)
|
|
192
|
+
for i in range(j, -1, -1):
|
|
193
|
+
y[i] = g[i]
|
|
194
|
+
for k in range(i + 1, j + 1):
|
|
195
|
+
y[i] -= H[i, k] * y[k]
|
|
196
|
+
y[i] /= H[i, i]
|
|
197
|
+
|
|
198
|
+
# Update solution
|
|
199
|
+
x = x + Z[:, :j + 1] @ y
|
|
200
|
+
return x, 0
|
|
201
|
+
|
|
202
|
+
# Solve the upper triangular system
|
|
203
|
+
j_end = j + 1
|
|
204
|
+
y = np.zeros(j_end, dtype=dtype)
|
|
205
|
+
for i in range(j_end - 1, -1, -1):
|
|
206
|
+
y[i] = g[i]
|
|
207
|
+
for k in range(i + 1, j_end):
|
|
208
|
+
y[i] -= H[i, k] * y[k]
|
|
209
|
+
y[i] /= H[i, i]
|
|
210
|
+
|
|
211
|
+
# Update solution
|
|
212
|
+
x = x + Z[:, :j_end] @ y
|
|
213
|
+
|
|
214
|
+
# Compute new residual
|
|
215
|
+
r = b - matvec(x)
|
|
216
|
+
|
|
217
|
+
# Did not converge
|
|
218
|
+
return x, total_iter
|
schurilu/krylov/pcg.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Preconditioned Conjugate Gradient solver.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Optional, Union, Callable, Tuple, Any
|
|
6
|
+
import numpy as np
|
|
7
|
+
import numpy.typing as npt
|
|
8
|
+
from scipy.sparse import spmatrix
|
|
9
|
+
from scipy.sparse.linalg import LinearOperator
|
|
10
|
+
|
|
11
|
+
# Type alias for preconditioner
|
|
12
|
+
Preconditioner = Union[
|
|
13
|
+
LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]], Any
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def pcg(
|
|
18
|
+
A: Union[spmatrix, LinearOperator, Callable[[npt.NDArray[Any]], npt.NDArray[Any]]],
|
|
19
|
+
b: npt.NDArray[Any],
|
|
20
|
+
x0: Optional[npt.NDArray[Any]] = None,
|
|
21
|
+
M: Optional[Preconditioner] = None,
|
|
22
|
+
tol: float = 1e-6,
|
|
23
|
+
maxiter: int = 1000,
|
|
24
|
+
atol: Optional[float] = None,
|
|
25
|
+
callback: Optional[Callable[[float], None]] = None,
|
|
26
|
+
) -> Tuple[npt.NDArray[Any], int]:
|
|
27
|
+
"""
|
|
28
|
+
Preconditioned Conjugate Gradient for solving Ax = b.
|
|
29
|
+
|
|
30
|
+
The matrix A must be symmetric positive definite (SPD).
|
|
31
|
+
The preconditioner M should approximate A^{-1}.
|
|
32
|
+
|
|
33
|
+
Parameters
|
|
34
|
+
----------
|
|
35
|
+
A : sparse matrix or LinearOperator
|
|
36
|
+
The system matrix (must be SPD).
|
|
37
|
+
b : ndarray
|
|
38
|
+
Right-hand side vector.
|
|
39
|
+
x0 : ndarray, optional
|
|
40
|
+
Initial guess. Default is zero vector.
|
|
41
|
+
M : LinearOperator or callable, optional
|
|
42
|
+
Preconditioner. Can be:
|
|
43
|
+
- LinearOperator with matvec method
|
|
44
|
+
- Callable that takes a vector and returns M^{-1} * v
|
|
45
|
+
- Object with solve() method (like ILUResult)
|
|
46
|
+
Default is identity (no preconditioning).
|
|
47
|
+
tol : float, optional
|
|
48
|
+
Relative tolerance for convergence. Default is 1e-6.
|
|
49
|
+
maxiter : int, optional
|
|
50
|
+
Maximum number of iterations. Default is 1000.
|
|
51
|
+
atol : float, optional
|
|
52
|
+
Absolute tolerance. If None, uses tol * ||b||.
|
|
53
|
+
callback : callable, optional
|
|
54
|
+
Called after each iteration with the current residual norm.
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
x : ndarray
|
|
59
|
+
The solution vector.
|
|
60
|
+
info : int
|
|
61
|
+
0: successful convergence
|
|
62
|
+
>0: did not converge, info = number of iterations
|
|
63
|
+
"""
|
|
64
|
+
n = len(b)
|
|
65
|
+
|
|
66
|
+
# Get matrix-vector product
|
|
67
|
+
if hasattr(A, 'matvec'):
|
|
68
|
+
matvec = A.matvec
|
|
69
|
+
elif hasattr(A, '__matmul__'):
|
|
70
|
+
matvec = lambda v: A @ v
|
|
71
|
+
else:
|
|
72
|
+
matvec = A
|
|
73
|
+
|
|
74
|
+
# Get preconditioner application
|
|
75
|
+
if M is None:
|
|
76
|
+
psolve = lambda v: v.copy()
|
|
77
|
+
elif hasattr(M, 'solve'):
|
|
78
|
+
psolve = M.solve
|
|
79
|
+
elif hasattr(M, 'matvec'):
|
|
80
|
+
psolve = M.matvec
|
|
81
|
+
elif callable(M):
|
|
82
|
+
psolve = M
|
|
83
|
+
else:
|
|
84
|
+
psolve = lambda v: v.copy()
|
|
85
|
+
|
|
86
|
+
# Initial guess
|
|
87
|
+
if x0 is None:
|
|
88
|
+
x = np.zeros(n, dtype=b.dtype)
|
|
89
|
+
else:
|
|
90
|
+
x = x0.copy()
|
|
91
|
+
|
|
92
|
+
# Compute initial residual
|
|
93
|
+
r = b - matvec(x)
|
|
94
|
+
bnorm = np.linalg.norm(b)
|
|
95
|
+
|
|
96
|
+
if bnorm == 0:
|
|
97
|
+
bnorm = 1.0
|
|
98
|
+
|
|
99
|
+
# Determine tolerance
|
|
100
|
+
if atol is None:
|
|
101
|
+
atol = tol * bnorm
|
|
102
|
+
|
|
103
|
+
# Check initial convergence
|
|
104
|
+
rnorm = np.linalg.norm(r)
|
|
105
|
+
if rnorm <= atol or rnorm <= tol * bnorm:
|
|
106
|
+
return x, 0
|
|
107
|
+
|
|
108
|
+
# Apply preconditioner
|
|
109
|
+
z = psolve(r)
|
|
110
|
+
p = z.copy()
|
|
111
|
+
rho = np.dot(r, z)
|
|
112
|
+
|
|
113
|
+
for k in range(maxiter):
|
|
114
|
+
# Matrix-vector product
|
|
115
|
+
Ap = matvec(p)
|
|
116
|
+
|
|
117
|
+
# Step length
|
|
118
|
+
alpha = rho / np.dot(p, Ap)
|
|
119
|
+
|
|
120
|
+
# Update solution and residual
|
|
121
|
+
x = x + alpha * p
|
|
122
|
+
r = r - alpha * Ap
|
|
123
|
+
|
|
124
|
+
# Compute residual norm
|
|
125
|
+
rnorm = np.linalg.norm(r)
|
|
126
|
+
|
|
127
|
+
if callback is not None:
|
|
128
|
+
callback(rnorm)
|
|
129
|
+
|
|
130
|
+
# Check convergence
|
|
131
|
+
if rnorm <= atol or rnorm <= tol * bnorm:
|
|
132
|
+
return x, 0
|
|
133
|
+
|
|
134
|
+
# Apply preconditioner
|
|
135
|
+
z = psolve(r)
|
|
136
|
+
|
|
137
|
+
# New rho
|
|
138
|
+
rho_new = np.dot(r, z)
|
|
139
|
+
|
|
140
|
+
# Direction update
|
|
141
|
+
beta = rho_new / rho
|
|
142
|
+
p = z + beta * p
|
|
143
|
+
rho = rho_new
|
|
144
|
+
|
|
145
|
+
# Did not converge
|
|
146
|
+
return x, k + 1
|