Load discrete profile from file and interpolate as a function of time

Dear Community,

I would like to load a discrete profile x(t), e.g. a scalar position as a function of time, into LAMMPS from a file, which has the following format,

Time Position
0.0 0.36
0.1 0.48

and interpolate these values in LAMMPS as a function of time during a run such that

“variable position equal positionInterp(time)”

The objective is to impose the position to a group (via fix move) on the basis of external data (e.g. measured profile). Interpolation is not necessarily required since I could sample the data precisely at each time step. Hence, a list could be indexed by the timestep index. This operation seems possible by the python command, which I fear to be costly, if it is called at each time step. Is there an alternative?

Thank you,
Dominik

you can implement this as a file style variable (or atomfile style, if you want to provide positions for each atom individually), if you provide one entry per time step.
otherwise, you can implement a python style variable and do the file reading and possible interpolation from inside that code. that would not be too costly (unlike per-atom or even worse per-atom pair computations).

of course the non-script alternative would be to write your own fix command in C++.

axel.

Ok, i.e there a 3 possible solutions:

  • file style variable, if one entry per time step in file;
  • python style variable;
  • custom fix.

Concerning the first solution, I start by reading and printing (test) the values of the file line after line during a run as follows

variable alphaFile file alpha.txt
variable alpha equal next(v_)
fix alphaPrint all print 1 “Value in file = ${}”

I get, however, the following error after adding the third line of the previous 3 lines:

ERROR: Variable ID in variable formula does not exist (…/variable.cpp:4339)

The file alpha.txt has the following format

8.9815e-06
3.6093e-05
8.1584e-05

Do you know for what reason the variable alpha seems not to be correctly created? It seems to be related to the next() function.

Thank you again,
Dominik

Ok, i.e there a 3 possible solutions:

  • file style variable, if one entry per time step in file;
  • python style variable;
  • custom fix.

Concerning the first solution, I start by reading and printing (test) the values of the file line after line during a run as follows

variable alphaFile file alpha.txt
variable alpha equal next(v_alphaFile)
fix alphaPrint all print 1 “Value in file = ${alpha}”

I get, however, the following error after adding the third line of the previous 3 lines:

ERROR: Variable ID in variable formula does not exist (…/variable.cpp:4339)

The file alpha.txt has the following format

8.9815e-06
3.6093e-05
8.1584e-05

Do you know for what reason the variable alpha seems not to be correctly created? It seems to be related to the next() function.

yes, you didn’t read the documentation of the next() function carefully enough. you didn’t create a variable named v_alphaFile, hence the (deserved) error.

axel.

Hence, the solution is

variable alphaFile file alpha.txt
variable alpha equal next(alphaFile)
fix alphaPrint all print 1 “Value in file = ${alpha}”

… because of the documentation of “variable command”: " ".

There remains, however, a problem. The variable alpha advances by one increment in the list, when it is evaluated. For instance, the following cosine and sine functions are therefore not evaluated at the same angle but at angle “i” and “i+1”

variable cosAlpha equal cos(v_alpha)
variable sinAlpha equal sin(v_alpha)

How can I evaluate both functions at the same angle “i”?

I thought that this would be possible by defining an intermediate variable

variable alphaTmp equal v_alpha

Hence, the solution is

no, need to repeat it here. just check the documentation more carefully the next time.

[…]

Is there a way to use the value “i” multiple times at a time step?

you can use fix ave/time for caching such data (just run it with 1 1 1 settings). you need something else than a variable, since they are either expanded when the input is read or when referenced.
but if you know you need the value twice, why not simply put it into the file twice?

axel.

1. I try to give the final solution in my answers in case somebody else has the same problem.

2. Here is the solution with the fix ave/time :

variable alphaFile file alpha.txt
variable alpha equal next(alphaFile)
variable cosAlpha equal cos(v_alphaFile)
variable sinAlpha equal sin(v_alphaFile)
fix nextValue all ave/time 1 1 1 v_alpha

While v_alphaFile contains this value at index "i", the evaluation of alpha in the fix ave/time allows to go to the next value, i.e. t "i+1", at the following iteration.

Thank you again!
Dominik