*deck,usrogd       USERDISTRIB  parallel                                ashraf
      subroutine usrogd (mat,ndf,wini,wi,wij,wm,wmn,prs,pi,pij,beta,
     x                   gamma,xs,idx,i1,i2,xiii,xnu,bm)
c
c *** primary function:
c
c        this is a user subroutine to compute the derivatives of the
c      strain energy density 'W' with respect to the principal stretch
c      ratios of the right cauchy-green tensor, e.g., ogden-type
c      material law. the user can modify this subroutine in order to
c      incorporate their own ogden-tpye material law.
c      accessed with keyopt(7) = 1 and keyopt(1) = 1
c
c *** Notice - This file contains ANSYS Confidential information ***
c
c         *** Copyright ANSYS.  All Rights Reserved.
c         *** ansys, inc.
c
c *** comments:
c
c        by definition, a material is said to be hyperelastic, if there
c     exists a strain energy density function, which is a scalar
c     function of one of the strain or deformation tensors, whose
c     derivative with respect to a strain component determines the
c     corresponding stress component.
c
c        these strain energy density functions are functions of
c     principal stretch ratios of strain tensors and a set of constants
c     determined in the laboratory tests. the user can specify their own
c     form of the strain energy density functions in terms of principal
c     stretch ratios of the cauchy-green tensors and a number of
c     laboratory-determined constants.
c
c        in this subroutine the user is provided with (1) the invariants
c     'XIII' of the cauchy-green strain tensor, (2) a flag 'NDF'
c     identifying the dimensionality of the problem, (3) poisson'S
c     ratio 'XNU'.
c
c        the subroutine first computes the number of constants specified.
c     the material constants that were specified by the user
c     through the 'TB' table are now put into local variables, e.g.,
c     'ALPHA(.)' and 'MU(.)' in the ogden example. bulk modulus 'bm'
c     corresponding to small strain theory is computed from mu(1) and
c     mu(2) and xnu. the invariants are put into a local array txiii(.).
c     principal stretch ratio (squares) 'xc(.)' are then computed using
c     the subroutine strr3d. the square roots of 'xc(.)' are then put
c     into the array 'xs(.)'. the user then defines the expression
c     for the strain energy density function. the term of 'W' involving
c     'BM' must remain intact; the user will have their own expression
c     for the first part of 'W', e.g., terms involving constants
c     'ALPHA(.)' and 'MU(.)' of the example of the ogden material. the
c     expression for the  distortion-based pressure 'PRS' remains the
c     same. 'PRS' along with 'BM' control the degree of
c     incompressibility of the material.
c
c        the user will have to find the expressions for the derivatives
c     of 'W' (derivatives of 'PRS' --pi and pij-- remaining the same),
c     i.e., 'WI' and 'WIJ'. the computations of beta(.) and gamma(..)
c     will also remain the same. the subroutine returns the values of
c     'WINI', 'WI', 'WIJ', 'WM', 'WMN', 'BETA', 'GAMMA', 'PRS', 'PI',
c     'PIJ', 'BM', 'IDX', 'XS'.
c
c ----------------------------------------------------------------------
c
c  input arguments:
c     xiii     (dp,ar(3),in)     - invariants of the cauchy-green tensor
c     mat      (int,sc,in)       - material reference number
c     xnu      (dp,sc,in)        - input possions ratio
c     ndf      (int,sc,in)       - dimensionality of the problem
c                                  (2 for plane strain)
c                                  (3 for 3d/axisymmetric)
c  output arguments:
c     idx      (int,sc,out)      - singularity flag, where:
c                                           1 ==> roots not equal
c                                           2 ==> two roots equal
c                                           3 ==> all roots equal
c     i1       (int,sc,out)        singularity flag for idx = 2
c     i2       (int,sc,out)        singularity flag for idx = 2
c     xs       (dp,ar(6),out)    - lambda: roots of cauchy-green tensor
c     wi       (dp,ar(3),out)    - first derivatives of strain energy density
c                                  with respect to principal stretch ratios
c     wij      (dp,ar(3,3),out)  - second derivatives of strain energy density
c                                  with respect to principal stretch ratios
c     wini     (dp,sc,out)       - strain energy density in terms of stretch rat
c     wm       (dp,ar(3),out)    - first derivative of hydrostatic part of strai
c                                  energy density wrt third invariant
c     wmn      (dp,ag(3,3),out)  - second derivative of hydrostatic part of stra
c                                  energy density wrt third invariant
c     beta     (dp,ar(3),out)    - terms involving wi(i)
c     gammma   (dp,ar(3,3),out)  - terms involving wij(i,j)
c
c     pi       (dp,ar(3),out)    - first derivatives of pressure
c                                  with respect to invariants
c     pij      (dp,ar(3,3),out)  - second derivatives of pressure
c                                  with respect to invariants
c     prs      (dp,sc,out)       - pressure
c     bm       (dp,sc,out)       - bulk modulus
c
c ----------------------------------------------------------------------
c
c  definitions :
c     --------------------------------------------
c     wi(1) = d(w)/d(xs(1))
c     wi(2) = d(w)/d(xs(2))
c     wi(3) = d(w)/d(xs(3))
c
c     wij(1,1) = d**2(w)/d(xs(1))*d(xs(1))
c     wij(1,2) = d**2(w)/d(xs(1))*d(xs(2))
c     wij(1,3) = d**2(w)/d(xs(1))*d(xs(3))
c
c     wij(2,2) = d**2(w)/d(xs(2))*d(xs(2))
c     wij(2,3) = d**2(w)/d(xs(2))*d(xs(3))
c     wij(3,3) = d**2(w)/d(xs(3))*d(xs(3))
c     --------------------------------------------
c     pi(1) = d(prs)/d(xiii(1))
c     pi(2) = d(prs)/d(xiii(2))
c     pi(3) = d(prs)/d(xiii(3))
c
c     pij(1,1) = d**2(prs)/d(xiii(1))*d(xiii(1))
c     pij(1,2) = d**2(prs)/d(xiii(1))*d(xiii(2))
c     pij(1,3) = d**2(prs)/d(xiii(1))*d(xiii(3))
c
c     pij(2,2) = d**2(prs)/d(xiii(2))*d(xiii(2))
c     pij(2,3) = d**2(prs)/d(xiii(2))*d(xiii(3))
c     pij(3,3) = d**2(prs)/d(xiii(3))*d(xiii(3))
c     --------------------------------------------
c
#include "impcom.inc"


       external erhandler

