-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo.py
54 lines (43 loc) · 1.66 KB
/
demo.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
"""
Illustrates and plots the selection of aggregates in AMG based on smoothed aggregation
"""
import numpy
import scipy.io as sio
import matplotlib as mplt
import matplotlib.pyplot as plt
import pyamg
data = sio.loadmat('square.mat')
A = data['A'].tocsr() # matrix
V = data['vertices'][:A.shape[0]] # vertices of each variable
E = numpy.vstack((A.tocoo().row,A.tocoo().col)).T # edges of the matrix graph
# Create a multigrid solver
ml = pyamg.smoothed_aggregation_solver(A, max_levels=2, max_coarse=1, keep=True)
# AggOp[i,j] is 1 iff node i belongs to aggregate j
AggOp = ml.levels[0].AggOp
# determine which edges lie entirely inside an aggregate
# AggOp.indices[n] is the aggregate to which vertex n belongs
inner_edges = AggOp.indices[E[:,0]] == AggOp.indices[E[:,1]]
outer_edges = ~inner_edges
# set up a figure
fig, ax = plt.subplots()
# non aggregate edges
nonaggs = V[E[outer_edges].ravel(),:].reshape((-1, 2, 2))
col = mplt.collections.LineCollection(nonaggs,
color=[232.0/255, 74.0/255, 39.0/255],
linewidth=1.0)
ax.add_collection(col, autolim=True)
# aggregate edges
aggs = V[E[inner_edges].ravel(),:].reshape((-1, 2, 2))
col = mplt.collections.LineCollection(aggs,
color=[19.0/255, 41.0/255, 75.0/255],
linewidth=4.0)
ax.add_collection(col, autolim=True)
ax.autoscale_view()
ax.axis('equal')
figname = './output/aggregates.png'
import sys
if len(sys.argv) > 1:
if sys.argv[1] == '--savefig':
plt.savefig(figname, bbox_inches='tight', dpi=150)
else:
plt.show()