TABLE OF CONTENTS


::pwtk::barycenter

SYNOPSIS

proc ::pwtk::barycenter {atmPos {listOfAtoms {}}} {

PURPOSE

Return the barrycenter of atomic positions "atmPos". If the optional 'listOfAtoms' argument is specified, then only the barycenter of the specified atoms is calculated.

ARGUMENTS

SOURCE

    set atmPos [purifyCoor $atmPos]
    set nat    [getNAtoms $atmPos]

    # get the list of atoms for which to calculate the barycenter
    
    if { $listOfAtoms ne {} } {
        set sortedList [lsort -integer -increasing [::pwtk::parseRangeString $listOfAtoms $nat]]
    } else {
        set sortedList [seq 1 $nat]
    }

    # check that indices are in range

    foreach index $sortedList { ::pwtk::int_in_range1_ $index $nat }

    # get the listed atoms

    set atmPosList [split $atmPos \n]    
    foreach ind $sortedList { lappend coorL [lindex $atmPosList $ind-1] }

    # calculate the barycenter
    
    lassign {0. 0. 0.} mx my mz
    unset nat 
    foreach atom $coorL {
        lassign $atom atm x y z
        set mx [expr $mx + $x]
        set my [expr $my + $y]
        set mz [expr $mz + $z]
        incr nat
    }
    ifset nat 1; # just in case if $atmPos == {}
    return [lmap m "$mx $my $mz" {expr $m/double($nat)}]
}