!   TITLE:  coil.mac (PART_LIB)
!
!
!   This macro creates a circular cross-section coil spring.  Parameter
!   values can be adjusted to vary the:
!
!               a.) pitch
!               b.) coil radius
!               c.) wire radius
!               d.) number of coils
!
!       ctclcs:     Cartesian Coil Coordinate System
!       cylclcs:    Cylindrical Coil Coordinate System
!       sphclcs:    Spherical Coil Coordinate System
!
!
!******************************* TABLE OF CONTENTS **************************
!
!       Model Assembly Preparation
!
!   1.  Parameter List
!   2.  Model Coordinate System
!   3.  Solid Model
!
!
!************************** Model Assembly Preparation **********************
!
!
mod_assemb_prep                 ! prepare database for assembly
!
!
/title,Coil Spring
!
!
/units,bin
!
!
!<<<<<<<<<<<<<<<<<<<<<<<< 1.  Parameter List >>>>>>>>>>>>>>>>>>>>>>>>>>
!
!
*ask,rcoil,coil radius,5
*ask,pitch,coil pitch,3
*ask,rwire,wire radius,0.75
*ask,cycle,number of turns,5
!
!
!<<<<<<<<<<<<<<<<<<<< 2.  Model Coordinate Systems >>>>>>>>>>>>>>>>>>>>
!
!
mod_coord_system,0,1,2,3,0,0,0
!
!
!<<<<<<<<<<<<<<<<<<<<<<<<<<< 3.  Solid Model >>>>>>>>>>>>>>>>>>>>>>>>>>
!
!
k=pitch/(4*rtangle)
thetamax=cycle*(4*rtangle)
inclang=(atan(pitch/(2*pi*rcoil)))*dconv
s=2*rwire*sin(inclang*rconv)
phi=((s/rcoil)/2)*dconv
lcount=0
!
csys,cs1_
!
*do,theta,0,thetamax,rtangle
    k,,rcoil,theta,k*theta
*enddo
!
init_entity_num
!
*do,i,(km-(thetamax/rtangle)),km-1,1
    l,i,i+1
    lcount=lcount+1
*enddo
!
init_entity_num
!
ckp=km-(thetamax/rtangle)
!
create_cs,0,'cs_cs',1,kx(ckp),ky(ckp),kz(ckp),0,rtangle+inclang,0
!
*do,i,0,3*rtangle,rtangle
    k,,rwire,i,0
*enddo
!
init_entity_num
!
csys,0      ! remove for circular cross-section
a,km-3,km-2,ckp
a,km-2,km-1,ckp
a,km-1,km,ckp
a,km,km-3,ckp
!
csys,cs1_
!
*do,i,0,((4*cycle)-1),1
    beta=mod((rtangle*i),4*rtangle)
    asel,selv,loc,y,beta-phi,beta+phi
    asel,r,loc,x,(rcoil-rwire),(rcoil+rwire)
    asel,r,loc,z,(k*rtangle*i-rwire),(k*rtangle*i+rwire)
    vdrag,all,,,,,,lm-(lcount-1-i),
*enddo
!
init_entity_num
!
lplot