Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
Tool for Mesh Agglomeration

Author: Nicola De March nicol.nosp@m.a.de.nosp@m.march.nosp@m.@pol.nosp@m.imi.i.nosp@m.t

This tutorial documents the example examples/mesh/agglomeration/agglomerate_mesh.cpp, a command-line tool that reads a fine 2D or 3D mesh, groups its cells into agglomerates (polygons in 2D, polyhedra in 3D) and writes the agglomerated mesh. Every option is read from a JSON file; a template is provided in examples/mesh/agglomeration/agglomeration_parameters.json.

The agglomeration strategies implemented in Vulpes follow the approach of the MAGNET library; see References.

Quick Start

The executable is built together with the other examples (target agglomerate_mesh). It runs on one MPI rank only:

./agglomerate_mesh <settings.json>

If no argument is given, it looks for agglomeration.json.

A minimal 3D settings file:

{
"dim": 3,
"input_mesh": "meshes/cube_tet.vtu",
"output_mesh": "output/cube_parmetis_512el.vtu",
"n_cells": 512,
"backend": "parmetis",
"method": "direct_kway"
}

The program prints a summary of the settings, the size of the input mesh, the number of agglomerates and the time spent in each phase, then writes the agglomerated mesh to output_mesh (the parent directory is created if needed).

