Compute gradients on mesh and unstructured data objects.
This repository aims to fill a gap: no native Python code was available to compute a particular field gradient on a mesh. Some implementations may exist for a structured grid, but this is the first time gradient can be calculated on unstructured mesh (without external libraries such as Paraview or Pyvista).
While Pyvista does provide this functionality, it gets impossible to make it work with other libraries, such as Machine Learning framework. Therefore, we built this library using Tensorflow (which can be replaced with other Deep Learning framework), allowing one to compute gradient on a mesh while performing gradient descent.
We used numpy and tensorflow for the computation part and meshio to read/write mesh.
We also developed a multiprocessing version of our functions based on the library Ray. This gives full support to tailored computing power even on large clusters.
We can read a mesh using meshio:
mesh_pv = meshio.read('example.vtu',"vtu")
and compute an AGS matrix with the following:
from meshgradient.matrix import build_AGS_matrix
matrix = build_AGS_matrix(mesh)
More informations can be found in this notebook.
Each matrix can also be computed in a multi processed fashion using the '_multiprocessing' function, such as build_AGS_matrix_multiprocess
.
We use three different methods to compute the gradient of a field:
- PCE (Per-Cell Linear Estimation)
- AGS (Average Gradient on Star)
- CON (Connectivity, for gradients on boundaries)
You can use any of these three methods or use our built-in functions to compute gradients (that makes the best of these three methods at the same time)
We based our implementation on the one provided by the authors of the paper Gradient Field Estimation on Triangle Meshes. We describe below the main ideas of the methods, more details can be found in the paper.
These three methods were built for triangle cells. Any other sort of cells won't be considered.
This method estimates a constant gradient inside each cell. First, we define a linear interpolation at any point in a cell of a function with:
where are the vertices of the cell and the barycentric coordinates of wrt. the vertices.
With this estimation, for a triangle with 3 vertices, we have:
where is the area of the triangle.
Given a node, we can use the PCE method to compute a gradient in each cell of the start of the node , thus having: