/gopr
/uis,msgpop,3
/out,mvrot3ds,out
/nerr,0,1e4,-1
!    mvrot3ds.mac
!    For a periodic model, moves the lower edge of the rotor to this 
!    angle from the existing position for a 3D machine model.
!    For a full model (360 degrees), moves the rotor by an incremental
!    angle. This is modified to address skewed periodic models. The mvrot3d.mac
!    should be used for other 3d models.
!
!    Assumes stator is defined as component "STATOR"
!    and rotor is defined as "ROTOR"
!
!    Assumes that permach has been called. If not,  the rotor will be moved
!    but the model will not be connected by this macro.  A message is
!    printed if the mvrotor was requested to stitch the rotor/stator and
!    the periodic conditions were not applied before mvrotor was used.
!    
!
!     arg1 = angle (D) measured CCW from +X axis
!
!     arg2 = 0 no action (The rotor is moved, but they are not stitched.)
!          = 1 call macro to connect the rotor to the stator
!     arg9 = 1, user is prompted for the angle of the rotor
!
alls
csys,1
esel,,mat,,5
nsle
*get,thmn,node,,mnloc,y
*get,thmx,node,,mxloc,y
*get,_mnu,active,,menu
*if,_nomsg,eq,0,then
*if,_mnu,ne,0,then
 *if,arg9,eq,1,then
  *if,abs(thmx-thmn),lt,200,then
   *ask,_arg1,Enter the Angle of Position for the Rotor,0
  *else
   *ask,_arg1,Enter the Angle Increment for the Rotor,0
  *endif
  arg1=_arg1    !  7/19/97
 *endif
*endif
*endif
fini
_evod=1        !   sets flag to odd condition
/prep7
nsel,all
immed,0

_ierror=0
_appsti=arg2
*msg,info,arg1
  move to angle: %g
*get,_ac_ce,active,,ce
*if,_ac_ce,gt,0,then
  cedel,all,,,any
  /nerr,0,1e5
  numc,ce
*endif
!  check for full models
*if,(thmx-thmn),gt,200,then
 a_rg1=arg1
 _thtinc=arg1      !   2/18/96
 !   it is a full model
 !  first rotate
 cmsel,,rotor
 *get,cur_a,node,mrk_nod,loc,y
 _cthtmn=cur_a
 csys,1
 !  ngen,2,,all,,,0,a_rg1-cur_a
 ngen,2,,all,,,0,a_rg1
 cmsel,,rotor
 nsel,r,ext
 *get,rdmx,node,,mxloc,x
 nsel,r,loc,x,rdmx-.00001,rdmx+1
 *get,_ncount,node,,count      !  1//30/96
 cm,ce_n,node
 cmsel,,stator
 esel,r,mat,,1
 cmsel,,ce_n
 ceintf,.1
*else
 !  it is periodic model


 *if,arg2,ne,0,then
  !   check periodic conditions 
  cmsel,,stator
  csys,1
  nsel,r,ext
  *get,_rmin,node,,mnloc,x
  *get,_rmax,node,,mxloc,x
  _rave=(_rmin+_rmax)/2
  *get,_thtmax,node,,mxloc,y
  nsel,r,loc,y,_thtmax-.0001,_thtmax+.001  !   3/2/01
  _perndr=node(_rave,_thtmax,0)

  *get,_cpmx,active,,cp
  *get,_cemx,active,,ce

  nsel,,,,_perndr
  nsel,r,cp,,1,_cpmx
  *get,_ncpnd,node,,count

  nsel,,,,_perndr
  nsel,r,ce,,1,_cemx
  *get,_ncend,node,,count
  *if,_ncpnd,ne,0,then
    _evod=2        !    sets to even condition
  *endif
  *if,_ncend+_ncpnd,eq,0,then
    !    no periodic conditions have been applied
    !    determine which condition is to be applied
    _appsti=0
    _ierror=1
  *endif

 *endif

cmsel,,rotor
csys,1
*get,_cthtmx,node,,mxloc,y
*get,_cthtmn,node,,mnloc,y
_thtinc=arg1-_cthtmn
nsle

