Allen CDNE

Computational Design in Nuclear Engineering · Allen

Download the PDF

Computational Design in Nuclear Engineering, second edition, by Kenneth S. Allen, Ph.D., P.E.

Download the PDF

Chapter 4 · The Monte Carlo Method for Transport

Simple Monte Carlo Transport Example

On this page

Chapter 4 · 4 / 58 min readPDF page 114

Worked examples

Let us examine a simple gamma shielding problem to gain a better understanding of how Monte Carlo can be applied to a radiation transport problem. Figure 4.7 is an illustration of a simple shield for a beam of mono-energetic γ\gamma-rays.

Figure 4.7. Simple shielding problem of mono-energetic beam of \gamma-rays impacting a shield of thickness, t.
Figure 4.7. Simple shielding problem of mono-energetic beam of γ\gamma-rays impacting a shield of thickness, tt.

As mentioned in Chapter 1, the total probability of interaction for gamma rays per unit path length is given by the linear attenuation coefficient, μ\mu. If we analyze the number of the gamma rays that are not attenuated, NN, in a shield of thickness, t, as a function of the initial number of gamma rays, N0N_0, and the linear attenuation coefficient, we get the deterministic negative exponential function shown in Equation 4.11 which is in the same form as the basic equation for activity presented in Equation 1.1.

Equation 4.11
N=N0e−μtN = N_0 e^{-\mu t}

Using the definition of the linear attenuation coefficient, we know that the mean free path for a gamma ray is:

Equation 4.12
λ=1μ\lambda = \frac{1}{\mu}

Equation 4.11 represents the deterministic solution to the transport of uncollided gamma rays through our shield. We can perform the same calculation using the probability of interaction, μ\mu, and a set of random numbers to represent our particles to describe the distance the particle travels in our material. The mean free path, Equation 4.12, is the average distance a gamma will travel through a material prior to having an interaction and is the inverse of the attenuation coefficient. This provides avergage distance traveled, but we require a general solution. The probability density function (PDF) for our shield is probability that the first interaction of an incident gamma will take place at a depth between xx and x+dxx + dx is given by Equation 4.13.

Equation 4.13
P(x)dx=P(x)μdxP(x)dx=P(x)\mu dx

If we integrate the probability function we get the cumulative probability. We can use the cumulative probability of interaction and set that equal to a random number (R1)(R_1) between 1 and 0. The result is presented in Equation 4.14.

Equation 4.14
Pc(x)=1−e−μx=R1P_c (x)=1-e^{-\mu x}=R_1

Solving for xx (the location of the first collision) we get Equation 4.15.

Equation 4.15
x=−1μln⁡(1−R1)x=-\frac{1}{\mu} \ln{(1-R_1)}

We can now write a program that uses a random number to get a location of the first collision. We solve for the number of particles that transmit through our shield, NN, using a conditional loop, to tally the number of particles that have an uncollided distance, xx, beyond the thickness of the shield. If we use enough particles, the fraction of particles passing through the shield, N/N0N/N_0 should compare favorably to the deterministic solution from Equation 4.11. We should include a standard deviation for our solution to perform a good statistical analysis of our results.

The same approach outlined above for the basic Monte Carlo calculation of gamma shielding can also be performed for neutron shielding. For neutrons, the total macroscopic cross section, Σ\Sigma, is used in place of the total attenuation coefficient, μ\mu. In Example 4.2, a different simple Monte Carlo solution is presented where the remaining activity of a radioactive source is calculated and compared to the deterministic results in Equation 1.1.

Example 4.2

Given the half-life for a radioisotope, write a Monte Carlo program in Julia to estimate the percentage of remaining number of atoms, activity, or mass for a radioactive isotope after a given period of time, tt. The user will provide the half-life of the source, elapsed time, and the number of particles to sample for the Monte Carlo solution. Using 60^{60}Co decayed for 2 years as a test case, compare the results for sample sizes of 100, 1000, 1×105{1 \times 10^5}, and 1×106{1 \times 10^6} source particles to the deterministic solution given by Equation 1.1.

