Skip to main content

High-quality 2D mesh generator based on distmesh

Project description

dmsh

The worst mesh generator you'll ever use.

PyPi Version PyPI pyversions GitHub stars PyPi downloads

Discord

Inspired by distmesh, dmsh can be slow, requires a lot of memory, and isn't terribly robust either.

On the plus side,

  • it's got a user-friendly interface,
  • is pure Python (and hence easily installable on any system), and
  • it produces pretty high-quality meshes.

Combined with optimesh, dmsh produces the highest-quality 2D meshes in the west.

Examples

Primitives

circle circle circle
import dmsh
import meshio
import optimesh

geo = dmsh.Circle([0.0, 0.0], 1.0)
X, cells = dmsh.generate(geo, 0.1)

# optionally optimize the mesh
X, cells = optimesh.optimize_points_cells(X, cells, "CVT (full)", 1.0e-10, 100)

# visualize the mesh
dmsh.show(X, cells, geo)

# and write it to a file
meshio.Mesh(X, {"triangle": cells}).write("circle.vtk")
import dmsh

geo = dmsh.Rectangle(-1.0, +2.0, -1.0, +1.0)
X, cells = dmsh.generate(geo, 0.1)
import dmsh

geo = dmsh.Polygon(
    [
        [0.0, 0.0],
        [1.1, 0.0],
        [1.2, 0.5],
        [0.7, 0.6],
        [2.0, 1.0],
        [1.0, 2.0],
        [0.5, 1.5],
    ]
)
X, cells = dmsh.generate(geo, 0.1)

Combinations

Difference
import dmsh

geo = dmsh.Circle([-0.5, 0.0], 1.0) - dmsh.Circle([+0.5, 0.0], 1.0)
X, cells = dmsh.generate(geo, 0.1)
import dmsh

geo = dmsh.Circle([0.0, 0.0], 1.0) - dmsh.Polygon([[0.0, 0.0], [1.5, 0.4], [1.5, -0.4]])
X, cells = dmsh.generate(geo, 0.1, tol=1.0e-10)

The following example uses a nonconstant edge length; it depends on the distance to the circle c.

import dmsh
import numpy as np

r = dmsh.Rectangle(-1.0, +1.0, -1.0, +1.0)
c = dmsh.Circle([0.0, 0.0], 0.3)
geo = r - c

X, cells = dmsh.generate(geo, lambda pts: np.abs(c.dist(pts)) / 5 + 0.05, tol=1.0e-10)
Union
import dmsh

geo = dmsh.Circle([-0.5, 0.0], 1.0) + dmsh.Circle([+0.5, 0.0], 1.0)
X, cells = dmsh.generate(geo, 0.15)
import dmsh

geo = dmsh.Rectangle(-1.0, +0.5, -1.0, +0.5) + dmsh.Rectangle(-0.5, +1.0, -0.5, +1.0)
X, cells = dmsh.generate(geo, 0.15)
import dmsh
import numpy as np

angles = np.pi * np.array([3.0 / 6.0, 7.0 / 6.0, 11.0 / 6.0])
geo = dmsh.Union(
    [
        dmsh.Circle([np.cos(angles[0]), np.sin(angles[0])], 1.0),
        dmsh.Circle([np.cos(angles[1]), np.sin(angles[1])], 1.0),
        dmsh.Circle([np.cos(angles[2]), np.sin(angles[2])], 1.0),
    ]
)
X, cells = dmsh.generate(geo, 0.15)

Intersection

import dmsh

geo = dmsh.Circle([0.0, -0.5], 1.0) & dmsh.Circle([0.0, +0.5], 1.0)
X, cells = dmsh.generate(geo, 0.1, tol=1.0e-10)
import dmsh
import numpy as np

angles = np.pi * np.array([3.0 / 6.0, 7.0 / 6.0, 11.0 / 6.0])
geo = dmsh.Intersection(
    [
        dmsh.Circle([np.cos(angles[0]), np.sin(angles[0])], 1.5),
        dmsh.Circle([np.cos(angles[1]), np.sin(angles[1])], 1.5),
        dmsh.Circle([np.cos(angles[2]), np.sin(angles[2])], 1.5),
    ]
)
X, cells = dmsh.generate(geo, 0.1, tol=1.0e-10)

The following uses the HalfSpace primtive for cutting off a circle.

import dmsh

geo = dmsh.HalfSpace([1.0, 1.0]) & dmsh.Circle([0.0, 0.0], 1.0)
X, cells = dmsh.generate(geo, 0.1)

Rotation, translation, scaling

import dmsh
import numpy as np

geo = dmsh.Rotation(dmsh.Rectangle(-1.0, +2.0, -1.0, +1.0), 0.1 * np.pi)
X, cells = dmsh.generate(geo, 0.1, tol=1.0e-10)
import dmsh

geo = dmsh.Rectangle(-1.0, +2.0, -1.0, +1.0) + [1.0, 1.0]
X, cells = dmsh.generate(geo, 0.1)
import dmsh

geo = dmsh.Rectangle(-1.0, +2.0, -1.0, +1.0) * 2.0
X, cells = dmsh.generate(geo, 0.1, tol=1.0e-5)

Local refinement

local-refinement

All objects can be used to refine the mesh according to the distance to the object; e.g. a Path:

import dmsh

geo = dmsh.Rectangle(0.0, 1.0, 0.0, 1.0)

p1 = dmsh.Path([[0.4, 0.6], [0.6, 0.4]])


def target_edge_length(x):
    return 0.03 + 0.1 * p1.dist(x)


X, cells = dmsh.generate(geo, target_edge_length, tol=1.0e-10)

Custom shapes

