diff --git a/src/library.cpp b/src/library.cpp index cb5c62aa87..b53257fc45 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -516,6 +516,9 @@ treated as part of the command and will **not** start a second command. The function returns the expanded string in a new string buffer that must be freed with :cpp:func:`lammps_free` after use to avoid a memory leak. +*See also* + :cpp:func:`lammps_eval' + \endverbatim * * \param handle pointer to a previously created LAMMPS instance @@ -2910,10 +2913,13 @@ int lammps_variable_info(void *handle, int idx, char *buffer, int buf_size) { .. versionadded:: TBD -This function takes a string with an expression, like what can be used +This function takes a string with an expression that can be used for :doc:`equal style variables `, evaluates it and returns the resulting (scalar) value as a floating point number. +*See also* + :cpp:func:`lammps_expand' + \endverbatim * \param handle pointer to a previously created LAMMPS instance cast to ``void *``. diff --git a/unittest/c-library/test_library_objects.cpp b/unittest/c-library/test_library_objects.cpp index a175952dfd..4a51381e64 100644 --- a/unittest/c-library/test_library_objects.cpp +++ b/unittest/c-library/test_library_objects.cpp @@ -233,3 +233,34 @@ TEST_F(LibraryObjects, expand) EXPECT_THAT((char *)ptr, StrEq("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'")); lammps_free(ptr); } + +TEST_F(LibraryObjects, eval) +{ + lammps_get_last_error_message(lmp, nullptr, 1); + ::testing::internal::CaptureStdout(); + lammps_commands_string(lmp, "region box1 block 0 10 0 5 -0.5 0.5\n" + "lattice fcc 0.8\n" + "create_box 1 box1\n" + "create_atoms 1 box\n" + "mass * 1.0\n" + "pair_style lj/cut 4.0\n" + "pair_coeff * * 1.0 1.0\n" + "variable t equal 15.0\n" + "velocity all create 1.5 532656\n" + "fix 1 all nve\n" + "run 0 post no\n"); + lammps_command(lmp, "variable one index 1 2 3 4"); + lammps_command(lmp, "variable two equal 2"); + std::string output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_EQ(lammps_has_error(lmp), 0); + + EXPECT_DOUBLE_EQ(lammps_eval(lmp, "4+5"), 9.0); + EXPECT_EQ(lammps_has_error(lmp), 0); + EXPECT_DOUBLE_EQ(lammps_eval(lmp, "v_one / 2.0"), 0.5); + EXPECT_EQ(lammps_has_error(lmp), 0); + EXPECT_DOUBLE_EQ(lammps_eval(lmp, "count(all)"), 36.0); + EXPECT_EQ(lammps_has_error(lmp), 0); + EXPECT_DOUBLE_EQ(lammps_eval(lmp, "pe"), -3.9848867644689534); + EXPECT_EQ(lammps_has_error(lmp), 0); +} diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 2066514318..06dcacd412 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -538,6 +538,27 @@ create_atoms 1 single & expanded = self.lmp.expand("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'") self.assertEqual(expanded, "'xx_$(4+5)_$(PI) ${one}-${two}-${three}'") + def test_eval(self): + self.lmp.commands_string( + """region box1 block 0 10 0 5 -0.5 0.5 + lattice fcc 0.8 + create_box 1 box1 + create_atoms 1 box + mass * 1.0 + pair_style lj/cut 4.0 + pair_coeff * * 1.0 1.0 + variable t equal 15.0 + velocity all create 1.5 532656 + fix 1 all nve + run 0 post no""") + self.lmp.command("variable one index 1 2 3 4") + self.lmp.command("variable two equal 2") + + self.assertEqual(self.lmp.eval("4+5"), 9.0) + self.assertEqual(self.lmp.eval("v_one / 2.0"), 0.5) + self.assertEqual(self.lmp.eval("count(all)"), 36.0) + self.assertEqual(self.lmp.eval("pe"), -3.9848867644689534) + def test_get_thermo(self): self.lmp.command("units lj") self.lmp.command("atom_style atomic")