Writing codes that can read data, perform calculations on that data, and write or plot output is an essential skill for engineering. It is common for the output from nuclear engineering codes to contain large amounts of data in a variety of tables in a plain text format. As we have seen, this is the case for SCALE and MCNP. If a user only needs to find and record one or two data points from a single output file, it makes sense to use a text editor, like Textpad, to open the output file and search for the required information. This is the procedure we have used in the examples in this text to this point. As described in the previous section, the process for collecting and sorting the data from a lattice from a SCALE NEWT output file is challenging and prone to error. Other situations occur in codes, like MCNP, where the data is spread over many tables or pages, and it is not easily extracted manually. For example, the production of actinides and fission products throughout the burnup of a fuel rod or assembly requires the user to search through pages of data for the desired output at each timestep in the process.
When the process of extracting and recording the data from an output file becomes difficult, tedious, or repetitive, the user should look to write a parser program. The parser program reads the output file, extracts the data, arranges, or sorts the data into a format the user can use, and, if desired, plots the results. A parser can be written in most any computer language. For this book, we will continue to use Julia as our language. The steps can be followed and adjusted in another interpreted code, like Python, or in a compiled code, like C++. The parser example in Example 8.4 is versatile as it will plot the flux or fission power for any rectangular or hexagonal assembly from SCALE NEWT. It also writes a comma separated variable (CSV) file in the process. This file could be saved, and the user could manipulate or plot using a spreadsheet program. The parser program can be improved or modified by including other calculations. For example, the program could calculate the peak-to-average power for each fuel element and plot that quantity. Typically, the peak-to-average power must remain below an upper limit within a reactor design to ensure there are not hot spots where local fuel element failure can occur.
Example 8.4
Write a parser program in Julia that reads in a SCALE NEWT output file for either a rectangular or hexagonal fuel assembly and generates a surface plot of either the flux or fission power.
Solution: To begin the process of writing a parser, the user needs to identify lines or characters within the output file that serve as landmarks within the file to find the desired data. For our parser, we will refer to the line that starts the data table as the “keyline” and the line that terminates the data table as the “stopline”. Different codes will read in text files in different ways and the process of extracting data may be different. For example, C++ will read in a line of text and numbers will be read as numbers. Also, the user can specify the code to read to a particular column within C++ and then read a number into a array. This process is especially effective to retrieve information and requires little sorting or manipulation. Unfortunately, Python will read in an entire line of text and will, by default, read the information as a string. The code will require a second step that divides the string into individual words and then the words must be reassigned to an appropriate number format, such as an integer or float. Julia also reads in a line of code and can be written, like Python, to parse the line into an array. However, Julia has a more powerful method to get the results we want. In Julia, we can read the table data we want, write that table to a text file, and then catenate, or combine, the table with another table in a dataframe. The dataframe converts the data to numbers and allows for sorting and query functions like a spreadsheet. The dataframe can also be easily plotted.
This program follows the same method used for programming explained in Section 2.5.
Write out the algorithm using a logical flowchart.

Write a meaningful header with appropriate comments. Open Visual Studio Code and create a new file named “Parser1.jl” and start with comments and include the packages necessary for program.
Julia — Example 8.4, listing 1#= **************************************************************************** Julia parser program Purpose to develop program to read plain text input file and retrieve values Written 21 Feb 2023 Dr. Kenneth Allen Program reads SCALE ouput files for assembly models from either triangular pitch or square pitch assemblies. It searches for the geometry specifications for X and Y locations of fuel and the assoicated flux or fission power and plots the results. ALT+J+O include("Parser1.jl") ****************************************************************************=# #Load packages needed using DataFrames using DelimitedFiles using Query using Plots; gr() using CSVDeclare constants and variables. The program will have two keylines representing the unique lines in the output file to mark the start of the geometry and zone tables. These lines are text and must be exactly as they are presented in the output file. This means the line cannot have more or less spaces between the words etc. A technique is to cut and paste directly from the output file to the VSC editor and surround by quotes. While the keyline is unique to the output file, the stopline does not have to be. The stopline is a line that follows the keyline that, when read, terminates the loop. The stopline text can occur in other places within the file and it will not trigger action unless the keyline has been read and the loop is initiated reading in data. The other items in this block are counting variables initialized to zero.
Julia — Example 8.4, listing 1# Set constants used in the function #----------------------------------------------------------------------------| keyline = " Type Unit Number nx ny x y A B Angle Information" stopline = "1 *****************************" keyline2 = "No. No. Flux Fission Power Volume" stopline2 = "module xsproc is finished. completion code 0." line_count = 0 instance = 0 instance2 = 0 countblock = 0 countblock2 = 0Write a routine to receive input. This program will ask the user for the SCALE output filename, the unit for the fuel rod, the mixture for the fuel, whether the assembly is rectangular or hexagonal, and a name for the type of assembly which will be displayed on the chart title. The majority of the input are strings with the exception of the material and zone numbers which are parsed into integers.
Julia — Example 8.4, listing 1# Write a routine to request input #----------------------------------------------------------------------------| # Request name of SCALE ouput file # Request compression factor for problem println("Enter the name of the SCALE output file:") F = readline() # opening a file in read_mode # r is the default mode f = open(F, "r") # Request type of assembly, hex or rectangular println("Enter type of array cub for rectangular or hex for hexagon:") arraytype = readline() # Request zone number of fuel println("Enter unit number of fuel from SCALE geometry:") ut = readline() ut = parse(Int64,ut) # Request mixture number of fuel println("Enter mixture number of fuel from SCALE mixing table:") mat = readline() mat = parse(Int64,mat) # Request to plot either Flux or Fission Power println("Enter either Flux or Power to plot:") type = readline() # Request to name of assembly type println("Enter type of assembly:") title = readline()Write routines and subroutines to execute desired calculations. Referring to the logical flowchart, there are two major loops to gather the data from the output file. The program will first open the file in the read mode. It then then proceeds to go line-by-line to search through the file to find the first keyline. When found, it will go into a loop that will continue until the stopline if found. Within this loop, it will read the line and write it to separate text file. When the stopline is found, it continues to search for the second keyline and completes the same operation for the zone data.
The use of global for the counter variables allows for the counters to be updated in multiple loops. As a technique, the global variables have separate names for the two major loops associated with the geometry table and the zone data table. However, because they are reset after the stopline, they could be reused in the second loop.
Julia — Example 8.4, listing 1# Write routines and subroutines to calculate results #---------------------------------------------------------------------------| # Open the file and gather the data needed for lines in readlines(f) #read in the output file and find the geometry block if contains(lines, keyline) #finds geometry global instance += 1 end if instance > 0 if lines != stopline #reads until stop global countblock +=1 if countblock > 2 #prints data to file open("geodata.txt", "a") do file write(file, lines, "\n") end end elseif lines == stopline #stopline terminates instance = 0 end end #concludes gathering of geometry in separate file #read in the output file and find the fission and flux if contains(lines, keyline2) #finds zone data global instance2 += 1 end if instance2 > 0 if lines != stopline2 #reads until stop global countblock2 +=1 if countblock2 > 2 #prints data to file open("zonedata.txt", "a") do file write(file, lines, "\n") end end elseif lines == stopline2 #stopline terminates instance2 = 0 end end #concludes gathering of zone data in separate file # increment line_count global line_count += 1 end close(f)The next step to compiling the data involves reading the two text files created for the tables into a delineated array. This process begins by reading the text file into an array named A for the geometry data. This array is then placed in a DataFrame. The DataFrame can then be queried to sort out the X and Y data only associated with the unit number of the fuel elements. The same process is applied to the zone data table by sorting out only the elements that have the fuel mixture and a power greater than zero.
Julia — Example 8.4, listing 2# Take the gathered data and arrange in a format that can be sorted #reads geomtry data into an deliminated array A = readdlm("geodata.txt") header=["Type", "Unit", "Number", "nx", "ny", "x", "y", "A", "B", "Angle"] rawdata = DataFrame(A, header ) data = identity.(rawdata) q1 = @from i in data begin @where i.Type ==arraytype && i.Unit == ut @select {i.x, i.y} @collect DataFrame end #reads zone data into a delimited array B = readdlm("zonedata.txt") header2=["Zone", "Mix", "Flux", "Power", "Volume"] rawdata2 = DataFrame(B, header2 ) data2 = identity.(rawdata2) q2 = @from i in data2 begin @where i.Mix == mat && i.Power > 0 @select {i.Flux, i.Power} @collect DataFrame endWrite routine that handles output. The program will then delete the text files as they are no longer needed. The program then combines the two dataframes into a single dataframe. The catenate function completes the combination.
To get the program to properly present the surface plot, values must be assigned to the borders of the plot. The irregular surface plot will attempt to average unspecified z-axis values between known values. With the border, the values will go off in unusual directions when not available. This is especially confusing with a hexagonal lattice. The working area for the plot is rectangular between X and Y and there is a significant lack of data points on the corners of the hexagon.
Because the program needs to be useful for either rectangular or hexagonal lattices, the parser will need to have two routines for addressing the borders. The method for solving the plot borders starts by completing a query in the combined dataframe to look for the maximum and minimum values for the flux and power.
The program then uses the type of lattice (hex or cuboid) to either apply a minimum value of zero for the hexagonal lattice or use the minimum value from the dataframe for the rectangular lattice.
The program concludes by transferring the dataframe information to a CSV file and then plotting that information with the name of the assembly the user provides.
Julia — Example 8.4, listing 1# Complete the sort and combine files and plot results #delete text files that held data rm("geodata.txt") rm("zonedata.txt") #combine two DataFrames df1 = hcat(q1, q2) maxxvalue = maximum(df1[!,:x]) minxvalue = minimum(df1[!,:x]) maxyvalue = maximum(df1[!,:y]) minyvalue = minimum(df1[!,:y]) minFluxvalue = minimum(df1[!,:Flux]) minPowervalue = minimum(df1[!,:Power]) minx = minxvalue-1 miny = minyvalue-1 maxx = maxxvalue+1 maxy = maxyvalue+1 if type == "Flux" && arraytype == "cub" minz = minFluxvalue elseif type == "Power" && arraytype == "cub" minz = minPowervalue else minz = 0.0 end C = [minx miny minz minz; minx maxy minz minz; maxx miny minz minz; maxx maxy minz minz] header3 =["x","y","Flux","Power"] borderdata = DataFrame(C, header3) df = vcat(df1,borderdata) CSV.write("plotdata.csv", df) x = CSV.read("plotdata.csv", values, select=[1]) y = CSV.read("plotdata.csv", values, select=[2]) if type == "Flux" z = CSV.read("plotdata.csv", values, select=[3]) elseif type == "Power" z = CSV.read("plotdata.csv", values, select=[4]) end plt = surface(x, y, z, c = :rainbow, alpha = 0.9, title="$type Across $title Assembly", zaxis= "Relative $type", size = (900, 700)) display("image/png",plt)
The following plots are power and flux plots for a subcritical assembly that is a cylindrical core with a hexagonal lattice of fuel rods. Within the assembly, there are five non-fuel locations. The power plot shows the location of those rods as they do not have fuel to produce power. The plot representation is as expected with the peak at the center for flux and power tapering uniformly across the core.


This video shows the parser program developed in Example 8.4 being used for a hexagonal and rectangular assembly.
This video details the use of a parser written in Julia to plot the flux from F4 tallies from either a hexagonal or rectangular lattice output in MCNP.
