Page MenuHomec4science

sto.libmetalsSNII.py.svn-base
No OneTemporary

File Metadata

Created
Sun, Oct 20, 13:59

sto.libmetalsSNII.py.svn-base

#!/usr/bin/env python
import Ptools as pt
from pNbody import myNumeric
from numpy import *
from scipy.integrate import cumtrapz
import string
import libimf
def WriteParams(f,Mmin,Mmax,m_s,a_s,MetalFile,HeliumCoreFile,nelts,Mmin1,Mmax1,Mmin2,Mmax2,dM,n):
Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi = ReadAndInterpolate(MetalFile,HeliumCoreFile,nelts,Mmin1,Mmax1,Mmin2,Mmax2,dM,n)
# output steps
lStep1 = (log10(Mmax*1.25) - log10(Mmin1) )/float(n-1)
lStep2 = (log10(Mmax*1.25) - log10(Mmin2) )/float(n-1)
Ms1 = 10**(arange(n)*lStep1 + log10(Mmin1))
Ms2 = 10**(arange(n)*lStep2 + log10(Mmin2))
Ress, Hcos, Fes, Mgs, Oxs, Mes = ComputeEjectats(Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi, Ms1,Ms2)
IRess,IHcos,IFes,IMgs,IOxs,IMes = ComputeIntegrals(Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi ,Mmin,Mmax,m_s,a_s, Ms1,Ms2)
label = "#### Metal Parameters ####\n"
#WriteOutput(f,label,Ress, Hcos, Fes, Mgs, Oxs, Mes, n,nelts,Mmin1,lStep1,Mmin2,lStep2)
eltList = [ ("Re",Ress,Mmin2,lStep2),("Hc",Hcos,Mmin2,lStep2),("Fe",Fes,Mmin1,lStep1),("Mg",Mgs,Mmin1,lStep1),("Ox",Oxs,Mmin1,lStep1),("Me",Mes,Mmin1,lStep1) ]
WriteOutput2(f,label,eltList, n,nelts)
f.write("\n")
label = "#### Metal Ejecta Parameters ####\n"
#WriteOutput(f,label,IRess,IHcos,IFes,IMgs,IOxs,IMes,n,nelts,Mmin1,lStep1,Mmin2,lStep2)
eltList = [ ("IRe",IRess,Mmin2,lStep2),("IHc",IHcos,Mmin2,lStep2),("IFe",IFes,Mmin1,lStep1),("IMg",IMgs,Mmin1,lStep1),("IOx",IOxs,Mmin1,lStep1),("IMe",IMes,Mmin1,lStep1) ]
WriteOutput2(f,label,eltList, n,nelts)
def WriteOutput(f,label,IRess,IHcos,IFes,IMgs,IOxs,IMes,n,nelts,Mmin1,lStep1,Mmin2,lStep2):
f.write(label)
f.write("\n# Number of points / Number of elements\n")
f.write("%d %d\n"%(n,nelts))
f.write("\n# Int ( 1- Residual ) m^a\n")
f.write("\n%g %g\n"%(Mmin2,lStep2))
for i in range(len(IRess)):
f.write("%e\n"%IRess[i])
f.write("\n# Int ( 1- Helium Core ) m^a\n")
f.write("\n%g %g\n"%(Mmin2,lStep2))
for i in range(len(IHcos)):
f.write("%e\n"%IHcos[i])
f.write("\n# Int ( Fe ) m^a\n")
f.write("\n%g %g\n"%(Mmin1,lStep1))
for i in range(len(IFes)):
f.write("%e\n"%IFes[i])
f.write("\n# Int ( Mg ) m^a\n")
f.write("\n%g %g\n"%(Mmin1,lStep1))
for i in range(len(IMgs)):
f.write("%e\n"%IMgs[i])
f.write("\n# Int ( O ) m^a\n")
f.write("\n%g %g\n"%(Mmin1,lStep1))
for i in range(len(IOxs)):
f.write("%e\n"%IOxs[i])
f.write("\n# Int ( Metals ) m^a\n")
f.write("\n%g %g\n"%(Mmin1,lStep1))
for i in range(len(IMes)):
f.write("%e\n"%IMes[i])
def WriteOutput2(f,label,eltsList,n,nelts):
f.write(label)
f.write("\n# Number of points / Number of elements\n")
f.write("%d %d\n"%(n,nelts))
for elt,data,Mmin,lStep in eltsList:
f.write("\n# %s\n"%elt)
f.write("\n%g %g\n"%(Mmin,lStep))
for i in range(len(data)):
f.write("%e\n"%data[i])
def ReadAndInterpolate(MetalFile,HeliumCoreFile,nelts,Mmin1,Mmax1,Mmin2,Mmax2,dM,n):
'''
Read data
and compute linear interpolation, using
Mmin1,dM
Mmin2,dM
return *i
'''
# read ejecta
M1,Fe,Mg,Ox,Me = pt.io.read_ascii(MetalFile,[0,1,2,3,4],skipheader=True,cchar='#')
Fe = Fe/M1
Mg = Mg/M1
Ox = Ox/M1
Me = Me/M1
# read residual and helium core
M2,Res,Hco = pt.io.read_ascii(HeliumCoreFile,[0,1,2],skipheader=True,cchar='#')
Res = Res/M2
Hco = Hco/M2
# linear interpolation
Mi1 = arange(Mmin1,Mmax1+dM,dM)
Mi2 = arange(Mmin2,Mmax2+dM,dM)
# interpolate
Fei = myNumeric.lininterp1d(Mi1.astype(float32),M1.astype(float32),Fe.astype(float32))
Mgi = myNumeric.lininterp1d(Mi1.astype(float32),M1.astype(float32),Mg.astype(float32))
Oxi = myNumeric.lininterp1d(Mi1.astype(float32),M1.astype(float32),Ox.astype(float32))
Mei = myNumeric.lininterp1d(Mi1.astype(float32),M1.astype(float32),Me.astype(float32))
Resi = myNumeric.lininterp1d(Mi2.astype(float32),M2.astype(float32),Res.astype(float32))
Hcoi = myNumeric.lininterp1d(Mi2.astype(float32),M2.astype(float32),Hco.astype(float32))
Fei = where(Fei<=0,0,Fei)
Mgi = where(Mgi<=0,0,Mgi)
Oxi = where(Oxi<=0,0,Oxi)
Mei = where(Mei<=0,0,Mei)
Resi = where(Resi<=0,0,Resi)
Hcoi = where(Hcoi<=0,0,Hcoi)
return Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi
def ComputeEjectats(Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi, Ms1,Ms2):
'''
From the interpolated values *i,
return interpolation of ejectats
'''
###############################################
# output
###############################################
Fes = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),Fei.astype(float32))
Mgs = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),Mgi.astype(float32))
Oxs = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),Oxi.astype(float32))
Mes = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),Mei.astype(float32))
Ress = myNumeric.lininterp1d(Ms2.astype(float32),Mi2.astype(float32),(1-Resi).astype(float32))
Hcos = myNumeric.lininterp1d(Ms2.astype(float32),Mi2.astype(float32),(1-Hcoi).astype(float32))
return Ress,Hcos,Fes,Mgs,Oxs,Mes
def ComputeIntegrals(Mi1,Fei,Mgi,Oxi,Mei, Mi2,Resi,Hcoi, Mmin,Mmax,m_s,a_s, Ms1,Ms2):
'''
From the interpolated values *i,
integrate and return interpolation
'''
# imf
b_s = libimf.ComputeConstants(m_s,a_s,Mmin,Mmax)
imf1 = libimf.imfv(Mi1,array(m_s),array(a_s),array(b_s),Mmin,Mmax)
imf2 = libimf.imfv(Mi2,array(m_s),array(a_s),array(b_s),Mmin,Mmax)
IFe = cumtrapz(imf1 * Fei,Mi1)
IMg = cumtrapz(imf1 * Mgi,Mi1)
IOx = cumtrapz(imf1 * Oxi,Mi1)
IMe = cumtrapz(imf1 * Mei,Mi1)
IRes = cumtrapz(imf2 * (1-Resi),Mi2)
IHco = cumtrapz(imf2 * (1-Hcoi),Mi2)
###############################################
# output
###############################################
IFes = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),IFe.astype(float32))
IMgs = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),IMg.astype(float32))
IOxs = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),IOx.astype(float32))
IMes = myNumeric.lininterp1d(Ms1.astype(float32),Mi1.astype(float32),IMe.astype(float32))
IRess = myNumeric.lininterp1d(Ms2.astype(float32),Mi2.astype(float32),IRes.astype(float32))
IHcos = myNumeric.lininterp1d(Ms2.astype(float32),Mi2.astype(float32),IHco.astype(float32))
return IRess,IHcos,IFes,IMgs,IOxs,IMes
def Plot():
###############################################
# plot
###############################################
# plot 1
pt.subplot(211)
pt.plot(M2,Res,'ko')
pt.plot(Mi2,Resi,'k-')
pt.plot(M2,Hco,'ro')
pt.plot(Mi2,Hcoi,'r-')
pt.plot(M1,Fe,'yo')
pt.plot(Mi1,Fei,'y-')
pt.plot(M1,Mg,'go')
pt.plot(Mi1,Mgi,'g-')
pt.plot(M1,Ox,'bo')
pt.plot(Mi1,Oxi,'b-')
pt.plot(M1,Me,'co')
pt.plot(Mi1,Mei,'c-')
pt.semilogx()
pt.semilogy()
pt.axis([Mmin,Mmax1,1e-4,1e0])
# plot 2
pt.subplot(212)
pt.plot(Mi1[1:],IFe*500,'y')
pt.plot(Mi1[1:],IMg*500,'g')
pt.plot(Mi1[1:],IOx*10,'b')
pt.plot(Mi1[1:],IMe*10,'c')
pt.plot(Mi2[1:],IRes,'k')
pt.plot(Mi2[1:],IHco,'r')
'''
pt.plot(Ms1,IFes*500,'o')
pt.plot(Ms1,IMgs*500,'o')
pt.plot(Ms1,IOxs*10 ,'o')
pt.plot(Ms1,IMes*10 ,'o')
pt.plot(Ms2,IRess,'o')
pt.plot(Ms2,IHcos,'o')
'''
pt.semilogx()
pt.axis([Mmin,Mmax1,0.,4*b])
pt.show()

Event Timeline