I am using a while loop to integrate various quantities from the surface of a star inwards using appropriate boundary conditions and stellar structure equations.
I am using dictionaries to represent physical variables such as pressure and density, where the plan is for the radii to be keys, and the value to be the pressure or density.
I have a key:value pair for the surface, and then I step inwards iteratively using a while loop updating the dictionaries as below:
import constants
import math
import matplotlib.pyplot as plt
mass=3*constants.solar_mass
radius=1.5*constants.solar_radius
#Variables to guess
core_temperature=1.4109*10**7
core_pressure= 2.6851*10**14
luminosity=pow(3,3.5)*constants.solar_luminosity
#Functions we are searching for
temperature={}
#guess
temperature[0]=core_temperature
#From the steffan boltzmann law
temperature[radius]=pow(luminosity/(4*math.pi*pow(radius,2)*constants.stefan_boltzmann_constant),0.25)
pressure={}
#guess
pressure[0]=core_pressure
#Pressure surface boundary condition
pressure[radius]=(2*constants.gravitation_constant*mass)/(3*constants.opacity*pow(radius,2))
mass_enclosed={}
#boundary conditions
mass_enclosed[0]=0
mass_enclosed[radius]=mass
density={}
#density surface boundary condition
density[radius]=(constants.mean_molecular_weight*pressure[radius])/(constants.gas_constant*temperature[radius])
delta_radius=int(radius/100)
#Polytropic constant
K=(pressure[radius]*constants.mean_molecular_weight)/(constants.gas_constant*pow(density[radius],constants.adiabatic_constant))
def integrate_from_surface():
i=0
while radius-i*delta_radius>(0.5*radius):
#temporary radius just for each loop through
r=radius-i*delta_radius
#updating pressure
pressure[r-delta_radius]=pressure[r]+(density[r]*constants.gravitation_constant*mass_enclosed[r]*delta_radius)/pow(r,2)
#updating density
density[r-delta_radius]=pow((pressure[r-delta_radius]*constants.mean_molecular_weight)/(constants.gas_constant*K),1.0/constants.adiabatic_constant)
#updating mass enclosed
mass_enclosed[r-delta_radius]=mass_enclosed[r]-4*math.pi*pow(r,2)*delta_radius*density[r]
i=i+1
integrate_from_surface()
While Loop: Radius and dictionaries are defined above
I am getting a KeyError, as shown below:
Traceback (most recent call last):
File "main.py", line 63, in <module>
integrate_from_surface()
File "main.py", line 51, in integrate_from_surface
pressure[r-delta_radius]=pressure[r]+(density[r]*constants.gravitation_constant*mass_enclosed[r]*delta_radius)/pow(r,2)
KeyError: 1043966868.09
If I print out the variable r in the while group, the process works perfectly until r is 1043966868.09. I do not understand, surely on the previous iteration I made this a key, so there should be no KeyError.
Constants file below:
solar_mass=1.9891*10**30
solar_radius=6.9598*10**8
solar_luminosity=3.8515*10**26
gas_constant=8.3145*10**3
gravitation_constant=6.6726*10**-11
radiation_constant=7.5646*10**-16
speed_of_light=2.9979*10**8
stefan_boltzmann_constant= radiation_constant*speed_of_light * 0.25
opacity=0.034
adiabatic_constant = 5.0/3
mean_molecular_weight = 8.0/13
Thanks in advance for any help.