Hello everyone,
I am currently trying to simulate the photon flux through a layer of water and air to compare with my analytical calculation and check if my simulation is correct. I’m also trying to get to grips with the software and understand what I’m doing because I’ve only been using OpenMC for 1 month.
I’ve filled my box with nothing (empty) and just a layer of water and air but every time OpenMC measures the flux, it measures the flux in the empty space as if it couldn’t see the layer of water and air. And when I display my geometry and my universe, my layers are there. So I don’t understand, I’ve tried everything I can.
Can anyone help me?
Here is my code :
import openmc
import openmc.statepoint
import openmc.stats
import matplotlib.pyplot as plt
#################################################### Easy model (void) ####################################################################
##### Define material
air = openmc.Material(name='air')
air.add_element(element='N', percent=0.79, percent_type='ao')
air.add_element(element='O', percent=0.21, percent_type='ao')
air.set_density(units='g/cm3', density=1.293e-3)
water = openmc.Material(name='water')
water.add_elements_from_formula(formula='H2O')
#water.add_element(element='H', percent=2.0, percent_type='ao')
#water.add_element(element='O', percent=1.0, percent_type='ao')
water.set_density(units='g/cm3', density=1.0)
materials = openmc.Materials(materials=[air, water])
materials.export_to_xml()
##### Define the universe
pitch = 20.0
left = openmc.XPlane(x0=-pitch/2, boundary_type='vacuum')
right = openmc.XPlane(x0=pitch/2, boundary_type='vacuum')
bottom = openmc.YPlane(y0=-pitch/2, boundary_type='vacuum')
top = openmc.YPlane(y0=pitch/2, boundary_type='vacuum')
# Define a layer of water
water_left = openmc.XPlane(x0=0.)
water_right = openmc.XPlane(x0=2.0)
# Define a layer of air
air_left = openmc.XPlane(x0=2.0)
air_right = openmc.XPlane(x0=4.0)
##### Define the regions
water_box = +water_left & -water_right & +bottom & -top
air_box = +air_left & -air_right & +bottom & -top
box = +left & -right & +bottom & -top
##### Define the cell
water_cell = openmc.Cell(name='water_cell', region=water_box, fill=water)
air_cell = openmc.Cell(name='air_cell', region=air_box, fill=air)
box_cell = openmc.Cell(name='box_cell', region=box)
##### Define the universe
universe = openmc.Universe(name='universe', cells=[water_cell, air_cell, box_cell])
universe.plot(origin=(0.0, 0.0, 0.0), width=(10.0, 10.0), pixels=(1000, 1000), basis='xy', color_by='cell', colors={box_cell:'red', water_cell:'blue', air_cell:'green'},
legend=True, axis_units='cm')
#plt.show()
#quit()
##### Define the geometry
geometry = openmc.Geometry(root=universe)
geometry.export_to_xml()
##### Visualize the geometry
plot = openmc.Plot()
plot.filename = 'box_plot'
plot.width = (10.0, 10.0)
plot.pixels = (1000, 1000)
plot.color_by = 'cell'
plot.colors = {box_cell : 'red', water_cell : 'blue', air_cell : 'green'}
##### Visualize the 3D geometry
plot_3D = openmc.Plot()
plot_3D.filename = 'box_3D_plot'
plot_3D.type = 'voxel'
plot_3D.width = (10.0, 10.0, 10.0)
plot_3D.pixels = (100, 100, 100)
plots = openmc.Plots(plots=[plot, plot_3D])
plots.export_to_xml()
openmc.plot_geometry(output=True)
##### Define photon source
source_strenght = 10000 # Unit is source/cm/s in 2D and source/s in 3D
lower_source_coords = (-6.0, -1.0, 0.0)
upper_source_coords = (-4.0, 1.0, 0.0)
source_surface = (upper_source_coords[0]-lower_source_coords[0])*(upper_source_coords[1]-lower_source_coords[1]) # Unit is cm
source = openmc.IndependentSource()
source.space = openmc.stats.Box(lower_left=lower_source_coords, upper_right=upper_source_coords)
#source.space = openmc.stats.Point(xyz=(-5., 0., 0.))
source.angle = openmc.stats.Monodirectional(reference_uvw=[1, 0, 0])
source.energy = openmc.stats.Discrete(x=[30e3], p=[1.0])
source.time = openmc.stats.Uniform(a=0.0, b=1.0)
source.particle = 'photon'
source.strenght = source_strenght
##### Set the parameters of the simulation
settings = openmc.Settings()
settings.particles = 10000
settings.batches = 1
settings.source = source
settings.photon_transport = True # Activate the transport photon
settings.run_mode = 'fixed source' # Fixed source mode for photons
settings.export_to_xml()
##### Define a tally just in front of the source
# Define the mesh tally with a RegularMesh
mesh = openmc.RegularMesh(name='tally_mesh')
mesh_lower_coords = (5.0, -1.0)
mesh_upper_coords = (6.0, 1.0)
mesh.dimension = [100, 100] # Number of divisions in each direction
mesh.lower_left = mesh_lower_coords # Adjust the position at 10 cm of the source
mesh.upper_right = mesh_upper_coords # Adjust the position at 10 cm of the source
d = mesh_lower_coords[0] - upper_source_coords[0] # Tally-source distance
# Create a mesh filter
mesh_filter = openmc.MeshFilter(mesh)
##### Define a tally for photon flux
tally = openmc.Tally(name='photon_flux')
tally.scores = ['flux'] # Unit is particule.cm/source
tally.filters = [mesh_filter]
tallies = openmc.Tallies([tally])
tallies.export_to_xml()
##### Run the simulation
openmc.run()
# openmc.run(tracks=True) # Track every photons
##### Read the results
# Find the last statepoint file
statepoint_file = openmc.StatePoint('statepoint.1.h5')
##### Access the tally results
final_tally = statepoint_file.get_tally(name='photon_flux')
flux = final_tally.mean.flatten() # Unit is particule.cm/source
##### Print the total flux
total_flux = flux.sum() # Unit is particules.cm/source
print(f'Total photon flux: {(total_flux*source_strenght)} photons/source')
print(f'Total photon flux: {(total_flux*source_strenght)/(source_surface)} photons/cm2/source')
statepoint_file.close()