*deck,userfx       USERDISTRIB  parallel                                pck
      subroutine userfx (ielc,elem,time,nr,u, ndat,dat)
c *** primary function: change element face heat flux surface info
c
c         *** Copyright ANSYS.  All Rights Reserved.
c         *** ansys, inc.
c
c         in order to activate this user programmable feature,
c         the user must enter the 'usrcal,userfx' command.
c
c         this routine is called during each substep of each load step.
c         it is called for each equilibrium iteration.
c         it is called once per element.  it is called only during the heat
c         flow load vector formulation stage, and not during the heat flow
c         evaluation stage.
c         the call to get the standard ansys input heat flux surfaces
c         is made just before entering this routine, so this information is
c         available to be modified, if desired.
c
c     input arguments:
c        variable (typ,siz,intent)    description
c        ielc   (int,ar(IELCSZ),in) - array of element type characteristics
c        elem     (int,sc,in)       - element number for operation.
c        time     (dp,sc,in)        - time of current substep
c        nr       (int,sc,in)       - number of nodal temperatures
c                                         of the element
c        u        (dp,ar(nr),in)    - vector of most recent values of the
c                                      temperatures
c        ndat     (int,sc,in)       - number of data points per element
c                                      for example, for solid70, ndat = 24 = 6*4
c                                      where 6 = faces per element
c                                            4 = corners per face
c        dat    (dp,ar(ndat),inout) - fluxes
c                                      (has input values for each corner
c                                      of each face)

c
c     output arguments:
c        variable (typ,siz,intent)    description
c        dat    (dp,ar(ndat),inout) - fluxes
c                                      (defines input values for each corner
c                                      of each face)
c
#include "impcom.inc"
#include "echprm.inc"
#include "locknm.inc"
#include "ansysdef.inc"
c
      external erhandler,pplock,ppunlock,FaceNodeList,prope1

      external eprget,elmiqr,rlget,ndgall,elmget,cmpiqr,cmploc
      integer  eprget,elmiqr,rlget,ndgall,elmget,cmpiqr,cmploc
      external         vsum
      double precision vsum
      external cmpactL
      logical  cmpactL
      external cmpiqrL
      LONGINT  cmpiqrL

c
      integer elem,ielc(IELCSZ),nr,ndat
      integer nodes(20),numnd,npt,iface,num,inode(8),i,nsloc,
     x imat,nsur(125),j,ntab,leng,elmdat(EL_DIM)
      LONGINT iptrL

      double precision time,u(nr), dat(ndat)
      double precision xyzang(6,20),press(6),rcon(20),temf(8),avetem,
     x spht
      character*(PARMSIZE)  cName_in
      data nsur /1,2,4, 2,3,5, 3,1,6,
     x           1,2, 2,3, 3,4, 4,1,
     x           1,2,5, 2,3,6, 3,4,7, 4,1,8,
     x           1,2,3,5,6,7, 2,1,4,5,8,9, 3,2,4,6,9,10, 4,1,3,8,7,10,
     x           1,2,3,4, 2,1,5,6, 3,2,6,7, 4,3,7,8, 1,4,8,5, 6,5,8,7,
     x 1,2,3,4,9,10,11,12, 2,1,5,6,9,17,13,18, 3,2,6,7,10,18,14,19,
     x 4,3,7,8,11,19,15,20, 1,4,8,5,12,20,16,17, 6,5,8,7,13,16,15,14/
c
c   *** customer should remove the below warning when making any changes ***
c
      call erhandler('userfx',5000,2,
     x 'ANSYS,INC.-supplied version of coding for USERFX has been used.'
     x ,0.0d0,' ')
c
c     **** dummy logic to satisfy analyzer (should be removed by UPF programmer)
      if (elem .eq. -9876) dat(1) = time*u(1) + ielc(IETYP)
      if (elem .ne. 4) go to 999
c     ---- lock (and unlock) needed only if running on a parallel machine
      call pplock (LOCKDB)
