# Demo entry 6768199

**center of mass**

Submitted by **anonymous**
on Nov 02, 2018 at 07:03

Language: Tcl. Code size: 1.1 kB.

proc center_of_mass {selection} { # some error checking if {[$selection num] <= 0} { error "center_of_mass: needs a selection with atoms" } # set the center of mass to 0 set com [veczero] # set the total mass to 0 set mass 0 # [$selection get {x y z}] returns the coordinates {x y z} # [$selection get {mass}] returns the masses # so the following says "for each pair of {coordinates} and masses, # do the computation ..." foreach coord [$selection get {x y z}] m [$selection get mass] { # sum of the masses set mass [expr $mass + $m] # sum up the product of mass and coordinate set com [vecadd $com [vecscale $m $coord]] } # and scale by the inverse of the number of atoms if {$mass == 0} { error "center_of_mass: total mass is zero" } # The "1.0" can't be "1", since otherwise integer division is done return [vecscale [expr 1.0/$mass] $com] }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.