c
c ----------------------------------------------------------------------
c  define the size of the user defined data (this may be increased or decreased)
      integer maxdat
      parameter (maxdat=12)
      double precision uprop(maxdat)
c ----------------------------------------------------------------------
c
c     --- function
      external lastv,tbpget,vdot
      double precision vdot
      integer  lastv,tbpget
      external  strr3d,vzero,vmove,erinfo
c
c     --- argument
      integer mat,ndf,idx,i1,i2
c     --- local
      integer i,j,ncof,ncof1,ncof2
c
c     --- argument
      double precision
     x xnu,xiii(3),wi(3),wij(3,3),wini,wm(3),wmn(3,3),pi(3),pij(3,3),
     x prs,bm,beta(3),gamma(3,3),xs(6)
c     --- local
      double precision
     x xc(6),txi,txii,txiii,vec1(6),vec2(6),vec3(6),vec4(6),
     x alpha(6),mu(6),wxxx,wi1,wi2,wi3,wij11,wij12,wij22,wij13,wij23,
     x wij33
c
#include "kronec.inc"
c
c     --- customer should remove the below warning when making any changes
c
      call erhandler('usrogd',5000,2,
     x 'ANSYS,INC.-supplied version of coding for USROGD has been used.'
     x ,0.0d0,' ')
c

c
c -----------------------------------------------------
c     --- here is the example of ogden material ---
c -----------------------------------------------------
c
c     --- retrieve user input constants ***
c     --- tbpget retrieves the data from 1 to maxdat and puts it in uprop
c     --- 13 refers to the user input table (tb,user) for material mat
      i = tbpget (mat,13,1,maxdat,uprop(1))
c     --- i is the number of data values obtained
      if (i.eq.0) then
         call erinfo (18,11)
c
         call erhandler('usrogd',5010,4,
     x    ' No constants (TB,USER) were input for the user hyperelastic 
     x    routine USROGD.'
     x    ,0.d0,' ')