c
c     ***** demonstration coding ***
c
c     **** given a component name, see if logic is to be performed
c     ****   conv2 is given as an example of a component name
      cName_in = 'CONV2   '
      ntab = cmploc (cName_in)
c     **** if (ntab .le. 0) no such component
      leng = cmpiqr (ntab,2)
c     **** leng is the number of entitities(e.g. elements)
c     **** may be packed; i.e. 5,6,7,8 may be 5,-8, so that leng = 2, not 4
      iptrL = cmpiqrL (ntab,0)
c     **** iptrL is pointer location
      if (cmpactL (elem,iptrL,leng)) then
c        **** perform logic of choice
      endif
      call ppunlock (LOCKDB)
c     **** given the element number, get node and property numbers with elmget:
c     **** also see userpr for another approach
      numnd = elmget (elem,elmdat(1),nodes(1))
c     --- keep the analyzer happy
      numnd = numnd
c     **** numnd = number of nodes
c     **** nodes = vector of node numbers
c
c     **** given a node number, get the coordinates using ndgall
c     **** input - node number, output = coordinates and angles
      i = ndgall (nodes(3),xyzang(1,1))
c
c     **** given a face and the nodes of the element, get the number of
c     **** nodes on that face(num), and the numbers of those nodes (inode)
c     **** by using FaceNodeList:
      iface = 2
      call FaceNodeList (iface,ielc(1),nodes(1),num,inode(1))
c
c     **** given an element number and a face, get the applied pressure on
c     **** that face (valid only for multifield elements)
      i = eprget (elem,iface,press(1))
c
c     **** get real constants, if desired
      i = rlget (elmdat(EL_REAL),rcon(1))
c
c     **** given the element temperature vector(u), extract the temperatures
c     **** of the active face(temf) using the nsur data statement.
c
c     *** npt = number per face
c     *** nsloc = starting location in nsur array
      nsloc = 0
      if (ielc(ISHAP) .eq. JTRIAN .and. ielc(MNODE) .gt. 0) then
c     ****      triangle-shaped elements with    midside nodes (e.g. plane35)
         npt = 3
         nsloc =  1
      elseif (ielc(ISHAP) .eq. JQUAD  .and. ielc(MNODE) .eq. 0) then
c     **** quadrilateral-shaped elements without midside nodes (e.g. plane55)
         npt = 2
         nsloc = 10
      elseif (ielc(ISHAP) .eq. JQUAD  .and. ielc(MNODE) .gt. 0) then
c     **** quadrilateral-shaped elements with    midside nodes (e.g. plane77)
         npt = 3
         nsloc = 18
      elseif (ielc(ISHAP) .eq. JTET   .and. ielc(MNODE) .gt. 0) then
c     ****   tetrahedron-shaped elements with    midside nodes (e.g. solid87)
         npt = 6
         nsloc = 30
      elseif (ielc(ISHAP) .eq. JBRICK .and. ielc(MNODE) .eq. 0) then
c     ****         brick-shaped elements without midside nodes (e.g. solid70)
         npt = 4
         nsloc = 54
      elseif (ielc(ISHAP) .eq. JBRICK .and. ielc(MNODE) .gt. 0) then
c     ****         brick-shaped elements with    midside nodes (e.g. solid90)
         npt = 8
         nsloc = 78
      endif
c
      i = (iface-1)*npt + nsloc - 1
      do j = 1,npt
         i = i + 1
         temf(j) = u(nsur(i))
      enddo
c
c     **** given an element number, get the material number.  then evaluate
c     **** the material property(specific heat in this example) at the
c     **** average face temperature.
c     **** see el101 for more information on prope1.
      imat = elmiqr (elem,-EL_MAT)
      avetem = vsum (temf(1),npt)/dble (npt)
      call prope1 (elem,imat,22,avetem,spht)
c
c     **** for a small test problem, the film coefficient actually used
c     **** may be verified by inputting   /debug,,, ,,1  just before
c     **** the solve command.
c
 999  continue
      return
      end
