Pregunta

im new to programming and have been using python to simulate to some physical systems, in Spyder on OSX 10.9.2. I dont think this a problem with my code because it runs fine once but then after that when i hit run, the command line (Python interpreter i think its called?) just displays runfile('/Users/Paddy/....name of file) and i cant run the code again after that. even other simple small programs wont run. The '>>>' in the command line has disappeared.

I have searched the web for a solution but to be honest, im not exactly sure what im looking for or what type of error this is, whether its a bug in Spyder or otherwise. Should my code have some sort of 'termination'?

Ive included the full body of code im working on just incase there is an error in there. Like i say, im completely new to this and i cnt tell whether this is an issue with Spyder or my code. Any help would be greatly appreciated, i have a deadline looming! Thanks

# Velocity Verlet integrator

def Verlet(x, V, dt, A):

    x_new = x + V*dt + (A(x,V,R)*dt**2)/2
    V_new = V + (A(x,V,R) + (2/(2-dt))*((((48/x_new**13)-(24/x_new**7)) - V + (0.5)*A(x,V,R)*dt + 2**(0.5) * R)) )/2 * dt
    return (x_new, V_new)


# Start main program

# Import required libraries
import numpy as np
from numpy import array, zeros
import random   

mu, sigma = 0, 0.1 # mean and variance
S = np.random.normal(mu, sigma, 1000) # Random numbers generated from gaussian



# Then the function itself



def A(x,V,R):

    Acc = (((48/x**13)-(24/x**7)) - V + 2**(0.5) * R)

    return Acc

# Set starting values for position and velocity
x = array([5])
V = array([0])




N = 1000 # integration time steps
M = 10  # save position every M timestep
dt = 1.0 / (N) # calculate timestep length in seconds

# Lists for storing the position and velocity
Xlist = zeros([1,N/M]) #define vector dimensions
Vlist = zeros([1,N/M])
# Put the initial values into the lists
Xlist[:,0] = x
Vlist[:,0] = V

# Run simulation

print "Total number of steps:", N
print "Saving location every %d steps." % (M)
print "Start."
for i in range(N/M):
    # Run for M steps before saving values
    for j in range(M):
          # Update position and velocity based on the current ones
          # and the acceleration function 
          R = random.choice(S) # selects random number from S 
          x, V = Verlet(x, V, dt, A)

    # Save values into lists
    Xlist[:, i] = x
    Vlist[:, i] = V
print ("Stop.")

print (Xlist)
print (Vlist)



L = zeros([1,N/M])

k=0
while k < 101:
    l = k+1
    L[:,l]

print (L)



# Plot results  
from matplotlib import pyplot as plt
#plt.plot(L, Xlist) 
# Set equal axis
plt.axis('equal')
# Draw x and y axis lines
plt.axhline(color="black")
plt.axvline(color="black")

#plt.show()
¿Fue útil?

Solución

It's an infinite loop in your while k < 101 loop because you never increment k. Try for example:

k=0
while k < 100:
    L[:,k]
    k += 1

Also note that python is 0 based. So you need k to go from 0 to 99 for a 100 length vector, not 1 to 100.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top