! TITLE:    r_mass.mac (POST)
!
!   - added third argument array
!   - added reporting for full model    
!   - changed *return to return,,ar67 
! OBJECTIVE:
!
!  Report mass properties full model and components in array.
!
! COMMAND SYNTAX: 
! 
!             (1)     (2)
!   r_mass, 'fname', calc, 'array'
!
!   Note: arguments in single quotes are strings and must be entered in ' '.
!
! ARGUMENTS:
!   
!   'fname' = name of file created.  Assume .txt file extension.  Default
!             is r_mass.txt if left blank or a numeric scalar (e.g. if do not
!             put name in single quotes will be scalar).
!
!   calc = Controls the degree of tessellation used in the calculation of area 
!          properties as follows.
!          0 = default
!          1 = fine (Not generally recommended because takes longer and accuracy 
!                    very similarexcept for thin, hollow models)                 
!              If selected, macro provides more decimal places in output file.
!
!   array = 1-D character array containing component volume names of interest.
!           [default = 'model']
!
!
! DESCRIPTION:
!
!   Reports volume, density, mass, and centroid information for full model and
!   for components volumes defined in array.  
!   The default array is 'model' which is created via mod_assemb_prep command.  
!   - Can invoke before or after mesh. If invoke before mesh, will use material
!     info assigned to volume by VATT. If invoke after mesh, will uses material
!     assigned to mesh. 
!   - Assumes one material per part in component volume in array
!   - The second argument can be used to increase accuracy.  See VSUM command 
!     for further info on accuracy including thin hollow models and very thin 
!     volumes, such that the ratio of the minimum to the maximum dimension 
!     is less than 0.01.
!   - Geometry items can only be reported in the global Cartesian coordinate 
!     system due to limitations in VSUM. 
!     
!
! WARNING:
!   - cannot detect difference between ARG1 blank or if forgot single quotes
!     around name.
!   - will leave output as default output location 
!
*GET,nargs_,CMD,0,nargs

*GET,AR67,ACTIVE,,PRKEY
/NOPR

!- ARG1 parameter type
*GET,AR80,PARM,ARG1,TYPE
!  Parameter type: 0=scalar, 1=array, 2=table, 3=character scalar, 
!                  4=character array, -1=undefined Parameter
*IF,AR80,NE,3,THEN
   *MSG,WARN
   Output file will be r_mass.txt because ARG1 blank or %?&
   defined incorrectly (ex. forgot single quotes around name).
   ARG1='r_mass'
*ENDIF

!- ARG3 parameter type
*IF,nargs_,LT,4,THEN    !didn't define arg3
   arg3='model'   !default
*ENDIF

*GET,AR83,PARM,%ARG3%,TYPE
!  Parameter type: 0=scalar, 1=array, 2=table, 3=character scalar, 
!                  4=character array, -1=undefined Parameter
*IF,AR83,NE,4,THEN
   *MSG,WARN
   r_mass ARG3 was not defined correctly or the character %/&
   array does not exist. %/&
   Hint: need to put single quotes around character array name. %/&
   Command aborted.
   *set,nargs_
   return,,ar67
*ENDIF

fini
/prep7
allsel

!- create array with first row = 'allsel' rest with arg3 array appended
*DIM,tmp_arr,CHAR,1
 tmp_arr(1)='allsel'
 array_append,arg3,'tmp_arr'

*GET,AR40,PARM,tmp_arr,Dim,X
*DIM,r_mass,ARRAY,AR40,7        ! row = size or array or names.
                                ! column = volume, density, mass, mass*g_bin, CENTx, CENTy, CENTz

*DO,i,1,AR40
    *IF,i,EQ,1,THEN
       allsel
    *ELSE
       select_part,tmp_arr(i)
    *ENDIF

    *IF,ARG2,EQ,1,THEN
       vsum,fine
    *ELSE
       vsum,default
    *ENDIF
    *get,r_mass(i,1),VOLU,,VOLU
    *get,r_mass(i,5),VOLU,,CENT,x
    *get,r_mass(i,6),VOLU,,CENT,y
    *get,r_mass(i,7),VOLU,,CENT,z

    *get,emax_,ELEM,,Num,Max       !assume all component is same material
    *get,vmax_,VOLU,,Num,Max

    *get,vmat_,VOLU,vmax_,ATTR,MAT   !Get material attribute assigned to volume
    *IF,emax_,GT,0,THEN
       *get,emat_,ELEM,emax_,ATTR,MAT
       *get,r_mass(i,2),DENS,emat_
    *ELSE
       *IF,vmat_,GT,0,THEN
          *get,r_mass(i,2),DENS,vmat_
       *ELSE
          *MSG,WARN
          The density cannot be determined because the volume(s) %/&
          were not assigned a material or are not meshed. %/&
          %/&
          The command assumes that the max volume +/or element %/&
          represent all volumes and elements.
       *ENDIF
    *ENDIF
    r_mass(i,3)=r_mass(i,1)*r_mass(i,2)
    r_mass(i,4)=r_mass(i,3)*g_bin
*ENDDO

!-- Output r_mass array to text file 
*CFCLOS   !in case prior *CFOPEN
*IF,ARG2,EQ,1,THEN   !output with more accuracy

/OUTPUT,%ARG1%,txt
/COM,     -------       MASS PROPERTIES       ----------
/COM,
/COM,    Part          Volu           Dens         Vol*Dens     Vol*Dens*386
*VWRITE,tmp_arr(1),r_mass(1,1),r_mass(1,2),r_mass(1,3),r_mass(1,4)
("  "A10,"  "E13.6,"  "E13.6,"  "E13.6,"  "E13.6)

/COM,
/COM,    Part        CENTx       CENTy       CENTz
*VWRITE,tmp_arr(1),r_mass(1,5),r_mass(1,6),r_mass(1,7)
("  "A10,"  "E10.3,"  "E10.3,"  "E10.3)

/COM,
/COM,
/COM,Created via r_mass.mac
/output

*ELSE       !output with less accuracy

/OUTPUT,%ARG1%,txt
/COM,     -------       MASS PROPERTIES       ----------
/COM,
/COM,    Part        Volu        Dens      Vol*Dens   Vol*Dens*386
*VWRITE,tmp_arr(1),r_mass(1,1),r_mass(1,2),r_mass(1,3),r_mass(1,4)
("  "A10,"  "E10.3,"  "E10.3,"  "E10.3,"  "E10.3)

/COM,
/COM,    Part        CENTx       CENTy       CENTz
*VWRITE,tmp_arr(1),r_mass(1,5),r_mass(1,6),r_mass(1,7)
("  "A10,"  "E10.3,"  "E10.3,"  "E10.3)

/COM,
/COM,
/COM,Created via r_mass.mac
/output

*ENDIF  !(ARG2)

allsel

!CleanUp
*set,r_mass(1)
*set,tmp_arr(1)
*set,emax_
*set,vmax_
*set,vmat_
*set,emat_
*set,nargs_
*set,i

*IF,AR67,EQ,1,THEN
  /GOPR
*ENDIF
/eof