# Import from gmsh and define boundary conditions

**URL:** <https://fenicsproject.discourse.group/t/import-from-gmsh-and-define-boundary-conditions/2740>\
**Category:** Uncategorized\
**Created:** [March 28, 2020, 9:14am UTC](https://fenicsproject.discourse.group/t/import-from-gmsh-and-define-boundary-conditions/2740 "2020-03-28T09:14:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michi-ost](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@michi-ost](https://fenicsproject.discourse.group/u/michi-ost)\
**Post date:** [March 28, 2020, 9:14am UTC](https://fenicsproject.discourse.group/t/import-from-gmsh-and-define-boundary-conditions/2740/1 "2020-03-28T09:14:14Z")

</div>

Hi  
I have troubles to define the boundary condition correctly when I import a geometry from gmsh.  
Can someone help me? I looked for hours, but I have no idea how to do it correctly…

I think I mixed somethings I should only use when I create a geometry directly in fenics and something I should use when I import geometries…

I added some parts of the code:

```
#Defnie Mesh

    
msh = meshio.read("./beam.msh")
for cell in msh.cells:
    if cell.type == "triangle":
        triangle_cells = cell.data
    elif cell.type == "tetra":
        tetra_cells = cell.data

for key in msh.cell_data_dict["gmsh:physical"].keys():
    if key == "triangle":
        triangle_data = msh.cell_data_dict["gmsh:physical"][key]
    elif key == "tetra":
        tetra_data = msh.cell_data_dict["gmsh:physical"][key]
tetra_mesh = meshio.Mesh(points=msh.points, cells={"tetra": tetra_cells})
triangle_mesh =meshio.Mesh(points=msh.points,
                           cells=[("triangle", triangle_cells)],
                           cell_data={"name_to_read":[triangle_data]})
meshio.write("mesh.xdmf", tetra_mesh)

meshio.write("mf.xdmf", triangle_mesh)

# Create mesh 
mesh = Mesh()
with XDMFFile("mesh.xdmf") as infile:
    infile.read(mesh)
File("beam_mesh.pvd").write(mesh)

mvc = MeshValueCollection("size_t", mesh, 1)
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc)
File("beam_facets.pvd").write(mf)

Boundary Conditions:
# Create mesh function over the cell facets

boundary_subdomains = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)

#boundary_subdomains.set_all(0)
#force_boundary = AutoSubDomain(right)
#force_boundary.mark(boundary_subdomains, 3)

#Define measure for boundary condition integral
dss = ds(subdomain_data = boundary_subdomains)

#Set up boudary condition at left end
zero = Constant((0.0, 0.0, 0.0))
bc = DirichletBC(V, zero, mf, 1)

Load:
# Work of external forces
def Wext(u_):
    return dot(u_, p)*dss(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:** [March 28, 2020, 9:38am UTC](https://fenicsproject.discourse.group/t/import-from-gmsh-and-define-boundary-conditions/2740/2 "2020-03-28T09:38:19Z")

</div>

Please take some time to carefully format your question such that code snippets and mathematical formulae are correctly presented.

Furthermore construct a _minimal_ working example as described [here](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21).

The more succinct your question, the more likely it is that people will be able to offer help.

For your specific question, there is a long thread, Where I have covered most of the different issues you can get when importing external meshes, see [Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/17)
