From 7709dfa11854561d92dd7e05d4bb76f864bdd89c Mon Sep 17 00:00:00 2001 From: jrgissing Date: Sat, 11 Feb 2023 16:11:35 -0500 Subject: [PATCH] add 'atom type' test option to force style tests --- unittest/force-styles/test_config.h | 2 ++ unittest/force-styles/test_config_reader.cpp | 15 +++++++++++++++ unittest/force-styles/test_config_reader.h | 1 + unittest/force-styles/test_fix_timestep.cpp | 12 ++++++++++++ unittest/force-styles/test_main.cpp | 19 +++++++++++++++++++ unittest/force-styles/test_main.h | 1 + 6 files changed, 50 insertions(+) diff --git a/unittest/force-styles/test_config.h b/unittest/force-styles/test_config.h index b284052d6d..e3d6c06033 100644 --- a/unittest/force-styles/test_config.h +++ b/unittest/force-styles/test_config.h @@ -70,6 +70,7 @@ public: std::vector restart_pos; std::vector run_vel; std::vector restart_vel; + std::vector run_atom_types; TestConfig() : lammps_version(""), date_generated(""), basename(""), epsilon(1.0e-14), input_file(""), @@ -94,6 +95,7 @@ public: restart_pos.clear(); run_vel.clear(); restart_vel.clear(); + run_atom_types.clear(); global_vector.clear(); } TestConfig(const TestConfig &) = delete; diff --git a/unittest/force-styles/test_config_reader.cpp b/unittest/force-styles/test_config_reader.cpp index 3f99f251a5..5eb41db4bf 100644 --- a/unittest/force-styles/test_config_reader.cpp +++ b/unittest/force-styles/test_config_reader.cpp @@ -48,6 +48,7 @@ TestConfigReader::TestConfigReader(TestConfig &config) : config(config) consumers["run_forces"] = &TestConfigReader::run_forces; consumers["run_pos"] = &TestConfigReader::run_pos; consumers["run_vel"] = &TestConfigReader::run_vel; + consumers["run_atom_types"] = &TestConfigReader::run_atom_types; consumers["pair_style"] = &TestConfigReader::pair_style; consumers["pair_coeff"] = &TestConfigReader::pair_coeff; @@ -228,6 +229,20 @@ void TestConfigReader::run_vel(const yaml_event_t &event) } } +void TestConfigReader::run_atom_types(const yaml_event_t &event) +{ + config.run_atom_types.clear(); + config.run_atom_types.resize(config.natoms + 1); + std::stringstream data((char *)event.data.scalar.value); + std::string line; + + while (std::getline(data, line, '\n')) { + int tag, atom_type; + sscanf(line.c_str(), "%d %d", &tag, &atom_type); + config.run_atom_types[tag] = atom_type; + } +} + void TestConfigReader::pair_style(const yaml_event_t &event) { config.pair_style = (char *)event.data.scalar.value; diff --git a/unittest/force-styles/test_config_reader.h b/unittest/force-styles/test_config_reader.h index 1af7589add..91512da655 100644 --- a/unittest/force-styles/test_config_reader.h +++ b/unittest/force-styles/test_config_reader.h @@ -41,6 +41,7 @@ public: void run_forces(const yaml_event_t &event); void run_pos(const yaml_event_t &event); void run_vel(const yaml_event_t &event); + void run_atom_types(const yaml_event_t &event); void pair_style(const yaml_event_t &event); void pair_coeff(const yaml_event_t &event); void bond_style(const yaml_event_t &event); diff --git a/unittest/force-styles/test_fix_timestep.cpp b/unittest/force-styles/test_fix_timestep.cpp index d2e35b463f..302425ebd3 100644 --- a/unittest/force-styles/test_fix_timestep.cpp +++ b/unittest/force-styles/test_fix_timestep.cpp @@ -284,6 +284,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (normal run, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (normal run, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (normal run, verlet)", lmp->atom, test_config.run_atom_types); int ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -334,6 +335,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (restart, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (restart, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (restart, verlet)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -373,6 +375,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (rmass, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (rmass, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (rmass, verlet)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -426,6 +429,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (normal run, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (normal run, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (normal run, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -464,6 +468,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (restart, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (restart, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (restart, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -503,6 +508,7 @@ TEST(FixTimestep, plain) EXPECT_POSITIONS("run_pos (rmass, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (rmass, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (rmass, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -584,6 +590,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (normal run, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (normal run, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (normal run, verlet)", lmp->atom, test_config.run_atom_types); int ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -634,6 +641,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (restart, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (restart, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (restart, verlet)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -673,6 +681,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (rmass, verlet)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (rmass, verlet)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (rmass, verlet)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -725,6 +734,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (normal run, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (normal run, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (normal run, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -763,6 +773,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (restart, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (restart, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (restart, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { @@ -802,6 +813,7 @@ TEST(FixTimestep, omp) EXPECT_POSITIONS("run_pos (rmass, respa)", lmp->atom, test_config.run_pos, epsilon); EXPECT_VELOCITIES("run_vel (rmass, respa)", lmp->atom, test_config.run_vel, epsilon); + EXPECT_ATOM_TYPES("run_atom_types (rmass, respa)", lmp->atom, test_config.run_atom_types); ifix = lmp->modify->find_fix("test"); if (ifix < 0) { diff --git a/unittest/force-styles/test_main.cpp b/unittest/force-styles/test_main.cpp index 80f1ca4e30..4a98c489be 100644 --- a/unittest/force-styles/test_main.cpp +++ b/unittest/force-styles/test_main.cpp @@ -70,6 +70,7 @@ void EXPECT_FORCES(const std::string &name, Atom *atom, const std::vector &x_ref, double epsilon) { + if (x_ref.empty()) return; SCOPED_TRACE("EXPECT_POSITIONS: " + name); double **x = atom->x; tagint *tag = atom->tag; @@ -87,6 +88,7 @@ void EXPECT_POSITIONS(const std::string &name, Atom *atom, const std::vector &v_ref, double epsilon) { + if (v_ref.empty()) return; SCOPED_TRACE("EXPECT_VELOCITIES: " + name); double **v = atom->v; tagint *tag = atom->tag; @@ -101,6 +103,23 @@ void EXPECT_VELOCITIES(const std::string &name, Atom *atom, const std::vector &at_ref) +{ + if (at_ref.empty()) return; + SCOPED_TRACE("EXPECT_ATOM_TYPES: " + name); + int *type = atom->type; + tagint *tag = atom->tag; + const int nlocal = atom->nlocal; + ASSERT_EQ(nlocal + 1, at_ref.size()); + ErrorStats stats; + for (int i = 0; i < nlocal; ++i) { + EXPECT_EQ(type[i], at_ref[tag[i]]); + EXPECT_EQ(type[i], at_ref[tag[i]]); + EXPECT_EQ(type[i], at_ref[tag[i]]); + } + if (print_stats) std::cerr << name << " stats" << stats << std::endl; +} + // common read_yaml_file function bool read_yaml_file(const char *infile, TestConfig &config) { diff --git a/unittest/force-styles/test_main.h b/unittest/force-styles/test_main.h index 36bcd7bdff..ede7402cdc 100644 --- a/unittest/force-styles/test_main.h +++ b/unittest/force-styles/test_main.h @@ -41,5 +41,6 @@ void EXPECT_STRESS(const std::string & name, double * stress, const stress_t & e void EXPECT_FORCES(const std::string & name, LAMMPS_NS::Atom * atom, const std::vector & f_ref, double epsilon); void EXPECT_POSITIONS(const std::string & name, LAMMPS_NS::Atom * atom, const std::vector & x_ref, double epsilon); void EXPECT_VELOCITIES(const std::string & name, LAMMPS_NS::Atom * atom, const std::vector & v_ref, double epsilon); +void EXPECT_ATOM_TYPES(const std::string & name, LAMMPS_NS::Atom * atom, const std::vector & at_ref); #endif