This page was generated from unit-2.11-matrixfree/matrixfree.ipynb.
2.11 Matrix free operator application¶
Usually, we assemble matrices in sparse matrix format. Certain methods allow to improve performance and reduce memory requirements considerably by avoiding building and storing the system matrix. The counterpart of this approach is that it is now difficult to build preconditioners
Hybrid mixed method¶
consider the hybrid mixed method
where the matrix \(A\) comes from
and the matrix \(B\) from
For \(\sigma\) we use a VectorL2
space, with a Piola-mapping to the physical elements
[1]:
import netgen.gui
from netgen.geom2d import unit_square
from ngsolve import *
mesh = Mesh(unit_square.GenerateMesh(maxh=0.3))
order=1
Sigma = VectorL2(mesh, order=order, piola=True)
Vt = L2(mesh, order=order-1)
Vf = FacetFESpace(mesh, order=order)
V = FESpace([Vt,Vf])
sigma,tau = Sigma.TnT()
(ut,uf), (vt,vf) = V.TnT()
[2]:
a = BilinearForm(Sigma)
a += sigma*tau * dx
b = BilinearForm(trialspace=Sigma, testspace=V)
b += div(sigma) * vt * dx
n = specialcf.normal(mesh.dim)
dS = dx(element_boundary=True)
b += -sigma*n*vf * dS
b.Assemble()
compute element matrices …
we should observe that the matrices from the \(A\) operator are block-diagonal with \(2\times 2\) blocks, and each element matrix from \(B\) belongs to one of two equivalence classes.
[3]:
for el in mesh.Elements():
felS = Sigma.GetFE(el)
trafo = mesh.GetTrafo(el)
elmat = a.integrators[0].CalcElementMatrix(felS, trafo)
print (elmat)
0.5 0 0 0.5 0 0
0 0.25 0 0 0.25 0
0 0 0.0833333 0 0 0.0833333
0.5 0 0 1 0 0
0 0.25 0 0 0.5 0
0 0 0.0833333 0 0 0.166667
0.429018 0 0 0.293119 0 0
0 0.214509 0 0 0.146559 0
0 0 0.071503 0 0 0.0488532
0.293119 0 0 0.782994 0 0
0 0.146559 0 0 0.391497 0
0 0 0.0488532 0 0 0.130499
0.55152 0 0 0.080668 0 0
0 0.27576 0 0 0.040334 0
0 0 0.0919199 0 0 0.0134447
0.080668 0 0 0.465092 0 0
0 0.040334 0 0 0.232546 0
0 0 0.0134447 0 0 0.0775153
0.756887 0 0 0.38797 0 0
0 0.378443 0 0 0.193985 0
0 0 0.126148 0 0 0.0646617
0.38797 0 0 0.529169 0 0
0 0.193985 0 0 0.264584 0
0 0 0.0646617 0 0 0.0881948
0.908222 0 0 0.377269 0 0
0 0.454111 0 0 0.188635 0
0 0 0.15137 0 0 0.0628782
0.377269 0 0 0.431978 0 0
0 0.188635 0 0 0.215989 0
0 0 0.0628782 0 0 0.0719963
1.03274 0 0 0.367864 0 0
0 0.516371 0 0 0.183932 0
0 0 0.172124 0 0 0.0613106
0.367864 0 0 0.373108 0 0
0 0.183932 0 0 0.186554 0
0 0 0.0613106 0 0 0.0621846
0.402283 0 0 0.105411 0 0
0 0.201142 0 0 0.0527057 0
0 0 0.0670472 0 0 0.0175686
0.105411 0 0 0.649074 0 0
0 0.0527057 0 0 0.324537 0
0 0 0.0175686 0 0 0.108179
0.780109 0 0 0.409939 0 0
0 0.390054 0 0 0.204969 0
0 0 0.130018 0 0 0.0683231
0.409939 0 0 0.535886 0 0
0 0.204969 0 0 0.267943 0
0 0 0.0683231 0 0 0.0893144
0.501906 0 0 0.106893 0 0
0 0.250953 0 0 0.0534466 0
0 0 0.0836511 0 0 0.0178155
0.106893 0 0 0.520866 0 0
0 0.0534466 0 0 0.260433 0
0 0 0.0178155 0 0 0.0868111
0.675354 0 0 0.164434 0 0
0 0.337677 0 0 0.0822168 0
0 0 0.112559 0 0 0.0274056
0.164434 0 0 0.410212 0 0
0 0.0822168 0 0 0.205106 0
0 0 0.0274056 0 0 0.0683687
0.608169 0 0 0.293634 0 0
0 0.304084 0 0 0.146817 0
0 0 0.101361 0 0 0.0489391
0.293634 0 0 0.552842 0 0
0 0.146817 0 0 0.276421 0
0 0 0.0489391 0 0 0.0921403
0.740526 0 0 0.251212 0 0
0 0.370263 0 0 0.125606 0
0 0 0.123421 0 0 0.0418687
0.251212 0 0 0.422818 0 0
0 0.125606 0 0 0.211409 0
0 0 0.0418687 0 0 0.0704697
0.984078 0 0 0.69779 0 0
0 0.492039 0 0 0.348895 0
0 0 0.164013 0 0 0.116298
0.69779 0 0 0.748834 0 0
0 0.348895 0 0 0.374417 0
0 0 0.116298 0 0 0.124806
0.515848 0 0 0.147224 0 0
0 0.257924 0 0 0.073612 0
0 0 0.0859747 0 0 0.0245373
0.147224 0 0 0.526657 0 0
0 0.073612 0 0 0.263328 0
0 0 0.0245373 0 0 0.0877761
0.585476 0 0 0.239805 0 0
0 0.292738 0 0 0.119903 0
0 0 0.0975794 0 0 0.0399675
0.239805 0 0 0.525224 0 0
0 0.119903 0 0 0.262612 0
0 0 0.0399675 0 0 0.0875374
0.670122 0 0 -0.00524371 0 0
0 0.335061 0 0 -0.00262186 0
0 0 0.111687 0 0 -0.000873952
-0.00524371 0 0 0.373108 0 0
0 -0.00262186 0 0 0.186554 0
0 0 -0.000873952 0 0 0.0621846
0.832164 0 0 0.44778 0 0
0 0.416082 0 0 0.22389 0
0 0 0.138694 0 0 0.07463
0.44778 0 0 0.541368 0 0
0 0.22389 0 0 0.270684 0
0 0 0.07463 0 0 0.090228
0.663533 0 0 0.281516 0 0
0 0.331766 0 0 0.140758 0
0 0 0.110589 0 0 0.0469193
0.281516 0 0 0.496209 0 0
0 0.140758 0 0 0.248105 0
0 0 0.0469193 0 0 0.0827016
0.596508 0 0 0.175197 0 0
0 0.298254 0 0 0.0875985 0
0 0 0.099418 0 0 0.0291995
0.175197 0 0 0.470562 0 0
0 0.0875985 0 0 0.235281 0
0 0 0.0291995 0 0 0.078427
0.514165 0 0 0.39257 0 0
0 0.257083 0 0 0.196285 0
0 0 0.0856942 0 0 0.0654284
0.39257 0 0 0.785956 0 0
0 0.196285 0 0 0.392978 0
0 0 0.0654284 0 0 0.130993
0.494319 0 0 0.286403 0 0
0 0.24716 0 0 0.143202 0
0 0 0.0823865 0 0 0.0477339
0.286403 0 0 0.671685 0 0
0 0.143202 0 0 0.335843 0
0 0 0.0477339 0 0 0.111948
0.468282 0 0 -0.00503816 0 0
0 0.234141 0 0 -0.00251908 0
0 0 0.078047 0 0 -0.000839693
-0.00503816 0 0 0.533921 0 0
0 -0.00251908 0 0 0.26696 0
0 0 -0.000839693 0 0 0.0889868
0.770324 0 0 0.408029 0 0
0 0.385162 0 0 0.204015 0
0 0 0.128387 0 0 0.0680048
0.408029 0 0 0.540666 0 0
0 0.204015 0 0 0.270333 0
0 0 0.0680048 0 0 0.090111
0.650649 0 0 0.283971 0 0
0 0.325324 0 0 0.141986 0
0 0 0.108441 0 0 0.0473286
0.283971 0 0 0.508169 0 0
0 0.141986 0 0 0.254085 0
0 0 0.0473286 0 0 0.0846949
[4]:
for el in mesh.Elements():
felS = Sigma.GetFE(el)
felV = V.GetFE(el)
fel = MixedFE(felS, felV)
trafo = mesh.GetTrafo(el)
# try integratos 0 and 1 ...
elmat = b.integrators[0].CalcElementMatrix(fel, trafo)
print (elmat)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-8901926dddb7> in <module>
2 felS = Sigma.GetFE(el)
3 felV = V.GetFE(el)
----> 4 fel = MixedFE(felS, felV)
5 trafo = mesh.GetTrafo(el)
6
NameError: name 'MixedFE' is not defined
Geometry-free matrix multiplication¶
NGSolve provides now the possibility to benefit from the geometry independent element matrix. Just one matrix per equivalence class is stored, and the matrix vector product is performed for all elements simultaneously using dense matrix algebra.
[5]:
from netgen.csg import *
geom = CSGeometry()
geom.Add (Sphere(Pnt(50,50,50),80) \
-Cylinder(Pnt(-100,0,0),Pnt(200,0,0), 40) \
-Cylinder(Pnt(100,-100,100),Pnt(100,200,100),40)
-Cylinder(Pnt(0,100,-100), Pnt(0,100,200),40)
-Sphere(Pnt(50,50,50),50))
# geom.Draw()
mesh = Mesh(geom.GenerateMesh(maxh=25))
mesh.Curve(5)
# Draw (mesh)
Same as above, with the geom_free
flag for the BilinearForm
:
[6]:
order=5
SetHeapSize(100*1000*1000)
Sigma = VectorL2(mesh, order=order, piola=True)
Vt = L2(mesh, order=order-1)
Vf = FacetFESpace(mesh, order=order, dirichlet=[2])
V = FESpace([Vt,Vf])
print ("Sigma.ndof =", Sigma.ndof, ", V.ndof =", V.ndof)
sigma,tau = Sigma.TnT()
(ut,uf), (vt,vf) = V.TnT()
b = BilinearForm(trialspace=Sigma, testspace=V, geom_free=True)
b += div(sigma) * vt * dx
n = specialcf.normal(mesh.dim)
dS = dx(element_boundary=True)
b += -sigma*n*vf * dS
b.Assemble()
Sigma.ndof = 80304 , V.ndof = 41174
Check timing with geom_free=True/False
:
[7]:
x = b.mat.CreateRowVector()
y = b.mat.CreateColVector()
x[:] = 1
%timeit y.data = b.mat * x
1.82 ms ± 113 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
L2
finite element spaces have a Mass
method generating a linear operator for multiplication with the diagonal mass matrix. In case of variable coefficients or curved elements it uses numerical integration. It also provide an inverse operator:
[8]:
ainv = Sigma.Mass(rho=1).Inverse()
Combine the operators to form the Schur complement, which is actually a discretization of the Laplace operator:
[9]:
Laplace = b.mat @ ainv @ b.mat.T
[10]:
f = LinearForm (V)
f += 1*vt * dx
f.Assemble()
proj = Projector(V.FreeDofs(), True)
gfu = GridFunction (V)
from time import time
start = time()
with TaskManager():
solvers.CG(mat=Laplace, pre=proj, rhs=f.vec, sol=gfu.vec, maxsteps=5000, printrates=False)
print ("time =", time()-start)
time = 4.666211128234863
The iteration count is very high since we used only the most trivial preconditioner. We can add a low order coarse-grid preconditioner to get rid of the mesh dependency, and also local inverses to improve the \(p\)-dependency.
[11]:
Draw (gfu.components[1])
[12]:
help (solvers.CG)
Help on function CG in module ngsolve.krylovspace:
CG(mat, rhs, pre=None, sol=None, tol=1e-12, maxsteps=100, printrates=True, initialize=True, conjugate=False, callback=None)
preconditioned conjugate gradient method
Parameters
----------
mat : Matrix
The left hand side of the equation to solve. The matrix has to be spd or hermitsch.
rhs : Vector
The right hand side of the equation.
pre : Preconditioner
If provided the preconditioner is used.
sol : Vector
Start vector for CG method, if initialize is set False. Gets overwritten by the solution vector. If sol = None then a new vector is created.
tol : double
Tolerance of the residuum. CG stops if tolerance is reached.
maxsteps : int
Number of maximal steps for CG. If the maximal number is reached before the tolerance is reached CG stops.
printrates : bool
If set to True then the error of the iterations is displayed.
initialize : bool
If set to True then the initial guess for the CG method is set to zero. Otherwise the values of the vector sol, if provided, is used.
conjugate : bool
If set to True, then the complex inner product is used.
Returns
-------
(vector)
Solution vector of the CG method.