c

      endif
c
c --- separate ogden material constants
      call vmove (uprop(1), mu(1), 6)
      call vmove (uprop(7), alpha(1), 6)
c
c     --- find out how many ogden constants are defined
      ncof1 = lastv (alpha(1),6) - 1
      ncof2 = lastv (mu(1),6) - 1
      if (ncof1 .ge. ncof2) then
         ncof = ncof1
      else
         ncof = ncof2
      endif
c
c     --- compute bulk modulus
      bm = 0.5d0*vdot(mu(1),alpha(1), 6)
      bm = bm*(2.0d0*(1.0d0 + xnu))
      bm = bm/(3.0d0*(1.0d0 - 2.0d0*xnu))
c
c     --- compute principal stretch ratios for 3-d
      txi = xiii(1)
      txii = xiii(2)
      txiii = xiii(3)
      call strr3d (xc,txi,txii,txiii,idx)
c
c     --- find out which two xs's are equal for idx = 2 and set index
      i1 = 0
      i2 = 0
      if (idx .eq. 2) then
         if (xc(1) .eq. xc(2)) then
            i1 = 3
            i2 = 1
         elseif (xc(2) .eq. xc(3)) then
            i1 = 1
            i2 = 3
         elseif (xc(1) .eq. xc(3)) then
            i1 = 2
            i2 = 1
         endif
      endif
c
      do 10 i = 1,6
         xs(i) = sqrt (xc(i))
   10 continue
c
c     --- compute  s.e.d. and derivatives of s.e.d. wrt xs(.) for singular
c     --- and non-singular cases for 3-d
c     -------------------------------------------------------------
c     --- initialize energy and derivatives
      wini = 0.0d0
      call vzero (wi(1), 3)
      call vzero (wij(1,1), 9)
c
c     --- loop for 3 cases: singular and non-singular
c
c     --- roots of cgt(..) are distinct: case(1)
      if (idx .eq. 1) then
c
c        --- define some facilitator variables
         do 20 i = 1,ncof
            vec2(i) = alpha(i)/3.0d0
            vec3(i) = vec2(i) + 1
            vec4(i) = 2.0d0*vec2(i) - 1.0d0
            vec1(i) = vec2(i) * (xs(1)**(-vec2(i))) *
     x         (xs(2)**(-vec2(i))) * (xs(3)**(-vec2(i)))
   20    continue
c
c        --- the strain energy density function
         do 30 i = 1,ncof
            wxxx = (xs(1)**(2.0d0*vec2(i)))
     x          * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(-vec2(i))) + (xs(1)**(-vec2(i))) *
     x          (xs(2)**(2.0d0*vec2(i))) * (xs(3)**(-vec2(i)))
     x          + (xs(1)**(-vec2(i))) * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(2.0d0*vec2(i))) - 3.0d0
            wxxx = wxxx*mu(i)/alpha(i)
            wini = wini + wxxx
   30    continue
            wini = wini + 0.5d0*bm*((sqrt (xiii(3)) - 1.0d0)**2)
c
c        --- compute the first partial derivative
         do 40 i = 1,ncof
            wi1 = vec1(i) * (2.0d0*(xs(1)**(alpha(i)-1.0d0)) -
     x          ((xs(1)**(-1.0d0)) * (xs(2)**alpha(i)))
     x          - (xs(1)**(-1.0d0) * (xs(3)**alpha(i))))
            wi1 = wi1*mu(i)/alpha(i)
            wi(1) = wi(1) + wi1
c
            wi2 = vec1(i) * ((-xs(2)**(-1.0d0)) * (xs(1)**
     x          alpha(i)) + (2.0d0*(xs(2)**(alpha(i)-1.0d0))) -
     x          ((xs(2)**(-1.0d0)) * (xs(3)**alpha(i))))
            wi2 = wi2*mu(i)/alpha(i)
            wi(2) = wi(2) + wi2
c
            wi3 = vec1(i) * (((-xs(3)**(-1.0d0)) * (xs(1)**
     x         alpha(i))) - ((xs(3)**(-1.0d0))
     x         *(xs(2)**alpha(i)))
     x         + (2.0d0*(xs(3)**(alpha(i)-1.0d0))))
            wi3 = wi3*mu(i)/alpha(i)
            wi(3) = wi(3) + wi3
   40    continue
