A simple code problem

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

Hi everybody

I have a simple code as follow:

Code: Alles auswählen

import pylab
from scipy.integrate import odeint
from scipy.constants import pi, G, c, hbar, m_n

Msun=1.98892e30
Gamma0 = 5.0/3.0
K0 = (3.0*pi**2)**(2.0/3.0)*hbar**2/(5.0*m_n**(8.0/3.0))
Gamma1 = 3
rho1 = 5e17
P1 = K0*rho1**Gamma0
K1 = P1/rho1**Gamma1

def eos(rho):
    if rho<rho1: return K0*rho**Gamma0
    else: return K1*rho**Gamma1 

def inveos(P):
    if P<P1: return (P/K0)**(1.0/Gamma0)
    else: return (P/K1)**(1.0/Gamma1)

def tov(y, r):
    P, m =y[0], y[1]
    rho = inveos(P)
    dPdr = -G*(rho+P/c**2)*(m+4.0*pi*r**3*P/c**2)
    dPdr = dPdr/(r*(r-2.0*G*m/c**2))
    dmdr = 4.0*pi*r**2*rho
    return pylab.array([dPdr,dmdr])

def tovsolve(rhoc):
    r = pylab.arange(10.0, 20000.0, dr)
    m = pylab.zeros_like(r)
    P = pylab.zeros_like(r)
    m[0] = 4.0*pi*r[0]**3*rhoc
    P[0] = eos(rhoc)
    y = pylab.array([P[0], m[0]])
    i =0
    while P[i] > 0.0 and i < len(r)-1:
        dr = r[i+1]-r[i]
        y = odeint.rk4(tov, y, r[i], dr)
        P[i+1] = y[0]
        m[i+1] = y[1]
        i = i + 1
        return m[i-1]/Msun, r[i-1]/1000.0


rhoc = pylab.logspace(17.5,20)
M = pylab.zeros_like(rhoc)
R = pylab.zeros_like(rhoc)

for i in range(len(rhoc)):
     print rhoc[i]
     M[i], R[i] = tovsolve(rhoc[i])

pylab.plot(R,M)
pylab.xlab('Mass (km)')
pylab.ylab('Mass (solar)')
pylab.grid()
pylab.show()
pl.savefig("M-R Diagram.png")
pl.savefig("M-R Diagram.eps")
but, when I run it I get the following error:
File "TOV4.py", line 45, in <module>
M, R = tovsolve(rhoc)
File "TOV4.py", line 26, in tovsolve
r = pylab.arange(10.0, 20000.0, dr)
UnboundLocalError: local variable 'dr' referenced before assignment


Can anybody help me to fix it pls :K . The problem seems to be with
tovsolve funcion.

Thanks
Zuletzt geändert von Homer.Simpson am Donnerstag 21. März 2013, 14:52, insgesamt 3-mal geändert.
Benutzeravatar
/me
User
Beiträge: 3555
Registriert: Donnerstag 25. Juni 2009, 14:40
Wohnort: Bonn

Homer.Simpson hat geschrieben:

Code: Alles auswählen

UnboundLocalError: local variable 'dr' referenced before assignment
For gods sake, please use 4 spaces for each indention level.

The error is quite clear.

Code: Alles auswählen

 r = pylab.arange(10.0, 20000.0, dr)
You use dr, but you haven't defined it before trying to use it.
BlackJack

@Homer.Simpson: I would not call this code simple. And it is definately not easy to read as it is formatted now. If you want anybody to read it, you should put in blank lines at the proper places and indent it with four spaces per level: Style Guide for Python Code.
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

Sorry guys for inappropriate style :( . In fact I'm new in python or in other words I do not write in python. I need to run this code to understand the physics behind it.
I will try to fix the point which you have mentioned.

Bests
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

/me hat geschrieben: The error is quite clear.

Code: Alles auswählen

 r = pylab.arange(10.0, 20000.0, dr)
You use dr, but you haven't defined it before trying to use it.
Hi \me

It is not true, if you have a look in to line 34 I have defined it before using it.
BlackJack

@Homer.Simpson: No you didn't. The only line where you assigned something to that name is line 38 and that ist 8 lines *after* you tried to use it the first time.
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

BlackJack hat geschrieben:@Homer.Simpson: No you didn't. The only line where you assigned something to that name is line 38 and that ist 8 lines *after* you tried to use it the first time.
You are right. But I don't use it till I define it in line 38 (or better to say 39). I define "r" in line 31, but the code doesn't need its dr argument till line 40, i.e., after its definition.

In fact did not write this code, the original one is here ( on page 20), and it seems it worked:
http://www.ictp-saifr.org/schoolgr/Lect ... ighton.pdf


The only difference of my code with the original one is in the first line:

The original one has:

Code: Alles auswählen

import pylab, odeint
and I have

Code: Alles auswählen

import pylab
from scipy.integrate import odeint
Benutzeravatar
snafu
User
Beiträge: 6738
Registriert: Donnerstag 21. Februar 2008, 17:31
Wohnort: Gelsenkirchen

@Homer.Simpson:

`r = pylab.arange(10.0, 20000.0, dr)` is the very first line in your function. At that time Python does not know about the name `dr`. But it is used for the definition of `r`. This is why the error is thrown.

EDIT: Maybe you think that Python magically puts something into `dr` when `pylab.arange()` is called. But this is not how Python works. There are no references to memory adresses as you perhaps know from C programming.
Zuletzt geändert von snafu am Donnerstag 21. März 2013, 15:34, insgesamt 1-mal geändert.
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

snafu hat geschrieben:@Homer.Simpson:

`r = pylab.arange(10.0, 20000.0, dr)` is the very first line in your function. At that time Python does not know about the name `dr`. But it is used for the definition of `r`. This is why the error is thrown.

Thanks for your replay. But can you first have a look in to one post before your post which I posted :) I mean:

http://www.python-forum.de/viewtopic.ph ... 28#p237028

There I explained why there I do not need it.

Thanks you again
BlackJack

@Homer.Simpson: Don't expect program code on lecture slides or blackboards to actually work. ;-)

It is odd anyway: first line of `tosolve()` defines `r` using the value of (non-existing) `dr` and later `dr` is defined by using this very `r`. Such a circular definition can not work even if the arguments *would* be evaluated lazy instead of eager.
Benutzeravatar
snafu
User
Beiträge: 6738
Registriert: Donnerstag 21. Februar 2008, 17:31
Wohnort: Gelsenkirchen

You just explained why you *think* that you don't need it. But as you can see Python throws the error anyway. Thus, don't you think there may be some misunderstanding by you? ;)

Trust me/us: If you change your code to define `dr` *before* the function call then it will work without an error. Please also note the EDIT in my previous comment.
Homer.Simpson
User
Beiträge: 6
Registriert: Donnerstag 21. März 2013, 14:08

Thank you all, for your hints. I *will* try to stop posting and fix my code with your suggestions.


Bests
Benutzeravatar
/me
User
Beiträge: 3555
Registriert: Donnerstag 25. Juni 2009, 14:40
Wohnort: Bonn

In short what you try to do is similar to

Code: Alles auswählen

def function():
    a = b
    b = a
That won't work.
Antworten