*deck,userinter    parallel   USERDISTRIB                                yz
      subroutine userinter (ndim,coor,nkeyopt,keyopt,nrl,rlconst,
     x  npropu,uprop,nintIn,intIn,nrealIn,realIn,kupdhis,localr,
     x  nuval,nintp,usvr,ncomp,stress,strain0,strain,
     x  kstat,mu,dt,dtdp,kdamp,damp,fdiss,elener,keyerr,keycnv)
c
c *** primary function:   Allow users to write their own interaction behavior.
c                         This logic is accessed with tb,inter with tbopt=user.
c *** secondary function: demonstrate the use of user-written interface laws
c                         in this routine:
c                         a. update history variables
c                         b. compute consistent tangent matrix
c
c *** Notice - This file contains ANSYS Confidential information ***
c
c
c         *** Copyright ANSYS.  All Rights Reserved.
c         *** ansys, inc.
c
c  input arguments:
c     variable (type,sze,intent)    description
c
c     elem     (int,sc,in)         - element number
c     intpt    (int,sc,in)         - element integration point number
c     ndim     (int,sc,in)         - number of dimensions of the problem
c                                    = 2 2D
c                                    = 3 3D
c     nintp    (int,sc,in)         - the total number of integration points of
c                                    an element to be used with this routine
c     nuval    (int)               - number of additional state variables per
c                                    integration point
c         note:  nuval x nintp = nstv(on nsvr command); cannot exceed 840!
c
c     intIn    (int,ar(*),in)      - integer variables passed in
c                                    intIn(1) = element number
c                                    intIn(2) = element integration point number
c                                    intIn(3) = material reference number
c                                    intIn(4) = element type ID number (absolute value)
c                                               > 0 for CONTA171-CONTA177
c                                               < 0 for CONTA178
c                                    intIn(5) = real constant ID number
c                                    intIn(6) = associated contact nodal number
c                                    intIn(7) = contact indicator
c                                               0: intersection is found
c                                               otherwise: no intersection
c                                    intIn(8) = target element number
c                                    intIn(9) = flag for forcing sliding 
c                                               frictional case
c                                               0: - not forcing
c                                               1: - forcing (Slip direction is 
c                                                    defined through CMROT command)
c                                    intIn(10) = 1 first pass through 
c                                                  (1st iteration)
c                                                  (useful for initializing state 
c                                                   variables to a non-zero value)
c                                              = 2 first pass through key of 
c                                                  a restart
c                                              = 3 first pass through key of 
c                                                  a rezoning
c                                    intIn(11) = current load step number
c                                    intIn(12) = current substep number
c                                    intIn(13) = current equilibrium iteration number
c                                    intIn(14) = flag for using unsymmetric matrices 
c                                                (nropt,unsym)
c                                                0: - symmetric
c                                                1: - unsymmetric
c                                    intIn(15) = Linear perturbation flag
c                                                0: - a general load step
c                                                1: - a linear perturbation step
c                                    intIn(16) = key to indicate output pass
c                                                0: not a output pass
c                                                1: output pass
c                                    intIn(17) = key for displacement & force 
c                                                convergence
c                                                1: converged
c                                                0: not converged
c                                    intIn(18) = key to indicate transient effects 
c                                                1 : transient is active
c                                                0 : transient is not active
c                                    intIn(19) = large deformation key [nlgeom cmd]
c                                                1 : on
c                                                0 : off
c                                    intIn(20) = analysis type (derived from antype)
c                                                0 : a static analysis
c                                                1 : a buckling analysis
c                                                2 : a modal analysis
c                                                3 : a harmonic analysis
c                                                4 : a transient analysis
c                                                7 : a substructure analysis
c                                                8 : a spectrum analysis
c     realIn   (dp,ar(*),in)       - real variables passed in
c                                    realIn(1) = contact element length
c                                    realIn(2) = contact element depth
c                                    realIn(3) = area associated with the contact 
c                                                detection point
c                                    realIn(4) = pinball radius
c                                    realIn(5) = unscaled normal penalty stiffness
c                                    realIn(6) = time (or frequency for a harmonic 
c                                                analysis) at the beginning of this 
c                                                load step
c                                    realIn(7) = time (or frequency for a harmonic 
c                                                analysis) at the end of this load step 
c                                    realIn(8) = current time value (or frequency value
c                                                for a harmonic analysis)
c                                    realIn(9) = time increment (or frequency increment 
c                                                for a harmonic analysis) over this
c                                                substep
c                                    realIn(10) = temperature offset from absolute 
c                                                 zero
c                                    realIn(11) = geometric penetration/gap 
c                                                 (current substep) 
c                                                 > 0 : gap
c                                                 < 0 : penetration
c                                    realIn(12) = time increment scaling factor to
c                                                 be used for structural transient 
c                                                 dynamics
c                                    realIn(13) = convection coefficient (SFE command)
c                                    realIn(14) = bulk temp (SFE command)
c     nkeyopt  (int,sc,in)         - number of key options
c     keyopt   (int,ar(nkeyopt),in)- array containing key options
c                                    keyopt(1) : Select degree of freedom
c                                    keyopt(2) : Contact algorithm
c                                    ... so on (see ANSYS documentation)
c     nrl      (int,sc,in)         - number of real constants
c     rlconst  (dp,ar(nrl),in)     - array containing real constants 
c                                    Elements CONTA171 to CONTA177
c                                    rlconst(1) : R1
c                                    rlconst(2) : R2
c                                    rlconst(3) : FKN
c                                    rlconst(4) : FTOLN
c                                    ... so on (see ANSYS documentation)
c                                    Element CONTA178
c                                    rlconst(1) : FKN
c                                    rlconst(2) : GAP
c                                    ... so on (see ANSYS documentation)
c     ncomp    (int,sc,in)         - number of stress/force component = 6
c     stress   (dp,ar(ncomp),inout)- stress components (current substep)
c                                    It is passed in as the stress at the beginning 
c                                    of the current substep. It is updated to be 
c                                    the stress at the end of this current substep
c                                    stress(1) = frictional stress in direction 1
c                                    stress(2) = frictional stress in direction 2 
c                                                (3D only)
c                                    stress(3) = contact normal pressure
c                                    > 0 : compression
c                                    < 0 : tension
c                                    the above contact traction must be defined in
c                                    a local coordinate system (see localr)
c                                    Lagrange multiplier contribution is added 
c                                    if keyopt(2)=3,4  
c                                    stress(4) = heat flux (per area) 
c                                                flowing into contact
c                                    stress(5) = heat flux (per area) 
c                                                flowing into target
c                                    < 0 heat flowing into a surface
c                                    > 0 heat flowing out of a surface
c                                    stress(6) = electrical current density 
c                                                (per area) flowing from contact
c                                                to target element
c                                    > 0 current flowing from contact to target
c                                    < 0 current flowing target to contact
c     strain0  (dp,ar(ncomp),in)   - strain components in the end of the previous 
c                                    substep
c                                    (see strain for each component definition)
c     strain   (dp,ar(ncomp),in)   - current strain components
c                                    strain(1) = slip increment in direction 1
c                                    strain(2) = slip increment in direction 2 
c                                                (3D only)
c                                    strain(3) = contact normal gap/penetration
c                                    < 0 : gap
c                                    > 0 : penetration
c                                    strain(4) = temperature at the contact point
c                                               (from TEMP DOF or temperature load)
c                                    strain(5) = temperature at the target point
c                                               (only from TEMP DOF)
c                                    strain(6) = voltage difference between contact
c                                                and target (only from VOLT DOF)
c                                    
c     kstat    (int,sc,inout)      - contact status (current substep)
c                                    It is passed in as the status at the 
c                                    beginning of the current substep. 
c                                    It is updated to be the status at the 
c                                    end of the current substep
c                                    3 : stick
c                                    2 : sliding
c                                    1 : open contact (near)
c                                    0 : open contact (far)  
c     coor     (dp,ar(6),in)       - Coordinates of the contact detection point
c                                    coor(1) current x
c                                    coor(2) current y
c                                    coor(3) current z
c                                    coor(4) initial x
c                                    coor(5) initial y
c                                    coor(6) initial z
c     localr   (dp,ar(3,3),in)     - the direction cosines of the local surface 
c                                    coordinate system at contact detection
c                                    localr(1,1),localr(1,2),localr(1,3) in 
c                                                          slip direction 1
c                                    localr(2,1),localr(2,2),localr(2,3) in
c                                                          slip direction 2
c                                    localr(3,1),localr(3,2),localr(3,3) in 
c                                                           normal direction
c                                    
c     npropu   (int,sc,in)         - number of user-defined interaction properties
c     uprop    (dp,ar(npropu),in)  - user-defined material properties
c
c     usvr     (dp,ar(nuval,nintp),inout)- additional state variables from
c                                     previous equilibrium iteration (saved
c                                     if the nsvr command is used)
c     kupdhis  (int,sc,in)         - key to indicate if history-dependent
c                                    variables (user defined) need to be 
c                                    updated after the substep has converged
c                                    1 : update (converged)
c                                    0 : do not update (not converged)
c
c  output arguments:
c     variable (type,sze,intent)   description
c
c     kstat    (int,sc,inout)      - updated contact status
c     stress   (dp,ar(ncomp),inout)- updated stress components
c                                    
c     dt   (dp,ar(ncomp,ncomp),out)- interface stiffness matrix:
c                                    dt(i,j) defines the partial derivative of 
c                                    the ith stress component at the current 
c                                    substep w.r.t. the jth component of the 
c                                    relative strain increment array.
c                                    If symmetric solver option used, ANSYS will 
c                                    symmetrize the matrix bu averaging the 
c                                    off-diagonal terms.
c                                    rows and columns of dt matrix are
c                                    associated to:
c                                    row 1 : frictional stress in direction 1 
c                                    row 2 : frictional stress in direction 2
c                                    row 3 : normal pressure 
c                                    row 4 : heat flux into contact
c                                    row 5 : heat flux into target 
c                                    row 6 : electrical current density flowing 
c                                            from contact to target element
c                                    col 1 : sliding in direction 1 
c                                    col 2 : sliding in direction 2
c                                    col 3 : normal gap 
c                                    col 4 : temperature at contact
c                                    col 5 : temperature at target
c                                    col 6 : voltage difference (from contact 
c                                            to target)
c                                    relevant components to be filled in are:
c                                    dt(1,1): d(tau1)/d(slip1)
c                                    dt(1,2): d(tau1)/d(slip2)
c                                    dt(1,3): d(tau1)/d(normal gap)
c                                    dt(1,4): d(tau1)/d(tempC)
c                                    dt(1,5): d(tau1)/d(tempT)
c                                    dt(1,6): d(tau1)/d(voltC)
c                                    dt(1,7): d(tau1)/d(voltT)
c                                    dt(2,1): d(tau2)/d(slip1)
c                                    dt(2,2): d(tau2)/d(slip2)
c                                    dt(2,3): d(tau2)/d(normal gap)
c                                    ...
c                                    dt(3,1): d(pres)/d(slip 1)
c                                    dt(3,2): d(pres)/d(slip 2)
c                                    dt(3,3): d(pres)/d(normal gap)
c                                    ...
c                                    dt(4,1): d(fluxC)/d(slip 1)
c                                    dt(4,2): d(fluxC)/d(slip 2)
c                                    dt(4,3): d(fluxC)/d(normal gap)
c                                    dt(4,4): d(fluxC)/d(tempC)
c                                    dt(4,5): d(fluxC)/d(tempT)
c                                    dt(4,6): d(fluxC)/d(dvolt)
c                                    ...
c                                    dt(5,4): d(fluxT)/d(tempC)
c                                    dt(5,5): d(fluxT)/d(tempT)
c                                    dt(5,6): d(fluxT)/d(dvolt)
c                                    ...
c                                    dt(6,4): d(eleC)/d(tempC)
c                                    dt(6,5): d(eleC)/d(tempT)
c                                    dt(6,6): d(eleC)/d(dvolt)
c     dtdp     (dp,ar(ncomp),out)  - partial derivative of the frictional stress
c                                    in direction 1,2 w.r.t. normal pressure 
c                                    used in Lagrange multiplier method 
c                                    (keyopt(2)=3,4). 
c     damp     (dp,ar(3,3),out)    - interface damping matrix (structure only)
c                                    it can be used only in Linear perturbation
c                                    modal analysis or transient analysis or
c                                    harmonic analysis in frequence domain.
c                                    damp(i,j) defines the partial derivative of 
c                                    the ith stress component at the current 
c                                    substep w.r.t. the jth component of the 
c                                    strain increment rate array.
c                                    rows and columns of dt matrix are
c                                    associated to:
c                                    row 1 : frictional stress in direction 1 
c                                    row 2 : frictional stress in direction 2
c                                    row 3 : normal pressure 
c                                    col 1 : sliding rate in direction 1 
c                                    col 2 : sliding rate in direction 2
c                                    col 3 : normal gap rate
c     kdamp    (int,sr,out)        - damping matrix index
c                                    0 : no damping matrix 
c                                    1 : taking damping matrix into account
c     usvr     (dp,ar(nuval,nintp),inout)- updated additional state variables
c                                    For example, mu value and absolute/relative
c                                    accumulated slip could be output as follows:
c                                    usvr(1,intpt) : mu
c                                    usvr(2,intpt) : abs. acc. slip in dir1
c                                    usvr(3,intpt) : abs. acc. slip in dir2
c                                    usvr(4,intpt) : acc. slip in dir1
c                                    usvr(5,intpt) : acc. slip in dir2
c                                    They are passed in as the values at the 
c                                    beginning of this substep. They are updated
c                                    to be the values at the end of this substep.
c                                    Use NSVR command to size usvr array and
c                                    set nuval to same value as number of
c                                    variables on NSVR commands
c                                    Use userou.F to save these values
c                                    on NMISC record for output purposes.
c                                    The number of user defined output items on 
c                                    NMISC should be equal or less than NSTV
c                                    on nsvr command). It cannot exceed 120.
c
c     mu       (dp,sc,inout)       - The current frictional coefficient
c     fdiss    (dp,sc,out)         - incremental frictional dissipation
c                                    per unit area
c     elener   (dp,sc,out)         - incremental elastic stored energy 
c                                    per unit area
c
c     keyerr (int,sc,out)          - key to indicate if there is any element 
c                                    formulation error, like 
c                                    contact status changes abruptly,
c                                    too much penetration.
c                                    The error could be caused by too
c                                    large incremental step, illegal model.
c                                    = 0 no error (preset value before calling)
c                                    = 1 some error happens. ANSYS will
c                                    decide to stop the analysis or cutback
c                                    the substep (bi-section) based on other
c                                    user input and information at higher
c                                    level.
c     keycnv (int,sc,inout)        - key to flag if this element satisfies
c                                    the user defined element convergence
c                                    criterion. 
c                                    = 1, yes, the criterion is satisfied
c                                      or don't have any criterion at all
c                                      it is preset value before calling
c                                    = 0, no, the element doesn't satisfy
c                                      element convergence criterion. If
c                                      this is the case, the iteration will
c                                      not converge even when both force
c                                      and displacement converge 
c
c  internal variables:
c     variable (type,sze)      description
c
#include "impcom.inc"

