{"id":16722703,"url":"https://github.com/ahojukka5/gmshparser","last_synced_at":"2025-03-21T21:30:46.236Z","repository":{"id":50650957,"uuid":"256580476","full_name":"ahojukka5/gmshparser","owner":"ahojukka5","description":"gmshparser is a lightweight, 100 % tested and well documented package that aims to reliably parse the Gmsh ascii file format (.msh). The package does not introduce any external dependencies and thus fits well with the needs of your own FEM research code as a small stand-alone package.","archived":false,"fork":false,"pushed_at":"2020-10-27T08:27:51.000Z","size":127,"stargazers_count":26,"open_issues_count":3,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T05:12:08.678Z","etag":null,"topics":["element","fem","finite","gmsh","mesh","method","parser","python"],"latest_commit_sha":null,"homepage":"https://gmshparser.readthedocs.io/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahojukka5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-17T18:29:55.000Z","updated_at":"2024-12-07T19:18:18.000Z","dependencies_parsed_at":"2022-09-04T15:04:36.343Z","dependency_job_id":null,"html_url":"https://github.com/ahojukka5/gmshparser","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahojukka5%2Fgmshparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahojukka5%2Fgmshparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahojukka5%2Fgmshparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahojukka5%2Fgmshparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahojukka5","download_url":"https://codeload.github.com/ahojukka5/gmshparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244874117,"owners_count":20524574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["element","fem","finite","gmsh","mesh","method","parser","python"],"created_at":"2024-10-12T22:35:26.258Z","updated_at":"2025-03-21T21:30:45.868Z","avatar_url":"https://github.com/ahojukka5.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gmshparser - parse Gmsh .msh file format\n\n[![Python CI - Status][gh-ci-img]][gh-ci-url]\n[![PyPI - Version][pypi-img]][pypi-url]\n[![PyPI - Downloads][pypi-dl-img]][pypi-dl-url]\n[![Coverate Status][coveralls-img]][coveralls-url]\n[![Documentation Status][documentation-img]][documentation-url]\n\nPackage author: Jukka Aho (@ahojukka5)\n\nGmshparser is a small Python package which aims to do only one thing: parse Gmsh\nmesh file format. Package does not have any external dependencies to other\npackages and it aims to be a simple stand-alone solution for a common problem:\nhow to import mesh to your favourite research FEM code?\n\n- Project source in GitHub: [https://github.com/ahojukka5/gmshparser](https://github.com/ahojukka5/gmshparser)\n- Project documentation in ReadTheDocs: [https://gmshparser.readthedocs.io](https://gmshparser.readthedocs.io)\n- Project releases in PyPi: [https://pypi.org/project/gmshparser](https://pypi.org/project/gmshparser)\n\n## Installing package\n\nTo install the most recent package from Python Package Index (PyPi), use git:\n\n```bash\npip install gmshparser\n```\n\nTo install the development version, you can install the package directly from\nthe GitHub:\n\n```bash\npip install git+git://github.com/ahojukka5/gmshparser.git\n```\n\n## Using application programming interface\n\nTo read mesh into `Mesh` object, use command `parse`. It takes a filename and\nparses the file with the set of parsers, defined in `DEFAULT_PARSERS` (see\ndeveloping package section for more info..!)\n\n```python\nimport gmshparser\nmesh = gmshparser.parse(\"data/testmesh.msh\")\nprint(mesh)\n```\n\n```text\nMesh name: data/testmesh.msh\nMesh version: 4.1\nNumber of nodes: 6\nMinimum node tag: 1\nMaximum node tag: 6\nNumber of node entities: 1\nNumber of elements: 2\nMinimum element tag: 1\nMaximum element tag: 2\nNumber of element entities: 1\n```\n\nAfter reading the model, you can querying your data form `mesh` object. For\nexample, to extract all nodes from the model:\n\n```python\nfor entity in mesh.get_node_entities():\n    for node in entity.get_nodes():\n        nid = node.get_tag()\n        ncoords = node.get_coordinates()\n        print(\"Node id = %s, node coordinates = %s\" % (nid, ncoords))\n```\n\n```text\nNode id = 1, node coordinates = (0.0, 0.0, 0.0)\nNode id = 2, node coordinates = (1.0, 0.0, 0.0)\nNode id = 3, node coordinates = (1.0, 1.0, 0.0)\nNode id = 4, node coordinates = (0.0, 1.0, 0.0)\nNode id = 5, node coordinates = (2.0, 0.0, 0.0)\nNode id = 6, node coordinates = (2.0, 1.0, 0.0)\n```\n\nExtract all elements from the model:\n\n```python\nfor entity in mesh.get_element_entities():\n    eltype = entity.get_element_type()\n    print(\"Element type: %s\" % eltype)\n    for element in entity.get_elements():\n        elid = element.get_tag()\n        elcon = element.get_connectivity()\n        print(\"Element id = %s, connectivity = %s\" % (elid, elcon))\n```\n\n```text\nElement type: 3\nElement id = 1, connectivity = [1, 2, 3, 4]\nElement id = 2, connectivity = [2, 5, 6, 3]\n```\n\nIf you are writing your FEM stuff with Python, then you have access to the all\nrelevant properties of the model using `mesh` object. For further information on\nhow to access nodes, elements, physical groups, and other things what Gmsh\nprovides, take a look of [documentation](https://gmshparser.readthedocs.io).\n\n### Using command line interface\n\ngmshparser can also be useful even if you don't make FEM code in Python. The\nabove loops used to extract nodes and elements are actually so common, that you\ncan use them from the command line. This way you can print nodes and elements in\na simpler format with command-line tools, making it easier to read an element\nmesh with C ++ or Fortran, for example. To extract nodes:\n\n```bash\njukka@jukka-XPS-13-9380:~$ gmshparser data/testmesh.msh nodes\n```\n\n```text\n6\n1 0.000000 0.000000 0.000000\n2 1.000000 0.000000 0.000000\n3 1.000000 1.000000 0.000000\n4 0.000000 1.000000 0.000000\n5 2.000000 0.000000 0.000000\n6 2.000000 1.000000 0.000000\n```\n\nTo extract elements, use choice `elements`. The first line is having the total\nnumber of elements, and the rest of the lines are in format `element_id\nelement_type element_connectivity`. The length of the line naturally depends on\nhow many nodes the element is having.\n\n```bash\njukka@jukka-XPS-13-9380:~$ gmshparser data/testmesh.msh elements\n```\n\n```text\n2\n1 3 1 2 3 4\n2 3 2 5 6 3\n```\n\n### Visualizing meshes using gmshparser and matplotlib\n\nThe intention of the package is not to visualize meshes. But as it is a quite\ncommon task to visualize 2-dimensional triangluar meshes in acedemic papers,\nlecture notes, and things like that, it can be done easily using gmshparser and\nmatplotlib. There's a helper function `gmshparser.helpers.get_triangles`, which\nreturns a tuple `(X, Y, T)` which can then be passed to matplotlib to get a mesh\nplot:\n\n```python\nimport gmshparser\nmesh = gmshparser.parse(\"data/example_mesh.msh\")\nX, Y, T = gmshparser.helpers.get_triangles(mesh)\n\nimport matplotlib.pylab as plt\nplt.figure()\nplt.triplot(X, Y, T, color='black')\nplt.axis('equal')\nplt.axis('off')\nplt.tight_layout()\nplt.savefig('docs/example_mesh.svg')\n```\n\n![](docs/example_mesh.svg)\n\n## Developing package\n\ngmshparser is written such a way, that it's easy to define your own parsers\nwhich are responsible for parsing some section, starting with `$SectionName` and\nending with `$EndSectionName`. For example, a parser which is responsible to\nparse `MeshFormat` setion is `MainFormatParser` and it is defined with the\nfollowing code:\n\n```python\nclass MeshFormatParser(AbstractParser):\n\n    @staticmethod\n    def get_section_name():\n        return \"$MeshFormat\"\n\n    @staticmethod\n    def parse(mesh: Mesh, io: TextIO) -\u003e None:\n        s = io.readline().strip().split(\" \")\n        mesh.set_version(float(s[0]))\n        mesh.set_ascii(int(s[1]) == 0)\n        mesh.set_precision(int(s[2]))\n```\n\nAll the active parsers used in parsing are then appended to the list of parsers\nin [MainParser](gmshparser/main_parser.py), from where they are called when an\nappropriate `get_section_name()` is found from file considered to be parsed. The\n`MainParser` itself is then called in `parse` to get things done:\n\n```python\ndef parse(filename: str) -\u003e Mesh:\n    \"\"\"Parse Gmsh .msh file and return `Mesh` object.\"\"\"\n    mesh = Mesh()\n    mesh.set_name(filename)\n    parser = MainParser()\n    with open(filename, \"r\") as io:\n        parser.parse(mesh, io)\n    return mesh\n```\n\nIf you want to learn how to write your own parser, you can e.g. take of look of\n[NodesParser](gmshparser/nodes_parser.py) which is responsible for parsing nodes\nand [ElementsParser](gmshparser/elements_parser.py) which is responsible for\nparsing elements, to get an idea how things are implemented.\n\n## Contributing to project\n\nLike in all other open source projects, contributions are always welcome to this\nproject too! If you have some great ideas how to make this package better,\nfeature requests etc., you can open an issue on gmshparser's [issue\ntracker][issues] or contact me (ahojukka5@gmail.com) directly.\n\n[gh-ci-img]: https://github.com/ahojukka5/gmshparser/workflows/Python%20CI/badge.svg\n[gh-ci-url]: https://github.com/ahojukka5/gmshparser/actions\n[travis-img]: https://travis-ci.com/ahojukka5/gmshparser.svg?branch=master\n[travis-url]: https://travis-ci.com/ahojukka5/gmshparser\n[coveralls-img]: https://coveralls.io/repos/github/ahojukka5/gmshparser/badge.svg?branch=master\n[coveralls-url]: https://coveralls.io/github/ahojukka5/gmshparser?branch=master\n[pypi-img]: https://img.shields.io/pypi/v/gmshparser\n[pypi-url]: https://pypi.org/project/gmshparser\n[pypi-dl-img]: https://img.shields.io/pypi/dm/gmshparser\n[pypi-dl-url]: https://pypi.org/project/gmshparser\n[documentation-img]: https://readthedocs.org/projects/gmshparser/badge/?version=latest\n[documentation-url]: https://gmshparser.readthedocs.io/en/latest/?badge=latest\n[issues]: https://github.com/ahojukka5/gmshparser/issues\n[gmsh]: https://gmsh.info/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahojukka5%2Fgmshparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahojukka5%2Fgmshparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahojukka5%2Fgmshparser/lists"}