Home > OS >  I can't get output from my code in Python
I can't get output from my code in Python

Time:10-14

I'm trying to write a very simple code using the equation of state for Neutron stars. In the first stage, I define my constants and assign them to the list as the 1st element. Then I do my first-step equations and append them to the list as the second. I create a while loop for the next operations and append my lists within this loop. And at each step I print these appended constants to a out file.

"""
Created on Wed Oct 12 20:44:10 2022
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

f = open("radial.out","w")
### Constants
h_bar = 1.055e-27      # reduced planck constant (kg m2 s-1)
c = 2.99e 10           # m/s
G = 6.67430e-8        # Gravitational constant m^3/(kg*s)b
pi = 3.1415926535897
gamma =5/3
m_sun = 2.998e33
K = 1e10
rho_c = 2e15           # Density centeral kg/me3
P_c = K*rho_c**gamma
km = 1e5
dr = 10e3
M0 = (4*pi*(dr**3)*rho_c)/3

####Initial values for r = 0
N = 1600
m = np.zeros(N)
P = np.zeros(N)
rho = np.zeros(N)
r = np.zeros(N)
GR = np.zeros(N)

r[0] = 0
m[0] = 0
P[0] = P_c
rho[0] = rho_c
GR[0] = 0

data=r,rho,P,m,GR
f.write(str(data) '\n')
np.savetxt("radial.out", m, delimiter=" ")

####### Second Appends
r[1] = dr
rho[1] = rho_c
P[1] = P_c
m[1] = 4*pi*r[1]**3*rho[1]/3 
GR[1] = 0

data=r,rho,P,m,GR
f.write(str(data) '\n')
np.savetxt("radial.out", m,delimiter=" ")

for i in range(1,len(P)):
    while P[i] > 0:
        m[i 1] = m[i]   4*pi*r[i]**2*rho[i]*dr
        rho[i 1] = (P[i]/K)**(3/5)
        GR[i 1] = (1   (4*pi*r[i]**3*P[i])/(m[i]*c**2))*(1   P[i]/(rho[i]*c**2))/(1 - (2*G*m[i])/(r[i]*c**2))
        P[i 1] = P[i] - (G * m[i] * rho[i] * GR[i])/ r[i]**2
        r[i 1] = r[i]   dr
        data=r,rho,P,m,GR
        f.write(str(data) '\n')
        np.savetxt("radial.out", m,delimiter=" ")

f.close()

As a result it gives me an empty file, how can I proceed next.

CodePudding user response:

The original program does two differnt write operators (file handle f, and np.savetxt) on the same file, and they both use "w" mode, which would overwrite the existing file. Changes both mode to "a" would solve the issue.

Recommendation: Using only one method to write to a file is recommended(i.e. only use file handler f with a mode. or visevers, only using np.savetxt for all writes operation..)

Be aware, to enable np.savetxt to append content, we need to provide its first argument fname to be a file handler with a mode. i.e.

f=open('file.txt','a') 

Then np.savetxt would append write content. i.e.

np.savetxt(f, m, delimiter=" ")

CodePudding user response:

I edited my code like this based on the answers I received and it solved my problem.

It appeared because I overprinted it using two different methods.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

f = open("radial.txt","a")
### Constants
h_bar = 1.055e-27      # reduced planck constant (kg m2 s-1)
c = 2.99e 10           # m/s
G = 6.67430e-8        # Gravitational constant m^3/(kg*s)b
pi = 3.1415926535897
gamma =5/3
m_sun = 2.998e33
K = 1e10
rho_c = 2e15           # Density centeral kg/me3
P_c = K*rho_c**gamma
km = 1e5
dr = 10e3
M0 = (4*pi*(dr**3)*rho_c)/3

####Initial values for r = 0
N = 1502
m = np.zeros(N)
P = np.zeros(N)
rho = np.zeros(N)
r = np.zeros(N)
GR = np.zeros(N)

r[0] = 0
m[0] = 0
P[0] = P_c
rho[0] = rho_c
GR[0] = 0

data=r,rho,P,m,GR
f.write(str(data) '\n')
np.savetxt(f, (m,P,rho,r), delimiter=" ")

####### Second Appends
r[1] = dr
rho[1] = rho_c
P[1] = P_c
m[1] = 4*pi*r[1]**3*rho[1]/3 
GR[1] = 0

data=r,rho,P,m,GR
f.write(str(data) '\n')
np.savetxt(f, (m,P,rho,r), delimiter=" ")

for i in range(1,len(P)):
    while P[i] > 0:
        m[i 1] = m[i]   4*pi*r[i]**2*rho[i]*dr
        rho[i 1] = (P[i]/K)**(3/5)
        GR[i 1] = (1   (4*pi*r[i]**3*P[i])/(m[i]*c**2))*(1   P[i]/(rho[i]*c**2))/(1 - (2*G*m[i])/(r[i]*c**2))
        P[i 1] = P[i] - (G * m[i] * rho[i] * GR[i])/ r[i]**2
        r[i 1] = r[i]   dr
        data=r,rho,P,m,GR
        f.write(str(data) '\n')
        np.savetxt(f, (m,P,rho,r), delimiter=" ")

f.close()
  • Related