! ANSYS $RCSfile: TORQ2D.MAC,v $
! Modified on $Date: 2010-11-14 13:40:31 -0500 (Sun, 14 Nov 2010) $ 
! Source ID = $Revision: 435640 $
!                                                             mpg vz dfo
/nopr 
*get,_nops,active,,prkey
*get,_mnu,active,,menu
*get,_arout,active,,rout
*if,_arout,ne,31,then
 *msg,warn
 Enter Post1 before issuing this command macro
 _ok=1
 *else
 _ok=0
 torque=
/out,scratch
/COM,   MACRO CALCULATING TORQUE ON A BODY
/COM,   USING THE MAXWELL STRESS TENSOR APPROACH. 
/COM,
/COM,   *** ASSUMPTIONS
/COM,       VALID FOR 2-D PLANAR 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 TORQUE
/COM,              THE PATH TABLE IS CLEARED AT COMPLETION OF MACRO 
/COM,
/COM,   *** ANSYS REVISION 5.3
/COM,
/COM,   *** CALCULATED OUTPUT QUANTITIES
/COM,                                   
/COM,       TORQUE = TORQUE ABOUT GLOBAL ORIGIN 
/COM,
/COM,       (PARAMETER IS RETAINED)
/COM,
/out
*get,_sysr,active,,rsys             ! get active RSYS c.s.
*get,_sysd,active,,dsys             ! get active DSYS c.s.
*get,_unit,active,,solu,emunit      ! 1=mks, 2=cgs, 3=user
*get,_mu,active,,solu,muzro
*get,_dimn,active,,solu,dimn   
!  convert keydim from new to old meaning   pck 10/98 qt-13025
!  new:  0=planar, 1=axisym, 2=axisym-harmonic, 3=3d
!  old:  1=axisym, 2=planar, 3=3d
*if,_dimn,eq,0,then
_dimn=2
*endif
!  convert to new usage as convenient
!  end of keydim conversion
*get,_antyp,active,,anty

! 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

*if,_dimn,eq,3,then
 *msg,warn
 Command macro TORQ2D may not be valid for 3-d analysis
 _ok=0
*endif
*if,_dimn,eq,1,then
 *msg,error
 Command macro TORQ2D is not available for axisymmetric analysis
 _ok=1
 *else
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,_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_=12345
CSYS,0

_hat=
*dim,_hat,array,3
*get,_lsno2,active,,set,lstp
 *if,_lsno2,eq,0,then
   *msg,error
   Read in desired load step data (see SET command)
  _ok=1
 *endif
PDEF,pbx,B,X
*get,check_,path,,max,pbx
*if,check_,eq,12345,then
 *msg,error
 Path must be defined before calling command macro
 _ok=1
 *else
  *if,_antyp,ne,3,then
  ! Static or Transient Calculation
PDEF,PBY,B,Y
PDEF,PBZ,B,Z
*if,_mg1,ne,1,then
 _mg1=0
*endif
*if,_mnu*(_mg1-1),ne,0,then
 /PBC,PATH,1
 dsys,0
 NPLOT                              ! display path on node plot
