/out,pl_force,out
!  Plots the forces for the specified nodes.  If nodes are entered as arguements
!  then the first four active nodes are processed, starting with the minimum node number
!  This file must contain all the forces, and the header data which contains the 
!  number of iterations must be correct.
!
!   arg1=name of file containing the forces  (.dat)
!   arg2=0, compute radial, tangential force components  (usful for stator)
!       =1, leave as global cartesian force components   (must be used for rotor)
!   arg3=0, plot the radial or global X force
!       =1, plot the tangential or global Y force
!   arg4,5,6,7 =node numbers

!
_err=0
_arg1=arg1
*get,_targ1,parm,_arg1,type
*if,_targ1,ne,3,then
 /nerr
   *msg,error
    File name is not character data-No action
   _err=1
*endif

_chk=0
/gopr
*set,_chk
*dim,_chk,,1,10
*vread,_chk(1,1),%_arg1%,dat   !  number of iterations
(55x,f10.0)

*vread,_chk(1,3),%_arg1%,dat   !  wall clock time in the file
(15x,f15.0)

*if,_chk(1,1),le,0,then
 /nerr
   *msg,error
    File data is corrput-No action
   _err=1
*endif

*vread,_chk(1,2),%_arg1%,dat    !  number of nodes for each iteration
(30x,f15.0)

n_line=_chk(1,1)*_chk(1,2)

*set,_fxy
*dim,_fxy,,n_line,5
*vread,_fxy(1,1),%_arg1%,dat
(f15.0)

*vread,_fxy(1,2),%_arg1%,dat
(15x,f15.0)

*vread,_fxy(1,3),%_arg1%,dat
(75x,f10.0)

parsav,all,temp,par


*set,_npl
*dim,_npl,,6

*set,_coln
*dim,_coln,,6

cm,_cnn1,node

*if,arg4,le,0,then
  *get,_ncnt,node,,count
  *if,_ncnt,le,0,then
    /out
    /nerr
    *msg,error
    No nodes were specified, no nodes are active-No action
    _err=1
  *else
   *if,_ncnt,gt,4,then
    _ncnt=4
   *endif
   *set,_npl
   *dim,_npl,,_ncnt
   *do,_ipl1,1,_ncnt
     *get,_npl(_ipl1),node,,num,min
     nsel,u,,,_npl(_ipl1)
   *enddo
  *endif
  /out,pl_force,out,,append
  /nerr,0,1e5
*else
 _npl(1)=arg4,arg5,arg6,arg7
 *do,_ncnt,1,6
   *if,_npl(_ncnt),le,0,exit
 *enddo
 _ncnt=_ncnt-1
*endif
*stat,_npl


*if,_err,gt,0,:end

*set,_fnx
*dim,_fnx,,_chk(1,1),_ncnt

*set,_fny
*dim,_fny,,_chk(1,1),_ncnt

*set,_rang
*dim,_rang,,_chk(1,1)

_loc1=1
/com,load the angles
 *do,_ipl2,1,_chk(1,1)
  /gopr
   _cloc=_loc1+(_ipl2-1)*(_chk(1,2)+1)
   _rang(_ipl2)=_fxy(_cloc,1)
 *enddo

*set,_tfx
*dim,_tfx,table,_chk(1,1),_ncnt

*set,_tfy
*dim,_tfy,table,_chk(1,1),_ncnt

_tfx(0,0)=1e-6
_tfy(0,0)=1e-6

*vfunc,_tfx(1,0),copy,_rang(1)
*vfunc,_tfy(1,0),copy,_rang(1)

*set,_nran
*dim,_nran,,_ncnt,3
csys,1
*afun,deg
*do,_ipl1,1,_ncnt
  *get,_nran(_ipl1),node,_npl(_ipl1),loc,y
  _nran(_ipl1,2)=sin(_nran(_ipl1,1))
  _nran(_ipl1,3)=cos(_nran(_ipl1,1))
  _coln(_ipl1)=_ipl1
*enddo
*stat,_nran
csys

*do,_ipl1,1,_ncnt
 _tfx(0,_ipl1)=_ipl1
 _tfy(0,_ipl1)=_ipl1

 /gopr
 *vfunc,_fxy(1,4),copy,_fxy(1,3)
 _cnode=_npl(_ipl1)
 *vabs,1
 *voper,_fxy(1,4),_fxy(1,4),sub,_cnode
 /com, locate the node
 *vscfun,_loc1,lmin,_fxy(1,4)
 *do,_ipl2,1,_chk(1,1)
   _cloc=_loc1+(_ipl2-1)*(_chk(1,2)+1)
   _cnod=_npl(_ipl1)
   *if,arg2,eq,0,then
    _fnx(_ipl2,_ipl1)=_fxy(_cloc,1)*_nran(_ipl1,3)+_fxy(_cloc,2)*_nran(_ipl1,2)
    _fny(_ipl2,_ipl1)=-_fxy(_cloc,1)*_nran(_ipl1,2)+_fxy(_cloc,2)*_nran(_ipl1,3)
   *else
    _fnx(_ipl2,_ipl1)=_fxy(_cloc,1)
    _fny(_ipl2,_ipl1)=_fxy(_cloc,2)
   *endif
 *enddo
 *vfunc,_tfx(1,_ipl1),copy,_fnx(1,_ipl1)
 *vfunc,_tfy(1,_ipl1),copy,_fny(1,_ipl1)
