0

dear expert i am trying to write a simulation and in my code i have a class like this:

... (some def are here)
 class multipole:
 global xxp,yyp,zzp,x,y,z,xp,yp,zp,t,tm,h
 xxp,yyp,zzp,x,y,z,xp,yp,zp =xxpyypzzp() # some initial values calling
 @staticmethod 
 def quad(f1, f2,f3):
 global t,h,mass,ksimax 
 while t < ksimax:
 rk4_two(t,h,mass, f1, f2, f3, xxp, yyp, zzp) # rk function for new xxp, yyp and zzp 
 t = t + h
 tm.append(t)
 xp.append(xxp[1])
 x.append(xxp[0])
 yp.append(yyp[1])
 y.append(yyp[0])
 zp.append(zzp[1])
 z.append(zzp[0])
 return xp, x, yp,y,zp,z,tm
 if __name__ == "__main__":
 qp=multipole()
 quxp, qux, quyp,quy,quzp,quz,qutm=qp.quad(0.,0.,0.)
 hxp, hx, hyp,hy,hzp,hz,htm =qp.quad(0.022,0.,0.)
 oxp, ox, oyp,oy,ozp,oz,otm =qp.quad(-0.023,-0.032,0.0 )

my question is this code only calculate (quxp, qux, quyp,quy,quzp,quz,qutm), but not others (others will turn same value of quxp, qux, quyp,quy,quzp,quz,qutm) could you please tell me why? i am new in python any comments will be appreciated.

asked Feb 16, 2015 at 9:04
6
  • this isn't a class. you never create one, and it has no methods. also, using global inside a class body is highly bizarre. where did you learn about python classes? Commented Feb 16, 2015 at 9:07
  • By "a class like this" do you mean a meaningless mess? There's really no point in a class that only has one static method; Python isn't Java! Please provide a minimal example with sensible names, see e.g. stackoverflow.com/help/mcve. Commented Feb 16, 2015 at 9:10
  • @Eevee, what do you mean, "never create one"? The class definition is there (ignoring strange design) and an instance is instantiated in the if __main__ ... block. Commented Feb 16, 2015 at 9:27
  • er, yes, sorry. but it has no state; it doesn't do anything. you don't even need that instance since you could call quad directly on the class Commented Feb 16, 2015 at 9:30
  • Hi Husnu, did you solve your problem? Commented Mar 4, 2015 at 20:32

1 Answer 1

0

Ignoring the fact that this code is... somewhat flawed. I think that the problem is that you are using t which is apparently global but you don't reset it anywhere - so this loop:

while t < ksimax:
 ...

Will only run once, unless you reset t somewhere. Some pseudo code to explain why this happens:

counter = 0
def do_something():
 global counter
 print "Starting at", counter
 while counter <= 10:
 print counter
 counter += 5
 print "Done"
do_something()
# Starting at 0
# 0
# 5
# 10
# Done
do_something() # Called again, the counter is at 10 now:
# Starting at 10
# Done

As others have mentioned, your code could benefit from some heavy refactoring. Some starting points:

  • Naming! What does xxpyypzzp even mean? Even if it's obvious to you today, it must be hard to read even for you and unless you have Rainman-like memory you will not understand this next week. Try using descriptive names and if you find yourself adding prefixes or suffixes to variables because you run out of names - think about encapsulating some of this complexity in a class. It seems like the suffixes xp, x, yp, y, zp, z and tm are used a lot. At least create a named tuple to hold these values.
  • Global variables is generally considered harmful. They make it hard to reason about code - which is why you got this bug in the first place. If you are sprinkling global statements over your code there is time to redesign it. Think about which part of your code should "own" which parts of the state.
  • Python has a coding standard, called PEP8 - read it and try to follow it.
answered Feb 16, 2015 at 9:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.