Solution: The percentage of radioactive atoms that have not decayed is equivalent to the activity in the material and its mass. This program will allow the user to multiply the results by a starting number of atoms, activity, or mass and determine the remainder. Our solution depends on the decay constant, λ\lambda, for the isotope which is:

λ=ln⁡2T1/2\lambda =\frac{\ln{2}}{T_{1/2}}

To begin the Monte Carlo solution we first develop the probability density function (PDF). The PDF for our decay problem is probability that the day will take place at a time between tt and t+dtt + dt is given by:

P(t)dt=P(t)λdtP(t)dt=P(t)\lambda dt

If we integrate the probability function we get the cumulative probability. We can use the cumulative probability of decay and set that equal to a random number (R1)(R_1) between 1 and 0.

Pc(t)=1−e−λt=R1P_c (t)=1-e^{-\lambda t}=R_1

Solving for tt (the time for decay) we get:

t=−1λln⁡(1−R1)t=-\frac{1}{\lambda} \ln{(1-R_1)}

Using the approximation given by Equation 4.10, the standard deviation is given by:

σ=1N\sigma = \sqrt{\frac{1}{N}}

Now that we have the governing equation for the Monte Carlo solution, we will use the steps outlined in Chapter 2 for the development of a computer code to write the solution.

1. Map out the algorithm steps using a logical flowchart.

The following flowchart includes an extra check in the beginning of the code that verifies the units for the elapsed time and the time entered for the half-life are the same. This is essential as the units must cancel out. By using a conditional, the code will check the units and then put everything into seconds. We could have chosen any unit of time as it will cancel in the calculation.

In addition to the conditional on the entry, the code requires a loop to iterate each particle. In this process, the code will select a random number and then test it in the equation we solved for t above to determine if it is longer than the elapsed time for the test.

The parallel process in the code will use the deterministic equation derived from Equation 1.1 to calculate the value for the percent decayed. This will be used in the final process that will compare the Monte Carlo and deterministic solutions to each other.

Example 4.2, listing 1

2. Write a meaningful header with appropriate comments. Open Visual Studio Code and create a new file named “ActivityMC.jl” and start with comments and include the packages necessary for program.

Julia — Example 4.2, listing 2
#|Function to estimate the percentage of undecayed mass or activity |
#|of a radioisotope using a Monte Carlo approach and comparing      |
#|to a deterministic solution                                       |
#|                                                                  |
#|Written by: Kenneth Allen 18 January 2023                         |
#|------------------------------------------------------------------|
# Load Modules necessary
using IJulia
using Printf
# Set initial conditions for counters
#-------------------------------------------------------------------|

3. Declare constants and variables. This problem uses three counters within the Monte Carlo conditional statement. One for the while loop counter, and two for the tallies for the particles that have decayed. These counters are iterated from an initial condition of 0.0 specified here. Because the final standard deviation calculation uses the approximation presented in Equation 4.10, the sumsq value does not contribute to the final results and could be eliminated from the program entirely.

Julia — Example 4.2, listing 3
# Set initial conditions for counters
#-------------------------------------------------------------------|
sumsq = 0.0;
npi = 0;
k = 0;
# NOTE using iJulia, can assign Greek letters as variables by typing
# a \ followed by the phonetic name and then ctrl+space  such as
#   \lambda

4. Write a routine to receive input. For this program, the user will specify the half-life and its units, the elapsed time and its units, and the number of trials for the Monte Carlo estimate calculation.

Julia — Example 4.2, listing 4
#           Write a routine to request input
#-------------------------------------------------------------------|
# Request the half-life of the radioisotope
println("Enter the half-life of the radioistope:\n\n")
halflife = readline()
halflife = parse(Float64, halflife)

#Request the units for the half-life
println("Enter the units for half-life as s, m, h, d or y:\n\n")
units = readline()

# Request the elapsed time for decay
println("Enter the elapsed time of decay:\n\n")
T = readline()
T = parse(Float64, T)

#Request the units for the elapsed time
println("Enter the units for elapsed time as s, m, h, d or y:\n\n")
units_T = readline()

