From: Axel Kohlmeyer (akohlmey_at_cmm.chem.upenn.edu)
Date: Sat Dec 08 2007 - 16:03:07 CST

On Sat, 8 Dec 2007, Manali Mehendale wrote:

MM> Hi,
MM>
MM> I tried changing my script in the following way, but it still doesnt get
MM> past ~800 molecules. Can you please let me know specifically where the
MM> problem is and how i can change it ?
MM> Thanks in advance.
MM> Manali
MM>
MM> set outfile1 [open length1.dat w];
MM> foreach filename [lsort -dictionary [glob *.mol2 ]] {
MM> mol load mol2 $filename
MM> set distAB 0
MM> set sel [atomselect top all]
MM> set s [$sel get index]
MM> set length [llength $s]
MM>
MM> for {set i 0 } {$i < $length } { incr i} {
MM> for { set j 0 } { $j < $length} {incr j} {
MM>
MM> set sel1 [atomselect top "index $i"]
MM> set sel2 [atomselect top "index $j"]

here you are creating new atomselect functions
in each iteration. the variables $sel1 / $sel2
only store the name of the atomselect _not_
the function itself. this is like doing

ptr = malloc(size);

MM> set A [lindex [$sel1 get { x y z}] 0]
MM> set B [lindex [$sel2 get { x y z}] 0]
MM>

but you don't put a "$sel1 delete ; $sel2 delete"
here, which would be equivalent to a: free(ptr).

this is the reason for your memory leak.

MM> lappend distAB [vecdist $A $B]
MM> }
MM> }

apart from that, you can achieve the whole thing
in a _much_ faster fashion and without creating
many selections:

set outfile1 [open "length1.dat" w]
foreach filename [lsort -dictionary [glob *.mol2]] {
  set mol [mol new $filename waitfor all] ; # note "mol load" is obsolete
  set distAB {}
  set sel [atomselect $mol all]
  set allcoord [$sel get {x y z}]

  foreach i $allcoord {
    foreach j $allcoord {
      lappend distAB [vecdist $i $j]
    }
  }
  puts $outfile1 "[lindex [lsort -decreasing $disAB] 0]"
  $sel delete
  mol delete $mol
}
close $outfile1

cheers,
   axel.

MM> set result [lindex [lsort -decreasing $distAB] 0]
MM> puts $outfile1 $result
MM> $sel delete
MM> $sel1 delete
MM> $sel2 delete
MM> mol delete all
MM> }
MM> close $outfile1
MM>
MM>
MM>
MM>
MM> On Fri, 7 Dec 2007, Axel Kohlmeyer wrote:
MM>
MM> > On Dec 7, 2007 2:15 PM, Manali Mehendale <manali_at_adrik.bchs.uh.edu> wrote:
MM> > > Hi,
MM> > >
MM> > > I am trying to calculate the length of multiple (thousands) small
MM> > > molecules (upto 100 atom length).
MM> > > My script works perfect for few molecules but when i try to process
MM> > > thousands it takes up too much memory and freezes up the system.
MM> > > Any help will be greatly appreciated.
MM> >
MM> > manali,
MM> >
MM> > please check out the tips and hints at:
MM> > http://biocore.ks.uiuc.edu/biocore/biofs/VMD%20(Public)/tips/tclscripts.html
MM> >
MM> > your script is creating many atom selections and not deleting them
MM> > => memory leak
MM> > => freeze due to excessive swapping
MM> >
MM> > axel.
MM> >
MM> > >
MM> > > Thanks,
MM> > > Manali
MM> > >
MM> > >
MM> > > The script is as follows:
MM> > >
MM> > > set outfile1 [open length.dat w];
MM> > > set m 0
MM> > > foreach filename [lsort -dictionary [glob *.mol2]] {
MM> > > puts $filename
MM> > > mol new $filename type mol2 waitfor all
MM> > > molinfo top set drawn 0
MM> > > incr m 1
MM> > > }
MM> > >
MM> > > for {set k 0 } {$k < 4000 } { incr k} {
MM> > >
MM> > > set distAB 0
MM> > > set sel [atomselect $k all]
MM> > > set s [$sel get index]
MM> > > set length [llength $s]
MM> > >
MM> > > for {set i 0 } {$i < $length } { incr i} {
MM> > > for { set j 0 } { $j < $length} {incr j} {
MM> > >
MM> > > set sel1 [atomselect $k "index $i"]
MM> > > set sel2 [atomselect $k "index $j"]
MM> > > set A [lindex [$sel1 get { x y z}] 0]
MM> > > set B [lindex [$sel2 get { x y z}] 0]
MM> > >
MM> > > lappend distAB [vecdist $A $B]
MM> > > }
MM> > > }
MM> > > set result [lindex [lsort -decreasing $distAB] 0]
MM> > > puts $outfile1 $result
MM> > > }
MM> > > close $outfile1
MM> > >
MM> > >
MM> >
MM> >
MM> >
MM> > --
MM> > =======================================================================
MM> > Axel Kohlmeyer akohlmey_at_cmm.chem.upenn.edu http://www.cmm.upenn.edu
MM> > Center for Molecular Modeling -- University of Pennsylvania
MM> > Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
MM> > tel: 1-215-898-1582, fax: 1-215-573-6233, office-tel: 1-215-898-5425
MM> > =======================================================================
MM> > If you make something idiot-proof, the universe creates a better idiot.
MM> >
MM>

-- 
=======================================================================
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.