23. Iterative Solvers#
So far we have used direct solvers to solve the linear system of equations. Although a direct solver can profit from the sparse matrix, it’s arithmetic complexity is sub-optimal. For large-scale problems iterative solvers are a must.
The conjugate gradient (cg) method is the standard method for symmetric and positive definite matrices. It’s convergence rate depends on a preconditioner, what is a cheap approximative inverse to the matrix.
from ngsolve import *
from ngsolve.webgui import Draw
from netgen.webgui import Draw as DrawGeo
We generate a 3D geometry and mesh using the OCC constructive solid geometry (CSG) modeler:
from netgen.occ import *
cube = Box((0,0,0),(1,1,1))
cyl = Cylinder((0,0.5,0.5),X, r=0.2, h=1)
cube.faces.name = "outer"
cyl.faces.name = "cyl"
shape = cube-cyl
DrawGeo(shape);
ngmesh = OCCGeometry(shape).GenerateMesh(maxh=0.1)
for l in range(0):
ngmesh.Refine()
mesh = Mesh(ngmesh)
mesh.Curve(3)
Draw (mesh);
fes = H1(mesh, order=3, dirichlet="outer", wb_withedges=False)
print ("we have", fes.ndof, "unknowns")
u = fes.TrialFunction()
v = fes.TestFunction()
a = BilinearForm(fes)
a += grad(u)*grad(v)*dx
f = LinearForm(fes)
f += v*dx
# c = Preconditioner(a, "direct", inverse="sparsecholesky")
c = Preconditioner(a, "local")
# c = Preconditioner(a, "bddc")
gfu = GridFunction(fes)
we have 24122 unknowns
assemble system and setup preconditioner in parallel:
ngsglobals.msg_level=0
with TaskManager():
a.Assemble()
f.Assemble()
solve the system using the preconditioned conjugate gradient method:
from ngsolve.krylovspace import CGSolver
with TaskManager():
inv = CGSolver(mat=a.mat, pre=c.mat, printrates='\r', maxiter=1000)
gfu.vec.data = inv * f.vec
CG iteration 1, residual = 0.04806510211955677
CG iteration 2, residual = 0.05344491649550201
CG iteration 3, residual = 0.055978693050393775
CG iteration 4, residual = 0.04586947132393056
CG iteration 5, residual = 0.03064999080365226
CG iteration 6, residual = 0.024350139933223342
CG iteration 7, residual = 0.018971694657541442
CG iteration 8, residual = 0.013456673742784586
CG iteration 9, residual = 0.009691378582122125
CG iteration 10, residual = 0.00760292152158016
CG iteration 11, residual = 0.0067429063707960726
CG iteration 12, residual = 0.006435687149362532
CG iteration 13, residual = 0.00637191205436759
CG iteration 14, residual = 0.00672751465359335
CG iteration 15, residual = 0.006627266907791075
CG iteration 16, residual = 0.005696907354488685
CG iteration 17, residual = 0.004423754350765525
CG iteration 18, residual = 0.0033608706866228976
CG iteration 19, residual = 0.002536597004582901
CG iteration 20, residual = 0.0019181433938188891
CG iteration 21, residual = 0.0015753395979253672
CG iteration 22, residual = 0.0013996176084161887
CG iteration 23, residual = 0.0012669872488662375
CG iteration 24, residual = 0.0011384226489814255
CG iteration 25, residual = 0.0009909055706566802
CG iteration 26, residual = 0.0008561178275399174
CG iteration 27, residual = 0.0007149435463959494
CG iteration 28, residual = 0.0005817165198820656
CG iteration 29, residual = 0.0004807539155406616
CG iteration 30, residual = 0.00040195008262801363
CG iteration 31, residual = 0.00033617615638427997
CG iteration 32, residual = 0.0002656615907361851
CG iteration 33, residual = 0.0002074008177649625
CG iteration 34, residual = 0.00015962402318253142
CG iteration 35, residual = 0.000126634801087262
CG iteration 36, residual = 0.00010202537668824331
CG iteration 37, residual = 8.520359637808186e-05
CG iteration 38, residual = 7.637275579162254e-05
CG iteration 39, residual = 7.031401193856226e-05
CG iteration 40, residual = 6.352282560115993e-05
CG iteration 41, residual = 5.616675627864194e-05
CG iteration 42, residual = 4.7509209253314846e-05
CG iteration 43, residual = 3.86223791713412e-05
CG iteration 44, residual = 3.0756875977618745e-05
CG iteration 45, residual = 2.38389714860435e-05
CG iteration 46, residual = 1.846519245956541e-05
CG iteration 47, residual = 1.4431021234217446e-05
CG iteration 48, residual = 1.1428458487443847e-05
CG iteration 49, residual = 9.647351724751879e-06
CG iteration 50, residual = 8.817024336650674e-06
CG iteration 51, residual = 8.201009184670373e-06
CG iteration 52, residual = 7.903920342008066e-06
CG iteration 53, residual = 7.120008835425021e-06
CG iteration 54, residual = 5.9908766583358066e-06
CG iteration 55, residual = 4.830530488977775e-06
CG iteration 56, residual = 3.585518265778479e-06
CG iteration 57, residual = 2.6171907183841164e-06
CG iteration 58, residual = 1.9371172490481405e-06
CG iteration 59, residual = 1.5340054724924599e-06
CG iteration 60, residual = 1.3251054146196412e-06
CG iteration 61, residual = 1.2364901733679504e-06
CG iteration 62, residual = 1.197458984371213e-06
CG iteration 63, residual = 1.1381918221831465e-06
CG iteration 64, residual = 1.026751050073538e-06
CG iteration 65, residual = 8.519136807142441e-07
CG iteration 66, residual = 6.649806871390978e-07
CG iteration 67, residual = 5.16233235571811e-07
CG iteration 68, residual = 3.9962754254619026e-07
CG iteration 69, residual = 3.1408101147486025e-07
CG iteration 70, residual = 2.5679117665400085e-07
CG iteration 71, residual = 2.1860287200207113e-07
CG iteration 72, residual = 1.9356954102429998e-07
CG iteration 73, residual = 1.7523856001046341e-07
CG iteration 74, residual = 1.56618873965127e-07
CG iteration 75, residual = 1.3935108142987795e-07
CG iteration 76, residual = 1.2144225860006397e-07
CG iteration 77, residual = 1.0087258095910289e-07
CG iteration 78, residual = 8.163338682520446e-08
CG iteration 79, residual = 6.59556350351198e-08
CG iteration 80, residual = 5.332216081857191e-08
CG iteration 81, residual = 4.20373310691318e-08
CG iteration 82, residual = 3.426074314802997e-08
CG iteration 83, residual = 2.81019880981836e-08
CG iteration 84, residual = 2.351315028894017e-08
CG iteration 85, residual = 1.9782537130399495e-08
CG iteration 86, residual = 1.7133466708603913e-08
CG iteration 87, residual = 1.5186927432287227e-08
CG iteration 88, residual = 1.360279354558086e-08
CG iteration 89, residual = 1.21232454398056e-08
CG iteration 90, residual = 1.0504973800455926e-08
CG iteration 91, residual = 8.792624352183583e-09
CG iteration 92, residual = 7.219311352272364e-09
CG iteration 93, residual = 5.633025875995565e-09
CG iteration 94, residual = 4.325235977572173e-09
CG iteration 95, residual = 3.3977823635641397e-09
CG iteration 96, residual = 2.7698373399373578e-09
CG iteration 97, residual = 2.3356529760385166e-09
CG iteration 98, residual = 2.064976356921994e-09
CG iteration 99, residual = 1.866904277470292e-09
CG iteration 100, residual = 1.7029693709873805e-09
CG iteration 101, residual = 1.551455126570384e-09
CG iteration 102, residual = 1.3426310142876964e-09
CG iteration 103, residual = 1.1018278978753784e-09
CG iteration 104, residual = 8.707861365262764e-10
CG iteration 105, residual = 6.745064672549372e-10
CG iteration 106, residual = 5.076082934449581e-10
CG iteration 107, residual = 3.9291122110777004e-10
CG iteration 108, residual = 3.11129453856638e-10
CG iteration 109, residual = 2.649920931924755e-10
CG iteration 110, residual = 2.3837765177368077e-10
CG iteration 111, residual = 2.225188144842397e-10
CG iteration 112, residual = 2.0885893092372014e-10
CG iteration 113, residual = 1.8883978447635868e-10
CG iteration 114, residual = 1.6031386397015726e-10
CG iteration 115, residual = 1.2645731258927568e-10
CG iteration 116, residual = 9.387576344946969e-11
CG iteration 117, residual = 6.990662676851192e-11
CG iteration 118, residual = 5.33995944974947e-11
CG iteration 119, residual = 4.390894230104411e-11
CG iteration 120, residual = 3.929743733528255e-11
CG iteration 121, residual = 3.6892942396289417e-11
CG iteration 122, residual = 3.589113576812359e-11
CG iteration 123, residual = 3.423288002036115e-11
CG iteration 124, residual = 3.0303342936854575e-11
CG iteration 125, residual = 2.4581316692830116e-11
CG iteration 126, residual = 1.8474613382016514e-11
CG iteration 127, residual = 1.3279230960750384e-11
CG iteration 128, residual = 9.686671286591162e-12
CG iteration 129, residual = 7.435634277869624e-12
CG iteration 130, residual = 6.031771803421876e-12
CG iteration 131, residual = 5.3838626275387946e-12
CG iteration 132, residual = 5.1791967488441865e-12
CG iteration 133, residual = 5.097171953227744e-12
CG iteration 134, residual = 4.791370709187812e-12
CG iteration 135, residual = 4.223495864805348e-12
CG iteration 136, residual = 3.302588405726527e-12
CG iteration 137, residual = 2.408278726713918e-12
CG iteration 138, residual = 1.7097514283877776e-12
CG iteration 139, residual = 1.2075554889771404e-12
CG iteration 140, residual = 9.088513782586566e-13
CG iteration 141, residual = 7.559150631828224e-13
CG iteration 142, residual = 6.835352316052409e-13
CG iteration 143, residual = 6.800329734334368e-13
CG iteration 144, residual = 6.75036673408816e-13
CG iteration 145, residual = 6.163897068132652e-13
CG iteration 146, residual = 5.101661217538946e-13
CG iteration 147, residual = 3.8071469300125603e-13
CG iteration 148, residual = 2.757028815928223e-13
CG iteration 149, residual = 1.9582859048357199e-13
CG iteration 150, residual = 1.4135332703825085e-13
CG iteration 151, residual = 1.0957048506765315e-13
CG iteration 152, residual = 9.320332676353627e-14
CG iteration 153, residual = 8.635189617658892e-14
CG iteration 154, residual = 8.43008811064005e-14
CG iteration 155, residual = 8.154242381775074e-14
CG iteration 156, residual = 7.408736042672486e-14
CG iteration 157, residual = 6.138411304974658e-14
CG iteration 158, residual = 4.712906873279064e-14
CG converged in 158 iterations to residual 4.712906873279064e-14
Draw (gfu);