c
c        --- compute the second order partial derivative
         do 50 i = 1,ncof
            wij11 = vec1(i) * (2.0d0*(vec4(i)*(xs(1)**(alpha(i)
     x         -2.0d0))) + (vec3(i)*(xs(1)**(-2.0d0))
     x          *(xs(2)**alpha(i))) + (vec3(i)*(xs(1)**(-2.0d0))
     x          *(xs(3)**alpha(i))))
            wij11 = wij11*mu(i)/alpha(i)
            wij(1,1) = wij(1,1) + wij11
c
            wij12 = vec1(i) * ((-2.0d0*vec2(i)*(xs(1)**(alpha(i)
     x         -1.0d0)) * (xs(2)**(-1.0d0))) -
     x         (2.0d0*vec2(i)*(xs(2)**(alpha(i)-1.0d0)) *
     x         (xs(1)**(-1.0d0)))+(vec2(i)*(xs(1)**(-1.0d0))*
     x         (xs(2)**(-1.0d0))*(xs(3)**alpha(i))))
            wij12 = wij12*mu(i)/alpha(i)
            wij(1,2) = wij(1,2) + wij12
c
            wij13 = vec1(i) * ((-2.0d0*vec2(i)*(xs(1)**(alpha(i)
     x         -1.0d0)) * (xs(3)**(-1.0d0))) +
     x         (vec2(i)*(xs(1)**(-1.0d0))*(xs(2)**alpha(i))*
     x         (xs(3)**(-1.0d0)))-(2.0d0*vec2(i)*
     x         (xs(1)**(-1.0d0))*(xs(3)**(alpha(i)-1.0d0))))
            wij13 = wij13*mu(i)/alpha(i)
            wij(1,3) = wij(1,3) + wij13
c
            wij22 = vec1(i) * ((vec3(i)*(xs(2)**(-2.0d0)) *
     x         (xs(1)**alpha(i)))+(2.0d0*vec4(i) *
     x         (xs(2)**(alpha(i)-2.0d0)))+(vec3(i)
     x         *(xs(2)**(-2.0d0)) * (xs(3)**alpha(i))))
            wij22 = wij22*mu(i)/alpha(i)
            wij(2,2) = wij(2,2) + wij22
c
            wij23 = vec1(i)*((vec2(i)*(xs(2)**(-1.0d0))
     x         *(xs(1)**alpha(i))*(xs(3)**(-1.0d0)))
     x         - (2.0d0*vec2(i)*(xs(2)**(alpha(i)-1.0d0))
     x         * (xs(3)**(-1.0d0))) - (2.0d0*vec2(i)
     x         *(xs(2)**(-1.0d0))*(xs(3)**(alpha(i)-1.0d0))))
            wij23 = wij23*mu(i)/alpha(i)
            wij(2,3) = wij(2,3) + wij23
c
            wij33 = vec1(i)*(vec3(i)*(xs(3)**(-2.0d0))*
     x         (xs(1)**alpha(i))+(vec3(i)*(xs(3)**(-2.0d0))*
     x         (xs(2)**alpha(i))) + (2.0d0*vec4(i)*
     x         (xs(3)**(alpha(i)-2.0d0))))
            wij33 = wij33*mu(i)/alpha(i)
            wij(3,3) = wij(3,3) + wij33
   50    continue
c
         wij(2,1) = wij(1,2)
         wij(3,1) = wij(1,3)
         wij(3,2) = wij(2,3)
c
c     --- two roots of cgt(..) are equal: case(2)
      elseif (idx .eq. 2) then
c
c        --- define some facilitator variables
         do 60 i = 1,ncof
            vec2(i) = alpha(i)/3.0d0
            vec3(i) = vec2(i) + 1
            vec4(i) = 2.0d0*vec2(i) - 1.0d0
            vec1(i) = vec2(i) * (xs(1)**(-vec2(i))) *
     x         (xs(2)**(-vec2(i))) * (xs(3)**(-vec2(i)))
   60    continue
