!   TITLE:  ln_size.mac (MESH)
!
!
!   OBJECTIVE:
!
!   This sub-macro sizes a set of lines for element divisions.
!
!
!   COMMAND SYNTAX:
!
!                  (1)     (2)     (3)       (4)      (5)
!        LN_SIZE, esize, mshtyp, selctopt, mshdens, lresize
!
!
!   ARGUMENTS:
!
!       (1) esize = element (minimum) edge length
!
!       (2) mshtyp = size lines for mapped or free meshing:
!
!               0 (or blank) = free mesh
!
!               1 = map mesh
!
!       (3) selctopt = line selection options (if not left blank, options
!                      specified below must be in single quotes):
!
!               'l' (or blank) = currently selected set of lines (only free
!                                meshing can be performed with this option)
!
!               'a' = lines attached to the currently selected set of areas
!
!               'array_name' = lines attached to the set of areas in the array
!                              whose name is specified in this argument
!
!       (4) mshdens = mesh density.  If the desired mesh density is coarse or
!                     fine, then this parameter will create the desired mesh.
!
!                 If the desired mesh density is fine, the line sizing
!                 algorithm will size all the lines that are less than or
!                 equal to 2.5 times the minimum element edge length, ESIZE,
!                 to having only 1 element along its edge length.
!
!                 If the desired mesh density is coarse, the line sizing
!                 algorithm will size all the lines that are less than or
!                 equal to 3 times the minimum element edge length, ESIZE,
!                 to having 2 or 3 elements along its edge length, depending
!                 on the algorithm criteria.
!
!                 This will aid in selecting the proper mesh relevant to the
!                 type of analysis being performed (e.g., in stress analysis,
!                 the desired mesh density may need to be fine; in dynamic
!                 analysis, it may suffice that the required mesh density to
!                 be coarse).
!
!                       0 (or blank) = fine mesh density [default]
!
!                       1 = coarse mesh density
!
!                       n = user defined divisions for all lines less
!                           than or equal to 3.0*ESZ_.
!
!                 If MSHDENS is assigned any numerical value (n) other
!                 than 0 or 1, then any line that is less than or equal
!                 to 3 times ESIZE is sized that numerical value line
!                 divisions.
!
!       (5) lresize = line resize parameter:
!
!               0 (or blank) = if a line is already sized, do not resize it
!
!               1 = resize line regardless if it is already sized
!
!
!   DESCRIPTION:
!
!   The lines may be selected by selecting the set of areas that they are
!   attached to. The areas may be stored in a pre-existing array and may be
!   accessed through the array (the array must have each area stored in each
!   row, but all in the 1st column). The desired lines for sizing may be sel-
!   ected by simply selecting the lines. The options for line selection are
!   outlined in argument 3 above.
!
!   The lines are sized according to a specified element edge length. The
!   number of divisions is calculated for each line using this edge length.
!
!   The lines may be specified for either map meshing or free meshing. The
!   selected areas must meet the criteria for map meshing, i.e., a 3 or 4
!   sided polygon, where the opposite sides must have equal divisions.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
cm,CL01_,line
cm,CA01_,area
cm,CV01_,volu
!
esz_=arg1
mshtyp_=arg2
selopt_=arg3
mshdens_=arg4
lresize_=arg5
!
*if,mshdens_,eq,1,then
     nelm_1=1
     nelm_2=1
     mshfctr_=2.75
*elseif,mshdens_,eq,0,then
     nelm_1=2
     nelm_2=3
     mshfctr_=2.5
*else
     nelm_1=mshdens_
     nelm_2=mshdens_
     mshfctr_=3.0
*endif
!
*if,esz_,eq,0,then
    esz_=0.500
*endif
!
*get,optyp_,parm,selopt_,type
!
*if,optyp_,eq,0,then
    selopt_='l'
    mshtyp_=0
*elseif,optyp_,eq,3,then
    *if,selopt_,eq,'l',then
        mshtyp_=0
    *endif
*endif
!
*if,mshtyp_,ne,1,then
    *if,selopt_,eq,'a',then
        lsla,s
    *elseif,selopt_,ne,'l',then
        *get,xdim_,parm,%selopt_%,dim,x
        !
        *do,jj_,1,xdim_,1
            *if,jj_,eq,1,then
                asel,s,area,,%selopt_%(jj_,1)
            *else
                asel,a,area,,%selopt_%(jj_,1)
            *endif
        *enddo
        !
        lsla,s
    *endif
    !
    entity_array,'l','lsiz','array'
    !
    *do,jj_,1,ecount,1
        !
        *get,lleng_,line,lsiz(jj_,1),leng
        !
        *if,lleng_,le,2.0*esz_,then
            nelm_=nelm_1
        *elseif,lleng_,le,mshfctr_*esz_,then
            nelm_=nelm_2
        *else
            nelm_=nint(lleng_/esz_)
        *endif
        !
        *get,lszchk_,line,lsiz(jj_,1),attr,ndnx
        !
        *if,lszchk_,eq,0,then
            lesize,lsiz(jj_,1),,,nelm_,1,
        *else
            *if,lresize_,eq,1,then
                lesize,lsiz(jj_,1),,,nelm_,1,
            *endif
        *endif
    *enddo
    !
    *set,lsiz(1),
