Skip to main content

Example 8 — Triangulated Icosphere

Last updated

13 November 2026

Rendered with RayTraceHeatTransfer.jl v0.26.1 and Julia 1.13.3.

This example extends Example 7 from axis-aligned quads to an arbitrary convex triangulated geometry: a unit sphere approximated by recursively subdividing a regular icosahedron. A small hot cap of triangles is placed at the north pole and a matching cold cap at the south pole; all remaining triangles are in radiative equilibrium.

This example demonstrates three features of the package: arbitrary triangulated geometry (view factors are computed analytically via Narayanaswamy (2015) for any closed convex polyhedron built from planar triangles), the separate mesh / view factor / solve steps that let the user inspect the mesh before committing to the expensive view factor computation, and analytical view factors that are exact to working precision, in Float64 or in any higher precision, so that no smoothing is needed.

Step 1: Build and inspect the mesh

The icosphere is constructed by a helper function icosphere_mesh(level) that returns (points, faces) at the requested subdivision level. The mesh is then passed to ViewFactorDomain3D along with boundary conditions: at this point the domain holds the geometry and boundary conditions but not yet any view factors.

using RayTraceHeatTransfer
using GLMakie
using LinearAlgebra

include(joinpath(pkgdir(RayTraceHeatTransfer), "examples", "icosphere_mesh.jl"))   # defines icosphere_mesh

subdivision_level = 2                               # 0 → 20 triangles, 1 → 80, 2 → 320, 3 → 1280
points, faces = icosphere_mesh(subdivision_level)   # points: one row (x, y, z) per vertex; faces: three vertex indices per triangle
n_tri = size(faces, 1)                              # number of triangles

# Hot cap at the north pole and cold cap at the south pole:
# the n_cap triangles whose centroids lie highest and lowest in z
n_cap = 6
centroids   = [vec(sum(points[faces[i, :], :], dims = 1)) ./ 3 for i in 1:n_tri]   # centroid of triangle i: mean of its three vertices
z_centroids = [c[3] for c in centroids]                                             # height of every centroid
hot_ids  = partialsortperm(z_centroids, 1:n_cap, rev = true)                        # indices of the n_cap highest triangles
cold_ids = partialsortperm(z_centroids, 1:n_cap)                                    # indices of the n_cap lowest triangles

# boundary conditions, one entry per triangle
epsilon = ones(n_tri)                               # black surfaces
q_in_w  = zeros(n_tri)                              # zero net source wherever the temperature is unknown
T_in_w  = fill(-1.0, n_tri)                         # negative: temperature unknown, i.e. radiative equilibrium
T_in_w[hot_ids]  .= 1000.0                          # hot cap [K]
T_in_w[cold_ids] .=    0.0                          # cold cap [K]

Ndim = 1                                            # subdivisions per triangle edge; 1 keeps every triangle as one element
domain3D = ViewFactorDomain3D(points, faces, Ndim, q_in_w, T_in_w, epsilon);   # geometry and boundary conditions, no view factors yet
true

With Ndim = 1 each triangle is a single element, so element k is triangle k of the faces matrix. Hovering is the direct way to confirm the caps landed where intended; it works in an interactive Julia session, while on this page the figure is a static image:

fig  = Figure(size = (1200, 700))
ax   = LScene(fig[1, 1], scenekw = (camera = cam3d!, show_axis = true))
info = Label(fig[1, 2], ""; tellheight = false, tellwidth = true,
             halign = :left, justification = :left,
             fontsize = 14, font = "DejaVu Sans Mono") # with info as in Example 7
colsize!(fig.layout, 2, Relative(0.5))
plotMesh(ax, domain3D; inspect = true, label = info)
DataInspector(fig)
fig

Inspecting the mesh before committing to the view factor computation is especially valuable for triangulated geometries, where the subcell count scales as n_triangles², i.e. it grows quadratically with the number of geometric triangle elements.

Step 2: Compute view factors

Once the mesh looks right, view factors are computed by calling the domain as a functor:

