!
!       TITLE:  rplot_nodal.mac (POST)
!
!                         Add support for running in batch mode:
!                         Added /nopr & /gopr commands near line 160
!                         Added more examples
!
!  Plot nodal results versus node location for the selected node set.
!
!  The input arguments are:
!
!   (1) cs = Coordinate system number to use when obtaining
!            node locations and nodal results.
!
!   (2) SrtDir = Coorinate system direction to sort nodes by.
!
!   (3) Item1 = Nodal degree of freedom result label, "Item1" from ANSYS
!               Commands Manual, *get, Table 5. i.e. displacement (U),
!               stress (S), current (CURR), etc.
!
!   (4) Comp1 = Component "IT1NUM" from Table 5 of the *get command.
!
!   (5-m) Itemn,Compn = Additional results to plot on the same graph
!                       (n = 8 max, m = 12 max). All these arguments
!                       are optional and can be left blank.
!
!  Additional plotting manipulations and capabilities are obtained by
!  setting certain parameters prior to calling this macro. None of the
!  parameters defined below are required, but they can be specified in
!  any combination to enable desired features.
!
!   The nodal results can optionally have a function or mathematical
!   operation applied to the x-axis data or y-axis data prior to plotting.
!   This is done by setting any of the following parameters:
!
!       fx='FN'     ! Ansys function applied to x-axis data, StrDir
!
!       fy1='FN'    ! Ansys function applied to y-axis data Item1, Comp1
!       .
!       .
!       fyn='FN'    ! Optional Ansys function to apply to Itemn, Compn
!                   ! (where n = 8 or less)
!
!       Where 'FN' is any of the single argument functions listed in
!       section 3.9 of the ANSYS APDL Guide.
!       (file:///D|/ANSYS56/docu/catalog/english/ansyshelp/Hlp_P_APDL3_9.html)
!
!       The mathematical operations are applied through the definition
!       of Ansys parameters, fx, fy1, etc.  fy1 is applied to the
!       first curve, fy2 is applied to the second curve and so on.
!
!       A mathematical function is applied by suffixing the character
!       contents of the fyn parameter with "(X)"  where "X" is a result
!       value.  See examples below.
!
!       NOTE:  These parm's are unset after each call to rplot_nodal.
!
!   Two additional parameters can be set prior to calling this macro
!   which will cause a data file to be written for use in other
!   plotting programs:
!
!       mkasc=1     If this parm is defined, an ASCII data file called
!                   rplot_nodal.asc will be written for import into
!                   Tk-Solver. Use the File -> Import... menu to bring
!                   in the data.
!
!       mkdat=1     Define this parm to have an ASCII data file created
!                   in the format easily read into PSI-Plot, Excel, etc.
!                   The file will be named rplot_nodal.dat.
!
!   NOTE:  These parameters are unset at the end of rplot_nodal.
!
!
!             (1)  (2)     (3)    (4)    (5)    (6)... (11 max) (12 max)
! rplot_nodal,cs ,SrtDir, Item1, Comp1, Item2, Comp2,...,Itemn, Compn
!
!   NOTE: Only four arguments are required.
!
!
!   1.  Plot the absolute value of minimum principal stress and von Mises
!       stress verses angular position of the currently selected node set.
!       Write data files, rplot_nodal.asc and rplot_nodal.dat.
!
!           mkasc=1
!           mkdat=1
!           fy1='abs'
!           rplot_nodal,cyl1cs,'y','s','3','s','eqv'
!
!   2.  Plot von Mises stress, 1/2 the principal stress and the
!       absolute value of the shear stress verses the x coordinates
!       of the currently selected node set. Do NOT write data files,
!       rplot_nodal.asc and rplot_nodal.dat.
!
!           fy2='.5*'
!           fy3='abs'
!           rplot_nodal,ct1cs,'x','s','eqv','s','1','s','xy'
!
!   3.  Plot radial displacement of a roller (cylinder) along an arc
!       length of contact elements (type 3). The coordinate system CSc
!       is a cylindrical CS at the center of the roller. The center of
!       contact is at y = theta = +90 deg. Plot 90 deg as zero inches of
!       arc length. Plot from zero to the end of the contact elements.
!       The arc length s = R*(90 - theta*pi/180) where theta is in degrees
!       from setting of "*afun,deg" and then must be converted to
!       radians and arranged in the form fx(theta).  This gives fx as
!       shown:
!
!           esel,s,type,,3
!           nsle,s
!           fx='0.5*Rr*pi-Rr*pi/180*'
!           rplot_nodal,CSc,'y','U','X'
!
!
!   rplot_nodal.mac
!       striplis.perl
!       line1.perl
!

! # of data sets (curves) to plot
*get,npairs_,cmd,0,nargs
npairs_=(npairs_-3)/2

!*get,prkey_,active,0,prkey
!/nopr

fini
/post1

!
! Give the input meaningful names
!
cs_=arg1
SrtDir_=upcase(arg2)

*dim,items_,char,npairs_
*dim,comps_,char,npairs_
*dim,cmb_,char,npairs_      ! Combine items & comps

*do,i_,1,npairs_
    *if,i_,lt,5,then
        items_(i_)=arg%3+2*(i_-1)%
    *else
        items_(i_)=ar%3+2*(i_-1)%
    *endif

    *if,i_,lt,4,then
        comps_(i_)=arg%4+2*(i_-1)%
    *else
        comps_(i_)=ar%4+2*(i_-1)%
    *endif
        cmb_(i_)='%items_(i_)%%comps_(i_)%'
*enddo

!
! Save current settings
!
*get,csys_,active,0,csys
*get,rsys_,active,0,rsys
*get,dsys_,active,0,dsys

!
! All three coordinate systems must be set to get
! consistent results
!
rsys,cs_
csys,cs_
dsys,cs_        ! Needed for consistent NLIST

!
! Create a list of nodal locations for the abscissa (x-axis)
!
nsort,loc,SrtDir_,1,0
/nopr
/out,coords_,lis
nlist,all,,,coord,SrtDir_,node,node
/out

!
! Strip away extraneous lines from the list files
! before reading them into Ansys
!
/sys,runperl.bat striplis.perl coords_.lis

*get,ncount_,node,0,count               ! Number of nodes in set

!
! Read the abscissa (x-axis) data into a vector
! Depending on the "SrtDir" read the 2nd, 3rd or 4th column
! of numbers from "coords.lis"
!
! The file format is (I9,2X,3(1X,G19.12))
!
*set,%SrtDir_%_
*dim,%SrtDir_%_,array,ncount_

*if,SrtDir_,eq,'X',then
*vread,%SrtDir_%_(1),coords_,lis
(12X,G19.12)

*elseif,SrtDir_,eq,'Y'
*vread,%SrtDir_%_(1),coords_,lis
(32X,G19.12)

*elseif,SrtDir_,eq,'Z'
*vread,%SrtDir_%_(1),coords_,lis
(52X,G19.12)

*else
*msg,error,SrtDir_
Sort Direction %C is invalid, use X, Y or Z.
*endif

!
! Apply fx to the x-axis data
!
*get,ptype_,parm,fx,type    ! Define math function
*if,ptype_,eq,3,then
    *do,j_,1,ncount_
        %SrtDir_%_(j_)=%fx%(%SrtDir_%_(j_))
    *enddo
*endif

!
! Read in the node numbers
!
*dim,vnodes_,array,ncount_

*vread,vnodes_(1),coords_,lis
(F9.0)
!
! Read the y-axis data into a table
!
avprin,0,0                          ! Use nodal average

*set,a_
*dim,a_,table,ncount_,npairs_       ! Store all the y-axis data here

!
! The following algorithim could be written with two *do loops
! instead of three.  But, this is faster for the case of plotting
! many data points and no applied math function.
!
*do,i_,1,npairs_
    *get,ptype_,parm,fy%i_%,type    ! Define math function
    *if,ptype_,eq,3,then
        temp_=fy%i_%
        *do,j_,1,ncount_
            *get,dup,node,vnodes_(j_),items_(i_),comps_(i_)
            dup=%temp_%(dup)
            a_(j_,i_)=dup
        *enddo
    *else
        *do,j_,1,ncount_
            *get,a_(j_,i_),node,vnodes_(j_),items_(i_),comps_(i_)
        *enddo
    *endif
    /gcolumn,i_,%items_(i_)%%comps_(i_)%
*enddo

*vplot,%SrtDir_%_(1),a_(1,1),2,3,4,5,6,7,8

!
! Write the data to a Tk-Solver ".asc" file
! if parameter "MKASC" is defined.
!
*get,ptype_,parm,mkasc,type
*if,ptype_,ne,-1,then
/nopr
/out,rplot_nodal,asc
*msg,info
node:
*vwrite,vnodes_(1)
(F8.0)

*msg,info,SrtDir_
%C:
*vwrite,%SrtDir_%_(1)
(E20.12)


*do,i_,1,npairs_
*msg,info,cmb_(i_)
%C:
*vwrite,a_(1,i_)
(E20.12)
*enddo
/out

*endif

!
! Write the data to a PSI-Plot ".dat" file
! if parameter "MKDAT" is defined.
!
*get,ptype_,parm,mkdat,type
*if,ptype_,ne,-1,then

! Create the first line of column names
/nopr
/out,rplot_nodal,dat
*vwrite
('node')
*vwrite,SrtDir_
(A8)
*vwrite,cmb_(1)
(A8)
/out
/sys,runperl.bat line1.perl rplot_nodal.dat

! Write out all the data in column format
/out,rplot_nodal,dat,,append
*if,npairs_,eq,1,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1,1)
(F8.0,2E20.12)
*elseif,npairs_,eq,2,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2)
(F8.0,3E20.12)
*elseif,npairs_,eq,3,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3)
(F8.0,4E20.12)
*elseif,npairs_,eq,4,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3),a_(1,4)
(F8.0,5E20.12)
*elseif,npairs_,eq,5,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3),a_(1,4),a_(1,5)
(F8.0,6E20.12)
*elseif,npairs_,eq,6,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3),a_(1,4),a_(1,5),a_(1,6)
(F8.0,7E20.12)
*elseif,npairs_,eq,7,then
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3),a_(1,4),a_(1,5),a_(1,6),a_(1,7)
(F8.0,8E20.12)
*else
*vwrite,vnodes_(1),%SrtDir_%_(1),a_(1),a_(1,2),a_(1,3),a_(1,4),a_(1,5),a_(1,6),a_(1,7),a_(1,8)
(F8.0,9E20.12)
*endif
/out

*endif

!
! Restore settings and Clean Up
!
/nopr
rsys,rsys_
csys,csys_
dsys,dsys_

*do,i_,1,npairs_
    *set,fy%i_%
*enddo

*set,mkasc
*set,mkdat

!*set,%SrtDir_%_    ! If this is unset then plotting won't work
!*set,SrtDir_       ! If this is unset then plotting won't work
                    ! Also do NOT unset "a_"

*set,cmb_
*set,comps_
*set,Comp_
*set,csys_
*set,cs_
*set,dsys_
*set,dup
*set,fx
*set,items_
*set,Item_
*set,i_
*set,j_
*set,ncount_
*set,npairs_
*set,ptype_
*set,rsys_
*set,temp_
*set,vnodes_

*if,prkey_,eq,1,then
    /go
*endif