add tests for lammps_eval() and its python counterpart

This commit is contained in:
Axel Kohlmeyer
2025-01-12 14:49:07 -05:00
parent dce1231052
commit 85dec585a6
3 changed files with 59 additions and 1 deletions

View File

@ -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 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. must be freed with :cpp:func:`lammps_free` after use to avoid a memory leak.
*See also*
:cpp:func:`lammps_eval'
\endverbatim \endverbatim
* *
* \param handle pointer to a previously created LAMMPS instance * \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 .. 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 <variable>`, evaluates it and returns for :doc:`equal style variables <variable>`, evaluates it and returns
the resulting (scalar) value as a floating point number. the resulting (scalar) value as a floating point number.
*See also*
:cpp:func:`lammps_expand'
\endverbatim \endverbatim
* \param handle pointer to a previously created LAMMPS instance cast to ``void *``. * \param handle pointer to a previously created LAMMPS instance cast to ``void *``.

View File

@ -233,3 +233,34 @@ TEST_F(LibraryObjects, expand)
EXPECT_THAT((char *)ptr, StrEq("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'")); EXPECT_THAT((char *)ptr, StrEq("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'"));
lammps_free(ptr); 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);
}

View File

@ -538,6 +538,27 @@ create_atoms 1 single &
expanded = self.lmp.expand("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'") expanded = self.lmp.expand("'xx_$(4+5)_$(PI) ${one}-${two}-${three}'")
self.assertEqual(expanded, "'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): def test_get_thermo(self):
self.lmp.command("units lj") self.lmp.command("units lj")
self.lmp.command("atom_style atomic") self.lmp.command("atom_style atomic")