Skip to content

Commit d800277

Browse files
authored
Merge pull request #42 from RemDelaporteMathurin/no-monkey
Improvements for use case without monkey patching
2 parents 30ca586 + dae867f commit d800277

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ mat1.add_element('C', 0.05)
162162

163163
mats = openmc.Materials([mat1])
164164

165-
mats.download_cross_section_data(
165+
odd.download_cross_section_data(
166+
mats,
166167
libraries=["FENDL-3.1d"],
167168
set_OPENMC_CROSS_SECTIONS=True,
168169
particles=["neutron"],
@@ -181,7 +182,8 @@ mat1.add_element('C', 0.05)
181182

182183
mats = openmc.Materials([mat1])
183184

184-
mats.download_cross_section_data(
185+
odd.download_cross_section_data(
186+
mats,
185187
libraries=['ENDFB-7.1-NNDC', 'TENDL-2019'],
186188
set_OPENMC_CROSS_SECTIONS=True,
187189
particles=["neutron"],
@@ -202,7 +204,8 @@ my_mat.add_s_alpha_beta('Be_in_BeO')
202204

203205
mats = openmc.Materials([my_mat])
204206

205-
mats.download_cross_section_data(
207+
odd.download_cross_section_data(
208+
mats,
206209
libraries=['ENDFB-7.1-NNDC', 'TENDL-2019'],
207210
set_OPENMC_CROSS_SECTIONS=True,
208211
particles=["neutron"],
@@ -221,7 +224,8 @@ mat1.add_element('C', 0.05)
221224

222225
mats = openmc.Materials([mat1])
223226

224-
mats.download_cross_section_data(
227+
odd.download_cross_section_data(
228+
mats,
225229
libraries=['ENDFB-7.1-NNDC', 'TENDL-2019'],
226230
set_OPENMC_CROSS_SECTIONS=True,
227231
particles=["neutron", "photon"],

src/openmc_data_downloader/utils.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def expand_materials_to_elements(materials: openmc.Materials):
9191

9292

9393
def download_cross_section_data(
94-
self,
94+
materials: openmc.Materials,
9595
libraries: typing.Iterable[str] = (
9696
"TENDL-2019",
9797
"ENDFB-7.1-NNDC",
@@ -103,7 +103,23 @@ def download_cross_section_data(
103103
set_OPENMC_CROSS_SECTIONS: bool = True,
104104
overwrite: bool = False,
105105
) -> str:
106-
""" """
106+
"""Download cross section data for materials
107+
108+
Args:
109+
materials: Materials for which to download cross section data
110+
libraries: list of libraries from which to download cross section data.
111+
destination: Specifies a folder location to save the downloaded files. By
112+
default, the files are saved in the current working directory.
113+
particles: list of particles for which to download cross section data ("neutron", "photon")
114+
set_OPENMC_CROSS_SECTIONS: Set the OPENMC_CROSS_SECTIONS environment variable
115+
overwrite: if set to True will overwrite any existing files
116+
117+
Raises:
118+
ValueError: If the particle is not one of the following: "neutron", "photon"
119+
120+
Returns:
121+
Path to the cross sections xml file
122+
"""
107123

108124
for entry in particles:
109125
if entry not in PARTICLE_OPTIONS:
@@ -114,7 +130,7 @@ def download_cross_section_data(
114130
dataframe = pd.DataFrame()
115131

116132
if "neutron" in particles:
117-
isotopes = expand_materials_to_isotopes(self)
133+
isotopes = expand_materials_to_isotopes(materials)
118134
# filters the large dataframe of all isotopes into just the ones you want
119135
dataframe_isotopes_xs = identify_isotopes_to_download(
120136
libraries=libraries,
@@ -123,14 +139,14 @@ def download_cross_section_data(
123139
dataframe = pd.concat([dataframe, dataframe_isotopes_xs])
124140

125141
if "photon" in particles:
126-
elements = expand_materials_to_elements(self)
142+
elements = expand_materials_to_elements(materials)
127143
dataframe_elements_xs = identify_elements_to_download(
128144
libraries=libraries,
129145
elements=elements,
130146
)
131147
dataframe = pd.concat([dataframe, dataframe_elements_xs])
132148

133-
sabs = expand_materials_to_sabs(self)
149+
sabs = expand_materials_to_sabs(materials)
134150
if len(sabs) > 0:
135151
dataframe_sabs_xs = identify_sabs_to_download(
136152
libraries=libraries,
@@ -148,7 +164,7 @@ def download_cross_section_data(
148164
cross_section_xml_path = create_cross_sections_xml(dataframe, destination)
149165

150166
if set_OPENMC_CROSS_SECTIONS is True:
151-
self.cross_sections = cross_section_xml_path
167+
materials.cross_sections = cross_section_xml_path
152168
# making the cross section xml requires openmc and returns None if
153169
# openmc is not found.
154170
if cross_section_xml_path is not None:

tests/test_use_in_openmc.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import openmc
88
import openmc_data_downloader
99

10+
import pytest
1011

11-
def test_materials_download():
12+
13+
@pytest.mark.parametrize("monkeypatch", [True, False])
14+
def test_materials_download(monkeypatch: bool):
1215
"""openmc.Materials are a container for openmc.Material objects. This
1316
test checks that they are handeled correctly"""
1417

@@ -24,12 +27,21 @@ def test_materials_download():
2427
my_mat_2.add_nuclide("As75", 1.3752e-3)
2528
mats = openmc.Materials([my_mat_1, my_mat_2])
2629

27-
mats.download_cross_section_data(
28-
destination="my_custom_nuclear_data_with_materials",
29-
libraries=["ENDFB-8.0-NNDC"],
30-
set_OPENMC_CROSS_SECTIONS=True,
31-
particles=["neutron"],
32-
)
30+
if monkeypatch:
31+
mats.download_cross_section_data(
32+
destination="my_custom_nuclear_data_with_materials",
33+
libraries=["ENDFB-8.0-NNDC"],
34+
set_OPENMC_CROSS_SECTIONS=True,
35+
particles=["neutron"],
36+
)
37+
else:
38+
openmc_data_downloader.download_cross_section_data(
39+
materials=mats,
40+
destination="my_custom_nuclear_data_with_materials",
41+
libraries=["ENDFB-8.0-NNDC"],
42+
set_OPENMC_CROSS_SECTIONS=True,
43+
particles=["neutron"],
44+
)
3345
mats.export_to_xml()
3446

3547
# Create a sphere of my_mat

0 commit comments

Comments
 (0)