Debugging the Swarzchild Metric - ValueError

In summary, the metric function doesn't work and the code is giving errors when trying to fill the stars.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
I'm messing around with the swarzchild metric, and I keep getting errors. First, it was a memory, which I could have guessed, 10000x10000 array, so I lowered it to 1000x1000 and it moves past that point, now.
However, this is where I'm getting my error:
Python:
Gravity = zeros([1000,1000])
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)

print("Done filling\n")
On the first line of the fill, the one with all the 1's, it's giving me this error:
"ValueError: setting an array element with a sequence."
From googling around, it seems that
"the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array." -stack exchange.
I was wondering what I am missing that's making this error out?
 
Technology news on Phys.org
  • #2
BiGyElLoWhAt said:
I'm messing around with the swarzchild metric, and I keep getting errors. First, it was a memory, which I could have guessed, 10000x10000 array, so I lowered it to 1000x1000 and it moves past that point, now.
However, this is where I'm getting my error:
Python:
Gravity = zeros([1000,1000])
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)

print("Done filling\n")
On the first line of the fill, the one with all the 1's, it's giving me this error:
"ValueError: setting an array element with a sequence."
From googling around, it seems that
"the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array." -stack exchange.
I was wondering what I am missing that's making this error out?
I'm no Python expert, but taking it to be similar to C++: where are I and j incremented?
 
  • Like
Likes BiGyElLoWhAt
  • #3
This is not a Python error. It is a NumPy error. Python has no problem with a composite (list, array, map, ...) in which one element is a number, another is a string, yet another is a list, and yet another is a map. This can be a "Good Thing", particularly with maps. Because it can be (and oftentimes is) a good thing to do, python allows it.

NumPy does have problems with this. By default, NumPy tries to be dimensionally correct from a mathematical perspective. Your code starts with Gravity = zeros([1000,1000]). Each element of that 1000 by 1000 array is a number. Assigning a non-numerical value such as a string, a list, a vector, a map, a set, or some other random object doesn't make sense. Python: No complaint. NumPy: Big complaint. This, too, is a "Good Thing".

You haven't shown us what your metric function is doing. Presumably it is not returning something that is trivially reducible to a number. It is instead returning a sequence.
 
  • Like
Likes BiGyElLoWhAt
  • #4
Python:
def metric(r,rs, dt, theta,dtheta, v):
    tau2 = (1-rs/r)*dt**2 - ((1 - rs/r)**-1)*(v*dt)**2 - (r**2)*(dtheta**2)
    return tau2,
Here's the metric function.
I feel a little dumb for leaving out the increments, but adding them in didn't fix the problem.
 
  • #5
well, here's the whole code, because why not...
Code:
# -*- coding: utf-8 -*-
"""
Created on Fri May 06 14:42:16 2016

@author: Tyler
"""

from __future__ import division
from math import sin, cos, sqrt, pi
from numpy import zeros

step = 1
G = 6.674*10**(-11)
c = 3*10**8
Mass1 = 10**20
Mass2 = 10**30
RS1 = 10**3
RS2 = -10**3
ThetaS1= 0
ThetaS2 = pi
Rs1 = 2*G*Mass1/(c**2)
Rs2 = 2*G*Mass2/(c**2)
distance = 100
CoM1 = distance/(1+Mass1/Mass2) #Distance to center of mass of system
CoM2 = distance/(1+Mass2/Mass1) #Distance to center of mass of system
speed1 = sqrt(G*Mass2/CoM1)
speed2 = sqrt(G*Mass1/CoM2)
omega1 = speed1/CoM1
omega2 = speed2/CoM2def Move_Stars(r, theta):
    x= r*cos(theta + step)
    y = r*sin(theta + step)
    return x,y
def metric(r,rs, dt, theta,dtheta, v):
    tau2 = (1-rs/r)*dt**2 - ((1 - rs/r)**-1)*(v*dt)**2 - (r**2)*(dtheta**2)
    return tau2,

#1 element represents 10000x10000 m
#zero = 5000,5000
i,j = 0,0

Array = [1000,1000]
Gravity = zeros(Array)
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)
        j+=1
    i+=1
print("Done filling\n")
I've also adjusted it a couple times, trying to define Gravity in different ways, but none of them have solved the problem.
 
  • #6
What is the significance of the comma after "return tau"?
 
  • Like
Likes BiGyElLoWhAt
  • #7
Well, that's cool. I though I looked through it all pretty carefully. I fixed the comma, got a math domain error, and it turns out I also had a misplaced parentheses. Thanks everyone for pointing out my stupid mistakes. I'll try to look more carefully next time.
 
  • #8
It works fine now, is what I mean.
 

Related to Debugging the Swarzchild Metric - ValueError

1. What is the Swarzchild metric?

The Swarzchild metric is a solution to Einstein's field equations in general relativity that describes the geometry of spacetime outside a non-rotating, spherically symmetric mass.

2. What does "ValueError" mean in the context of debugging the Swarzchild metric?

"ValueError" is a type of error that occurs when a function or operation receives an argument or input that is not of the correct type or format. In the context of debugging the Swarzchild metric, this could mean that there is an issue with the values being used in the mathematical calculations or equations.

3. How can I fix a ValueError when trying to debug the Swarzchild metric?

To fix a ValueError in the context of debugging the Swarzchild metric, you will need to carefully check the values being used in the calculations and equations. Make sure they are of the correct type and format and that they are being used correctly in the code. You may also need to consult with other experts or references to identify and resolve the issue.

4. Are there any common mistakes that can lead to a ValueError when debugging the Swarzchild metric?

Yes, there are a few common mistakes that can lead to a ValueError when debugging the Swarzchild metric. These include using incorrect values or units, incorrect syntax in the code, and errors in the mathematical calculations or equations. It is important to carefully check all of these aspects when trying to resolve a ValueError.

5. Why is it important to debug the Swarzchild metric?

Debugging the Swarzchild metric is important because it allows us to ensure the accuracy and reliability of this mathematical model. The Swarzchild metric is used in many areas of physics and astronomy, so any errors or issues with it could have significant implications for our understanding of the universe. By debugging and improving the metric, we can continue to make progress in our understanding of gravity and spacetime.

Similar threads

  • Programming and Computer Science
Replies
2
Views
935
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
8
Views
12K
  • STEM Academic Advising
Replies
13
Views
2K
Back
Top