c
c        --- the strain energy density function
         do 70 i = 1,ncof
            wxxx = (xs(1)**(2.0d0*vec2(i)))
     x          * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(-vec2(i))) + (xs(1)**(-vec2(i))) *
     x          (xs(2)**(2.0d0*vec2(i))) * (xs(3)**(-vec2(i)))
     x          + (xs(1)**(-vec2(i))) * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(2.0d0*vec2(i))) - 3.0d0
            wxxx = wxxx*mu(i)/alpha(i)
            wini = wini + wxxx
   70    continue
            wini = wini + 0.5d0*bm*((sqrt (xiii(3)) - 1.0d0)**2)
c
c        --- consider 3 cases under idx = 2
         if (i1.eq.3 .and. i2.eq.1) then
c           --- compute the first partial derivative
            do 80 i = 1,ncof
               wi3 = vec1(i) * (((-xs(3)**(-1.0d0)) * (xs(1)**
     x            alpha(i))) - ((xs(3)**(-1.0d0))
     x            *(xs(2)**alpha(i)))
     x            + (2.0d0*(xs(3)**(alpha(i)-1.0d0))))
               wi3 = wi3*mu(i)/alpha(i)
               wi(3) = wi(3) + wi3
c
               wi1 = vec1(i) * (2.0d0*(xs(1)**(alpha(i)-1.0d0)) -
     x             ((xs(1)**(-1.0d0)) * (xs(2)**alpha(i)))
     x             - (xs(1)**(-1.0d0) * (xs(3)**alpha(i))))
               wi1 = wi1*mu(i)/alpha(i)
               wi(1) = wi(1) + wi1
   80       continue
         elseif (i1.eq.1 .and. i2.eq.3) then
            do 81 i = 1,ncof
               wi1 = vec1(i) * (2.0d0*(xs(1)**(alpha(i)-1.0d0)) -
     x             ((xs(1)**(-1.0d0)) * (xs(2)**alpha(i)))
     x             - (xs(1)**(-1.0d0) * (xs(3)**alpha(i))))
               wi1 = wi1*mu(i)/alpha(i)
               wi(1) = wi(1) + wi1
c
               wi3 = vec1(i) * (((-xs(3)**(-1.0d0)) * (xs(1)**
     x            alpha(i))) - ((xs(3)**(-1.0d0))
     x            *(xs(2)**alpha(i)))
     x            + (2.0d0*(xs(3)**(alpha(i)-1.0d0))))
               wi3 = wi3*mu(i)/alpha(i)
               wi(3) = wi(3) + wi3
   81       continue
         elseif (i1.eq.2 .and. i2.eq.1) then
            do 82 i = 1,ncof
               wi2 = vec1(i) * ((-xs(2)**(-1.0d0)) * (xs(1)**
     x             alpha(i)) + (2.0d0*(xs(2)**(alpha(i)-1.0d0))) -
     x             ((xs(2)**(-1.0d0)) * (xs(3)**alpha(i))))
               wi2 = wi2*mu(i)/alpha(i)
               wi(2) = wi(2) + wi2
c
               wi1 = vec1(i) * (2.0d0*(xs(1)**(alpha(i)-1.0d0)) -
     x             ((xs(1)**(-1.0d0)) * (xs(2)**alpha(i)))
     x             - (xs(1)**(-1.0d0) * (xs(3)**alpha(i))))
               wi1 = wi1*mu(i)/alpha(i)
               wi(1) = wi(1) + wi1
   82       continue
         endif
c
c        --- consider 3 cases under idx = 2
         if (i1.eq.3 .and. i2.eq.1) then
c           --- compute the second order partial derivative
            do 90 i = 1,ncof
               wij11 = vec1(i) * (2.0d0*(vec4(i)*(xs(1)**(alpha(i)
     x            -2.0d0))) + (vec3(i)*(xs(1)**(-2.0d0))
     x             *(xs(2)**alpha(i))) + (vec3(i)*(xs(1)**
     x             (-2.0d0))*(xs(3)**alpha(i))))
               wij11 = wij11*mu(i)/alpha(i)
               wij(1,1) = wij(1,1) + wij11
c
               wij13 = vec1(i) * ((-2.0d0*vec2(i)*(xs(1)**(alpha(i)
     x            -1.0d0)) * (xs(3)**(-1.0d0))) +
     x            (vec2(i)*(xs(1)**(-1.0d0))*(xs(2)**alpha(i))*
     x            (xs(3)**(-1.0d0)))-(2.0d0*vec2(i)*
     x            (xs(1)**(-1.0d0))*(xs(3)**(alpha(i)-1.0d0))))
               wij13 = wij13*mu(i)/alpha(i)
               wij(1,3) = wij(1,3) + wij13
c
               wij33 = vec1(i)*(vec3(i)*(xs(3)**(-2.0d0))*
     x            (xs(1)**alpha(i))+(vec3(i)*(xs(3)**(-2.0d0))*
     x            (xs(2)**alpha(i))) + (2.0d0*vec4(i)*
     x            (xs(3)**(alpha(i)-2.0d0))))
               wij33 = wij33*mu(i)/alpha(i)
               wij(3,3) = wij(3,3) + wij33
   90       continue
c
            wij(3,1) = wij(1,3)
         elseif (i1.eq.1 .and. i2.eq.3) then
            do 91 i = 1,ncof
               wij11 = vec1(i) * (2.0d0*(vec4(i)*(xs(1)**(alpha(i)
     x            -2.0d0))) + (vec3(i)*(xs(1)**(-2.0d0))
     x             *(xs(2)**alpha(i))) + (vec3(i)*(xs(1)**
     x             (-2.0d0))*(xs(3)**alpha(i))))
               wij11 = wij11*mu(i)/alpha(i)
               wij(1,1) = wij(1,1) + wij11
c
               wij13 = vec1(i) * ((-2.0d0*vec2(i)*(xs(1)**(alpha(i)
     x            -1.0d0)) * (xs(3)**(-1.0d0))) +
     x            (vec2(i)*(xs(1)**(-1.0d0))*(xs(2)**alpha(i))*
     x            (xs(3)**(-1.0d0)))-(2.0d0*vec2(i)*
     x            (xs(1)**(-1.0d0))*(xs(3)**(alpha(i)-1.0d0))))
               wij13 = wij13*mu(i)/alpha(i)
               wij(1,3) = wij(1,3) + wij13
c
               wij33 = vec1(i)*(vec3(i)*(xs(3)**(-2.0d0))*
     x            (xs(1)**alpha(i))+(vec3(i)*(xs(3)**(-2.0d0))*
     x            (xs(2)**alpha(i))) + (2.0d0*vec4(i)*
     x            (xs(3)**(alpha(i)-2.0d0))))
               wij33 = wij33*mu(i)/alpha(i)
               wij(3,3) = wij(3,3) + wij33
   91       continue
c
            wij(3,1) = wij(1,3)
         elseif (i1.eq.2 .and. i2.eq.1) then
            do 92 i = 1,ncof
               wij11 = vec1(i) * (2.0d0*(vec4(i)*(xs(1)**(alpha(i)
     x            -2.0d0))) + (vec3(i)*(xs(1)**(-2.0d0))
     x             *(xs(2)**alpha(i))) + (vec3(i)*(xs(1)**
     x             (-2.0d0))*(xs(3)**alpha(i))))
               wij11 = wij11*mu(i)/alpha(i)
               wij(1,1) = wij(1,1) + wij11
c
               wij12 = vec1(i) * ((-2.0d0*vec2(i)*(xs(1)**(alpha(i)
     x            -1.0d0)) * (xs(2)**(-1.0d0))) -
     x            (2.0d0*vec2(i)*(xs(2)**(alpha(i)-1.0d0)) *
     x            (xs(1)**(-1.0d0)))+(vec2(i)*(xs(1)**
     x            (-1.0d0))*(xs(2)**(-1.0d0))*(xs(3)**
     x            alpha(i))))
               wij12 = wij12*mu(i)/alpha(i)
               wij(1,2) = wij(1,2) + wij12
c
               wij22 = vec1(i) * ((vec3(i)*(xs(2)**(-2.0d0)) *
     x            (xs(1)**alpha(i)))+(2.0d0*vec4(i) *
     x            (xs(2)**(alpha(i)-2.0d0)))+(vec3(i)
     x            *(xs(2)**(-2.0d0)) * (xs(3)**alpha(i))))
               wij22 = wij22*mu(i)/alpha(i)
               wij(2,2) = wij(2,2) + wij22
   92       continue
