/gopr
/out,mtorqc2d,out
/com, ANSYS REVISION 5.5, mTORQC2D.MAC, Modified on 12/25/00
/com,  12/25/00:  allows the periodic models to straddle the -X axis, if rotor side is
/com,             computing the torque.
!
!  Macro for computing torque about a circular arc
!
!  Assumptions:
!      2-d planar analysis
!      torque is about global origin
!      This macro uses the macros from the maglib macro library.
!
!  ANSYS Rev 5.3
!
!  Input parameters for macro
!
!  arg1 = radius of the circle to evaluate the torque
!  arg2 = number of nodes generated to define the paths
!  arg3 = Enter local cylindrical c.s. about global origin
!  arg4 = Enter radial tolerance for selecting elements about path
!  arg5 = Z coordinate
!
!  Default conditions:
!   arg2 = 18 nodes at radius arg1
!   arg3 = 99
!   arg4 = (no select performed)
!
!  Output parameter
!
!   tor_que = torque (N-m or Dyne-cm) for modelled sector

!   (Parameter is retained)
!
!/out
cmsel,,stator
csys,1
*get,_rmn,node,,mnloc,x
*if,arg1,lt,_rmn,then
  cmsel,,rotor
*endif
esel,r,mat,,1

cm,_elm,elem
cm,_nod,node
_ok=0
*if,_mg1,ne,1,then
 _mg1=0
*endif
*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
 /wait,_dely
 _ok=1
 *else
 tor_que=
 /gsave,sasigrph
 *get,_sysr,active,,rsys
 *get,_sysd,active,,dsys
 *get,_sysc,active,,csys
 *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 

 *get,_antyp,active,,anty

! Perform correction check on planar or 3d analysis for structural line elements
 *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,_elm
  _num2d=
*endif

*if,_dimn,eq,3,then
 *msg,warn
 Command macro TORQC2D may not be valid for 3-d analysis
 /wait,_dely
 _ok=0
*endif
*if,_dimn,eq,1,then
 *msg,error
 Command macro TORQC2D is not available for axisymmetric analysis
 /wait,dely
  _ok=1
*else
 _afun=atan(1)
 *if,_afun,gt,1,then
  *afun,rad              ! check *afun setting to ensure radians
 *endif
  _pi=4*atan(1)
 *if,arg1,eq,0,then
  *msg,error
  Radius for torque evaluation is required, reissue command macro
  /wait,_dely
  _ok=1
 *endif
 *if,arg2,eq,0,then
   arg2=18
 *endif
 *if,_unit,gt,2,then
  *msg,error
  Invalid units option specified, please reissue command macro
   _ok=1
 *endif
 *if,_unit,le,2,then
  *if,arg3,eq,0,then
   _tcsys=99
   local,_tcsys,1
  *elseif,arg3,eq,1,then
   _tcsys=99
   local,_tcsys,1
  *msg,info
  Local c.s. (csys=1) changed to 99 (csys=99)
  *else
  _tcsys=arg3
  csys,_tcsys
  *endif
  nsle        ! get the nodes for the present elements
  *get,_endang,node,,mxloc,y
  *get,_staang,node,,mnloc,y
  _partcir=1           ! default to a part circle model

  cscir,_tcsys,1
  *get,_endang1,node,,mxloc,y
  *get,_staang1,node,,mnloc,y
  cscir,_tcsys

   _hat=
   *dim,_hat,array,2
  _phist = _pi*_staang/180
  _phien = _pi*_endang/180
  _xbeg = arg1*cos(_phist)
  _ybeg = arg1*sin(_phist)
  _xend = arg1*cos(_phien)
  _yend = arg1*sin(_phien)

 *get,_lsno2,active,,set,lstp
  *if,_lsno2,eq,0,then
   *msg,error
   Read in desired load step data (see SET command)
  _ok=1
  *endif

  _de_a1=_endang1-_staang1
  _de_a2=_endang-_staang
  _partcir=1
  *if,_de_a2,gt,180,then
    *if,_de_a1,gt,180,then
        _partcir=0
    *endif
  *endif
  *msg,info,_partcir
  _partcir flag: %g

 _strad=0
 *if,_partcir,eq,1,then
   *if,_endang*_staang,lt,0,then
     /com,  model straddles the -X axis
     _strad=1
    *afun,deg
    _xbeg = arg1*cos(_staang1)
    _ybeg = arg1*sin(_staang1)
    _xend = arg1*cos(_endang1)
    _yend = arg1*sin(_endang1)
    _sendang=_endang
    _endang=_endang1
    *afun,rad
   *endif
 *endif

