Unexpected behaviour from mesh.GetParentElement

Hi,

I am trying to identify elements before and after refinement with each other. However, mesh.GetParentElement is returning strange results (both using mesh.Refine() and mesh.RefineUniform()) Running the following MWE

from ngsolve import *
mesh = Mesh(unit_square.GenerateMesh(maxh=2))
mesh.Refine()  # mesh.RefineUniform()
for i in range(mesh.ne):
    print(i, mesh.GetParentElement(ElementId(VOL, i)).nr)

prints

0 18446744073709551615
1 18446744073709551615
2 0
3 1
4 0
5 1
6 2
7 3

As the original mesh only has two volume elements, I was expecting only 1 and 0 as outputs. Uniform refinements leads to the parent ElementId Nr 18446744073709551615 for all elements. Same results on latest master branch on linux and v6.2.2608 on mac. Is this a bug or have I misunderstood the usage?

Best, Henry

1 Like

Hi Henry,

coarse grid elements don’t have parents, which is encoded by size_t(-1) in the C++ code, which returns this big number 2**64-1 to Python.

The element-hierarchy is only stored from refinement by bisection, which you get from mesh.Refine(), but not mesh.RefineUniform(),

best, Joachim

Hi Joachim,

Thank you for the quick reply! However, I still don’t quite understand the output: Even when I call mesh.Refine() once, I get 8 elements from the two (checked in the gui) and parent numbers 2 and 3.

Best, Henry

Hi Henry,

for that I have to explain how we encode the element hierarchy:

  • The mesh is an array of the current elements. All elements in the whole hierarchy are about twice as many, they are not stored.
  • Every element stores one parent element. That’s enough for the whole hierarchy.
  • If an element is split into two elements, say left and right one. The left takes the number of the parent, the right one is appended at the end of the array. The right one stores the parent number, the left one keeps the parent of the parent.

From this storage we cannot do grid transfer operators on the individual elements, but we can do it efficiently for the whole mesh: When we prolongate the value from parent to the right child, we also prolongate to the left child (and overwrite the parent value).

Hope this explains the numbers you see.

The mesh.Refine() calls the bisection twice, and the number of elements increases by a factor for 4 in 2D (when all elements are marked). To get only one bisection step, call it with:

mesh.Refine(onlyonce=True)

Best, Joachim

Hi Joachim,

Thank you for the detailed explanation!

Best, Henry