TABLE OF CONTENTS


::pwtk::input::namelistAtmSymb2Ityp

SYNOPSIS

proc ::pwtk::input::namelistAtmSymb2Ityp {nmlName varName} {

USAGE

   ::pwtk::input::namelistAtmSymb2Ityp  namelistName  VarName

PURPOSE

Transform "ntyp"-type variable by relacing the atomic-label with atomic-index, e.g., "starting_magnetization(Fe)" will be transformed to "starting_magnetization(1)", where 1 is the index of Fe species.

multidimension arrays are also handled, e.g., starting_ns_eigenvalue(5,1,Fe1) is replaced by starting_ns_eigenvalue(5,1,1)

Beware that such a transformation should be done only during the input file creation.

RETURN VALUE

SOURCE

    global ::pwtk::ntyp_variables
    
    if { [info exists ::pwtk::ntyp_variables($nmlName)] \
             && $::pwtk::ntyp_variables($nmlName) } {
        
        # BEWARE:  for arrays: varName == var(index),        
        #          split var(index) to {var index}
        
        lassign [split $varName ()] var index

        if { $index eq "" } {
            # variable is not array
            return $varName
        }

        set usedSpecies [::pwtk::pwi::getAtomicLabels]
        set allSpecies  [::pwtk::pwi::getAllAtomicLabels]

        if { [info exists ::pwtk::ntyp_multidimensions($nmlName)] \
                 && $var in $::pwtk::ntyp_multidimensions($nmlName) } {                     

            # var is a multidimension
            
            set multiL [::pwtk::varvalue ::pwtk::ntyp_multidimensions($nmlName)]
            set dim [lindex $multiL [lsearch -nocase $multiL $var]+1]

            if { ! [string is integer -strict $dim] } {
                ::pwtk::error "::pwtk::ntyp_multidimensions list is malformed, check the respective config/*i.tcl config file" 1
            }
            
            set indL [split $index ,]
            set symb [lindex $indL $dim]
            
            if { $symb in $usedSpecies } {
                set ind [expr [lsearch $usedSpecies $symb] + 1]
                set newi [join [lreplace $indL $dim $dim $ind] ,]
                return ${var}($newi)

            } elseif { $symb in $allSpecies } {
                # "symb" is defined but not currently used, filter it out,
                # i.e., return an empty string
                return ""
                
            } else {
                # "symb" is not defined, signal an error
                ::pwtk::error "atomic species \"$symb\" is not defined in ATOMIC_SPECIES card" 1
            }                
        }        

        set ind [lsearch $usedSpecies $index]

        if { $ind == -1 } {
            if { $index in $allSpecies } {
                # we have variable(AtomicLabel), but the atomic label is
                # not used; filter it out, i.e., return nothing
                return ""
            } else {
                # perhaps we have variable(atomicLabel) and
                # atomicLabel is not even defined; note that Fortran
                # arrays use numeric indices, whereas atomicLabel is a
                # string, hence filter out variable(atomicLabel) if
                # atomicLabel is not a numeric index.
                #
                # BEWARE: Fortran indices can be "1,2" or "-1:5,-3:+4" !
                #
                set parsed [regsub -all {[,:+-]} $index {}]                
                if { ! [string is integer $parsed] } {
                    return ""
                }
            }
        } elseif { $ind > -1 } {
            incr ind
            return $var\($ind\)
        }     
    }
    
    return $varName            
}