LAMMPS WWW Site - LAMMPS Documentation - LAMMPS Mailing List Archives
Re: [lammps-users] summing too many variables in a loop

# Re: [lammps-users] summing too many variables in a loop

 From: Mostafa Nejatolahi Date: Sat, 15 Jul 2017 04:44:30 +0430

Thank you dear Axel.

On Sat, Jul 15, 2017 at 4:35 AM, Axel Kohlmeyer wrote:

On Fri, Jul 14, 2017 at 7:40 PM, Mostafa Nejatolahi wrote:
Hello all,

I will be appreciated if someone tells me a way to calculate a summation like the following in a shorter way (the number of variables could be higher like 81, 125, ...).

variable        Jx equal v_Jx1+v_Jx2+v_Jx3+v_Jx4+v_Jx5+v_Jx6+v_Jx7&
+v_Jx8+v_Jx9+v_Jx10+v_Jx11+v_Jx12+v_Jx13+v_Jx14&
+v_Jx15+v_Jx16+v_Jx17+v_Jx18+v_Jx19+v_Jx20+v_Jx21&
+v_Jx22+v_Jx23+v_Jx24+v_Jx25+v_Jx26+v_Jx27

I used a loop-next-jump command but I got this error:
ERROR: Variable has circular dependency (../variable.cpp:1823)

variable sum equal 0
variable i loop 27
label start_of_loop_5
#--------------------------------
variable sum equal v_sum+v_Jx\$i
#--------------------------------
next
jump SELF start_of_loop_5

​you can escape the circular dependency by using an immediate expansion ​in between to carry the sum value to the next loop. check this out:

cat in.loopme

variable i1 equal 1
variable i2 equal 10
variable i3 equal 100
variable i4 equal 1000

variable sum equal 0.0
variable i loop 4
label  loop
variable hold equal \${sum}
variable sum equal v_hold+v_i\$i
print "i=\$i sum=\${sum}"
next i
jump SELF loop

print "final sum=\${sum}"

​axel​

p.s.: please note, that any input in LAMMPS that requires *that* many variables is probably doing something that is much simpler and more efficiently (and more likely correctly) done in post-processing. script languages like python are much more suitable for such tasks. please also note, that recent LAMMPS versions, if compiled accordingly, also support python style variables to embed python scriptlets.

Regards
MN

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most