VMD-L Mailing List
From: Axel Kohlmeyer (akohlmey_at_gmail.com)
Date: Thu May 10 2012 - 20:00:51 CDT
- Next message: Bennion, Brian: "RE: Loop for"
- Previous message: Andrés Morales: "Loop for"
- In reply to: Andrés Morales: "Loop for"
- Next in thread: Bennion, Brian: "RE: Loop for"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
On Thu, May 10, 2012 at 7:40 PM, Andrés Morales
<h.andres.m1986_at_gmail.com> wrote:
> Dear VMD users
>
> I am trying to calculate an specific amount (electric potential) from data
> extracted from a text file. The script works fine to calculate this value
> for an specific point (specific values of x, y, z variables), but when I
> tried to run it for a range of values it doesn't work. I used the following
> script:
>
> ###################################
> set file2 [open total.txt r]
> set output2 [open campo.txt w];
> puts $output2 "y z potencial"
> set sum 0
> for {set i -35 } {$i <= 35 } { incr i 5 } {
> for {set j -30 } {$j <= 55 } { incr j 5 } {
> set x1 0
> set y1 [expr $i]
> set z1 [expr $j]
> while { [gets $file2 line2] != -1 } {
> set dist [expr pow(([lindex $line2 1]-$x1)*([lindex $line2 1]-$x1) +
> ([lindex $line2 2]-$y1)*([lindex $line2 2]-$y1) + ([lindex $line2
> 3]-$z1)*([lindex $line2 3]-$z1),0.5)]
> set campo [expr [lindex $line2 0] / abs($dist)]
> set sum [expr $sum + $campo]
> }
> puts $output2 "$i $j $sum"
> }}
> close $output2
> #######################################
>
> I got an output like this:
>
> y z potencial
> -35 -30 0.01074960712757333
> -35 -25 0.01074960712757333
> -35 -20 0.01074960712757333
> -35 -15 0.01074960712757333
> -35 -10 0.01074960712757333
> -35 -5 0.01074960712757333
> -35 0 0.01074960712757333
> -35 5 0.01074960712757333
> -35 10 0.01074960712757333
> ....
>
> Does anybody know what is wrong and how I can solve it???
hard to tell. it looks however, like you messed up the loops.
also quote the code can be simplified and made more
readable (and faster). this assumes, there are only 4 words
in each line of total.txt, otherwise you need to replace:
foreach {val x1 y1 z1} $line {}
with:
lassign $line val x1 y1 z1
###################################
set file2 [open total.txt r]
set output2 [open campo.txt w];
puts $output2 "y z potencial"
while { [gets $file2 line2] != -1 } {
foreach {val x1 y1 z1} $line {}
set vec1 [list $x1 $y1 $z1]
set sum 0
for {set i -35 } {$i <= 35 } { incr i 5 } {
for {set j -30 } {$j <= 55 } { incr j 5 } {
set dist [vecdist $vec1 [list 0 $i $j]]
set sum [expr {$sum + ($val / abs($dist))}]
}
}
puts $output2 "$i $j $sum"
}
close $output2
#######################################
>
>
> Thanks a lot
>
> Andres
>
>
>
-- Dr. Axel Kohlmeyer akohlmey_at_gmail.com http://goo.gl/1wk0 College of Science and Technology Temple University, Philadelphia PA, USA.
- Next message: Bennion, Brian: "RE: Loop for"
- Previous message: Andrés Morales: "Loop for"
- In reply to: Andrés Morales: "Loop for"
- Next in thread: Bennion, Brian: "RE: Loop for"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]