Programming in Julia is like programming in any computer language with the added features of having built-in functions available to assist with calculations. One of the first steps to programming is to declare variables and constants. Some constants are built within Julia such as (pi). In Julia, it is written as either “pi” or the TeX completion of \ pi followed by the tab which will appear as the Greek letter. To declare a constant, write the name for the constant and assign it a value (such as c = 2.99792E8). To declare a variable, give a name and use it in an expression. You can also initialize it by assigning it a number (x=1) which will assign an integer to the variable x. You can also use an array for a variable with multiple values. For example, (A = Array{ Float64, 1}(undef, 3)) is an array with three values that are all initialized to an undefined value. The value will change to when numbers are read into the array and they will take on a Float64 format. Information on integers and floating-point numbers can be found at https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers.
A key to programming a working algorithm is the use of loops and conditional statements. Loops are operations that operate over a set number of iterations or execute if a condition is either met or not met. In Julia, there are two primary loop operations – “while” and “for”.
The while command repeats the series of operations until a termination condition is met. For example:
Energy=0.001; #initialize the value for variable x
m= 1.6750E-27; #constant mass of a neutron in kg
eV= 1.60218E-19; #constant eV=1.60218E-19 Joules
while Energy < 0.1
v=sqrt((2*Energy*eV)/m) #energy to velocity
sigma_slope=1/v #assign value to sigma
global Energy=Energy+0.001 #iterate energy by 0.001eV
endThis example will start with a value of 0.001 eV and then converts that value to a velocity using the mechanics equation for kinetic energy; . It then uses the velocity to calculate the slope of the microscopic cross section . After it passes the value of the slope to the variable sigma_ slope, the energy value is increased by 0.001 eV. The loop will continue to calculate and pass values for the slope until E = 0.1 eV when it will terminate.
The for loop repeats a series of operations between the command for and the termination command end. For example:
for ii = 1:4 #index ii in range object from 1 to 4
mu = cos(ii*pi/2)
println(mu)
end
6.123233995736766e-17
-1.0
-1.8369701987210297e-16
1.0This loop iterates the cosine of the angle () from angles of to radians and prints the results. It will terminate after the four values are calculated.
Conditional statements allow for the computer to decide an action based on a true or false relationship. The format for the conditional construct uses if, elseif and else. As with the loops, the conditional statements terminate with an end. The conditional allows for a comparison and multiple outcomes based upon an initial value. For example:
if Energy <= 1E-7
Sigma_Region = ‘thermal’
elseif 1E-7<E<=1E-2
Sigma_Region = ‘resonance’
else
Sigma_Region = ‘fast’
endThis example compares the given energy in MeV to three different ranges – less than or equal to ; greater than and less than and any other value. The third condition could have also used an elseif with a condition of E > 1E-2.
A program will often nest loops and conditional statements within each other. For instance, a program may have a conditional that, when true, causes a looped calculation to be performed.
A general programming procedure for setting up and developing a computer program follows:
Map out the algorithm steps using a logical flowchart. Refer to the symbology in Table 2.1 and create a visual map of the logic flow for the program.
Write a meaningful header with appropriate comments. Comments in Julia are preceded with a # symbol. The Julia editor will make the text following the #
teal blueand in italics indicating that it won’t be executed. In-line comments are also inserted using the same syntax. Detailed comments to start the program and throughout the body of the program are essential for good programming. Comments not only allow others to understand the steps used in the program, but also reminds the programmer in the future what he or she wrote and why. You can use a #= to start a block of comments and close with a =# and everything in between over multiple lines will not be executed. This is especially useful if you want to make a header or need to comment out many lines of code.Declare constants and variables. If the programming language requires, as in C++, initialize all variable types.
Write a routine to receive input. This can be reading in values from a file or requesting input from a user. Input is then stored for use.
Write routines and subroutines to execute desired calculations. Depending on the program, it may be beneficial to write subroutines for calculations that are performed multiple times. A subroutines is just a short program that the main program calls. It can greatly shorten and increase the efficiency of the program. For example, a program might perform a statistical check on the results of several different calculations. In this case, it makes sense to write a subroutine that performs the statistical check and calls it each time it is needed versus writing a separate statistical check after each calculation. Subroutines are also useful because they can be easily transferred from one program to another.
Write routine that handles output. This may be writing the output to a file, printing it to a screen, creating an array that can be used in another program, or plotting the results. Within Julia, the manipulation and plotting of results can be either written directly into a script or written separately in the command line.
These steps are followed in Example 2.2 which outlines a Julia program for plotting the Klein-Nishina differential gamma ray scattering cross section.
Example 2.2
The differential scattering cross section for Compton scattered photons depends upon the initial energy of the incident photon and the angle of the scattered photon. The cross section is given by the Klein-Nishina formula (Weinberg; 2011):
For an incident photon of energy ,the differential cross section is:
Where is the differential cross section with respect to solid angle, is the fine structure constant (1/137.04), is the scattering angle, is the reduced Compton wavelength of the electron (0.38616 pico-meters), and is the ratio of the photon energy after and before the collision given by:
Where is the rest mass energy of an electron (511 keV).
Write a Julia program that plots the differential scattering cross section for all values of scattered angle (= 0 to 2) for up to 5 user-supplied incident photon energies (). Compare the results to other published examples. Based on the plot, what conclusions can be drawn about the angular probability of a Compton scattered photon as a function of energy?
Solution: For this program example, we will follow the steps for programming outlined earlier.
1. Map out the algorithm steps using a logical flowchart.

