# FallFriction.py
# Model of height and velocity of a ball that someone drops.
# Air friction considered and modeled as 0.5*C*density*A*v^2.  

import math

def FallFriction(velocity = 0, position = 400, DT = 0.001, simLength = 15):
    numIterations = int(simLength/DT) + 1
    mass = 0.5
    acceleration_due_to_gravity = -9.81
    weight = mass * acceleration_due_to_gravity
    radius = 0.05
    projected_area = math.pi * radius * radius
    air_friction = -0.65 * projected_area *  velocity * abs(velocity)
    total_force = weight + air_friction
    acceleration = total_force / mass
#    print "t\tposition\tvelocity"
    change_in_velocity = acceleration        
    change_in_position = velocity
    t = 0
    timeLst = [0]
    velocityLst = [velocity]
    positionLst = [position]
#    print '%10.2f\t%12.2f\t%12.2f' % (t, position, velocity)
    for i in range(1, numIterations):
        t = i * DT
        timeLst.append(t)	    
        velocity = velocity + (change_in_velocity) * DT
        velocityLst.append(velocity)	    
        position = position + (change_in_position) * DT
        positionLst.append(position)	    
        air_friction = -0.65 * projected_area * velocity * abs(velocity)
        total_force = weight + air_friction
        acceleration = total_force / mass
        change_in_position = velocity
        change_in_velocity = acceleration
#        print '%10.2f\t%12.2f\t%12.2f' % (t, position, velocity)

    outfile = open("fallfriction.dat", 'w')
    for i in range(numIterations):
        outfile.write("%6.3f\t%6.2f\t%6.2f\n" % (timeLst[i], positionLst[i], velocityLst[i]))
    outfile.close()


FallFriction()

