Merge pull request #4041 from akohlmey/test-fix-numdiff

Add fix numdiff based tests for bonded interactions
This commit is contained in:
Axel Kohlmeyer
2024-02-06 14:15:28 -05:00
committed by GitHub
18 changed files with 286 additions and 40 deletions

View File

@ -27,6 +27,7 @@
#include "atom.h"
#include "compute.h"
#include "exceptions.h"
#include "fix.h"
#include "fmt/format.h"
#include "force.h"
#include "info.h"
@ -528,6 +529,59 @@ TEST(AngleStyle, omp)
if (!verbose) ::testing::internal::GetCapturedStdout();
};
TEST(AngleStyle, numdiff)
{
if (!LAMMPS::is_installed_pkg("EXTRA-FIX")) GTEST_SKIP();
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();
LAMMPS::argv args = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite"};
::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(args, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
if (!lmp) {
std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n";
for (auto &prerequisite : test_config.prerequisites) {
std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n";
}
GTEST_SKIP();
}
EXPECT_THAT(output, StartsWith("LAMMPS ("));
EXPECT_THAT(output, HasSubstr("Loop time"));
// abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms, nlocal);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("fix diff all numdiff 2 6.05504e-6");
lmp->input->one("run 2 post no");
if (!verbose) ::testing::internal::GetCapturedStdout();
Fix *ifix = lmp->modify->get_fix_by_id("diff");
if (ifix) {
double epsilon = test_config.epsilon * 5.0e8;
ErrorStats stats;
double **f1 = lmp->atom->f;
double **f2 = ifix->array_atom;
SCOPED_TRACE("EXPECT FORCES: numdiff");
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f1[i][0], f2[i][0], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][1], f2[i][1], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][2], f2[i][2], epsilon);
}
if (print_stats)
std::cerr << "numdiff stats: " << stats << " epsilon: " << epsilon << std::endl;
}
if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
}
TEST(AngleStyle, single)
{
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();

View File

@ -27,6 +27,7 @@
#include "bond.h"
#include "compute.h"
#include "exceptions.h"
#include "fix.h"
#include "fmt/format.h"
#include "force.h"
#include "info.h"
@ -530,6 +531,60 @@ TEST(BondStyle, omp)
if (!verbose) ::testing::internal::GetCapturedStdout();
};
TEST(BondStyle, numdiff)
{
if (!LAMMPS::is_installed_pkg("EXTRA-FIX")) GTEST_SKIP();
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();
LAMMPS::argv args = {"BondStyle", "-log", "none", "-echo", "screen", "-nocite"};
::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(args, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
if (!lmp) {
std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n";
for (auto &prerequisite : test_config.prerequisites) {
std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n";
}
GTEST_SKIP();
}
EXPECT_THAT(output, StartsWith("LAMMPS ("));
EXPECT_THAT(output, HasSubstr("Loop time"));
// abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms, nlocal);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("fix diff all numdiff 2 6.05504e-6");
lmp->input->one("run 2 post no");
if (!verbose) ::testing::internal::GetCapturedStdout();
Fix *ifix = lmp->modify->get_fix_by_id("diff");
if (ifix) {
double epsilon = test_config.epsilon * 5.0e8;
ErrorStats stats;
double **f1 = lmp->atom->f;
double **f2 = ifix->array_atom;
SCOPED_TRACE("EXPECT FORCES: numdiff");
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f1[i][0], f2[i][0], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][1], f2[i][1], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][2], f2[i][2], epsilon);
}
if (print_stats)
std::cerr << "numdiff stats: " << stats << " epsilon: " << epsilon << std::endl;
}
if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
}
TEST(BondStyle, single)
{
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();

View File

@ -27,6 +27,7 @@
#include "compute.h"
#include "dihedral.h"
#include "exceptions.h"
#include "fix.h"
#include "fmt/format.h"
#include "force.h"
#include "info.h"
@ -531,3 +532,57 @@ TEST(DihedralStyle, omp)
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
};
TEST(DihedralStyle, numdiff)
{
if (!LAMMPS::is_installed_pkg("EXTRA-FIX")) GTEST_SKIP();
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();
LAMMPS::argv args = {"DihedralStyle", "-log", "none", "-echo", "screen", "-nocite"};
::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(args, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
if (!lmp) {
std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n";
for (auto &prerequisite : test_config.prerequisites) {
std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n";
}
GTEST_SKIP();
}
EXPECT_THAT(output, StartsWith("LAMMPS ("));
EXPECT_THAT(output, HasSubstr("Loop time"));
// abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms, nlocal);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("fix diff all numdiff 2 6.05504e-6");
lmp->input->one("run 2 post no");
if (!verbose) ::testing::internal::GetCapturedStdout();
Fix *ifix = lmp->modify->get_fix_by_id("diff");
if (ifix) {
double epsilon = test_config.epsilon * 5.0e8;
ErrorStats stats;
double **f1 = lmp->atom->f;
double **f2 = ifix->array_atom;
SCOPED_TRACE("EXPECT FORCES: numdiff");
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f1[i][0], f2[i][0], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][1], f2[i][1], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][2], f2[i][2], epsilon);
}
if (print_stats)
std::cerr << "numdiff stats: " << stats << " epsilon: " << epsilon << std::endl;
}
if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
}

View File