*enddo


/axlab,x,Mechanical Ang. (D)
/axlab,y,Force (N)
*vscf,_lmx,max,_rang(1)
*vscf,_lmn,min,_rang(1)
/xran,_lmn,_lmx

/grid,1
*do,_ipl1,1,_ncnt
 /gmarker,_ipl1,_ipl1
*enddo
*if,arg2,eq,0,then
 *if,arg3,eq,0,then
  /title, %mname%  Radial Force (N)
 *else
  /title, %mname%  Tangential Force (N)
 *endif
*else
 *if,arg3,eq,0,then
  /title, %mname%  Global X Force (N)
 *else
  /title, %mname%  Global Y Force (N)
 *endif
*endif

/nopr
/out,pl_force,sum
*msg,info
______________Summary of Nodal Forces (N) for Rotor Rotation___________
*msg,info

*msg,info,_arg1
Nodal force file name:____________ %c
*msg,info,_chk(1,3)
Wall clock time in file:__________ %g
*if,arg2,eq,0,then
*msg,info
 Force direction:_________________ Radial(Fr) and Tangential(Ft)
*else
*msg,info
 Force direction:_________________ Global X (Fx) and Global Y (Fy)
*endif
*msg,info
_____Legend Definition_______
*msg,info
___Node____Col Number____
*msg,info
____________On Plot______
*vlen,_ncnt
*vwrit,_npl(1),_coln(1)
(4x,f5.0,f8.0)
*msg,info

*if,_ncnt,eq,1,then
  *msg,info,_npl(1)
  Node:  %i
  !234567890!234567890!234567890!234567890!234567890!234567890!234567890
*if,arg2,eq,0,then
*msg,info
____Angle_____Fr_______Ft______
*else
*msg,info
____Angle_____Fx_______Fy______
*endif
*vwrit,_rang(1),_fnx(1,1),_fny(1,1)
(f9.2,2f9.2)

*if,arg3,eq,0,then
 *vplo,_tfx(1,0),_tfx(1,1)
*else
 *vplo,_tfy(1,0),_tfy(1,1)
*endif

*elseif,_ncnt,eq,2,then

*vlen,1
*vwrit,_npl(1),_npl(2)
('    Node:',7x,f7.0,11x,f7.0)
*if,arg2,eq,0,then
*msg,info
____Angle_____Fr_______Ft_______Fr_______Ft______
*else
*msg,info
____Angle_____Fx_______Fy_______Fx_______Fy______
*endif
*vwrit,_rang(1),_fnx(1,1),_fny(1,1),_fnx(1,2),_fny(1,2)
(f9.2,4f9.2)

*if,arg3,eq,0,then
 *vplo,_tfx(1,0),_tfx(1,1),2
*else
 *vplo,_tfy(1,0),_tfy(1,1),2
*endif

*elseif,_ncnt,eq,3,then

*vlen,1
*vwrit,_npl(1),_npl(2),_npl(3)
('    Node:',7x,f7.0,11x,f7.0,11x,f7.0)
*if,arg2,eq,0,then
*msg,info
____Angle_____Fr_______Ft_______Fr_______Ft_______Fr_______Ft______
*else
*msg,info
____Angle_____Fx_______Fy_______Fx_______Fy_______Fx_______Fy______

*endif
*vwrit,_rang(1),_fnx(1,1),_fny(1,1),_fnx(1,2),_fny(1,2),_fnx(1,3),_fny(1,3)
(f9.2,6f9.2)

*if,arg3,eq,0,then
 *vplo,_tfx(1,0),_tfx(1,1),2,3
*else
 *vplo,_tfy(1,0),_tfy(1,1),2,3
*endif


*elseif,_ncnt,eq,4,then
*vlen,1
*vwrit,_npl(1),_npl(2),_npl(3),_npl(4)
('    Node:',7x,f7.0,11x,f7.0,11x,f7.0,11x,f7.0)
*if,arg2,eq,0,then
*vwrit
('____Angle_____Fr_______Ft_______Fr_______Ft_______Fr_______Ft_______Fr_______Ft______')
*else
*vwrit
('____Angle_____Fx_______Fy_______Fx_______Fy_______Fx_______Fy_______Fx_______Fy______')

*endif
*vwrit,_rang(1),_fnx(1,1),_fny(1,1),_fnx(1,2),_fny(1,2),_fnx(1,3),_fny(1,3),_fnx(1,4),_fny(1,4)
(f9.2,8f9.2)


*if,arg3,eq,0,then
 *vplo,_tfx(1,0),_tfx(1,1),2,3,4
*else
 *vplo,_tfy(1,0),_tfy(1,1),2,3,4
*endif



*endif

/out
*uili,pl_force,sum


parsav,all,temp,par

cmsel,,_cnn1
cmdel,_cnn1

:end
/out