Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Created August 17, 2019 23:06
Show Gist options
  • Save asmallteapot/3a5c4f02abdfed4e1245ce7d0f05a871 to your computer and use it in GitHub Desktop.
Save asmallteapot/3a5c4f02abdfed4e1245ce7d0f05a871 to your computer and use it in GitHub Desktop.
modified version of @alon_levy’s train performance calculator
def integratel(f,a,b,n):
y = 0
for i in range(0,n):
y = y + f(a + (b-a)*i/n)
return y*(b-a)/n
def acceleration_time_fn(k,a,b,c,m):
return lambda x : max(x/(k - a*x - b*x**2 - c*x**3), 1/m)
def deceleration_time_fn(k,a,b,c,m):
return lambda x : max(x/(k + a*x + b*x**2 + c*x**3), 1/m)
def acceleration_distance_fn(k,a,b,c,m):
return lambda x : max((x**2)/(k - a*x - b*x**2 - c*x**3), x/m)
def deceleration_distance_fn(k,a,b,c,m):
return lambda x : max((x**2)/(k + a*x + b*x**2 + c*x**3), x/m)
def acceleration_time(k,a,b,c,m,x1,x2,n):
return integratel(acceleration_time_fn(k,a,b,c,m),x1,x2,n)
def deceleration_time(k,a,b,c,m,x1,x2,n):
return integratel(deceleration_time_fn(k,a,b,c,m),x1,x2,n)
def acceleration_distance(k,a,b,c,m,x1,x2,n):
return integratel(acceleration_distance_fn(k,a,b,c,m),x1,x2,n)
def deceleration_distance(k,a,b,c,m,x1,x2,n):
return integratel(deceleration_distance_fn(k,a,b,c,m),x1,x2,n)
def acceleration_penalty(k,a,b,c,m,x1,x2,n):
return acceleration_time(k,a,b,c,m,x1,x2,n) - acceleration_distance(k,a,b,c,m,x1,x2,n)/x2
def deceleration_penalty(k,a,b,c,m,x1,x2,n):
return deceleration_time(k,a,b,c,m,x1,x2,n) - deceleration_distance(k,a,b,c,m,x1,x2,n)/x2
def slow_zone_penalty(k,a,b,c,m,x1,x2,n):
return acceleration_penalty(k,a,b,c,m,x1,x2,n) + deceleration_penalty(k,a,b,c,m,x1,x2,n)
print "This is a train performance calculator."
print "Please input the train's performance specs:"
print "k is power/weight ratio in kW/t. For N700 Shinkansen, use 26.74. For a generic train, use 20."
print "m is initial acceleration in m/s^2. For N700, use 0.9; for generic, use 0.5."
print "a, b, c are the constant, linear, quadratic terms in track resistance."
print "For X2000, use a = 0.0059, b = 0.000118, c = 0.000022."
print "For latest-model Shinkansen, use c = 0.000018."
print "Use acceleration_penalty and deceleration_penalty for acceleration/deceleration penalties from speed x1 to x2 m/s, where x2 > x1. slow_zone_penalty = acceleration_penalty + deceleration_penalty."
print "Input an arbitrary large integer for n. Try n = 500."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment