Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Photon Transport Simulation with Two Layers of Material & Unstructured Mesh #27

Open
wants to merge 84 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
23c1469
Add files via upload
anu1217 Oct 4, 2024
8d1f88b
Deleted unused Energy_List lines
anu1217 Oct 4, 2024
d7e58a3
Update OpenMC_PhotonTransport.py
anu1217 Oct 10, 2024
39e2d4b
Delete unused 'Energy_List.txt' line
anu1217 Oct 10, 2024
3dfb567
Delete print statement
anu1217 Oct 10, 2024
5bb02ff
Remove summed strengths text file as output
anu1217 Oct 10, 2024
1fdb1b1
Read summed photon strengths from Source_Mesh_Reader
anu1217 Oct 10, 2024
569eb28
Add source strengths for each individual mesh element
anu1217 Oct 10, 2024
1008270
Update OpenMC_PhotonTransport.py
anu1217 Oct 15, 2024
19e86ef
Adding materials/geometry files
anu1217 Oct 15, 2024
6fa1367
Import materials/geometry from TwoLayers_Materials & TwoLayers_Geometry
anu1217 Oct 15, 2024
c3cf5db
Delete materials and geometry functions from current script
anu1217 Oct 15, 2024
e217e00
Update OpenMC_PhotonTransport.py
anu1217 Oct 25, 2024
94519f9
Update TwoLayers_Materials.py
anu1217 Nov 8, 2024
3854660
Update TwoLayers_Materials.py
anu1217 Nov 11, 2024
a3db102
Update references to some variables
anu1217 Nov 11, 2024
4199c1b
Update TwoLayers_Geometry.py
anu1217 Nov 11, 2024
fdcb11f
Import variables from TwoLayers_Materials
anu1217 Nov 12, 2024
2e209a6
Update directory of OpenMC_PhotonTransport
anu1217 Nov 14, 2024
e8a305a
Update directory of Photon_Tallytovtk
anu1217 Nov 14, 2024
3aa85b3
Update directory of TwoLayers_Geometry
anu1217 Nov 14, 2024
1cff395
Update directory of TwoLayers_Materials
anu1217 Nov 14, 2024
64d14d5
Update directory of Source_Mesh_Reader
anu1217 Nov 14, 2024
88a9e83
Update TwoLayers_Geometry.py
anu1217 Nov 14, 2024
2259f79
Add functions to read, plot, and save data
anu1217 Nov 14, 2024
84bac07
Fix indentation of return statement
anu1217 Nov 15, 2024
0713d73
Created functions to extract and write relevant data
anu1217 Nov 15, 2024
5c03a21
Define arrange_source_strengths function
anu1217 Nov 15, 2024
d761b1c
Add main function that calls all helper functions
anu1217 Nov 15, 2024
fd3cb83
Rewrite make_source in terms of data_extractor from Source_Mesh_Reader
anu1217 Nov 15, 2024
ed0e05d
Fix summation in make_source loop
anu1217 Nov 15, 2024
c7a0c03
Changed variable name in function definition
anu1217 Nov 15, 2024
4568353
Updated main function that calls helper functions
anu1217 Nov 15, 2024
f91820f
Rename write_source_density
anu1217 Nov 19, 2024
2347332
Rewrite extract_source_data
anu1217 Nov 19, 2024
d3b612e
Change extract_source_data docstring
anu1217 Nov 19, 2024
99af15a
Moved Particle_Filter to tallies
anu1217 Nov 19, 2024
bc4e8d3
Add inner_radius as input
anu1217 Nov 19, 2024
e06fba5
Change inputs required for make_source
anu1217 Nov 19, 2024
7afd568
Remove Mesh_Dist as output of make_source
anu1217 Nov 19, 2024
2b5e67b
Change inputs required for tallies & fix capitalization
anu1217 Nov 20, 2024
4fb3156
Change variable names from uppercase to lowercase
anu1217 Nov 20, 2024
c81e21f
Rewrite export_to_xml as create_openmc_model
anu1217 Nov 20, 2024
1ae47ce
Deleted comments describing self-explanatory code
anu1217 Nov 20, 2024
c3509d6
Add docstrings to functions
anu1217 Nov 21, 2024
6676191
Add input to tallies function
anu1217 Nov 21, 2024
e6c9518
Changed bounds to numpy array
anu1217 Nov 25, 2024
3a09bf8
Change the way upper & lower energy bounds are accessed
anu1217 Nov 26, 2024
0d0418b
Fixed typo in make_source
anu1217 Nov 26, 2024
93d5b56
Add inputs to yaml file
anu1217 Nov 26, 2024
80382a4
Update and rename PhotonTransport_Inputs.yaml to WC_Layers/PhotonTran…
anu1217 Nov 26, 2024
ab1b09b
Remove hard-coded inputs that have been moved to yaml
anu1217 Nov 26, 2024
43270df
Merge branch 'cnerg:main' into mesh_photontransport
anu1217 Nov 27, 2024
8aff672
Add information to calculate effective dose
anu1217 Dec 16, 2024
0679a68
Redefine inputs for make_spherical_shells
anu1217 Dec 16, 2024
fc583b1
Edit function docstring
anu1217 Dec 16, 2024
785511e
Changed inputs for extract_source_data
anu1217 Dec 16, 2024
ee8df5b
Add main function to Source_Mesh_Reader
anu1217 Dec 16, 2024
790db58
Add default to argparse input
anu1217 Dec 16, 2024
3b808c1
Add files via upload
anu1217 Dec 16, 2024
052b312
Merge branch 'cnerg:main' into mesh_photontransport
anu1217 Dec 16, 2024
771b147
Add input to function
anu1217 Dec 16, 2024
7f76500
Add docstrings to functions
anu1217 Dec 16, 2024
2a7d52c
Change variable names and inputs
anu1217 Dec 16, 2024
6b18246
Change variable key
anu1217 Dec 16, 2024
a3a9921
Update PhotonTransport_Inputs.yaml
anu1217 Dec 16, 2024
608adba
Fix typo in run mode
anu1217 Dec 16, 2024
15f64ef
Update and rename Source_Mesh_Reader_Inputs.yaml to WC_Layers/Source_…
anu1217 Dec 16, 2024
8af7f12
Fix variable updating to avoid seg fault
anu1217 Dec 20, 2024
8aad344
Add main() function to compile all functions
anu1217 Dec 20, 2024
3c272b8
Calculate dose and add main() function
anu1217 Dec 20, 2024
95b52b1
Update PhotonTransport_Inputs.yaml
anu1217 Dec 20, 2024
2dde9f3
Update Source_Mesh_Reader.py
anu1217 Dec 20, 2024
d07dd9d
Create README.md
anu1217 Dec 20, 2024
da12a00
Update and rename WC_Layers/Conversion_h5m.py to WC_Layers/R2S_Step1_…
anu1217 Dec 20, 2024
cc2dad7
Update and rename WC_Layers/PyNE_Lib.py to WC_Layers/R2S_Step1_Input/…
anu1217 Dec 20, 2024
589bff0
Modularize main() and change total_mesh to unstructured_mesh
anu1217 Jan 8, 2025
d813414
Modularize main()
anu1217 Jan 8, 2025
2ae754a
Modularize main()
anu1217 Jan 8, 2025
9e8bb76
Change inputs for make_spherical_shells()
anu1217 Jan 8, 2025
d3bc003
Update WC_Layers/Source_Mesh_Reader.py
anu1217 Jan 9, 2025
18ca1b5
Change variable names and function inputs
anu1217 Jan 10, 2025
792717e
Change variable names and function inputs
anu1217 Jan 10, 2025
b1e219d
Add extra parameters
anu1217 Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions OpenMC_PhotonTransport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 7 10:07:33 2024

