Skip to content

Commit

Permalink
Update basicsim.py
Browse files Browse the repository at this point in the history
  • Loading branch information
marquezj authored Jan 24, 2025
1 parent c0788e6 commit e85282e
Showing 1 changed file with 118 additions and 13 deletions.
131 changes: 118 additions & 13 deletions devel/openmc/basicsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,131 @@
## ##
################################################################################

def main():
import numpy as np
import openmc

TRUE_VALUES = np.array([5.60481868e+01, 5.75019668e-04, 7.67958251e-01])
TRUE_ERRORS = np.array([2.58956360e-03, 4.83597510e-06, 1.96171839e-03])
TRUE_POSITIONS = np.array([0.00872665, 1.15202318, 2.29531971])

def is_interactive():
import sys
return ( '--interactive' in sys.argv[1:]
or '-i' in sys.argv[1:] )

def pencil_beam_model(cfg, E0, N):
"""Return an openmc.Model() object for a monoenergetic pencil
beam hitting a 1 mm sphere filled with the material defined by
the cfg string, and compute angular distribution for the
Debye-Scherrer cones"""

import NCrystal as NC

# Material definition

m1 = openmc.Material.from_ncrystal(cfg)
materials = openmc.Materials([m1])

# Geometry definition

sample_sphere = openmc.Sphere(r=0.1)
outer_sphere = openmc.Sphere(r=100, boundary_type="vacuum")
cell1 = openmc.Cell(region=-sample_sphere, fill=m1)
cell2_region = +sample_sphere & -outer_sphere
cell2 = openmc.Cell(region=cell2_region, fill=None)
geometry = openmc.Geometry([cell1, cell2])

import openmc
# Source definition

mat_al = openmc.Material.from_ncrystal('Al_sg225.ncmat')
materials = openmc.Materials([mat_al])
source = openmc.IndependentSource()
source.space = openmc.stats.Point((0, 0, -20))
source.angle = openmc.stats.Monodirectional(reference_uvw=(0, 0, 1))
source.energy = openmc.stats.Discrete([E0], [1.0])

sphere0 = openmc.Sphere(r=10, boundary_type='vacuum')
cell0 = openmc.Cell(region=-sphere0, fill=mat_al)
geometry = openmc.Geometry([cell0])
# Execution settings

settings = openmc.Settings()
settings.source = openmc.IndependentSource()
settings.run_mode = 'fixed source'
settings.source = source
settings.run_mode = "fixed source"
settings.batches = 10
settings.particles = 1000
settings.particles = N

# Run OpenMC model
model = openmc.Model(geometry, materials, settings)
model.run()
# Tally definition

wl = NC.ekin2wl(E0)
angular_binwdidth = np.radians(1.0)
debye_cones = np.array([ 2.0*np.asin(wl/(2.0*hkl.d)) for hkl in NC.createInfo(cfg).hklObjects() if wl < 2.0*hkl.d ])
angular_bins = np.sort(np.concatenate(([0,angular_binwdidth], debye_cones-0.5*angular_binwdidth, debye_cones+0.5*angular_binwdidth)))
print(angular_bins)

tally1 = openmc.Tally(name="debye-scherrer cones")
tally1.scores = ["current"]
filter1 = openmc.SurfaceFilter(sample_sphere)
filter2 = openmc.PolarFilter(angular_bins)
filter3 = openmc.CellFromFilter(cell1)
tally1.filters = [filter1, filter2, filter3]

tally2 = openmc.Tally(name="angular distribution")
tally2.scores = ["current"]
filter2b = openmc.PolarFilter(np.linspace(0, np.pi, 180+1))
tally2.filters = [filter1, filter2b, filter3]
tallies = openmc.Tallies([tally1, tally2])

return openmc.Model(geometry, materials, settings, tallies)

def check_results(sp_file):
with openmc.StatePoint(sp_file) as sp:
tal = sp.get_tally(name='debye-scherrer cones')
df = tal.get_pandas_dataframe()
bin_high = df['polar high [rad]'].values
bin_low = df['polar low [rad]'].values
values = df['mean'].values/(bin_high - bin_low)
errors = df['std. dev.'].values/(bin_high - bin_low)
assert np.shape(TRUE_VALUES) == np.shape(values), 'Wrong number of Debye-Scherrer cones'
return np.all(values > TRUE_VALUES-TRUE_ERRORS) and np.all(values < TRUE_VALUES+TRUE_ERRORS)

def plot_tally(sp_file):
with openmc.StatePoint(sp_file) as sp:
tal = sp.get_tally(name='debye-scherrer cones')
df = tal.get_pandas_dataframe()
bin_high = df['polar high [rad]'].values
bin_low = df['polar low [rad]'].values
cone_positions = 0.5*(bin_high + bin_low)
cone_values = df['mean'].values/(bin_high - bin_low)
cone_errors = df['std. dev.'].values/(bin_high - bin_low)

with openmc.StatePoint(sp_file) as sp:
tal = sp.get_tally(name='angular distribution')
df = tal.get_pandas_dataframe()
bin_high = df['polar high [rad]'].values
bin_low = df['polar low [rad]'].values
angdist_bins = bin_high
angdist_values = df['mean'].values/(bin_high - bin_low)
angdist_errors = df['std. dev.'].values/(bin_high - bin_low)

import matplotlib.pyplot as plt
plt.figure()
plt.step(np.rad2deg(bin_high), angdist_values)
plt.errorbar(np.rad2deg(cone_positions), cone_values, yerr=cone_errors, fmt='.')
plt.errorbar(np.rad2deg(TRUE_POSITIONS), TRUE_VALUES, yerr=TRUE_ERRORS, fmt='.', label='True values')
plt.yscale('log')
plt.ylabel('density')
plt.xlabel('Polar angle [deg]')
plt.legend()
plt.show()

def main():

n_particles = 1000000
E0 = 0.0045 # eV
cfg = 'Al_sg225.ncmat'
model = pencil_beam_model(cfg, E0, n_particles)
sp_file = model.run()

if is_interactive():
plot_tally(sp_file)

assert check_results(sp_file), 'Statistically significant deviation from true values'

if __name__ == '__main__':
main()

0 comments on commit e85282e

Please sign in to comment.