domain3D(; parallel = true, verbose = false)

This computes a view factor for every pair of subcells and is typically the most expensive step in the workflow. Rows are normalised on assembly, so F_raw conserves energy exactly. The analytical view factors are also reciprocal to round-off, as shown below, so they are used directly, without smoothing.

Step 3: Solve and visualise

solveEquilibrium!(domain3D, domain3D.F_raw; verbose = false)

fig  = Figure(size = (1200, 700))
ax   = LScene(fig[1, 1], scenekw = (camera = cam3d!, show_axis = true))
info = Label(fig[1, 2], ""; tellheight = false, tellwidth = true,
             halign = :left, justification = :left,
             fontsize = 14, font = "DejaVu Sans Mono") # with info as in Example 7
colsize!(fig.layout, 2, Relative(0.5))
plotField(ax, domain3D; field = :T, inspect = true, label = info)
DataInspector(fig)
fig

The resulting temperature field shows a bright hot cap at the north pole, a dark cold cap at the south, and a nearly isothermal bulk throughout most of the sphere — as expected when a small hot source and a small cold sink are embedded in a highly concave enclosure.

Machine-precision symmetry

The icosphere is centrally symmetric, and the two caps are exact antipodes of each other. Swapping the cap temperatures therefore maps the problem onto itself, with every triangle exchanged for its opposite triangle. The equations are linear in the emissive powers, so adding the original solution and the swapped one gives the solution in which both caps have the emissive power of T_hot⁴ + T_cold⁴; in a black enclosure that solution is uniform. Every triangle in radiative equilibrium and its antipode therefore satisfy

T_k^4 + T_{-k}^4 = T_{\text{hot}}^4 + T_{\text{cold}}^4

exactly. The identity holds for the discretised sphere itself at every subdivision level, not only in the limit of a fine mesh, so any deviation from it is numerical error.

Alongside the symmetry check, the function below reports the reciprocity defect of the view factors,

\delta_R(F) = \sqrt{ \sum_{i \lt j} \frac{(w_i F_{ij} - w_j F_{ji})^2} {w_i^2 + w_j^2} }

where w_i is the element weight (surface area in 3D). It is a sum over pairs, not a percentage, so it grows with element count too. The whole pipeline is written as one function of the floating-point type T, so the same code runs in higher precision further below:

# reciprocity defect δ_R of a view factor matrix F with element weights w, as defined above
function reciprocity_defect(F, w)
    s = zero(eltype(F))                             # running sum, in the precision of F
    for i in 1:size(F, 1), j in (i + 1):size(F, 2)  # every pair i < j once
        s += (w[i] * F[i, j] - w[j] * F[j, i])^2 / (w[i]^2 + w[j]^2)
    end
    return sqrt(s)
end

