! ANSYS REVISION 5.3, FOR2D.MAC, Modified on 96/04/30, (SID) = 3.2
/out,for_off,out
/COM,   MACRO FOR CALCULATING MAGNETIC FORCES ON A BODY
/COM,   USING THE MAXWELL STRESS TENSOR APPROACH. 
/COM,
/COM,   *** ASSUMPTIONS
/COM,       VALID FOR 2-D PLANAR OR AXISYMMETRIC ANALYSIS.
/COM,       THIS ROUTINE WRITTEN FOR MKS OR CGS UNITS.
/COM,       PATH MUST BE SPECIFIED PRIOR TO CALLING THIS MACRO
/COM,       THE /SHOW COMMAND MUST BE ISSUED PRIOR TO CALLING MACRO
/COM,           IF RESULTS ARE TO BE DISPLAYED
/COM,
/COM,   *** NOTE:  PATH MUST BE SPECIFIED COUNTERCLOCKWISE FOR CORRECT
/COM,              SIGN ON FORCE
/COM,              THE PATH TABLE IS CLEARED AT COMPLETION OF MACRO 
/COM,
/COM,   *** ANSYS REVISION 5.3
/COM,
/COM,   *** CALCULATED OUTPUT QUANTITIES
/COM,                                   
/COM,       FX   = FORCE IN X DIRECTION 
/COM,       FY   = FORCE IN Y DIRECTION 
/COM,
/COM,       (PARAMETERS ARE RETAINED)
/COM,

/gopr
*get,_nops,active,,prkey
*get,_mnu,active,,menu
*get,_arout,active,,rout
_ok=0
*if,_arout,ne,31,then
 *msg,warn
 Enter Post1 before issuing this command macro
 _ok=1
 *else
 fx=
 fy=
*get,_sysr,active,,rsys             ! get active RSYS c.s.
*get,_unit,active,,solu,emunit      ! 1=mks, 2=cgs, 3=user
*get,_mu,active,,solu,muzro
*get,_dimn,active,,solu,dimn        ! 1=axisym, 2=planar, 3=3d 

! Perform correction check on planar or 3d analysis for structural line elements
cm,_curelm,elem
*if,_dimn,eq,3,then        ! if 3d detected, then
 esel,,ename,,13,53,40      ! select all possible 2-d emag field elements
 *get,_num2d,elem,,count
 *if,_num2d,gt,0,then
  _dimn=2               ! if 2-d emag element exists, switch dimensionality 
 *else
  _dimn=3
 *endif
cmsel,s,_curelm
_num2d=
*endif
cmdele,_curelm

*get,_sysd,active,,dsys             ! get active DSYS c.s.
RSYS,0
_afun=atan(1)
*if,_afun,gt,1,then
 *afun,rad
*endif
_pi4=16*atan(1)                     ! 4*pi
_relz=1/_mu                         ! reluctivity of free space
*if,_dimn,eq,3,then
 *msg,warn
 Command macro FOR2D may not be valid for 3-d analysis
 _ok=0
*endif
*if,_unit,gt,2,then
 *msg,error
 Invalid units option specified, please reissue command macro
 _ok=1
