- Notifications
You must be signed in to change notification settings - Fork161
nschloe/pygmsh
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Gmsh for Python.
pygmsh combines the power ofGmsh with the versatility of Python.It provides useful abstractions from Gmsh's own Python interface so you can createcomplex geometries more easily.
To use, install Gmsh itself and pygmsh frompypi:
[sudo] apt install python3-gmshpip install pygmsh
This document and thetests/
directory contain many small examples. Seehere for the full documentation.
Polygon | Circle | (B-)Splines |
Codes:
importpygmshwithpygmsh.geo.Geometry()asgeom:geom.add_polygon( [ [0.0,0.0], [1.0,-0.2], [1.1,1.2], [0.1,0.7], ],mesh_size=0.1, )mesh=geom.generate_mesh()# mesh.points, mesh.cells, ...# mesh.write("out.vtk")
importpygmshwithpygmsh.geo.Geometry()asgeom:geom.add_circle([0.0,0.0],1.0,mesh_size=0.2)mesh=geom.generate_mesh()
importpygmshwithpygmsh.geo.Geometry()asgeom:lcar=0.1p1=geom.add_point([0.0,0.0],lcar)p2=geom.add_point([1.0,0.0],lcar)p3=geom.add_point([1.0,0.5],lcar)p4=geom.add_point([1.0,1.0],lcar)s1=geom.add_bspline([p1,p2,p3,p4])p2=geom.add_point([0.0,1.0],lcar)p3=geom.add_point([0.5,1.0],lcar)s2=geom.add_spline([p4,p3,p2,p1])ll=geom.add_curve_loop([s1,s2])pl=geom.add_plane_surface(ll)mesh=geom.generate_mesh()
The return value is always ameshio mesh, so tostore it to a file you can
mesh.write("test.vtk")
The output file can be visualized with various tools, e.g.,ParaView.
With
pygmsh.write("test.msh")
you can access Gmsh's native file writer.
extrude | revolve | twist |
importpygmshwithpygmsh.geo.Geometry()asgeom:poly=geom.add_polygon( [ [0.0,0.0], [1.0,-0.2], [1.1,1.2], [0.1,0.7], ],mesh_size=0.1, )geom.extrude(poly, [0.0,0.3,1.0],num_layers=5)mesh=geom.generate_mesh()
frommathimportpiimportpygmshwithpygmsh.geo.Geometry()asgeom:poly=geom.add_polygon( [ [0.0,0.2,0.0], [0.0,1.2,0.0], [0.0,1.2,1.0], ],mesh_size=0.1, )geom.revolve(poly, [0.0,0.0,1.0], [0.0,0.0,0.0],0.8*pi)mesh=geom.generate_mesh()
frommathimportpiimportpygmshwithpygmsh.geo.Geometry()asgeom:poly=geom.add_polygon( [ [+0.0,+0.5], [-0.1,+0.1], [-0.5,+0.0], [-0.1,-0.1], [+0.0,-0.5], [+0.1,-0.1], [+0.5,+0.0], [+0.1,+0.1], ],mesh_size=0.05, )geom.twist(poly,translation_axis=[0,0,1],rotation_axis=[0,0,1],point_on_axis=[0,0,0],angle=pi/3, )mesh=geom.generate_mesh()
Gmsh also supports OpenCASCADE (occ
), allowing for a CAD-style geometry specification.
frommathimportpi,cosimportpygmshwithpygmsh.occ.Geometry()asgeom:geom.characteristic_length_max=0.1r=0.5disks= [geom.add_disk([-0.5*cos(7/6*pi),-0.25],1.0),geom.add_disk([+0.5*cos(7/6*pi),-0.25],1.0),geom.add_disk([0.0,0.5],1.0), ]geom.boolean_intersection(disks)mesh=geom.generate_mesh()
# ellpsoid with holesimportpygmshwithpygmsh.occ.Geometry()asgeom:geom.characteristic_length_max=0.1ellipsoid=geom.add_ellipsoid([0.0,0.0,0.0], [1.0,0.7,0.5])cylinders= [geom.add_cylinder([-1.0,0.0,0.0], [2.0,0.0,0.0],0.3),geom.add_cylinder([0.0,-1.0,0.0], [0.0,2.0,0.0],0.3),geom.add_cylinder([0.0,0.0,-1.0], [0.0,0.0,2.0],0.3), ]geom.boolean_difference(ellipsoid,geom.boolean_union(cylinders))mesh=geom.generate_mesh()
# puzzle pieceimportpygmshwithpygmsh.occ.Geometry()asgeom:geom.characteristic_length_min=0.1geom.characteristic_length_max=0.1rectangle=geom.add_rectangle([-1.0,-1.0,0.0],2.0,2.0)disk1=geom.add_disk([-1.2,0.0,0.0],0.5)disk2=geom.add_disk([+1.2,0.0,0.0],0.5)disk3=geom.add_disk([0.0,-0.9,0.0],0.5)disk4=geom.add_disk([0.0,+0.9,0.0],0.5)flat=geom.boolean_difference(geom.boolean_union([rectangle,disk1,disk2]),geom.boolean_union([disk3,disk4]), )geom.extrude(flat, [0,0,0.3])mesh=geom.generate_mesh()
# boundary refinementimportpygmshwithpygmsh.geo.Geometry()asgeom:poly=geom.add_polygon( [ [0.0,0.0], [2.0,0.0], [3.0,1.0], [1.0,2.0], [0.0,1.0], ],mesh_size=0.3, )field0=geom.add_boundary_layer(edges_list=[poly.curves[0]],lcmin=0.05,lcmax=0.2,distmin=0.0,distmax=0.2, )field1=geom.add_boundary_layer(nodes_list=[poly.points[2]],lcmin=0.05,lcmax=0.2,distmin=0.1,distmax=0.4, )geom.set_background_mesh([field0,field1],operator="Min")mesh=geom.generate_mesh()
# mesh refinement with callbackimportpygmshwithpygmsh.geo.Geometry()asgeom:geom.add_polygon( [ [-1.0,-1.0], [+1.0,-1.0], [+1.0,+1.0], [-1.0,+1.0], ] )geom.set_mesh_size_callback(lambdadim,tag,x,y,z:6.0e-2+2.0e-1* (x**2+y**2) )mesh=geom.generate_mesh()
# ball with mesh refinementfrommathimportsqrtimportpygmshwithpygmsh.occ.Geometry()asgeom:geom.add_ball([0.0,0.0,0.0],1.0)geom.set_mesh_size_callback(lambdadim,tag,x,y,z:abs(sqrt(x**2+y**2+z**2)-0.5)+0.1 )mesh=geom.generate_mesh()
pygmsh can optimize existing meshes, too.
importmeshiomesh=meshio.read("mymesh.vtk")optimized_mesh=pygmsh.optimize(mesh,method="")
You can also use the command-line utility
pygmsh-optimize input.vtk output.xdmf
where input and output can be any format supported bymeshio.
To run the pygmsh unit tests, check out this repository and type
pytest
Docs are built usingSphinx.
To build, run
sphinx-build -b html doc doc/_build
This software is published under theGPLv3 license.
About
🕸️ Gmsh for Python