*elseif,mshtyp_,eq,1,then
    *if,selopt_,eq,'a',then
        entity_array,'a','msha_','array'
        acount_=ecount
        selopt_='msha_'
    *else
        *get,acount_,parm,%selopt_%,dim,x
    *endif
    !
    *do,jj_,1,acount_,1
        !
        asel,s,area,,%selopt_%(jj_,1)
        lsla,s
        *get,lcount_,line,,count
        !
        *if,lcount_,eq,3,then
            max_enty_dim,'l'
            !
            *if,max_leng,le,2.0*esz_,then
                nelm_=nelm_1
            *elseif,max_leng,le,mshfctr_*esz_,then
                nelm_=nelm_2
            *else
                nelm_=nint(max_leng/esz_)
            *endif
            !
            *if,mod(nelm_,2),ne,0,then
                nelm_=nelm_+1
            *endif
            !
            lesize,all,,,nelm_,1,
        *elseif,lcount_,gt,3,then
            max_e_dim_array,'l','lsiz','array'
            !
            hihld_=lsizc(max_indx,1)
            hilin_=lsiz(max_indx,1)
            !
            frsthld_=lsizc(1,1)
            frstlin_=lsiz(1,1)
            !
            lsizc(1,1)=hihld_
            lsiz(1,1)=hilin_
            !
            lsizc(max_indx,1)=frsthld_
            lsiz(max_indx,1)=frstlin_
            !
            *do,ii_,1,2,1
                *if,ii_,eq,1,then
                    *if,lsizc(ii_,1),le,2.0*esz_,then
                        nelm_=nelm_1
                    *elseif,lsizc(ii_,1),le,mshfctr_*esz_,then
                        nelm_=nelm_2
                    *else
                        nelm_=nint(lsizc(ii_,1)/esz_)
                    *endif
                    !
                    lesize,lsiz(ii_,1),,,nelm_,1,
                *else
                    *if,oppside_,eq,2,then
                        opphld_=lsizc(oppside_,1)
                        opplin_=lsiz(oppside_,1)
                        !
                        thrdhld_=lsizc(3,1)
                        thrdlin_=lsiz(3,1)
                        !
                        lsizc(oppside_,1)=thrdhld_
                        lsiz(oppside_,1)=thrdlin_
                        !
                        lsizc(3,1)=opphld_
                        lsiz(3,1)=opplin_
                    *endif
                    !
                    *if,lsizc(ii_,1),le,2.0*esz_,then
                        nelm_=nelm_1
                    *elseif,lsizc(ii_,1),le,mshfctr_*esz_,then
                        nelm_=nelm_2
                    *else
                        nelm_=nint(lsizc(ii_,1)/esz_)
                    *endif
                    !
                    lesize,lsiz(ii_,1),,,nelm_,1,
                *endif
                !
                *do,kk_,ii_,4,1
                    *if,kk_,eq,ii_,then
                        *get,k1_,line,lsiz(kk_,1),kp,1
                        *get,k2_,line,lsiz(kk_,1),kp,2
                    *endif
                    !
                    *if,kk_,ne,ii_,then
                        *get,k%kk_+1%_,line,lsiz(kk_,1),kp,1
                        *get,k%kk_+2%_,line,lsiz(kk_,1),kp,2
                        !
                        *if,k%kk_+1%_,ne,k1_,then
                            *if,k%kk_+1%_,ne,k2_,then
                                *if,k%kk_+2%_,ne,k1_,then
                                    *if,k%kk_+2%_,ne,k2_,then
                                        lesize,lsiz(kk_,1),,,nelm_,1,
                                        oppside_=kk_
                                    *endif
                                *endif
                            *endif
                        *endif
                    *endif
                *enddo
            *enddo
            !
            *set,lsizc(1),
            *set,lsiz(1),
        *endif
    *enddo
*endif
!
cmsel,s,CL01_
cmsel,s,CA01_
cmsel,s,CV01_
!
lplot
!
*set,ii_,
*set,jj_,
*set,kk_,
*set,esz_,
*set,lresize_,
*set,nelm_,
*set,nelm_1,
*set,nelm_2,
*set,mshfctr_,
*set,ecount,
*set,acount_,
*set,lcount_,
*set,opphld_,
*set,opplin_,
*set,thrdhld_,
*set,thrdlin_,
*set,oppside_,
*set,selopt_,
*set,k1_,
*set,k2_,
*set,mshtyp_,
*set,mshdens_,
*set,frstlin_,
*set,frsthld_,
*set,hihld_,
*set,hilin_,
*set,lleng_,
*set,optyp_,
*set,max_leng,
*set,xdim_,
cmdele,CL01_
cmdele,CA01_
cmdele,CV01_
!
*if,prkey_,eq,1,then
    /go
*endif