*deck,userpl       USERDISTRIB  parallel                                dc
      subroutine userpl (elem,intpt,mat,ncomp,kfirst,kfsteq,e,nu,dens,
     x prop,d,ktform,timval,timinc,tem,dtem,toffst,flu,dflu,epel,eppl,
     x statev,usvr,epeq,plwork,sigepl,sigrat,depeq,dt)
c
c *** primary function:   allow users to write their own plasticity laws.
c                           this logic is accessed with tb,user.
c                           the below demonstration logic is the same as using
c                           tb,bkin, without adaptive descent (nropt,,,off).
c                           Other plasticity rules may require internal 
c                           iterations and/or the more general definition of
c                           plasticity theory, discussed in the Theory
c                           Manual.
c *** secondary function:    demonstrate the use of user-written plasticity laws
c                 in this routine:
c                    a.    update the nonlinear strain history
c                    b.    compute the material tangent matrix if requested
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 (label)
c     intpt    (int,sc,in)         - element integration point number
c     mat      (int,sc,in)         - material reference number
c     ncomp    (int,sc,in)         - no. of stress/strain components (1,4 or 6)
c                                    1 - x
c                                    4 - x,y,z,xy
c                                    6 - x,y,z,xy,yz,xz
c     kfirst   (int,sc,in)         - 1 if first time through, 0 otherwise
c                                    (useful for initializing state variables
c                                    to a non-zero value)
c     kfsteq   (int,sc,in)         - 1 if first equilibrium iteration of a
c                                    substep, 0 otherwise
c
c     e        (dp,sc,in)          - average elastic modulus
c     nu       (dp,sc,in)          - average poisson ratio
c     dens     (dp,sc,in)          - current material density (mass/volume)
c     prop                         - linear material property array
c              (dp,ar(9),in)         (ex,ey,ez, gxy,gyz,gxz, nuxy,nuyz,nuxz)
c              (dp,ar(1),in)         if ncomp=1 (ex)
c     d        (dp,ar(ncomp,ncomp),in)- elastic stress-strain matrix
c     ktform   (int,sc,in)         - request key for tangent matrix formation
c                                    (=1, form tangent  .ne.1, do not form)
c
c     timval   (dp,sc,in)          - current time value
c     timinc   (dp,sc,in)          - time increment over this substep
c
c     tem      (dp,sc,in)          - temperature at the end of this substep
c     dtem     (dp,sc,in)          - temperature increment over this substep
c     toffst   (dp,sc,in)          - temperature offset from absolute zero
c     flu      (dp,sc,in)          - fluence at the end of this substep
c     dflu     (dp,sc,in)          - fluence increment over this substep
c
c     epel     (dp,ar(ncomp),inout)- modified total strain (trial strain)
c                                    epel = eptot - eppl - eptherm - ...
c                                    if a large strain analysis, epel is
c                                    rotation neutralized and is the hencky
c                                    (i.e. log) strain
c     eppl     (dp,ar(ncomp),inout)- plastic strain from previous substep
c
c     statev   (dp,ar(ncomp,6),inout)- state variables from previous substep
c     usvr     (dp,ar(nuval,nintp),inout)- additional state variables from
c                                    previous equilibrium iteration (saved
c                                    if the nsvr command is used)
c
c     epeq     (dp,sc,inout)       - effective plastic strain from prev substep
c     plwork   (dp,sc,inout)       - accumulated plastic work from prev substep
c
c  output arguments:
c     variable (type,sze,intent)    description
c
c     epel     (dp,ar(ncomp),inout)- elastic strain
c     eppl     (dp,ar(ncomp),inout)- updated plastic strain
c
c     statev   (dp,ar(ncomp,6),inout)- updated state variables
c     usvr     (dp,ar(nuval,nintp),inout)- updated additional state variables
c
c     epeq     (dp,sc,inout)       - updated effective plastic strain
c     plwork   (dp,sc,inout)       - updated accumulated plastic work
c
c     sigepl   (dp,sc,out)         - stress value on stress-strain curve at epeq
c     sigrat   (dp,sc,out)         - ratio of trial stress to yield stress
c     depeq    (dp,sc,out)         - increment in plastic strain (equivalent)
c                                    (used for auto time stepping - time step
c                                    is reduced if it exceeds .05)
c
c     dt       (dp,ar(ncomp,ncomp),out)- material tangent modulus
c
c  fortran parameters (to be defined by the user):
c     variable (type)              description
c     numinp   (int)               - number of data items in the user-defined
c                                    data table (tbdat commands)
c     nuval    (int)               - number of additional state variables per
c                                    integration point
c     nintp    (int)               - maximum number of integration points of
c                                    an element to be used with this routine
c                                    (14 is the maximum)
c         note:  nuval x nintp = nstv(on nsvr command); cannot exceed 840!
c
c  internal variables:
c     variable (type,sze)      description
c     b        (dp,ar(6,6))    - 2nd derivative of the yield function
c     c        (dp,ar(6,12))   - part of deff
c     con      (dp,sc)         - temporary variable
c     deppl    (dp,ar(6))      - plastic strain increment
c     dfds     (dp,ar(6))      - derivative of the yield function (normal)
c     dlamb    (dp,sc)         - plastic multiplier
c     ep       (dp,ar(6))      - shifted strain
c     epshfo   (dp,ar(6))      - initial shift strain (center of the yield surf)
c     epshft   (dp,ar(6))      - shift strain (center of the yield surface)
c     et       (dp,sc)         - tangent modulus (stress/total strain)
c     h        (dp,sc)         - plastic tangent modulus (stress/plastic strain)
c     n2       (int,sc)        - ncomp squared, ncomp*ncomp
c     seqtr    (dp,sc)         - equivalent (von mises) trial stress
c     sigtr    (dp,ar(6))      - trial stress
c     sigy     (dp,sc)         - yield stress
c     vect     (dp,ar(6))      - temporary vector
c
#include "impcom.inc"


c  user-defined fortran parameters
c     --- size of usvr data
      integer nuval,nintp
      parameter (nuval=1,nintp=1)
c     --- number of data items in the user-defined data table (tbdat commands)
      integer numinp
      parameter (numinp=2)
      double precision eps
      parameter (eps=1.0d-8)
c
c  external subroutines and functions
      external tbuser, vmove,vamb,maxv,maxv1,vmult,vamb1,vapb1,
     x vapcb1,vmult1,vzero,vapb,maat,matsym,maxb,erhandler

      external         vdot
      double precision vdot
      external symeqn
      integer  symeqn
c
c  integer variables
      integer elem,intpt,mat,ncomp,kfirst,kfsteq,ktform,
     x n2,i
c
c  double precision variables
      double precision e,nu,dens,prop(*),d(ncomp,ncomp),timval,timinc,
     x tem,dtem,toffst,flu,dflu,epel(ncomp),eppl(ncomp),statev(ncomp,6),
     x usvr(nuval,nintp),epeq,plwork,sigepl,sigrat,depeq,
     x dt(ncomp,ncomp), proptb(numinp),
     x b(6,6),c(6,12),con,deppl(6),dfds(6),dlamb,ep(6),
     x epshfo(6),epshft(6),et,h,seqtr,sigtr(6),sigy,vect(6)
c
c   this routine demonstrates the computation of plastic strains and tangent
c   matrix using the von mises yield criteria with bilinear kinematic hardening.
c   see section 3.1 of the ansys theoretical manual for more detailed
c   documentation.
c   in a large strain analysis, all strains are the hencky (i.e. log) strains.
c   all quantities are rotation-neutralized and are in the rotated element
c   coordinate system (not global).
c
c
c  Conditional tracking for qa activated by stability
#ifdef TRACK_QA
      external TrackPoint
      call TrackPoint('userpl')
#endif
c
c
c --- initial checks
c     --- author should remove the warning below when making any changes
      if (intpt.eq.1 .and. kfirst.eq.1)
c
     X call erhandler('userpl',5000,2,
     x  'ANSYS,INC.-supplied version of coding for USERPL has been 
     x  used.'
     x  ,0.0d0,' ')
c

c     ---- author should remove the following line.  it is only there
c     ---- for the analyzer.
      con = dflu+dtem+flu+timval+timinc+usvr(1,1)+dens+prop(1)+tem+
     x     toffst + dble(elem+kfsteq)
c     --- check that author-defined usvr size is within bounds
c
      if (nuval*nintp.gt.840)
     Xcall erhandler('userpl',5010,4,
     x 'Maximum storage allowed by USVR has been exceeded.'
     x ,0.0d0,' ')
c

c
c     --- the below demonstration logic is the same as using tb,bkin,
c     --- without adaptive descent (nropt,,,off)
c
c --- initialize the tangent matrix for no plasticity or no form cases
      n2 = ncomp*ncomp
      call vmove (d(1,1),dt(1,1), n2)
      depeq = 0.d0
c
c        ***** retrieve the user-specified material data *****
c
c --- this example uses 2 input quanitities (tb,user,mat,,2):
c         c1 - yield stresss   c2 - tangent modulus
      call tbuser (mat,numinp,proptb(1))
      sigy = proptb(1)
      et = proptb(2)
c     --- check for no data specified
      if (sigy.le.0.0d0) go to 999
c --- convert tangent to plastic modulus format
      h = e*et/(e - et)
c
c --- save initial values
c     --- the 1st state variable column is for the shift strain (the center
c         of the yield surface)
      call vmove (statev(1,6),epshft(1),ncomp)
c     --- to verify all the columns of array statev(ncomp,6)
      call vmove (epshft(1),epshfo(1),ncomp)
c
c         ***** calculate the trial stress *****
c
c --- calculate the trial stress after subtracting off the yield surface shift
      call vamb (epel(1),epshft(1),ep(1),ncomp)
      call maxv (d(1,1),ep(1),sigtr(1), ncomp,ncomp)
c     --- compute the equivalent stress
      if (ncomp.eq.1) then
         seqtr = abs(sigtr(1))
      else
         seqtr = (sigtr(1)-sigtr(2))**2 + (sigtr(2)-sigtr(3))**2 +
     x           (sigtr(3)-sigtr(1))**2 + 6.0d0*sigtr(4)**2
         if (ncomp.eq.6) seqtr = seqtr +
     x     6.0d0*(sigtr(5)**2 + sigtr(6)**2)
         seqtr = sqrt (0.5d0*seqtr)
      endif
c     --- calculate the equivalent stress and stress ratio (in case no yielding)
      sigepl = sigy + h*epeq
      sigrat = seqtr/sigy
c
c             ***** check for yielding ****
c
c     --- check for yielding (slightly above yield) - if seqtr.ge.(1+eps)*sigy
      con = seqtr/sigy - 1.0d0
      if (con.lt.eps) then
c        --- no yielding - check if near for tangent - if seqtr.gt.(1-eps)*sigy
         if (con.gt.-eps) then
c           --- compute the yield surface derivatives
            if (ncomp.eq.1) then
               dfds(1) = 1.0d0
               if (sigtr(1).lt.0.0d0) dfds(1) = -1.0d0
            else
               con = (sigtr(1)-sigtr(2))**2 + (sigtr(2)-sigtr(3))**2 +
     X               (sigtr(3)-sigtr(1))**2 + 6.0d0*sigtr(4)**2
               dfds(1) = sigtr(1) - 0.5d0*(sigtr(2) + sigtr(3))
               dfds(2) = sigtr(2) - 0.5d0*(sigtr(1) + sigtr(3))
               dfds(3) = sigtr(3) - 0.5d0*(sigtr(1) + sigtr(2))
               dfds(4) = 3.0d0*sigtr(4)
               if (ncomp.eq.6) then
                  con = con + 6.0d0*(sigtr(5)**2 + sigtr(6)**2)
                  dfds(5) = 3.0d0*sigtr(5)
                  dfds(6) = 3.0d0*sigtr(6)
               endif
               con = sqrt (0.5d0*con)
               call vmult1 (dfds(1),ncomp,1.0d0/con)
            endif
            dlamb = 0.0d0
            go to 500
         endif
         go to 999
      endif
c
c
c      ***** calculate the plastic strain increment *****
c
c --- get the derivative of the yield function
      if (ncomp.eq.1) then
         dfds(1) = 1.0d0
         if (sigtr(1).lt.0.0d0) dfds(1) = -1.0d0
      else
         con = (sigtr(1)-sigtr(2))**2 + (sigtr(2)-sigtr(3))**2 +
     x         (sigtr(3)-sigtr(1))**2 + 6.0d0*sigtr(4)**2
         dfds(1) = sigtr(1) - 0.5d0*(sigtr(2) + sigtr(3))
         dfds(2) = sigtr(2) - 0.5d0*(sigtr(1) + sigtr(3))
         dfds(3) = sigtr(3) - 0.5d0*(sigtr(1) + sigtr(2))
         dfds(4) = 3.0d0*sigtr(4)
         if (ncomp.eq.6) then
            con = con + 6.0d0*(sigtr(5)**2 + sigtr(6)**2)
            dfds(5) = 3.0d0*sigtr(5)
            dfds(6) = 3.0d0*sigtr(6)
         endif
         con = sqrt (0.5d0*con)
         call vmult1 (dfds(1),ncomp,1.0d0/con)
      endif
c
c --- compute the plastic constant lambda
      call maxv (d(1,1),dfds(1),vect(1), ncomp,ncomp)
      con = h + vdot(vect(1),dfds(1),ncomp)
      dlamb = (seqtr - sigy)/con
c
c --- calculate the strain increment
      call vmult (dfds(1),deppl(1), ncomp,dlamb)
c
c      ***** update the strains and other quantities *****
c
c --- update strains
      call vamb1 (epel(1),deppl(1),ncomp)
      call vapb1 (eppl(1),deppl(1),ncomp)
      con = 2.0d0*h*(1.0d0 + nu)/(3.0d0*e)
      call vapcb1 (epshft(1),deppl(1), ncomp,con)
      call vmove (epshft(1),statev(1,6),ncomp)
c
c --- update the accumulated plastic strain
      depeq = dlamb
      epeq = epeq + depeq
c
c --- update the plastic work (using the average stress x deppl)
      call vapb (epshft(1),epshfo(1),vect(1),ncomp)
      call vmult1 (vect(1),ncomp,0.5d0)
      call maxv1 (d(1,1),vect(1), ncomp,ncomp)
      call vapcb1 (vect(1),sigtr(1), ncomp,sigy/seqtr)
      plwork = plwork + vdot(vect(1),deppl(1),ncomp)
c
c --- recalculate the equivalent stress and stress ratio
      sigepl = sigy + h*epeq
      sigrat = seqtr/sigy
c
c     ***** form the tangent matrix if requested *****
c
 500  if (ktform.eq.1) then
c        --- do not form with exactly h=0
         if (h.lt.1.0d-9*e) h = 1.0d-9*e
         if (ncomp.eq.1) then
            dt(1,1) = e*h/(e + h)
         else
c
c       --- form the effective material modulus
c           --- check if significant plasticity has occurred
            if (dlamb.gt.1.0d-6) then
c              --- deff = (i + dlamb*d*b)-1*d;  b = (m - n*nt)/sigy
               con = dlamb/sigy
               call vzero (b(1,1),36)
c              --- set up m first
               b(1,1) = 1.0d0
               b(2,2) = 1.0d0
               b(3,3) = 1.0d0
               b(4,4) = 3.0d0
               if (ncomp.eq.6) then
                  b(5,5) = 3.0d0
                  b(6,6) = 3.0d0
               endif
               b(2,1) = -0.5d0
               b(3,2) = -0.5d0
               b(3,1) = -0.5d0
               call vmult1 (b(1,1),36,con)
c              --- add in n*nt term
               call maat (dfds(1),b(1,1), 6,ncomp, -con)
               call matsym (b(1,1),6,ncomp)
               call maxb (d(1,1),b(1,1),c(1,1), ncomp,6,6, ncomp,ncomp,
     x           ncomp)
               c(1,1) = c(1,1) + 1.0d0
               c(2,2) = c(2,2) + 1.0d0
               c(3,3) = c(3,3) + 1.0d0
               c(4,4) = c(4,4) + 1.0d0
               if (ncomp.eq.6) then
                  c(5,5) = c(5,5) + 1.0d0
                  c(6,6) = c(6,6) + 1.0d0
               endif
               i = symeqn (c(1,1),6,ncomp,-ncomp,0)
               if (i .gt. 0) then
c                 --- When nature of error is better understood,
c                 --- message should be changed to reflect that.  pck 3/04
                  call erhandler ('userpl',5030,-3,
     x             'An unanticipated divide by zero has been encountered
     x             in symeqn.F (called by userpl.F) causing the program
     x             to terminate.',0.0d0,' ')
               endif
               call maxb (c(1,ncomp+1),d(1,1),dt(1,1), 6,ncomp,ncomp,
     x           ncomp,ncomp,ncomp)
            else
c              --- insignificant plasticity - use the stress-strain matrix
               call vmove (d(1,1),dt(1,1), ncomp*ncomp)
            endif
c
c       --- calculate the consistent tangent modulus
            call maxv (dt(1,1),dfds(1),vect(1), ncomp,ncomp)
            con = 1.0d0/(h + vdot(dfds(1),vect(1),ncomp))
            call maat (vect(1),dt(1,1), ncomp,ncomp, -con)
            call matsym (dt(1,1),ncomp,ncomp)
         endif
      endif
c
 999  return
      end
