Debugging the Swarzchild Metric - ValueError

Click For Summary

Discussion Overview

The discussion revolves around debugging a Python implementation of the Schwarzschild metric, focusing on errors encountered while filling a NumPy array with calculated gravitational values. The scope includes programming issues, mathematical modeling, and computational challenges.

Discussion Character

  • Technical explanation
  • Debugging
  • Mathematical reasoning

Main Points Raised

  • One participant describes encountering a "ValueError" when attempting to assign values to a NumPy array, suggesting that the issue may stem from the output of the metric function not being a single numerical value.
  • Another participant clarifies that the error is specific to NumPy's handling of data types and dimensionality, contrasting it with Python's more flexible data handling.
  • A participant shares the implementation of the metric function, which returns a tuple, potentially causing issues with array assignments.
  • There is a question raised about the significance of a comma in the return statement of the metric function, indicating uncertainty about its impact on the function's output.
  • One participant acknowledges fixing a math domain error and a misplaced parenthesis after receiving feedback, indicating iterative refinement of their code.
  • A later reply confirms that the code works correctly after adjustments were made, but does not specify the nature of the adjustments.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the errors being related to the handling of data types in NumPy, but there is no consensus on the exact cause of the initial error until further clarifications are made.

Contextual Notes

The discussion highlights limitations related to the handling of output types from functions in Python and NumPy, as well as the importance of careful debugging in programming. Specific assumptions about the behavior of the metric function and the structure of the data being processed remain unresolved.

BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
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
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   Reactions: BiGyElLoWhAt
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   Reactions: BiGyElLoWhAt
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.
 
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.
 
What is the significance of the comma after "return tau"?
 
  • Like
Likes   Reactions: BiGyElLoWhAt
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.
 
It works fine now, is what I mean.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 2 ·
Replies
2
Views
11K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
13K