Calculations of specific entries in a.mat/f.vec.

Hi everyone,

I have a question related to the Bilinear and Linear form integrators and corresponding assembly in NGSolve. More specifically, I would like an efficient way to access specific entries in a.mat/f.vec, avoiding the complete assembly of the related bilinear/linear forms. For instance, how could one detect the specific nodes of the mesh which correspond to a non–zero contribution for (i,j)-entry of a.mat and then calculate the related element contributions compute the (i,j)-entry? Thank you in advance for your consideration,

Hi George,

you can compute element-matrices (and vectors) via the Python interface, and you can query the degrees of freedom of a certain element as in the example below.

This tutorial shows how to explore the mesh topology, like finding the elements associated with a certain vertex:


from ngsolve import *
from netgen.geom2d import unit_square

mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))

fes = H1(mesh)
u,v = fes.TnT()

bfi = SymbolicBFI(grad(u)*grad(v))

elid = ElementId(7)

# the element provides the shape functions
el = fes.GetFE(elid)

# the mapping from reference element to physical element
trafo = mesh.GetTrafo(elid)

dofs = fes.GetDofNrs(elid)
print ("dofs:", dofs)

elmat = bfi.CalcElementMatrix(el, trafo)
print ("element matrix:", elmat)

Hi Joachim,

thank you for your reply - this was exactly what i was looking for. However, I would appreciate an additional clarification. For my purposes, the Bilinear form includes terms with normal derivative jumps on specific facets:

bfi3 = SymbolicBFI(form = …,VOL_or_BND = VOL, skeleton=True)

Whenever trying to compute the respective element contributions to the assembled bilinear form using

elmat3 = bfi3.CalcElementMatrix(el, trafo)

I get the error message:

netgen.libngpy._meshing.NgException: FacetBilinearFormIntegrator can not assemble volumetric element matrices!

Is there some way to bypass this complication? Thank you in advance.

All the best,

Hi George,

since you need jump-terms, you set the skeleton=True flag. Such integrators calculate the element matrices for both elements at the facet at once.
This requires both finite elements, and both element transformations.
These FacetBilinearFormIntegrators don’t provide the usual CalcElementMatrix, thus the exception.

The two-element CalcElementMatrix is not available from Python.
I recommend to use a hybrid-DG like formulation for the (normal-derivative) jump term, then you don’t need the skeleton integration.

An example for C0-interior penalty methods for the plate equation is here:
2.9 Fourth order equations — NGS-Py 6.2.2302 documentation


Dear Joachim,

thank you for your response and for pointing me to the tutorial with the dG formulation. However, I am not sure I understand how skeleton integration can be avoided through this approach and I was hoping for some additional information.

Please find attached a partial code containing the ghost penalty term I am interested in calculating element matrices for. As you will see, an additional complication in my case is that the Bilinear Form is defined on specific facets marked via an indicator coefficient function. Is it possible without skeleton integration? Now the error message reads: ‘ngsolve.comp.SumOfIntegrals’ object has no attribute ‘CalcElementMatrix’.

Thank you in advance.