*if,_ierror,eq,0,then
/com, location 1
ngen,2,,all,,,0,_thtinc

!  complete the connection of the edge nodes which may not have been
!  connected by the stitch3d macro.
!  locate the edge nodes
*get,_cpmx,active,,cp
*get,_cemx,active,,ce
*set,_pernd
*dim,_pernd,,4,4
_pernd(1,3)=10,1,10,1
_pernd(1,4)=1,-1,1,1
*set,_cpce
*dim,_cpce,char,2
*set,_invr
*dim,_invr,table,4
_invr(1,0)=-1e8,-.1,.1,1e8
_invr(1,1)=0,1,1,0
_cpce(1)='ce','cp'
csys,1
cmsel,,stator
*get,_rmin,node,,mnloc,x
*get,_thtmax,node,,mxloc,y
*get,_thtmin,node,,mnloc,y

!  construct an array for the z coordinate 
esel,,mat,,2
nsle
csys,1
*get,_naxial,node,,count
*get,_ncpu1,active,,time,cpu
*set,_nnodz
*dim,_nnodz,,_naxial

*do,_inx1,1,_naxial
 /gopr
 /out,mvrot3ds,out,,append
 *msg,info,_inx1
 cycle in _inx1: %i
 *get,_ndmx,node,,num,max
 *if,_ndmx,eq,0,exit
 *get,_nnodz(_inx1),node,,mnloc,z
 nsel,u,loc,z,_nnodz(_inx1)-.0001,_nnodz(_inx1)+.0001
*enddo
_naxial=_inx1-1
*get,_ncpu2,active,,time,cpu
_ndcpu=_ncpu2-_ncpu1
*stat,_nnodz,1,_naxial

*if,_appsti,eq,1,then
 *do,_i3d1,1,_naxial
  /gopr
  *msg,info,_i3d1
  cycle in _i3d1: %i

  *msg,info,_i3d1,_nnodz(_i3d1)
  cycle= %i  and current Z plane: %g
  stitch3d,_nnodz(_i3d1)
  /out,mvrot3ds,out,,append
  cmsel,,_cerotor               !  1//30/96
  *get,_ncount,node,,count      !  1//30/96

  cmsel,,stator
  nsel,r,loc,z,_nnodz(_i3d1)-.0001,_nnodz(_i3d1)+.0001
  _c_z=_nnodz(_i3d1)
  _pernd(1,1)=node(_rmin,_thtmax,_c_z)
  _pernd(2,1)=node(_rmin,_thtmin,_c_z)
  nsel,,,,_pernd(1,1)
  nsel,a,,,_pernd(2,1)
  esln
  nsle
  nsel,u,,,_pernd(1,1)
  nsel,u,,,_pernd(2,1)

  *get,_rmax,node,,mxloc,x
  _pernd(3,1)=node(_rmax,_thtmax,_c_z)
  _pernd(4,1)=node(_rmax,_thtmin,_c_z)
  _cpcemx=_cpmx+_cemx
  cmsel,,stator
  _cemx=_cemx+1
  *msg,info,_i3d1,_nnodz(_i3d1)
  remaining CEs for cycle= %i  and current Z plane: %g
  !ce,_cemx,0,_pernd(1,1),mag,1,_pernd(2,1),mag,_pernd(_evod,4)
  _cemx=_cemx+1
  !ce,_cemx,0,_pernd(3,1),mag,1,_pernd(4,1),mag,_pernd(_evod,4)
 *enddo
*endif    !  end of if logic for _appsti=1

*endif   ! end of error condition logic

*endif    !   bypass logic for periodic models



cmsel,,rotor
*get,_ecnt,elem,,count
!   update the magnet position
_idmag=0
_do_mag=10
*if,_ierror,eq,0,then

