In [1]:
import pyamg
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [2]:
n = 10
x = np.linspace(0,1,n+2)[1:-1]
A = pyamg.gallery.poisson((n,), format='csr')
In [3]:
ml = pyamg.smoothed_aggregation_solver(A, max_coarse=1, improve_candidates=None, keep=True)
In [4]:
ml
Out[4]:
multilevel_solver
Number of Levels:     4
Operator Complexity:  1.536
Grid Complexity:      1.700
Coarse Solver:        'pinv2'
  level   unknowns     nonzeros
    0           10           28 [65.12%]
    1            4           10 [23.26%]
    2            2            4 [ 9.30%]
    3            1            1 [ 2.33%]
In [6]:
T = ml.levels[0].T.toarray()
plt.spy(T, marker='o')
T
Out[6]:
array([[ 0.70710678,  0.        ,  0.        ,  0.        ],
       [ 0.70710678,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.57735027,  0.        ,  0.        ],
       [ 0.        ,  0.57735027,  0.        ,  0.        ],
       [ 0.        ,  0.57735027,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.57735027,  0.        ],
       [ 0.        ,  0.        ,  0.57735027,  0.        ],
       [ 0.        ,  0.        ,  0.57735027,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.70710678],
       [ 0.        ,  0.        ,  0.        ,  0.70710678]])
In [7]:
T = ml.levels[0].T.toarray()
for i in range(T.shape[1]):
    plt.plot(x, T[:, i])
In [8]:
P = ml.levels[0].P.toarray()
plt.spy(P, marker='s')
Out[8]:
<matplotlib.lines.Line2D at 0x10c120f98>
In [9]:
P = ml.levels[0].P.toarray()
for i in range(P.shape[1]):
    plt.plot(x, P[:, i])
In [10]:
B = np.ones((n, 2))
B[:,1] = x
print(B)
[[ 1.          0.09090909]
 [ 1.          0.18181818]
 [ 1.          0.27272727]
 [ 1.          0.36363636]
 [ 1.          0.45454545]
 [ 1.          0.54545455]
 [ 1.          0.63636364]
 [ 1.          0.72727273]
 [ 1.          0.81818182]
 [ 1.          0.90909091]]
In [11]:
ml = pyamg.smoothed_aggregation_solver(A, B=B, max_coarse=1, improve_candidates=None, keep=True)
In [12]:
ml
Out[12]:
multilevel_solver
Number of Levels:     4
Operator Complexity:  3.143
Grid Complexity:      2.400
Coarse Solver:        'pinv2'
  level   unknowns     nonzeros
    0           10           28 [31.82%]
    1            8           40 [45.45%]
    2            4           16 [18.18%]
    3            2            4 [ 4.55%]
In [13]:
T = ml.levels[0].T.toarray()
for i in range(T.shape[1]):
    plt.plot(x, T[:, i])
In [14]:
plt.spy(T, marker='o')
Out[14]:
<matplotlib.lines.Line2D at 0x10c05b860>
In [15]:
T = ml.levels[0].P.toarray()
for i in range(T.shape[1]):
    plt.plot(x, T[:, i])
In [16]:
A, B = pyamg.gallery.elasticity.linear_elasticity((4, 4))
In [17]:
A
Out[17]:
<32x32 sparse matrix of type '<class 'numpy.float64'>'
	with 400 stored elements (blocksize = 2x2) in Block Sparse Row format>
In [18]:
B
Out[18]:
array([[ 1. ,  0. ,  1.5],
       [ 0. ,  1. , -1.5],
       [ 1. ,  0. ,  1.5],
       [ 0. ,  1. , -0.5],
       [ 1. ,  0. ,  1.5],
       [ 0. ,  1. ,  0.5],
       [ 1. ,  0. ,  1.5],
       [ 0. ,  1. ,  1.5],
       [ 1. ,  0. ,  0.5],
       [ 0. ,  1. , -1.5],
       [ 1. ,  0. ,  0.5],
       [ 0. ,  1. , -0.5],
       [ 1. ,  0. ,  0.5],
       [ 0. ,  1. ,  0.5],
       [ 1. ,  0. ,  0.5],
       [ 0. ,  1. ,  1.5],
       [ 1. ,  0. , -0.5],
       [ 0. ,  1. , -1.5],
       [ 1. ,  0. , -0.5],
       [ 0. ,  1. , -0.5],
       [ 1. ,  0. , -0.5],
       [ 0. ,  1. ,  0.5],
       [ 1. ,  0. , -0.5],
       [ 0. ,  1. ,  1.5],
       [ 1. ,  0. , -1.5],
       [ 0. ,  1. , -1.5],
       [ 1. ,  0. , -1.5],
       [ 0. ,  1. , -0.5],
       [ 1. ,  0. , -1.5],
       [ 0. ,  1. ,  0.5],
       [ 1. ,  0. , -1.5],
       [ 0. ,  1. ,  1.5]])
In [ ]:
A, B = pyamg.gallery.elasticity.linear_elasticity((100, 100), nu=0.4)
In [ ]:
ml = pyamg.smoothed_aggregation_solver(A.tocsr(), max_coarse=10)
res=[]
b = np.zeros((A.shape[0],))
x0 = np.random.rand(A.shape[0])
u = ml.solve(b, x0=x0, residuals=res)
print(len(res))
print(ml)
In [ ]:
res
In [ ]:
usub0 = u[1::2]
usub1 = u[0::2]
print(len(u))
print(len(usub0))
print(len(usub1))
In [ ]:
plt.pcolor(usub1.reshape((100,100)))
plt.colorbar()
In [ ]:
ml = pyamg.smoothed_aggregation_solver(A, B=B, max_coarse=10)
res=[]
b = np.zeros((A.shape[0],))
x0 = np.random.rand(A.shape[0])
u = ml.solve(b, x0=x0, residuals=res)
len(res)
In [ ]:
res
In [ ]:
usub0 = u[1::2]
usub1 = u[0::2]
print(len(u))
print(len(usub0))
print(len(usub1))
plt.pcolor(usub1.reshape((100,100)))
In [ ]:
A
In [ ]:
ml
In [ ]: