0

I am relatively new to Python and I was using it to solve a system of equations in a Cosmology project. Here is the code:

#Import Math Module for Square Root Function, Numpy for Computations and Matplot for Plotting

import math
import numpy as np
import matplotlib.pyplot as plt

#Planck Mass

Mpl=2.43*10**18*10**9*1.6*10**-19 #Mpl value is 2.43*10^18 GeV

#Compute Initial Values of the Densities

RhoM_today= 2.746*(10**(-27)) #Today's Value of Matter Density
RhoRad_today= 4.546*(10**(-31)) #Today's Value of Radiation Density
RhoL_today=0.0 #Value of Cosmological Constant Density
Rho=RhoM_today+RhoRad_today+RhoL_today #Initial Value of the Overall Density

#Set Initial Values

#Initial Values of the Fields

Sigma = 1.0 # Initial Value of Sigma (a=10)
Phi=1.0

#Values of Parameters

Omega=1.0 #Dummy Value of Omega
Alpha_Three=1.0 #Dummy Value of Alpha_3
Alpha_Four=1.0 #Dummy Value of Alpha_4
Alpha_Sigma=1.0 #Dummy Value of Alpha_Sigma
C1=1.0 #Dummy Value of C1

mg=1.0 #Dummy Value of the Graviton Mass

Lambda=0.0   #Value of the Cosmological Constant

H=2.27*10**-18 #Initial Value of H

# Initial Values for computing the number of Steps for RK4

x=0 #Corresponding to a = 1 (today)
xn=-11.5 #Corresponding to a ~ 10^-5
h = 0.115 #Step Size
n = int((x-xn)/h) #Number of Steps/Points

#Note: n+1 points but n Steps?

print(n)

#Creating Arrays for Storing Data

xp=np.linspace(x,xn,n+1)   #Array for Storing x
Sigmap=np.empty(n+1,float) #Array for Storing Sigma
up=np.empty(n+1,float)     #Array for Storing u
Hp=np.empty(n+1,float)     #Array for Storing H
#Hxp=np.empty(n+1,float)     #Array for Storing Hx
#Sigmap[0]=Sigma            #Save Initial Value of Sigma
#up[0]=u                    #Save Initial Value of u
#Hp[0]=H                    #Save Initial Value of H
#Hp[0]=H/H0                    #Save Initial Value of H
#Hxp[0]=Hx                  #Save Initial Value of Hx

#CHp=np.empty(n+1,float) #Array for Conformal Hubble Parameter
#CHp[0]=H*math.exp(x)


#for i in range (0,n+1):
#    print (Hp[i])

#Print Parameters and Initial Values
print('Planck Mass is',Mpl)
print('Initial Value of H is ',H )
print('Initial Value of Sigma is ',Sigma)
print('Initial Value of Energy Density Rho is ',Rho)
print('a goes from ',math.exp(x),'to ',math.exp(xn))
print('x (lna) goes from ',x,'to ',xn)
print('Total Number of Steps in RK1 is ',n)


#The Header of the output table

print('x \t\t\t\t\t\t \t\tSigma\t\t\t\t\t\t u \t\t\t\t\t \t\t\t\t H')
#print(x,'\t\t\t\t\t\t\t',Sigma,'\t\t\t\t\t\t',u,'\t\t\t\t\t\t',H)
#print('%f \t\t\t\t\t \t %f \t\t\t\t \t %f \t\t\t\t\t \t %f'% (x,Sigma,u,H)) # The Initial Values of x, Sigma & u

#Start of RK1

#Start of The RK1 Loop

for i in range(0, n):

    # Generate Values of Rho (from RhoM, RhoRad & H, re-written in terms of x)

    RhoM = RhoM_today / (math.exp(x) ** 3)
    RhoRad = RhoRad_today / (math.exp(x) ** 4)
    RhoL = 0.0

    Rho = RhoM + RhoRad + RhoL  # New Value of the Overall Density

    # Generate Values of the Pressures

    Pressure_Matter = 0
    Pressure_Radiation = RhoRad / 3
    Pressure = Pressure_Matter + Pressure_Radiation

    #Save the Values of Sigma and H

    Sigmap[i] = Sigma  # Save the Value of Sigma
    Hp[i] = H          #Save the Value of H

    # Compute  Values for X, J, Lambda_X & Their Derivatives

    X = math.exp(Sigma) / math.exp(x)
    print(X)
        J = 3 + 3 * Alpha_Three * (1 - X) + Alpha_Four * (1 - X) ** 2
    print(J)

    Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]

    # Compute Value of u

    u = (1 / H) * (math.sqrt(2 / Omega)) * math.sqrt(3 * H * 2 - Lambda - Lambda_X - Rho / Mpl ** 2)

    up[i]=u

    print('%.3f \t\t\t\t\t \t %.6f \t\t\t\t \t%.6f \t\t\t\t\t \t %9.3e' % (x, Sigma, u, H))  # Newer Computed values of x,Sigma,u & H

    # Compute Value of Lambda_X_Dot & Lambda_X_Dash

    Lambda_X_Dot = 3 * (mg ** 2) * math.exp(Sigma) * H * [u - 1] * [J + X * {Alpha_Three * (X - 1) - 2}] / math.exp(x)
    Lambda_X_Dash = Lambda_X_Dot / H

    # Compute Value of q

    q = (Alpha_Sigma ** 0.5) * [u / (X * mg)] / math.sqrt([(1 / H ** 2) - {C1 / (math.exp(x) ** 4 * X * (1 - X) * J * H)}])

    # Compute Value of H_Dash

    H_Dash = (q - 1) * Lambda_X_Dash / (6 * H * (u - 1)) - Omega * H * u ** 2 / 2 - (Rho + Pressure) / (2 * H)

    #Compute the next Values of H and Sigma

    H=H+H_Dash*h
    Sigma=Sigma+u*h

    #Increment x (Move on to next h)
    x-=h

I have included the entire code for completeness. The errors I get are towards the end of the code in the three equations- the one in Lambda_X, another in Lambda_X_Dot and the last one in q).

The errors I am getting are of the form: 'Expected Float, got set[float] instead ' & 'Expected Float got list[float] instead'etc.

I am unsure where they come from since I had written a similar program which did not have such errors. I donno if it helpsbut I am using PyCharm to write the code. Any input are greatly appreciated! Thanks in advance.

1
  • 1
    chop this code down into the essence of the problem to make it easier for us to help you. Chances are in doing so you might even answer your own question. From looking at the error message you are getting you have a float wrapped in a list/set, and you're trying to use it in the place of a normal float. For example trying to do [4.] / 2 instead of 4. / 2. Commented Jun 10, 2018 at 23:36

1 Answer 1

2

In your code see this line specifically,

Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]

Usually, in mathematics, we use square braces and curly brace along with normal braces, but in python, they mean the different thing. {} this makes a set and [] this makes a list. So you can't use them as you have used here. You might want to replace them with normal braces.

For example, above mentioned line will become

Lambda_X = (mg ** 2) * (X - 1) * (J + (X - 1) * ((Alpha_Three) * (X - 1) - 3))

You will have to do the same for other statements in the code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That made sense and fixed the problem I was facing.
@VishalVerma Feel free to accept the answer if its satisfactory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.