@author: Anupama Rajendra
"""

import openmc
import argparse
import numpy as np

Mesh_File = 'OpenMC_Mesh.h5m'
Strengths = []
#Performing simulation for the first decay time
with open('Sum_1.txt', 'r') as Sum_1:
Lines = Sum_1.readlines()
Split = Lines[0].split()
for sums in Split:
Strengths.append(float(sums))
anu1217 marked this conversation as resolved.
Show resolved Hide resolved

parser = argparse.ArgumentParser(description="Specify required inputs: file path to ALARA Element Library, element name, inner radius [cm], outer_radius [cm]")

#Required user inputs:
parser.add_argument('--filepath', type=str, required=True)
parser.add_argument('--element_1', type=str, required=True)
parser.add_argument('--element_2', type=str, required=True)
#Shell radii are left as user inputs, but the mesh is specific to W_inner_radius = 1000, W_outer_radius = 1005, C_inner_radius = 995
parser.add_argument('--W_inner_radius', type=float, required=True)
parser.add_argument('--W_outer_radius', type=float, required=True)
parser.add_argument('--C_inner_radius', type=float, required=True)

args = parser.parse_args()

fp = args.filepath
E_1 = args.element_1
E_2 = args.element_2
R_W_1 = args.W_inner_radius
R_W_2 = args.W_outer_radius
R_C_1 = args.C_inner_radius

# fp = '../../ALARA/data/elelib.std'
# E_1 = 'W'
# E_2 = 'C'
# R_W_1 = 1000
# R_W_2 = 1005
# R_C_1 = 995
bounds = [0,
1.00e+4,
2.00e+4,
5.00e+4,
1.00e+5,
2.00e+5,
3.00e+5,
4.00e+5,
6.00e+5,
8.00e+5,
1.00e+6,
1.22e+6,
1.44e+6,
1.66e+6,
2.00e+6,
2.50e+6,
3.00e+6,
4.00e+6,
5.00e+6,
6.50e+6,
8.00e+6,
1.00e+7,
1.20e+7,
1.40e+7,
2.00e+7]

#ALARA Element Library to read density for specified element:
# with open(""+fp+"") as ALARA_Lib:
with open("elelib.std") as ALARA_Lib:
Lib_Lines = ALARA_Lib.readlines()

# Create materials & export to XML:
#Simulating tungsten shell:

def make_W(element_1):
anu1217 marked this conversation as resolved.
Show resolved Hide resolved
M_1 = openmc.Material(material_id=1, name=element_1)
for line in Lib_Lines:
if line.split()[0].lower() == element_1.lower():
Density_M_1 = float(line.strip().split()[3])
M_1.set_density('g/cm3', Density_M_1)
M_1.add_element(element_1, 1.00)
return M_1

def make_C(element_2):
M_2 = openmc.Material(material_id=2, name=element_2)
for line in Lib_Lines:
if line.split()[0].lower() == element_2.lower():
#if line.startswith(element_2.lower()):
Density_M_2 = float(line.strip().split()[3])
M_2.set_density('g/cm3', Density_M_2)
M_2.add_element(element_2, 1.00)
return M_2

def all_mat(M_1, M_2):
all_materials = openmc.Materials([M_1, M_2])
all_materials.cross_sections = '../fendl-3.2-hdf5/cross_sections.xml'
all_materials.export_to_xml()

# Create geometry
#Spherical shell:
def make_spherical_shell(inner_radius_W, outer_radius_W, inner_radius_C, M_1, M_2):
S_W_1= openmc.Sphere(r=inner_radius_W) #sphere of radius 1000cm
inside_W_sphere_1 = -S_W_1
outside_W_sphere_1 = +S_W_1
S_W_2 = openmc.Sphere(r=outer_radius_W, boundary_type='vacuum')
inside_W_sphere_2 = -S_W_2
outside_W_sphere_2 = +S_W_2
S_W_3 = outside_W_sphere_1 & inside_W_sphere_2 #filled with specified material

S_C_1= openmc.Sphere(r=inner_radius_C) #sphere of radius 995cm
inside_C_sphere_1 = -S_C_1
outside_C_sphere_1 = +S_C_1
S_C_3 = outside_C_sphere_1 & inside_W_sphere_1 #filled with specified material

# Mapping materials to geometry:
Void = openmc.Cell(fill=None, region = inside_C_sphere_1)
W_Shell = openmc.Cell(fill=M_1, region=S_W_3)
C_Shell = openmc.Cell(fill=M_2, region=S_C_3)
Cells = [Void, W_Shell, C_Shell]
geometry = openmc.Geometry(Cells)
geometry.export_to_xml()
return geometry, Void, W_Shell, C_Shell, Cells

#Define source:
def make_source(W_Shell, C_Shell, Cells):
Source_List = []
Particle_Filter = openmc.ParticleFilter('photon')
Total_Mesh = openmc.UnstructuredMesh(Mesh_File, library='moab')
Mesh_Dist = openmc.stats.MeshSpatial(Total_Mesh, volume_normalized=False)
anu1217 marked this conversation as resolved.
Show resolved Hide resolved
for index, bound in enumerate(bounds[:-1]):
Energy_Dist = openmc.stats.Uniform(a=bounds[index], b=bounds[index + 1])
#Source strengths given by Strengths list at the top
Source = Source_List.append(openmc.IndependentSource(space=Mesh_Dist, energy=Energy_Dist, strength=Strengths[index], particle='photon', domains=Cells))
np.savetxt('Energy_List.txt', Energy_List, fmt='%s')
return Source_List, Particle_Filter, Total_Mesh, Mesh_Dist

# Define tallies
def tallies(W_Shell, C_Shell, Particle_Filter, Total_Mesh):
# global Total_Mesh
# global Total_Filter
Total_Filter = openmc.MeshFilter(Total_Mesh)

neutron_tally = openmc.Tally(tally_id=1, name="Neutron tally")
neutron_tally.scores = ['flux', 'elastic', 'absorption']

# Implementing filter for neutron tally through W shell
global cell_filter
cell_filter = openmc.CellFilter([W_Shell, C_Shell])
neutron_tally.filters = [cell_filter, Total_Filter]

# Creating a tally to get the flux energy spectrum.
# An energy filter is created to assign to the flux tally.
energy_filter_flux = openmc.EnergyFilter.from_group_structure("VITAMIN-J-42")

spectrum_tally = openmc.Tally(tally_id=2, name="Flux spectrum")
# Implementing energy and cell filters for flux spectrum tally
spectrum_tally.filters = [cell_filter, Total_Filter, energy_filter_flux, Particle_Filter]
spectrum_tally.scores = ['flux']

tall = openmc.Tallies([neutron_tally, spectrum_tally])
tall.export_to_xml()
return tall, neutron_tally, spectrum_tally, Total_Filter, cell_filter

# Assign simulation settings
def settings(Source_List):
sets = openmc.Settings()
sets.batches = 10
sets.inactive = 1
sets.particles = 100000
sets.source = Source_List
sets.run_mode = 'fixed source'
sets.export_to_xml()
return sets

def plot_universe(Void, W_Shell, C_Shell, Cells):
universe_ss = openmc.Universe(cells=Cells)
Plot = universe_ss.plot(width=(2500.0, 2500.0), basis='xz',
colors={Void: 'blue', W_Shell: 'red', C_Shell: 'green'}, legend=True)
Plot.figure.savefig('Universe')
return universe_ss

# Exporting materials, geometry, and tallies to .xml
def export_to_xml(element_1, element_2, inner_radius_W, outer_radius_W, inner_radius_C):
OpenMC_W = make_W(element_1)
OpenMC_C = make_C(element_2)
OpenMC_Mat = all_mat(OpenMC_W, OpenMC_C)
OpenMC_Geometry = make_spherical_shell(inner_radius_W, outer_radius_W, inner_radius_C, OpenMC_W, OpenMC_C)
OpenMC_Source = make_source(OpenMC_Geometry[2], OpenMC_Geometry[3], OpenMC_Geometry[4])
OpenMC_Settings = settings(OpenMC_Source[0])
OpenMC_Tallies = tallies(OpenMC_Geometry[2], OpenMC_Geometry[3], OpenMC_Source[1], OpenMC_Source[2])
OpenMC_Universe = plot_universe(OpenMC_Geometry[1], OpenMC_Geometry[2], OpenMC_Geometry[3], OpenMC_Geometry[4])
#return OpenMC_Materials, OpenMC_Geometry, OpenMC_Source, OpenMC_Settings, OpenMC_Tallies, OpenMC_Universe
return OpenMC_W, OpenMC_C, *OpenMC_Geometry, *OpenMC_Source, OpenMC_Settings, *OpenMC_Tallies

M_1, M_2, geometry, Void, W_Shell, C_Shell, Cells, Source_List, Particle_Filter, Total_Mesh, Mesh_Dist, tall, neutron_tally, spectrum_tally, Total_Filter, cell_filter, sets = export_to_xml(E_1, E_2, R_W_1, R_W_2, R_C_1)
35 changes: 35 additions & 0 deletions Photon_TallytoVtk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 3 23:18:20 2024

@author: Anupama Rajendra
"""
import openmc
import matplotlib.pyplot as plt

