Skip to content
DLR-AMRPublic

About

Example program to calculate Pi using adaptive mesh refinement

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

t8pie

CI status

Computing π with adaptive mesh refinement — powered by t8code.

Animation: a square mesh of triangles is refined level by level, only where the unit circle cuts the triangles. The triangle count rises from 2 to 167,516 at level 13, while the approximation of pi converges to 3.141592654. The right side zooms onto the circle and colors each triangle by the part of it inside the circle, from gray (outside) to green (inside).

How do you compute π with a mesh? Take a square, put the unit circle inside, and add up the area of everything inside the circle — its area is exactly π · 1² = π. The trick is to be accurate only where it matters: at the circle's boundary. t8pie refines a triangle mesh adaptively along the circle. With its default method it gets nine correct digits from fewer than 100,000 triangles; a uniform mesh of the same resolution would need about 34 million.

t8pie is a small standalone project in the t8code family. It shows how to use t8code as an external library — fetched and built automatically by CMake — for a complete, parallel AMR workflow: build a coarse mesh, adapt it recursively, evaluate the elements in parallel, reduce the result with MPI, and write VTK output.

How it works

  1. Coarse mesh. The square [-1,1]² is covered by two triangles.
  2. Adapt. Level by level, up to a maximum --level, every triangle that the circle cuts is refined; the triangles completely inside or outside of the circle stay coarse. After each step t8_forest_iterate_replace carries over which triangles are cut, so this is computed only for the new triangles. Only a thin band of elements along the circle gets fine. Since this concentrates the elements on some processes, the mesh and the cut indicators are repartitioned after each step, so that every process holds the same number of triangles.
  3. Sum up. Each process adds up, for each triangle, the part of its area that lies inside the circle (fractional area). For the triangles that the circle cuts, the arc is replaced by the chord between the two points where the circle crosses the triangle's edges. An MPI_Allreduce gives the global area — the approximation of π.
  4. Extrapolate. The chords make the error shrink by a factor of 4 per level (order 2). So t8pie computes the area once at --level − 1, refines this mesh by one more level and computes it again. The Richardson extrapolation (4 · Alevel − Alevel−1) / 3 cancels the leading error term.
  5. Visualize. For levels below 15 the mesh is written to VTK, with a field Elements_in_circle that holds the fraction of each triangle inside the circle.

The original, simpler method counts a triangle fully if its midpoint is inside the circle, and not at all otherwise. It is available with --fractional=0. Since it does not know which triangles the circle cuts, it refines every triangle whose midpoint is closer to the circle than the triangle's diameter, a band that is a few triangles wide.

Left: the adapted mesh at level 13 with 167,516 triangles, refined only along the red unit circle; the approximation is 3.141592654 with an error of 8.1e-11. Right: a zoom onto the circle's boundary. The triangles are colored by the part inside the circle, from gray to green; the refinement drops from fine at the circle to coarse inside within a few triangles.
Default method, level 13: 167,516 triangles give π ≈ 3.141592654. Right: zoom onto the boundary — only the triangles that the circle cuts are refined; the color shows the part of each triangle inside the circle.

Results

Log-log plot of the approximation error against the number of triangles for three methods. Midpoint: the error falls irregularly to 7.9e-8 at 3.2 million triangles. Fractional, with only about 40 percent of the triangles: the error falls steadily to 2.3e-10 at 1.3 million triangles. Fractional plus Richardson: the error falls fastest, to 3.1e-13.

Level Triangles, midpoint Error, midpoint Triangles, fractional Error, fractional Error, fractional + Richardson (default)
4 362 9.7 · 10⁻⁴ 218 4.8 · 10⁻³ 9.5 · 10⁻⁵
8 10,772 1.9 · 10⁻⁴ 5,042 1.6 · 10⁻⁵ 2.0 · 10⁻⁷
12 194,444 7.5 · 10⁻⁶ 83,630 6.0 · 10⁻⁸ 8.5 · 10⁻¹⁰
16 3,202,244 7.9 · 10⁻⁸ 1,342,154 2.3 · 10⁻¹⁰ 3.1 · 10⁻¹³
  • Midpoint: the error drops roughly like 1/N in the number of triangles N, but not at every level, since triangles wrongly counted inside and outside the circle partly cancel.
  • Fractional: the error drops steadily like 1/N², by a factor of 4 per level, and the result is always slightly below π. Refining only the cut triangles needs about 2.4 times fewer triangles than the band of the midpoint method, for the same result.
  • Fractional + Richardson: typically another 2–3 orders of magnitude. The gain varies from level to level, since the cut positions of the circle in the mesh change irregularly.

