      ! ************************************************************************
      
      ! THIS MODULE IS A CONTAINER FOR THE INITIALISATION AND SOLUTION
      ! OF A USER EQUATION OF STATE
      
      ! THE FOLLOWING ROUTINES ARE INCLUDED:

      ! MODULE EOS_USER_1
      !   DEFINE VARIABLES THAT ARE COMMON BETWEEN THE ROUTINES BELOW

      ! SUBROUTINE INIT_EOS_USER_1
      !   DEFINE THE INPUT PARAMETERS FOR THE USER EOS

      ! SUBROUTINE CHECK_EOS_USER_1
      !   CHECK PARAMETERS ARE VALID FOR THE USER EOS

      ! SUBROUTINE SET_EOS_USER_1
      !   SET SHORTCUTS TO PARAMETERS FOR THE USER EOS

      ! SUBROUTINE SOLVE_EOS_USER_1
      !   SOLVE THE USER EQUATION OF STATE

      ! SUBROUTINE BULK_EOS_USER_1
      !   GET BULK MODULUS FOR THE USER EQUATION OF STATE

      ! BEFORE EACH ROUTINE IS CALLED, THE FOLLOWING POINTERS ARE SET-UP
      !   MTL - POINTER TO THE CURRENT MATERIAL
      !   EQ  - POINTER TO THE CURRENT FLAG/EQUATION/MATERIAL OPTION
      
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      ! NOTE: CARE SHOULD BE TAKEN WHEN USING THIS MATERIAL MODEL WITH THE 
      !       ANP TETRAHEDRAL ELEMENTS. IT IS HIGHLY RECOMMENDED THAT YOU
      !       CONTACT ANSYS FOR ADVICE ON THE TYPES OF MATERIALS
      !       APPLICABLE TO THE ANP TERTRAHEDRAL ELEMENT.
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      ! ************************************************************************

      MODULE EOS_USER_1
      USE kindef
      IMPLICIT NONE
      SAVE

      ! SPECIFY COMMON VARIABLES TO BE ACCESSED BY ROUITNES BELOW HERE
      !INTEGER(INT4) ::
      !REAL(REAL8) ::
      REAL(REAL8), DIMENSION(50) :: EC
      
      END MODULE EOS_USER_1

      SUBROUTINE INIT_EOS_USER_1(IFACT)

      USE material
      USE eos_user_1

      IMPLICIT NONE

      INTEGER (INT4) ::   IFACT

      ! ************************************************************************

      ! THIS SUBROUTINE INITIALISES (ALLOCATES) PARAMETERS AND DATA

      ! FLAG - IMF_EOS_USER_1

      ! INPUT - IFACT = 0 JUST GET NAME OF EQUATION TO DISPLAY IN UI
      !         IFACT = 1 EQUATION IS ACTIVE HENCE ALLOCATE AND SET DATA

      ! ************************************************************************

      ! DEFINE PARAMETERS TO ALLOW ALLOCATION
      EQ%EQTYPE = IMF_EOS_USER_1 ! DO NOT MODIFY THIS LINE
      EQ%NAME = 'User EOS #1'
      EQ%NPAR = 11               ! NUMBER OF REAL INPUT PARAMETER
      EQ%NUMOPT = 0              ! NUMBER OF OPTION LISTS
      EQ%NDEPFLG = 0             ! NUMBER OF NON-OPTIONAL DEPENDANT (CHILD) FLAGS/MODEL OPTION
      EQ%NCHAR = 0
      EQ%NPAR_VEC = 0

      IF (IFACT==1) THEN
        CALL ALLOC_EQ ! DO NOT MODIFY THIS LINE, ALLOCATES MEMORY

        ! FOR EACH REAL INPUT PARAMETER, ASSIGN DATA
        !   FOR EXAMPLE, TO SET VALUES FOR PARAMETER 1:

        !             ('name        '      L, T, M, H, val, min,max, default,0,required)
        EQ%PAR(1)=PRMT (1,'Bulk Modulus'     ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO,0,1)
        EQ%PAR(2)=PRMT (2,'EC(2)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(3)=PRMT (3,'EC(3)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(4)=PRMT (4,'EC(4)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(5)=PRMT (5,'EC(5)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(6)=PRMT (6,'EC(6)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(7)=PRMT (7,'EC(7)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(8)=PRMT (8,'EC(8)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(9)=PRMT (9,'EC(9)'           ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(10)=PRMT(10,'EC(10)'          ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)
        EQ%PAR(11)=PRMT(11,'EC(11)'          ,-1,-2,1,0,ZERO,ZERO,BIG,ZERO   ,0,0)

        ! FOR EACH OPTION LIST, ASSIGN DATA
        !   FOR EXAMPLE, SELECT AN EOS FOR THE SOLID PHASE
        !   EQ%OPTION(1)%NAME = 'Solid EOS'  ! OPTION LIST NAME
        !   EQ%OPTION(1)%NUMOPT = 3          ! NUMBER OF OPTIONS IN THE LIST
        !   EQ%OPTION(1)%DEFAULT = 1         ! DEFAULT OPTION
        !   EQ%OPTION(1)%SELECTED = 1        ! SELECTED OPTION
        !   CALL ALLOC_OPTION(1)             ! ALLOCATE THE MEMORY
        !   DEFINE OPTIONS
        !                                ('name  ',active,' ',0 / Dependant (child) flag)
        !   EQ%OPTION(1)%OPTS(1) = OPTION('Linear','Y',' ',IMF_EOS_LINEAR)
        !   EQ%OPTION(1)%OPTS(2) = OPTION('Polynomial','Y',' ',IMF_EOS_POLYNOMIAL)
        !   EQ%OPTION(1)%OPTS(3) = OPTION('Shock','Y',' ',IMF_EOS_SHOCK)

        ! FOR EACH NON-OPTIONAL DEPENDANT (CHILD) FLAG/MODEL OPTION, ASSIGN DEPENDANT FLAG
        !   EQ%DEPFLG(1) = IMF_TEMPERATURE
      ENDIF

      ! SET IN ACTIVE SWITCH FOR APPROPRIATE PROCESSOR TYPE:: ALL ON BY DEFAULT
      !   FOR EXAMPLE, SWITCH OF FOR EULER FCT
      EQ%IFSOLVER(ISLV_FCT) = 0

      RETURN
      END SUBROUTINE INIT_EOS_USER_1


      SUBROUTINE SET_EOS_USER_1

      USE material
      USE eos_user_1

      IMPLICIT NONE

      ! ************************************************************************

      ! THIS SUBROUTINE ASSIGNS SHORTCUTS FOR DIRECT USE IN THE SOLVER

      ! ************************************************************************

      RHOREF = MTL%RHOREF  ! DO NOT MODIFY THIS LINE
      A1     = EQ%PAR(1)%VAL
      EC(1) = EQ%PAR(1)%VAL
      EC(2) = EQ%PAR(2)%VAL
      EC(3) = EQ%PAR(3)%VAL
      EC(4) = EQ%PAR(4)%VAL
      EC(5) = EQ%PAR(5)%VAL
      EC(6) = EQ%PAR(6)%VAL
      EC(7) = EQ%PAR(7)%VAL
      EC(8) = EQ%PAR(8)%VAL
      EC(9) = EQ%PAR(9)%VAL
      EC(10) = EQ%PAR(10)%VAL
      EC(11) = EQ%PAR(11)%VAL

      !   ISEL_OPT = EQ%OPTION(1)%SELECTED

      RETURN

      END SUBROUTINE SET_EOS_USER_1

      SUBROUTINE CHECK_EOS_USER_1

      USE material
      USE eos_user_1

      IMPLICIT NONE

      ! ************************************************************************

      ! THIS SUBROUTINE CHECKS INPUT DATA

      ! ************************************************************************

      ! PLACE USER CHECKS HERE

      RETURN

      END SUBROUTINE CHECK_EOS_USER_1

      SUBROUTINE SOLVE_EOS_USER_1 &
      (P0        ,E0        ,ALPHA     ,DEN       ,DVV       ,DSIE      , &
       Q         ,PP        ,EE        ,SOUND     ,IFAIL     ,IFSPHQ    , &
       IF_SIMULTANEOUS      ,IFGAS     ,ARHO      ,BRHO      ,DADETA    ,DBDETA )

      USE material
      USE eos_user_1
      USE wrapup
 
      IMPLICIT NONE

      INTEGER(INT1)  ::  IFAIL
      INTEGER (INT4) :: IFSPHQ,  IFLAG,  IFGAS, IF_SIMULTANEOUS
      REAL (REAL8)   ::  ALPHA,    DEN,   DSIE,    DVV,     EE,     E0
      REAL (REAL8)   ::     PP,     P0,      Q,  SOUND,   ARHO,   BRHO
      REAL (REAL8)   :: DADETA, DBDETA, EMU, BULK

      ! ************************************************************************

      ! THIS IS A USER SUPPLIED SUBROUTINE WHICH CALCULATES PRESSURE
      ! AND ENERGY AS A FUNCTION OF DENSITY. YOU ARE REQUIRED TO
      ! SIMULTANEOSLY SOLVE THE EQUATION OF STATE ( P=f(DEN,E) ) AND
      ! THE ENERGY EQUATION ( DE + P*DV = 0 ).

      ! INPUT PARAMETERS

      !    P0      OLD PRESSURE + OLD ART. VISCOSITY
      !    E0      OLD INTERNAL ENERGY
      !    ALPHA   OLD ALPHA
      !    DEN     NEW DENSITY
      !    DVV     DV/V FROM OLD VALUES TO NEW VALUES
      !    DSIE    SPECIFIC INTERNAL ENERGY DEPOSITION
      !    Q       NEW ARTIFICIAL VISCOSITY
      !               IFSPHQ = 0 - NEW VISCOUS PRESSURE
      !               IFSPHQ = 1 - VISCOUS ENERGY INCREMENT (PER UNIT MASS)
      !    IFAIL   STRESS STATE INDICATOR
      !            = 0   HYDRO
      !            = 1   ELASTIC
      !            = 2   PLASTIC
      !            > 3   BULK FAILURE
      !    IFSPHQ  =1 FOR A SPH SUBGRID

      ! OUTPUT PARAMETERS

      !    IF_SIMULTANEOUS  INDICATE WHETHER YOU WANT AUTODYN TO DO A SIMULTANEOUS SOLUTION
      !                     FOR PRESSURE AND ENERGY, OR WHETHER THIS IS INCLUDED IN THE USER
      !                     SUBROUTINE
      !    DSIE    SPECIFIC INTERNAL ENERGY DEPOSITION
      !    Q       RESET TO ZERO IF CELL IS FAILED
      !    IFAIL   STRESS STATE INDICATOR (SEE ABOVE)


      !    IF_SIMULTANEOUS = 0, YOU WANT AUTODYN TO DO THE SIMULTANEOUS SOLUTION
      !      ASSUMES THAT EOS IS IN THE FORM P = ARHO(DVV) + BRHO(DVV)*DEN*ENERGY
      !      SEE EQN 11.17 TO 11.21 OF THEORY MANUAL FOR MORE DETAILS
      !      OUTPUT:
      !         ARHO    
      !         BRHO    
      !         DADETA  d(ARHO)/d(DVV) DIFFERENTIAL OF ARHO wrt DVV
      !         DBDETA  D(BRHO)/d(DVV) DIFFERENTIAL OF BRHO wrt DVV

      !    IF_SIMULTANEOUS = 1, SIMULTANEOUS SOLUTION DONE IN USER MODEL
      !      OUTPUT:
      !         PP      NEW PRESSURE
      !         EE      NEW INTERNAL ENERGY
      !         SOUND   BULK SOUNDSPEED

      
      ! ADDITIONAL INFORMATION AVAILABLE THROUGH 

      !    MATNO          THE MATERIAL NUMBER OF THE MATERIAL BEING PROCESSED

      ! ************************************************************************

      ! TEMPORARY ERROR MESSAGE - REPLACE WITH YOUR OWN CODE
      CALL USR_MESSAG ('User subroutine USER_1 missing')
      NSWRAP = 9


      ! EXAMPLE FOR A LINEAR EOS:
      ! CALL BULK_EOS_USER_1 (BULK)
      ! IF_SIMULTANEOUS = 0
      ! EMU = (DEN/RHOREF-ONE)
      ! ARHO = BULK*EMU
      ! BRHO = ZERO
      ! DADETA = BULK
      ! DBDETA = ZERO

      ! TO ACCESS A V4.3 USER SUBROUITNE FOR AUTODYN-2D, UNCOMMENT THE NEXT TWO LINES
      ! IF_SIMULTANEOUS = 1
      ! CALL EXEOS (P0,E0,ALPHA,DEN,DVV,DSIE,Q,PP,EE,SOUND,IFAIL,IFSPHQ)

      RETURN

      END SUBROUTINE SOLVE_EOS_USER_1


      SUBROUTINE BULK_EOS_USER_1 (BULK      )

      USE material
      USE eos_user_1

      IMPLICIT NONE

      REAL (REAL8)   ::        BULK

      ! ************************************************************************

      ! THIS SUBROUTINE SETS THE BULK MODULUS FOR THE EOS
 
      ! ************************************************************************

      ! FOR EXAMPLE
      ! BULK = A1

      ! TO ACCESS A V4.3 USER SUBROUITNE FOR AUTODYN-2D, UNCOMMENT THE NEXT LINE
      !CALL EXBULK(BULK)

      RETURN

      END SUBROUTINE BULK_EOS_USER_1

