Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.32 KB | None | 0 0
  1. '''This program calculates mass and density relationships for iron and carbon white dwarf stars
  2. based upon a central density. This is done by using the balance of gravity
  3. and pressure of relatavistic degenerate electron gas and solves using the fourth order
  4. Runge-Kutta algorithm'''
  5. #Norman Khan 2017
  6.  
  7. ######################################
  8. import math
  9. from math import pi
  10. import matplotlib.pyplot as plt
  11. import numpy as np
  12. #from uncertainties import ufloat
  13.  
  14. #####################################
  15. ##############CONSTANTS##############
  16. #####################################
  17. #Physical Constants
  18. SPEED_OF_LIGHT=   2.99792458e8      #metres per second
  19. H_BAR=            1.05457266e-34    #joule seconds
  20. ELECTRON_CHARGE=  1.60217733e-19    #coulombs
  21. ELECTRON_MASS=    9.1093897e-31     #kilograms
  22. PROTON_MASS=      1.6726231e-27     #kilograms
  23. GRAVITATION=      6.67259e-11       #MKS units
  24. SOLAR_MASS=       1.98855e30        #kilograms
  25. SOLAR_RADIUS=     6.957e8           #metres
  26.  
  27. #Known Stars
  28. StarNames = ['Sirius B', '40 Eri B', 'Stein 2051']
  29. StarMass = [1.053, 0.48, 0.50]          #Solar Masses
  30. StarMassErr = [0.028, 0.02, 0.05]      
  31. StarRadius = [0.0074, 0.0124, 0.0115]   #Solar Radii
  32. StarRadiusErr = [0.0006, 0.0005, 0.0012]
  33. Markers = ['o', '*', '^']               #markers used on scatter plot
  34.  
  35. #####################################
  36. ##############VARIABLES##############
  37. #####################################
  38. CENTRAL_DENSITY=0.0                             #Density at the center of the white Dwarf
  39.  
  40. Y_e = 0.5                                       #Number of electrons per nucleon
  41.                                      
  42. Y_e_CARBON = 0.5                                #For Carbon Based White Dwarfs      
  43. Y_e_IRON = 26.0/56.0                            #For iron white Dwarfs
  44.  
  45.  
  46. #For full explaination of these constants see associated paper.
  47. RHO0 = 9.79e8 / Y_e                             #Bunch of constants over number of electrons per nucleon used for scaling density
  48. RHO0_CARBON = 9.79e8 / Y_e_CARBON
  49. RHO0_IRON = 9.79e8 / Y_e_IRON
  50.  
  51. R0 = ((3.0*Y_e*ELECTRON_MASS*SPEED_OF_LIGHT**2.0)/(4.0*pi*GRAVITATION*RHO0*PROTON_MASS))**0.5
  52. R0_CARBON = ((3.0*Y_e_CARBON*ELECTRON_MASS*SPEED_OF_LIGHT**2.0)/(4.0*pi*GRAVITATION*RHO0_CARBON*PROTON_MASS))**0.5
  53. R0_IRON = ((3.0*Y_e_IRON*ELECTRON_MASS*SPEED_OF_LIGHT**2.0)/(4.0*pi*GRAVITATION*RHO0_IRON*PROTON_MASS))**0.5
  54.          
  55. M0 = 4.0/3.0 * pi * RHO0 * R0**3                            #Constant used for scaling Mass (Total Mass)
  56. M0_CARBON = 4.0/3.0 * pi * RHO0_CARBON * R0_CARBON**3       #Constant used for scaling Mass (Total Mass)
  57. M0_IRON = 4.0/3.0 * pi * RHO0_IRON * R0_IRON**3             #Constant used for scaling Mass (Total Mass)
  58.  
  59. MASS, DENSITY, RADIUS = [], [], []                          #List for holding Mass, Density and Radius values
  60. CENTRAL_DENSITY_LIST = []                                   #list for holding central densities
  61.  
  62. #Lists used for comparing stars/ groups of stars with different parameters
  63. STAR_1_MASS, STAR_1_RADIUS, STAR_1_DENSITY = [], [], []
  64. STAR_2_MASS, STAR_2_RADIUS, STAR_2_DENSITY = [], [], []
  65.  
  66. #####################################
  67. ##############FUNCTIONS##############
  68. #####################################
  69.  
  70. ###Gamma(y)### relativistic free Fermi gas
  71. gamma = lambda y : (y**(2.0/3.0))/(3.0*(1.0+y**(2.0/3.0))**(1.0/2.0))
  72.  
  73.  
  74. '''
  75. x = independent variable   ----> r
  76. y = list of dependent variables. eg [y1.y2]  --> [rho, m]
  77. h = increment
  78. n = order. eg 2 corresponds to two equations'''
  79. def runkut(n, x, y, h):
  80.     #"Advances the solution of diff eqn defined by derivs from x to x+h"
  81.     y0=y[:]
  82.     k1=derivs(n, x, y)
  83.     for i in range(1,n+1): y[i]=y0[i]+0.5*h*k1[i]
  84.     k2=derivs(n, x+0.5*h, y)
  85.     for i in range(1,n+1): y[i]=y0[i]+h*(0.2071067811*k1[i]+0.2928932188*k2[i])
  86.     k3=derivs(n, x+0.5*h, y)
  87.     for i in range(1,n+1): y[i]=y0[i]-h*(0.7071067811*k2[i]-1.7071067811*k3[i])
  88.     k4=derivs(n, x+h, y)
  89.     for i in range(1,n+1):
  90.         a=k1[i]+0.5857864376*k2[i]+3.4142135623*k3[i]+k4[i]
  91.         y[i]=y0[i]+0.16666666667*h*a
  92.     #print x, y
  93.     x+=h
  94.     return (x,y)
  95.  
  96.  
  97. def derivs(n, x, y):
  98.     #"The function DERIVS calculates y from x and y"
  99.     dy=[0 for i in range(0,n+1)]
  100.     #0 density is defined as the radius of the star
  101.     if x==0:
  102.         dy[1]=0
  103.         return dy
  104.                
  105.     if y[1] >= 0:  #Only Calculate if density is > 0 (Ending boundry condition)
  106.         dy[1] = -(y[2]*y[1])/((x**2)*gamma(y[1]))      #d'(rho)/dr'  
  107.         dy[2] = 3*(x**2)*y[1]                             #d'(m)/dr'    
  108.         return dy
  109.    
  110.     else: #if density is less than 0 then do not perform any calculations
  111.         return dy
  112.    
  113.    
  114. #####################################
  115. ##############   MAIN   #############
  116. #####################################
  117.  
  118. #=========================================================#
  119. #Mass vs Radius Iron and Carbon for many central densities#
  120. #=========================================================#
  121.  
  122. #Calculating N_Stars Different Central Densities
  123.  
  124. #Plotting Many central densities Mass - Radius
  125.  
  126. N_Stars = 1000 #Number of stars of different central densities to create
  127. for CENTRAL_DENSITY in np.logspace(-3, 6 ,N_Stars): #Full Radius Plot
  128. #integrating from r=0 to r=R  with initial conditions rho(r) = CENTRAL_DENSITY   ,m(r) = 0
  129. #r;   y=[ignore, rho, mass]
  130.  
  131.     N=2000 #number of interations per calculations for each star, higher number will result in higher resolution
  132.     x=0.0; y=[0.0, CENTRAL_DENSITY, 0.0] # Set initial Boundary Conditions
  133.     # Calculate and print the solution for x= 0 to 1 using N steps
  134.     CENTRAL_DENSITY_INITIAL=CENTRAL_DENSITY
  135.     #for j in range(0,N):
  136.     while y[1]>1e-13:
  137.         (x,y) = runkut(2, x, y, 1.0/N)
  138.        
  139.         MASS.append(y[2])
  140.         RADIUS.append(x)
  141.         DENSITY.append(y[1])
  142.        
  143.         #print "Central Density", CENTRAL_DENSITY,  "r =", x, "rho/rho0 =", y[1], "m/M0 =", y[2]
  144.         if y[1]<1e-13:   #ending boundry condition rho(r)=0
  145.             #print "rho =", y[1]
  146.             #print "m =", y[2]
  147.             STAR_1_MASS.append(MASS[-1]*M0_CARBON/SOLAR_MASS)
  148.             STAR_1_RADIUS.append(RADIUS[-1]*R0_CARBON/SOLAR_RADIUS)
  149.             STAR_1_DENSITY.append(CENTRAL_DENSITY_INITIAL)
  150.            
  151.             STAR_2_MASS.append(MASS[-1]*M0_IRON/SOLAR_MASS)
  152.             STAR_2_RADIUS.append(RADIUS[-1]*R0_IRON/SOLAR_RADIUS)
  153.             STAR_2_DENSITY.append(CENTRAL_DENSITY_INITIAL)
  154.            
  155.             #if 0.0115-0.0012 < STAR_2_RADIUS[-1] < 0.0115+0.0012:
  156.             print  "Central density=", CENTRAL_DENSITY, "Radius=", STAR_2_RADIUS[-1], "mass=", STAR_2_MASS[-1]
  157.            
  158.             break
  159.  
  160. fig, ax1 = plt.subplots(figsize=(3.37,2.5))#10,8))
  161. ax1.set_ylabel('Central Density ('+r'$\rho_{0}$)', fontsize=7)
  162. ax1.set_xlabel('Mass ('+r'$M_{\odot}$)', fontsize=7)
  163. ax1.tick_params(axis='both', which='major', labelsize=7)
  164.  
  165. '''
  166. #Central density plot and axis
  167. ax2 = ax1.twiny()
  168. ax2.invert_xaxis()
  169. ax2.tick_params(axis='both', which='major', labelsize=7)
  170. #ax2.set_xscale('log')
  171. ax2.set_xlabel('Central Density ('+r'$\rho_{0}$)', fontsize=7)
  172. '''
  173.  
  174. '''
  175. #Plotting known stars
  176. for i in range(len(StarNames)):
  177.    #ax1.annotate(StarNames[i], (StarRadius[i],StarMass[i]))
  178.    #ax1.axvline(x=StarRadius[i], color='r', linewidth=0.25, linestyle='-')
  179.    #ax1.text(StarRadius[i], 0.4, 'froggfhfghgfhgfhfhf', fontsize=4, rotation='vertical')
  180.    
  181.    ax1.scatter(StarRadius[i], StarMass[i], marker=Markers[i],s=10, lw = 0, color='k',  label=StarNames[i])
  182.    ax1.errorbar(StarRadius[i], StarMass[i], xerr=StarRadiusErr[i], yerr=StarMassErr[i], capsize=1.0, elinewidth=0.25, color='k')
  183. '''
  184. ax1.plot(STAR_1_RADIUS, STAR_1_DENSITY, 'k', linewidth=0.5, label='Carbon White Dwarf')
  185. ax1.plot(STAR_2_RADIUS, STAR_2_DENSITY,'k--', linewidth=0.5, label='Iron White Dwarf')
  186.  
  187. #ax2.plot(STAR_1_DENSITY, STAR_1_MASS, linewidth=1)
  188. ax1.set_yscale('log')
  189. ax1.set_ylim(ymin = 0.0)
  190. ax1.set_xlim(xmin=0.000)
  191. #plt.axvline(x=max(STAR_1_MASS), color='r', linewidth=0.5, linestyle='-')
  192. #plt.axvline(x=max(STAR_2_MASS), color='r', linewidth=0.5, linestyle='-')
  193.  
  194. #ax1.text(max(STAR_1_MASS)-0.20, 0.20, r'%s $M_{\odot}$' %np.around(max(STAR_1_MASS), 2), fontsize=7)
  195. #ax1.text(max(STAR_2_MASS)-0.25, 0.6, r'%s $M_{\odot}$' %np.around(max(STAR_2_MASS), 2), fontsize=7)
  196.  
  197. ax1.set_yscale('log')
  198.  
  199.  
  200.  
  201. ax1.legend(fontsize=7)
  202. fig.savefig('Plot1.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches = 0.0)
  203. fig.savefig('Plot1.png', format='png', dpi=1000, bbox_inches='tight', pad_inches = 0.0)
  204.  
  205.  
  206. print "Carbon white dwarf maximum mass", STAR_1_MASS[-1]
  207. print "Iron white dwarf maximum mass", STAR_2_MASS[-1]
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. ######################################
  215. #Plotting internal structure of stars#
  216. ######################################
  217.  
  218. #========================================================#
  219. #Mass vs Radius Iron and Carbon for given central density#
  220. #========================================================#
  221.  
  222. #resetting variables
  223. CENTRAL_DENSITY = 1.0
  224.  
  225. fig2, ax2 = plt.subplots(figsize=(10,8))#figsize=(3.37,2.5))#
  226. ax2.set_ylabel('Mass ('+r'$M_{\odot}$)', fontsize=7)
  227. #ax2.set_ylabel('Density ('+r'$\rho_{0}$)', fontsize=7)
  228. ax2.set_xlabel('Radius ('+r'$R_{\odot}$)', fontsize=7)
  229. #plt.xticks(np.arange(0.0, 0.0016, 0.0005)) #Manual adjustment of xticks
  230.  
  231. ax2.tick_params(axis='both', which='major', labelsize=7) #Set x and y labels to fontsize 7
  232.  
  233.  
  234.  
  235. #while CENTRAL_DENSITY<1:
  236.  
  237. MASS, RADIUS, DENSITY = [],[],[]
  238. STAR_1_MASS, STAR_1_RADIUS, STAR_1_DENSITY = [], [], []
  239. STAR_2_MASS, STAR_2_RADIUS, STAR_2_DENSITY = [], [], []
  240. N=2000 #number of interations per calculations for each star, higher number will result in higher resolution
  241. x=0.0; y=[0.0, CENTRAL_DENSITY, 0.0] # Set initial Boundary Conditions
  242.  
  243.  
  244. #for j in range(0,N):
  245. while y[1]>1e-13:
  246.     (x,y) = runkut(2, x, y, 1.0/N)# Calculate the solution for x= 0 to 1 using N steps
  247.     #print x, y[1], y[2]
  248.     MASS.append(y[2]*M0/SOLAR_MASS)
  249.     RADIUS.append(x*R0/SOLAR_RADIUS)
  250.     DENSITY.append(y[1])
  251.    
  252.     STAR_1_MASS.append(y[2]*M0_CARBON/SOLAR_MASS) #Carbon
  253.     STAR_1_RADIUS.append(x*R0_CARBON/SOLAR_RADIUS)#Carbon
  254.     STAR_1_DENSITY.append(y[1])
  255.    
  256.     STAR_2_MASS.append(y[2]*M0_IRON/SOLAR_MASS) #iron
  257.     STAR_2_RADIUS.append(x*R0_IRON/SOLAR_RADIUS)#iron
  258.     STAR_2_DENSITY.append(y[1])
  259.                        
  260.    
  261.    
  262.  
  263. ax2.plot(STAR_1_RADIUS,STAR_1_MASS,'k', linewidth=1.0, label='Carbon White Dwarf')
  264. ax2.plot(STAR_2_RADIUS,STAR_2_MASS,'k--', linewidth=1.0, label='Iron White Dwarf')
  265. print '------INDIVIDUAL STAR OF SINGULAR CENTRAL DENSITY----------'
  266. print 'Central Density =',CENTRAL_DENSITY
  267. print 'A carbon star has radius', STAR_1_RADIUS[-1], 'mass', STAR_1_MASS[-1]
  268. print 'An Iron star has radius', STAR_2_RADIUS[-1], 'mass', STAR_2_MASS[-1]
  269.  
  270. '''
  271. #Lines for Maximum Masses
  272. plt.axhline(y=STAR_1_MASS[-1], color='r', linewidth=0.5, linestyle='-')
  273. plt.axhline(y=STAR_2_MASS[-1], color='r', linewidth=0.5, linestyle='-')
  274. ax2.text(0.00005, STAR_1_MASS[-1], r'%s $M_{\odot}$' %np.around(STAR_1_MASS[-1], 2), fontsize=7)
  275. ax2.text(0.00005, STAR_2_MASS[-1], r'%s $M_{\odot}$' %np.around(STAR_2_MASS[-1], 2), fontsize=7)
  276. '''
  277.  
  278. #Other annotations
  279. ax2.text(0.0004, 0.35, r'Central Density: $\rho_{0}$', fontsize=7)
  280. ax2.legend(fontsize=7)
  281. plt.ylim(ymin=0)
  282. plt.xlim(xmin=0)
  283. #fig2.savefig('indv_Mass_Radius_rho0.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches = 0.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement