# compute a variable incrementally

Hi everybody,

I’m not sure if this argument has never been debated before; but I could not find a solution in the mailing list.

Is there a way to force LAMMPS to compute a variable (atom-style or equal-style does not matter) in an incremental way?

What I mean is something like the simple "x = x +1 "

I tried many different ways such as

fix 40 all property/atom d_accumulate
set group all d_accumulate 0.

run 1

fix Xold all store/state 0 d_accumulate
variable Xnew atom f_Xold+1
set group all d_accumulate v_Xnew

run 1

but they all fail.

The bottom line is that LAMMPS updates all the variables together and d_accumulate remains 1 all the time

Alessio

Lecturer in Chemical Engineering
School of Chemical Engineering
University of Birmingham
Edgbaston
Birmingham, B15 2TT
United Kingdom
Telephone +44 (0) 121 414 5305

Hi everybody,

I'm not sure if this argument has never been debated before; but I could
not find a solution in the mailing list.

Is there a way to force LAMMPS to compute a variable (atom-style or
equal-style does not matter) in an incremental way?

please explain in more detail ​what you want to achieve by this?​

axel.

Hi - as Axel hinted there are probably simpler
ways to do what you want, than what follows.

You can increment either equal or atom style
variables, though it’s not as simple as a programming
language (which the input script syntax is not).
The key thing to remember is that equal and
atom style vars do not store values, they store
formulas.

For an equal var, you can overwrite the formula.
whenever you like, e.g. each iteration of a loop,
like this:

variable x equal 0
print \$x

variable n loop 10
label loop
variable x equal \$x+1 # key line
print \$x
next n
jump SELF loop

In the key line, the \$x is getting substituted with
is the current value of the variable, so the stored
formula is “0+1”, then “1+1”, “2+1”, etc. The increment value
of “1” could be anything, e.g. some system property
like total energy.

For atom variables, you have to use a fix, b/c you
want to store an accumulating value for each atom,
which persist with the atom as it migrates around.
Variables can’t do that, they only store formulas.

So something like this works:

variable a atom “f_accum + 1”
fix accum all store/state 10 v_a
compute sum all reduce sum f_accum
variable sum equal c_sum
fix out all print 20 “Sum \${sum}”

run 100

The last 3 lines before the run are simply
a way to output the stored atom quantities
as a single summed scalar. You could also
dump them to a file, etc. Again the +1 in the
formula could be anything, e.g. per-atom energy.
And the group for fix store/state could be any
subset of atoms. Note that in this case the
values stored by the fix will get incremented every
10 steps. They are also incremented (from 0) at
the beginning of the first run after you define it.

Steve

1 Like