This page was generated from unit-2.2-eigenvalues/pinvit.ipynb.
2.2 Programming an eigenvalue solver¶
We solve the generalized eigenvalue problem
where \(A\) comes from \(\int \nabla u \nabla v\), and \(M\) from \(\int u v\), on the space \(H_0^1\).
This tutorial shows how to implement linear algebra algorithms.
[1]:
from ngsolve import *
from ngsolve.webgui import Draw
import scipy.linalg
mesh = Mesh(unit_square.GenerateMesh(maxh=0.1))
We setup a stiffness matrix \(A\) and a mass matrix \(M\), and declare a preconditioner for \(A\):
[2]:
fes = H1(mesh, order=4, dirichlet=".*")
u,v = fes.TnT()
a = BilinearForm(grad(u)*grad(v)*dx)
pre = Preconditioner(a, "multigrid")
a.Assemble()
m = BilinearForm(u*v*dx).Assemble()
u = GridFunction(fes)
The inverse iteration is
where the Rayleigh quotient
converges to the smallest eigenvalue \(\lambda_1\), with rate of convergence \(\lambda_1 / \lambda_2,\) where \(\lambda_2\) is the next smallest eigenvalue.
The preconditioned inverse iteration (PINVIT), see [Knyazef+Neymeyr], replaces \(A^{-1}\) by an approximate inverse \(C^{-1}\):
The optimal step-size \(\alpha\) is found by minimizing the Rayleigh-quotient on a two-dimensional space:
This minimization problem can be solved by a small eigenvalue problem
with matrices
Then, the new iterate is
where \(y\) is the eigenvector corresponding to the smaller eigenvalue.
Implementation in NGSolve¶
First, we create some help vectors. CreateVector
creates new vectors of the same format as the existing vector, i.e., same dimension, same real/ complex type, same entry-size, and same MPI-parallel distribution if any.
[3]:
r, w, Mu, Au, Mw, Aw = u.vec.CreateVectors(6)
Next, we pick a random initial vector, which is zeroed on the Dirichlet boundary.
Below, the FV
method (short for FlatVector
) lets us access the abstract vector’s linear memory chunk, which in turn provides a "numpy" view of the memory. The projector clears the entries at the Dirichlet boundary:
[4]:
# r.FV().NumPy()[:] = random.rand(fes.ndof)
r.SetRandom()
u.vec.data = Projector(fes.FreeDofs(), True) * r
Finally, we run the PINVIT algorithm. Note that the small matrices \(a\) and \(m\) defined above are called asmall
and msmall
below. They are of type Matrix
, a class provided by NGSolve for dense matrices.
[5]:
for i in range(20):
Au.data = a.mat * u.vec
Mu.data = m.mat * u.vec
auu = InnerProduct(Au, u.vec)
muu = InnerProduct(Mu, u.vec)
# Rayleigh quotient
lam = auu/muu
print (lam / (pi**2))
# residual
r.data = Au - lam * Mu
w.data = pre.mat * r.data
# w.data = 1/Norm(w) * w
w *= 1/Norm(w)
Aw.data = a.mat * w
Mw.data = m.mat * w
# setup and solve 2x2 small eigenvalue problem
asmall = Matrix(2,2)
asmall[0,0] = auu
asmall[0,1] = asmall[1,0] = InnerProduct(Au, w)
asmall[1,1] = InnerProduct(Aw, w)
msmall = Matrix(2,2)
msmall[0,0] = muu
msmall[0,1] = msmall[1,0] = InnerProduct(Mu, w)
msmall[1,1] = InnerProduct(Mw, w)
# print ("asmall =", asmall, ", msmall = ", msmall)
eval,evec = scipy.linalg.eigh(a=asmall, b=msmall)
# print (eval, evec)
u.vec.data = float(evec[0,0]) * u.vec + float(evec[1,0]) * w
Draw (u);
21.904404687835495
2.0923100469358484
2.001383689323411
2.000176076957144
2.0000343152091773
2.0000072299731357
2.000001550275353
2.000000335620112
2.0000000730586853
2.0000000160117657
2.00000000355071
2.0000000008201924
2.0000000002198197
2.000000000087511
2.0000000000582783
2.0000000000518057
2.000000000050371
2.000000000050052
2.0000000000499796
2.000000000049964
Simultaneous iteration for several eigenvalues¶
Here are the steps for extending the above to num
vectors.
Declare a GridFunction
with multiple components to store several eigenvectors:
[6]:
num = 5
u = GridFunction(fes, multidim=num)
Create list of help vectors, and a set of random initial vectors in u
, with zero boundary conditions:
[7]:
r = u.vec.CreateVector()
Av = u.vec.CreateVector()
Mv = u.vec.CreateVector()
vecs = []
for v in u.vec.CreateVectors(2*num):
vecs.append(v)
for v in u.vecs:
r.SetRandom()
v.data = Projector(fes.FreeDofs(), True) * r
Compute num
residuals, and solve a small eigenvalue problem on a 2 \(\times\) num
dimensional space:
[8]:
asmall = Matrix(2*num, 2*num)
msmall = Matrix(2*num, 2*num)
lams = num * [1]
for i in range(20):
for j in range(num):
vecs[j].data = u.vecs[j]
r.data = a.mat * vecs[j] - lams[j] * m.mat * vecs[j]
vecs[num+j].data = pre.mat * r
for j in range(2*num):
Av.data = a.mat * vecs[j]
Mv.data = m.mat * vecs[j]
for k in range(2*num):
asmall[j,k] = InnerProduct(Av, vecs[k])
msmall[j,k] = InnerProduct(Mv, vecs[k])
ev,evec = scipy.linalg.eigh(a=asmall, b=msmall)
lams[:] = ev[0:num]
print (i, ":", [lam/pi**2 for lam in lams])
for j in range(num):
u.vecs[j][:] = 0.0
for k in range(2*num):
u.vecs[j].data += float(evec[k,j]) * vecs[k]
Draw (u);
0 : [12.28222712715127, 81.99992911901175, 91.889018570505, 104.46884797170426, 118.86745477421942]
1 : [2.0536531324331766, 7.283738007133293, 9.223445127135268, 19.786812497263043, 22.13947028625233]
2 : [2.0016408135252703, 5.098538312694369, 5.158130693843703, 9.670403101888954, 11.714654058451456]
3 : [2.000254608070317, 5.005794119945735, 5.014419141091149, 8.367187346589246, 10.401762259797527]
4 : [2.00005168671441, 5.000510112486473, 5.001518457988203, 8.111459016213269, 10.13513023490242]
5 : [2.0000112289812435, 5.000072419666015, 5.000181950172392, 8.04042356776779, 10.052179015338691]
6 : [2.0000024840525388, 5.000013283774099, 5.000025493474522, 8.01627943569109, 10.020551253121317]
7 : [2.00000055721153, 5.000002622076089, 5.000004262176816, 8.006931988818867, 10.00815219272721]
8 : [2.0000001259423072, 5.0000005083938275, 5.000000846257322, 8.003035325530876, 10.003239812243569]
9 : [2.0000000286566366, 5.00000010218525, 5.000000189311071, 8.001345897078984, 10.001289365422299]
10 : [2.000000006577866, 5.000000024647574, 5.000000047253441, 8.00060020794647, 10.000513621466354]
11 : [2.000000001545002, 5.000000009500526, 5.00000001486911, 8.000268466870647, 10.000204824344378]
12 : [2.0000000003934346, 5.0000000064392, 5.000000007310825, 8.000120272541675, 10.000081784670487]
13 : [2.000000000129104, 5.000000005459536, 5.00000000586714, 8.000053948797767, 10.000032731886527]
14 : [2.0000000000682463, 5.000000005047378, 5.0000000057179745, 8.000024228916956, 10.000013163452884]
15 : [2.0000000000541975, 5.000000004946533, 5.0000000056872596, 8.000010903066352, 10.000005354022377]
16 : [2.0000000000509455, 5.000000004922548, 5.000000005680792, 8.000004924732126, 10.000002236038474]
17 : [2.00000000005019, 5.0000000049165285, 5.000000005679234, 8.00000224184978, 10.000000990798304]
18 : [2.0000000000500138, 5.000000004914949, 5.000000005679054, 8.000001037461649, 10.000000493320531]
19 : [2.000000000049974, 5.0000000049148685, 5.000000005678596, 8.00000049667848, 10.00000029453272]
The multidim-component select in the Visualization dialog box allows to switch between the components of the multidim-GridFunction (Netgen-gui only).
Implementation using MultiVector¶
The simultaneous iteration can be optimized by using MultiVector
s. These are arrays of vectors of the same format. You can think of a MultiVector with m components of vector size n as an \(n \times m\) matrix.
a
MultiVector
consisting of num vectors of the same format as an existing vector vec is created via MultiVector(vec, num).we can iterate over the components of a MultiVector, and the bracket operator allows to access a subset of vectors
linear operator application is optimized for MultiVector
vector operations are optimized and called as mv * densematrix: \(x = y * mat\) results in x[i] = sum_j y[j] * mat[j,i] (where x and y are 'MultiVector’s, and mat is a dense matrix)
pair-wise inner products of two MultiVectors is available, the result is a dense matrix:
InnerProduct(x,y)[i,j] = InnerProduct(x_j, y_i)
. Before ngsolve-version 6.2.2301-242, the result was the transposed matrix.mv.Orthogonalize() uses modified Gram-Schmidt to orthogonalize the vectors. Optionally, a matrix defining the inner product can be provided.
with mv.Append(vec) we can add another vector to the array of vectos. A new vector is created, and the values are copied
mv.AppendOrthogonalize(vec) appends a new vector, and orthogonalizes it against the existing vectors, which are assumed to be orthogonal.
[9]:
uvecs = MultiVector(u.vec, num)
vecs = MultiVector(u.vec, 2*num)
for v in vecs[0:num]:
v.SetRandom()
uvecs[:] = pre * vecs[0:num]
lams = Vector(num * [1])
[10]:
for i in range(20):
vecs[0:num] = a.mat * uvecs - (m.mat * uvecs).Scale (lams)
vecs[num:2*num] = pre * vecs[0:num]
vecs[0:num] = uvecs
vecs.Orthogonalize() # m.mat)
asmall = InnerProduct (vecs, a.mat*vecs)
msmall = InnerProduct (vecs, m.mat*vecs)
ev,evec = scipy.linalg.eigh(a=asmall, b=msmall)
lams = Vector(ev[0:num])
print (i, ":", [l/pi**2 for l in lams])
uvecs[:] = vecs * Matrix(evec[:,0:num])
0 : [282.1858148448292, 888.3473212212325, 932.0000741821251, 994.9016375381223, 1094.32583933004]
1 : [2.0087411490565636, 8.124820085795387, 8.649649692520843, 17.075907551222674, 21.16932022520222]
2 : [2.000132655002776, 5.104323161656337, 5.1996147852849735, 8.427570263744157, 10.913848743411824]
3 : [2.000015944572217, 5.005697793933679, 5.0198550469098935, 8.044264225073226, 10.247421690695436]
4 : [2.000003050290933, 5.000454283190808, 5.0021904205997005, 8.009314561171154, 10.085061309507646]
5 : [2.000000630164311, 5.000055900101992, 5.000268920081767, 8.002869755016835, 10.031607338130595]
6 : [2.000000134440842, 5.000009866443806, 5.000037995188168, 8.001062702161006, 10.012080387964371]
7 : [2.0000000292802893, 5.00000206677387, 5.000006198389233, 8.000429781056832, 10.004665446917228]
8 : [2.0000000064909105, 5.000000465286182, 5.000001153081151, 8.000182390866957, 10.001810171755816]
9 : [2.0000000014855375, 5.0000001108145975, 5.000000238647732, 8.000078907791764, 10.000704743905773]
10 : [2.0000000003726135, 5.000000030037183, 5.000000055530675, 8.00003444446368, 10.000274873135305]
11 : [2.000000000123053, 5.000000011331014, 5.000000016342804, 8.000015109601264, 10.00010751265119]
12 : [2.0000000000666236, 5.000000006891655, 5.000000007663943, 8.000006656365201, 10.000042167339169]
13 : [2.000000000053781, 5.000000005477871, 5.000000006044923, 8.000002954253572, 10.000016636426492]
14 : [2.0000000000508416, 5.0000000050486975, 5.000000005764469, 8.000001329412823, 10.000006640165594]
15 : [2.0000000000501643, 5.00000000494618, 5.000000005699291, 8.000000615951278, 10.000002724376968]
16 : [2.0000000000500067, 5.000000004921932, 5.000000005683788, 8.000000302242404, 10.000001187959661]
17 : [2.0000000000499703, 5.000000004916229, 5.000000005679861, 8.000000164269329, 10.000000584899118]
18 : [2.0000000000499627, 5.000000004914812, 5.000000005678879, 8.000000103524739, 10.000000347888164]
19 : [2.000000000049962, 5.000000004914542, 5.000000005679116, 8.000000076775507, 10.000000254711718]
The operations are implemented using late evaluation. The operations return expressions, and computation is done within the assignment operator. The advantage is to avoid dynamic allocation. An exception is InnerProduct, which allows an expression in the second argument (and then needs vector allocation in every call).
[ ]:
[ ]: