triangulax
Overview
This Python package provides data-structures for triangular meshes and a
geometry processing toolkit based on
JAX, fully compatible with
automatic differentiation and just-in-time compilation. Additionally,
triangulax provides tools for simulating membranes, thin shells, and
tissue sheets.
Design
triangulax is designed for modularity and flexibility. The library
includes a suite of geometry processing tools based on discrete
differential geometry
(Voronoi duals, curvatures, Laplace operator, …) and represents surfaces
as half-edge
meshes.
Automatic differentiation and just-in-time compilation with JAX
The main feature of triangulax is (forward- and reverse-mode)
automatic differentiation to compute of derivatives of any mesh-based
function. triangulaxuses
JAX, a “Python library for
accelerator-oriented array computation and program transformation,
designed for high-performance numerical computing and large-scale
machine learning.” Most triangulax functions are compatible with JAX’s
just-in-time (JIT) compilation. JIT provides high performance in Python
(rather than C++) and allows running on GPUs.
To simulate dynamics or minimize energies, triangulax integrates with
JAX ecosystem libraries like
optimistix (optimization) and
diffrax (ODE integration),
creating end-to-end differentiable simulations.
Prerequisites: triangulax assumes familiarity with triangular
meshes (tutorial
0),
and basic JAX usage (tutorial
1)
Use cases
Triangular meshes are ubiquitous in computer graphics and in scientific computing. Examples in soft-matter and biophysics include:
- Cell-resolved models of two-dimensional tissue sheets like the self-propelled Voronoi model (tutorial 3).
- Reaction-diffusion systems on 3D curved surfaces (tutorial 4)
- Mechanics of membranes and thin elastic shells in 3D (tutorial 5)
These tasks revolve around a mesh-based “energy”. JAX automatically computes their gradients, making it easy to optimize energies or to simulate forces. For “multiphysics” simulations (e.g. reaction-diffusion systems on deforming surfaces), it suffices to specify the combined energy of the system - all forces and cross-terms are calculated automatically.
Inverse problems
Since triangulax is fully JAX-compatible, one can differentiate a
simulation with respect to its parameters. This means one can apply
gradient-based optimization to inverse problems (tutorial
2).
Effectively, a simulation becomes a “neural network” which maps initial
conditions to simulation results. The parameters of the simulation can
be fitted to data, or optimized to find a mechanism that generates a
desired shape.
Documentation
Documentation can be found hosted on GitHub
pages. Jupyter
notebooks tutorials can be found in the nbs/tutorials/ folder.
Installation instructions
The triangulax package is hosted on
PyPI. Install it as follows:
- (Recommended) Initialize a virtual environment, for instance with
conda:
$ conda env create -n triangulax
$ conda activate triangulax
See the JAX documentation for how to install JAX with GPU support.
- Install with
pip:
$ pip install triangulax
(Optional) Install optional dependencies for running the tutorials:
$ pip install triangulax[tutorials]
- Verify installation:
conda run -n triangulax python -c "from triangulax import mesh, geometry"
- (Optional) Download jupyter notebooks to run tutorials interactively from GitHub
Usage
triangulax comprises the following modules (see full
documentation for
details):
trigonometry: trigonometry and linear algebra utilitiestriangular: input/output for triangular meshesmesh: half-edge data structure for triangular meshes, compatible with JAXtopology: topological modifications (edge flip, collapse, and split)adjacency: vertex-vertex, vertex-face, and face-face adjacency operatorsgeometry: angles, edge lengths, triangle areas, Voronoi areas, curvaturesperiodic: geometry computations under periodic boundary conditionslinops: discrete differential operators (Laplacian, mass matrix, gradient)interp: linear interpolation and closest-point querieselastic: discrete elastic energies for shells and membranesalgorithms: Delaunay flipping, mesh quality improvementsimulation: Utilities for time-dependent simulations and checkpointing
Minimal example
import jax
import jax.numpy as jnp
from triangulax import triangular, mesh, geometry
# load example mesh and convert to half-edge mesh
vertices, faces = triangular.read_obj("test_meshes/disk.obj", dim=2)
hemesh = mesh.HeMesh.from_triangles(vertices.shape[0], faces)
# with the half-edge mesh, you can carry out various operations, for example
# compute the coordination number by summing incoming half-edges per vertex
coord_number = jnp.zeros(hemesh.n_vertices)
coord_number = coord_number.at[hemesh.dest].add(jnp.ones(hemesh.n_hes))
print("Mean coordination number:", coord_number.mean())
# Let's define a simple geometric function and compute its gradient with JAX
def mean_voronoi_area(vertices: jax.Array, hemesh: mesh.HeMesh) -> jax.Array:
"""Compute the mean Voronoi area per vertex."""
voronoi_areas = geometry.get_voronoi_areas(vertices, hemesh)
return jnp.mean(voronoi_areas)
value, gradient = jax.value_and_grad(mean_voronoi_area)(vertices, hemesh)
print("Mean gradient norm:", jnp.linalg.norm(gradient, axis=1).mean())
Warning: readOBJ() ignored non-comment line 3:
o flat_tri_ecmc
Mean coordination number: 5.40458
Mean gradient norm: 0.0003638338
See also
-
libigl Geometry processing library with Python bindings. You can use
libiglfunctions ontriangulaxmeshes via the.facesattribute -
VertAX JAX-based simulations of 2D tissues.
Developer guide
This package is developed based on Jupyter notebooks, which are
converted into python modules using nbdev. Details are in
.github/copilot-instructions.md.
Install triangulax in Development mode
- Clone the GitHub repository
$ git clone https://github.com/nikolas-claussen/triangulax.git
- Create a conda environment with all Python dependencies
$ conda env create -n triangulax -f environment.yml
$ conda activate triangulax
- Install the
triangulaxpackage
# make sure triangulax package is installed in development mode
$ pip install -e .
- Install
nbdevpackage
pip install nbdev
- Optional: edit the package notebooks and export the code into Python modules
# make changes under nbs/ directory
# ...
# export to have changes apply to triangulax
$ nbdev_export
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file triangulax-0.1.0.tar.gz.
File metadata
- Download URL: triangulax-0.1.0.tar.gz
- Upload date:
- Size: 71.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c32d50f312b8fb9051f9d45132721cbb6e51c47154dd947767fdd61d23ccdb30
|
|
| MD5 |
c80d15d0019e05cc37eeb4f173569aa1
|
|
| BLAKE2b-256 |
5e50be4cab449d7734c327571dc7e0eb83d60e1db917609898cc2884bfdf5310
|
File details
Details for the file triangulax-0.1.0-py3-none-any.whl.
File metadata
- Download URL: triangulax-0.1.0-py3-none-any.whl
- Upload date:
- Size: 74.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bff3d62d750cc78ecfe30d90a8dd79b25c010cf8588f3f6977ec33ae4357c68
|
|
| MD5 |
c5d4fb1abe21a4b672051d3c36a01c4f
|
|
| BLAKE2b-256 |
5f226b60fce3a34b31f269c52251353a2f9d75c757de13c22b773265927e192d
|