All Projects → nschloe → Dmsh

nschloe / Dmsh

Licence: gpl-3.0
Simple mesh generator inspired by distmesh.

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects

Projects that are alternatives of or similar to Dmsh

Optimesh
Mesh optimization, mesh smoothing.
Stars: ✭ 261 (+130.97%)
Mutual labels:  mesh, mathematics
Pygalmesh
A Python frontend to CGAL's mesh generators.
Stars: ✭ 245 (+116.81%)
Mutual labels:  mesh, mathematics
Vedo
A python module for scientific analysis of 3D objects based on VTK and numpy
Stars: ✭ 741 (+555.75%)
Mutual labels:  mesh, finite-elements
continuum mechanics
Utilities for doing calculations in continuum mechanics.
Stars: ✭ 18 (-84.07%)
Mutual labels:  mathematics, finite-elements
Meshio
input/output for many mesh formats
Stars: ✭ 814 (+620.35%)
Mutual labels:  mesh, mathematics
Computer Science Resources
A list of resources in different fields of Computer Science (multiple languages)
Stars: ✭ 1,316 (+1064.6%)
Mutual labels:  mathematics
Gl Catmull Clark
A javascript implementation of the Catmull-Clark subdivision surface algorithm
Stars: ✭ 100 (-11.5%)
Mutual labels:  mesh
Trimesh
Python library for loading and using triangular meshes.
Stars: ✭ 1,303 (+1053.1%)
Mutual labels:  mesh
Plexus
Polygonal mesh processing.
Stars: ✭ 90 (-20.35%)
Mutual labels:  mesh
Studybook
Study E-Book(ComputerVision DeepLearning MachineLearning Math NLP Python ReinforcementLearning)
Stars: ✭ 1,457 (+1189.38%)
Mutual labels:  mathematics
Polylidar
Polylidar3D - Fast polygon extraction from 3D Data
Stars: ✭ 106 (-6.19%)
Mutual labels:  mesh
Algorithms
A collection of algorithms and data structures
Stars: ✭ 11,553 (+10123.89%)
Mutual labels:  mathematics
Funmath
☔️ Implementations of mathematical functions, formulas and concepts
Stars: ✭ 93 (-17.7%)
Mutual labels:  mathematics
Project Euler Solutions
Runnable code for solving Project Euler problems in Java, Python, Mathematica, Haskell.
Stars: ✭ 1,374 (+1115.93%)
Mutual labels:  mathematics
Octsympy
A Symbolic Package for Octave using SymPy
Stars: ✭ 92 (-18.58%)
Mutual labels:  mathematics
Sage
Mirror of the Sage source tree -- please do not submit PRs here -- everything must be submitted via https://trac.sagemath.org/
Stars: ✭ 1,656 (+1365.49%)
Mutual labels:  mathematics
Coordinateaxischart
Drawing graphs of point, linear function, power function, exponential function, logarithmic function, circular function, etc in a coordinate. (实现了在坐标系中画点,一次函数,幂函数,指数函数,对数函数,三角函数等)
Stars: ✭ 90 (-20.35%)
Mutual labels:  mathematics
D3graphtheory
💥 Interactive and colorful 🎨 graph theory tutorials made using d3.js ⚡️
Stars: ✭ 1,364 (+1107.08%)
Mutual labels:  mathematics
Math Expression Evaluator
Math JS library. Super advanced & efficient Math expression evaluator
Stars: ✭ 102 (-9.73%)
Mutual labels:  mathematics
Wifimeshraspberrypi
Workshop to create a sensor application over a WiFi Mesh network
Stars: ✭ 99 (-12.39%)
Mutual labels:  mesh

dmsh

The worst mesh generator you'll ever use.

PyPi Version Packaging status PyPI pyversions GitHub stars PyPi downloads

Discord

gh-actions codecov LGTM Code style: black

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.helpers.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.Difference(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.Difference(
    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 = dmsh.Difference(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.Union([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.Union(
    [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.Intersection([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
import numpy as np

geo = dmsh.Intersection(
    [
        dmsh.HalfSpace(np.sqrt(0.5) * np.array([1.0, 1.0]), 0.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.Translation(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.Scaling(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 edge_size(x):
    return 0.03 + 0.1 * p1.dist(x)


X, cells = dmsh.generate(geo, edge_size, 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):
        super().__init__()
        self.r = 1.0
        self.x0 = [0.0, 0.0]
        self.bounding_box = [-1.0, 1.0, -1.0, 1.0]
        self.feature_points = np.array([[], []]).T

    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

MPLBACKEND=Agg pytest

(Setting the environment variable prevents the test figures from being displayed.)

License

This software is published under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].