!  *if,_endang,gt,135,then
!   *if,_staang,lt,-145,then
!    the model is a full circle because there are nodes on both sides of
!    of the 180 D line
!    _partcir=0
!   *endif
!  *endif

  *if,_partcir,eq,0,then            ! full model
   /com, full circle option is used
     cscir,_tcsys,0
     path,_torq,2,,arg2*48
     ppath,1,,-arg1,-1.e-6,arg5,_tcsys
     ppath,2,,-arg1,0,arg5,_tcsys
  *elseif,_partcir,ne,0,then                   ! partial model
   /com, partial circle
   *if,_staang,gt,180,then
     /com, option 1
     cscir,_tcsys,1
     path,_torq,2,,arg2*48
     ppath,1,,_xbeg,_ybeg,arg5
     ppath,2,,_xend,_yend,arg5
   *elseif,_endang,ge,180,then
     /com, option 2
     path,_torq,3,,arg2*24
     ppath,1,,_xbeg,_ybeg,arg5
     ppath,2,,-arg1,arg1*1.e-4,arg5
     cscir,_tcsys,1
     ppath,3,,_xend,_yend,arg5
   *else
     /com, option 3
     cscir,_tcsys,0
     path,_torq,2,,arg2*48
     ppath,1,,_xbeg,_ybeg,arg5
     ppath,2,,_xend,_yend,arg5
   *endif
  *endif

!  computes the torque based on the maxwell's stress tensor
   rsys
   csys
  *if,_antyp,ne,3,then
  /com,  Static or Transient Calculation
     pdef,_bax1,b,x
     pdef,_bay1,b,y
     rsys,1
     pdef,bradial,b,x
     pdef,btanget,b,y
     rsys
     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
    tor_que = _hat(1)
  *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
    tor_que = 0.5*(_hat(1)+_hat(2))
  *endif

    *if,_mnu*(1-_mg1),ne,0,then
      /auto
!     plots nodes
      dsys,0
      /pbc,path,1
      nplot
      /pbc,path,0
    *endif
 *endif

  *if,_unit,eq,2,then
    tor_que=tor_que/(16*atan(1))
  *endif
   /out,mtorqc2d,sum
  *msg,info
________________SUMMARY OF CIRCULAR TORQUE CALCULATIONS______________
  *if,_partcir,eq,0,then
   *msg,info
   %/Torque Evaluation is based on a full circle
   *if,_unit,le,1,then
    *msg,info,arg1,tor_que
    %/tor_que at radius ( %g ) is : %g N*m/m
   *elseif,_unit,eq,2,then
    *msg,info,arg1,tor_que
    %/Torque at radius ( %g ) is : %g Dyne*cm/cm
    /wait,_dely
   *endif
   *else
   *msg,info,(_endang-_staang)
   %/Torque Evaluation is based on an arc of %g deg.
   *msg,info,_staang
   Starting Angle of Evaluation     :  %g
   *msg,info,_endang
   Ending Angle of Evaluation       :  %g
   *if,_unit,le,1,then
    *msg,info,arg1,tor_que
    %/Torque at radius ( %g ) is : %g N*m/m
   *elseif,_unit,eq,2,then
    *msg,info,arg1,tor_que
    %/Torque at radius ( %g ) is : %g Dyne*cm/cm
   *endif
   *msg,info
   %/Parameter defined for the torque: tor_que
  *endif
   ! pdef,clear
   ! padel
  *msg,info
   _____________________________________________________________________
  /out,mtorqc2d,out,,append
  /nopr
  arn= $_bfac= $_bmax= $bmin= $_btax= $_btin= $_delang= $_difflas=
  _endang= $_ierror= $_iexec= $ind1_= $ind2_= $_nnmax= $de=
  _nnstar= $_nodnag= $_npath= $_nrem= $_partcir= $_nmax1=
  pfac= $_pi= $pmax= $pmin= $_rintdiv= $_rrem= $_rres= $_staang=
  _tcsys= $_tfac= $tmin= $_tmax= $_torq= $_ylast= $_ynexlas=
  _arg1= $_rres= $_rrem=
  _nnsav=
  _phist= $_phien= $_xbeg= $_ybeg= $_xend= $_yend=
  _hat= $_bax1= $_bay1= $_bax2= $_bay2= $_hator= $_antyp= $_lsno2=

 *endif
  _mu=
*endif
_npathn=
 _nnode=
 rsys,_sysr
 dsys,_sysd
 csys,_sysc
 _sysr=
 _sysd=
 _sysc=
_unit=
_mu=
_dimn=
 *if,_afun,gt,1,then
  *afun,deg
 *endif
 _afun=
 /out,mtorqc2d,out,,append
 /gresume,sasigrph
dummy=
_dely=
/out,mtorqc2d,out,,append
/out
*if,_arout*(_ok+1),eq,31,then
  *list,mtorqc2d,sum          ! batch listing
  *if,_mnu*(1-_mg1),ne,0,then
   *uilist,mtorqc2d,sum       ! ui listing
  *endif
*endif
_arout=
_mnu=
_ok=
/uis,msgpop
cmsel,s,_elm
cmsel,s,_nod
cmdele,_elm
cmdele,_nod
/out
_unit=
*if,_nops,eq,0,then
 /nopr
 _nops=
 *else
 _nops=
 /go
*endif