winvn_class.py
Go to the documentation of this file.
1 import pandas as pd
2 import numpy as np
3 from scipy.interpolate import interp1d
4 
5 
6 
7 class winvn(object):
8 
9  def __init__(self,path):
10  """
11  Class to read and manage a winvn
12  """
13  self.__path = path
14 
15 
16  def reset_winvn(self):
17  self.__df = self.__df_reset
18 
19  def read_winvn(self):
20  """
21  Read the winvn. (stolen from Carlos)
22  """
23  with open(self.__path,'r') as winvn:
24  """Open file with the context manager and extract data."""
25  # read file content
26  winvn.readline()
27 
28  # read the ugly log temperature string
29  self.T_string = winvn.readline().strip()
30  str_size = 3
31  logT = np.array([int(self.T_string[i:i+str_size])
32  for i in range(0,len(self.T_string),str_size)])
33  logT[-1]*=10 # I think there is a factor 10 missing!!!
34  logT = logT*1e-2
35 
36  # skip the hacky name part, i.e. look for the last name,
37  # it appears twice
38  name1,name2 = ("","-")
39  nameList = []
40  while (name1!=name2):
41  name2 = name1
42  name1 = winvn.readline().strip()
43  nameList.append(name1)
44 
45  # get rid of the last name which appears twice...
46  #...why don't they just give the number of elements!!!
47  nameList.pop()
48 
49 
50  # initialize element data container
51  eleList = []
52 
53 
54  # now, get the data for each of these elements
55  for element in nameList:
56  # extract element data
57  #..what is mass excess again??!! I hate c12 related quantities!!
58  wv_l = winvn.readline().split()
59  if len(wv_l)==7:
60  name, A, Z, N, sp, mass_excess, no_idea = wv_l
61  else:
62  name, A, Z, N, sp, mass_excess = wv_l
63  no_idea="unknown"
64  # I don't trust that guys! Let's check the order!
65  if (name != element):
66  sys.exit("OMG!!!")
67  # extract partition function
68  pf_list = []
69  # read three lines...
70  for i in range(3):
71  pf_list.extend(winvn.readline().split())
72  pf_values = np.array(pf_list,dtype=float)
73 
74  func = interp1d(logT,pf_values,bounds_error=False,kind="cubic",fill_value="extrapolate")
75  # save element data in a list
76  eleList.append([int(Z),int(N),name,float(A),float(sp),
77  float(mass_excess),str(no_idea), pf_values,func] )
78 
79  # give the element data list entries names
80  column_names = ['Z','N','name','A','spin','mass excess','mass model',
81  'partition function','function']
82  # build table
83  self.__df = pd.DataFrame(eleList)
84  # set column names
85  self.__df.columns=column_names
86  self.__df = self.__df.set_index("name")
87  self.__df["name"] = self.__df.index
88 
89  self.__df_reset = self.__df.copy()
90 
91  # Calculate binding energies
92  mexc_prot = self.__df.loc["p","mass excess"]
93  mexc_neut = self.__df.loc["n","mass excess"]
94  self.__df["binding energy"] = mexc_prot*self.__df["Z"]+mexc_neut*self.__df["N"]-self.__df["mass excess"]
95 
96  def filter_with_sunet(self,nuclei):
97  self.__df = self.__df.loc[nuclei,:]
98 
99  def write_winvn(self,path="winvn.dat"):
100  """
101  Save the winvn to a file.
102  """
103  out=""
104  out+="\n"
105  out+=self.T_string+"\n"
106 
107  # Create the long list of nuclei
108  for ind,row in self.__df.iterrows():
109  out+=row["name"].rjust(5)+"\n"
110  out+=row["name"].rjust(5)+"\n"
111 
112  for ind,row in self.__df.iterrows():
113  out+=row["name"].rjust(5)+5*" "+"{:.3f}".format(row["A"]).rjust(7)+\
114  " "+str(int(row["Z"])).rjust(3)+" "+str(int(row["N"])).rjust(3)+\
115  "{:.1f}".format(row["spin"]).rjust(6)+"{:.3f}".format(row["mass excess"]).rjust(10)+\
116  row["mass model"].rjust(6)+\
117  "\n"
118  out+=" "
119  # Now the partition functions
120  for i in range(len(row['partition function'])):
121  out+="{:.5E}".format(row['partition function'][i]).rjust(12)
122  if (i+1)%8 == 0:
123  out+="\n "
124  out = out[:-1]
125 
126  with open(path,"w") as f:
127  f.write(out)
128 
129 
130  def calculate_Sn(self):
131  """
132  Calculate the neutron separation energies
133  """
134  mexc_n = self.__df.loc["n","mass excess"]
135 
136  # Put mass excesses in 2D array
137  Zwinvn = self.__df["Z"].values
138  Nwinvn = self.__df["N"].values
139  mexc = self.__df["mass excess"].values
140 
141  # Create 2D array with Z and N, fill non existing with nans
142  Sn_Z_N_winvn = np.zeros((Zwinvn.max()+1,Nwinvn.max()+1))
143  Sn_Z_N_winvn[:] = np.nan
144  Sn_Z_N_winvn[Zwinvn,Nwinvn] = mexc
145 
146  # Now calculate the neutron separation energy
147  Sn_Z_N_winvn[:,1:] = (Sn_Z_N_winvn[:,0:-1] + mexc_n) - Sn_Z_N_winvn[:,1:]
148 
149  # Put it into df
150  self.__df["Sn"] = Sn_Z_N_winvn[Zwinvn,Nwinvn]
151 
152 
153  def rate_factor(self,reactants,products,temperature):
154  """
155  Calculate the rate factor for a given reaction
156  Reactants: List of nuclei of the reactants of the inverse reaction
157  Products: List of nuclei of the products of the inverse reaction
158  temperature: temperature in GK
159  """
160  # Nominator
161  nom = 1e0
162  for reac in reactants:
163  nom= nom*self.__df.loc[reac,"function"](temperature)
164  # Denominator
165  den = 1e0
166  for prod in products:
167  den= den*self.__df.loc[prod,"function"](temperature)
168  factor = den/nom
169  return factor
170 
171 
172  def get_dataframe(self):
173  """
174  Get the dataframe
175  """
176  return self.__df
177 
178  def set_dataframe(self,value):
179  """
180  Get the dataframe
181  """
182  self.__df = value
183 
184 
185 if __name__=="__main__":
186  w = winvn("data/winvne_v2.0.dat")
187  w.read_winvn()
188  fac = w.rate_factor(["mg24"],["he4","ne20"],np.linspace(1,5))
189  print(fac)
src_files.winvn_class.winvn.__df_reset
__df_reset
Definition: winvn_class.py:89
src_files.winvn_class.winvn.write_winvn
def write_winvn(self, path="winvn.dat")
Definition: winvn_class.py:99
src_files.winvn_class.winvn.get_dataframe
def get_dataframe(self)
Definition: winvn_class.py:172
src_files.winvn_class.winvn.reset_winvn
def reset_winvn(self)
Definition: winvn_class.py:16
src_files.winvn_class.winvn.__df
__df
Definition: winvn_class.py:17
src_files.winvn_class.winvn.rate_factor
def rate_factor(self, reactants, products, temperature)
Definition: winvn_class.py:153
summarize.format
format
Definition: summarize.py:77
src_files.winvn_class.winvn.__path
__path
Definition: winvn_class.py:13
src_files.winvn_class.winvn.__init__
def __init__(self, path)
Definition: winvn_class.py:9
inter_module::interp1d
subroutine, public interp1d(n, xp, xb, yp, res, flin, itype)
Interface for 1D interpolation routines.
Definition: inter_module.f90:100
src_files.winvn_class.winvn.calculate_Sn
def calculate_Sn(self)
Definition: winvn_class.py:130
src_files.winvn_class.winvn.read_winvn
def read_winvn(self)
Definition: winvn_class.py:19
src_files.winvn_class.winvn.set_dataframe
def set_dataframe(self, value)
Definition: winvn_class.py:178
src_files.winvn_class.winvn.T_string
T_string
Definition: winvn_class.py:29
src_files.winvn_class.winvn
Definition: winvn_class.py:7
src_files.winvn_class.winvn.filter_with_sunet
def filter_with_sunet(self, nuclei)
Definition: winvn_class.py:96