# Request the number of trials for the Monte Carlo calculation
println("Enter the number of trials:\n\n")
n_trials = readline()
n_trials = parse(Float64, n_trials)

5. Write routines and subroutines to execute desired calculations. Referring to the flowchart, this section begins with a request for two times followed by two conditional if, elseif, and else routine to determine the units for each and to covert each into seconds. The program then calculates the decay constant and uses a while loop to perform the Monte Carlo estimate. When the while loop finishes, it uses Equation 4.10 to estimate the standard deviation and then calculates the deterministic solution and qualitatively compares the two results.

Julia — Example 4.2, listing 5
#  Write routines and subroutines to calculate results
#-------------------------------------------------------------------|
# Check and convert half-life to seconds for  calc
if (units == "s")
    halflifec = halflife
elseif (units == "m")
    halflifec = halflife*60
elseif(units == "h")
    halflifec = halflife*3600
elseif(units == "d");
    halflifec = halflife*3600*24
elseif(units == "y");
    halflifec = halflife*3600*24*365
end # end if conditional
Julia — Example 4.2, listing 6
#Calculate decay constant in inverse units of time
λ = (log(2))/halflifec

# Check and convert elapsed time to seconds for calc
if (units_T == "s")
    Tc = T
elseif (units_T == "m")
    Tc = T*60
elseif(units_T == "h")
    Tc = T*3600
elseif(units_T == "d");
    Tc = T*3600*24
elseif(units_T == "y");
    Tc = T*3600*24*365
end # end if conditional

#Set up while loop to calculate number of atoms
while k <= n_trials
    R = rand()
    t=(-1/λ)*log(1-R)
    if(t>=Tc)
      global npi=npi+1
      global sumsq+=1
    end
    global k+=1
end

#Calculate the Monte Carlo Estimate for percent decayed
percentMC = npi/n_trials

#Calcualte standard deviation of Monte Carlo estimate
standev =  Float64(sqrt(1/n_trials))

#Calcualte the deterministic solution for percent decayed
percentDET = exp(-(λ)*Tc)

#Calcuate difference between solutions
diff = 100 * abs(percentMC-percentDET)/percentDET

6. Write routine that handles output. This program writes the results to the screen. It uses the @print function to limit the number of decimal points in the float numbers to 4 decimals using "%.4f". This makes the results easier to read for the user.

Julia — Example 4.2, listing 7
# Write a routine that handles output
#-------------------------------------------------------------------|
#declare funtction that represents decay over time
println("Using ", n_trials,
" trials, the final estimate for the percent decayed is ")
@printf("%.4f", percentMC)
print("\n")
println(" with a standard deviation of +- ")
@printf("%.4f", standev)
print("\n")
println("The deterministic solution is ")
@printf("%.4f",percentDET)
print("\n")
println("The difference between the two values is ")
@printf("%.4f", diff)
print(" %")

Now that the entire program, MCActivity.jl, is written, it can be executed. To run the program within Visual Studio Code, use ALT+J+O to bring up the REPL terminal within Visual Studio Code. At the Julia> prompt enter include("MCActivity.jl") which will start the program. HINT: when writing the filename in the command line, start with include("M and then press the tab key. Julia will recognize the file you intend to access and will auto-complete the line. When the file opens answer the prompts. For this example, the entries for 60^{60}Co are half-life of 1925.28 days and elapsed time of 2 years with the number of trials specified in the problem statement.

N TrialsFraction DecayedStandard DeviationPercent Difference
1000.8000.10004.0472%
10000.79400.03163.2668%
1E50.76770.00320.1576%
1E60.76880.00100.0113%

The results from the program support the accuracy of the Monte Carlo estimate and the trend of the reduction of the Standard Deviation and the convergence between the solution as the number of trials increased. Because of the simplicity of the program, there is not a noticeable difference in computational time between 100 and 1E6 trial.

Computational Design in Nuclear Engineering, second edition, by Kenneth S. Allen, Ph.D., P.E. — the complete book as a 394-page PDF.

Download the PDF