I have a system of coupled equations: the hydrostatic equilibrium equation, the mass continuity equation, and an equation of state of the ideal gas. These are, in mathematical grammer,
\frac{dP}{dr}=- \rho*g
,
where \rho
is the density and g
is the gravitational acceleration.
\frac{dM}{dr}=4*pi* r^2*\rho
and
p=\rho* k_B* T/(\mu *m_p)
,
where k_B
is boltzmann's constant, \mu
is the mean molecular weight, and m_p
is the proton mass.
I want to solve these coupled equations using the Runge-Kutta numerical technique, and I herein show the python code I have devised to solve this problem:
from scipy.constants import m_p,G,k,pi
from pylab import *
#mu may be changed for different molecular composition:
mu=2
def g(r_atm, p_atm):
T=165
return 4*pi*r_atm**2*mu*m_p*p_atm/(k*T)
def f(r_atm,p_atm, m_atm):
T=165
return -mu*m_p*p_atm*G*m_atm/(k*T*r_atm**2)
def rk4_1(g,f, r0, p0,m0, r1, n):
r_atm = [0]*(n + 1)
p_atm = [0]*(n + 1)
m_atm=[0]*(n + 1)
h = (r1 - r0)/n
# h=-20
r_atm[0]=r0
p_atm[0]=p0
m_atm[0]=m0
for i in range(0,10000000):
if p_atm[i]<100000:
k0 = h*g(r_atm[i], p_atm[i])
l0 = h*f(r_atm[i], p_atm[i], m_atm[i])
k1 = h*g(r_atm[i] + 0.5*h, p_atm[i] + 0.5*k0)
l1 = h*f(r_atm[i] + 0.5*h, p_atm[i] + 0.5*l0, m_atm[i]+0.5*k0)
k2 = h*g(r_atm[i] + 0.5*h, p_atm[i] + 0.5*k1)
l2 = h*f(r_atm[i] + 0.5*h, p_atm[i] + 0.5*l1, m_atm[i]+0.5*k1)
k3 = h*g(r_atm[i] + h, p_atm[i] + k2)
l3 = h*f(r_atm[i] + h, p_atm[i] + l2, m_atm[i]+k2)
r_atm[i+1] = r0 + (i+1)*h
p_atm[i+1] = p_atm[i] + (l0 + 2*l1 + 2*l2 + l3)/6
m_atm[i+1] = m_atm[i] + (k0 + 2*k1 + 2*k2 + k3)/6
else:
break
return h, r_atm, p_atm, m_atm
h, r_atm, p_atm, m_atm = rk4_1(g,f, 6.991e7, 1e-6*1e5, 1.898e27, 2.0e7,10000000) #bar to pascals (*1e5)
for intial conditions for the pressure, p_atm
, radius, r_atm
, and mass, m_atm
, I use the values I have shown in h, r_atm, p_atm, m_atm = rk4_1(g,f, 6.991e7, 1e-6*1e5, 1.898e27, 2.0e7,10000000)
. Notice that I am approaching this boundary-value problem from the upper atmosphere (where the initial conditions are given) and progressing downward in the atmosphere (notice that h is negative). My intention is to evaluate this numerical integration from 10^-1
Pascals to 100000
Pascals. The result I get from running this code is that the pressure simply blows up to ~1e+123
in three steps, so there is obviously something terribly wrong streaming about, but it would help to have another eye or perspective, for this is the first time I am performing Runga-Kutta methodology.