lammps/lammps

[BUG] Vector variable interaction with loop variable.

hothello opened this issue · 2 comments

Summary

Operations on a vector variable result in a syntax error depending on the order of declaration of or operations on unrelated variables.

LAMMPS Version and Platform

Tested with:

  • LAMMPS (21 Nov 2023)
  • LAMMPS (7 Feb 2024 - Update 1)

Expected Behavior

The syntax of the variable de and instant variable $(v_e0[1]+v_de*(v_j-1)) is correct and should not be affected by the order of declaration of other variables. It seems that the operations on the vector variable e0 are affected by operations on the variable i, namely:

  • The presence of a loop before the evaluation: jump SELF cycle1
  • variable i delete

Actual Behavior

The scripts yields an ERROR: Variable e0: Invalid syntax in variable formula (../variable.cpp:2359) when evaluating the variable de, even though the syntax is correct.

Steps to Reproduce

These scripts yield the error:

  1. Interaction with a loop before operations on the vector variable:
    # Input variables.
    variable N  equal  10
    variable i loop $N
    variable e0 vector [-10,-5]
    
    # First cycle.
    label       cycle1
    print "Cycle: $i"
    next      i
    jump SELF cycle1
    
    # Just the operation.
    variable de equal "(v_e0[2]-v_e0[1])/v_N"
    print ${de}
  2. Deletion of a loop variable created before the vector variable:
    # Input variables.
    variable N  equal  10
    variable i loop $N
    variable e0 vector [-10,-5]
    variable i delete
    
    # Only one cycle.
    variable de equal "(v_e0[2]-v_e0[1])/v_N"
    print ${de}
  3. A more complex script, with two loops. The inconsistent behaviour is highlighted in the next section.
    # Input variables.
    variable N  equal  10
    variable i loop $N
    variable j loop $N
    variable e0 vector [-10,-5]
    
    # First cycle.
    label       cycle1
    print "Cycle: $i"
    next      i
    jump SELF cycle1
    variable i delete
    
    # Second cycle, with operations on the vector variable.
    variable de equal "(v_e0[2]-v_e0[1])/v_N"
    print ${de}
    label       cycle2
    print "Operation: $(v_e0[1]+v_de*(v_j-1))"
    next      j
    jump SELF cycle2
    variable j delete

Note: the same error appears with variable i index 1 2 3 4 5 6 7 8 9 10.

Further Information, Files, and Links

These variations of the previous scripts are executed correctly.

  1.  # Input variables.
     variable N  equal  10
     variable i loop $N
     variable e0 vector [-10,-5]
      
     # Execute the operation first.
     variable de equal "(v_e0[2]-v_e0[1])/v_N"
     print ${de}
    
     # Then the cycle.
     label       cycle1
     print "Cycle: $i"
     next      i
     jump SELF cycle1
  2.  # Input variables.
     variable N  equal  10
     variable i loop $N # The loop variable is defined, but not deleted.
     variable e0 vector [-10,-5]
     
     # Only one cycle.
     variable de equal "(v_e0[2]-v_e0[1])/v_N"
     print ${de}
  3.  # Input variables (order matters!).
     variable N  equal  10
     variable j loop $N
     variable e0 vector [-10,-5]
     variable i loop $N # This somewhat works!
     
     # First cycle.
     label       cycle1
     print "Cycle: $i"
     next      i
     jump SELF cycle1
     variable i delete
     
     # Second cycle, with operations on the vector variable.
     variable de equal "(v_e0[2]-v_e0[1])/v_N"
     print ${de}
     label       cycle2
     print "Operation: $(v_e0[1]+v_de*(v_j-1))"
     next      j
     jump SELF cycle2
     variable j delete
  4.  # Input variables (original order).
     variable N  equal  10
     variable i loop $N
     variable j loop $N
     variable e0 vector [-10,-5]
     
     # Second cycle executed first.
     variable de equal "(v_e0[2]-v_e0[1])/v_N"
     print ${de}
     label       cycle2
     print "Operation: $(v_e0[1]+v_de*(v_j-1))"
     next      j
     jump SELF cycle2
     variable j delete
    
     # First cycle.
     label       cycle1
     print "Cycle: $i"
     next      i
     jump SELF cycle1
     variable i delete

@hothello - thanks for the simple test scripts! I found the issue and will post a PR in a few minutes to fix the bug.

Fixed by PR #4085