Skip to content

Commit 5529731

Browse files
authored
Refactor endf_data to be a fixture (#3539)
1 parent 2a278c9 commit 5529731

File tree

10 files changed

+59
-86
lines changed

10 files changed

+59
-86
lines changed

openmc/config.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ def _default_config() -> _Config:
193193
194194
"""
195195
config = _Config()
196-
if "OPENMC_CROSS_SECTIONS" in os.environ:
197-
config['cross_sections'] = os.environ["OPENMC_CROSS_SECTIONS"]
198-
if "OPENMC_MG_CROSS_SECTIONS" in os.environ:
199-
config['mg_cross_sections'] = os.environ["OPENMC_MG_CROSS_SECTIONS"]
200-
chain_file = os.environ.get("OPENMC_CHAIN_FILE")
201-
xs_path = config.get('cross_sections')
196+
for key,var in _Config._PATH_KEYS.items():
197+
if var in os.environ:
198+
config[key] = os.environ[var]
199+
200+
chain_file = config.get("chain_file")
201+
xs_path = config.get("cross_sections")
202202
if chain_file is None and xs_path is not None and xs_path.exists():
203203
try:
204204
data = DataLibrary.from_xml(xs_path)
@@ -209,10 +209,8 @@ def _default_config() -> _Config:
209209
else:
210210
for lib in reversed(data.libraries):
211211
if lib['type'] == 'depletion_chain':
212-
chain_file = xs_path.parent / lib['path']
212+
config['chain_file'] = xs_path.parent / lib['path']
213213
break
214-
if chain_file is not None:
215-
config['chain_file'] = chain_file
216214
return config
217215

218216

tests/conftest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pytest
23
import openmc
34

@@ -28,7 +29,10 @@ def run_in_tmpdir(tmpdir):
2829
yield
2930
finally:
3031
orig.chdir()
31-
32+
33+
@pytest.fixture(scope="module")
34+
def endf_data():
35+
return os.environ['OPENMC_ENDF_DATA']
3236

3337
@pytest.fixture(scope='session', autouse=True)
3438
def resolve_paths():

tests/unit_tests/test_data_decay.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@ def ufloat_close(a, b):
1717

1818

1919
@pytest.fixture(scope='module')
20-
def nb90():
20+
def nb90(endf_data):
2121
"""Nb90 decay data."""
22-
endf_data = os.environ['OPENMC_ENDF_DATA']
2322
filename = os.path.join(endf_data, 'decay', 'dec-041_Nb_090.endf')
2423
return openmc.data.Decay.from_endf(filename)
2524

2625

2726
@pytest.fixture(scope='module')
28-
def ba137m():
27+
def ba137m(endf_data):
2928
"""Ba137_m1 decay data."""
30-
endf_data = os.environ['OPENMC_ENDF_DATA']
3129
filename = os.path.join(endf_data, 'decay', 'dec-056_Ba_137m1.endf')
3230
return openmc.data.Decay.from_endf(filename)
3331

3432

3533
@pytest.fixture(scope='module')
36-
def u235_yields():
34+
def u235_yields(endf_data):
3735
"""U235 fission product yield data."""
38-
endf_data = os.environ['OPENMC_ENDF_DATA']
3936
filename = os.path.join(endf_data, 'nfy', 'nfy-092_U_235.endf')
4037
return openmc.data.FissionProductYields.from_endf(filename)
4138

tests/unit_tests/test_data_kalbach_mann.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99

10+
import openmc
1011
from openmc.data import IncidentNeutron
1112
from openmc.data.kalbach_mann import _separation_energy, _AtomicRepresentation
1213
from openmc.data import kalbach_slope
@@ -129,7 +130,7 @@ def test_kalbach_slope():
129130
('Hg204.h5', 'n-080_Hg_204.endf')
130131
]
131132
)
132-
def test_comparison_slope_hdf5(hdf5_filename, endf_filename):
133+
def test_comparison_slope_hdf5(hdf5_filename, endf_filename, endf_data):
133134
"""Test the calculation of the Kalbach-Mann slope done by OpenMC
134135
by comparing it to HDF5 data. The test is based on the first product
135136
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):
147148
148149
"""
149150
# HDF5 data
150-
hdf5_directory = Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
151+
hdf5_directory = Path(openmc.config.get('cross_sections')).parent
151152
hdf5_data = IncidentNeutron.from_hdf5(hdf5_directory / hdf5_filename)
152153
hdf5_product = hdf5_data[5].products[0]
153154
hdf5_distribution = hdf5_product.distribution[0]
154155

155156
# ENDF data
156-
endf_directory = Path(os.environ['OPENMC_ENDF_DATA'])
157+
endf_directory = Path(endf_data)
157158
endf_path = endf_directory / 'neutrons' / endf_filename
158159
endf_data = IncidentNeutron.from_endf(endf_path)
159160
endf_product = endf_data[5].products[0]

tests/unit_tests/test_data_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_data_library(tmpdir):
3030
assert os.path.exists(filename)
3131

3232
new_lib = openmc.data.DataLibrary()
33-
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
33+
directory = os.path.dirname(openmc.config.get('cross_sections'))
3434
new_lib.register_file(os.path.join(directory, 'H1.h5'))
3535
assert new_lib[-1]['type'] == 'neutron'
3636
new_lib.register_file(os.path.join(directory, 'c_Zr_in_ZrH.h5'))

tests/unit_tests/test_data_multipole.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
@pytest.fixture(scope='module')
1010
def u235():
11-
directory = pathlib.Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
11+
directory = pathlib.Path(openmc.config.get('cross_sections')).parent
1212
u235 = directory / 'wmp' / '092235.h5'
1313
return openmc.data.WindowedMultipole.from_hdf5(u235)
1414

1515

1616
@pytest.fixture(scope='module')
1717
def b10():
18-
directory = pathlib.Path(os.environ['OPENMC_CROSS_SECTIONS']).parent
18+
directory = pathlib.Path(openmc.config.get('cross_sections')).parent
1919
b10 = directory / 'wmp' / '005010.h5'
2020
return openmc.data.WindowedMultipole.from_hdf5(b10)
2121

@@ -48,17 +48,15 @@ def test_export_to_hdf5(tmpdir, u235):
4848
assert os.path.exists(filename)
4949

5050

51-
def test_from_endf():
51+
def test_from_endf(endf_data):
5252
pytest.importorskip('vectfit')
53-
endf_data = os.environ['OPENMC_ENDF_DATA']
5453
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
5554
assert openmc.data.WindowedMultipole.from_endf(
5655
endf_file, log=True, wmp_options={"n_win": 400, "n_cf": 3})
5756

5857

59-
def test_from_endf_search():
58+
def test_from_endf_search(endf_data):
6059
pytest.importorskip('vectfit')
61-
endf_data = os.environ['OPENMC_ENDF_DATA']
6260
endf_file = os.path.join(endf_data, 'neutrons', 'n-095_Am_244.endf')
6361
assert openmc.data.WindowedMultipole.from_endf(
6462
endf_file, log=True, wmp_options={"search": True, 'rtol':1e-2})

tests/unit_tests/test_data_neutron.py

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,128 +14,113 @@
1414
@pytest.fixture(scope='module')
1515
def pu239():
1616
"""Pu239 HDF5 data."""
17-
directory = os.path.dirname(os.environ['OPENMC_CROSS_SECTIONS'])
17+
directory = os.path.dirname(openmc.config.get('cross_sections'))
1818
filename = os.path.join(directory, 'Pu239.h5')
1919
return openmc.data.IncidentNeutron.from_hdf5(filename)
2020

2121

2222
@pytest.fixture(scope='module')
23-
def xe135():
23+
def xe135(endf_data):
2424
"""Xe135 ENDF data (contains SLBW resonance range)"""
25-
endf_data = os.environ['OPENMC_ENDF_DATA']
2625
filename = os.path.join(endf_data, 'neutrons', 'n-054_Xe_135.endf')
2726
return openmc.data.IncidentNeutron.from_endf(filename)
2827

2928

3029
@pytest.fixture(scope='module')
31-
def sm150():
30+
def sm150(endf_data):
3231
"""Sm150 ENDF data (contains MLBW resonance range)"""
33-
endf_data = os.environ['OPENMC_ENDF_DATA']
3432
filename = os.path.join(endf_data, 'neutrons', 'n-062_Sm_150.endf')
3533
return openmc.data.IncidentNeutron.from_endf(filename)
3634

3735

3836
@pytest.fixture(scope='module')
39-
def gd154():
37+
def gd154(endf_data):
4038
"""Gd154 ENDF data (contains Reich Moore resonance range and reosnance
4139
covariance with LCOMP=1)."""
42-
endf_data = os.environ['OPENMC_ENDF_DATA']
4340
filename = os.path.join(endf_data, 'neutrons', 'n-064_Gd_154.endf')
4441
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
4542

4643

4744
@pytest.fixture(scope='module')
48-
def cl35():
45+
def cl35(endf_data):
4946
"""Cl35 ENDF data (contains RML resonance range)"""
50-
endf_data = os.environ['OPENMC_ENDF_DATA']
5147
filename = os.path.join(endf_data, 'neutrons', 'n-017_Cl_035.endf')
5248
return openmc.data.IncidentNeutron.from_endf(filename)
5349

5450

5551
@pytest.fixture(scope='module')
56-
def am241():
52+
def am241(endf_data):
5753
"""Am241 ENDF data (contains Madland-Nix fission energy distribution)."""
58-
endf_data = os.environ['OPENMC_ENDF_DATA']
5954
filename = os.path.join(endf_data, 'neutrons', 'n-095_Am_241.endf')
6055
return openmc.data.IncidentNeutron.from_endf(filename)
6156

6257

6358
@pytest.fixture(scope='module')
64-
def u233():
59+
def u233(endf_data):
6560
"""U233 ENDF data (contains Watt fission energy distribution)."""
66-
endf_data = os.environ['OPENMC_ENDF_DATA']
6761
filename = os.path.join(endf_data, 'neutrons', 'n-092_U_233.endf')
6862
return openmc.data.IncidentNeutron.from_endf(filename)
6963

7064

7165
@pytest.fixture(scope='module')
72-
def u236():
66+
def u236(endf_data):
7367
"""U236 ENDF data (contains Watt fission energy distribution)."""
74-
endf_data = os.environ['OPENMC_ENDF_DATA']
7568
filename = os.path.join(endf_data, 'neutrons', 'n-092_U_236.endf')
7669
return openmc.data.IncidentNeutron.from_endf(filename)
7770

7871

7972
@pytest.fixture(scope='module')
80-
def na22():
73+
def na22(endf_data):
8174
"""Na22 ENDF data (contains evaporation spectrum)."""
82-
endf_data = os.environ['OPENMC_ENDF_DATA']
8375
filename = os.path.join(endf_data, 'neutrons', 'n-011_Na_022.endf')
8476
return openmc.data.IncidentNeutron.from_endf(filename)
8577

8678

8779
@pytest.fixture(scope='module')
88-
def na23():
80+
def na23(endf_data):
8981
"""Na23 ENDF data (contains MLBW resonance covariance with LCOMP=0)."""
90-
endf_data = os.environ['OPENMC_ENDF_DATA']
9182
filename = os.path.join(endf_data, 'neutrons', 'n-011_Na_023.endf')
9283
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
9384

9485

9586
@pytest.fixture(scope='module')
96-
def be9():
87+
def be9(endf_data):
9788
"""Be9 ENDF data (contains laboratory angle-energy distribution)."""
98-
endf_data = os.environ['OPENMC_ENDF_DATA']
9989
filename = os.path.join(endf_data, 'neutrons', 'n-004_Be_009.endf')
10090
return openmc.data.IncidentNeutron.from_endf(filename)
10191

10292

10393
@pytest.fixture(scope='module')
104-
def h2():
105-
endf_data = os.environ['OPENMC_ENDF_DATA']
94+
def h2(endf_data):
10695
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_002.endf')
10796
return openmc.data.IncidentNeutron.from_njoy(
10897
endf_file, temperatures=_TEMPERATURES)
10998

11099

111100
@pytest.fixture(scope='module')
112-
def am244():
113-
endf_data = os.environ['OPENMC_ENDF_DATA']
101+
def am244(endf_data):
114102
endf_file = os.path.join(endf_data, 'neutrons', 'n-095_Am_244.endf')
115103
return openmc.data.IncidentNeutron.from_njoy(endf_file)
116104

117105

118106
@pytest.fixture(scope='module')
119-
def ti50():
107+
def ti50(endf_data):
120108
"""Ti50 ENDF data (contains Multi-level Breit-Wigner resonance range and
121109
resonance covariance with LCOMP=1)."""
122-
endf_data = os.environ['OPENMC_ENDF_DATA']
123110
filename = os.path.join(endf_data, 'neutrons', 'n-022_Ti_050.endf')
124111
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
125112

126113

127114
@pytest.fixture(scope='module')
128-
def cf252():
115+
def cf252(endf_data):
129116
"""Cf252 ENDF data (contains RM resonance covariance with LCOMP=0)."""
130-
endf_data = os.environ['OPENMC_ENDF_DATA']
131117
filename = os.path.join(endf_data, 'neutrons', 'n-098_Cf_252.endf')
132118
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
133119

134120

135121
@pytest.fixture(scope='module')
136-
def th232():
122+
def th232(endf_data):
137123
"""Th232 ENDF data (contains RM resonance covariance with LCOMP=2)."""
138-
endf_data = os.environ['OPENMC_ENDF_DATA']
139124
filename = os.path.join(endf_data, 'neutrons', 'n-090_Th_232.endf')
140125
return openmc.data.IncidentNeutron.from_endf(filename, covariance=True)
141126

@@ -453,8 +438,7 @@ def test_laboratory(be9):
453438

454439

455440
@needs_njoy
456-
def test_correlated(tmpdir):
457-
endf_data = os.environ['OPENMC_ENDF_DATA']
441+
def test_correlated(tmpdir, endf_data):
458442
endf_file = os.path.join(endf_data, 'neutrons', 'n-014_Si_030.endf')
459443
si30 = openmc.data.IncidentNeutron.from_njoy(endf_file, heatr=False)
460444

@@ -480,8 +464,7 @@ def test_nbody(tmpdir, h2):
480464

481465

482466
@needs_njoy
483-
def test_ace_convert(run_in_tmpdir):
484-
endf_data = os.environ['OPENMC_ENDF_DATA']
467+
def test_ace_convert(run_in_tmpdir, endf_data):
485468
filename = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
486469
ace_ascii = 'ace_ascii'
487470
ace_binary = 'ace_binary'
@@ -515,8 +498,7 @@ def test_ace_table_types():
515498

516499

517500
@needs_njoy
518-
def test_high_temperature():
519-
endf_data = os.environ['OPENMC_ENDF_DATA']
501+
def test_high_temperature(endf_data):
520502
endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
521503

522504
# Ensure that from_njoy works when given a high temperature

tests/unit_tests/test_data_photon.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010

1111
@pytest.fixture(scope='module')
12-
def elements_endf():
12+
def elements_endf(endf_data):
1313
"""Dictionary of element ENDF data indexed by atomic symbol."""
14-
endf_data = os.environ['OPENMC_ENDF_DATA']
1514
elements = {'H': 1, 'O': 8, 'Al': 13, 'Cu': 29, 'Ag': 47, 'U': 92, 'Pu': 94}
1615
data = {}
1716
for symbol, Z in elements.items():
@@ -145,8 +144,8 @@ def test_export_to_hdf5(tmpdir, element):
145144
element2.export_to_hdf5(filename, 'w')
146145

147146

148-
def test_photodat_only(run_in_tmpdir):
149-
endf_dir = Path(os.environ['OPENMC_ENDF_DATA'])
147+
def test_photodat_only(run_in_tmpdir, endf_data):
148+
endf_dir = Path(endf_data)
150149
photoatomic_file = endf_dir / 'photoat' / 'photoat-001_H_000.endf'
151150
data = openmc.data.IncidentPhoton.from_endf(photoatomic_file)
152151
data.export_to_hdf5('tmp.h5', 'w')

0 commit comments

Comments
 (0)