Refactor endf_data to be a fixture (#3539)

This commit is contained in:
GuySten 2025-08-29 15:06:33 +03:00 committed by GitHub
parent 2a278c93a6
commit 5529731e2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 59 additions and 86 deletions

View file

@ -193,12 +193,12 @@ def _default_config() -> _Config:
"""
config = _Config()
if "OPENMC_CROSS_SECTIONS" in os.environ:
config['cross_sections'] = os.environ["OPENMC_CROSS_SECTIONS"]
if "OPENMC_MG_CROSS_SECTIONS" in os.environ:
config['mg_cross_sections'] = os.environ["OPENMC_MG_CROSS_SECTIONS"]
chain_file = os.environ.get("OPENMC_CHAIN_FILE")
xs_path = config.get('cross_sections')
for key,var in _Config._PATH_KEYS.items():
if var in os.environ:
config[key] = os.environ[var]
chain_file = config.get("chain_file")
xs_path = config.get("cross_sections")
if chain_file is None and xs_path is not None and xs_path.exists():
try:
data = DataLibrary.from_xml(xs_path)
@ -209,10 +209,8 @@ def _default_config() -> _Config:
else:
for lib in reversed(data.libraries):
if lib['type'] == 'depletion_chain':
chain_file = xs_path.parent / lib['path']
config['chain_file'] = xs_path.parent / lib['path']
break
if chain_file is not None:
config['chain_file'] = chain_file
return config

View file

@ -1,3 +1,4 @@
import os
import pytest
import openmc
@ -28,7 +29,10 @@ def run_in_tmpdir(tmpdir):
yield
finally:
orig.chdir()
@pytest.fixture(scope="module")
def endf_data():
return os.environ['OPENMC_ENDF_DATA']
@pytest.fixture(scope='session', autouse=True)
def resolve_paths():

View file

@ -17,25 +17,22 @@ def ufloat_close(a, b):
@pytest.fixture(scope='module')
def nb90():
def nb90(endf_data):
"""Nb90 decay data."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'decay', 'dec-041_Nb_090.endf')
return openmc.data.Decay.from_endf(filename)
@pytest.fixture(scope='module')
def ba137m():
def ba137m(endf_data):
"""Ba137_m1 decay data."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'decay', 'dec-056_Ba_137m1.endf')
return openmc.data.Decay.from_endf(filename)
@pytest.fixture(scope='module')
def u235_yields():
def u235_yields(endf_data):
"""U235 fission product yield data."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'nfy', 'nfy-092_U_235.endf')
return openmc.data.FissionProductYields.from_endf(filename)

View file

@ -7,6 +7,7 @@ import pytest
import numpy as np
import openmc
from openmc.data import IncidentNeutron
from openmc.data.kalbach_mann import _separation_energy, _AtomicRepresentation
from openmc.data import kalbach_slope
@ -129,7 +130,7 @@ def test_kalbach_slope():
('Hg204.h5', 'n-080_Hg_204.endf')
]
)
def test_comparison_slope_hdf5(hdf5_filename, endf_filename):
def test_comparison_slope_hdf5(hdf5_filename, endf_filename, endf_data):
"""Test the calculation of the Kalbach-Mann slope done by OpenMC
by comparing it to HDF5 data. The test is based on the first product
of MT=5 (neutron). The isotopes tested have been selected because the
@ -147,13 +148,13 @@ def test_comparison_slope_hdf5(hdf5_filename, endf_filename):
"""
# HDF5 data
hdf5_directory = Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
hdf5_directory = Path(openmc.config.get('cross_sections')).parent
hdf5_data = IncidentNeutron.from_hdf5(hdf5_directory / hdf5_filename)
hdf5_product = hdf5_data[5].products[0]
hdf5_distribution = hdf5_product.distribution[0]
# ENDF data
endf_directory = Path(os.environ['OPENMC_ENDF_DATA'])
endf_directory = Path(endf_data)
endf_path = endf_directory / 'neutrons' / endf_filename
endf_data = IncidentNeutron.from_endf(endf_path)
endf_product = endf_data[5].products[0]

View file

@ -30,7 +30,7 @@ def test_data_library(tmpdir):
assert os.path.exists(filename)
new_lib = openmc.data.DataLibrary()
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
directory = os.path.dirname(openmc.config.get('cross_sections'))
new_lib.register_file(os.path.join(directory, 'H1.h5'))
assert new_lib[-1]['type'] == 'neutron'
new_lib.register_file(os.path.join(directory, 'c_Zr_in_ZrH.h5'))

View file

@ -8,14 +8,14 @@ import openmc.data
@pytest.fixture(scope='module')
def u235():
directory = pathlib.Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
directory = pathlib.Path(openmc.config.get('cross_sections')).parent
u235 = directory / 'wmp' / '092235.h5'
return openmc.data.WindowedMultipole.from_hdf5(u235)
@pytest.fixture(scope='module')
def b10():
directory = pathlib.Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
directory = pathlib.Path(openmc.config.get('cross_sections')).parent
b10 = directory / 'wmp' / '005010.h5'
return openmc.data.WindowedMultipole.from_hdf5(b10)
@ -48,17 +48,15 @@ def test_export_to_hdf5(tmpdir, u235):
assert os.path.exists(filename)
def test_from_endf():
def test_from_endf(endf_data):
pytest.importorskip('vectfit')
endf_data = os.environ['OPENMC_ENDF_DATA']
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
assert openmc.data.WindowedMultipole.from_endf(
endf_file, log=True, wmp_options={"n_win": 400, "n_cf": 3})
def test_from_endf_search():
def test_from_endf_search(endf_data):
pytest.importorskip('vectfit')
endf_data = os.environ['OPENMC_ENDF_DATA']
endf_file = os.path.join(endf_data, 'neutrons', 'n-095_Am_244.endf')
assert openmc.data.WindowedMultipole.from_endf(
endf_file, log=True, wmp_options={"search": True, 'rtol':1e-2})

View file

@ -14,128 +14,113 @@ _TEMPERATURES = [300., 600., 900.]
@pytest.fixture(scope='module')
def pu239():
"""Pu239 HDF5 data."""
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
directory = os.path.dirname(openmc.config.get('cross_sections'))
filename = os.path.join(directory, 'Pu239.h5')
return openmc.data.IncidentNeutron.from_hdf5(filename)
@pytest.fixture(scope='module')
def xe135():
def xe135(endf_data):
"""Xe135 ENDF data (contains SLBW resonance range)"""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-054_Xe_135.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def sm150():
def sm150(endf_data):
"""Sm150 ENDF data (contains MLBW resonance range)"""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-062_Sm_150.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def gd154():
def gd154(endf_data):
"""Gd154 ENDF data (contains Reich Moore resonance range and reosnance
covariance with LCOMP=1)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-064_Gd_154.endf')
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
@pytest.fixture(scope='module')
def cl35():
def cl35(endf_data):
"""Cl35 ENDF data (contains RML resonance range)"""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-017_Cl_035.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def am241():
def am241(endf_data):
"""Am241 ENDF data (contains Madland-Nix fission energy distribution)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-095_Am_241.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def u233():
def u233(endf_data):
"""U233 ENDF data (contains Watt fission energy distribution)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-092_U_233.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def u236():
def u236(endf_data):
"""U236 ENDF data (contains Watt fission energy distribution)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-092_U_236.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def na22():
def na22(endf_data):
"""Na22 ENDF data (contains evaporation spectrum)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-011_Na_022.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def na23():
def na23(endf_data):
"""Na23 ENDF data (contains MLBW resonance covariance with LCOMP=0)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-011_Na_023.endf')
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
@pytest.fixture(scope='module')
def be9():
def be9(endf_data):
"""Be9 ENDF data (contains laboratory angle-energy distribution)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-004_Be_009.endf')
return openmc.data.IncidentNeutron.from_endf(filename)
@pytest.fixture(scope='module')
def h2():
endf_data = os.environ['OPENMC_ENDF_DATA']
def h2(endf_data):
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_002.endf')
return openmc.data.IncidentNeutron.from_njoy(
endf_file, temperatures=_TEMPERATURES)
@pytest.fixture(scope='module')
def am244():
endf_data = os.environ['OPENMC_ENDF_DATA']
def am244(endf_data):
endf_file = os.path.join(endf_data, 'neutrons', 'n-095_Am_244.endf')
return openmc.data.IncidentNeutron.from_njoy(endf_file)
@pytest.fixture(scope='module')
def ti50():
def ti50(endf_data):
"""Ti50 ENDF data (contains Multi-level Breit-Wigner resonance range and
resonance covariance with LCOMP=1)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-022_Ti_050.endf')
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
@pytest.fixture(scope='module')
def cf252():
def cf252(endf_data):
"""Cf252 ENDF data (contains RM resonance covariance with LCOMP=0)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-098_Cf_252.endf')
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
@pytest.fixture(scope='module')
def th232():
def th232(endf_data):
"""Th232 ENDF data (contains RM resonance covariance with LCOMP=2)."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'neutrons', 'n-090_Th_232.endf')
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
@ -453,8 +438,7 @@ def test_laboratory(be9):
@needs_njoy
def test_correlated(tmpdir):
endf_data = os.environ['OPENMC_ENDF_DATA']
def test_correlated(tmpdir, endf_data):
endf_file = os.path.join(endf_data, 'neutrons', 'n-014_Si_030.endf')
si30 = openmc.data.IncidentNeutron.from_njoy(endf_file, heatr=False)
@ -480,8 +464,7 @@ def test_nbody(tmpdir, h2):
@needs_njoy
def test_ace_convert(run_in_tmpdir):
endf_data = os.environ['OPENMC_ENDF_DATA']
def test_ace_convert(run_in_tmpdir, endf_data):
filename = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
ace_ascii = 'ace_ascii'
ace_binary = 'ace_binary'
@ -515,8 +498,7 @@ def test_ace_table_types():
@needs_njoy
def test_high_temperature():
endf_data = os.environ['OPENMC_ENDF_DATA']
def test_high_temperature(endf_data):
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
# Ensure that from_njoy works when given a high temperature

View file

@ -9,9 +9,8 @@ import openmc.data
@pytest.fixture(scope='module')
def elements_endf():
def elements_endf(endf_data):
"""Dictionary of element ENDF data indexed by atomic symbol."""
endf_data = os.environ['OPENMC_ENDF_DATA']
elements = {'H': 1, 'O': 8, 'Al': 13, 'Cu': 29, 'Ag': 47, 'U': 92, 'Pu': 94}
data = {}
for symbol, Z in elements.items():
@ -145,8 +144,8 @@ def test_export_to_hdf5(tmpdir, element):
element2.export_to_hdf5(filename, 'w')
def test_photodat_only(run_in_tmpdir):
endf_dir = Path(os.environ['OPENMC_ENDF_DATA'])
def test_photodat_only(run_in_tmpdir, endf_data):
endf_dir = Path(endf_data)
photoatomic_file = endf_dir / 'photoat' / 'photoat-001_H_000.endf'
data = openmc.data.IncidentPhoton.from_endf(photoatomic_file)
data.export_to_hdf5('tmp.h5', 'w')

View file

@ -13,7 +13,7 @@ from . import needs_njoy
@pytest.fixture(scope='module')
def h2o():
"""H in H2O thermal scattering data."""
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
directory = os.path.dirname(openmc.config.get('cross_sections'))
filename = os.path.join(directory, 'c_H_in_H2O.h5')
return openmc.data.ThermalScattering.from_hdf5(filename)
@ -21,15 +21,14 @@ def h2o():
@pytest.fixture(scope='module')
def graphite():
"""Graphite thermal scattering data."""
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
directory = os.path.dirname(openmc.config.get('cross_sections'))
filename = os.path.join(directory, 'c_Graphite.h5')
return openmc.data.ThermalScattering.from_hdf5(filename)
@pytest.fixture(scope='module')
def h2o_njoy():
def h2o_njoy(endf_data):
"""H in H2O generated using NJOY."""
endf_data = os.environ['OPENMC_ENDF_DATA']
path_h1 = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
path_h2o = os.path.join(endf_data, 'thermal_scatt', 'tsl-HinH2O.endf')
return openmc.data.ThermalScattering.from_njoy(
@ -37,17 +36,15 @@ def h2o_njoy():
@pytest.fixture(scope='module')
def hzrh():
def hzrh(endf_data):
"""H in ZrH thermal scattering data."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'thermal_scatt', 'tsl-HinZrH.endf')
return openmc.data.ThermalScattering.from_endf(filename, divide_incoherent_elastic=True)
@pytest.fixture(scope='module')
def hzrh_njoy():
def hzrh_njoy(endf_data):
"""H in ZrH generated using NJOY."""
endf_data = os.environ['OPENMC_ENDF_DATA']
path_h1 = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
path_hzrh = os.path.join(endf_data, 'thermal_scatt', 'tsl-HinZrH.endf')
with_endf_data = openmc.data.ThermalScattering.from_njoy(
@ -60,9 +57,8 @@ def hzrh_njoy():
@pytest.fixture(scope='module')
def sio2():
def sio2(endf_data):
"""SiO2 thermal scattering data."""
endf_data = os.environ['OPENMC_ENDF_DATA']
filename = os.path.join(endf_data, 'thermal_scatt', 'tsl-SiO2.endf')
return openmc.data.ThermalScattering.from_endf(filename, divide_incoherent_elastic=True)
@ -102,8 +98,7 @@ def test_graphite_xs(graphite):
assert elastic([1e-3, 1.0]) == pytest.approx([0.0, 0.62586153])
@needs_njoy
def test_graphite_njoy():
endf_data = os.environ['OPENMC_ENDF_DATA']
def test_graphite_njoy(endf_data):
path_c0 = os.path.join(endf_data, 'neutrons', 'n-006_C_000.endf')
path_gr = os.path.join(endf_data, 'thermal_scatt', 'tsl-graphite.endf')
graphite = openmc.data.ThermalScattering.from_njoy(
@ -141,8 +136,7 @@ def test_continuous_dist(h2o_njoy):
assert isinstance(dist, openmc.data.IncoherentInelasticAE)
def test_h2o_endf():
endf_data = os.environ['OPENMC_ENDF_DATA']
def test_h2o_endf(endf_data):
filename = os.path.join(endf_data, 'thermal_scatt', 'tsl-HinH2O.endf')
h2o = openmc.data.ThermalScattering.from_endf(filename, divide_incoherent_elastic=True)
assert not h2o.elastic

View file

@ -54,11 +54,11 @@ def simple_chain():
@pytest.fixture(scope='module')
def endf_chain():
endf_data = Path(os.environ['OPENMC_ENDF_DATA'])
decay_data = (endf_data / 'decay').glob('*.endf')
fpy_data = (endf_data / 'nfy').glob('*.endf')
neutron_data = (endf_data / 'neutrons').glob('*.endf')
def endf_chain(endf_data):
endf_dir = Path(endf_data)
decay_data = (endf_dir / 'decay').glob('*.endf')
fpy_data = (endf_dir / 'nfy').glob('*.endf')
neutron_data = (endf_dir / 'neutrons').glob('*.endf')
return Chain.from_endf(decay_data, fpy_data, neutron_data)