2. Write a meaningful header with appropriate comments. Open Visual Studio Code and create a new file named “Klien Nishina Plot Program.jl” and start with comments and include the packages necessary for program.
#=Differential Klien Nishina Plot Program
|------------------------------------------------------------------|
|Function to plot the Klein-Nishina differential cross section for |
|for Compton Scattered photon per steradian for up to 5 different |
|user-supplied energies in keV |
|Common energies are 2.75E-3, 60, 511, 1460, & 10000 keV |
|the user can choose any energy |
|Written by: Kenneth Allen 5 December 2022 |
|------------------------------------------------------------------|=#
using IJulia
using Plots3. Declare constants and variables. Note use of comments.
#-------------SET CONSTANTS USED IN PROGRAM-------------------------
#Reduced Compton wavelength r(c)/hm(e)c
r = 0.38616E-12 # 0.3861 pico meters
#Fine structure constant (~1/137.04)
α = (1/137.04) #Using IJulia, can get Greek by \<lettername> + tab
#Electron rest mass energy
mc² = 511 # 511 keV
#Angle theta from zero to 2π in 500 increments for plotting
θ = range(0, stop=2π, length=500)
#Create a counter and intialize to zero
k = 1;
E = Float64[] #create an empty vector to pass energies into4. Write a routine to receive input. Because this is a simple script, we can request the user supply several energies (up to 5 total) to plot and read those values one by one into the plotting routine. A better approach would have been to enter the values into an array and use that array as the input for the plotting function. This program does not do that to demonstrate the conditional and loop operations. Also, for simplicity, this program does not perform an error check on the user’s input. For example, the user does not receive a message that an invalid entry is given if they provide values for the number of energies that are less than 1 or greater than 5. A simple conditional statement can be added to include an error check.
#---------------REQUEST USER INPUT---------------------------------
#Request number of energies to plot
print("Enter number of energies to plot (up to 5):\n\n")
# Calling rdeadline() function
nₑ = readline()
nₑ = parse(Int64, nₑ )
while k <= nₑ
print("Enter energy of the incident photon in kev:\n\n")
Evalue = readline()
Evalue = parse(Float64, Evalue)
# Pass values into vector
push!(E, Evalue)
global k += 1
end5. Write routines and subroutines to execute desired calculations. Referring to the flowchart, this section begins with a loop. The loop will first determine how many iterations of the calculation to perform. This example uses a while loop that repeats the calculation for each user-supplied energy up to the number entered. For each step, the program will prompt for an energy and then generate a plot. The plotting routine uses a conditional if, elseif, and else routine to determine how many energies to plot based on the user's input.
#--------PROCESS DATA AND PLOT RESULTS----------------------------
# Set up conditional to handle 5 different cases
if nₑ == 1
# The differential Klien Nishina scattering cross section equation
ρ(θ,E) = (α^2)*(r^2)*((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^2)*(((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))+
((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^-1.0)-1.0+(cos(θ))^2)/2)
# plot RESULTS
plot([(θ, ρ.(θ,E[1]))], proj=:polar )
elseif nₑ == 2
# The differential Klien Nishina scattering cross section equation
ρ(θ,E) = (α^2)*(r^2)*((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^2)*(((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))+
((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^-1.0)-1.0+(cos(θ))^2)/2)
# plot RESULTS
plot([(θ, ρ.(θ,E[1])),(θ, ρ.(θ,E[2]))], proj=:polar )
elseif nₑ == 3
# The differential Klien Nishina scattering cross section equation
ρ(θ,E) = (α^2)*(r^2)*((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^2)*(((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))+
((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^-1.0)-1.0+(cos(θ))^2)/2)
# plot RESULTS
plot([(θ, ρ.(θ,E[1])),(θ, ρ.(θ,E[2])),(θ, ρ.(θ,E[3]))], proj=:polar )
elseif nₑ == 4
# The differential Klien Nishina scattering cross section equation
ρ(θ,E) = (α^2)*(r^2)*((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^2)*(((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))+
((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^-1.0)-1.0+(cos(θ))^2)/2)
# plot RESULTS
plot([(θ, ρ.(θ,E[1])),(θ, ρ.(θ,E[2])),(θ, ρ.(θ,E[3])),(θ, ρ.(θ,E[4]))], proj=:polar )
elseif nₑ == 5
# The differential Klien Nishina scattering cross section equation
ρ(θ,E) = (α^2)*(r^2)*((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^2)*(((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))+
((1.0/(1.0+(E/mc²)*(1.0-cos(θ))))^-1.0)-1.0+(cos(θ))^2)/2)
# plot RESULTS
plot([(θ, ρ.(θ,E[1])),(θ, ρ.(θ,E[2])),(θ, ρ.(θ,E[3])),(θ, ρ.(θ,E[4])),(θ, ρ.(θ,E[5]))], proj=:polar )
else
print("Too many energies selected")
endThis conditional is added for demonstration purposes and would not be necessary if an array was used to store the results from the differential cross section calculations and then the plot was called one time. In that case, Julia would assign a different color to each data series in the plot.
6. Write routine that handles output. In this example, there is no requirement to make a separate routine for the output. The program plots the results as soon as they are calculated and terminates when all of the user-supplied values are plotted.
Now that the entire program, Klien Nishina Plot Program.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("Klien Nishina Plot Program.jl") which will start the program. HINT: when writing the filename in the command line, start with include("K 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:
Enter energy of the incident photon in kev:
60
Enter energy of the incident photon in kev:
511
Enter energy of the incident photon in kev:
1460
Enter energy of the incident photon in kev:
10000The plot will open in another window within VSC as:

Now the results of the program plot can be analyzed. Several references have similar plots for comparison. When compared to Figure 2.19 in John Knoll’s Radiation Detection and Measurement, the Julia plot matches the published result. (Knoll; 2020)
So, what does the plot tell us about the Compton cross section? By looking at the plot, we can see that at low energies, the Compton scattered photon is just as likely to scatter backwards as it is forwards. However, as the incident photon energy increases, the cross section becomes more forward-peaked meaning backscatter is less probable. This is important when we perform shielding and detection calculations.
References
Full bibliography- Knoll, G. F. (2020). Radiation Detection and Measurement, John Wiley. https://books.google.com/books?vid=ISBN9780470131480
- Weinberg, S. (2011). The quantum theory of Fields, Cambridge Univ. Press. https://openlibrary.org/works/OL3398955W/The_Quantum_Theory_of_Fields