The results do not depend on the number of MPI processes.

Quick start

Requirements: CMake ≥ 3.19, a C/C++20 compiler, MPI and git. t8code and its dependencies (libsc, p4est, googletest-mpi) are downloaded and built automatically.

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j
mpirun -np 4 ./t8pie_compute_pi --level 12
[t8] Approximating Pi with parameters:
Level	12
X	1.000000
Shape	Triangle
Fractional	1
Richardson	1
[t8] Constructed uniform forest with 2 global elements.
[t8] Into t8_forest_adapt from 2 total elements
[t8] Done t8_forest_adapt with 8 total elements
[t8] Into t8_forest_iterate_replace
[t8] Done t8_forest_iterate_replace
[t8] Enter  forest partition.
[t8] Done forest partition.
[t8] Enter forest partition data.
[t8] Done forest partition data.
...
[t8] Done t8_forest_adapt with 41696 total elements
[t8] Into t8_forest_iterate_replace
[t8] Done t8_forest_iterate_replace
[t8] Enter  forest partition.
[t8] Done forest partition.
[t8] Enter forest partition data.
[t8] Done forest partition data.
[t8] Pi at level 11:	3.14159241019350599089
[t8] Error = 2.43396e-07
[t8] Into t8_forest_adapt from 41696 total elements
[t8] Done t8_forest_adapt with 83630 total elements
[t8] Into t8_forest_iterate_replace
[t8] Done t8_forest_iterate_replace
[t8] Enter  forest partition.
[t8] Done forest partition.
[t8] Enter forest partition data.
[t8] Done forest partition data.
[t8] Pi is approximately:	3.14159259337786611610
[t8] Error = 6.02119e-08
[t8] Richardson extrapolation of levels 11 and 12:	3.14159265443931934314
[t8] Error = 8.49526e-10
Option Default Meaning
-l, --level 4 Maximum refinement level.
-f, --fractional on Count the part of each triangle inside the circle. -f0 uses the midpoint method.
-r, --richardson on Richardson extrapolation of levels level − 1 and level. -r0 turns it off. Needs --fractional and level ≥ 1, otherwise it is turned off with a note.

Write the values without a space (-f0, --fractional=0).

Open the written t8_compute_Pi__Triangle_x_1p000000_level_12_fractional.pvtu in ParaView and color by Elements_in_circle, the part of each triangle inside the circle. ./t8pie_compute_pi --help lists all options.

Run the tests with:

ctest --output-on-failure

CMake options

Option Default Meaning
T8PIE_T8CODE_GIT_TAG v4.0.0-26.12 t8code tag, branch or commit to fetch.
T8PIE_USE_SYSTEM_T8CODE OFF Use an installed t8code instead (-DT8CODE_DIR=... -DP4EST_DIR=... -DSC_DIR=...).
T8PIE_BUILD_TESTS ON Build the tests.
T8PIE_TEST_NUM_PROCESSES 4 Number of MPI processes for the tests.
T8CODE_ENABLE_VTK OFF Build the fetched t8code with VTK. t8pie then uses VTK as well.

Project layout

Path Content
CMakeLists.txt Top-level CMake file, fetches t8code.
src/ The t8pie library with the π approximation (t8pie_compute_pi.hxx/.cxx).
t8pie_main.cxx The t8pie_compute_pi executable.
test/ GoogleTest tests, run in parallel via CTest.
doc/images/ Pictures for this README.
AGENTS.md Development guide: build, test and coding guidelines.
NEWS.md Release notes.
CITATION.cff How to cite t8pie.

Background and credits

The π code (src/t8pie_compute_pi.cxx, t8pie_main.cxx) comes from the t8code pull request DLR-AMR/t8code#1800 and was adapted to t8code v4. The animation and the screenshot show t8pie 1.0.0 runs (levels 0 to 13, 4 MPI processes), in the style of the pictures made for Pi Approximation Day 2025 (July 22nd) with the original midpoint method. Those pictures, the full-resolution video, the ParaView state and the VTK files are in DLR-AMR/t8data#6. The convergence plot was generated with t8pie runs for levels 0 to 16.

Citing

If you use t8pie, please cite it with the metadata in CITATION.cff (GitHub shows it under "Cite this repository") and cite t8code as well.

Contributing

See CONTRIBUTING.md and AGENTS.md. The t8code coding guidelines apply.

License

t8pie is licensed under the GNU General Public License v3 or later, see LICENSE.

About

Example program to calculate Pi using adaptive mesh refinement

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages