How to use vector variables

Can anyone help with the use of the vector variable style? I want to define a range in a vector and access its values, but apparently I am getting the syntax wrong. Here is my example:

# Input variables.
variable e0 vector [-13.1,-6.9]
variable N  equal  20

# Derived variables.
variable de equal "(v_e0[2]-v_e0[1])/v_N"
print ${de}
variable i loop $N
label       cycle1
print "Step: $(v_e0[1]+v_de*(v_i-1))"
next i
jump SELF cycle1
variable 1 delete

I get the following error (21 Nov 23):
ERROR: Variable e0: Invalid syntax in variable formula (../variable.cpp:2359) Last command: print ${de}

I cannot reproduce this.

This input does not error out for me with either the 7 Feb 2024 or the 2 Aug 2023 version.

Works fine for me as well with 2Aug2023. I get this error only if I use an older lammps version like 29 Sep 2021.

How interesting, the simplified script actually works for me as well!
The fact is, I was experimenting with a more complex script and the error appeared when this script is executed:

# Input variables.
variable count  equal  -1
variable i loop 20
variable fzcom equal -1
variable ten equal 10
variable e0 vector [-13.1,-6.9]
variable N  equal  20

# Test variables for counting and logic operations.
print "# fzcom count exit" append /tmp/test
label       cycle1

if "$(abs(v_fzcom)) > 0.1" then &
  "variable count equal $(v_count+1)"

variable exit equal "abs(v_fzcom) > 0.1 && v_count > v_ten"

if "${count} > $(v_ten)" then & 
  "print '${fzcom} ${count} ${exit}' append /tmp/test"

print "Cycle: $i Count: ${count} Exit ${exit}"
next      i
jump SELF cycle1
variable i delete

# Derived variables.
variable de equal "(v_e0[2]-v_e0[1])/v_N"
print ${de}
variable j loop $N
label       cycle2
print "Exponent: $(v_e0[1]+v_de*(v_j-1))"
next      j
jump SELF cycle2
variable j delete

Interestingly, the error only appears if the loop cycle1 is executed before cycle2, but not if cycle1 is deleted or cycle2 is executed first.

Update
The error boils down to the order of definition of the variables i and e0:

  • ERROR: Variable e0: Invalid syntax in variable formula (…/variable.cpp:2359)
    variable i loop 20
    variable e0 vector [-13.1,-6.9]
    
  • No error :face_with_spiral_eyes:
    variable e0 vector [-13.1,-6.9]
    variable i loop 20
    

Please file this as a bug report issue on github. This needs to be looked at by @sjplimp who doesn’t hang out here but is the main architect of the variable code and specifically implemented the vector style variable support.

1 Like

Here we go: [BUG] Vector variable interaction with loop variable. · Issue #4081 · lammps/lammps · GitHub

BTW, these delete operations are NOPs since the variables are automatically deleted when the loop values are exhausted. So you would only need to delete them (for reusing at a later part or an outside loop) if you “jump” out of the loop before the iterations are complete.

This bug is now fixed: Fix a bug with removing variables when a vector variable is defined by sjplimp · Pull Request #4085 · lammps/lammps · GitHub

2 Likes