Surprising segmentation fault

I’m working on a laptop with 24GB of memory and solving the scalar Helmholtz equation with a 0 right-hand side using NewtonMinimization. I was running into memory limits in 3D when trying to get the precision I needed, so I decided to try 2D for the particular question I was looking at involving small changes of geometry. In 3D, roughly 500,000 degrees of freedom was close to my memory limit.

To my great surprise, my memory is completely filling up and causing my script to fail with a segmentation fault with only 716 degrees of freedom in 2D after adaptive meshing. I can start with a much smaller maxh and successfully run NewtonMinimization with 836 degrees of freedom, so the adaptive meshing seems somehow related.

Is there something obvious I’m missing, or are there debug tools I can use to find where in NewtonMinimization’s source the memory overflow is happening?

Thanks for any tips. I’ve attached examples that illustrate the two cases I described.
mwe.py (4.2 KB)
mwe_output.txt (27 Bytes)
mwe_works.py (4.3 KB)
mwe_works_output.txt (638 Bytes)

There’s definitely something strange happening here. I don’t get the segmentation fault if I further reduce the maxh!
mwe_maxh1en4_output.txt (1.0 KB)
mwe_output.txt (27 Bytes)
mwe_maxh5en4.py (4.2 KB)
mwe_maxh1en4.py (4.2 KB)
Edit: looks like the segmentation faults didn’t output to the file, I guess I messed up the stderr pipe. But in cases where only the import statement prints, I get a ‘Segmentation fault (core dumped)’ and massive use of memory.

The issue is in the combination of curving and refinement. We will fix that on the C++ side, in the meantime use this workaround:

while FEobjects.fes.ndof < ndof_limit and level < max_level:
    calc_error(FEobjects)
    FEobjects.mesh.Refine()
    mesh.Curve(3)
    print(f'refined ndof={FEobjects.fes.ndof}')

This recalculates the curving parameters after refine again.

Fixed with Merge branch 'curve_after_mesh_refine' into 'master' · NGSolve/netgen@ea1b53e · GitHub

Thanks for the very rapid fix!