*endif
*if,_unit,le,2,then                 ! error trap for no LPATH
 check_=0
 !PDEF,BPX,B,X
 !*get,check_,path,,max,BPX
 *if,check_,eq,12345,then
  *msg,error
  Path must be defined before calling command macro
  _ok=1
  *else
   cmsel,,rotor
   *get,rmrmdx,node,,mxloc,x
   esel,r,mat,,3
   esel,a,mat,,2
   nsle
   csys,1
   *get,_rrdmx,node,,mxloc,x
   _rtorq=_rrdmx+.25*(rmrmdx-_rrdmx)
   arg1=_rtorq
   arg2=20
   _tcsys=99
   esel,,mat,,1

   *get,_antyp,active,,anty
   *if,_antyp,ne,3,then
    _nget=1
   *else
    _nget=2
   *endif
   *do,_ifor1,1,_nget
    *if,_ifor1,eq,1,then
     set,1,1
    *else
     set,1,1,,1
    *endif
    local,_tcsys,1  !  ##1
    cscir,_tcsys,0
    path,_torq,2,,arg2*48
    ppath,1,,-arg1,-1.e-6,,_tcsys
    ppath,2,,-arg1,0,,_tcsys
    PDEF,BPX,B,X
    PDEF,BPY,B,Y
    PDEF,BPZ,B,Z
    DSYS,0
    *if,_mg1,ne,1,then
     _mg1=0
    *endif
    *if,_mnu*(_mg1-1),ne,0,then
      /PBC,PATH,1
      NPLOT                              ! display path on node plot
    *endif
    PCALC,MULT,BX2,BPX,BPX              ! bx2 = bpx * bpx
    PCALC,MULT,BY2,BPY,BPY              ! by2 = bpy * bpy
    PCALC,ADD,B2,BX2,BY2                ! b2 = bx2 + by2
    PCALC,ADD,T11,BX2,B2,,-0.5          ! t11 = bx2 - 0.5 * b2
    PCALC,MULT,T12,BPX,BPY              ! t12 = bpx * bpy
    PCALC,ADD,T22,BY2,B2,,-0.5          ! t22 = by2 - 0.5 * b2
    csys  !   ##1
    PVECT,NORM,NX,NY,NZ                 ! get normals to the path
    PDOT,TNX,T11,T12,BPZ,NX,NY,NZ       ! tnx = [t11,t12,0] . [nx,ny,0]
    PDOT,TNY,T12,T22,BPZ,NX,NY,NZ       ! tny = [t12,t22,0] . [nx,ny,0]
    *IF,_dimn,eq,1,THEN                 ! axisymmetric
     PCALC,MULT,TNX,TNX,XG,_pi4/2       ! otherwise _mult. tnx and tny
     PCALC,MULT,TNY,TNY,XG,_pi4/2       !   by 2 * PI * r
    *ENDIF
    PCALC,INTG,FX,TNX,S,_RELZ           ! fx = tnx integrated over path / perm.
    *GET,FX,PATH,,LAST,FX               ! store this value
    PCALC,INTG,FY,TNY,S,_RELZ           ! fy = tny integrated over path / perm.
    *GET,FY,PATH,,LAST,FY               ! store this value
    *if,_unit,eq,2,then
      fx=fx/_pi4
      fy=fy/_pi4
    *endif
    !  PDEF,CLEAR
    *if,_ifor1,eq,1,then
      fxr=fx
      fyr=fy
     *else
      fxi=fx
      fyi=fy
      fx=.5*(fxr+fxi)
      fy=.5*(fyr+fyi)
    *endif
   *enddo
/nopr
/out,for2d,out
*msg,info
%/___SUMMARY OF FORCE CALCULATIONS BY MAXWELL STRESS TENSOR____
 *if,_antyp,eq,3,then
  *msg,info
  The forces are RMS values for an AC analysis
*if,_dimn,eq,2,then
 *if,_unit,le,1,then
  *msg,info,FX
  %/Force in x-direction = %G N/m
  *msg,info,FY
  %/Force in y-direction = %G  N/m
 *elseif,_unit,eq,2,then
  *msg,info,FX
  %/Force in x-direction = %G Dyne/cm
  *msg,info,FY
  %/Force in y-direction = %G  Dyne/cm
 *endif
*elseif,_dimn,eq,1,then
 *if,_unit,le,1,then
  *msg,info,FX
  %/Force in x-direction = %G N
  *msg,info,FY
  %/Force in y-direction = %G N
 *elseif,_unit,eq,2,then
  *msg,info,FX
  %/Force in x-direction = %G Dyne
  *msg,info,FY
  %/Force in y-direction = %G Dyne
 *endif
*endif
*msg,info
%/Parameter defined for force in X direction:  FX
*msg,info
%/Parameter defined for FORCE in Y direction:  FY
*msg,info
_____________________________________________________________
/out
/gopr
rsys,_sysr
dsys,_sysd
/pbc,path,0
*endif
*endif
rsys,_sysr
dsys,_sysd
_pi4=
_sysd=
_unit=
_mu=
_dimn=
_sysr=
_relz=
check_=
*if,_afun,gt,1,then
 *afun,deg
*endif
_afun=
*endif
*if,_arout*(1+_ok),eq,31,then
  *list,for2d,out          ! batch listing
  *if,_mnu*(1-_mg1),ne,0,then
   *uilist,for2d,out       ! ui listing
  *endif
*endif
_arout=
_mnu=
_dely=
_ok=
*if,_nops,eq,0,then
 /nopr
 _nops=
 *else
 _nops=
 /go
*endif