forked from sbradnam/JADE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsdirpyne.py
392 lines (326 loc) · 12.1 KB
/
xsdirpyne.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 17:21:15 2019
@author: Pyne https://github.com/pyne/pyne
Copyright 2011-2020, the PyNE Development Team. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE PYNE DEVELOPMENT TEAM ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of the stakeholders of the PyNE project or the employers of PyNE developers.
"""
import os
from typing import List, Tuple
class Xsdir(object):
"""This class stores the information contained in a single MCNP xsdir file.
Attributes
----------
f : file handle
The xsdir file.
filename : str
Path to the xsdir file.
directory : str
Path to the directory containing the xsdir file.
datapath : str
The data path specified in the first line of the xsdir file, if it
exists.
awr : dict
Maps material ids to their atomic weight ratios.
tables : list
Entries are XsdirTable objects, that appear in the same order as the
xsdir table lines.
Notes
-----
See MCNP5 User's Guide Volume 3 Appendix K for more information.
"""
def __init__(self, filename):
"""Parameters
----------
filename : str
Path to xsdir file.
"""
self.f = open(filename, 'r')
self.filename = os.path.abspath(filename)
self.directory = os.path.dirname(filename)
self.awr = {}
self.tables = []
self.read()
# It is useful to have a list of the available tables names to be
# computed only once at initializations
tablenames = []
for table in self:
name = table.name
zaidname = name[:-4]
libname = name[-3:]
tablenames.append((zaidname, libname))
self.tablenames = tablenames
def read(self):
"""Populate the Xsdir object by reading the file.
"""
# Go to beginning of file
self.f.seek(0)
# Read first section (DATAPATH)
line = self.f.readline()
words = line.split()
if words:
if words[0].lower().startswith('datapath'):
index = line.index('=')
self.datapath = line[index+1:].strip()
# Read second section
line = self.f.readline()
words = line.split()
assert len(words) == 3
assert words[0].lower() == 'atomic'
assert words[1].lower() == 'weight'
assert words[2].lower() == 'ratios'
while True:
line = self.f.readline()
words = line.split()
# Check for end of second section
if len(words) % 2 != 0 or words[0] == 'directory':
break
for zaid, awr in zip(words[::2], words[1::2]):
self.awr[zaid] = awr
# Read third section
while words[0] != 'directory':
words = self.f.readline().split()
while True:
words = self.f.readline().split()
if not words:
break
# Handle continuation lines
while words[-1] == '+':
extraWords = self.f.readline().split()
words = words[:-1] + extraWords # Correction
assert len(words) >= 7
# Create XsdirTable object and add to line
table = XsdirTable()
self.tables.append(table)
# All tables have at least 7 attributes
table.name = words[0]
table.awr = float(words[1])
table.filename = words[2]
table.access = words[3]
table.filetype = int(words[4])
table.address = int(words[5])
table.tablelength = int(words[6])
if len(words) > 7:
table.recordlength = int(words[7])
if len(words) > 8:
table.entries = int(words[8])
if len(words) > 9:
table.temperature = float(words[9])
if len(words) > 10:
table.ptable = (words[10] == 'ptable')
def find_table(self, name, mode='default'):
"""Find all tables for a given ZIAD.
*Modified for JADE, a bug was corrected since table.name do not
find natural zaids.
mode: 'default' default behaviour
'exact' check also the library
'default-fast' accelerated loop giving only the lib names
Parameters
----------
name : str
The ZIAD name.
Returns
-------
tables : list
All XsdirTable objects for a given ZIAD.
"""
if mode == 'exact':
# Faster, checks for the exact name
ans = self._exact_loop(name, self.tablenames)
elif mode == 'default':
# Checks all available libraries for the zaid
tables = []
for table in self:
# tablename = table.name.split('.')[0]
if name == table.name[:-4]:
tables.append(table)
ans = tables
elif mode == 'default-fast':
ans = self._all_fast_loop(name, self.tablenames)
return ans
@staticmethod
def _exact_loop(name: str, tablenames: List[Tuple[str, str]]) -> bool:
for zaidname, libname in tablenames:
if name == zaidname+'.'+libname:
return True
return False
@staticmethod
def _all_fast_loop(name: str, tablenames: List[Tuple[str, str]]) -> List[str]:
libs = []
for zaidname, libname in tablenames:
if name == zaidname:
libs.append(libname)
return libs
################# Added by Davide Laghi ###############################
def find_zaids(self, lib):
"""Find all zaids for a given library.
Parameters
----------
lib : str
The library suffix.
Returns
-------
tables : list
All XsdirTable objects for a given library.
"""
tables = []
for table in self:
tablelib = table.name.split('.')[-1]
if lib == tablelib:
tables.append(table)
return tables
############################################################################
def to_xsdata(self, filename):
"""Writes a Serpent xsdata file for all continuous energy xs tables.
Parameters
----------
filename : str
The output filename.
"""
xsdata = open(filename, 'w')
for table in self.tables:
if table.serpent_type == 1:
xsdata.write(table.to_serpent() + '\n')
xsdata.close()
def __iter__(self):
for table in self.tables:
yield table
def nucs(self):
"""Provides a set of the valid nuclide ids for nuclides contained
in the xsdir.
Returns
-------
valid_nucs : set
The valid nuclide ids.
"""
valid_nucs = set(nucname.id(table.name.split('.')[0])
for table in self.tables if
nucname.isnuclide(table.name.split('.')[0]))
return valid_nucs
class XsdirTable(object):
"""Stores all information that describes a xsdir table entry, which appears
as a single line in xsdir file. Attribute names are based off of those
found in the MCNP5 User's Guide Volume 3, appendix K.
Attributes
----------
name : str
The ZAID and library identifier, delimited by a '.'.
awr : float
The atomic mass ratio of the nuclide.
filename : str
The relative path of the file containing the xs table.
access : str
Additional string to specify an access route, such as UNIX directory.
This entry is typically 0.
filetype : int
Describes whether the file contains formated (1) or unformated (2)
file.
address : int
If filetype is 1, address is the line number of the xsdir table. If
filetype is 2, address is the record number.
tablelength : int
Length of the second block of a data table.
recordlength : int
Unused for filetype = 1. For filetype = 2, recordlength is the number
of entires per record times the size (in bytes) of each entry.
entries : int
Unused for filetype = 1. For filetype = 2, it is the number of entries
per record
temperature : float
Temperature in MeV for neutron data only.
ptable : bool
True if xs table describes continuous energy neutron data with
unresolved resonance range probability tables.
"""
def __init__(self):
self.name = None
self.awr = None
self.filename = None
self.access = None
self.filetype = None
self.address = None
self.tablelength = None
self.recordlength = None
self.entries = None
self.temperature = None
self.ptable = False
@property
def alias(self):
"""Returns the name of the table entry <ZIAD>.<library id>.
"""
return self.name
@property
def serpent_type(self):
"""Converts cross section table type to Serpent format:
:1: continuous energy (c).
:2: dosimetry table (y).
:3: termal (t).
"""
if self.name.endswith('c'):
return 1
elif self.name.endswith('y'):
return 2
elif self.name.endswith('t'):
return 3
else:
return None
@property
def metastable(self):
"""Returns 1 is xsdir table nuclide is metastable. Returns zero
otherwise.
"""
# Only valid for neutron cross-sections
if not self.name.endswith('c'):
return
# Handle special case of Am-242 and Am-242m
if self.zaid == '95242':
return 1
elif self.zaid == '95642':
return 0
# All other cases
A = int(self.name.split('.')[0]) % 1000
if A > 600:
return 1
else:
return 0
@property
def zaid(self):
"""Returns the ZIAD of the nuclide.
"""
return self.name[:self.name.find('.')]
def to_serpent(self, directory=''):
"""Converts table to serpent format.
Parameters
----------
directory : str
The directory where Serpent data is to be stored.
"""
# Adjust directory
if directory:
if not directory.endswith('/'):
directory = directory.strip() + '/'
return "{0} {0} {1} {2} {3} {4} {5:.11e} {6} {7}".format(
self.name,
self.serpent_type, self.zaid, 1 if self.metastable else 0,
self.awr, self.temperature/8.6173423e-11, self.filetype - 1,
directory + self.filename)
def __repr__(self):
return "<XsDirTable: {0}>".format(self.name)