Write GMSH mesh as binary file with Python API

634 views Asked by At

I am trying to use the Python API to Gmsh to generate a VTK mesh file in binary format.

Here is a MnWE :

import gmsh as g
# Initialize gmsh:
g.initialize()
  
# square points:
lc = 1e-2
point1 = g.model.geo.add_point(0, 0, 0, lc)
point2 = g.model.geo.add_point(1, 0, 0, lc)
point3 = g.model.geo.add_point(1, 1, 0, lc)
point4 = g.model.geo.add_point(0, 1, 0, lc) 

# Edge of square:
line1 = g.model.geo.add_line(point1, point2)
line2 = g.model.geo.add_line(point2, point3)
line3 = g.model.geo.add_line(point3, point4)
line4 = g.model.geo.add_line(point4, point1)
# faces of square:
edges = g.model.geo.add_curve_loop([line1, line2, line3, line4])

g.model.geo.synchronize()
g.model.mesh.generate() 
g.model.mesh.set_order(2)

# Write mesh data:
g.write("unitsquare.vtk")

# It finalize the Gmsh API
g.finalize()

I am fairly sure that I can pass command line arguments to g.initialize but I am not sure how. The API says:

# Gmsh Python API begins here

def initialize(argv=[], readConfigFiles=True, run=False):
    """
    gmsh.initialize(argv=[], readConfigFiles=True, run=False)

    Initialize the Gmsh API. This must be called before any call to the other
    functions in the API. If `argc' and `argv' (or just `argv' in Python or
    Julia) are provided, they will be handled in the same way as the command
    line arguments in the Gmsh app. If `readConfigFiles' is set, read system
    Gmsh configuration files (gmshrc and gmsh-options). If `run' is set, run in
    the same way as the Gmsh app, either interactively or in batch mode
    depending on the command line arguments. If `run' is not set, initializing
    the API sets the options "General.AbortOnError" to 2 and "General.Terminal"
    to 1. If compiled with OpenMP support, it also sets the number of threads
    to "General.NumThreads".
    """
    api_argc_, api_argv_ = _iargcargv(argv)
    ierr = c_int()
    lib.gmshInitialize(
        api_argc_, api_argv_,
        c_int(bool(readConfigFiles)),
        c_int(bool(run)),
        byref(ierr))
    if ierr.value != 0:
        raise Exception(logger.getLastError())

From man gmsh one has the argument -bin:

   -bin
       use binary format when available.

but when I tried g.initialize(argv=["-bin"]) or g.initialize(argv=["bin"]) it didn't work.

1

There are 1 answers

0
Not a chance On

OK so I thought that maybe the first argument might not be used in the right way so I tried :

g.initialize(argv=["","-bin"])

which worked.