!   TITLE:  cpl_nodes.mac (LOADS)
!
!
!   This sub-macro couples a user specified set of degrees of freedom (DOF)
!   of two sets of surface nodes from two separate parts.  The surfaces must
!   be coplanar, coincident or separated by a given distance within a specified
!   tolerance.
!
!   This macro may be used to couple the DOF of nodes from two separate and
!   different mesh layout patterns on any 2 given surfaces, allowing coupling
!   of non-coincident nodes.  This is achieved by specifying a cubical tol-
!   erance band around each node, with each node located at the center of
!   the 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 depicted in figure 1.
!
!                            ___________________
!                           |       TOL         | TOL/2
!                           |                   |
!                           |       y ^         |
!                           |         |         |
!                           |         |         |
!                           |         *---> x   | TOL
!                           |      source       |
!                           |       node        |
!                           |                   |
!                           |                   |
!                           |___________________| -TOL/2
!                        -TOL/2               TOL/2
!
!                       FIGURE 1.  Tolerance band
!
!   Of the two surfaces, one surface (set of areas) is specified as the source
!   area(s), and the other set target area(s).  The area(s) of each surface are
!   stored in a component group, and those names are entered in as arguments
!   as specified below.  The algorithm then takes each node of the source
!   area and checks for the nearest node in the target area within the tolerance
!   band. Any nodes from the target area that are within the tolerance region
!   in figure 1 will be considered for coupling.  If there is more than 1 node
!   in this region, the algorithm will couple the source node with a target node
!   that is the least distance from it.
!
!   The input for this command macro must be carefully defined SOURCE and TARGET
!   area sets.  These area sets must be component groups with distinct user de-
!   fined names. Once the area sets are defined, then the input arguments to be
!   supplied to this macro may be input as follows:
!
!
!       (1) source_aset = component group character name of area(s) com-
!                         prising the source area.
!
!       (2) target_aset = component group character name of area(s) com-
!                         prising the target area.
!
!       (3) node_cs = the coordinate system in which the nodal coordinates
!                     are to be measured from. The local coordinate system
!                     created at each node is a cartesian coordinate sys-
!                     tem from which the tolerance band is created, and
!                     is created from NODE_CS.
!
!       (4) tol = the total width of the tolerance bands for considering a
!                 node for coupling.
!
!       (5) dof_lab = the DOF label for the coupling. (e.g., UX, UY, UZ,
!                     ROTX, ALL, ....) See the CP command help for all
!                     options.
!
!
!
!                (1)          (2)        (3)    (4)    (5)
! CPL_NODES, source_aset, target_aset, node_cs, tol, dof_lab
!
!
!
*get,prkey_,active,0,prkey
/nopr

s_aset=arg1
t_aset=arg2
n_cs=arg3
tol_=arg4
dof_lab=arg5
!
!
csys,n_cs
!
cmsel,s,%s_aset%
nsla,s,1
nrotat,all
cm,s_nset,node
!
cmsel,s,%t_aset%
nsla,s,1
nrotat,all
cm,t_nset,node
!
nsel,s,node,,s_nset
entity_array,'n','s_set','array'
s_cnt=ecount
!
*do,qrs_,1,s_cnt,1
    !
    csys,n_cs
    !
    x_=nx(s_set(qrs_,1))
    y_=ny(s_set(qrs_,1))
    z_=nz(s_set(qrs_,1))
    !
    create_cs,0,,0,x_,y_,z_,0,0,0
    !
    nsel,s,node,,t_nset
    nsel,r,loc,x,-tol_/2,tol_/2
    nsel,r,loc,y,-tol_/2,tol_/2
    nsel,r,loc,z,-tol_/2,tol_/2
    !
    nsel,a,node,,s_set(qrs_,1)      ! select source node
    s_node=s_set(qrs_,1)
    !
    *get,ncnt_,node,,count          ! node count parameter
    !
    *if,ncnt_,ge,2,then
        !
        nsel,u,node,,s_node
        !
        *if,qrs_,eq,1,then
            entity_array,'n','msr_set','array'
        *else
            *get,arr_chk1,parm,msr_set,type
            !
            *if,arr_chk1,eq,1,then
                *set,msr_set(1),
            *endif
            !
            entity_array,'n','msr_set','array'
        *endif
        !
        msr_cnt=ecount
        !
        nsel,a,node,,s_node
        !
        *if,qrs_,eq,1,then
            *dim,dist_,array,msr_cnt
        *else
            *get,arr_chk2,parm,dist_,type
            !
            *if,arr_chk2,eq,1,then
                *set,dist_(1),
            *endif
            !
            *dim,dist_,array,msr_cnt
        *endif
        !
        *do,ijk_,1,msr_cnt,1
            dist_(ijk_,1)=distnd(s_node,msr_set(ijk_,1))
        *enddo
        !
        min_value,'dist_',msr_cnt
        !
        nsel,s,node,,msr_set(min_indx,1)
        nsel,a,node,,s_node
        !
        cp,next,%dof_lab%,all
        !
        nsel,s,node,,t_nset
        nsel,u,node,,msr_set(min_indx,1)
        cm,t_nset,node
    *endif
*enddo
!
allsel,all
!
*set,qrs_,
*set,arr_chk1,
*set,arr_chk2,
*set,s_cnt,
*set,msrcnt,
*set,s_node,
*set,n_cs,
*set,dof_lab,
*set,x_,
*set,y_,
*set,z_,
*set,s_set(1),
*set,msr_set(1),
*set,dist_(1),
cmdele,%s_aset%
cmdele,%t_aset%
*set,s_aset,
*set,t_aset,
*set,s_nset,
*set,t_nset,

*if,prkey_,eq,1,then
    /go
*endif