Skip to content

Replace Shewchuk Triangle with fortfem's MIT-licensed Triangle-compatible mesher#27

Draft
krystophny wants to merge 1 commit into
mainfrom
drop-triangle
Draft

Replace Shewchuk Triangle with fortfem's MIT-licensed Triangle-compatible mesher#27
krystophny wants to merge 1 commit into
mainfrom
drop-triangle

Conversation

@krystophny

Copy link
Copy Markdown
Member

Replaces Shewchuk's Triangle with the clean-room, MIT-licensed Triangle-compatible mesher from fortfem (lazy-fortran/fortfem#56). Triangle's license forbids distribution as part of a commercial system without arrangement with the author; Debian classifies it as non-free. That restriction conflicts with MEPHIT's MIT grant.

Resolves #25

Changes

  • src/mephit_triangulate.f90: new Fortran module calling fortfem's triangulate_compat (equivalent to Triangle flags pq20Yz) and writing outer.msh in the same format as the old C writer, including C %.16e float rendering.
  • src/mephit_mesh.F90: write_FreeFem_mesh calls the Fortran routine; the C interface block is gone.
  • src/mephit_fem.c, src/mephit_fem.h: FEM_triangulate_external and the triangle.h include removed. No caller of Triangle remains.
  • cmake/SetupFortfem.cmake replaces cmake/SetupTriangle.cmake and cmake/setup_triangle.sh: FetchContent of fortfem with FORTFEM_TRIANGLE_COMPAT_ONLY=ON, which builds only the dependency-free mesher library fortfem_triangle_compat (no fortplot, no extra SuiteSparse/BLAS requirements).
  • README.md: Triangle removed from the prerequisites.

Notes

Verification

Mesher parity against Triangle on six MEPHIT-derived cases

Golden meshes generated with the original Triangle binary (triangle -BejnpqYz, the flags from FEM_triangulate_external) on six PSLG cases derived from MEPHIT data: TCFP LCFS from data/g000001.0001_TCFP, the exact write_FreeFem_mesh TCFP annulus (256-point LCFS + 32-point outer ellipse + hole seed), the AUG convex wall from data/convexwall_asdex.dat, and D-shape/annulus variants at 400 and 1000 vertices. fortfem's triangle_compat_cli output compared against these:

case1_tcfp_lcfs:      474 vertices, 690 triangles  max abs coord diff 0.000e+00, connectivity equal
case2_tcfp_annulus:   529 vertices, 770 triangles  max abs coord diff 0.000e+00, connectivity equal
case3_aug_wall:        54 vertices,  68 triangles  max abs coord diff 0.000e+00, connectivity equal
case4_aug_dshape_400: 731 vertices, 1060 triangles max abs coord diff 0.000e+00, connectivity equal
case5_aug_annulus:    818 vertices, 1186 triangles max abs coord diff 0.000e+00, connectivity equal
case6_aug_dshape_1000: 1893 vertices, 2784 triangles max abs coord diff 0.000e+00, connectivity equal

Vertex coordinates are bit-exact doubles in identical order in all six cases. Triangle connectivity is identical; in cases 3 to 6 also row by row, in cases 1 and 2 up to enumeration order (see below).

End-to-end: old MEPHIT path vs new MEPHIT path

Before (unmodified FEM_triangulate_external from src/mephit_fem.c linked against Debian-patched Triangle 1.6) and after (mephit_triangulate linked against fortfem_triangle_compat), driven by the same harness on the write_FreeFem_mesh boundary geometry, comparing the generated outer.msh:

TCFP annulus (256+32 boundary points, hole at (170, 0)):

header identical:                                    True   (529 770 288)
vertex lines identical (order+bytes):                True
triangle lines identical as sets:                    True
triangle lines identical (order+bytes):              False  (253 of 770 rows permuted)
same triangles with same cyclic orientation:         True
segment lines identical (order+bytes):               True

AUG annulus (400+50 boundary points, hole at (165, 0)):

$ cmp outer5_old.msh outer5_new.msh && echo "case5 outer.msh BYTE-IDENTICAL"
case5 outer.msh BYTE-IDENTICAL

The only deviation anywhere is the enumeration order of triangles in the TCFP case: same vertices (bit-exact, same indices), same triangles (same vertex triples, same orientation), different row order. Triangle's row order is an artifact of its internal memory-pool traversal. outer.msh is read by FreeFem (readmesh in maxwell_daemon.edp/extmesh.edp) and merged with the core plasma mesh; FreeFem renumbers elements on mesh addition, so triangle enumeration order does not enter the results.

Build

Full build from clean configure with this branch, gfortran 15.2 on macOS (fortfem fetched from GitHub by SetupFortfem.cmake):

[240/245] Linking CXX shared library lib/libmephit.dylib
[242/245] Linking Fortran executable bin/mephit_post.x
[243/245] Linking C executable bin/mephit_run.x
[244/245] Linking Fortran executable bin/mephit_test.x

Before this change, the same configure required fetching the non-free Triangle source from salsa.debian.org and building libtriangle.so.

The fortfem side is covered by test_triangle_compat_golden and test_triangle_compat_oracle in lazy-fortran/fortfem#56 (both pass under ctest).

Triangle forbids distribution as part of a commercial system without
arrangement with the author, which conflicts with MEPHIT's MIT license.
fortfem's clean-room triangle_compat module reproduces Triangle's
behavior for the flags MEPHIT uses (BejnpqYz): on the TCFP annulus
geometry of write_FreeFem_mesh, the generated outer.msh has
byte-identical vertices and segments and the same triangles up to
enumeration order; on the AUG annulus the file is byte-identical.

The mesher is now called directly from Fortran (mephit_triangulate),
so the C wrapper around triangulate() goes away along with the
Triangle fetch-and-patch machinery. fortfem is fetched via
FetchContent with FORTFEM_TRIANGLE_COMPAT_ONLY=ON, which builds only
the dependency-free mesher library.

Resolves #25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Triangle (Shewchuk) has a non-commercial restriction incompatible with MIT license

1 participant