Algorithmcore
Collision estimator
Also called collision tally.
Estimates the flux in a region by summing weight over total cross section at each collision in that region, divided by the number of histories and the volume.
- Time
- O(N K) average
- Space
- O(B) extra
- Where
- B = the number of tally bins; K = the mean number of collisions per history; N = the number of histories
- Model
- Real RAM
- Properties
- randomizedexactiterativerandomized
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Nicholas Metropolis, Stanislaw Ulam, 1949
The collision estimator turns the histories of a Monte Carlo transport run into an estimate of the scalar flux in a region. Each time a particle of weight collides in the region, it scores , where is the total cross section at the collision point; the sum of the scores divided by the number of histories and the volume is an unbiased estimate of the average flux in the region. The reason is that the collision rate density is , so turns a count of collisions into a flux. The track-length estimator estimates the same quantity from the path lengths and is better in thin regions and the only choice in a void, while the collision estimator is better in dense regions with many short flights. Next-event estimation scores the expected contribution to a point detector at each collision, and path tracing in graphics uses the same idea for the light that reaches the eye.
In plain words
You want to know how many particles pass through a box, but you only see where they hit atoms. In a dense material the particles hit atoms often, so a lot of hits does not mean a lot of particles.
The fix is to give each hit a value of one over the density of hits per centimetre. In a dense box each hit counts for little; in a thin box each hit counts for a lot. Add up these values for all particles, divide by the number of particles and the size of the box, and you get the average flux in the box. The answer is right on average. In a box with almost no atoms there are few hits, each with a large value, so the answer is noisy, and in an empty box there are no hits at all.
The problem
A Monte Carlo transport run follows independent histories, as in analog Monte Carlo transport. The geometry has tally regions (bins) with volumes . The quantity to estimate is the average scalar flux per source particle,
or a reaction rate for a reaction . Here is the scalar flux of the linear Boltzmann transport equation, in particles per per source particle for a fixed source problem. The code below uses a one-speed slab with bins of width , so the "volume" of a bin is its width, and the flux is in particles per cm per source particle.
The algorithm
COLLISION-ESTIMATOR(N, SOURCE, bins)- 1
for n ← 1 to N do - 2
(x, μ) ← SOURCE(), w ← 1, h ← 0 ▷ h holds the scores of this history - 3
loop: fly to the next collision with free-flight sampling - 4
if the particle left the system then break - 5
j ← the bin of x - 6
h_j ← h_j + w / Σ_t(x) ▷ the collision score - 7
with probability Σ_a / Σ_t break ▷ absorbed - 8
μ ← a new direction from the scattering law - 9
S_j ← S_j + h_j, Q_j ← Q_j + h_j² for every bin j - 10
return φ_j = S_j / (N V_j), with s_j = sqrt((Q_j / N - (S_j / N)²) / (N - 1)) / V_j
Line L6 is the only line that differs from an analog history. For a reaction rate it scores instead, which is times the probability that the collision is of type . Line L9 adds the total of one history, not the single scores, because the collisions of one history are correlated; the variance in line L10 is the variance of these totals. With implicit capture line L7 is replaced by a weight change, and line L6 scores the weight before the collision.
Example
Two histories by hand. A beam enters a slab of 2 cm with and , with two bins of 1 cm. Each score is cm.
| history | flight | collision at | bin | score | outcome |
|---|---|---|---|---|---|
| 1 | 0.3466 | 0 | 0.5 | scatters to | |
| 1 | 1.4979 | 1 | 0.5 | absorbed | |
| 2 | 0.3466 | 0 | 0.5 | absorbed |
The totals are and , so and per source particle.
A pure absorber. A beam on a slab of 2 cm with and has the flux , so the average over a bin is . With four bins of 0.5 cm and (random.Random(1)):
| bin | exact | collision estimate | standard error |
|---|---|---|---|
| [0, 0.5] | 0.786939 | 0.7869 | 0.0069 |
| [0.5, 1] | 0.477302 | 0.4776 | 0.0060 |
| [1, 1.5] | 0.289499 | 0.2798 | 0.0049 |
| [1.5, 2] | 0.175590 | 0.1795 | 0.0040 |
All four estimates lie within 2 standard errors of the exact value.
An infinite medium. A slab of 2 cm with mirrors on both faces, , and a uniform isotropic source of one particle per history ( per cm) behaves as an infinite medium, where . With (random.Random(2)) the four bins give , , and , each with a standard error of about .
Correctness
The claim is a theorem: for an analog game, or for any fair weighted game, exactly for every , provided everywhere in the bin. The proof uses the collision density of the game: the expected number of collisions in a small volume around is , and the analog Monte Carlo proof shows that with the solution of the transport equation. Then the expected score is . In a fair weighted game the expected weight that collides in is again , and the same argument applies. The full proof is below.
The variance of one history total is finite, because the number of collisions has a geometric tail and each score is bounded by . So the error bar of line L10 is valid in the limit of large by the central limit theorem.
Proof: The collision estimator is unbiased for the average fluxfull
We assume exact arithmetic and exact independent uniform random numbers, and a transport game in which every history ends with probability 1 and the number of collisions has a finite second moment (for the analog game this is Lemma 1 of the analog Monte Carlo proof). Write for the phase space of position, direction and energy, for the total cross section at , and for a tally bin, whose spatial part has volume .
The collision density. For a history, let be the phase space points just before its collisions, with weights ( in an analog game). The weighted collision density is defined by
for every bounded measurable with support where . For an analog game, Lemma 2 of the analog proof shows that is the sum of the densities of the -th collisions and solves the integral transport equation . Its solution is , where is the angular flux of the linear Boltzmann transport equation with the same source: the collision rate density is the cross section times the flux, by the definition of the cross section. A weighted game is called fair if its weighted collision density is the same ; implicit capture and Russian roulette are fair, which is shown on their pages.
Lemma (one history). The total score of one history in the bin, , satisfies , where is the spatial part of the bin and the scalar flux, if contains all directions and energies at its points (otherwise the angular and energy parts of the bin are integrated over).
Proof. Take , which is bounded because has a positive lower bound on . Then .
Theorem (unbiased). The estimate of line L10 satisfies for every , and .
Proof. is the sum of the history totals of line L9. They are independent and have the law of , because each history uses fresh random numbers. So and , and division by gives both statements with the lemma.
Finite variance. , where is the number of collisions. In the analog game the weights are 1 and has a finite second moment, so . Then tends in law to the standard normal law by the central limit theorem, and line L10 gives a valid asymptotic error bar. The variance of must be taken over the history totals: two collisions of one history in are not independent, so the sum of the squares of single scores does not give .
Reaction rates. With the score in place of , the same argument gives , the rate of reaction in the bin.
The void. If on a part of the bin, no collision happens there, and the estimate misses over that part. The lemma needs on the bin.
The slab example. For a beam on a pure absorber with constant, the only collision is at the end of the first flight, with density . A bin then has , and the flux average is this value divided by , which is the exact value used on the page.
Complexity
The tally adds work per collision with uniform bins, or with a search over bins of any size, to the cost of the histories, where is the mean number of collisions per history. It needs space for the sums and the scores of the current history. The cost that matters is the variance per history, which depends on the problem:
- Dense regions. Many collisions per history in the bin give a low variance, often lower than the track-length estimator, because each score is small.
- Thin regions. With , only a fraction of about of the particles that cross the bin collide in it, and each score is large. The relative variance then grows like . The track-length page has a table of this effect.
Properties
- Unbiased. Exactly, for every , where .
- No geometry work. The score needs only the collision point, not the path, so it is cheap in complex geometry and needs no distances to the bin faces.
- Same histories. It uses the histories of the run as they are, so several estimators can score from one run, and their difference is a check on the code.
- Point quantities. It cannot estimate the flux at a point or on a surface, because no collision happens exactly there. Next-event estimation does this.
Variants and improvements
- Reaction rates. Score for reaction , for example for the fission source in a criticality run.
- Absorption estimator. Score at each absorption only. It is also unbiased, but it has fewer scores and a higher variance in a scatterer.
- Next-event estimator. At each collision, score the probability that the next flight reaches a point detector. It is unbiased but has an infinite variance for collisions close to the detector.
- Combined estimators. A linear combination of the collision and track-length estimates with weights that minimise the sample variance has a lower variance than either one. This is a measured gain that depends on the problem.
When to use it
Use the collision estimator in thick regions with many collisions, for reaction rates in fuel and absorbers, and in geometries where computing path lengths in tally cells is expensive. Use the track-length estimator in thin regions, in gases and in voids. In a k-eigenvalue run it is common to compute both and their combination.
Pitfalls
- Voids. In a region with there are no collisions, and the estimator gives 0 for a flux that is not 0. It is unbiased only where .
- Energy dependence. The score uses at the energy of the particle before the collision. Using the energy after the collision gives a biased result.
- The score of the weight. With implicit capture or roulette, the score uses the weight before the collision changes it.
- Per-collision variance. A variance computed from the individual scores instead of the history totals is too small when a history collides many times in the bin.
- Bin volumes. The volume in line L10 must be the true volume of the tally region. In complex geometry, a wrong or estimated volume biases the result, even if the scores are right.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/collision-estimator/. The authors are listed in prov/NDP0HX.jsonl.
"""Collision estimator of the scalar flux in a one-speed slab.
The slab is 0 <= x <= L with total and scattering cross sections sigma_t
and sigma_s (cm^-1) and isotropic scattering. It is cut into equal bins of
width dx. Each history starts one particle of weight 1 from a source and
follows it with analog Monte Carlo. At each collision in bin j the history
scores w / sigma_t into bin j. The flux estimate in bin j is
phi_j = (sum of scores in bin j) / (N * dx),
which is the average flux over the bin per source particle. The standard
error comes from the variance of the per-history scores, because the
collisions inside one history are not independent.
The faces are "vacuum" (a particle that leaves is lost) or "reflect"
(specular reflection). A reflecting slab with a uniform source behaves
as an infinite medium.
"""
import math
def uniform_open(rng) -> float:
return 1.0 - rng.random()
def beam_source(length, rng):
"""A normally incident beam on the left face."""
return 0.0, 1.0
def uniform_isotropic_source(length, rng):
"""A source uniform in x on [0, L] and isotropic in direction."""
return length * rng.random(), 2.0 * rng.random() - 1.0
def move(x, mu, s, length, faces):
"""Move a distance s from x along mu. Returns (x, mu, inside)."""
x += s * mu
if faces == "vacuum":
return x, mu, 0.0 <= x <= length
while x < 0.0 or x > length: # unfold the path by mirror reflection
if x < 0.0:
x = -x
else:
x = 2.0 * length - x
mu = -mu
return x, mu, True
def collision_estimator(n, length, sigma_t, sigma_s, nbins, rng, source=beam_source, faces="vacuum"):
"""Returns (phi, se): flux per source particle in each bin, and its standard error."""
dx = length / nbins
p_abs = (sigma_t - sigma_s) / sigma_t
total = [0.0] * nbins
total_sq = [0.0] * nbins
for _ in range(n): # @line L1
x, mu = source(length, rng) # @line L2
w = 1.0
hist = {}
while True: # @line L3
s = -math.log(uniform_open(rng)) / sigma_t
x, mu, inside = move(x, mu, s, length, faces)
if not inside: # @line L4
break
j = min(int(x / dx), nbins - 1) # @line L5
hist[j] = hist.get(j, 0.0) + w / sigma_t # @line L6
if rng.random() < p_abs: # @line L7
break
mu = 2.0 * rng.random() - 1.0 # @line L8
for j, v in hist.items(): # @line L9
total[j] += v
total_sq[j] += v * v
phi, se = [], []
for j in range(nbins): # @line L10
mean = total[j] / n
var = (total_sq[j] / n - mean * mean) * n / (n - 1)
phi.append(mean / dx)
se.append(math.sqrt(max(var, 0.0) / n) / dx)
return phi, se
test_collision_estimator.py 82 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/collision-estimator/. The authors are listed in prov/NDP0HX.jsonl.
import math
import random
import pytest
from collision_estimator import collision_estimator, move, uniform_isotropic_source
class FixedRng:
def __init__(self, values):
self.values = list(values)
def random(self):
return self.values.pop(0)
def expint(order, z, steps=2000):
"""E_n(z) = integral over u in (0, 1] of exp(-z/u) u^(n-2) du, by Simpson's rule (n >= 2)."""
if z == 0.0:
return 1.0 / (order - 1)
h = 1.0 / steps
total = 0.0
for k in range(steps + 1):
u = k * h
f = 0.0 if u == 0.0 else math.exp(-z / u) * u ** (order - 2)
total += f * (1 if k in (0, steps) else (4 if k % 2 else 2))
return total * h / 3.0
def test_one_history_by_hand():
# Beam on a slab of 2 cm, sigma_t = 2, sigma_s = 1, two bins of 1 cm.
# r = 0.5: flight ln 2 / 2 = 0.3466, collision in bin 0, score 1/2.
# r = 0.7: scatter; r = 1.0: mu = 1. r = 0.9: flight ln 10 / 2 = 1.1513, x = 1.4979.
# Bin 1 scores 1/2. r = 0.1: absorbed.
# The second history collides in bin 0 (score 1/2) and is absorbed there.
# With N = 2 and dx = 1 cm: phi_0 = 1.0 / 2 and phi_1 = 0.5 / 2.
rng = FixedRng([0.5, 0.7, 1.0, 0.9, 0.1, 0.5, 0.1])
phi, se = collision_estimator(2, 2.0, 2.0, 1.0, 2, rng)
assert phi == pytest.approx([(0.5 + 0.5) / 2, 0.5 / 2])
def test_pure_absorber_beam_exact():
sig, length, nb, n = 1.0, 2.0, 4, 20000
phi, se = collision_estimator(n, length, sig, 0.0, nb, random.Random(1))
dx = length / nb
for j in range(nb):
exact = (math.exp(-sig * j * dx) - math.exp(-sig * (j + 1) * dx)) / (sig * dx)
assert abs(phi[j] - exact) < 4.0 * se[j]
def test_reflecting_slab_is_infinite_medium():
# Source 1 per history spread over L, so Q = 1 / L; then phi = Q / sigma_a everywhere.
length, sig_t, sig_s, n = 2.0, 1.0, 0.5, 6000
phi, se = collision_estimator(n, length, sig_t, sig_s, 4, random.Random(2), uniform_isotropic_source, "reflect")
exact = (1.0 / length) / (sig_t - sig_s)
for p, e in zip(phi, se):
assert abs(p - exact) < 4.0 * e
def test_pure_absorber_volume_source_against_e3():
# phi(x) = (Q / (2 sigma)) (2 - E2(sigma x) - E2(sigma (L - x))) with Q = 1 / L.
length, sig, nb, n = 2.0, 1.0, 4, 30000
phi, se = collision_estimator(n, length, sig, 0.0, nb, random.Random(3), uniform_isotropic_source)
q, dx = 1.0 / length, length / nb
for j in range(nb):
a, b = j * dx, (j + 1) * dx
left = (expint(3, sig * a) - expint(3, sig * b)) / sig
right = (expint(3, sig * (length - b)) - expint(3, sig * (length - a))) / sig
exact = q / (2.0 * sig * dx) * (2.0 * dx - left - right)
assert abs(phi[j] - exact) < 4.0 * se[j]
def test_mirror_move():
x, mu, inside = move(0.5, 1.0, 1.0, 1.0, "reflect") # one reflection at x = 1
assert inside and x == pytest.approx(0.5) and mu == -1.0
x, mu, inside = move(0.5, 1.0, 2.0, 1.0, "reflect") # reflections at x = 1 and x = 0
assert inside and x == pytest.approx(0.5) and mu == 1.0
x, mu, inside = move(0.5, -1.0, 0.75, 1.0, "reflect")
assert x == pytest.approx(0.25) and mu == 1.0
assert move(0.5, 1.0, 0.75, 1.0, "vacuum")[2] is False
History
Collision scores are as old as Monte Carlo transport: the first neutron calculations at Los Alamos in the late 1940s counted collisions and absorptions in regions. The theory of estimators as unbiased functionals of the random walk was worked out in the 1950s and 1960s. Spanier and Gelbard gave the collision and track-length estimators side by side with their variances in their 1969 book, and Lux and Koblinger gave a general theory of estimators for transport in 1991.
References
- Jerome Spanier and Ely M. Gelbard, Monte Carlo Principles and Neutron Transport Problems, Addison-Wesley, 1969, Dover reprint 2008, the estimators of linear functionals of the flux, with the collision density as the basic quantity
- Ivan Lux and Laszlo Koblinger, Monte Carlo Particle Transport Methods, Neutron and Photon Calculations, CRC Press, 1991, the chapter on estimators, with the collision, track-length and next-event estimators
- Malvin H. Kalos and Paula A. Whitlock, Monte Carlo Methods, second edition, Wiley-VCH, 2008, Chapter 6 (radiation transport), the estimators of the flux
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag NDP0HX Edit this page History Report an error Markdown