# the full pipeline on one icosphere level, in the floating-point type T
function solve_icosphere(::Type{T}, level; n_cap = 6) where {T}
    points, faces = icosphere_mesh(T, level)        # the sphere, built in T
    n_tri = size(faces, 1)                          # number of triangles

    # caps: the triangles with the highest and the lowest centroids, as in Step 1
    centroids   = [vec(sum(points[faces[i, :], :], dims = 1)) ./ 3 for i in 1:n_tri]
    z_centroids = [c[3] for c in centroids]
    hot_ids  = partialsortperm(z_centroids, 1:n_cap, rev = true)
    cold_ids = partialsortperm(z_centroids, 1:n_cap)

    # each triangle's antipode: the triangle whose centroid is closest to the mirrored centroid
    antipode = [argmin([sum(abs2, centroids[j] .+ centroids[i]) for j in 1:n_tri]) for i in 1:n_tri]
    Set(antipode[hot_ids]) == Set(cold_ids) ||      # the identity needs exactly antipodal caps
        error("level $level: the hot and cold caps are not antipodal; choose another cap size")

    # boundary conditions in T: black surfaces, prescribed caps, everything else in radiative equilibrium
    T_hot, T_cold = T(1000), T(0)                   # cap temperatures [K]
    epsilon = ones(T, n_tri)
    q_in_w  = zeros(T, n_tri)
    T_in_w  = fill(-one(T), n_tri)
    T_in_w[hot_ids]  .= T_hot
    T_in_w[cold_ids] .= T_cold

    # the three workflow steps
    domain = ViewFactorDomain3D(points, faces, 1, q_in_w, T_in_w, epsilon)   # mesh, one element per triangle
    domain(; parallel = true, verbose = false)                               # view factors for every pair of triangles
    solveEquilibrium!(domain, domain.F_raw; verbose = false)                 # solve, on the view factors as computed

    # reciprocity defect of F_raw, with the element areas as weights
    areas = [sf.area for f in domain.facesMesh for sf in f.subFaces]         # element areas, in the order of F
    δ_raw = reciprocity_defect(domain.F_raw, areas)

    # largest relative deviation from the symmetry identity over all equilibrium triangles
    Tw   = [domain.facesMesh[i].subFaces[1].T_w for i in 1:n_tri]            # temperature of every triangle [K]
    free = setdiff(1:n_tri, hot_ids, cold_ids)                               # the triangles in radiative equilibrium
    deviation = maximum(abs.(Tw[free] .^ 4 .+ Tw[antipode[free]] .^ 4 .- (T_hot^4 + T_cold^4))) / T_hot^4

    return n_tri, δ_raw, deviation, domain.energy_error
end

# Float64 at three subdivision levels: 80, 320 and 1280 triangles (level 0 has no antipodal 6-triangle caps)
for level in 1:3
    n_tri, δ_raw, deviation, _ = solve_icosphere(Float64, level)
    println("Level $level: $n_tri triangles → δ_R(F_raw) = $(round(δ_raw, sigdigits = 3)), ",
            "symmetry deviation = $(round(deviation, sigdigits = 3))")
end
Level 1: 80 triangles → δ_R(F_raw) = 3.31e-14, symmetry deviation = 7.12e-14
Level 2: 320 triangles → δ_R(F_raw) = 8.49e-13, symmetry deviation = 2.5e-12
Level 3: 1280 triangles → δ_R(F_raw) = 3.62e-10, symmetry deviation = 2.11e-9

Both quantities stay at round-off level at every subdivision. The view factors are evaluated from Narayanaswamy’s analytical expression, with the Clausen function in it summed to full precision, so F_raw is reciprocal to working precision and smoothing has nothing to correct. Smoothing remains essential for ray-traced exchange factors, where Monte Carlo noise breaks reciprocity.

Arbitrary precision

That the remaining deviation is round-off, and not a property of the method, can be checked directly. The view factors and the grey solve run in any floating-point type, so the same pipeline can be repeated in 128-bit arithmetic, which carries about 38 significant digits instead of 16:

setprecision(BigFloat, 128)                         # 128-bit BigFloat arithmetic
n_tri, δ_raw, deviation, energy_error = solve_icosphere(BigFloat, 2)   # level 2: 320 triangles
println("Level 2 in 128 bits: $n_tri triangles → δ_R(F_raw) = $(round(Float64(δ_raw), sigdigits = 3)), ",
        "symmetry deviation = $(round(Float64(deviation), sigdigits = 3)), ",
        "energy error = $(round(Float64(energy_error), sigdigits = 3))")
Level 2 in 128 bits: 320 triangles → δ_R(F_raw) = 2.2599999999999998e-35, symmetry deviation = 7.699999999999999e-35, energy error = -1.03e-39

Every error falls by about 22 orders of magnitude relative to the Float64 run at the same level, which is the ratio of the two machine precisions. The deviations in Float64 are therefore pure round-off, and energy is conserved to the working precision in either arithmetic.

References

Narayanaswamy, Arvind. 2015. “An Analytic Expression for Radiation View Factor Between Two Arbitrarily Oriented Planar Polygons.” International Journal of Heat and Mass Transfer 91: 841–47. https://doi.org/10.1016/j.ijheatmasstransfer.2015.07.131.