From 4ac74a41537470566f2526f6856eb8c5ce9fe8e7 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 6 Aug 2022 21:44:03 -0400 Subject: [PATCH] add is_os() special function to the variable command --- doc/src/variable.rst | 16 +++++++++++++++- src/variable.cpp | 15 ++++++++++++++- unittest/commands/test_variables.cpp | 17 +++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/doc/src/variable.rst b/doc/src/variable.rst index d5c05a205e..1224323e34 100644 --- a/doc/src/variable.rst +++ b/doc/src/variable.rst @@ -66,7 +66,7 @@ Syntax bound(group,dir,region), gyration(group,region), ke(group,reigon), angmom(group,dim,region), torque(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) 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 @@ -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* 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() ` function. +The argument *name* is interpreted as a regular expression as documented +for the :cpp:func:`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 LAMMPS executable and the running simulation via calling the :cpp:func:`lammps_extract_setting` library function. For example, the diff --git a/src/variable.cpp b/src/variable.cpp index 168ccfa1cd..a564847b68 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -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,"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,"extract_setting") != 0) + strcmp(word,"is_os") != 0 && strcmp(word,"extract_setting") != 0) return 0; // 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; } 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) { if (narg != 1) print_var_error(FLERR,"Invalid extract_setting() function in variable formula",ivar); diff --git a/unittest/commands/test_variables.cpp b/unittest/commands/test_variables.cpp index 29b985927d..cf124eeafd 100644 --- a/unittest/commands/test_variables.cpp +++ b/unittest/commands/test_variables.cpp @@ -144,12 +144,14 @@ TEST_F(VariableTest, CreateDelete) command("variable ten3 uloop 4 pad"); command("variable dummy index 0"); command("variable file equal is_file(MYFILE)"); + command("variable iswin equal is_os(^Windows)"); + command("variable islin equal is_os(^Linux)"); END_HIDE_OUTPUT(); - ASSERT_EQ(variable->nvar, 18); + ASSERT_EQ(variable->nvar, 20); BEGIN_HIDE_OUTPUT(); command("variable dummy delete"); END_HIDE_OUTPUT(); - ASSERT_EQ(variable->nvar, 17); + ASSERT_EQ(variable->nvar, 19); ASSERT_THAT(variable->retrieve("three"), StrEq("three")); variable->set_string("three", "four"); ASSERT_THAT(variable->retrieve("three"), StrEq("four")); @@ -168,6 +170,17 @@ TEST_F(VariableTest, CreateDelete) platform::unlink("MYFILE"); 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(); command("variable seven delete"); command("variable seven getenv TEST_VARIABLE");