# Different behaviour of a mesh coming from generate\_mesh() resp. UnitSquareMesh()?

**URL:** <https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901>\
**Category:** mesh\
**Created:** [November 15, 2019, 9:01am UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901 "2019-11-15T09:01:35Z")\
**Posts on this page:** 11\
**Page:** 2

<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:** [January 15, 2021, 5:36pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/22 "2021-01-15T17:36:00Z")

</div>

Gmsh creates triangles and tetrahedrons by default. Using `recombine`, see: [Files · master · gmsh / gmsh · GitLab](https://gitlab.onelab.info/gmsh/gmsh/-/blob/master/tutorial/python/t6.py)

> [@EvaAnton](#):
>
> I use the `create_mesh` function presented [here](https://jsdokken.com/converted_files/tutorial_gmsh.html). Is there any way I can get the mesh node coordinates?

The create mesh function is part of `dolfinx` not dolfin.

> [@EvaAnton](#):
>
> Why do we need two xdmf filesL the triangle and the facet (cell type=lines)?

One is to save the mesh, while the other one is saving the facet data. To read it into `dolfin`, these needs to be separate.

> [@EvaAnton](#):
>
> It is not clear to me how I can read the mesh and use the markers I made in gmsh in order to add boundary conditions. Later, I will need to add a sudbomain in my mesh and problem, so I think that will answer my future question too.

You can load it in the same way as you load in mesh data. See for instance: [Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #3 by dokken](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/3)

---

<div class="post-metadata">

**Author:** ![EvaAnton](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/evaanton/32/1959_2.png) [@EvaAnton](https://fenicsproject.discourse.group/u/EvaAnton)\
**Post date:** [January 15, 2021, 5:46pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/23 "2021-01-15T17:46:06Z")

</div>

> [@dokken](#):
>
> Gmsh creates triangles and tetrahedrons by default. Using `recombine` , see: [tutorial/python/t6.py · master · gmsh / gmsh · GitLab](https://gitlab.onelab.info/gmsh/gmsh/-/blob/master/tutorial/python/t6.py)

Still, how can I tell which one I have in a mesh so I can read it appropriately?

> [@dokken](#):
>
> The create mesh function is part of `dolfinx` not dolfin.

This is a user defined function. In the thread mentioned earlier, the components of this function are used separate. So I found it easier to make it a function. Why/What do I need to differently?

> [@dokken](#):
>
> You can load it in the same way as you load in mesh data. See for instance: [Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #3 by dokken](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/3)

I do not see how in this comment is presented how to read the named boundaries from gmsh to fenics. I expected to have something reading the name boundaries and then using something like `bcu_noslip = DirichletBC(V, Constant((0, 0)), mf, inflow)` in order to apply the BC. Also, what do `cpp, MeshFunctionSizet` do?

---

<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:** [January 15, 2021, 6:17pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/24 "2021-01-15T18:17:00Z")

</div>

> [@EvaAnton](#):
>
> Still, how can I tell which one I have in a mesh so I can read it appropriately

Not sure what you mean by this. As that tutorial explains, if you do not use recombine, your mesh will consist of triangles when you call

```auto
gmsh.model.mesh.generate(2)

```

and tetrahedrons if you use

```auto
gmsh.model.mesh.generate(3)

```

> [@EvaAnton](#):
>
> This is a user defined function. In the thread mentioned earlier, the components of this function are used separate. So I found it easier to make it a function. Why/What do I need to differently?

As far as I can tell, you are referring to:

```auto
from dolfinx.mesh import create_mesh
from mpi4py import MPI
mesh = create_mesh(MPI.COMM_WORLD, cells, x, ufl_domain)

```

As I said earlier, if you are using `fenics`/`dolfin`, you should stop following the tutorial after

```auto
gmsh.write("mesh3D.msh")

```

> [@EvaAnton](#):
>
> `bcu_noslip = DirichletBC(V, Constant((0, 0)), mf, inflow)` in order to apply the BC. Also, what do `cpp, MeshFunctionSizet` do?

`cpp.MeshFunctionSizet` creates the `MeshFunction ` you can use in your snippet. Dolfin does not read in strings, so inflow has to be the integer you tagged your boundary with.

---

<div class="post-metadata">

**Author:** ![EvaAnton](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/evaanton/32/1959_2.png) [@EvaAnton](https://fenicsproject.discourse.group/u/EvaAnton)\
**Post date:** [January 15, 2021, 6:33pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/25 "2021-01-15T18:33:27Z")

</div>

> [@dokken](#):
>
> Not sure what you mean by this. As that tutorial explains, if you do not use recombine, your mesh will consist of triangles when you call
> 
> ```auto
> gmsh.model.mesh.generate(2)
> 
> ```
> 
> and tetrahedrons if you use
> 
> ```auto
> gmsh.model.mesh.generate(2)
> 
> ```

You wrote the same command for either triangles or tetrahedrons. So how can I tell if my mesh is with triangles or tetrahedrons?

> [@dokken](#):
>
> As far as I can tell, you are referring to:
> 
> ```auto
> from dolfinx.mesh import create_mesh
> from mpi4py import MPI
> mesh = create_mesh(MPI.COMM_WORLD, cells, x, ufl_domain)
> 
> ```
> 
> As I said earlier, if you are using `fenics` / `dolfin` , you should stop following the tutorial after
> 
> ```auto
> gmsh.write("mesh3D.msh")
> 
> ```

No, I am [using](https://jsdokken.com/converted_files/tutorial_pygmsh.html)

> `def create_mesh(mesh, cell_type): cells = numpy.vstack([cell.data for cell in mesh.cells if cell.type==cell_type]) data = numpy.hstack([mesh.cell_data_dict["gmsh:physical"][key] for key in mesh.cell_data_dict["gmsh:physical"].keys() if key==cell_type]) mesh = meshio.Mesh(points=mesh.points, cells={cell_type: cells}, cell_data={"name_to_read":[data]}) return mesh`

> [@dokken](#):
>
> `cpp.MeshFunctionSizet` creates the `MeshFunction ` you can use in your snippet. Dolfin does not read in strings, so inflow has to be the integer you tagged your boundary with.

So in order to get the `inlet` from the `gmsh` file should I write something like:

> ```
> mvc = MeshValueCollection("size_t", mesh, 2)
> with XDMFFile("mf.xdmf") as infile:
> infile.read(mvc, "name_to_read")
> mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
> bcu_noslip = DiriclhetBC(V, Constant((0,0)),mf,3)
> 
> ```

where 3 is chosen as this boundary has defined 3rd on gmsh?

There is still no documentation to explain what these commands and their arguments are so I still don’t know what the `MeshValueCollection` does and what are the input both for that and the `MeshFunctionSize` correspond to.

I also get this error:

> ```
> mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
> AttributeError: module 'mshr.cpp' has no attribute 'mesh'
> 
> ```

---

<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:** [January 15, 2021, 7:10pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/26 "2021-01-15T19:10:50Z")

</div>

> [@EvaAnton](#):
>
> You wrote the same command for either triangles or tetrahedrons. So how can I tell if my mesh is with triangles or tetrahedrons?

Sorry, typo on my side, corrected in the precious post to be 2 for triangles,3 for tetrahedrons.

> [@EvaAnton](#):
>
> No, I am [using](https://jsdokken.com/converted_files/tutorial_pygmsh.html)

Then you simply write the string for the cell type you are using. You can expect mesh.cells\_dict to see the different typos.

> [@EvaAnton](#):
>
> where 3 is chosen as this boundary has defined 3rd on gmsh?

Inlet should match the inlet\_marker you defined when generating your mesh.

> [@EvaAnton](#):
>
> There is still no documentation to explain what these commands and their arguments are so I still don’t know what the `MeshValueCollection` does and what are the input both for that and the `MeshFunctionSize` correspond to.

Are you expecting me to write out the documentation of every function? FEniCS has a Python [API](https://fenicsproject.org/olddocs/dolfin/latest/python/)  
and a C++ [API](https://fenicsproject.org/olddocs/dolfin/latest/cpp/classes.html).  
For instance, you can find what a MeshValueCollection is in the C++ API:  
[https://fenicsproject.org/olddocs/dolfin/latest/cpp/d0/db6/classdolfin\_1\_1MeshValueCollection.html](https://fenicsproject.org/olddocs/dolfin/latest/cpp/d0/db6/classdolfin_1_1MeshValueCollection.html)  
Under detailed description.

Dolfin in general has plenty of documented demos Which cover the base usage of dolfin: [https://fenicsproject.org/olddocs/dolfin/latest/python/demos.html](https://fenicsproject.org/olddocs/dolfin/latest/python/demos.html)  
and there are many posts on this forum on how to read in meshes.

---

<div class="post-metadata">

**Author:** ![EvaAnton](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/evaanton/32/1959_2.png) [@EvaAnton](https://fenicsproject.discourse.group/u/EvaAnton)\
**Post date:** [January 15, 2021, 7:16pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/27 "2021-01-15T19:16:16Z")

</div>

> [@dokken](#):
>
> Are you expecting me to write out the documentation of every function?

I am sorry if I came across as rude, I meant that I cannot find any documentation and therefore I am asking all these questions here.

quote=“EvaAnton, post:25, topic:1901”]  
I also get this error:

> ```auto
> mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
> 
> ```

AttributeError: module ‘mshr.cpp’ has no attribute ‘mesh’

> ```auto
> 
> ```

[/quote]

Any ideas about that?

> [@dokken](#):
>
> For instance, you can find what a MeshValueCollection is in the C++ API:  
> [https://fenicsproject.org/olddocs/dolfin/latest/cpp/d0/db6/classdolfin\_1\_1MeshValueCollection.html](https://fenicsproject.org/olddocs/dolfin/latest/cpp/d0/db6/classdolfin_1_1MeshValueCollection.html)  
> Under detailed description.
> 
> Dolfin in general has plenty of documented demos Which cover the base usage of dolfin: [Demos — DOLFIN documentation](https://fenicsproject.org/olddocs/dolfin/latest/python/demos.html)  
> and there are many posts on this forum on how to read in meshes.

Thank you these! I will read them carefully.

> [@dokken](#):
>
> You can expect mesh.cells\_dict to see the different typos.

What do you mean expect?

---

<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:** [January 15, 2021, 7:20pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/28 "2021-01-15T19:20:15Z")

</div>

> [@EvaAnton](#):
>
> AttributeError: module ‘mshr.cpp’ has no attribute ‘mesh’

Remove `from mshr import *` from your program. Mshr is no longer maintained and should not be imported.

> [@EvaAnton](#):
>
> What do you mean expect?

I meant inspect.  
For instance by using Ipython’s function embed  
([IPython reference — IPython 8.17.2 documentation](https://ipython.readthedocs.io/en/stable/interactive/reference.html#embedding)) you can inspect the data read in by meshio before writing them to XDMF

---

<div class="post-metadata">

**Author:** ![EvaAnton](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/evaanton/32/1959_2.png) [@EvaAnton](https://fenicsproject.discourse.group/u/EvaAnton)\
**Post date:** [January 18, 2021, 5:03pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/29 "2021-01-18T17:03:56Z")

</div>

Hi,

I have imported the mesh in fenics now correctly, thank you for your help.

In the weak formulation, I need dx and ds. When I was generating the mesh using fenics functions, these two were computed “automatically” (I think so at least, don’t know how to say it). Do I need to compute them separately here?

Thank you!

P.S.: I saw the updated tutorials and the new tutorial for dolfin-x on your webpage. Should I move to dolfin-x? I thought that wasn’t supporting fenics.

---

<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:** [January 18, 2021, 5:19pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/30 "2021-01-18T17:19:48Z")

</div>

Any measure, `dx` or `ds` In dolfin tried to extract the domain (the mesh) from the arguments in the form. If you want to make a measure with a given mesh, you can do the following:

```auto
V = FunctionSpace(mesh, "CG", 1)
u = interpolate(Expression("x[0]", degree=1), V)
dx_C = Measure("dx", domain=mesh)
print(assemble(u*dx_C), assemble(u*dx))

```

Note that the standard measure `dx` and `dx_C` gives the same result. A custom measure (`dx_C`) should be used whenever you are integrating over a subset of a volume/surface, by supplying the keyword argument: `subdomain_id=meshfunction_for_entity, subdomain_id=value_in_mesh_function`.

> [@EvaAnton](#):
>
> P.S.: I saw the updated tutorials and the new tutorial for dolfin-x on your webpage. Should I move to dolfin-x? I thought that wasn’t supporting fenics.

I would not suggest moving to dolfin-X just yet. Dolfin-x is the current development version of dolfin (no active development or bug fixing in the traditional dolfin/fenics).  
However, as it is under development, the user interface might change rapidly.  
The tutorial I’ve added on my webpage is a work in progress, which I update every time the interface of dolfin-x changes.

---

<div class="post-metadata">

**Author:** ![EvaAnton](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/evaanton/32/1959_2.png) [@EvaAnton](https://fenicsproject.discourse.group/u/EvaAnton)\
**Post date:** [January 18, 2021, 5:21pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/31 "2021-01-18T17:21:44Z")

</div>

Thank you for that. So for now, that I just have a mesh for the whole domain (no sudbomains), I don’t need to compute any new dx or ds, right?

> [@dokken](#):
>
> I would not suggest moving to dolfin-X just yet. Dolfin-x is the current development version of dolfin (no active development or bug fixing in the traditional dolfin/fenics).  
> However, as it is under development, the user interface might change rapidly.

Makes sense! Thanks!

---

<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:** [January 18, 2021, 5:23pm UTC](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901/32 "2021-01-18T17:23:20Z")

</div>

> [@EvaAnton](#):
>
> So for now, that I just have a mesh for the whole domain (no sudbomains), I don’t need to compute any new dx or ds, right?

That is correct (you can verify this by using the snippet from my previous post)

[Previous page](https://fenicsproject.discourse.group/t/different-behaviour-of-a-mesh-coming-from-generate-mesh-resp-unitsquaremesh/1901.md?page=1)
