! Program to Calculate the Deflection of Curve Balls in Baseball and Softball
! Fortran 95 for Scientists and Engineers
! by Anna Tapio
! 19 April 2007

PROGRAM curve

IMPLICIT NONE

! Calculating the deflection of a softball and baseball for curve ball and comparing it to the force applied on them
! Also calculating the force and spin needed for baseball and softball to have same deflection

!jc  A little more description here would be nice, both on what the program
!jc  does and where you got your info.


! Declaring  variables
! Variables with b are designated for baseballs
! Variables with s are designated for softballs
! fb and fs are the lateral forces exerted on the baseball/softball from their spins, in N
! db and ds are the deflections of the baseball/softball, in m
REAL:: lb, ls, vol_ratio
REAL:: spinb, spins, mb, ms, rb, rs, p, g
REAL, DIMENSION(10):: v, fb1, fs1, db, ds
REAL, DIMENSION(3)::d, vel, fb2, fs2, x
INTEGER:: i, ier
INTEGER, EXTERNAL:: pgbeg

! Creating and opening files to store information throughout the program
OPEN(11, FILE='baseball.dat', STATUS='REPLACE')
OPEN(12, FILE='softball.dat', STATUS='REPLACE')

rb = 0.0380			! radius of a baseball, in m
spinb = 199.			! spin rate of a baseball, in rad/s
mb = 0.1488			! mass of a baseball, in kg
lb = 16.76			! distance baseball travels during flight, in m

rs = 0.0486			! radius of a softball, in m
spins = (spinb*rb)/rs		! sprin rate of a softball, in rad/s
ms = 0.1878			! mass of a softball, in kg
ls = 14.02			! distance softball travels during flight, in m

p = 1.25			! density of air with conversion from kg/m^3 

!jc  good comments on variable declarations 

!jc  Indentation still a bit odd - don't indent the DO - indent inside the 
!jc  DO block.  Full tabs are a bit much for indentation

!jc  It might have been nice to make this interactive, so that users could 
!jc  vary ball speed and rotation rates and seen the results.
	DO i = 1, 11					
		v(i) = (50.+((i-1)*2.))*(1609.344/3600.)	! varying the velocity of the pitch 
								! between 50 and 70 mph
								! converting mph to m/s
								! 1 mile = 1609.344 m
								! 1 hour = 3600 s
	END DO

db = (3.14159*p*(rb**3)*spinb*(lb**2))/(4.*mb*v)		
fb1 = (3.14159*p*spinb*v*(rb**3))/2.				

ds = (3.14159*p*(rs**3)*spins*(ls**2))/(4.*ms*v)
fs1 = (3.14159*p*spins*v*(rs**3))/2.

!jc  These forces wouldn't really be constants.

WRITE(11, *) 'Baseball Data at spin = 199 rad/s'
WRITE(11, *) ''
WRITE(11, *) 'Velocity (m/s)    ', 'Deflection (m)   ', 'Force (N)       '
WRITE(11, *) '==================', '=================', '================'

WRITE(12, *) 'Softball Data at spin = 156 rad/s'
WRITE(12, *) ''
WRITE(12, *) 'Velocity (m/s)    ', 'Deflection (m)   ', 'Force (N)       '
WRITE(12, *) '==================', '=================', '================'

! Do loop to write information into the data files
	DO i = 1, 10

		WRITE(11, '(F12.3, 5X, F12.3, 5X, F12.4)') v(i), db(i), fb1(i)
		WRITE(12, '(F12.3, 5X, F12.3, 5X, F12.4)') v(i), ds(i), fs1(i)
	END DO

! Now calculating force and spin needed at same deflection and velocity
! Comparing the ratio of the forces to the ratio of the volumes

d = (/0.1, 0.3, 0.5/)				! deflection of both softball and baseball
vel = (/22.352, 23.246, 24.140/)		! velocity of both softball and baseball

fb2 = (2.*mb*d*(vel**2))/(lb**2)		
fs2 = (2.*ms*d*(vel**2))/(ls**2)		

x = fs2/fb2				! ratio of the forces

vol_ratio = (rs**3)/(rb**3)		! ratio of volumes

WRITE(11, *) ''
WRITE(11, *) 'Baseball Data at Similar Deflection and Velocity'
WRITE(11, *) ''
WRITE(11, *) 'Velocity (m/s)    ', 'Deflection (m)    ',  'Force (N)       '
WRITE(11, *) '==================', '==================', '================='

WRITE(12, *) ''
WRITE(12, *) 'Softball Data at Similar Deflection and Velocity'
WRITE(12, *) ''
WRITE(12, *) 'Velocity (m/s)    ', 'Deflection (m)    ',  'Force (N)       '
WRITE(12, *) '==================', '==================', '================='

! Do loop to write information into the data files
	DO i = 1, 3
		WRITE(11, '(F12.3, 5X, F12.3, 5X, F12.4)') vel(i), d(i), fb2(i)
		WRITE(12, '(F12.3, 5X, F12.3, 5X, F12.4)') vel(i), d(i), fs2(i)
	END DO	

PRINT*, 'The spin of the softball is', spins
PRINT*, 'Ratio of forces is', x
PRINT*, 'Ratio of volumes is', vol_ratio

! Plotting the deflection of baseball versus the deflection of the softball

  ier = pgbeg(0, '/png',1,1) 			! Initialize plot
						! '/ps' saves the plot to a file

  CALL pgenv(22., 31., 0.6, 0.95, 0, 1)		! set scale for plot according to my data taken
												
  CALL pglab ('Velocity (m/s)', 'Deflection (m)', ' Deflection Comparison')	
						! set labels
						! velocity is the x axis, deflection is the y axis

  CALL pgline(10, v, ds, 1)			! plot the points for all v(i) and deflection

  CALL pgline(10, v, db, 1)

  CALL pgpt(11,v(1:11), db(1:11), 21)		! creating markers along the line to make distinct

  CALL pgpt(11,v(1:11), ds(1:11), 15)		! creating markers along the line to make distinct

  CALL pgtext (26., 0.8, 'Baseball')		! placing label near the line

  CALL pgtext (26., 0.72, 'Softball')		! placing laber near the line

  CALL pgend	

CLOSE(11)
CLOSE(12)

END PROGRAM


!jc  Overall good program.  