The JSON file must be plain JSON: comments (//) are not allowed. The commented block at the top of agglomerate_mesh.cpp is only documentation.

Overview of the Pipeline

  1. Read the fine mesh (cell tags are imported from the VTK cell data).
  2. Input check (3D only): every fine cell must be a closed manifold polyhedron.
  3. Agglomerate: compute the fine cell -> agglomerate labels with the selected backend and method, optionally region by region (segregated_tag, regions).
  4. Repair (3D only, optional): fix agglomerates with non-manifold edges.
  5. Build the agglomerated mesh with Mesh::agglomerate.
  6. Validate (3D only): the mesh is written only if no agglomerate has a non-manifold edge or a face of zero area.
  7. Write the agglomerated mesh (.vtu/.vtk).

JSON Reference

Key Type Default Used by Description
dim int 3 all Mesh dimension: 2 or 3.
input_mesh string — (required) all Fine mesh. 2D: .vtu/.vtk with polygons. 3D: .vtu with tetrahedra or polyhedra.
output_mesh string — (required) all Path of the agglomerated mesh.
backend string "parmetis" all parmetis, kmeans or gnn.
method string "direct_kway" all direct_kway, bisect or mult_factor.
n_cells int 0 direct_kway, bisect Target number of agglomerates (see each method).
target_diameter float — mult_factor Maximum agglomerate diameter, in mesh units.
gnn_model string "base" gnn Pretrained model: base or hetero.
segregated_tag string "" all Name of a cell tag; each tagged region is agglomerated on its own.
regions object — needs segregated_tag Per-region override of n_cells and/or target_diameter.
repair bool true 3D Remove non-manifold agglomerates. Ignored (with a warning) in 2D.
repair_max_iterations int 100 3D, repair Maximum number of repair iterations.
repair_stall_iterations int 3 3D, repair After how many iterations a persistently non-manifold edge is resolved with new agglomerates.

Keys that do not apply to the chosen backend/method are ignored, so the same file can keep all of them (as the template does) and you only switch backend and method.

Methods

The method decides how the fine cells are split; the backend decides which algorithm computes each split.

direct_kway

A single k-way partition of the whole mesh (or of each region) into n_cells groups.

  • Parameter: n_cells >= 1.
  • Backends: parmetis, kmeans. Not available with gnn.

bisect

Recursive bisection: at each level every group is split in two. The number of levels is ceil(log2(n_cells)), so the result has up to 2^ceil(log2(n_cells)) agglomerates: n_cells = 1000 gives 10 levels, i.e. up to 1024 agglomerates.

  • Parameter: n_cells >= 2.
  • Backends: parmetis, kmeans, gnn.

mult_factor

Recursive bisection driven by size: every group whose (vertex-vertex) diameter is larger than target_diameter is bisected again, until all groups are small enough. The number of agglomerates follows from the geometry; n_cells is ignored.

  • Parameter: target_diameter (required, in the same units as the mesh coordinates).
  • Backends: parmetis, kmeans, gnn.

Groups that are not connected are split by the library, so the final number of agglomerates can differ slightly from n_cells. The repair (3D) can also add a few agglomerates.

Backends

parmetis

Graph partitioning of the dual graph of the mesh (cells are nodes, shared facets are edges) with METIS/ParMETIS. It uses only the connectivity, not the geometry, so it is the fastest backend and the only one that accepts 3D input meshes with non-manifold fine cells. ParMETIS is not deterministic: two runs can give slightly different agglomerations.

Method Required keys
direct_kway n_cells
bisect n_cells
mult_factor target_diameter
{
"dim": 3,
"input_mesh": "meshes/Brains/Brain_tet_1M.vtu",
"output_mesh": "meshes/Brains/Brain_parmetis_100kel.vtu",
"backend": "parmetis",
"method": "direct_kway",
"n_cells": 100000
}

kmeans

Clusters the fine cells by the position of their barycenters (k-means, with cell measures). It produces compact, roughly isotropic agglomerates; the connectivity is enforced afterwards. With bisect/mult_factor each split is a 2-means.

Method Required keys
direct_kway n_cells
bisect n_cells
mult_factor target_diameter
{
"dim": 2,
"input_mesh": "meshes/square_tria.vtu",
"output_mesh": "output/square_kmeans_h0.1.vtu",
"backend": "kmeans",
"method": "mult_factor",
"target_diameter": 0.1
}
Warning
In 3D the kmeans backend needs closed manifold fine cells (barycenters and measures are computed with geometry::Cell3D). If the input check finds non-manifold cells the program stops: fix the mesh or use parmetis.

gnn

Each bisection is computed by a pretrained Graph Neural Network (GraphSAGE, loaded with TorchScript from vulpes/geometry/gnn_models/), as proposed in MAGNET. The network bisects the dual graph using geometric features of the cells, aiming at well-shaped agglomerates with a quality comparable to METIS.

Key Values
gnn_model base: homogeneous features (SAGEbase2D.pt / SAGEbase3D.pt); hetero: features that also account for material/region heterogeneity (SAGEhetero2D.pt / SAGEhetero3D.pt).
Method Required keys
direct_kway not supported
bisect n_cells, gnn_model
mult_factor target_diameter, gnn_model
{
"dim": 3,
"input_mesh": "meshes/cube_tet.vtu",
"output_mesh": "output/cube_gnn_1024el.vtu",
"backend": "gnn",
"method": "bisect",
"n_cells": 1024,
"gnn_model": "base"
}
Warning
Like kmeans, the gnn backend refuses 3D meshes with non-manifold fine cells.

Agglomeration by Region (Materials)

Many problems have several materials or subdomains, and agglomerates must not cross the interface between them. Vulpes supports this through a cell tag:

  • segregated_tag: name of an integer cell-data array of the input mesh (e.g. a material ID). Cell-data arrays of the .vtu file are imported as cell tags with the same name; floating-point arrays are truncated to integers. Each region (set of cells with the same tag value) is agglomerated on its own, so no agglomerate contains cells of two regions.
  • Without regions, the same parameter is used in every region (with direct_kway, n_cells agglomerates per region).
  • regions: an object whose keys are tag values written as strings and whose values override n_cells and/or target_diameter for that region. Regions not listed use the top-level values. The regions are then agglomerated one at a time (the other cells are left untouched) and the labels are merged. Every key must be an existing value of the tag, otherwise the program stops.

Which key to override depends on the method:

Method Key to set in regions
direct_kway n_cells (agglomerates in that region)
bisect n_cells (converted to ceil(log2(n_cells)) levels in that region)
mult_factor target_diameter (maximum diameter in that region)

Example: a cube with two materials

The figure shows a cube meshed with tetrahedra and split into two materials (Material ID = 1 and Material ID = 2). The two halves are agglomerated independently and with different resolutions: the agglomerates of material 1 are small (fine agglomeration), those of material 2 are larger (coarse agglomeration), and no agglomerate crosses the material interface.

Cube agglomerated by material

Figure: tetrahedral cube with two materials (left) and its agglomeration with a different target size in each material (right).

A settings file that produces this kind of result, assuming the material is stored in a cell-data array named MaterialID (use the actual array name of your mesh, as shown in ParaView):

{
"dim": 3,
"input_mesh": "meshes/cube_two_materials_tet.vtu",
"output_mesh": "output/cube_two_materials_agg.vtu",
"backend": "parmetis",
"method": "mult_factor",
"target_diameter": 0.2,
"segregated_tag": "MaterialID",
"regions": {
"1": {"target_diameter": 0.1},
"2": {"target_diameter": 0.3}
},
"repair": true
}

With a fixed number of agglomerates per material, use direct_kway and override n_cells instead:

{
"dim": 3,
"input_mesh": "meshes/cube_two_materials_tet.vtu",
"output_mesh": "output/cube_two_materials_kway.vtu",
"backend": "kmeans",
"method": "direct_kway",
"n_cells": 100,
"segregated_tag": "MaterialID",
"regions": {
"1": {"n_cells": 400},
"2": {"n_cells": 50}
}
}

During the run, one line per region reports how many fine cells it has and how many groups were created:

region 1: 24000 cells -> 400 groups
region 2: 24000 cells -> 50 groups
Remarks
With the gnn backend and materials, "gnn_model": "hetero" uses the model trained on heterogeneous media.

Repair of 3D Agglomerates

In 3D an agglomerate can be made of two parts that touch only along an edge. That edge is then shared by more than two faces of the agglomerate (non-manifold edge): geometry::Cell3D does not support such polyhedra and the DG solvers abort on them. With "repair": true (default) the labels are fixed iteratively, until no such edge is left or repair_max_iterations is reached:

  1. Split wedges. Around each non-manifold edge of an agglomerate A, the fine cells of A are grouped into wedges (connected through the facets that contain the edge). The largest wedge stays in A; the others move to the neighbouring agglomerate that shares most facets with them, or to a new agglomerate if there is none. If an edge stays non-manifold for more than repair_stall_iterations iterations (the moves oscillate), its wedges become new agglomerates.
  2. Enforce connectivity. Every disconnected piece of an agglomerate, except the largest, is merged into its best neighbour.

Cells never move to an agglomerate of another region of segregated_tag, so the material interfaces are preserved.

After the agglomerated mesh is built it is validated: if any cell has a non-manifold edge or a face of zero area, the program exits with an error and the output mesh is not written. In that case:

  • with repair: false, set it to true;
  • with repair: true, increase repair_max_iterations, or rerun (ParMETIS is not deterministic).

In 2D the input check, repair and validation are skipped and the mesh is written as it is.

Related Examples

The other files in examples/mesh/agglomeration/ show how to call the agglomeration classes directly from C++:

  • 01_agg_kmeans.cpp, 02_agg_parmetis.cpp, 03_agg_gnn.cpp: the three backends;
  • 04_agg_segregated.cpp, 05_agg_segregated_multfact.cpp: segregated agglomeration by a cell tag;
  • 06_agg_metrics.cpp: quality metrics of the agglomerates.

References