*endif                            
PVECT,NORM,PNX,PNY,PNZ              ! get normals to the path
PCALC,MULT,PV01,pbx,pbx             ! Bx^2 (Bx * Bx)
PCALC,MULT,PV02,PBY,PBY             ! By^2
PCALC,ADD,PV01,PV01,PV02            ! Bx^2 + By^2 (=B^2)
PDOT,PV02,pbx,PBY,PBZ,PNX,PNY,PNZ   ! [Bx,By,0] . [Nx,Ny,0]
PCALC,MULT,PV03,XG,PBY              ! By * Rx
PCALC,MULT,PV04,YG,pbx              ! Bx * Ry
PCALC,ADD,PV03,PV03,PV04,,-1        ! (By * Rx) - (Bx * Ry)
PCALC,MULT,PV02,PV02,PV03           ! ([Bx,By,0] . [Nx,Ny,0])(ByRx - BxRy)
PCALC,MULT,PV03,XG,PNY              ! Rx * Ny
PCALC,MULT,PV04,YG,PNX              ! Ry * Nx
PCALC,ADD,PV03,PV03,PV04,,-1        ! (Rx * Ny) - (Ry * Nx)
PCALC,MULT,PV01,PV01,PV03           ! (B^2)(RxNy - RyNx)
PCALC,ADD,PV01,PV01,PV02,-0.5    ! [Bx,By,0].[Nx,Ny,0])(ByRx - BxRy)-.5(B^2)(RxNy-RyNx)
PCALC,INTG,TORQ,PV01,S,_RELZ        ! Integrate over path and divide by perm.
*GET,TORQUE,PATH,,LAST,TORQ         ! store as TORQUE
  *else
  ! Harmonic calculation
    *do,_i,1,2
     set,_lsno2,1,,_i-1
     *if,_i,eq,1,then
      pdef,_bax1,b,x
      pdef,_bay1,b,y
     *else
      pdef,_bax2,b,x
      pdef,_bay2,b,y
     *endif
    *enddo
     pvect,norm,nx,ny,nz
     pdot,pv02,_bax1,_bay1,_bay1,nx,ny,nz
     pcalc,mult,pv03,xg,_bay1
     pcalc,mult,pv04,yg,_bax1
     pcalc,add,pv03,pv03,pv04,,-1
     pcalc,mult,pv02,pv02,pv03
     pcalc,intg,torq,pv02,s,1/_mu
    *get,_hat(1),path,,last,torq
     pdot,pv02,_bax2,_bay2,_bay2,nx,ny,nz
     pcalc,mult,pv03,xg,_bay2
     pcalc,mult,pv04,yg,_bax2
     pcalc,add,pv03,pv03,pv04,,-1
     pcalc,mult,pv02,pv02,pv03
     pcalc,intg,torq,pv02,s,1/_mu
    *get,_hat(2),path,,last,torq
     PCALC,MULT,PV01,_bax1,_bax1        
     PCALC,MULT,PV02,_bay1,_bay1
     PCALC,ADD,PV01,PV01,PV02       
     PCALC,MULT,PV03,_bax2,_bax2        
     PCALC,MULT,PV04,_bay2,_bay2
     PCALC,ADD,PV03,PV03,PV04       
     PCALC,ADD,PV01,PV01,PV03       
     PCALC,MULT,PV03,XG,NY              ! Rx * Ny
     PCALC,MULT,PV04,YG,NX              ! Ry * Nx
     PCALC,ADD,PV03,PV03,PV04,,-1        ! (Rx * Ny) - (Ry * Nx)
     PCALC,MULT,PV01,PV01,PV03           ! (B^2)(RxNy - RyNx)
     pcalc,intg,torq,pv01,s,-.5/_mu
    *get,_hat(3),path,,last,torq
    torque = 0.5*(_hat(1)+_hat(2)+_hat(3))
  *endif

*if,_unit,eq,2,then
 torque=torque/_pi4
*endif
PDEF,CLEAR
/out,torq2d,out
*msg,info
%/________________SUMMARY OF TORQUE CALCULATIONS______________
*if,_antyp,eq,3,then
  *msg,info
  %/*** Harmonic Torque calculation is a beta feature at Rev 5.4 ***
*endif
 
*if,_unit,le,1,then
 *msg,info,TORQUE
 %/Torque = %g N*m/m
*elseif,_unit,eq,2,then
 *msg,info,TORQUE
 %/Torque = %g Dyne*cm/cm
*endif
*msg,info
%/Parameter defined for torque:  TORQUE
*msg,info
____________________________________________________________
/out
*endif
rsys,_sysr
dsys,_sysd
/pbc,path,0
*endif
*endif
rsys,_sysr
dsys,_sysd
*if,_arout*(1+_ok),eq,31,then
  *list,torq2d,out          ! batch listing
  *if,_mnu*(1-_mg1),ne,0,then
   *uilist,torq2d,out       ! ui listing
  *endif
*endif
_arout=
_mnu=
_ok=
_pi4=
_sysd=
_sysr=
_relz=
check_=
_unit=
_mu=
_dimn=
_hat= $_bax1= $_bay1= $_bax2= $_bay2= $_hator= $_antyp= $_lsno2=
*if,_afun,gt,1,then
 *afun,deg
*endif
_afun=
*endif
_arout=
_mnu=
*if,_nops,eq,0,then
 /nopr
 _nops=
 *else
 _nops=
 /go
*endif
