1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] What binary format is the binary mesh export and how can it be converted to ascii?

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 20:12.

  1. Stack

    Stack Membro Participativo

    I have generated some large meshes using Gmsh via the Python API. The ASCII files can be Gigabytes. Since binary files are smaller and faster to read, I set Gmsh to save the mesh files in binary format. My current issue is trying to figure out how to read that binary mesh back into Python in an interpretable format.

    What binary encoding does Gmsh use for the save files and how can they be read back into Python in an interpretable way.

    Here is a simplified version of the Gmsh tutorial 1 code (ref) modified to save the mesh in binary format.

    import gmsh
    import sys

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

    gmsh.model.add("t1")

    lc = 1e-2
    gmsh.model.geo.addPoint(0, 0, 0, lc, 1)
    gmsh.model.geo.addPoint(.1, 0, 0, lc, 2)
    gmsh.model.geo.addPoint(.1, .3, 0, lc, 3)
    p4 = gmsh.model.geo.addPoint(0, .3, 0, lc)

    gmsh.model.geo.addLine(1, 2, 1)
    gmsh.model.geo.addLine(3, 2, 2)
    gmsh.model.geo.addLine(3, p4, 3)
    gmsh.model.geo.addLine(4, 1, p4)

    gmsh.model.geo.addCurveLoop([4, 1, -2, 3], 1)
    gmsh.model.geo.addPlaneSurface([1], 1)

    gmsh.model.geo.synchronize()

    gmsh.model.addPhysicalGroup(1, [1, 2, 4], 5)
    gmsh.model.addPhysicalGroup(2, [1], name="My surface")

    gmsh.model.mesh.generate(2)
    gmsh.write("t1_binary.msh")
    gmsh.finalize()


    My initial naive approach was to read the file in binary format and try .decode(), but this approach fails once it gets past the $Entities line.

    mesh_file = []
    with open("t1_binary.msh", "rb") as f:
    for line in f.readlines():
    print(line)
    mesh_file.append(line.decode())


    When it fails I get the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 72: invalid start byte.

    Continue reading...

Compartilhe esta Página