Invalid syntax importing GMSH to FEniCS

Hello.

I’m new at GMSH and FEniCS and I’m trying to import the mesh from GMSH to FEniCS. I am using the fallowing code lines to try to do that:

gmsh -2 filename.geo
dolfin-convert filename.msh filename.xml
mesh = Mesh("filename.xml")

But I always get the error:

gmsh -2 filename.geo
        ^
SyntaxError: invalid syntax

The filename.geo and code.py files are in the same directory. I am using WSL and dolfin 2019.2.0.dev0.

Thanks,
Matheus Seidel.

To me it seems like you are trying to call gmsh -2 filename.geo as part of your python script.
To do so, you need to use os.system for such behavior, i.e.

from dolfin import *
import os
os.system("gmsh -2 filename.geo")
os.system("dolfin-convert filename.msh filename.xml")
mesh = Mesh("filename.xml")

Thanks.

Now I’m getting:

Info    : Running 'gmsh -2 filename.geo' [Gmsh 4.4.1, 1 node, max. 1 thread]
Info    : Started on Thu Oct 21 15:18:03 2021
Info    : Reading 'filename.geo'...
Info    : Done reading 'filename.geo'
Info    : Meshing 1D...
Info    : Meshing curve 1 (Line)
Info    : Meshing curve 2 (Line)
Info    : Meshing curve 3 (Line)
Info    : Meshing curve 4 (Line)
Info    : Done meshing 1D (0.0024824 s)
Info    : Meshing 2D...
Info    : Meshing surface 1 (transfinite)
Info    : Done meshing 2D (0.000872 s)
Info    : 121 vertices 244 elements
Info    : Writing 'filename.msh'...
Info    : Done writing 'filename.msh'
Info    : Stopped on Thu Oct 21 15:18:03 2021
Converting from Gmsh format (.msh, .gmsh) to DOLFIN XML format mbseidel@DESKTOP-NPRJ0E1:/mnt/c/users/matheus seidel/documents$
  File "/usr/bin/dolfin-convert", line 132, in <module>
    main(sys.argv[1:])
  File "/usr/bin/dolfin-convert", line 79, in main
    meshconvert.convert2xml(ifilename, ofilename, iformat=iformat)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 1301, in     convert2xml
    convert(ifilename, XmlHandler(ofilename), iformat=iformat)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 1322, in convert
    gmsh2xml(ifilename, handler)
  File "/usr/lib/python3/dist-packages/dolfin_utils/meshconvert/meshconvert.py", line 271, in     gmsh2xml 
num_elements = int(line)
ValueError: invalid literal for int() with base 10: '9 244 1 244\n' Traceback (most recent call last):
File "NS_test.py", line 17, in <module>
mesh = Mesh("filename.xml")
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to read data from XML file.
*** Reason:  Not a DOLFIN XML file.
*** Where:   This error was encountered inside XMLFile.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset:  unknown
*** -------------------------------------------------------------------------

The code is as fallows:

from future import print_function

from fenics import *

import matplotlib.pyplot as plt

from dolfin import *

import os

Mesh and function space definition

#gmsh -2 filename.geo

#dolfin-convert filename.msh filename.xml

#mesh = Mesh(“filename.xml”)

os.system(“gmsh -2 filename.geo”)

os.system(“dolfin-convert filename.msh filename.xml”)

mesh = Mesh(“filename.xml”)

V = FunctionSpace(mesh, ‘Lagrange’, 1)

Trial and test functions in the same space (BC defined separately)

u = TrialFunction(V)

v = TestFunction(V)

Boundary condition

u_D = Expression(‘x[1] <= 0 + DOLFIN_EPS ? 0 : x[0] <= 0 + DOLFIN_EPS && x[1] <= 0.2 + DOLFIN_EPS ? 0 : 1’, degree=1)

def boundary(x):

return x[0] <= 0 + DOLFIN_EPS or x[1] <= 0 + DOLFIN_EPS

bc = DirichletBC(V, u_D, boundary)

Define variational problem

vel = as_vector([0.707, 0.707])

f = Constant(0)

G = (vdot(vel, grad(u)) + dot(grad((0.000001u)), grad(v)))dx - fv*dx

R = dot(vel, grad(u)) - 0.000001*div(grad(u)) - f

modvel = 1

h = 0.1

tal = h/(2*modvel)

G += (dot(vel, grad(v)))talR*dx

a = lhs(G)

L = rhs(G)

Compute solution

u = Function(V)

solve(a == L, u, bc)

Plot solution and mesh

plot(u)

plot(mesh)

Save solution to file in VTK format

vtkfile = File(‘NS_test/solution.pvd’)

vtkfile << u

Hold plot

plt.show()

In general you should not use dolfin-convert to convert your meshes, and should rather use meshio as described here: Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #181 by rkailash

1 Like