-
Notifications
You must be signed in to change notification settings - Fork 176
/
nucname.pyx
193 lines (141 loc) · 4.09 KB
/
nucname.pyx
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
"""Python wrapper for nucname library."""
# Python imports
from collections import Iterable
# Cython imports
from libcpp.map cimport map
from libcpp.set cimport set as cpp_set
from cython.operator cimport dereference as deref
from cython.operator cimport preincrement as inc
# local imports
cimport std
cimport cpp_nucname
cimport stlconverters as conv
#
# Conversion dictionaries
#
LLzz = conv.map_to_dict_str_int(cpp_nucname.LLzz)
zzLL = conv.map_to_dict_int_str(cpp_nucname.zzLL)
#
# Elemental string sets
#
LAN = conv.cpp_to_py_set_str(cpp_nucname.LAN)
ACT = conv.cpp_to_py_set_str(cpp_nucname.ACT)
TRU = conv.cpp_to_py_set_str(cpp_nucname.TRU)
MA = conv.cpp_to_py_set_str(cpp_nucname.MA)
FP = conv.cpp_to_py_set_str(cpp_nucname.FP)
#
# Elemental integer sets
#
lan = conv.cpp_to_py_set_int(cpp_nucname.lan)
act = conv.cpp_to_py_set_int(cpp_nucname.act)
tru = conv.cpp_to_py_set_int(cpp_nucname.tru)
ma = conv.cpp_to_py_set_int(cpp_nucname.ma)
fp = conv.cpp_to_py_set_int(cpp_nucname.fp)
class NucTypeError(Exception):
def __init__(self, nuc=None):
self.nuc = nuc
def __str__(self):
msg = "Nuclide type not an int or str"
if self.nuc is not None:
msg += ": " + repr(self.nuc)
return msg
#
# Current Form Function
#
def current_form(nuc):
"""Find the current form of a nuclide.
Args:
* nuc (int or str): Input nuclide(s).
Returns:
* form_flag (str): The form identifier string from ["zzaaam", "LLAAAM", "MCNP"].
"""
cdef std.string cpp_curr_form
if isinstance(nuc, basestring):
cpp_curr_form = cpp_nucname.current_form(<char *> nuc)
elif isinstance(nuc, int) or isinstance(nuc, long):
cpp_curr_form = cpp_nucname.current_form(<int> nuc)
else:
raise NucTypeError(nuc)
return cpp_curr_form.c_str()
#
# zzaaam Functions
#
def zzaaam(nuc):
"""Converts a nuclide to its zzaaam (int) form.
Parameters
----------
nuc : int or str
Input nuclide.
Returns
-------
newnuc : int
Output nuclide in zzaaam form.
"""
if isinstance(nuc, basestring):
newnuc = cpp_nucname.zzaaam(<char *> nuc)
elif isinstance(nuc, int) or isinstance(nuc, long):
newnuc = cpp_nucname.zzaaam(<int> nuc)
else:
raise NucTypeError(nuc)
return newnuc
def LLAAAM(nuc):
"""Converts a nuclide to its LLAAAM form (str).
Args:
* nuc (int or str): Input nuclide.
Returns:
* newnuc (str): Output nuclide in LLAAAM form.
"""
cdef std.string newnuc
if isinstance(nuc, basestring):
newnuc = cpp_nucname.LLAAAM(<char *> nuc)
elif isinstance(nuc, int):
newnuc = cpp_nucname.LLAAAM(<int> nuc)
else:
raise NucTypeError(nuc)
return newnuc.c_str()
def mcnp(nuc):
"""Converts a nuclide to its MCNP form (int).
Args:
* nuc (int or str): Input nuclide.
Returns:
* newnuc (int): Output nuclide in MCNP form.
"""
if isinstance(nuc, basestring):
newnuc = cpp_nucname.mcnp(<char *> nuc)
elif isinstance(nuc, int):
newnuc = cpp_nucname.mcnp(<int> nuc)
else:
raise NucTypeError(nuc)
return newnuc
def serpent(nuc):
"""Converts a nuclide to its Serepnt form (str).
Args:
* nuc (int or str): Input nuclide.
Returns:
* newnuc (str): Output nuclide in serpent form.
"""
cdef std.string newnuc
if isinstance(nuc, basestring):
newnuc = cpp_nucname.serpent(<char *> nuc)
elif isinstance(nuc, int):
newnuc = cpp_nucname.serpent(<int> nuc)
else:
raise NucTypeError(nuc)
return newnuc.c_str()
#
# Helper Functions
#
def nuc_weight(nuc):
"""Calculates the weight of a nuclide in [amu].
Args:
* nuc (int or str): Input nuclide.
Returns:
* weight (float): Atomic weight of this nuclide [amu].
"""
if isinstance(nuc, basestring):
weight = cpp_nucname.nuc_weight(<char *> nuc)
elif isinstance(nuc, int):
weight = cpp_nucname.nuc_weight(<int> nuc)
else:
raise NucTypeError(nuc)
return weight