@ -26,6 +26,7 @@
#include "atom.h"
#include "compute.h"
#include "exceptions.h"
#include "fix.h"
#include "fmt/format.h"
#include "force.h"
#include "improper.h"
@ -524,3 +525,56 @@ TEST(ImproperStyle, omp)
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
};
TEST(ImproperStyle, numdiff)
{
if (!LAMMPS::is_installed_pkg("EXTRA-FIX")) GTEST_SKIP();
if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP();
LAMMPS::argv args = {"ImproperStyle", "-log", "none", "-echo", "screen", "-nocite"};
::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(args, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
if (!lmp) {
std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n";
for (auto &prerequisite : test_config.prerequisites) {
std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n";
}
GTEST_SKIP();
}
EXPECT_THAT(output, StartsWith("LAMMPS ("));
EXPECT_THAT(output, HasSubstr("Loop time"));
// abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms, nlocal);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("fix diff all numdiff 2 6.05504e-6");
lmp->input->one("run 2 post no");
if (!verbose) ::testing::internal::GetCapturedStdout();
Fix *ifix = lmp->modify->get_fix_by_id("diff");
if (ifix) {
double epsilon = test_config.epsilon * 5.0e8;
ErrorStats stats;
double **f1 = lmp->atom->f;
double **f2 = ifix->array_atom;
SCOPED_TRACE("EXPECT FORCES: numdiff");
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f1[i][0], f2[i][0], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][1], f2[i][1], epsilon);
EXPECT_FP_LE_WITH_EPS(f1[i][2], f2[i][2], epsilon);
}
if (print_stats)
std::cerr << "numdiff stats: " << stats << " epsilon: " << epsilon << std::endl;
}
if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout();
}

View File

@ -47,7 +47,7 @@ void EXPECT_STRESS(const std::string &name, double *stress, const stress_t &expe
EXPECT_FP_LE_WITH_EPS(stress[3], expected_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], expected_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], expected_stress.yz, epsilon);
if (print_stats) std::cerr << name << " stats" << stats << std::endl;
if (print_stats) std::cerr << name << " stats: " << stats << std::endl;
}
void EXPECT_FORCES(const std::string &name, Atom *atom, const std::vector<coord_t> &f_ref,
@ -64,7 +64,7 @@ void EXPECT_FORCES(const std::string &name, Atom *atom, const std::vector<coord_
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
}
if (print_stats) std::cerr << name << " stats" << stats << std::endl;
if (print_stats) std::cerr << name << " stats: " << stats << std::endl;
}
void EXPECT_POSITIONS(const std::string &name, Atom *atom, const std::vector<coord_t> &x_ref,
@ -81,7 +81,7 @@ void EXPECT_POSITIONS(const std::string &name, Atom *atom, const std::vector<coo
EXPECT_FP_LE_WITH_EPS(x[i][1], x_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(x[i][2], x_ref[tag[i]].z, epsilon);
}
if (print_stats) std::cerr << name << " stats" << stats << std::endl;
if (print_stats) std::cerr << name << " stats: " << stats << std::endl;
}
void EXPECT_VELOCITIES(const std::string &name, Atom *atom, const std::vector<coord_t> &v_ref,
@ -98,7 +98,7 @@ void EXPECT_VELOCITIES(const std::string &name, Atom *atom, const std::vector<co
EXPECT_FP_LE_WITH_EPS(v[i][1], v_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(v[i][2], v_ref[tag[i]].z, epsilon);
}
if (print_stats) std::cerr << name << " stats" << stats << std::endl;
if (print_stats) std::cerr << name << " stats: " << stats << std::endl;
}
// common read_yaml_file function

View File

@ -2,7 +2,7 @@
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:17:51 2022
epsilon: 5e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
angle cosine/delta

View File

@ -5,6 +5,7 @@ epsilon: 2.5e-13
prerequisites: ! |
atom full
angle fourier/simple
skip_tests: numdiff
pre_commands: ! ""
post_commands: ! ""
input_file: in.fourmol

View File

@ -1,7 +1,7 @@
---
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:17:52 2022
epsilon: 2.5e-13
epsilon: 2.5e-12
skip_tests:
prerequisites: ! |
atom full

View File

@ -2,7 +2,7 @@
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:17:52 2022
epsilon: 5e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
angle table

View File

@ -2,7 +2,7 @@
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:17:51 2022
epsilon: 2.5e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
bond table

View File

@ -2,7 +2,7 @@
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:18:01 2022
epsilon: 2.5e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
dihedral charmm

View File

@ -1,7 +1,7 @@
---
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:18:02 2022
epsilon: 2.5e-13
epsilon: 5.0e-12
skip_tests:
prerequisites: ! |
atom full

View File

@ -3,7 +3,7 @@ lammps_version: 17 Feb 2022
tags: unstable
date_generated: Fri Mar 18 22:18:02 2022
epsilon: 1e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
dihedral table/cut

View File

@ -3,7 +3,7 @@ lammps_version: 22 Dec 2022
tags: unstable
date_generated: Mon Dec 26 16:49:31 2022
epsilon: 7.5e-14
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
dihedral table

View File

@ -1,7 +1,7 @@
---
lammps_version: 22 Dec 2022
date_generated: Mon Dec 26 16:49:37 2022
epsilon: 1e-13
epsilon: 1.0e-13
skip_tests:
prerequisites: ! |
atom full

View File

@ -2,7 +2,7 @@
lammps_version: 17 Feb 2022
date_generated: Fri Mar 18 22:18:02 2022
epsilon: 2.5e-13
skip_tests:
skip_tests: numdiff
prerequisites: ! |
atom full
improper cossq