c
c  external subroutines and functions
      external vzero,erhandler
c
c     ------------------------ arguments -------------------------------
c
c  integer variables
      integer elem,intpt,ndim,nuval,nintp,nkeyopt,keyopt(nkeyopt),nrl,
     x ncomp,nintIn,intIn(nintIn),nrealIn,npropu,kstat,kdamp,kupdhis,
     x keyerr,keycnv
c
c  double precision variables
      double precision coor(6),localr(3,3),rlconst(nrl),
     x realIn(nrealIn),usvr(nuval,nintp),uprop(npropu),mu,
     x stress0(6),stress(ncomp),strain0(ncomp),strain(ncomp),
     x dt(ncomp,ncomp),dtdp(ndim-1),damp(3,3),fdiss,elener

c     -------------------------- local ---------------------------------

c  integer variables
c      integer 

c  double precision variables
      double precision err(2)

      double precision zero
      data zero/0.d00/
c
c   this routine demonstrates the computation of friction stress and
c   tangent matrix using Coulomb friction model.
c   see section 14.174 of the ansys theoretical manual for more detailed
c   documentation.
c   in large strain 3D analysis, slip increment and frictional stress 
c   are rotated with the element rotation.
c
c
c
c --- check that author-defined usvr size is within bounds
c
      elem = intIn(1)
      intpt = intIn(2)
      if (nuval*nintp.gt.840) then
         err(1) = elem
         call erhandler('userinter',5010,4,
     x     'Maximum storage allowed by USVR has been exceeded
     x     for contact element %I.'
     x     ,err(1),' ')
      end if
c
c --- initialization
c
      kdamp = 0
      keyerr = 0
      keycnv = 1 
      damp(1,1) = zero
      call vzero (dt(1,1),ncomp*ncomp)
      call vzero (dtdp(1),ndim-1)
      fdiss = zero
      elener = zero
c
c
c --- retrieve the user-specified interface data
c --- (user can also specify friction data here, e.g., mu as function
c ---  of variables like normal pressure, slip, slip rate, temp, etc.)
c
c --- this example uses 1 input quanitities (tb,inter,1,1,1,USER):
      mu = uprop(1)
c --- user variable to output mu--uncomment this line when using NSVR command
c      usvr(1,intpt) = mu
c
      if (mu .lt.zero) then
         err(1) = elem
         call erhandler('userinter',5020,4,
     x    'Friction coefficient used for contact element %I 
     x     should a be positive value.'
     x     ,err(1),' ')
         keyerr = 1
         go to 999
      end if
c
c
c ---    output user variable--uncomment the following lines when using NSVR command
c
c        if (kupdhis.eq.1) then
c        --- store friction coefficient
c           usvr(1,intpt) = mu
c        ---update absolute accumulated slip 
c           usvr(2,intpt) = usvr(2,intpt) + abs(strain(1))
c           usvr(3,intpt) = usvr(3,intpt) + abs(strain(2))
c        ---update accumulated slip
c           usvr(4,intpt) = usvr(4,intpt) + strain(1)
c           usvr(5,intpt) = usvr(5,intpt) + strain(2)
c        end if
c
 900  continue
c
 999  continue
c     
      return
      end
