# Strange behavior after using create\_mesh()

**URL:** <https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887>\
**Category:** dolfinx\
**Tags:** mesh, dolfinx\
**Created:** [June 4, 2024, 9:12am UTC](https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887 "2024-06-04T09:12:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsz7ncf24p](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rsz7ncf24p](https://fenicsproject.discourse.group/u/rsz7ncf24p)\
**Post date:** [June 4, 2024, 9:12am UTC](https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887/1 "2024-06-04T09:12:39Z")

</div>

Hello,

I am using the **Docker container of DOLFINx-v0.8.0**.  
I have a global mesh that is divided into two regions by cell markers. For visualization, I have to store the two sub-meshes. I wrote a function that loops over the connectivity and creates two new meshes, which works fine.  
**The problem** : When creating the new meshes with [create\_mesh()](https://docs.fenicsproject.org/dolfinx/v0.8.0/python/generated/dolfinx.mesh.html#dolfinx.mesh.create_mesh), I get strange errors in the code later on. I created a minimal example from the [Poisson demo](https://docs.fenicsproject.org/dolfinx/v0.8.0/python/demos/demo_poisson.html). This is just copy&paste, and I added the one line `random_mesh = create_mesh(...)` after the generation of the computational mesh.

```auto
from mpi4py import MPI
from petsc4py.PETSc import ScalarType # type: ignore
import numpy as np
import ufl
from dolfinx import fem, io, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import ds, dx, grad, inner

msh = mesh.create_rectangle(
    comm=MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=mesh.CellType.triangle,
)

# Comment this in or out to see the error
random_mesh = mesh.create_mesh(msh.comm, [[0, 1, 2]], msh.geometry.x[:, 0:2], msh.ufl_domain())

V = fem.functionspace(msh, ("Lagrange", 1))
facets = mesh.locate_entities_boundary(
    msh,
    dim=(msh.topology.dim - 1),
    marker=lambda x: np.isclose(x[0], 0.0) | np.isclose(x[0], 2.0),
)
dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)
bc = fem.dirichletbc(value=ScalarType(0), dofs=dofs, V=V)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = 10 * ufl.exp(-((x[0] - 0.5) **2 + (x[1] - 0.5)** 2) / 0.02)
g = ufl.sin(5 * x[0])
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx + inner(g, v) * ds
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
with io.XDMFFile(msh.comm, "out_poisson/poisson.xdmf", "w") as file:
    file.write_mesh(msh)
    file.write_function(uh)

```

This new mesh is just a single triangle using the first 3 points of the computational mesh. I am not doing anything with this newly created mesh, but the LinearProblem class throws `RuntimeError: Incompatible mesh. entity_maps must be provided.` Comment it out and errors are gone. Is there something I am overlooking?

Thank you!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 4, 2024, 9:27am UTC](https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887/2 "2024-06-04T09:27:04Z")

</div>

> [@rsz7ncf24p](#):
>
> `random_mesh = mesh.create_mesh(msh.comm, [[0, 1, 2]], msh.geometry.x[:, 0:2], msh.ufl_domain())`

I woudl guess an issue here is that the `msh.geometry.x` has way more nodes than [0,1,2], making it inconsistent.

Note that you can create submesh with `dolfinx.mesh.create_submesh` [dolfinx/python/dolfinx/mesh.py at bd5ba2ce6d3ac9ecfe3324d83dacd2c7be4de5ed · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/bd5ba2ce6d3ac9ecfe3324d83dacd2c7be4de5ed/python/dolfinx/mesh.py#L443) that does not have additional nodes in them.

---

<div class="post-metadata">

**Author:** ![rsz7ncf24p](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rsz7ncf24p](https://fenicsproject.discourse.group/u/rsz7ncf24p)\
**Post date:** [June 4, 2024, 9:48am UTC](https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887/3 "2024-06-04T09:48:35Z")

</div>

Thank you for the hint with `create_submesh()`, I will have a look.

Regarding the `create_mesh()`: Having a larger points array does not influence the mesh (it only picks according to the connectivity), the problem is the usage of `msh.ufl_domain()`. If I change the code to

```auto
random_mesh = mesh.create_mesh(msh.comm, [[0, 1, 2]], msh.geometry.x[:, 0:2], 
   ufl.Mesh(basix.ufl.element("Lagrange", 'triangle', 1, shape=(2,)))
)

```

it works. It seems as the `create_mesh()` routine changes something to the underlying `ufl.Mesh` element from the parent mesh. I do not know if this is intended behavior though.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 4, 2024, 11:36am UTC](https://fenicsproject.discourse.group/t/strange-behavior-after-using-create-mesh/14887/4 "2024-06-04T11:36:19Z")

</div>

The problem is that `ufl_domain()` has `ufl_cargo()` attached (circular reference):

```python
print(id(msh.ufl_domain().ufl_cargo()))
random_mesh = mesh.create_mesh(msh.comm, [[0, 1, 2]], msh.geometry.x[:, 0:2], msh.ufl_domain())
print(id(random_mesh.ufl_domain().ufl_cargo()))
print(id(msh.ufl_domain().ufl_cargo()))

```

which is set at

> <https://github.com/FEniCS/dolfinx/blob/634f51467624fff613b4b5a8dc3ee5d7454a7472/python/dolfinx/mesh.py#L88-L89>
