From af203f7fc7d3304de91026ea6b534306c185ecfd Mon Sep 17 00:00:00 2001 From: alphataubio Date: Sun, 4 Aug 2024 22:40:36 -0400 Subject: [PATCH] added run_torque and EXPECT_TORQUES --- unittest/force-styles/test_config.h | 2 ++ unittest/force-styles/test_config_reader.cpp | 16 ++++++++++++++++ unittest/force-styles/test_config_reader.h | 1 + unittest/force-styles/test_fix_timestep.cpp | 13 +++++++++++++ unittest/force-styles/test_main.cpp | 17 +++++++++++++++++ unittest/force-styles/test_main.h | 2 ++ 6 files changed, 51 insertions(+) diff --git a/unittest/force-styles/test_config.h b/unittest/force-styles/test_config.h index 09b32f54de..303f48ce73 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_torque; 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_torque.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..aed1dbec67 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_torque"] = &TestConfigReader::run_torque; consumers["pair_style"] = &TestConfigReader::pair_style; consumers["pair_coeff"] = &TestConfigReader::pair_coeff; @@ -228,6 +229,21 @@ void TestConfigReader::run_vel(const yaml_event_t &event) } } +void TestConfigReader::run_torque(const yaml_event_t &event) +{ + config.run_torque.clear(); + config.run_torque.resize(config.natoms + 1); + std::stringstream data((char *)event.data.scalar.value); + std::string line; + + while (std::getline(data, line, '\n')) { + int tag; + coord_t xyz; + sscanf(line.c_str(), "%d %lg %lg %lg", &tag, &xyz.x, &xyz.y, &xyz.z); + config.run_torque[tag] = xyz; + } +} + 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 0427049bfc..254ed82408 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_torque(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 4295cd902a..c25773e30a 100644 --- a/unittest/force-styles/test_fix_timestep.cpp +++ b/unittest/force-styles/test_fix_timestep.cpp @@ -244,6 +244,19 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) block += fmt::format("{:3} {:23.16e} {:23.16e} {:23.16e}\n", i, v[j][0], v[j][1], v[j][2]); } writer.emit_block("run_vel", block); + + // run_torque + + if(lmp->atom->torque_flag) { + block.clear(); + auto *t = lmp->atom->torque; + for (int i = 1; i <= natoms; ++i) { + const int j = lmp->atom->map(i); + block += fmt::format("{:3} {:23.16e} {:23.16e} {:23.16e}\n", i, t[j][0], t[j][1], t[j][2]); + } + writer.emit_block("run_torque", block); + } + cleanup_lammps(lmp, config); } diff --git a/unittest/force-styles/test_main.cpp b/unittest/force-styles/test_main.cpp index da2574a037..b45c6cf723 100644 --- a/unittest/force-styles/test_main.cpp +++ b/unittest/force-styles/test_main.cpp @@ -101,6 +101,23 @@ void EXPECT_VELOCITIES(const std::string &name, Atom *atom, const std::vector &t_ref, + double epsilon) +{ + SCOPED_TRACE("EXPECT_TORQUES: " + name); + double **t = atom->torque; + tagint *tag = atom->tag; + const int nlocal = atom->nlocal; + ASSERT_EQ(nlocal + 1, t_ref.size()); + ErrorStats stats; + for (int i = 0; i < nlocal; ++i) { + EXPECT_FP_LE_WITH_EPS(t[i][0], t_ref[tag[i]].x, epsilon); + EXPECT_FP_LE_WITH_EPS(t[i][1], t_ref[tag[i]].y, epsilon); + EXPECT_FP_LE_WITH_EPS(t[i][2], t_ref[tag[i]].z, epsilon); + } + 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 6bcb24fb96..eea96d9072 100644 --- a/unittest/force-styles/test_main.h +++ b/unittest/force-styles/test_main.h @@ -45,5 +45,7 @@ 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_TORQUES(const std::string &name, LAMMPS_NS::Atom *atom, + const std::vector &t_ref, double epsilon); #endif