23. Implement our own system assembling#
In this tutorial we
write integrators for \(\int_T f v dx\) and \(\int_T \nabla u \nabla v dx\):
put together element matrices to the global system matrix:
The implementation is available on github: TUWien-ASC/NGS-myassembling
from ngsolve import *
from ngsolve.webgui import Draw
import myassembling
dir (myassembling)
Loading myassembling library
['MyAssembleMatrix',
'MyLaplace',
'MySource',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__']
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
fes = H1(mesh, order=2, dirichlet=".*")
u, v = fes.TnT()
23.1. use our own integrators for element matrix calculation:#
a = BilinearForm(fes)
a += myassembling.MyLaplace(1)
a.Assemble()
f = LinearForm(fes)
f += myassembling.MySource(x)
f.Assemble();
gfu = GridFunction(fes)
gfu.vec.data = a.mat.Inverse(fes.FreeDofs()) * f.vec
Draw (gfu);
23.2. we can inspect the element matrix:#
mylap = myassembling.MyLaplace(1)
ei = ElementId(VOL,17)
mylap.CalcElementMatrix(fes.GetFE(ei), mesh.GetTrafo(ei))
0.665986 -0.426433 -0.239553 -0.0710721 0.110998 -0.0399256
-0.426433 0.648429 -0.221997 0.108072 -0.0710721 -0.0369995
-0.239553 -0.221997 0.46155 -0.0369995 -0.0399256 0.076925
-0.0710721 0.108072 -0.0369995 0.0369993 -0.017768 -0.00924986
0.110998 -0.0710721 -0.0399256 -0.017768 0.0369993 -0.00998139
-0.0399256 -0.0369995 0.076925 -0.00924986 -0.00998139 0.0369993
23.3. use our own matrix assembling function:#
for l in range(4): mesh.Refine()
fes.Update()
gfu.Update()
f.Assemble();
copy mesh complete
mymatrix = myassembling.MyAssembleMatrix(fes, myassembling.MyLaplace(CF(1)))
# mymatrix = myassembling.MyAssembleMatrix(fes, (grad(u)*grad(v)*dx)[0].MakeBFI())
if fes.ndof < 100000:
gfu.vec.data = mymatrix.Inverse(fes.FreeDofs()) * f.vec
Draw (gfu);
We assemble matrix
23.4. Exercise:#
Implement
MyAssembleVector
for building the right hand side vector for \(\int_\Omega f v \, dx\)Implement
MyNeumannIntegrator
for evaluating \(\int_{\partial \Omega} g v \, ds\)Have a look into thread-parallel assembling at NGSolve-itutorials unit 9.2