c
            wij(2,1) = wij(1,2)
         endif
c
c     --- all roots of cgt(..) are equal: case(3)
      elseif (idx .eq. 3) then
c
c        --- define some facilitator variables
         do 100 i = 1,ncof
            vec2(i) = alpha(i)/3.0d0
            vec3(i) = vec2(i) + 1
            vec4(i) = 2.0d0*vec2(i) - 1.0d0
            vec1(i) = vec2(i) * (xs(1)**(-vec2(i))) *
     x         (xs(2)**(-vec2(i))) * (xs(3)**(-vec2(i)))
  100    continue
c
c        --- the strain energy density function
         do 110 i = 1,ncof
            wxxx = (xs(1)**(2.0d0*vec2(i)))
     x          * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(-vec2(i))) + (xs(1)**(-vec2(i))) *
     x          (xs(2)**(2.0d0*vec2(i))) * (xs(3)**(-vec2(i)))
     x          + (xs(1)**(-vec2(i))) * (xs(2)**(-vec2(i)))
     x          * (xs(3)**(2.0d0*vec2(i))) - 3.0d0
            wxxx = wxxx*mu(i)/alpha(i)
            wini = wini + wxxx
  110    continue
            wini = wini + 0.5d0*bm*((sqrt (xiii(3)) - 1.0d0)**2)
c
c        --- compute the first partial derivative
         do 120 i = 1,ncof
            wi1 = vec1(i) * (2.0d0*(xs(1)**(alpha(i)-1.0d0)) -
     x          ((xs(1)**(-1.0d0)) * (xs(2)**alpha(i)))
     x          - (xs(1)**(-1.0d0) * (xs(3)**alpha(i))))
            wi1 = wi1*mu(i)/alpha(i)
            wi(1) = wi(1) + wi1
c
  120    continue
c
c        --- compute the second order partial derivative
         do 130 i = 1,ncof
            wij11 = vec1(i) * (2.0d0*(vec4(i)*(xs(1)**(alpha(i)
     x         -2.0d0))) + (vec3(i)*(xs(1)**(-2.0d0))
     x          *(xs(2)**alpha(i))) + (vec3(i)*(xs(1)**(-2.0d0))
     x          *(xs(3)**alpha(i))))
            wij11 = wij11*mu(i)/alpha(i)
            wij(1,1) = wij(1,1) + wij11
c
  130    continue
c
      endif
c     -------------------------------------------------------------
c
c     --- compute additional vector and matrix for convenience
      do 140 i = 1,3
         beta(i) = xs(i)*wi(i)
  140 continue
      do 160 i = 1,3
         do 150 j = 1,3
            gamma(i,j) = xs(j)*delta3(j,i)*wi(i) + xs(j)*xs(i)*
     x                   wij(j,i)
  150    continue
  160 continue
c
c     --- hydrostatic energy derivatives (wini computed above
c     --- already contains this portion of energy)
      call vzero (wm(1), 3)
      call vzero (wmn(1,1), 9)
      wm(3) = 0.5d0*bm*(1.0d0 - xiii(3)**(-0.5d0))
      wmn(3,3) = 0.25d0*bm*xiii(3)**(-1.5d0)
c
c     --- calculate pressure and pressure derivatives wrt invariants
      call vzero (pi(1), 3)
      call vzero (pij(1,1), 9)
      prs = - bm*(sqrt(xiii(3)) - 1.0d0)
      pi(3) = - 0.50d0*bm/(xiii(3)**0.5d0)
      pij(3,3) = 0.25d0*bm/(xiii(3)**1.5d0)
c
c     --- rearrange terms for 2d plane strain case for compactness
      if (ndf.eq.2) then
c
c        --- rearrange wi and beta terms
         wm(2) = wm(3)
         wm(3) = 0.0d0
         wmn(2,2) = wmn(3,3)
         wmn(3,3) = 0.0d0
c
c        --- rearrange p terms
         pi(2) = pi(3)
         pi(3) = 0.0d0
         pij(2,2) = pij(3,3)
         pij(3,3) = 0.0d0
      endif
c
      return
      end
