Skip to content

Commit

Permalink
add support for storing a global scalar and global vector
Browse files Browse the repository at this point in the history
  • Loading branch information
akohlmey committed Aug 9, 2020
1 parent 4b5bc8f commit 2edad43
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
5 changes: 4 additions & 1 deletion unittest/force-styles/test_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class TestConfig {
double run_coul;
stress_t init_stress;
stress_t run_stress;
double global_scalar;
std::vector<double> global_vector;
std::vector<coord_t> init_forces;
std::vector<coord_t> run_forces;
std::vector<coord_t> run_pos;
Expand All @@ -70,7 +72,7 @@ class TestConfig {
pair_style("zero"), bond_style("zero"), angle_style("zero"), dihedral_style("zero"),
improper_style("zero"), kspace_style("none"), natoms(0), init_energy(0), run_energy(0),
init_vdwl(0), run_vdwl(0), init_coul(0), run_coul(0), init_stress({0, 0, 0, 0, 0, 0}),
run_stress({0, 0, 0, 0, 0, 0})
run_stress({0, 0, 0, 0, 0, 0}), global_scalar(0)
{
prerequisites.clear();
pre_commands.clear();
Expand All @@ -87,6 +89,7 @@ class TestConfig {
restart_pos.clear();
run_vel.clear();
restart_vel.clear();
global_vector.clear();
}
virtual ~TestConfig(){};

Expand Down
23 changes: 23 additions & 0 deletions unittest/force-styles/test_config_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ TestConfigReader::TestConfigReader(TestConfig &config) : YamlReader(), config(co
consumers["run_vdwl"] = &TestConfigReader::run_vdwl;
consumers["run_coul"] = &TestConfigReader::run_coul;

consumers["global_scalar"] = &TestConfigReader::global_scalar;
consumers["global_vector"] = &TestConfigReader::global_vector;

consumers["bond_style"] = &TestConfigReader::bond_style;
consumers["bond_coeff"] = &TestConfigReader::bond_coeff;
consumers["angle_style"] = &TestConfigReader::angle_style;
Expand Down Expand Up @@ -299,3 +302,23 @@ void TestConfigReader::run_energy(const yaml_event_t &event)
{
config.run_energy = atof((char *)event.data.scalar.value);
}

void TestConfigReader::global_scalar(const yaml_event_t &event)
{
config.global_scalar = atof((char *)event.data.scalar.value);
}

void TestConfigReader::global_vector(const yaml_event_t &event)
{
std::stringstream data((char *)event.data.scalar.value);
config.global_vector.clear();
double value;
std::size_t num;
data >> num;
for (std::size_t i = 0; i < num; ++i) {
data >> value;
if (data.eof()) break;
config.global_vector.push_back(value);
}
}

3 changes: 2 additions & 1 deletion unittest/force-styles/test_config_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class TestConfigReader : public YamlReader<TestConfigReader> {
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 fix_style(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);
Expand All @@ -52,6 +51,8 @@ class TestConfigReader : public YamlReader<TestConfigReader> {
void run_coul(const yaml_event_t &event);
void init_energy(const yaml_event_t &event);
void run_energy(const yaml_event_t &event);
void global_scalar(const yaml_event_t &event);
void global_vector(const yaml_event_t &event);
};

#endif
26 changes: 24 additions & 2 deletions unittest/force-styles/test_fix_timestep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// natoms
writer.emit("natoms", natoms);

int ifix = lmp->modify->find_fix("test");
if (ifix < 0) {
std::cerr << "WARNING: no fix defined with fix ID 'test'\n";
} else {
Fix *fix = lmp->modify->fix[ifix];

// global scalar
if (fix->scalar_flag) {
double value = fix->compute_scalar();
writer.emit("global_scalar", value);
}

// global vector
if (fix->vector_flag) {
int num = fix->size_vector;
block = std::to_string(num);
for (int i = 0; i < num; ++i)
block += fmt::format(" {}", fix->compute_vector(i));
writer.emit_block("global_vector", block);
}
}

// run_pos
block.clear();
auto x = lmp->atom->x;
Expand Down Expand Up @@ -377,7 +399,7 @@ TEST(FixTimestep, plain)
}
if (print_stats) std::cerr << "run_pos, normal_run, respa: " << stats << std::endl;

v = lmp->atom->v;
v = lmp->atom->v;
ASSERT_EQ(nlocal + 1, v_ref.size());
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(v[i][0], v_ref[tag[i]].x, epsilon);
Expand Down Expand Up @@ -565,7 +587,7 @@ TEST(FixTimestep, omp)
}
if (print_stats) std::cerr << "run_pos, normal_run, respa: " << stats << std::endl;

v = lmp->atom->v;
v = lmp->atom->v;
ASSERT_EQ(nlocal + 1, v_ref.size());
for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(v[i][0], v_ref[tag[i]].x, epsilon);
Expand Down

0 comments on commit 2edad43

Please sign in to comment.