with openmc.StatePoint("statepoint.10.h5") as sp:
flux_spectrum = sp.get_tally(id=2)
mesh=sp.meshes[1]
# Get the reshaped tally data
tally_data_reshaped = flux_spectrum.get_reshaped_data(value='mean')

# Print the shape of the tally data
print("Tally data shape:", tally_data_reshaped.shape)

flux_sum_en = tally_data_reshaped.sum(axis=(0,1,3,4,5))
#Vitamin-J energy filter:
e_filter = flux_spectrum.filters[2]
#Lower bounds of the energy bins
e_filter_lower = e_filter.bins[:, 0]

# Plot flux spectrum
fix, ax = plt.subplots()
ax.loglog(e_filter_lower, flux_sum_en, drawstyle='steps-post')
ax.set_xlabel('Energy [eV]')
ax.set_ylabel('Flux [photon-cm/source]')
ax.grid(True, which='both')
plt.savefig('Photon_flux_vs_energy.png')
plt.show()

mesh_data = tally_data_reshaped.sum(axis=(0,2,3,4,5))
vtk = mesh.write_data_to_vtk(filename="Photon_Flux.vtk", datasets={"mean":mesh_data.flatten()})
41 changes: 41 additions & 0 deletions Source_Mesh_Reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 30 15:16:44 2024

@author: Anupama Rajendra
"""

import h5py
import numpy as np

# Open the HDF5 files
File_1 = h5py.File("source_mesh_1.h5m", 'r')
File_2 = h5py.File("source_mesh_2.h5m", 'r')
Files = [File_1, File_2]

# Number of photon groups
photon_groups = 24

# Process each file
for file_index, file in enumerate(Files):
# Extract data from the current file
tstt = file['tstt']
elements = tstt['elements']
tet_four = elements['Tet4']
tags = tet_four['tags']
sd = tags['source_density'][:]

# Write source density to a separate file for each mesh
with open(f'source_density_{file_index + 1}.txt', 'w') as source:
for tet_element in sd:
source.write(' '.join(map(str, tet_element)) + '\n')

# Calculate summed strengths for each photon group
summed_strengths = []
for group in range(photon_groups):
strengths = np.sum(sd[:, group]) # Sum over all mesh elements
summed_strengths.append(strengths)

# Write summed strengths to a separate file for each mesh
with open(f'summed_photon_strengths_{file_index + 1}.txt', 'w') as sums:
sums.write(' '.join(map(str, summed_strengths)) + '\n')