-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNuclides.py
148 lines (129 loc) · 5.01 KB
/
Nuclides.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
"""
This module provides data structures and functions for storing nuclear
reaction network data and identifying nuclei with their (n,z) values.
The (n,z) data is provided by nuclides.xml, obtained from the JINA nuclide
database (https://groups.nscl.msu.edu/jina/nucdatalib/tools).
Copyright 2015 Donald E. Willcox
This file is part of nucplotlib.
nucplotlib is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
nucplotlib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with nucplotlib. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import print_function
import numpy as np
import os
import xml.etree.ElementTree as etree
class Nuclide:
def __init__(self,n=None,z=None,x=None):
self.n = int(n)
self.z = int(z)
self.a = self.n + self.z
if x.all():
self.x = np.array(x)
else:
self.x = []
def set_abundance(self,x):
self.x = np.array(x)
class Nuclides:
def __init__(self):
self.nucdata = []
self.time = []
self.xlen = None
## Create a nuclide look-up table for Z and N mapping
self.nuclide_lut = {}
# put in neutrons and protons, named as they appear from TORCH
self.nuclide_lut['h1'] = {'abb':'h', 'z':1, 'a':1, 'n':0}
self.nuclide_lut['h2'] = {'abb':'h', 'z':1, 'a':2, 'n':1}
self.nuclide_lut['h3'] = {'abb':'h', 'z':1, 'a':3, 'n':2}
self.nuclide_lut['neut'] = {'abb':'neut', 'z':0, 'a':1, 'n':1}
self.nuclide_lut['prot'] = {'abb':'prot', 'z':1, 'a':1, 'n':0}
# Load Reaclib V1.0 Masses from a reaclib 'nuclides.xml' file
try:
thisdir = os.path.dirname(os.path.realpath(__file__))
tree = etree.parse(os.path.join(thisdir,
'nuclides.xml'))
except:
print('Error: nuclides.xml missing or corrupt!')
exit()
nuc_xml = tree.iter('nuclide')
for entry in nuc_xml:
nuc_entry = {}
nuc_entry['nuc'] = entry.attrib['nuc']
nuc_entry['z'] = int(entry.attrib['zvalue'])
nuc_entry['a'] = int(entry.attrib['mvalue'])
nuc_entry['n'] = nuc_entry['a']-nuc_entry['z']
self.nuclide_lut[entry.attrib['comp']] = nuc_entry
def is_nuclide(self,abbrev):
if abbrev.lower() in self.nuclide_lut:
return True
else:
return False
def clr_dataset(self):
self.nucdata = []
self.time = []
self.xlen = None
def add_nuc(self,abbrev=None,x=None,n=None,z=None):
if abbrev:
# Preferentially lookup isotope n and z using abbrev.
this_nuclide = self.nuclide_lut[abbrev.lower()]
nn = this_nuclide['n']
zz = this_nuclide['z']
else:
nn = int(n)
zz = int(z)
nuc = Nuclide(nn,zz,x)
self.nucdata.append(nuc)
if not self.xlen and x.all():
self.xlen = len(x)
elif self.xlen and x.all():
if (self.xlen != len(x)):
print('Error: unequal abundance vector lengths!')
exit()
def set_time(self,time):
self.time = np.array(time)
if not self.xlen:
self.xlen = len(time)
else:
if (self.xlen != len(time)):
print('Error: unequal abundance vector lengths!')
exit()
def load_dataset(self,data):
# data should be a dictionary keyed by isotope abbreviations
# values should be numpy arrays
# additional k,v pairs which are not isotopes may be present.
# load time if present
if 'time' in data:
self.set_time(data['time'])
# load any isotopes present as identified by abbreviation
dsk = [k.lower() for k in data.keys()]
for nuc in self.nuclide_lut.keys():
if nuc in dsk:
self.add_nuc(abbrev=nuc,x=data[nuc])
def get_range_nz(self):
# returns maximum and minimum n and z values in the dataset
# returns a dictionary d:
# d = {'n': [min_n,max_n], 'z': [min_z,max_z]}
d = {}
n = [-1,-1]
z = [-1,-1]
for nuc in self.nucdata:
if n[0] == -1:
n[0] = nuc.n
else:
n[0] = min([n[0],nuc.n])
n[1] = max([n[1],nuc.n])
if z[0] == -1:
z[0] = nuc.z
else:
z[0] = min([z[0],nuc.z])
z[1] = max([z[1],nuc.z])
d['n'] = n
d['z'] = z
return d