/out,g_force,out,,append

!  g_force.mac
!  Loads the rotor/stator nodes into the node force arrays and then
!  writes out the specified file.  The forces in the stator are in the
!  node component tor_sta, and those for the rotor are in tor_rot node
!  component.
!
!    arg1 = file name for stator forces (defaults to stator.dat)
!    arg2 = file name for rotor forces  (defaults to rotor.dat)
!    arg3 = current mechanical angle (D)
!    arg4 = Ith iteration in the solutions (printed out in header)(defaults to 1)
!    arg5 = total number of iterations for this run (printed out in header)(defaults to 1)
!
!
! ***************************************************************
!
!   format
!   line 1:   
!   1-15   angle (arg3) 
!  16-30   wall clock time at time of writing the record
!  31-45   number of nodes written
!  46-55   Ith iteration for this series of solutions
!  56-65   Total number of iterations for this, if they are all completed
!
!   line 2 - N+1  (N=number of nodes)
!   1-15   fx
!  16-30   fy
!  31-45   fz
!  46-55   X coordinate (global cartesian)
!  56-65   Y coordinate (global cartesian)
!  66-75   Z coordinate (global cartesian)
!  76-85   node number
!
!    internal arrays
!   1=>  stator
!   2=>  rotor
!   3=>  combined
!
_arg1=arg1
*get,_targ1,parm,_arg1,type
*if,_targ1,ne,3,then
  arg1='stator'
*endif

_arg2=arg2
*get,_targ2,parm,_arg2,type
*if,_targ2,ne,3,then
  arg2='rotor'
*endif

*if,arg4,le,0,then
  arg4=1
*endif
_arg4=arg4


*if,arg5,le,0,then
  arg5=1
*endif
_arg5=arg5


nsel,all
*get,_ndmx,node,,num,max
*set,_mskv
*dim,_mskv,,_ndmx,3

*set,_forxyz
*dim,_forxyz,,_ndmx,3

*set,_xyz
*dim,_xyz,,_ndmx,3

*set,nnod
*dim,nnod,,_ndmx

/com, load the stator nodes
cmsel,,tor_sta
nsle
cm,cn1,node
esel,,mat,,5
nsle
cmsel,r,cn1
cm,torstan,node
*get,ntorsta,node,,count
*vget,_mskv(1,1),node,1,nsel
*voper,_mskv(1,1),_mskv(1,1),gt,0


/com, load the rotor nodes
cmsel,,tor_rot
nsle
cm,cn1,node
esel,,mat,,2,3
nsle
cmsel,r,cn1
cm,torrotn,node
*get,ntorrot,node,,count

*vget,_mskv(1,2),node,1,nsel
*voper,_mskv(1,2),_mskv(1,2),gt,0

*voper,_mskv(1,3),_mskv(1,1),add,_mskv(1,2)
*voper,_mskv(1,3),_mskv(1,3),gt,0

cmsel,a,torstan
cmsel,,tor_sta
cmsel,a,tor_rot

*vmask,_mskv(1,3)
*vget,_forxyz(1,1),node,1,fmag,x
*vmask,_mskv(1,3)
*vget,_forxyz(1,2),node,1,fmag,y
*vmask,_mskv(1,3)
*vget,_forxyz(1,3),node,1,fmag,z

csys
*vmask,_mskv(1,3)
*vget,_xyz(1,1),node,1,loc,x
*vmask,_mskv(1,3)
*vget,_xyz(1,2),node,1,loc,y
*vmask,_mskv(1,3)
*vget,_xyz(1,3),node,1,loc,z

*vmask,_mskv(1,3)
*vfill,nnod(1),ramp,1,1

!parsav,all,temp,par

_mang=arg3
*get,cwall,active,,time,wall


 /com, write out the stator forces
 /nopr
 /out,%arg1%,dat,,append
  *vwrite,_mang,cwall,ntorsta,_arg4,_arg5
  (f15.0,f15.3,f15.0,2f10.0)
  *vmask,_mskv(1,1)
  *vwrite,_forxyz(1,1),_forxyz(1,2),_forxyz(1,3),_xyz(1,1),_xyz(1,2),_xyz(1,3),nnod(1)
  (3f15.6,3f10.6,f10.0)
 /out,g_force,out,,append
 /gopr

 /com, write out the rotor forces
 /nopr
 /out,%arg2%,dat,,append
  *vwrite,_mang,cwall,ntorrot,_arg4,_arg5
  (f15.0,f15.3,f15.0,2f10.0)
  *vmask,_mskv(1,2)
  *vwrite,_forxyz(1,1),_forxyz(1,2),_forxyz(1,3),_xyz(1,1),_xyz(1,2),_xyz(1,3),nnod(1)
  (3f15.6,3f10.6,f10.0)
 /out,g_force,out,,append
 /gopr



:end


/out