From: Axel Kohlmeyer (akohlmey_at_gmail.com)
Date: Tue Dec 04 2012 - 02:32:54 CST

On Tue, Dec 4, 2012 at 5:53 AM, Z.Y.Qiu <luoyuan126_at_gmail.com> wrote:
> Hi,everyone! In order to how many atoms in some radial distance certred at
> some atom in whole frame, I write a script
> to calculate it. The following is my script:
> proc getnum {totalframe} {
> set a 0
> set b 0
> set c 0
> set d 0

> for { set i 0} { $i<=$totalframe} { incr i 1} {
> atomselect 0 "type 2 and within 2.2 of index 2985" frame $i
> set num [atomselect$i num]
> set list [atomselect$i list]
> if {$num==0} {
> incr a 1
> } elseif {$num==1} {
> incr b 1
> } elseif {$num==2} {
> incr c 1
> } elseif {$num>2} {
> incr d 1
> }
> puts "frame $i $num $list\n"
> }
> puts " No.of 0=$a\n"
> puts " No.of 1=$b\n"
> puts " No.of 2=$c\n"
> puts " No.of more than 2=$d\n"
> }
>
> But I met a question. Sometimes I can run the script successfully. However,
> it can't be run for second time with an err that
> "invalid command name "atomselect0" or "invalid command name "atomselect1"
> and so on.I can't find where the problem

the explanation is simple. the atomselect command
creates new commands and since they have to be
unique, they are called atomselect# (with # being a
number starting from 0). to make those unique, the
counter is initialized to 0 when you launch VMD and
never reset, only incremented.

the way to handle this is to assign the output of the atomselect
command to a variable and then use that variable instead
of atomselect#. e.g.:

set sel [atomselect 0 "type 2 and within 2.2 of index 2985" frame $i]

in fact, in your case, you don't even need to recreate the atom
selection but the loop header of your script should be:

set sel [atomelect 0 "type 2 and within 2.2 of index 2985"]

  for { set i 0} { $i<=$totalframe} { incr i 1} {
    $sel frame $i
    $sel update
    set num [$sel num]
    set list [$sel list]

apart from that, i don't quite understand why you need to
loop over all frames until the desired one. this contradicts
the description of what you want to compute.

> is.Additional,how can I ouput the result to some file I specify??

you can use regular Tcl syntax for that,
i.e. open a file with open and then print to
the resulting file handle with puts. check out
the Tcl tutorial(s) on www.tcl.tk

axel.

> Thank you and appreciating any comment.
> yours sincerely
> Z.Y.Yau
> 12-03-2012

--
Dr. Axel Kohlmeyer  akohlmey_at_gmail.com  http://goo.gl/1wk0
International Centre for Theoretical Physics, Trieste. Italy.