add is_os() special function to the variable command

This commit is contained in:
Axel Kohlmeyer
2022-08-06 21:44:03 -04:00
parent 5b0a9cea99
commit 4ac74a4153
3 changed files with 44 additions and 4 deletions

View File

@ -66,7 +66,7 @@ Syntax
bound(group,dir,region), gyration(group,region), ke(group,reigon), bound(group,dir,region), gyration(group,region), ke(group,reigon),
angmom(group,dim,region), torque(group,dim,region), angmom(group,dim,region), torque(group,dim,region),
inertia(group,dimdim,region), omega(group,dim,region) inertia(group,dimdim,region), omega(group,dim,region)
special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), extract_setting(name) special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name)
feature functions = is_active(category,feature), is_available(category,feature), is_defined(category,id) feature functions = is_active(category,feature), is_available(category,feature), is_defined(category,id)
atom value = id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i], q[i] atom value = id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i], q[i]
atom vector = id, mass, type, mol, x, y, z, vx, vy, vz, fx, fy, fz, q atom vector = id, mass, type, mol, x, y, z, vx, vy, vz, fx, fy, fz, q
@ -939,6 +939,20 @@ The is_file(name) function is a test whether *name* is a (readable) file
and returns 1 in this case, otherwise it returns 0. For that *name* and returns 1 in this case, otherwise it returns 0. For that *name*
is taken as a literal string and must not have any blanks in it. is taken as a literal string and must not have any blanks in it.
The is_os(name) function is a test whether *name* is part of the OS
information that LAMMPS collects and provides in the
:cpp:func:`platform::os_info() <LAMMPS_NS::platform::os_info>` function.
The argument *name* is interpreted as a regular expression as documented
for the :cpp:func:`utils::strmatch() <LAMMPS_NS::utils::strmatch>`
function. This allows to adapt LAMMPS inputs to the OS it runs on:
.. code-block:: LAMMPS
if $(is_os(^Windows)) then &
"shell copy ${input_dir}/some_file.txt ." &
else &
"shell cp ${input_dir}/some_file.txt ."
The extract_setting(name) function enables access to basic settings for The extract_setting(name) function enables access to basic settings for
the LAMMPS executable and the running simulation via calling the the LAMMPS executable and the running simulation via calling the
:cpp:func:`lammps_extract_setting` library function. For example, the :cpp:func:`lammps_extract_setting` library function. For example, the

View File

@ -3942,7 +3942,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
strcmp(word,"trap") != 0 && strcmp(word,"slope") != 0 && strcmp(word,"gmask") != 0 && strcmp(word,"rmask") != 0 && strcmp(word,"trap") != 0 && strcmp(word,"slope") != 0 && strcmp(word,"gmask") != 0 && strcmp(word,"rmask") != 0 &&
strcmp(word,"grmask") != 0 && strcmp(word,"next") != 0 && strcmp(word,"is_active") != 0 && strcmp(word,"grmask") != 0 && strcmp(word,"next") != 0 && strcmp(word,"is_active") != 0 &&
strcmp(word,"is_defined") != 0 && strcmp(word,"is_available") != 0 && strcmp(word,"is_file") != 0 && strcmp(word,"is_defined") != 0 && strcmp(word,"is_available") != 0 && strcmp(word,"is_file") != 0 &&
strcmp(word,"extract_setting") != 0) strcmp(word,"is_os") != 0 && strcmp(word,"extract_setting") != 0)
return 0; return 0;
// parse contents for comma-separated args // parse contents for comma-separated args
@ -4340,6 +4340,19 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
treestack[ntreestack++] = newtree; treestack[ntreestack++] = newtree;
} else argstack[nargstack++] = value; } else argstack[nargstack++] = value;
} else if (strcmp(word,"is_os") == 0) {
if (narg != 1) print_var_error(FLERR,"Invalid is_os() function in variable formula",ivar);
value = utils::strmatch(platform::os_info(), args[0]) ? 1.0 : 0.0;
// save value in tree or on argstack
if (tree) {
auto newtree = new Tree();
newtree->type = VALUE;
newtree->value = value;
treestack[ntreestack++] = newtree;
} else argstack[nargstack++] = value;
} else if (strcmp(word,"extract_setting") == 0) { } else if (strcmp(word,"extract_setting") == 0) {
if (narg != 1) print_var_error(FLERR,"Invalid extract_setting() function in variable formula",ivar); if (narg != 1) print_var_error(FLERR,"Invalid extract_setting() function in variable formula",ivar);

View File

@ -144,12 +144,14 @@ TEST_F(VariableTest, CreateDelete)
command("variable ten3 uloop 4 pad"); command("variable ten3 uloop 4 pad");
command("variable dummy index 0"); command("variable dummy index 0");
command("variable file equal is_file(MYFILE)"); command("variable file equal is_file(MYFILE)");
command("variable iswin equal is_os(^Windows)");
command("variable islin equal is_os(^Linux)");
END_HIDE_OUTPUT(); END_HIDE_OUTPUT();
ASSERT_EQ(variable->nvar, 18); ASSERT_EQ(variable->nvar, 20);
BEGIN_HIDE_OUTPUT(); BEGIN_HIDE_OUTPUT();
command("variable dummy delete"); command("variable dummy delete");
END_HIDE_OUTPUT(); END_HIDE_OUTPUT();
ASSERT_EQ(variable->nvar, 17); ASSERT_EQ(variable->nvar, 19);
ASSERT_THAT(variable->retrieve("three"), StrEq("three")); ASSERT_THAT(variable->retrieve("three"), StrEq("three"));
variable->set_string("three", "four"); variable->set_string("three", "four");
ASSERT_THAT(variable->retrieve("three"), StrEq("four")); ASSERT_THAT(variable->retrieve("three"), StrEq("four"));
@ -168,6 +170,17 @@ TEST_F(VariableTest, CreateDelete)
platform::unlink("MYFILE"); platform::unlink("MYFILE");
ASSERT_THAT(variable->retrieve("file"), StrEq("0")); ASSERT_THAT(variable->retrieve("file"), StrEq("0"));
#if defined(_WIN32)
ASSERT_THAT(variable->retrieve("iswin"), StrEq("1"));
ASSERT_THAT(variable->retrieve("islin"), StrEq("0"));
#elif defined(__linux__)
ASSERT_THAT(variable->retrieve("iswin"), StrEq("0"));
ASSERT_THAT(variable->retrieve("islin"), StrEq("1"));
#else
ASSERT_THAT(variable->retrieve("iswin"), StrEq("0"));
ASSERT_THAT(variable->retrieve("islin"), StrEq("0"));
#endif
BEGIN_HIDE_OUTPUT(); BEGIN_HIDE_OUTPUT();
command("variable seven delete"); command("variable seven delete");
command("variable seven getenv TEST_VARIABLE"); command("variable seven getenv TEST_VARIABLE");