-
Notifications
You must be signed in to change notification settings - Fork 29
/
convert_mcnp70.py
executable file
·133 lines (102 loc) · 4.45 KB
/
convert_mcnp70.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
#!/usr/bin/env python3
"""
Convert ENDF/B-VII.0 ACE data from the MCNP5/6 distribution into an HDF5 library
that can be used by OpenMC. This assumes that you have a directory containing
files named endf70[a-k] and endf70sab. Optionally, if a recent photoatomic
library (e.g., eprdata14) is available, it can also be converted using the
--photon argument.
"""
import argparse
from collections import defaultdict
from pathlib import Path
import sys
import openmc.data
# Make sure Python version is sufficient
assert sys.version_info >= (3, 6), "Python 3.6+ is required"
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=CustomFormatter
)
parser.add_argument('-d', '--destination', type=Path, default=Path('mcnp_endfb70'),
help='Directory to create new library in')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
parser.add_argument('-p', '--photon', type=Path,
help='Path to photoatomic data library (eprdata12 or later)')
parser.add_argument('mcnpdata', type=Path,
help="Directory containing endf70[a-k], endf70sab, and mcplib")
args = parser.parse_args()
# Check arguments to make sure they're valid
assert args.mcnpdata.is_dir(), 'mcnpdata argument must be a directory'
if args.photon is not None:
assert args.photon.is_file(), 'photon argument must be an existing file'
# Get a list of all neutron ACE files
endf70 = args.mcnpdata.glob('endf70[a-k]')
# Create output directory if it doesn't exist
(args.destination / 'photon').mkdir(parents=True, exist_ok=True)
library = openmc.data.DataLibrary()
for path in sorted(endf70):
print(f'Loading data from {path}...')
lib = openmc.data.ace.Library(path)
# Group together tables for the same nuclide
tables = defaultdict(list)
for table in lib.tables:
zaid, xs = table.name.split('.')
tables[zaid].append(table)
for zaid, tables in sorted(tables.items()):
# Convert first temperature for the table
print(f'Converting: {tables[0].name}')
data = openmc.data.IncidentNeutron.from_ace(tables[0], 'mcnp')
# For each higher temperature, add cross sections to the existing table
for table in tables[1:]:
print(f'Adding: {table.name}')
data.add_temperature_from_ace(table, 'mcnp')
# Export HDF5 file
h5_file = args.destination / f'{data.name}.h5'
print(f'Writing {h5_file}...')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)
# Register with library
library.register_file(h5_file)
# Handle S(a,b) tables
endf70sab = args.mcnpdata / 'endf70sab'
if endf70sab.exists():
lib = openmc.data.ace.Library(endf70sab)
# Group together tables for the same nuclide
tables = defaultdict(list)
for table in lib.tables:
name, xs = table.name.split('.')
tables[name].append(table)
for zaid, tables in sorted(tables.items()):
# Convert first temperature for the table
print(f'Converting: {tables[0].name}')
data = openmc.data.ThermalScattering.from_ace(tables[0])
# For each higher temperature, add cross sections to the existing table
for table in tables[1:]:
print(f'Adding: {table.name}')
data.add_temperature_from_ace(table)
# Export HDF5 file
h5_file = args.destination / f'{data.name}.h5'
print(f'Writing {h5_file}...')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)
# Register with library
library.register_file(h5_file)
# Handle photoatomic data
if args.photon is not None:
lib = openmc.data.ace.Library(args.photon)
for table in lib.tables:
# Convert first temperature for the table
print(f'Converting: {table.name}')
data = openmc.data.IncidentPhoton.from_ace(table)
# Export HDF5 file
h5_file = args.destination / 'photon' / f'{data.name}.h5'
print(f'Writing {h5_file}...')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)
# Register with library
library.register_file(h5_file)
# Write cross_sections.xml
library.export_to_xml(args.destination / 'cross_sections.xml')