From: Axel Kohlmeyer (akohlmey_at_cmm.chem.upenn.edu)
Date: Wed Apr 23 2008 - 06:58:42 CDT

On Wed, 23 Apr 2008, politr_at_huji.ac.il wrote:

> Dear all,

dear anonymous,

> frames and after that it stuck the computer. I have tried to monitor the
> script performance with top and I have discovered that the script is using to
> much memory and at some point I'm out of the memory. My script is attached. I

please have a look at for common problems and solutions at:

http://biocore.ks.uiuc.edu/biocore/biofs/VMD%20(Public)/tips/tclscripts.html

> will appreciate it very much if anyone could help. Any help is more than
> welcome.
> Thank you in advance.
>
> mol load psf $psf_filename

mol load is obsolecent. please use mol new instead.

> animate read dcd $dcd_filename waitfor all

same here. please use mol addfile instead.

> set n [molinfo top get numframes]
> set all [atomselect top all]
>
> set total_wat [atomselect top "resname TIP3 and name OH2 "]
>
> #rotate through all the frames
> for { set i 0 } { $i < $n } { incr i 10} {
>
> $all frame $i
> $all update
> set mol_ID [molinfo top]
> molinfo $mol_ID set frame $i
> set box [molinfo $mol_ID get {a b c}]
> set length [lindex $box 0]
>
> set max_length [expr $length/2.0-3.0]
>
> set all [atomselect top all]

why define a new selection here? you already
defined all outside the loop and moved it to
the current frame. this is the reason of your
memory leak.

> set center [measure center $all]
> set cen_x [lindex $center 0]
> set cen_y [lindex $center 1]
> set cen_z [lindex $center 2]
> $total_wat frame $i
> $total_wat update

no need to update this selection. atom names and idices don't change.

> #to create list of resids. after that we can go through all coord and
> through all resids i
> n foreach loop (if we need to print resid)
> set wat_resid [$total_wat get resid]
>
> foreach water $wat_resid {
> set wat_coord [atomselect top "name OH2 and resid $water"]
> set xyz [$wat_coord get {x y z}]
> set wat_x [lindex [lindex $xyz 0] 0]
> set wat_y [lindex [lindex $xyz 0] 1]
> set wat_z [lindex [lindex $xyz 0] 2]
> set x [expr $wat_x-$cen_x]
> set y [expr $wat_y-$cen_y]
> set z [expr $wat_z-$cen_z]
> set distance [expr { sqrt( $x * $x + $y * $y + $z*$z) }]
> if {$distance <= $max_length} {
> #this part is used to find 4 closest water molecules
> set max 6
> set min [expr $max/2.0]
> set smalest 0
> set close_wat [atomselect top "name OH2 and within $min of (resid
> $water and name OH2)"]
> set ntot [$close_wat num]
> while {$ntot != 5} {
>
> if {$ntot > 5} {
> set max $min
> set min [expr $smalest + ($max-$smalest)/2.0]
> }
> if {$ntot < 5} {
> set smalest $min
> set min [expr $min + ($max-$min)/2.0]
> }
> set close_wat [atomselect top "name OH2 and within $min of
> (resid $water and name OH2)

       again you are creating a new selection without deleting
       the previous one. also there are more efficient ways of
       determining the 5 closest atoms. pick a larger enough distance.
       compute a list of the distances and pick the 5 shortest.

> "]
> set ntot [$close_wat num]
> }
> #find the center atom in list of atoms
>
> for { set j 0 } { $j < 5 } { incr j } {
>
> set resid [lindex [$close_wat get resid] $j]
> if {$resid == $water} {
> set c_atm $j
> }
>
> }
> set sum 0.0
> for { set k 0 } { $k < 4 } { incr k } {
> for { set l [expr $k+1] } { $l < 5 } { incr l } {
> if {$k != $c_atm && $l != $c_atm} {
> set atom1 [lindex [$close_wat get {x y z}] $k]
> set atom2 [lindex [$close_wat get {x y z}] $c_atm]
> set atom3 [lindex [$close_wat get {x y z}] $l]
> set angle [angle $atom1 $atom2 $atom3]
> set sum [expr {(($angle+1.0/3.0)*($angle+1.0/3.0))+$sum}]
>
> }
> }
> }
>
> set q [expr {1.0-((3.0/8.0)*$sum)}]
> puts $q_file "$q"
>
>
>
>
>
> }
>
> }
> puts $q_file "ENDMDL"
> }
> close $q_file

yep. plenty of selections created, none deleted -> memory leak.

cheers,
    axel.

>
>
>
>
>
>
>
>
>

-- 
=======================================================================
Axel Kohlmeyer   akohlmey_at_cmm.chem.upenn.edu   http://www.cmm.upenn.edu
    Center for Molecular Modeling   --   University of Pennsylvania
Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
tel: 1-215-898-1582,  fax: 1-215-573-6233,  office-tel: 1-215-898-5425
=======================================================================
If you make something idiot-proof, the universe creates a better idiot.