!   TITLE:  cpl_nodes_areas.mac (LOADS)
!
!
!   OBJECTIVE:
!
!   This sub-macro couples the specified degrees of freedom (DOF) of nodes
!   on the surface of a selected, or specified, set of areas.
!
!
!   COMMAND SYNTAX:
!
!                            (1)        (2)    (3)     (4)
!       CPL_NODES_AREAS, 'comp_aset', node_cs, tol, 'dof_lab'
!
!
!   ARGUMENTS:
!
!       (1) comp_aset = component group character name containing the areas.
!                       [MUST BE IN SINGLE QUOTES]
!
!       (2) node_cs = the coordinate system in which the nodal coordinates
!                     are to be rotated to. The local coordinate system
!                     created at each node is a cartesian coordinate sys-
!                     tem from which the tolerance band is created, and
!                     is created parallel to the NODE_CS.
!
!       (3) tol = the total width of the tolerance bands for considering a
!                 node for coupling.
!
!       (4) dof_lab = Coupling DOF label. (e.g., UX, UY, UZ, ROTX, ALL, etc.)
!                     If left blank, default lable is 'ALL'.
!                     [MUST BE IN SINGLE QUOTES]
!                     (See the CP command help for all options.)
!
!
!   DESCRIPTION:
!
!   This macro is used to couple the DOF's of coincident and/or non-coincident
!   nodes from two or more separate areas. The areas must contain nodes that
!   are within a distance as specified by the tolerance value (argument 3).
!
!   A cubical tolerance band is calculated around each node, with the nodes
!   located at the center of a cube. The tolerance band width argument, TOL,
!   is defined as the total width of the band around a node, as measured from
!   a cartesian c.s. centered at the node location. This is shown in figure 1.
!                            ___________________
!                           |   TOL             | TOL/2
!                           |       y ^         |
!                           |         |         |
!                           |         |         |
!                           |         *---> x   | TOL
!                           |      node         |
!                           |                   |
!                           |                   |
!                           |___________________| -TOL/2
!                        -TOL/2               TOL/2
!
!                       FIGURE 1.  Tolerance band
!
!   The areas may be specified in a component group (argument 1), or just the
!   current selected set of areas (argument 1 left blank).
!
!   The algorithm iterates through each node of the areas and couples the
!   specified DOF's (argument 4) with all nodes that are within the tolerance
!   band.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
cmpaset_=arg1
ncs_=arg2
tol_=arg3
doflab_=arg4
!
*get,argtype_,parm,cmpaset_,type
!
*if,argtype_,eq,0,then
    *if,cmpaset_,eq,0,then
        cmpaset_='cmpaset'
        cm,%cmpaset_%,area
    *endif
*endif
!
csys,ncs_
!
cmsel,s,%cmpaset_%
nsla,s,1
nrotat,all
cm,cmpnset_,node
entity_array,'n','nset_','array'
ncnt_=ecount
!
axdist_=(1/2)*tol_
ctcs_=csn+10
!
*do,qrs_,1,ncnt_,1
    *if,nset_(qrs_,1),gt,0,then
        csys,ncs_
        !
        clocal,ctcs_,0,nx(nset_(qrs_,1)),ny(nset_(qrs_,1)),nz(nset_(qrs_,1))
        !
        cmsel,s,cmpnset_
        !
        nsel,r,loc,x,-axdist_,axdist_
        nsel,r,loc,y,-axdist_,axdist_
        nsel,r,loc,z,-axdist_,axdist_
        !
        *get,numnode_,node,,count
        !
        *if,numnode_,gt,1,then
            !
            cp,next,%doflab_%,all
            !
            *get,parmtyp_,parm,nrmvset_,type
            !
            *if,parmtyp_,eq,1,then
                *set,nrmvset_(1),
            *endif
            !
            entity_array,'n','nrmvset_','array'     ! store nodes within the band
            numnode_=ecount                         ! # of nodes within the band
            !
            *do,ijk_,1,numnode_
                arry_search,'nset_',nrmvset_(ijk_,1)
                !
                row__=hit(1,1)
                col__=hit(1,2)
                !
                nset_(row__,col__,1)=0
                !
                *set,hit(1),
                *set,row__,
                *set,col__,
            *enddo
        *endif
    *endif
*enddo
!
csdele,ctcs_
!
cmdele,cmpnset_
cmdele,cmpaset_
!
init_entity_num,1
!
*set,ctcs_,
*set,doflab_,
*set,ecount,
*set,ijk_,
*set,ncs_,
*set,qrs_,
*set,cmpaset_,
*set,ncnt_,
*set,cmpnset_,
!*set,nset_(1),
*set,axdist_,
*set,tol_,
*set,parmtyp_,
*set,argtype_,
*set,nrmvset_(1),
!
*if,prkey_,eq,1,then
    /go
*endif