/out,mach3d,out
/com,mach3d.mac  3/14/98
!    This macro uses a 2D cross section of a machine model and extrudes 
!    it to make a 3D machine model. It is assumed that the 3D model is
!    symmetric about the XY plane.  Therefore only 1/2 of the model needs 
!    to be constructed in the Z direction, unless specified otherwise (in arg5)
!    The input arguements are for the total length of the stack.  The macro 
!    uses 1/2 of these values in the model generation unless arg5 is used.
!    If the parameters r_stk, s_stk, or s_stk are used, they are assumed 
!    to be the total stack lengths.
!
!    If arg5 is nonzero, the full dimensions are used, and the stacks are
!    centered about Z=0, the XY plane.  The model is first generated using
!    1/2 values for the stacks, then the rest of the model is generated
!    by symmetry
!
!    Required component names
!      rotor    contains all the elements on the rotor side of the model
!      stator   contains all the elements in the stator side of the model
!
!
!    arg1 = rotor stack thickness   (defaults to R_STK or unity)
!    arg2 = magnet stack thickness  (defaults to M_STK or arg1)
!    arg3 = stator stack thickness  (defaults to S_STK or arg2)
!    arg4 = axial divisons in the smallest stack dimension (defaults to 1)
!    arg5 = 0, no action, only 1/2 of the model is generated.
!         = 1, the full model is generated, and the center of the stack
!              is located at Z=0, the XY plane, and the stacks are centered
!              about Z=0.
!
!   NOTE:  The units of the thicknesses should be the same as the units
!          of the model parameters.  The parameter "GGEOM" is used to 
!          convert the model to meters (be sure GGEOM is specified)
!
!
!    REQUIRED parameters:  (usually specified in the parameter file)
!          component           parameter name       default
!      rotor iron material:       ri_mat               2         
!      magnet material:           mag_mat              3
!      stator iron:               st_mat               5
!
!    other parameters
!
!      a_stk           axial length of air outside the stack
!      ggeom           factor to convert the parameter units to the 
!                      SI system
!   OUTPUT:
!    component for the stator iron:   S_IRON (also stored in the parameter
!                                            "s_iron_c" )
!
fini
/prep7
*get,_mcp1,active,,time,cpu
*msg,info,arg1,arg2,arg3
arguement input: %g %g %g
arg1=arg1/2
arg2=arg2/2
arg3=arg3/2
/nerr,0,1e5
*get,_trimat,parm,ri_mat,type
*if,_trimat,ne,0,then
   ri_mat=2
*endif
*get,_tmagmat,parm,mag_mat,type
*if,_tmagmat,ne,0,then
   mag_mat=3
*endif
*get,_tstmat,parm,st_mat,type
*if,_tstmat,ne,0,then
   st_mat=5
*endif
*if,arg1,le,0,then
 *get,_trsthk,parm,r_stk,type
 *if,_trsthk,eq,0,then
    arg1=r_stk/2
 *endif
*endif
*if,arg2,le,0,then
 *get,_tmsthk,parm,m_stk,type
 *if,_tmsthk,eq,0,then
    arg2=m_stk/2
 *endif
*endif
*if,arg3,le,0,then
 *get,_tssthk,parm,s_stk,type
 *if,_tssthk,eq,0,then
    arg3=s_stk/2
 *endif
*endif



*if,arg1,le,0,then
  arg1 =1/2
*endif
*if,arg2,le,0,then
  arg2=arg1
*endif
*if,arg3,le,0,then
  arg3 =arg1
*endif
*if,arg4,le,0,then
  arg4 =1
*endif
_err=0
esel,none
cmsel,,rotor
*get,_ecnt,elem,,count
*if,_ecnt,eq,0,then
  /nerr
  *msg,error
  The ROTOR component does not exist or it has no elements
  _err=1
*endif

esel,none
cmsel,,stator
*get,_ecnt,elem,,count
*if,_ecnt,eq,0,then
  /nerr
  *msg,error
  The STATOR component does not exist or it has no elements
  _err=1
*endif

esel,,mat,,ri_mat
*get,_mat1,elem,,count
esel,,mat,,st_mat
*get,_mat2,elem,,count
esel,,mat,,mag_mat
*get,_mat3,elem,,count
*if,_mat1*_mat2*_mat3,le,0,then
  /nerr
  *msg,error
  At least one of the components has no elements: stator iron, rotor iron&
  or the rotor magnets.
  _err=1
