Access value of variable in C++

How do I access the value of a variable, in C++, if it was initialized with variable v equal "v_zz < sin(sin(5e5*v_xx))*sin(cos(5e5*v_yy))/10e4"?

I can see its ID doing

vvar = input->variable->find(vstr);
std::cout << "vvar: " << vvar << std::endl;

this gives me vvar: 3

You have two options:

Variable::retrieve() will return a string and take the name of the variable as argument.

Variable::compute_equal() will return a double and can take either the variable name or the variable index as argument (the function is overloaded).

These functions only take one input as arguments. If I need to get one point of the variable, given two other points (e.g., given X,Y what is the Z), is there a built-in function for that?

compute_equal gives me 0, which I would assume means it’s not equal, but have no idea what that means.

Variables take no arguments. If you want to compute an expression that is dependent on X and Y you have to define X and Y as variables, too and set their value. Example:

variable x equal 2.0
variable y equal 3.0
variable z equal v_x*v_z
print "z = ${z}"

A more elegant approach from within C++ code would be to use “internal” style variables. But to use those properly, you need to spend some time reading LAMMPS source code and the comments in it.

No. Variable::compute_equal() will evaluate an equal (or compatible) style variable (hence the “equal”) and return its value as a double precision floating point number. Its purpose can be easily deduced from reading the comments in the source code of its implementation.

Thank you for such quick insights, @akohlmey , and sorry for these beginner questions, still getting the hang on LAMMPS.

I’ve been trying to apply the LAMMPS methods, but the comments in the code aren’t as easy to understand as the documentation of LAMMPS. Will have to read it thoroughly, then.

Just to give closure to this topic, I found a solution that is likely far from ideal. However, the compute_equal method only seems to work with all independent variables defined, I found no other method to call it while supplying the values of X and Y. The solution I found was:

  1. Obtain the expression as a string from the data[ivar][0] object. For this, I had to alter the Variable class, since it only gives the data[ivar][1] object, i.e., the result of calculation.
  2. Change the v_xx and v_yy independent variables by the corresponding values, and crop the z < part. I want to keep spheres away from the space defined by the equation, and the center of the sphere is the X and Y.
  3. Finally, compute the value, using compute_equal.

I have to say it is my first time working with C++, so this solution is likely far away from an optimal solution. But it works. Btw, nice piece of code that evaluate method, really fun to see an implementation of string to mathematics.

I already told you, that that is not possible. The LAMMPS input script language is not designed to be a full programming language, but to provide sufficient flexibility to “connect” the different modules and commands. So instead of stubbornly looking for something that does not exist or create a workaround that emulates that, you should try to “think within the logic of what is available”. This takes some getting used to, but if you are working with an existing code base, it is necessary for being effective and writing code that is maintainable and suitable for later inclusion into the code base.

The method to use in your workflow is to define v_xx and v_yy as “internal” style variables and then you can easily set their values with calls to Variable::internal_set(). I had already mentioned internal variables, IIRC. No source code hacks needed for this.

1 Like