What am I doing wrong with my multidimensional state array?

  • Thread starter Thread starter Bishop556
  • Start date Start date
AI Thread Summary
The discussion revolves around using a Runge-Kutta numerical method to solve the equations of state for ordinary and neutron stars. The code provided initializes parameters and defines functions for the density and equations of state, but the user encounters issues with the output, which results in a straight line diverging to infinity. The user expresses confusion over the need for three equations to solve for mass, pressure, and density, given that only two equations are currently implemented. A point of contention is the potential misuse of the state array, which is defined as two-dimensional but may be treated as one-dimensional in later calculations. This discrepancy could contribute to the numerical instability and incorrect results observed in the simulation.
Bishop556
Messages
37
Reaction score
4
Hello, I am trying to now use my Runge Kutta code to numerically solve the equations of state for an ordinary star and then transcribe that to a neutron star. My code is below:
Code:
import numpy as np
import matplotlib.pyplot as plt
from math import pi

#Constants

G = 6.674*1e-11
N = 5000
Gamma = 1.5
K = 5000

dur = 5

dr = dur/float(N-1)

state = np.zeros([N,2])

#initial conditions

state[0,0] = 0
state[0,1] = 5

def rk4(y,r, dr, den, deriv):
    k1 = dr * deriv(y, den, r)
    k2 = dr * deriv(y + 0.5*k1,den, r + 0.5*dr)
    k3 = dr * deriv(y + 0.5*k2,den, r + 0.5*dr)
    k4 = dr * deriv(y + k3, den, r + dr)
    y_next = y + (k1 + 2*(k2+k3)+k4)/6
    return y_next

def density(state):
    return ((1/K)*state[1])**(-Gamma)   # row = (P/K)^(-Gamma)

def EoS(state,den, r):
    y0 = 4*pi*(r**2)*den                         #dM/dr = 4*pi*(r^2)*row
    y1 = (-G*state[1]*den)/(r**2)           #dP/dr  =  -G*M*row / r^2
    return np.array([y0,y1])
  
r = 1e-23 #starting value to avoid singularity
for i in range(N-1):
    den = density(state[i])
    state[i+1]= rk4(state[i],r,dr,den, EoS)
    r = r + drplt. plot(state[:,1], state[:,0])
plt. show()

What I am trying to do is solve the two equations of state while using the polytropic relation between density and pressure.Now, I am slightly confused on how I can accomplish this as I have three unknowns: mass, pressure, and density which requires three separate equations are needed to develop a unique solution. I, however, am only receiving a straight line that diverges to infinity on the second iteration. Does anyone have any ideas why this is occurring?
 
Last edited:
Technology news on Phys.org
Also, note that state[0] is mass, state[1] is pressure, and den is density.
 
Bishop556 said:
Also, note that state[0] is mass, state[1] is pressure, and den is density.
I'm not sure about this, but it seems to me that you defined your state array two be two-dimensional (your code: state = np.zeros([N,2]) ), but you are using it later as if it were a one-dimensional array, as in what you wrote above.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
2K
Replies
4
Views
8K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
13
Views
5K
Replies
11
Views
2K
Replies
4
Views
2K
Back
Top