Bad_alloc memory error when generating mesh using mshr

I have recently been exploring heat conduction modelling in FEniCS, and I have been using mshr and its solid constructive geometry functionality to generate my meshes. Today, one of my mesh generation attempts resulted in a bad_alloc memory error. The domain for the mesh was based on unions and intersections of circles and rectangles. Below, I have included a minimal working example of a mesh which reproduces the error.

from mshr import Rectangle, Circle, generate_mesh
from fenics import Point, plot
import matplotlib.pyplot as plt

r1 = Rectangle(Point(0.0, 0.0), Point(1.0, 1.0))
c1 = Circle(Point(0.3, 0.0), 0.7)
d1 = r1-c1
c2 = Circle(Point(0.5, 0.0), 0.7)
i1 = d1*c2
c3 = Circle(Point(1.2, 0.1), 0.4)
i2 = c3*i1
mesh = generate_mesh(i2, 20)

The complete traceback for the error generated by the above code is:

Traceback (most recent call last):
  File "mwe_tank_mesh_seg_fault.py", line 21, in <module>
    mesh = generate_mesh(i2, 20)
MemoryError: std::bad_alloc

My personal guess as to why this happens is that the mesh might somehow be too “complex”, thereby consuming a lot of memory. But I do not understand why this fairly simple geometry would create such a memory-consuming way.

Any input on what could be causing the error would be greatly appreaciated. Also, any recommended steps to avoid such errors would be very useful.

P.S.: While creating the MWE, I was also able to produce a segmentation fault with a slightly more complex code. I can post that code too if it is of any interest, but omitted it here to keep the post short.

Mshr is no longer maintained, and I would advise you to use an external mesh generator (such as Gmsh/pygmsh). See for instance: GitHub - nschloe/meshgen-comparison: A comparison of mesh generators.
for a comparison of several mesh generators

1 Like

I see. Thanks for the quick reply!