#!/usr/bin/env python # -*- coding: utf-8 -*- """ Definitions of Variables: entry_speed = speed at which train enters 30 km stretch, as entered by driver initially train_type = either 0 or 1, for type A (0) or type B (1) length1 = length of first stretch, length2 = length of second stretch, length3 = length of third stretch. accl1A = acceleration induced by gradient in stretch 1 for train A, accl1B = acceleration induced by gradient in stretch 1 for train B, accl2A = acceleration induced by gradient in stretch 2 for train A, accl2B = acceleration induced by gradient in stretch 2 for train B, accl3A = acceleration induced by gradient in stretch 3 for train A, accl3B = acceleration induced by gradient in stretch 3 for train B. min_speed_A = minimimum speed of Train A, min_speed_B = minimimum speed of Train B, max_speed_A = maximum speed of Train A, max_speed_B = maximum speed of Train B. All speeds in kms/hrs and accelerations in kms/(hrs*hrs). All lengths in kms. @date: Mon Jan 26 16:46:19 2015 @author: Arya Kumar Battacharya @copyright: Mahindra Ecole Centrale, 2015 """ import math # Input from the user train_type = input("Driver, please enter your train type as 0 (type A) or 1 (B) ") entry_speed = input("Driver, please enter the speed in km/hr ") length1 = 10.0 length2 = 10.0 length3 = 10.0 accl1A = -16.0 accl1B = -20.0 accl2A = 0.0 accl2B = 0.0 accl3A = 12.0 accl3B = 16.0 min_speed_A = 25.0 min_speed_B = 20.0 max_speed_A = 60.0 max_speed_B = 50.0 if (train_type == 0 and entry_speed > max_speed_A) or (train_type == 1 and entry_speed > max_speed_B): print "Speed above maximum limit, APPLY BRAKES IMMEDIATELY" elif (train_type == 0 and entry_speed < min_speed_A) or (train_type == 1 and entry_speed < min_speed_B): print "Speed below minimum limit, INCREASE TRACTION IMMEDIATELY" elif train_type == 0: # Stretch 1 Calculations v = math.sqrt(entry_speed ** 2 + 2.0 * accl1A * length1) if v < min_speed_A: t = (min_speed_A - entry_speed)/accl1A print "At", t, "hours after entry apply traction to prevent fall below minimum speed" # application of traction has stabilized speed at minimum v = min_speed_A s = entry_speed * t + 0.5 * accl1A * t * t t += (length1 - s)/min_speed_A else: t = (v - entry_speed)/accl1A # Now v is the entry speed into Stretch 2 which remains unchanged, and t the entry time # Stretch 2 calculations t += length2/v # Stretch #3 calculations v3 = math.sqrt(v ** 2 + 2.0 * accl3A * length3) #t3 = t if v3 > max_speed_A: t3 = (max_speed_A - v)/accl3A print "At", t3 + t, "hours after entry apply brakes to prevent crossing maximum speed" # application of traction has stabilized speed at maximum v3 = max_speed_A s = v * t3 + 0.5 * accl3A * t3 * t3 t3 = t + (length3 - s)/max_speed_A else: t3 = t + (v3 - v)/accl3A print "Final Speed is ", v3, " and final time is ", t3 else: # Stretch 1 Calculations v = math.sqrt(entry_speed ** 2 + 2.0 * accl1B * length1) if v < min_speed_B: t = (min_speed_B - entry_speed)/accl1B print "At", t, "hours after entry apply traction to prevent fall below minimum speed" # application of traction has stabilized speed at minimum v = min_speed_B s = entry_speed * t + 0.5 * accl1B * t * t t += (length1 - s)/min_speed_B else: t = (v - entry_speed)/accl1B # Now v is the entry speed into Stretch 2 which remains unchanged, and t the entry time # Stretch 2 calculations t += length2/v # Stretch #3 calculations v3 = math.sqrt(v ** 2 + 2.0 * accl3B * length3) #t3 = t if(v3 > max_speed_B): t3 = (max_speed_B - v)/accl3B print "At", t3 + t, "hours after entry apply brakes to prevent crossing maximum speed" # application of traction has stabilized speed at maximum v3 = max_speed_B s = v * t3 + 0.5 * accl3B * t3 * t3 t3 = t + (length3 - s)/max_speed_B else: t3 = t + (v3 - v)/accl3B print "Final Speed is", v3, "and final time is", t3 # Program ends