PROGRAM mpg
! accounting for cross-sectional area and air density, this program 
! calculates the change in gas efficiency of a car compared to a baseline as well as plots
! the efficiency as a function of cross sectional area
! Written 3-7-2007
! by Michael Risch-Janson
! PGPLOT section is an Example from PGPLOT documentation
! comments added by Jim Crumley  1/30/2007
! jc  The line above seems odd in this context ...

IMPLICIT NONE

!jc   It would nice nice to define the variables and their units

REAL(KIND=8) :: F_r, mass, area_ft, area_m, v, k, wCw, density_air,k_base, area_base, mph, wCwA    !initialize variables
REAL(KIND=8) :: W, x, F_base, dist, gas2 ,miles, W_base, miles2, effic, k_car, F_car, W_car
REAL(KIND=8) :: mileage(20), area_feet(20), area_meters(20), k_1(20), drag_area, drag_coeff(50)
REAL(KIND=8) :: effic_car,  mileage_car, mpg_car, efficiency_car
REAL(KIND=8) ::  Force_r(20), Work(20), mileage_base(20), efficiency(20) 
INTEGER :: i, PGBEG, IER, j, car_year(50)
CHARACTER(len=100) :: car_name(50), enter


  PRINT *, 'What is the cross-sectional area in square feet?'        ! promt for input of cross sectional area
  READ *, area_ft   ! square feet
  PRINT *,''

area_m = area_ft * .0929      ! square feet to square meters conversion
v= 30.                        ! calulations done at this velocity in meters per second = 67 mph
x = 1600.                     ! distance of one mile in meters
                                !jc a mile is not 1600 m to 4 digits.

wCw = .3125                   ! drag coefficient to be used for car - unitless
density_air = 1.2             ! air density in kg /m^3
k= 1./2. * wCw* density_air* area_m     ! kg/m - k value to be used in calculating resistance force

F_r = k * v**2.                ! resistive force by air on car

wCwA = 5.0 * .0929             ! drag coefficient * cross sectional area for base car - m^2
                                ! equivilent to 16 ft^2 area * .3125 drag coefficient
k_base = 1./2. * density_air * wCwA   ! kg/m - k value for base car
F_base = v**2.*k_base                ! resistive force on base car

W_base = F_base * x            ! work over a mile done on base car by resistive force - Joules
W = F_r * x                    ! work over a mile done on user's car by resistive force - Joules

! approx 136000000. joules in a gallon of gas
! since all end calulations are percentages or comparisons to base car
! the actual amount of joules doesn't matter
! the actual number will vary based on efficiency of engine but the efficiency of the engine
! is taken to be constant between the two vehicles like other factors

miles = 136000000. / W          !miles the car will travel on one gallon of gas based on accepted amount of joules in 
				! gallon of gas and work doneby air resistance force - user's car
miles2 = 136000000. / W_base    ! base car- miles

effic = miles / miles2 * 100.   ! comparison of user's car to base car- % of gas mileage efficiency

 !Print efficiency to screen and read statement to pause program until enter is pressed

  WRITE(*,'(A,F6.2,A)'), 'The gas mileage efficiency of your car based on the cross sectional area is: ', effic, '%'
  PRINT *, 'of that of a baseline car with cross sectional area of 16 square feet.'
  PRINT *,''
  PRINT *, 'Please press enter to continue.' 
  PRINT *, 'A list of drag coeffecient - cross sectional area constansts for various vehicles will be displayed.'
  READ *, 

!opens file which contains  specific car models and drag coeff-cross sectional area constants corrsponding
!to those cars
OPEN (10, file ='cda',STATUS='OLD', ACTION='READ')  !open data file which already exists and will only be read

 ! read and write in the data formatted 
  DO i=1,41                               ! reading in the data from the file 
	READ (10,*) drag_coeff(i), car_year(i), car_name(i) ! x
   	PRINT (*,'(F5.2,"  ", I5," ", A)'), drag_coeff(i), car_year(i), car_name(i)
  END DO

  ! input of constant by user
  PRINT *,''
  PRINT *, "Please type in the drag coefficient-area constant assosiated with your vehicle ", & 
  "or the vehicle which most closely resembles yours:"
  READ *, drag_area


! since drag - area constant is the only difference between base car and user's vehicle
! the following calculation gives the efficiency as a % of the base car
! based on that a efficiency and a value of 40 mpg for the base car it gives the 
!mpg of the user's car
!  this is added to give the user an idea of what a certain % means in actual numbers
! the 40 mpg is not an accurate number
efficiency_car = 5.0 / drag_area * 100.
mpg_car = 40. * efficiency_car / 100.

 !print the data and pause again until enter is pressed
  PRINT *,''
  WRITE(*,*) 'The gas mileage efficiency of your vehicle based on the drag coefficient-area constant',&
  ' you selected and compared to the base vehicle constant of 5.0 is:'
  WRITE (*,'(F6.2,A)'), efficiency_car, '%'
  PRINT *, ''
  WRITE (*,'(A,A,F6.2,A)') 'This means that if the base vehicle gets 40 mpg that your vehicle',&
  ' would achieve about:',mpg_car, ' mpg'
  PRINT *,''
  PRINT *, "Please press enter to continue."
  PRINT *, "A graph of gas mileage efficiency vs cross sectional area will be displayed with 16 square feet as the base."
  READ*,

! calculate graph points for various cross sectional areas
! Here would be the calculations except as before the only 
! variable between the base vehicle and the varying one is
! the cross sectional area-
!area_meters(i) = area_feet(i) * .0929
!k_1(i) = 1./2. * wCw* density_air* area_meters(i)
!Force_r(i) = k_1(i) * v**2.
!Work(i) = Force_r(i) * x
!mileage(i) = (136000000. / Work(i))
!efficiency(i) = mileage(i) / miles2 * 100.  
!These can be simplified to this Do loop
 DO i= 1, 15
	area_feet(i) = 14. + 2.*i
	efficiency(i) = 16. / area_feet(i) * 100.
  END DO

!graphin of the data

IER = PGBEG (0, '/XSERV',1,1)                                 
  
  IF (IER.NE.1) STOP

!sets up scale on plots(xmin, xmax, ymin, ymax,scales_equal?,grid_features)
CALL PGENV(16.,44., 0., 100. ,0,1)

!set x and y axis labels and title in that order
CALL PGLAB('Area (square feet)', 'Gas mileage efficiency (% of baseline)', 'Change in gas mileage efficiency due to area') 

! Draw the line for the theoretical  (num_points, x_data, y_data)
CALL PGLINE(15,REAL(area_feet(1:15)),REAL(efficiency(1:15)))             
!close the plot
CALL PGEND



END PROGRAM mpg

!jc  Fairly well commented, though more comments about the variable
!jc  definitions at the debinnging would nice.  Program seems reasonable,
!jc  though a bit boring mathematically - we probably should have found
!jc  a way to make the calculations more interesting.
