# Convergence test with gmsh: python API vs. gmsh native

**URL:** <https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210>\
**Category:** mesh\
**Tags:** mesh, dolfinx\
**Created:** [April 11, 2024, 12:14pm UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210 "2024-04-11T12:14:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [April 11, 2024, 12:14pm UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210/1 "2024-04-11T12:14:30Z")

</div>

Dear community.

I would like to run a fenicsx simulation on a family of **gmsh** meshes, with decreasing mesh size, in order to perform a **convergence test**. I have two options:

1. Creating a mesh in the python API of gmsh, like explained [here](https://jsdokken.com/dolfinx-tutorial/chapter3/em.html) and [here](https://jsdokken.com/src/tutorial_gmsh.html). This works well, however one **can not inspect the mesh** easily. One can export the mesh and check it in the gmsh UI or in paraview, but these are extra steps, that one has to perform.

2. Creating a mesh in gmsh, using .geo files. This works well, the gmsh UI can immediately show the result and the interplay between the .geo scripts and the visualization is very user friendly. E.g. one can create points, define a surface and read the implicitely defined identification tag (that every geometric entity has) using the UI. But: after importing the mesh to python, it **can not be refined**. I can not perform convergence tests like this.

How do the experts here handle this? Whats the best way to do it?

---

<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:** [April 11, 2024, 12:27pm UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210/2 "2024-04-11T12:27:00Z")

</div>

Note that you can use ` gmsh.fltk.run()` to make the gui display a mesh with GMSH, see: [Gmsh 4.12.2](https://gmsh.info/doc/texinfo/gmsh.html#x2)

---

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [April 17, 2024, 4:49pm UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210/3 "2024-04-17T16:49:09Z")

</div>

**A related question** :

Is it preferred to refine a mesh (created by gmsh or by dolfinx) inside a python script or to create a family of meshes with decreasing element size and importing them one by one?

I tried to implement the first case based on the [documentation](https://docs.fenicsproject.org/dolfinx/v0.4.0/cpp/refinement.html#):

```auto
from dolfinx import mesh, MPI
from mpi4py import MPI

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

# refine mesh uniformly
refined_mesh = mesh.refine(msh, redistribute=True)

```

However I get an error message:

```auto
RuntimeError: Edges must be initialised

```

How does one initialize the edges?

---

<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:** [April 18, 2024, 6:43am UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210/4 "2024-04-18T06:43:53Z")

</div>

> [@renol-kth](#):
>
> `RuntimeError: Edges must be initialised`

`mesh.topology.create_entities(1)`

> [@renol-kth](#):
>
> Is it preferred to refine a mesh (created by gmsh or by dolfinx) inside a python script or to create a family of meshes with decreasing element size and importing them one by one?

This depends on your geometry. If the boundary of the geometry is accurately represented on the coarse grid, i would refine it using dolfinx. However, it there are large curvatures, meaning that the coarse and finer grids would look significantly different, i would use Gmsh.

---

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [April 18, 2024, 7:02am UTC](https://fenicsproject.discourse.group/t/convergence-test-with-gmsh-python-api-vs-gmsh-native/14210/5 "2024-04-18T07:02:57Z")

</div>

Thank you very much for your answer!
