4 from .nucleus_class
import nucleus
10 Class to deal with lists of nucleus objects
12 def __init__(self,names=None,A=None,Z=None,N=None,Y=None,X=None):
16 nuclist - list of nucleus names
23 if not (names
is None):
25 elif not(A
is None)
or not(Z
is None)
or not (N
is None):
31 self.
Y = np.array(X)/self.
A
33 self.
Y = np.zeros(len(self.
A))
37 return self.
df.__str__()
41 Initialize the class using the list of nucleinames
45 self.
N = self.
A-self.
Z
50 Initialize the class using A,Z,N
53 if np.sum([not(self.
A is None), not(self.
Z is None),
not (self.
N is None)]) <2:
54 raise Exception(
"You need to provide at least two of A, Z, N!")
56 if not (self.
A is None):
57 if not (self.
Z is None):
58 self.
A = np.array(self.
A)
59 self.
Z = np.array(self.
Z)
61 if len(self.
A)!=len(self.
Z):
62 raise Exception(
"A and Z had different lengths (A: "+str(len(self.
A))+
", Z:"+str(len(self.
Z))+
")!")
63 self.
N = self.
A-self.
Z
64 elif not (self.
N is None):
65 self.
A = np.array(self.
A)
66 self.
N = np.array(self.
N)
68 if len(self.
A)!=len(self.
N):
69 raise Exception(
"A and N had different lengths (A: "+str(len(self.
A))+
", N:"+str(len(self.
N))+
")!")
70 self.
Z = self.
A-self.
N
72 raise Exception(
"Something strange happened!")
73 elif not (self.
Z is None):
74 self.
N = np.array(self.
N)
75 self.
Z = np.array(self.
Z)
76 if len(self.
Z)!=len(self.
N):
77 raise Exception(
"N and Z had different lengths (N: "+str(len(self.
N))+
", Z:"+str(len(self.
Z))+
")!")
78 self.
A = self.
Z+self.
N
80 raise Exception(
"Something strange happened!")
84 raise Exception(
"N was smaller zero!")
85 elif np.min(self.
Z)<0 :
86 raise Exception(
"Z was smaller zero!")
87 elif np.min(self.
A)<0:
88 raise Exception(
"A was smaller zero!")
98 Y_new = np.zeros([max_A+1,])
99 for i
in range(len(A)):
100 Y_new[int(A[i])] += Y[i]
101 return np.array(range(max_A+1)),Y_new
123 Get sum over equal A's
126 return np.array(atmp),np.array(xtmp)
131 Get sum over equal Z's
139 Get sum over equal Z's
150 mask = (self.
A==1) & (self.
Z==1)
152 raise Exception(
"Problem when getting hydrogen abundances!")
154 return self.
Y[mask][0]
160 mask = (self.
A==1) & (self.
N==1)
162 raise Exception(
"Problem when getting neutron abundances!")
164 return self.
Y[mask][0]
170 mask = (self.
A==4) & (self.
Z==2)
172 raise Exception(
"Problem when getting alpha abundances!")
174 return self.
Y[mask][0]
183 df[
"nucleus"] = self.
names
188 df[
"X"] = self.
Y*self.
A
194 Merge another list of nuclei into the own one.
196 A_merged = np.append(self.
A,A)
197 Z_merged = np.append(self.
Z,Z)
198 c_list = np.array([A_merged,Z_merged])
201 unique=np.unique(c_list,axis=1)
202 return unique[0,:],unique[1,:]
207 Multiply either two instances of nuclei lists or a float with this instance.
209 if isinstance(other,float)
or isinstance(other,int):
211 elif isinstance(other,nucleus_multiple):
213 merge_N = merge_A-merge_Z
214 Y_list = np.zeros(len(merge_N))
215 for ind,atmp
in enumerate(merge_A):
216 mask_self = (self.
A ==merge_A[ind]) & (self.
Z ==merge_Z[ind])
217 mask_other = (other.A==merge_A[ind]) & (other.Z==merge_Z[ind])
218 if (np.any(mask_self))
and (np.any(mask_other)):
219 Y_list[ind] = self.
Y[mask_self][0]*other.Y[mask_other][0]
229 Multiply either two instances of nuclei lists or a float with this instance.
231 if isinstance(other,float)
or isinstance(other,int):
233 elif isinstance(other,nucleus_multiple):
235 merge_N = merge_A-merge_Z
236 Y_list = np.zeros(len(merge_N))
237 for ind,atmp
in enumerate(merge_A):
238 mask_self = (self.
A ==merge_A[ind]) & (self.
Z ==merge_Z[ind])
239 mask_other = (other.A==merge_A[ind]) & (other.Z==merge_Z[ind])
240 if (np.any(mask_self))
and (np.any(mask_other)):
241 Y_list[ind] = self.
Y[mask_self][0]/other.Y[mask_other][0]
257 Add to instances of this class. The abundances are added for each nucleus.
259 if isinstance(other,float)
or isinstance(other,int):
261 elif isinstance(other,nucleus_multiple):
263 merge_N = merge_A-merge_Z
264 Y_list = np.zeros(len(merge_N))
265 for ind,atmp
in enumerate(merge_A):
266 mask_self = (self.
A ==merge_A[ind]) & (self.
Z ==merge_Z[ind])
267 mask_other = (other.A==merge_A[ind]) & (other.Z==merge_Z[ind])
268 if (np.any(mask_self)):
269 Y_list[ind] += self.
Y[mask_self][0]
270 if (np.any(mask_other)):
271 Y_list[ind] += other.Y[mask_other][0]
281 Write the result (contained in self.__abundances) into a final abundance file. The format is:
284 path - Path to output file
292 output = np.array([A,Z,N,Y,X]).T
293 np.savetxt(path,output,fmt=[
'%7i',
'%7i',
'%7i',
' %1.9e',
' %1.9e'],header=
'A Z N Y X')
301 for i
in range(len(self.
A)):
302 X = self.
Y[i]*self.
A[i]
303 mafra =
'%1.6e' % float(X)
304 if np.isnan(X)
or X<=1e-15:
306 nam = self.
names[i].lower()
308 if (nam.strip() ==
'neutron')
or (nam.strip() ==
'neutron1'):
310 elif nam.strip() ==
'h1':
312 elif nam.strip() ==
'h2':
314 elif nam.strip() ==
'h3':
317 line+= nam.rjust(5) +
' '*7 + mafra
318 if i != len(self.
A)-1:
324 with open(path,
"w")
as f:
328 if __name__ ==
"__main__":
329 """ Test the class """
331 nlist = [
"o16",
"ni56"]
334 Y = [0.1,0.1,0.1,0.1]
338 Y = [0.2,0.2,0.3,0.4,0.1]