All Projects → nschloe → Optimesh

nschloe / Optimesh

Licence: gpl-3.0
Mesh optimization, mesh smoothing.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Optimesh

Meshio
input/output for many mesh formats
Stars: ✭ 814 (+211.88%)
Mutual labels:  mesh, engineering, mathematics, pypi
Orthopy
Orthogonal polynomials in all shapes and sizes.
Stars: ✭ 75 (-71.26%)
Mutual labels:  engineering, mathematics, physics
Pygmsh
Gmsh for Python
Stars: ✭ 418 (+60.15%)
Mutual labels:  engineering, mathematics, pypi
Pygalmesh
A Python frontend to CGAL's mesh generators.
Stars: ✭ 245 (-6.13%)
Mutual labels:  mesh, engineering, mathematics
Awesome Scientific Computing
😎 Curated list of awesome software for numerical analysis and scientific computing
Stars: ✭ 476 (+82.38%)
Mutual labels:  engineering, mathematics, physics
Accupy
Accurate sums and dot products for Python.
Stars: ✭ 65 (-75.1%)
Mutual labels:  engineering, mathematics, pypi
meshgen-comparison
🕸️ A comparison of mesh generators.
Stars: ✭ 25 (-90.42%)
Mutual labels:  engineering, mathematics, fem
cplot
Function plotter
Stars: ✭ 27 (-89.66%)
Mutual labels:  physics, mathematics
spicyphysics
Sup bois, here's my work
Stars: ✭ 22 (-91.57%)
Mutual labels:  physics, mathematics
fundamental
Software to look for interrelationships between constants and find formulas for number sequences
Stars: ✭ 14 (-94.64%)
Mutual labels:  physics, mathematics
My NoteBook
サイエンス、テクノロジー、エンジニアリング関連の情報を記載したノート(忘備録)です。
Stars: ✭ 104 (-60.15%)
Mutual labels:  physics, mathematics
Freecad
This is the official source code of FreeCAD, a free and opensource multiplatform 3D parametric modeler. Issues are managed on our own bug tracker at https://www.freecadweb.org/tracker
Stars: ✭ 10,366 (+3871.65%)
Mutual labels:  engineering, fem
pymae
Materials for the book "Python for Mechanical and Aerospace Engineering"
Stars: ✭ 56 (-78.54%)
Mutual labels:  engineering, physics
maelstrom
Numerical simulation of magnetohydrodynamics.
Stars: ✭ 28 (-89.27%)
Mutual labels:  physics, mathematics
Dmsh
Simple mesh generator inspired by distmesh.
Stars: ✭ 113 (-56.7%)
Mutual labels:  mesh, mathematics
fem mesh matlab
MATLAB Toolbox for Handling 2D and 3D FEM Meshes
Stars: ✭ 23 (-91.19%)
Mutual labels:  mesh, fem
creative-coding-notebooks
🎨 An authorial collection of fundamental recipes on Creative Coding and Recreational Programming.
Stars: ✭ 17 (-93.49%)
Mutual labels:  physics, mathematics
featool-multiphysics
FEATool - "Physics Simulation Made Easy" (Fully Integrated FEA, FEniCS, OpenFOAM, SU2 Solver GUI & Multi-Physics Simulation Platform)
Stars: ✭ 190 (-27.2%)
Mutual labels:  engineering, fem
KalmanFlow
A simple Kalman Filter built in TensorFlow
Stars: ✭ 22 (-91.57%)
Mutual labels:  engineering, physics
FastExpm.jl
Implementation of a fast exponential matrix for large matrices (full and sparse)
Stars: ✭ 22 (-91.57%)
Mutual labels:  physics, mathematics

optimesh

Triangular mesh optimization.

PyPi Version PyPI pyversions GitHub stars PyPi downloads

gh-actions codecov LGTM Code style: black

