Get current step?

I have LAMMPS from Nov 3 2022.

I’m wondering if there is a way to set a variable to get the current step. The idea being, if I have a variable like the following:


run 1000

variable	radiusDownw equal "v_baseTw - step*dt*v_ratew"
run 1000

I could subtract 1000 in the radiusDownw formula, without having to use reset_timestep 0. ie

run 1000

variable   currstep equal currentstep
variable	radiusDownw equal "v_baseTw - (step-v_currstep)*dt*v_ratew"
run 1000

I know I can reset_timestep 0, but this would be handy to be able to run a dump throughout the program. Also I can`t simply add up the times of the previous runs, because there are also thermos, which have variable run times. I guess I could reset the timestep back to what it was before the thermo, but either way, wondering if I could just get the variable or no

1 Like

Yes, you have to keep in mind, that there are two ways to expand a variable: immediate expansion as part of the pre-processing of the input file and variable referencing as part of the variable syntax. In the first case the expansion starts with a $, in the second case with a v_.

If you define a variable mystep to reference the thermo keyword step:

variable mystep equal step

then it makes a difference whether you use:

variable	radiusDownw equal "v_baseTw - ${mystep}*dt*v_ratew"

or

variable	radiusDownw equal "v_baseTw - v_mystep*dt*v_ratew"

In the first case, a value of 1000 is used while in the second case the current timestep number.
You can see the difference when looking at the log file, since pre-processing expansion of variables is shown there.

There is a shortcut version of the immediate expansion that does not require defining a variable like mystep that uses parenthesis instead of curly braces:

variable	radiusDownw equal "v_baseTw - $(step)*dt*v_ratew"

Even simpler would be:

variable	radiusDownw equal "v_baseTw - $(time)*v_ratew"

which would be independent of the length of a timestep and thus also compatible with simulations where it changes or that use fix dt/reset.

Another option to have a continuous ramp of a variable across multiple run commands, is to an equal style variable expression with the ramp() function and then use the “start” and “stop” keywords with the “run” commands. Basically, if you use the same values for “start” and “stop” for all run statements, then the ramp will continue over those “run” commands.

1 Like

This is a very well-written explanation for the variable command. It’s the kind of example that would make the manual much more user-friendly. Maybe worth collecting into a new, more compact version of the documentation.

Feel free to collect and edit such bits and pieces into something that can be contributed to either the LAMMPS manual or the LAMMPS website. I have already suggested multiple times that creating a collection of “questions and answers” for discussing typical problems, errors, or misconceptions would be a good project for somebody to learn more about LAMMPS and provide a useful resource.

In this specific case (or similar ones), the “natural” to add this to the manual would be as a “Howto” chapter. Since this could contain sections that either answer “how does this work?” or “how do I do?” type of questions. The documentation of the individual commands is more of the “what can it do?” style. So those would be complementary.

1 Like