Hi:
I would like to be able to initialize a variable and increment it.
for example:
initialize:
variable a equal “20.0”
increment:
variable a “v_a+v_b”
where v_b will change over time. for my problem it is basically ‘press’ pressure.
I get a 'Variable has circular dependency" error.
How do I get around this?
Thanks!
Use of v_ to reference a variable will result in its right hand side (whatever follows “equal”) being evaluated every time the quantity is needed. Use of ${} specifies immediate evaluation.
This will get you stuck in a loop:
variable x equal 7
variable x equal v_x+1
print $x
This will print “8”:
variable x equal 7
variable x equal $x+1
print $x
See “Immediate Evaluation of Variables” in the doc page for the variable command.
Also, when playing around with this I notice the circular dependency error is not generated by the first script, using LAMMPS version 10 Jan 2015. Instead the program hangs, presumably in an infinite loop.
Anthony
Anthony,
Are you saying in the first case that gets you stuck in a loop, that the “print” command is never reached?
… and that lammps somewhere internally just continually keeps assigning x with the values 8, 9, 10, …
v/r,
dc
Correct. LAMMPS probably should throw an error on the second line of the first case, but this does not happen with my version (10 Jan 2015). The print statement is never reached. I expect an error will be thrown there using whatever version is used by Craig.
Anthony
Thanks!
However my problem is slightly different.
If I implement the code as:
variable x equal 20
variable x equal ${x}+1
read_restart restart.a
thermo_style custom v_x
thermo 1
run 5
I get:
x
21
21
21
21
21
21
When what I want is:
x
21
22
23
24
25
26
Thanks!
However my problem is slightly different.
If I implement the code as:
variable x equal 20
variable x equal ${x}+1
read_restart restart.a
thermo_style custom v_x
thermo 1
run 5
I get:
x
21
21
21
21
21
21
When what I want is:
x
21
22
23
24
25
26
please note that an equal style variable is like a function, that is
executed whenever needed. what you want is an operation that is
executed once every time step.
for this you need to use or program a fix style. for simple cases, you
could use fix ave/time for this purpose, e.g. with:
variable inc equal 1
variable sum equal f_store+v_inc
fix store all ave/time 1 1 1 v_sum
the store fix will evaluate the formula once per time step.
of course, this also works for cases where v_inc is an expression as well.
if you need something that is just incremented once per time step, you
don't need to go to this length, but just reference the "step" thermo
keyword.
axel.
Forgot to reply to the list:
I don’t think it’s possible to execute a variable assignment in the body of a variable as you’re trying to do. You seem to wish equal style variables behaved like functions which could have arbitrary side effects before returning, and they are not (though it might be cool).
If the application is as simple as the example you provided, could you instead create an equal style variable which depends on the timestep? Any thermo keyword can be part of an equal style variable, including step. If your variable will be evaluated at regular intervals in time, I think this is a possible solution. In your script, maybe you could define x as:
variable x equal step+21

Anthony
This hang will be fixed in the next patch.
Circular loops were caught in other contexts,
but not when the variable was substituted
for in an input script.
Steve