*set,_magno
*dim,_magno,,_ecnt
*if,rad_mag,eq,0,then
  *do,_ifdmag,1,_ecnt
    /gopr
    *get,_mxelem,elem,,num,max
    *if,_mxelem,eq,0,exit
    *get,_matno,elem,_mxelem,attr,mat
    *get,_mgxx,mgxx,_matno,,,const,1
    *get,_mgyy,mgyy,_matno,,,const,1
    *if,abs(_mgxx)+abs(_mgyy),ne,0,then
      _do_mag=0
      _mxelem=0
      _idmag=_idmag+1
      _magno(_idmag)=_matno
     *endif
     esel,u,mat,,_matno
  *enddo
*endif
_nummag=0

*set,_esyflg                                    !  11/01/94
*dim,_esyflg,,10000                             !  11/01/94

*if,rad_mag+_do_mag,eq,0,then
 *do,_jfdmag,1,_idmag
  /gopr
  *msg,info,_jfdmag
  outer loop: _jfdmag
  _matno=_magno(_jfdmag)
  cmsel,,rotor
  esel,r,mat,,_matno
  *get,_ecnt,elem,,count
  cm,_curmod,elem
  *do,_ishmag,1,_ecnt
   /gopr
   *msg,info,_ishmag
   inner loop: _ishmag  %i
   *get,_mxelem,elem,,num,max
   *if,_mxelem,eq,0,exit
   *get,_curesy,elem,_mxelem,attr,esys

   *if,_esyflg(_curesy),eq,0,then                  !  11/01/94
    esel,,,,_mxelem
    nsle
    *get,_mxnode,node,,num,max
    csys,_curesy
    nrot,all
    *get,_cesysa,node,_mxnode,ang,xy
    _newang=_cesysa+_thtinc
    local,_curesy,0,0,0,0,_newang
    csys
    nrot,all
    cmsel,,_curmod
    esel,u,esys,,_curesy
    cm,_curmod,elem
    _mxelem=0
    _nummag=_nummag+1
    _esyflg(_curesy)=1                               !  11/01/94
   *else                                            !  11/01/94
    _nummag=_nummag+1                                !  11/01/94
    cmsel,,_curmod                                   !  11/01/94
    esel,u,esys,,_curesy                             !  11/01/94
    cm,_curmod,elem                                  !  11/01/94
   *endif                                           !  11/01/94
  *enddo
 *enddo
 _ishmag=_ishmag-1
*endif



*endif   !  end of logic error condition

*set,_styno
*dim,_styno,char,2
_styno(1)='No','Yes'
alls
csys
*set,_esyflg
/out
/nopr
/out,mvrot3ds,sum
*msg,info
>________________MOVEMENT OF THE ROTOR_________________<
*if,_ierror,eq,0,then

*if,(thmx-thmn),lt,180,then
 *msg,info,_cthtmn
 OLD angle of the lower edge of the rotor:__________ %g
 *msg,info,arg1
 NEW angle of lower edge of the rotor:______________ %g
*else
 *msg,info,arg1
 Increment of Rotation of rotor(D):_________________ %g
*endif

 *msg,info,_ncount
 Number of nodes in rotor:__________________________ %i
*if,rad_mag,eq,0,then
 *msg,info,_nummag
 Number of magnet coordinate systems moved:_________ %i
*endif

*endif

*msg,info,_styno(_appsti+1)
 Was the Rotor "Stitched" to the Stator ?:__________ %c

*if,_ierror,eq,1,then
*msg,info
  THE MODEL DID not HAVE NOT ANY PERIODIC CONDITIONS
*msg,info
  THE STATOR COULD not BE CONNECTED TO THE ROTOR.
*msg,info
  USE THE <EVEN_BC> TO APPLY THE PERIODIC CONDITIONS.
*endif

*msg,info
>______________________________________________________<
/out
*list,mvrot3ds,sum

*if,_nomsg,eq,0,then
 *uilist,mvrot3ds,sum
 /uis,msgpop
 dsys
 /pbc,cp,1    !  2/17/96
 /pbc,ce,1    !  2/17/96
 *get,_dist,graph,1,dist
 /dist,,_dist
 _dist=
 /pbc,all
 /pbc,ce,1
 ! eplot
 immed,1
*endif
/gopr

:end
/out