-
Notifications
You must be signed in to change notification settings - Fork 3
/
ruge_stuben_custom_solver.py
200 lines (168 loc) · 7.23 KB
/
ruge_stuben_custom_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Classical AMG (Ruge-Stuben AMG)."""
from __future__ import absolute_import
import math
from warnings import warn
from scipy.sparse import csr_matrix, isspmatrix_csr, SparseEfficiencyWarning
from pyamg.multilevel import multilevel_solver
from pyamg.relaxation.smoothing import change_smoothers
from pyamg.strength import classical_strength_of_connection, \
symmetric_strength_of_connection, evolution_strength_of_connection, \
distance_strength_of_connection, energy_based_strength_of_connection, \
algebraic_distance, affinity_distance
from pyamg.classical.interpolate import direct_interpolation
from pyamg.classical import split
from pyamg.classical.cr import CR
import numpy as np
__all__ = ['ruge_stuben_custom_solver']
# similar to "ruge_stuben_solver" in pyamg, but with additional prolongation function parameter
def ruge_stuben_custom_solver(A, prolongation_function,
strength=('classical', {'theta': 0.25}),
CF='RS',
presmoother=('gauss_seidel', {'sweep': 'forward'}),
postsmoother=('gauss_seidel', {'sweep': 'forward'}),
max_levels=10, max_coarse=10, keep=False, **kwargs):
"""Create a multilevel solver using Classical AMG (Ruge-Stuben AMG).
Parameters
----------
A : csr_matrix
Square matrix in CSR format
prolongation_function : function
receives matrix A, splitting, and the baseline prolongation matrix P
outputs prolongation matrix P
strength : ['symmetric', 'classical', 'evolution', 'distance', 'algebraic_distance','affinity', 'energy_based', None]
Method used to determine the strength of connection between unknowns
of the linear system. Method-specific parameters may be passed in
using a tuple, e.g. strength=('symmetric',{'theta' : 0.25 }). If
strength=None, all nonzero entries of the matrix are considered strong.
CF : string
Method used for coarse grid selection (C/F splitting)
Supported methods are RS, PMIS, PMISc, CLJP, CLJPc, and CR.
presmoother : string or dict
Method used for presmoothing at each level. Method-specific parameters
may be passed in using a tuple, e.g.
presmoother=('gauss_seidel',{'sweep':'symmetric}), the default.
postsmoother : string or dict
Postsmoothing method with the same usage as presmoother
max_levels: integer
Maximum number of levels to be used in the multilevel solver.
max_coarse: integer
Maximum number of variables permitted on the coarse grid.
keep: bool
Flag to indicate keeping extra operators in the hierarchy for
diagnostics. For example, if True, then strength of connection (C) and
tentative prolongation (T) are kept.
Returns
-------
ml : multilevel_solver
Multigrid hierarchy of matrices and prolongation operators
Examples
--------
>>> from pyamg.gallery import poisson
>>> from pyamg import ruge_stuben_solver
>>> A = poisson((10,),format='csr')
>>> ml = ruge_stuben_solver(A,max_coarse=3)
Notes
-----
"coarse_solver" is an optional argument and is the solver used at the
coarsest grid. The default is a pseudo-inverse. Most simply,
coarse_solver can be one of ['splu', 'lu', 'cholesky, 'pinv',
'gauss_seidel', ... ]. Additionally, coarse_solver may be a tuple
(fn, args), where fn is a string such as ['splu', 'lu', ...] or a callable
function, and args is a dictionary of arguments to be passed to fn.
See [2001TrOoSc]_ for additional details.
References
----------
.. [2001TrOoSc] Trottenberg, U., Oosterlee, C. W., and Schuller, A.,
"Multigrid" San Diego: Academic Press, 2001. Appendix A
See Also
--------
aggregation.smoothed_aggregation_solver, multilevel_solver,
aggregation.rootnode_solver
"""
levels = [multilevel_solver.level()]
# convert A to csr
if not isspmatrix_csr(A):
try:
A = csr_matrix(A)
warn("Implicit conversion of A to CSR",
SparseEfficiencyWarning)
except BaseException:
raise TypeError('Argument A must have type csr_matrix, \
or be convertible to csr_matrix')
# preprocess A
A = A.asfptype()
if A.shape[0] != A.shape[1]:
raise ValueError('expected square matrix')
levels[-1].A = A
while len(levels) < max_levels and levels[-1].A.shape[0] > max_coarse:
extend_hierarchy(levels, strength, CF, keep, prolongation_function)
ml = multilevel_solver(levels, **kwargs)
change_smoothers(ml, presmoother, postsmoother)
return ml
# internal function
def extend_hierarchy(levels, strength, CF, keep, prolongation_function):
"""Extend the multigrid hierarchy."""
def unpack_arg(v):
if isinstance(v, tuple):
return v[0], v[1]
else:
return v, {}
A = levels[-1].A
# Compute the strength-of-connection matrix C, where larger
# C[i,j] denote stronger couplings between i and j.
fn, kwargs = unpack_arg(strength)
if fn == 'symmetric':
C = symmetric_strength_of_connection(A, **kwargs)
elif fn == 'classical':
C = classical_strength_of_connection(A, **kwargs)
elif fn == 'distance':
C = distance_strength_of_connection(A, **kwargs)
elif (fn == 'ode') or (fn == 'evolution'):
C = evolution_strength_of_connection(A, **kwargs)
elif fn == 'energy_based':
C = energy_based_strength_of_connection(A, **kwargs)
elif fn == 'algebraic_distance':
C = algebraic_distance(A, **kwargs)
elif fn == 'affinity':
C = affinity_distance(A, **kwargs)
elif fn is None:
C = A
else:
raise ValueError('unrecognized strength of connection method: %s' %
str(fn))
# Generate the C/F splitting
fn, kwargs = unpack_arg(CF)
if fn == 'RS':
splitting = split.RS(C, **kwargs)
elif fn == 'PMIS':
splitting = split.PMIS(C, **kwargs)
elif fn == 'PMISc':
splitting = split.PMISc(C, **kwargs)
elif fn == 'CLJP':
splitting = split.CLJP(C, **kwargs)
elif fn == 'CLJPc':
splitting = split.CLJPc(C, **kwargs)
elif fn == 'CR':
splitting = CR(C, **kwargs)
else:
raise ValueError('unknown C/F splitting method (%s)' % CF)
# Generate the interpolation matrix that maps from the coarse-grid to the
# fine-grid
baseline_P = direct_interpolation(A, C, splitting)
coarse_nodes = np.nonzero(splitting)[0]
# Create a prolongation matrix with the same coarse-fine splitting and sparsity pattern
# as the baseline
P = prolongation_function(A, coarse_nodes, baseline_P, C)
# Generate the restriction matrix that maps from the fine-grid to the
# coarse-grid
R = P.T.tocsr()
# Store relevant information for this level
if keep:
levels[-1].C = C # strength of connection matrix
levels[-1].splitting = splitting # C/F splitting
levels[-1].P = P # prolongation operator
levels[-1].R = R # restriction operator
levels.append(multilevel_solver.level())
# Form next level through Galerkin product
A = R * A * P
levels[-1].A = A