Several mesh smoothing/optimization methods with one simple interface. optimesh

  • is fast,
  • preserves submeshes,
  • only works for triangular meshes, flat and on a surface, (for now; upvote this issue if you're interested in tetrahedral mesh smoothing), and
  • supports all mesh formats that meshio can handle.

Install with

pip install optimesh

Example call:

optimesh in.e out.vtk

Output: terminal-screenshot

The left hand-side graph shows the distribution of angles (the grid line is at the optimal 60 degrees). The right hand-side graph shows the distribution of simplex quality, where quality is twice the ratio of circumcircle and incircle radius.

All command-line options are documented at

optimesh -h

Showcase

disk-step0

The following examples show the various algorithms at work, all starting from the same randomly generated disk mesh above. The cell coloring indicates quality; dark green is bad, yellow is good.

CVT (centroidal Voronoi tesselation)

cvt-uniform-lloyd2 cvt-uniform-qnb cvt-uniform-qnf
uniform-density relaxed Lloyd's algorithm (--method lloyd --omega 2.0) uniform-density quasi-Newton iteration (block-diagonal Hessian, --method cvt-block-diagonal) uniform-density quasi-Newton iteration (default method, full Hessian, --method cvt-full)

Centroidal Voronoi tessellation smoothing (Du et al.) is one of the oldest and most reliable approaches. optimesh provides classical Lloyd smoothing as well as several variants that result in better meshes.

CPT (centroidal patch tesselation)

cpt-cp cpt-uniform-fp cpt-uniform-qn
density-preserving linear solve (Laplacian smoothing, --method cpt-linear-solve) uniform-density fixed-point iteration (--method cpt-fixed-point) uniform-density quasi-Newton (--method cpt-quasi-newton)

A smoothing method suggested by Chen and Holst, mimicking CVT but much more easily implemented. The density-preserving variant leads to the exact same equation system as Laplacian smoothing, so CPT smoothing can be thought of as a generalization.

The uniform-density variants are implemented classically as a fixed-point iteration and as a quasi-Newton method. The latter typically converges faster.

ODT (optimal Delaunay tesselation)

odt-dp-fp odt-uniform-fp odt-uniform-bfgs
density-preserving fixed-point iteration (--method odt-dp-fp) uniform-density fixed-point iteration (--method odt-fixed-point) uniform-density BFGS (--method odt-bfgs)

Optimal Delaunay Triangulation (ODT) as suggested by Chen and Holst. Typically superior to CPT, but also more expensive to compute.

Implemented once classically as a fixed-point iteration, once as a nonlinear optimization method. The latter typically leads to better results.

Using optimesh from Python

You can also use optimesh in a Python program. Try

import optimesh

# [...] create points, cells [...]

points, cells = optimesh.optimize_points_cells(
    points, cells, "CVT (block-diagonal)", 1.0e-5, 100
)

# or create a meshplex Mesh
import meshplex

mesh = meshplex.MeshTri(points, cells)
optimesh.optimize(mesh, "CVT (block-diagonal)", 1.0e-5, 100)
# mesh.points, mesh.cells, ...

If you only want to do one optimization step, do

points = optimesh.get_new_points(mesh, "CVT (block-diagonal)")

Surface mesh smoothing

optimesh also supports optimization of triangular meshes on surfaces which are defined implicitly by a level set function (e.g., spheres). You'll need to specify the function and its gradient, so you'll have to do it in Python:

import meshzoo
import optimesh

points, cells = meshzoo.tetra_sphere(20)


class Sphere:
    def f(self, x):
        return 1.0 - (x[0] ** 2 + x[1] ** 2 + x[2] ** 2)

    def grad(self, x):
        return -2 * x


# You can use all methods in optimesh:
# points, cells = optimesh.cpt.fixed_point_uniform(
# points, cells = optimesh.odt.fixed_point_uniform(
points, cells = optimesh.optimize_points_cells(
    points,
    cells,
    "CVT (full)",
    1.0e-2,
    100,
    verbose=False,
    implicit_surface=Sphere(),
    # step_filename_format="out{:03d}.vtk"
)

This code first generates a mediocre mesh on a sphere using meshzoo,

and then optimizes. Some results:

odt-dp-fp odt-uniform-fp odt-uniform-bfgs
CPT ODT CVT (full Hessian)

Which method is best?

From practical experiments, it seems that the CVT smoothing variants, e.g.,

optimesh in.vtk out.vtk -m cvt-uniform-qnf

give very satisfactory results. (This is also the default method, so you don't need to specify it explicitly.) Here is a comparison of all uniform-density methods applied to the random circle mesh seen above:

(Mesh quality is twice the ratio of incircle and circumcircle radius, with the maximum being 1.)

Why optimize?

Gmsh mesh Gmsh mesh after optimesh dmsh mesh

Let us compare the properties of the Poisson problem (Δu = f with Dirichlet boundary conditions) when solved on different meshes of the unit circle. The first mesh is the on generated by Gmsh, the second the same mesh but optimized with optimesh, the third a very high-quality dmsh mesh.

We consider meshings of the circle with an increasing number of points:

gmsh-quality gmsh-cond gmsh-cg
average cell quality condition number of the Poisson matrix number of CG steps for Poisson problem

Quite clearly, the dmsh generator produces the highest-quality meshes (left). The condition number of the corresponding Poisson matrices is lowest for the high quality meshes (middle); one would hence suspect faster convergence with Krylov methods. Indeed, most CG iterations are necessary on the Gmsh mesh (right). After optimesh, one saves between 10 and 20 percent of iterations/computing time. The dmsh mesh cuts the number of iterations in half.

Access from Python

All optimesh functions can also be accessed from Python directly, for example:

import optimesh

X, cells = optimesh.odt.fixed_point_uniform(X, cells, 1.0e-2, 100, verbose=False)

Installation

optimesh is available from the Python Package Index, so simply do

pip install optimesh

to install.

Relevant publications

Testing

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

pytest

License

This software is published under the GPLv3 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].