*endif

/nerr,0,1e4
*if,_err,eq,1,:end



alls
ddel,all,all
sfdel,all,all
bfedel,all,all
cedel,all,all
cpdel,all,all

*get,_elmx1,elem,,num,max
*get,_xmax,node,,mxloc,x
_scup=0
*if,_xmax,lt,1,then
 csys
 nscal,,all,,,1/ggeom,1/ggeom
 _scup=1
*endif

!   original data
*set,_stka1
*dim,_stka1,,3,3
!   ordered data
*set,_stka2
*dim,_stka2,,3,3

!    unordered data-as input
! j             _stka1(i,j) 
! 1      material number of part
! 2      thickness of part
! 3      starting Z of the component
!
! i       component
! 1       rotor stack
! 2       magnet stack
! 3       stator stack
!
*get,_etyp,etyp,,num,max
_etyp=_etyp+1
et,_etyp,96

_stka1(1,1)=ri_mat,mag_mat,st_mat
_stka1(1,2)=arg1,arg2,arg3

_stka2(1,1)=ri_mat,mag_mat,st_mat
_stka2(1,2)=arg1,arg2,arg3


/com, order _stka2
*vfun,_stka2(1,2),asort,_stka2(1,2)
/com,check for duplicates
n_stk=3
*if,_stka2(1,2),eq,_stka2(2,2),then
 n_stk=2
 _stka2(2,2)=_stka2(3,2)
*endif
*if,_stka2(1,2),eq,_stka2(2,2),then
 n_stk=1
*endif


alls
*get,_ndmx0,node,,num,min

*do,_im3d1,1,2
 /gopr
 *msg,info,_im3d1
 cycle: %i

 *if,_im3d1,eq,1,then
  cmsel,,rotor
 *else
  cmsel,,stator
 *endif

 /com, the first axial extrusion

 cm,nbase,node
 cm,ebase,elem
 cmgrp,base,ebase,nbase


 nd_min=arg4

 /com, first call to e_exten.mac      
 e_exten,0,nd_min,,0,_stka2(1,2)/nd_min,_etyp
 /out,mach3d,out,,append
 cmsel,u,base
 cm,extr1e,elem
 cm,extr1n,node

 *if,n_stk,gt,1,then
  /com, 2 or greater extrusions
  nsel,all
  *get,_ndmx1,node,,num,max
  cmsel,,base
  _ndinc1=_ndmx1-_ndmx0+1
  csys
  ngen,2,_ndinc1,all,,,0,0,_stka2(1,2)
  egen,2,_ndinc1,all
  cmsel,u,base
  cm,nbase2,node
  cm,ebase2,elem
  cmgrp,base2,nbase2,ebase2
  _del1=_stka2(2,2)-_stka2(1,2)
  _ndiv2=2*nint(_del1/(_stka2(1,2)/nd_min))
  *if,_ndiv2,le,0,then
    _ndiv2=1
  *endif
  /com, second call to e_exten.mac  
  e_exten,0,_ndiv2,,0,_del1/_ndiv2,      _etyp

  /out,mach3d,out,,append
  cmsel,u,base2
  cm,extr2e,elem
  cm,extr2n,node
 *endif

 *if,n_stk,gt,2,then
  /com, 3 extrusions
  nsel,all
  *get,_ndmx2,node,,num,max
  cmsel,,base
  _ndinc2=_ndmx2-_ndmx0+1
  csys
  ngen,2,_ndinc2,all,,,0,0,_stka2(2,2)
  egen,2,_ndinc2,all
  cmsel,u,base
  cm,nbase3,node
  cm,ebase3,elem
  cmgrp,base3,nbase3,ebase3
  _del2=_stka2(3,2)-_stka2(2,2)
  _ndiv2=2*nint(_del2/(_stka2(2,2)/nd_min))
  *if,_ndiv2,le,0,then
    _ndiv2=1
  *endif
  /com, third call to e_exten.mac  
  e_exten,0,_ndiv2,,0,_del2/_ndiv2,_etyp

  /out,mach3d,out,,append
  cmsel,u,base3
  cm,extr3e,elem
  cm,extr3n,node
 *endif

 /com, generate the rest of the air elements
 *if,_im3d1,eq,1,then
  alls
  *get,_zmax,node,,mxloc,z
 *endif
 nsel,all
 *get,_ndmx3,node,,num,max
 cmsel,,base
 _ndinc3=_ndmx3-_ndmx0+1
 csys
 ngen,2,_ndinc3,all,,,0,0,_zmax
 egen,2,_ndinc3,all
 cmsel,u,base
 cm,nbase4,node
 cm,ebase4,elem
 cmgrp,base4,nbase4,ebase4
 _del3=_zmax-_stka2(3,2)

 *if,a_stk,gt,0,then
 _del3=a_stk
 _ndiv3=nint(_del3/(_stka2(3,2)/nd_min))
  *if,_ndiv3,le,0,then
    _ndiv3=1
  *endif
 /com, fourth call to e_exten.mac  
 e_exten,0,_ndiv3,,0,_del3/_ndiv3,_etyp       !   3/14/98
 /out,mach3d,out,,append
 cmsel,u,base4
 cm,extr4e,elem
 cm,extr4n,node
 emod,all,mat,1
 *endif

 ! emod,all,real,50   3/7/98
 *if,_im3d1,eq,1,then
   /com, definition of the rotor component group     
   alls
   cmsel,u,stator
   cm,n_rotor,node
   cm,e_rotor,elem
   cmgrp,rotor,n_rotor,e_rotor
 *else
   /com, definition of the stator component group     
   alls
   cmsel,u,rotor
   cm,n_stator,node
   cm,e_stator,elem
   cmgrp,stator,n_stator,e_stator
 *endif