It is also possible to define your own geometry. Simply create a class derived from dmsh.Geometry that contains a dist method and a method to project points onto the boundary.

import dmsh
import numpy as np


class MyDisk(dmsh.Geometry):
    def __init__(self):
        self.r = 1.0
        self.x0 = [0.0, 0.0]
        bounding_box = [-1.0, 1.0, -1.0, 1.0]
        feature_points = np.array([[], []]).T
        super().__init__(bounding_box, feature_points)

    def dist(self, x):
        assert x.shape[0] == 2
        y = (x.T - self.x0).T
        return np.sqrt(np.einsum("i...,i...->...", y, y)) - self.r

    def boundary_step(self, x):
        # project onto the circle
        y = (x.T - self.x0).T
        r = np.sqrt(np.einsum("ij,ij->j", y, y))
        return ((y / r * self.r).T + self.x0).T


geo = MyDisk()
X, cells = dmsh.generate(geo, 0.1)

Debugging

level-set-poly level-set-rect-hole

dmsh is rather fragile, but sometimes the break-downs are due to an incorrectly defined geometry. Use

geo.show()

to inspect the level set function of your domain. (It must be negative inside the domain and positive outside. The 0-level set forms the domain boundary.)

Installation

dmsh is available from the Python Package Index, so simply type

pip install dmsh

to install.

Testing

To run the dmsh unit tests, check out this repository and type

tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dmsh-0.3.6-cp314-none-any.whl (110.1 kB view details)

Uploaded CPython 3.14

dmsh-0.3.6-cp313-none-any.whl (105.9 kB view details)

Uploaded CPython 3.13

dmsh-0.3.6-cp312-none-any.whl (106.0 kB view details)

Uploaded CPython 3.12

dmsh-0.3.6-cp311-none-any.whl (115.9 kB view details)

Uploaded CPython 3.11

dmsh-0.3.6-cp310-none-any.whl (57.8 kB view details)

Uploaded CPython 3.10

File details

Details for the file dmsh-0.3.6-cp314-none-any.whl.

File metadata

  • Download URL: dmsh-0.3.6-cp314-none-any.whl
  • Upload date:
  • Size: 110.1 kB
  • Tags: CPython 3.14
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dmsh-0.3.6-cp314-none-any.whl
Algorithm Hash digest
SHA256 29c14c576bdf1aa10e5c98858d44795026da8e3add552e33ce09623d33075490
MD5 4d64cd74d6a51ab7d2b07e7dcad0c8ed
BLAKE2b-256 a27d28bc4a858f22adfb895efa2511f40609bf6e4f10cea05def5d556a3f5c3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmsh-0.3.6-cp314-none-any.whl:

Publisher: release.yml on meshpro/dmsh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dmsh-0.3.6-cp313-none-any.whl.

File metadata

  • Download URL: dmsh-0.3.6-cp313-none-any.whl
  • Upload date:
  • Size: 105.9 kB
  • Tags: CPython 3.13
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dmsh-0.3.6-cp313-none-any.whl
Algorithm Hash digest
SHA256 92147f4c6dff51a50621e7d659e662934ca0f0ceef36977bfca8453a36f05882
MD5 7305ed53d9534bc7f931ea27aa989b34
BLAKE2b-256 aed3b35c48a909023c34b463a2e1c07274e96812bf07aab1e9f391cccf656392

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmsh-0.3.6-cp313-none-any.whl:

Publisher: release.yml on meshpro/dmsh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dmsh-0.3.6-cp312-none-any.whl.

File metadata

  • Download URL: dmsh-0.3.6-cp312-none-any.whl
  • Upload date:
  • Size: 106.0 kB
  • Tags: CPython 3.12
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dmsh-0.3.6-cp312-none-any.whl
Algorithm Hash digest
SHA256 a41804d8ffacc987eb21db0ba75b8d7e7e50ddd8e8488f94819bda3b83f9dd69
MD5 fb4a04bd746546b7880fa4c98c7cf2d0
BLAKE2b-256 a33297332fc129a9329d0b5b52ccb735ebd65d74a7bbcc4bc027535aae48dc8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmsh-0.3.6-cp312-none-any.whl:

Publisher: release.yml on meshpro/dmsh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dmsh-0.3.6-cp311-none-any.whl.

File metadata

  • Download URL: dmsh-0.3.6-cp311-none-any.whl
  • Upload date:
  • Size: 115.9 kB
  • Tags: CPython 3.11
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dmsh-0.3.6-cp311-none-any.whl
Algorithm Hash digest
SHA256 6b6dfcabc5d6cdb14b5e77dcb8e33f771977cbc1c900a3aad5fe76d0f29b4874
MD5 7b856f8a377072e5304c9d2e862d7065
BLAKE2b-256 a10e0f3eb7f1192a321d7591f3a11a60190468d1804e4c2434b5852b2dab601b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmsh-0.3.6-cp311-none-any.whl:

Publisher: release.yml on meshpro/dmsh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dmsh-0.3.6-cp310-none-any.whl.

File metadata

  • Download URL: dmsh-0.3.6-cp310-none-any.whl
  • Upload date:
  • Size: 57.8 kB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dmsh-0.3.6-cp310-none-any.whl
Algorithm Hash digest
SHA256 a2ccc32dcab716adb1bb41d4535e94bd1932e1065b82f7da6225d850e30a6d3e
MD5 672f74b813383ee65a6b718cfebf4fe2
BLAKE2b-256 d51191ed04f21c6d65e0219af16b67656ec019629f992342a5bc537bfe50b6e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmsh-0.3.6-cp310-none-any.whl:

Publisher: release.yml on meshpro/dmsh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page