using RayTraceHeatTransfer
using GeometryBasics, StaticArrays
function build_mesh(Ndim) # the mesh builder of Example 1
vertices = SVector(
Point2(0.0, 0.0),
Point2(1.0, 0.0),
Point2(1.0, 1.0),
Point2(0.0, 1.0)
)
solidWalls = SVector(true, true, true, true) # all walls impenetrable by radiation
face = PolyVolume2D{Float64}(vertices, solidWalls, 1, 1.0, 0.0) # κ = 1, σₛ = 0 for the gas volume
face.T_in_w = [1000.0, 0.0, 0.0, 0.0] # bottom hot, rest cold
face.epsilon = [1.0, 1.0, 1.0, 1.0] # black walls
face.T_in_g = -1.0 # unknown (solve for this)
face.q_in_g = 0.0 # radiative equilibrium
mesh = RayTracingDomain2D([face], [(Ndim, Ndim)]; verbose = false) # mesh the domain
return mesh
end
Ndim = 11 # 11 × 11 elements
mesh = build_mesh(Ndim) # the domain to trace
emitters = length(mesh.surface_mapping) + length(mesh.volume_mapping) # wall elements plus gas cells
max_rays = emitters * 10^5; # cap on the rays of every run belowExample 13 — Tracing to a Tolerance
Rendered with RayTraceHeatTransfer.jl v0.27.0 and Julia 1.13.4.
The earlier examples choose the number of rays by hand. trace_to_tolerance! chooses it from a requested accuracy instead: it traces in rounds, and after every round it smooths, solves and predicts the relative standard error of the solution from the smoothed exchange factors and the ray counts. It stops when the prediction meets the target, or when a cap on the rays is reached. This example runs it on the problem of Example 1 and compares the predicted errors with errors measured against a reference trace.
The function reports two errors, each a root mean square error over a root mean square value: one for the blackbody emissive power σT⁴ of the elements whose temperature is unknown, here the gas cells, and one for the net power of the elements whose temperature is prescribed, here the walls. A relative error of the emissive power is four times the relative error of the temperature.
Step 1: The domain
The enclosure of Example 1: a 1 × 1 m square filled with an absorbing gas (κ = 1 m⁻¹), black walls, the bottom wall at 1000 K and the others at 0 K, meshed 11 × 11.
Step 2: One call
The call takes the target and the cap. With verbose = true, the default, it prints one line per round: the rays cast so far, the two predicted errors, and the factors applied to them.
result = trace_to_tolerance!(mesh; target = 0.01, max_rays = max_rays) # trace, smooth and solve to 1 % round 1: 82500 rays, emissive power error 4.34 %, net power error 1.78 %, factors 1.0 and 1.0
round 2: 1712113 rays, emissive power error 0.366 %, net power error 0.107 %, factors 0.46 and 0.44
(converged = true, rounds = 2, rays = 1712113, emissive_power_error = 0.00365795331579615, net_power_error = 0.0010709888424138198, emissive_power_factor = 0.46107484790048514, net_power_factor = 0.43886322071475525, smoothing_converged = true)
The first round casts 500 rays from every emitter. Its prediction sizes the second round, which by default casts all the rays that the prediction asks for, so most runs take two rounds. The returned named tuple holds what the last line printed, together with converged, rounds and rays. The domain now holds the raw and the smoothed exchange factors and the solution, exactly as after the separate trace, smooth and solve steps of Example 1.
The rays of the second round are not shared equally. With guided = true, the default, they go to the emitters that reduce the predicted variance most, so the ray counts differ between the elements:
println("rays per emitter: smallest = ", minimum(mesh.ray_counts), # the least-traced element
", largest = ", maximum(mesh.ray_counts)) # the most-traced elementrays per emitter: smallest = 923, largest = 79427
The guidance depends on the boundary conditions of the domain. The exchange factors remain valid for other boundary conditions, but they are most accurate for these.
Step 3: Guided against equal rays
With guided = false the added rays are shared equally among the emitters. The same target then shows what the guidance is worth on this problem: the two runs can be compared by the rays in their last printed line.
equal = trace_to_tolerance!(mesh; target = 0.01, max_rays = max_rays, guided = false) # equal rays in every round
println("rays: guided = ", result.rays, ", equal = ", equal.rays) # total rays of the two runs round 1: 82500 rays, emissive power error 4.34 %, net power error 1.78 %, factors 1.0 and 1.0
round 2: 1712113 rays, emissive power error 0.437 %, net power error 0.181 %, factors 0.46 and 0.46
rays: guided = 1712113, equal = 1712113
A second call on the same domain replaces the earlier trace.
Step 4: Pseudorandom rays
The predicted errors come from a variance model that assumes independent rays. Pseudorandom rays are independent, so with sampler = :random the prediction applies as it stands and both factors are one. Sobol rays, the default, are more accurate than independent rays. Their predicted errors are therefore multiplied by a factor that is estimated during the run from how much the solution changes between rounds, which takes a second round. The second round is sized before that factor is known, so a Sobol run casts as many rays as a pseudorandom run would need and ends below the target.
random = trace_to_tolerance!(mesh; target = 0.01, max_rays = max_rays, sampler = :random) # independent rays round 1: 82500 rays, emissive power error 4.35 %, net power error 1.78 %, factors 1.0 and 1.0
round 2: 1713825 rays, emissive power error 0.793 %, net power error 0.244 %, factors 1.0 and 1.0
(converged = true, rounds = 2, rays = 1713825, emissive_power_error = 0.007927262387578958, net_power_error = 0.0024389384375422944, emissive_power_factor = 1.0, net_power_factor = 1.0, smoothing_converged = true)
Step 5: Predicted against measured errors
Whether the prediction can be trusted is checked against a reference: one heavy trace on the same mesh, so that the comparison measures the sampling error alone and not the mesh error. The emissive powers of the gas cells and the net powers of the wall elements are read from a solved domain by two small functions.
# Blackbody emissive power σT⁴ of every gas cell of a solved domain
function gas_emissive_powers(mesh)
sigma = 5.670374419e-8 # Stefan-Boltzmann constant, W/(m² K⁴)
return [sigma * cell.T_g^4 for cell in mesh.fine_mesh[1]] # T_g: gas temperature from the solver
end
# Net power of every wall element of a solved domain, in the order of the exchange factor matrix
function wall_net_powers(mesh)
powers = zeros(length(mesh.surface_mapping)) # one entry per wall element
for ((coarse, fine, wall), index) in mesh.surface_mapping # the cell and wall behind every wall element
powers[index] = mesh.fine_mesh[coarse][fine].q_w[wall] # q_w: net power of that wall from the solver
end
return powers
end
# Root mean square difference of `values` from `reference`, over the root mean square of `reference`
function relative_difference(values, reference)
return sqrt(sum(abs2, values .- reference) / sum(abs2, reference))
end
reference = build_mesh(Ndim) # the same domain for the reference
reference(emitters * 2^18; method = :pathlength, verbose = false) # 2¹⁸ rays per emitter with the pathlength tracer
smooth!(reference; verbose = false) # reciprocity and energy conservation
solveEquilibrium!(reference, reference.F_smooth; verbose = false) # the reference solution
reference_gas = gas_emissive_powers(reference) # reference emissive powers of the gas cells
reference_wall = wall_net_powers(reference); # reference net powers of the wall elementsOne run gives one draw of a random error, so the comparison is made over several independent runs. For each sampler the function below repeats the 1 % run with different seeds and returns the root mean square of the predicted and of the measured errors.
# Repeat trace_to_tolerance! with every seed in `seeds`, and return the root mean square over the
# runs of the predicted and the measured relative errors, for the emissive power and the net power.
function predicted_and_measured(Ndim, target, max_rays, sampler, seeds, reference_gas, reference_wall)
predicted_gas = 0.0 # sum of squared predicted emissive power errors
predicted_wall = 0.0 # sum of squared predicted net power errors
measured_gas = 0.0 # sum of squared measured emissive power errors
measured_wall = 0.0 # sum of squared measured net power errors
for seed in seeds # one independent run per seed
mesh = build_mesh(Ndim) # a fresh domain
run = trace_to_tolerance!(mesh; target = target, max_rays = max_rays,
sampler = sampler, seed = seed, verbose = false) # trace to the target
predicted_gas += run.emissive_power_error^2 # the error the run predicts for the gas
predicted_wall += run.net_power_error^2 # the error the run predicts for the walls
measured_gas += relative_difference(gas_emissive_powers(mesh), reference_gas)^2 # its actual error, gas
measured_wall += relative_difference(wall_net_powers(mesh), reference_wall)^2 # its actual error, walls
end
runs = length(seeds) # number of runs
return (predicted_gas = sqrt(predicted_gas / runs), measured_gas = sqrt(measured_gas / runs),
predicted_wall = sqrt(predicted_wall / runs), measured_wall = sqrt(measured_wall / runs))
endpredicted_and_measured (generic function with 1 method)
using Printf
seeds = 100 .* (1:8) # eight runs; spaced, since a pseudorandom run uses seed, seed + 1, ...
for sampler in (:random, :sobol) # pseudorandom rays, then Sobol rays
errors = predicted_and_measured(Ndim, 0.01, max_rays, sampler, seeds, reference_gas, reference_wall)
@printf("sampler = :%-8s emissive power net power\n", sampler) # header of the block
@printf(" predicted error / %% %12.3f %9.3f\n", 100 * errors.predicted_gas, 100 * errors.predicted_wall)
@printf(" measured error / %% %12.3f %9.3f\n", 100 * errors.measured_gas, 100 * errors.measured_wall)
endsampler = :random emissive power net power
predicted error / % 0.795 0.245
measured error / % 0.799 0.252
sampler = :sobol emissive power net power
predicted error / % 0.387 0.140
measured error / % 0.371 0.106
Each block lists the predicted error above the measured one, in percent, for a target of 1 %. A prediction that can be trusted has the measured error at or below the predicted one.
Step 6: When the cap is reached first
If the target cannot be met within max_rays, the function stops at the cap, warns, and returns with converged = false. The domain still holds the solution of the rays that were cast, and the returned errors say how far it is from the target.
capped = trace_to_tolerance!(mesh; target = 1e-4, max_rays = emitters * 2000) # a target out of reach of this cap
capped.converged, capped.rays == emitters * 2000 # not converged, and stopped exactly at the cap round 1: 82500 rays, emissive power error 4.34 %, net power error 1.78 %, factors 1.0 and 1.0
round 2: 330000 rays, emissive power error 0.911 %, net power error 0.252 %, factors 0.51 and 0.45
┌ Warning: trace_to_tolerance! stopped at 330000 rays after 2 rounds without reaching the target of 0.0001: emissive power error 0.009111449810451946, net power error 0.0025165510044159005 └ @ RayTraceHeatTransfer C:\Users\Nikolaj\.julia\packages\RayTraceHeatTransfer\FR8X4\src\AdaptiveTracing\trace_to_tolerance.jl:251
(false, true)
Notes
trace_to_tolerance!requires a grey 2D domain without adirectional_model, with at most 2000 elements and at least one prescribed temperature. Every gas cell needs a nonzero extinction coefficient.- It traces with
method = :exchangeand smooths with:ml. - A finite
growthlimits the factor by which one round multiplies the rays. The run then takes more rounds, each with its own smoothing and solve, and a Sobol run casts fewer rays. - To add rays by hand instead, use
append = trueon the:exchangetracer, as in Step 7 of Example 1 and of Example 2.