forked from openmc-dev/data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_tendl_rand.py
executable file
·367 lines (310 loc) · 11.1 KB
/
convert_tendl_rand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
import argparse
import os
import sys
import tarfile
from pathlib import Path
from multiprocessing import Pool
import openmc.data
from utils import download
description = """
Download random TENDL libraries from PSI and convert it to a HDF5 library for use with OpenMC.
Only certain nuclides are available from PSI. This script generates a cross_sections_tendl.xml
file with random TENDL evaluations plus a standard library located in 'OPENMC_CROSS_SECTIONS'
"""
class CustomFormatter(
argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter
):
pass
def main():
n_choices = [
"all",
"O16",
"Si28",
"Si29",
"Si30",
"Fe54",
"Fe56",
"Fe57",
"Fe58",
"Na23",
"Pu240",
]
parser = argparse.ArgumentParser(
description=description, formatter_class=CustomFormatter
)
parser.add_argument("-n", "--nuclides", choices=n_choices, nargs="+",
default=["Fe56"], help="The nuclides to be downloaded. Available are: "
"'O16','Si28', 'Si29','Si30', 'Fe54', 'Fe56', 'Fe57', 'Fe58', 'Na23', 'Pu240'. Use 'all' for all availiable")
parser.add_argument("-d", "--destination", default=None,
help="Directory to create new library in")
parser.add_argument("-x", "--xlib", default=None,
help="cross_section.xml library to add random evaluations to")
parser.add_argument("-b", "--batch", action="store_true",
help="supresses standard in")
parser.add_argument("-f", "--format_only", default=False,
help="Only format previously sampled files to HDF5")
args = parser.parse_args()
if "all" in args.nuclides:
nuclides = [
"O16",
"Si28",
"Si29",
"Si30",
"Fe54",
"Fe56",
"Fe57",
"Fe58",
"Na23",
"Pu240",
]
else:
nuclides = args.nuclides
script_dir = Path.cwd()
library_name = "tendl_rand" # this could be added as an argument to allow different libraries to be downloaded
# the destination is decided after the release is know to avoid putting the release in a folder with a misleading name
if args.destination is None:
output_dir = script_dir / library_name
else:
output_dir = Path(args.destination).resolve()
xlib = args.xlib
if xlib == None:
xlib = os.getenv("OPENMC_CROSS_SECTIONS")
else:
xlib = Path(xlib).resolve()
endf_files_dir = output_dir / "endf"
ace_files_dir = output_dir / "ace"
hdf5_files_dir = output_dir / "hdf5"
format_only = args.format_only
release_details = {
"ENDF2017": {
"base_url": "https://tendl.web.psi.ch/tendl_2017/tar_files/",
"ending": ".random.tgz",
},
"ENDF2015": {
"base_url": "https://tendl.web.psi.ch/tendl_2015/tar_files/",
"ending": ".random.tgz",
},
"ACE2015": {
"base_url": "https://tendl.web.psi.ch/tendl_2015/tar_files/",
"ending": ".10.2016.tgz",
},
}
nuclide_details = {
"O16": {
"release": "ENDF2017",
"filename": "O016",
"webname": "O016",
"file_num": 642,
"down_size": 60,
"file_size": 244,
"is_it_ENDF": True,
"gunzip": False,
},
"Si28": {
"release": "ENDF2017",
"filename": "Si028",
"webname": "Si28",
"file_num": 600,
"down_size": 110,
"file_size": 428,
"is_it_ENDF": True,
"gunzip": True,
},
"Si29": {
"release": "ENDF2017",
"filename": "Si029",
"webname": "Si29",
"file_num": 600,
"down_size": 100,
"file_size": 425,
"is_it_ENDF": True,
"gunzip": True,
},
"Si30": {
"release": "ENDF2017",
"filename": "Si030",
"webname": "Si30",
"file_num": 600,
"down_size": 80,
"file_size": 337,
"is_it_ENDF": True,
"gunzip": True,
},
"Fe54": {
"release": "ENDF2017",
"filename": "Fe054",
"webname": "Fe054",
"file_num": 501,
"down_size": 320,
"file_size": 1300,
"is_it_ENDF": True,
"gunzip": False,
},
"Fe56": {
"release": "ENDF2017",
"filename": "Fe056",
"webname": "Fe056",
"file_num": 614,
"down_size": 410,
"file_size": 1600,
"is_it_ENDF": True,
"gunzip": False,
},
"Fe57": {
"release": "ENDF2017",
"filename": "Fe057",
"webname": "Fe057",
"file_num": 733,
"down_size": 525,
"file_size": 2000,
"is_it_ENDF": True,
"gunzip": False,
},
"Fe58": {
"release": "ENDF2017",
"filename": "Fe058",
"webname": "Fe058",
"file_num": 501,
"down_size": 280,
"file_size": 1100,
"is_it_ENDF": True,
"gunzip": False,
},
"Na23": {
"release": "ENDF2017",
"filename": "Na023",
"webname": "Na23",
"file_num": 832,
"down_size": 130,
"file_size": 544,
"is_it_ENDF": True,
"gunzip": False,
},
"Pu240": {
"release": "ENDF2017",
"filename": "Pu240",
"webname": "Pu240",
"file_num": 630,
"down_size": 200,
"file_size": 900,
"is_it_ENDF": True,
"gunzip": False,
},
}
if not format_only:
download_file_size = 0
uncompressed_file_size = 0
num_of_files = 0
for i in nuclides:
download_file_size += nuclide_details[i]["down_size"]
uncompressed_file_size += nuclide_details[i]["file_size"]
num_of_files += nuclide_details[i]["file_num"]
download_size = f"{download_file_size} MB"
uncom_file_size = f"{uncompressed_file_size} MB"
if download_file_size > 1000:
download_size = f"{download_file_size / 1000} GB"
if uncompressed_file_size > 1000:
uncom_file_size = f"{uncompressed_file_size / 1000} GB"
download_warning = """
WARNING: This script will download {} of
data, which is {} of data when processed.
This corresponds to {} random cross sections.
The nuclides to be processed are:
{}
Are you sure you want to continue? ([y]/n)
""".format(
download_size, uncom_file_size, num_of_files, nuclides
)
response = input(download_warning) if not args.batch else "y"
if response.lower().startswith("n"):
sys.exit()
# ==============================================================================
# DOWNLOAD FILES FROM WEBSITE
files_complete = []
for nucs in nuclides:
# Establish connection to URL
url = (
release_details[nuclide_details[nucs]["release"]]["base_url"]
+ nuclide_details[nucs]["webname"]
+ release_details[nuclide_details[nucs]["release"]]["ending"]
)
print(f"Downloading {nucs}...")
downloaded_file = download(url)
# ==============================================================================
# EXTRACT FILES FROM TGZ
for nucs in nuclides:
f = nuclide_details[nucs]["webname"] + ".random.tgz"
suffix = nucs
is_it_ENDF = nuclide_details[nucs]["is_it_ENDF"]
out_dir = endf_files_dir if is_it_ENDF else ace_files_dir
with tarfile.open(f, "r") as tgz:
print(f"Extracting {f}...")
tgz.extractall(path=out_dir / suffix)
# ==============================================================================
# Format file names
for nucs in nuclides:
f = nuclide_details[nucs]["webname"] + ".random.tgz"
is_it_ENDF = nuclide_details[nucs]["is_it_ENDF"]
numFiles = nuclide_details[nucs]["file_num"]
if is_it_ENDF:
out_dir = endf_files_dir / nucs
prefix = "n-"
suffix = "-rand-"
for i in range(numFiles):
OldNumber = f"{i:04}"
OldFile = prefix + nuclide_details[nucs]["filename"] + suffix + OldNumber
newFile = f"{nucs}-{i+1}"
if nuclide_details[nucs]["gunzip"]:
os.system(f"gunzip {out_dir}/{OldFile}.gz")
(out_dir / OldFile).rename(out_dir / newFile)
os.remove(f)
# ==============================================================================
# Convert ENDF files to HDF5 with njoy
def process_neutron_random(nuc, i, out_dir, in_dir, file_num):
"""Process ENDF neutron sublibrary file into HDF5 and write into a
specified output directory."""
fileIn = in_dir / f"{nuc}-{i}"
fileOut = out_dir / f"{nuc}-{i}.h5"
data = openmc.data.IncidentNeutron.from_njoy(fileIn)
data.name = f"{nuc}-{i}"
data.export_to_hdf5(fileOut, "w")
if i % 40 == 0:
print(f"Nuclide {nuc} {i}/{file_num} finished")
print("Beginning NJOY processing")
with Pool() as pool:
results = []
for nuc in nuclides:
file_num = nuclide_details[nuc]["file_num"]
in_dir = endf_files_dir / nuc
out_dir = hdf5_files_dir / nuc
hdf5_files_dir.mkdir(exist_ok=True)
out_dir.mkdir(exist_ok=True)
print(f"Beginning nuclide {nuc} ...")
for i in range(1, file_num + 1):
func_args = (nuc, i, out_dir, in_dir, file_num)
r = pool.apply_async(process_neutron_random, func_args)
results.append(r)
for r in results:
r.wait()
# ==============================================================================
# Create xml library
lib = openmc.data.DataLibrary()
lib = lib.from_xml(xlib) # Gets current
for nuc in nuclides:
file_num = nuclide_details[nuc]["file_num"]
out_dir = hdf5_files_dir / nuc
for i in range(1, file_num + 1):
fileOut = out_dir / f"{nuc}-{i}.h5"
lib.register_file(fileOut)
pre = output_dir / "cross_sections_pre.xml"
post = output_dir / "cross_sections_tendl.xml"
lib.export_to_xml(pre)
if post.exists():
command = f"combine_libraries.py -l {pre} {post} -o {post}"
os.system(command)
else:
lib.export_to_xml(post)
pre.unlink()
if __name__ == '__main__':
main()