*enddo  !  end of loop for rotor and the stator
cmsel,,rotor
numm,node
cmsel,,stator
numm,node
/com, all extrusions have been completed.
/com, reassigned the properties
!
esel,,mat,,ri_mat
nsle
nsel,r,loc,z,-1,_stka1(1,2)+.0001
esln,,1
cm,ce,elem
esel,,mat,,ri_mat
cmsel,u,ce
emod,all,mat,1
esel,,mat,,mag_mat
nsle
nsel,r,loc,z,-1,_stka1(2,2)+.0001
esln,,1
cm,ce,elem
esel,,mat,,mag_mat
cmsel,u,ce
emod,all,mat,1
esel,,mat,,st_mat
nsle
nsel,r,loc,z,-1,_stka1(3,2)+.0001
esln,,1
cm,ce,elem
esel,,mat,,st_mat
cmsel,u,ce
emod,all,mat,1
esel,,type,,_etyp
esel,inve
nsle
nsel,inve
edel,all
ndel,all
etdel,1,_etyp-1
alls
numc,type
numc,elem
numc,node

ddel,all,all
esel,,mat,,5
nsle
*get,_zstk,node,,mxloc,z
nsel,r,ext
nsel,r,loc,z,_zstk-.0001,_zstk+1
csys,1
*get,_thmin,node,,mnloc,y
*get,_thmax,node,,mxloc,y
avetht=.5*(_thmin+_thmax)
*get,_rdmx,node,,mxloc,x
nmagbc=node(_rdmx,avetht,_zstk)
d,nmagbc,mag
alls

*if,arg5,ne,0,then
  /com, use symmetry to build the other half
  nsel,all
  *get,ndmx,node,,num,max
  cmsel,,rotor
  *get,ndmn,node,,num,min
  ndinc=ndmx-ndmn+1
  csys
  nsymm,z,ndinc,all
  esymm,,ndinc,all
  numm,node
  numc,node
  numc,elem
  /com, definition of the rotor component group    
  cm,n_rotor,node
  cm,e_rotor,elem
  cmgrp,rotor,n_rotor,e_rotor
  nsel,all
  *get,ndmx,node,,num,max
  cmsel,,stator
  *get,ndmn,node,,num,min
  ndinc=ndmx-ndmn+1
  csys
  nsymm,z,ndinc,all
  esymm,,ndinc,all
  numm,node
  /com, definition of the stator component group
  cm,n_stator,node
  cm,e_stator,elem
  cmgrp,stator,n_stator,e_stator
*endif

mvbc3d   !  apply mvdi for torque calculation
mxbc3d

csys
alls
fini
/prep7
nscal,,all,,,ggeom,ggeom,ggeom
esel,,mat,,st_mat
cm,s_iron,elem
s_iron_c='s_iron'

esel,,mat,,2,3        !  2/25/01
cm,r_iron,elem        !  2/25/01

esel,all
:end
*get,_mcp2,active,,time,cpu
mdcpu=_mcp2-_mcp1
/out