      PROGRAM BEAM
******************************************************************
* This program calculates deflections in a cantilevered beam     *
* under a given load. Three different loading conditions are     *
* analyzed.                                                      *
*                                                                *
*   Case #1 : a single point load at the free end of the beam    *
*   Case #2 : a single point load at an interior point           *
*   Case #3 : load uniformly distributed along the beam          *
*                                                                *
*     ELAST  : modulus of elasticity                             *
*     INERT  : moment of inertia                                 *
*     LENG   : length of the beam                                *
*     NUMPTS : number of divisions at which deflections are found*
*     DELTAX : distance between points                           *
*     OPTION : option selected by the user                       *
*                                                                *
* Input:  ELAST, INERT, LENG, NUMPTS, OPTION                     *
* Output: A table of deflections                                 *
******************************************************************

      INTEGER NUMPTS, OPTION
      REAL ELAST, INERT, LENG, DELTAX

* Get beam information

      PRINT *, 'ENTER MODULUS OF ELASTICITY, MOMENT OF INTERTIA,',
     +         ' LENGTH OF THE BEAM'
      PRINT *, 'AND NUMBER OF POINTS TO USE:'   
      READ *, ELAST, INERT, LENG, NUMPTS
      DELTAX = LENG / REAL(NUMPTS)

* Repeat the following until user selects option 0 to stop

10    CONTINUE
          PRINT *
          PRINT *, 'SELECT ONE OF THE FOLLOWING OPTIONS'
          PRINT *, '0 : STOP'
          PRINT *, '1 : END LOAD'
          PRINT *, '2 : INTERMEDIATE LOAD'
          PRINT *, '3 : UNIFORM LOAD'
          READ *, OPTION
          IF (OPTION .EQ. 0) THEN
              PRINT *, 'DONE PROCESSING FOR THIS BEAM'
          ELSE IF (OPTION .EQ. 1) THEN
              CALL CASE1(ELAST, INERT, LENG, DELTAX, NUMPTS)
          ELSE IF (OPTION .EQ. 2) THEN
              CALL CASE2(ELAST, INERT, LENG, DELTAX, NUMPTS)
          ELSE IF (OPTION .EQ. 3) THEN
              CALL CASE3(ELAST, INERT, LENG, DELTAX, NUMPTS)
          ELSE
              PRINT *, 'NOT A VALID OPTION'
          END IF
      IF (OPTION .NE. 0) GO TO 10
      END


** CASE1 *********************************************************
* Computes deflections for a single point load at the free end   *
* of the beam.  Local variables used are:                        *
*     TLOAD  : total load                                        *
*     I      : control variable used in DO loop                  *
*     X      : distance from the free end of the beam            *
*     DEFLEC : deflection at distance X                          *
*     TEMP   : temporary variable used in calculation of DEFLEC  * 
* Accepts: ELAST, INERT, LENG, DELTAX, NUMPTS (see main program).*
* Input:   Total load TLOAD.                                    * 
* Output:  Table of deflections.                                 *
******************************************************************

      SUBROUTINE CASE1(ELAST, INERT, LENG, DELTAX, NUMPTS)

      REAL ELAST, INERT, LENG, DELTAX, TLOAD, X, DEFLEC, TEMP
      INTEGER NUMPTS, I

      PRINT *, 'ENTER TOTAL LOAD:'
      READ *, TLOAD
      X = 0.0
      DO 10 I = 1, NUMPTS
          TEMP = X**3 - 3.0 * LENG**2 * X + 2.0 * LENG**3
          DEFLEC = (-TLOAD / (6.0 * ELAST * INERT)) * TEMP
          PRINT *, 'AT DISTANCE', X, ' DEFLECTION = ', DEFLEC
          X = X + DELTAX
10    CONTINUE
      END


**CASE2***********************************************************
* Computes deflections for a single point load at an interior    *
* point of the beam.  Local variables used are:                  *
*     TLOAD  : total load                                        *
*     A,B    : distances along the beam                          *
*     I      : control variable used in DO loop                  *
*     X      : distance from the free end of the beam            *
*     DEFLEC : deflection at distance X                          *
*     TEMP   : temporary variable used in calculation of DEFLEC  *
* Accepts: ELAST, INERT, LENG, DELTAX, NUMPTS (see main program).*
* Input:   Total load TLOAD and distance A.                      *
* Output:  Table of deflections.                                 *
******************************************************************

      SUBROUTINE CASE2(ELAST, INERT, LENG, DELTAX, NUMPTS)

      REAL ELAST, INERT, DELTAX, TLOAD, A, B, X, DEFLEC, TEMP
      INTEGER NUMPTS, I

      PRINT *, 'ENTER TOTAL LOAD AND DISTANCE A:'
      READ *, TLOAD, A
      B = LENG - A
      X = 0.0
      DO 10 I = 1, NUMPTS
          IF (X .LT. B) THEN
              TEMP = -A**3 + 3.0 * A**2 * LENG - 3.0 * A**2 * X
          ELSE
              TEMP = (X - B)**3 - 3.0 * A**2 * (X - B) + 2.0 * A**3
          END IF
          DEFLEC = (-TLOAD / (6.0 * ELAST * INERT)) * TEMP
          PRINT *, 'AT DISTANCE', X, ' DEFLECTION = ', DEFLEC
          X = X + DELTAX
10    CONTINUE
      END


**CASE3***********************************************************
* Computes deflections for a load uniformly distributed along    *
* the beam.  Local variables used are:                           *
*     ULOAD  : unit load when uniformly distributed              *
*     TLOAD  : total load                                        *
*     I      : control variable used in DO loop                  *
*     X      : distance from the free end of the beam            *
*     DEFLEC : deflection at distance X                          *
*     TEMP   : temporary variable used in calculation of DEFLEC  *
* Accepts: ELAST, INERT, LENG, DELTAX, NUMPTS (see main program).*
* Input:   Unit load ULOAD.                                      *
* Output:  Table of deflections.                                 *
******************************************************************

      SUBROUTINE CASE3(ELAST, INERT, LENG, DELTAX, NUMPTS)

      REAL ELAST, INERT, DELTAX, ULOAD, TLOAD, X, TEMP
      INTEGER NUMPTS, I

      PRINT *, 'ENTER UNIT LOAD:'
      READ *, ULOAD
      TLOAD = ULOAD * LENG
      X = 0.0
      DO 10 I = 1, NUMPTS
          TEMP = X**4 - 4.0 * LENG**3 * X + 3.0 * LENG**4
          DEFLEC = (-TLOAD / (24.0 * ELAST * INERT * LENG)) * TEMP
          PRINT *, 'AT DISTANCE', X, ' DEFLECTION = ', DEFLEC
          X = X + DELTAX
10    CONTINUE
      END

