Repository navigation
Expand file tree
/
Copy pathtest_harmonic.cpp
More file actions
155 lines (135 loc) · 4.06 KB
/
Copy pathtest_harmonic.cpp
File metadata and controls
155 lines (135 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cassert>
#include <cmath>
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>
#include "pele/harmonic.hpp"
#include "pele/meta_pow.hpp"
#include "test_utils.hpp"
using pele::Array;
using pele::pos_int_pow;
static double const EPS = std::numeric_limits<double>::min();
#define EXPECT_NEAR_RELATIVE(A, B, T) \
EXPECT_NEAR(A / (fabs(A) + fabs(B) + EPS), B / (fabs(A) + fabs(B) + EPS), T)
class HarmonicTest : public ::testing::Test {
public:
size_t nr_particles;
size_t box_dimension;
size_t nr_dof;
Array<double> x;
double k;
double displ;
Array<double> xd;
double e_ini_true;
void SetUp() {
nr_particles = 100;
box_dimension = 3;
nr_dof = nr_particles * box_dimension;
x = Array<double>(nr_dof, 0);
k = 12.12;
displ = 43.44;
xd = Array<double>(nr_dof, displ);
e_ini_true = 0.5 * k * nr_dof * pos_int_pow<2>(displ);
}
};
TEST_F(HarmonicTest, SetKGetK_Works) {
auto pot = std::make_shared<pele::Harmonic>(x, k, box_dimension);
for (size_t k = 400; k < 500; ++k) {
pot->set_k(k);
const double pot_k = pot->get_k();
EXPECT_DOUBLE_EQ(pot_k, k);
}
}
TEST_F(HarmonicTest, Works) {
auto pot = std::make_shared<pele::Harmonic>(x, k, box_dimension);
const double e_ini = pot->get_energy(xd);
EXPECT_NEAR_RELATIVE(e_ini, e_ini_true, 1e-10);
for (size_t k = 0; k < 100; ++k) {
pot->set_k(k);
const double e_pot = pot->get_energy(xd);
const double e_true = 0.5 * k * nr_dof * pos_int_pow<2>(displ);
EXPECT_NEAR_RELATIVE(e_pot, e_true, 1e-10);
Array<double> actual_grad(nr_dof, 0);
const double e_pot_grad = pot->get_energy_gradient(xd, actual_grad);
EXPECT_NEAR_RELATIVE(e_pot, e_pot_grad, 1e-10);
const double e_true_pair = 0.5 * k * pos_int_pow<2>(displ);
const double e_pair =
pele::harmonic_interaction(k).energy(pos_int_pow<2>(displ), 0);
EXPECT_NEAR_RELATIVE(e_pair, e_true_pair, 1e-10);
for (size_t i = 0; i < nr_dof; ++i) {
EXPECT_NEAR_RELATIVE(actual_grad[i], k * displ, 1e-10);
}
}
}
TEST_F(HarmonicTest, COM_Works) {
auto pot = std::make_shared<pele::HarmonicCOM>(x, k, box_dimension);
// Put center of mass to zero.
for (size_t i = 0; i < xd.size() / 2; ++i) {
xd[i] *= -1;
}
const double e_ini = pot->get_energy(xd);
const double e_ini_true =
0.5 * k * nr_particles * box_dimension * pos_int_pow<2>(displ);
EXPECT_NEAR_RELATIVE(e_ini, e_ini_true, 1e-10);
}
class HarmonicAtomListTest : public PotentialTest {
public:
double natoms;
double k;
virtual void setup_potential() {
pele::Array<size_t> atoms(natoms);
for (size_t i = 0; i < atoms.size(); ++i) {
atoms[i] = i;
}
pot = std::shared_ptr<pele::BasePotential>(
new pele::HarmonicAtomList(k, atoms));
}
virtual void SetUp() {
natoms = 3;
k = 1.;
x = Array<double>(3 * natoms);
x[0] = 0.1;
x[1] = 0.2;
x[2] = 0.3;
x[3] = 0.44;
x[4] = 0.55;
x[5] = 1.66;
x[6] = 0;
x[7] = 0;
x[8] = -3.;
etrue = 17.6197;
setup_potential();
}
};
TEST_F(HarmonicAtomListTest, Energy_Works) { test_energy(); }
TEST_F(HarmonicAtomListTest, EnergyGradient_AgreesWithNumerical) {
test_energy_gradient();
}
TEST_F(HarmonicAtomListTest, EnergyGradientHessian_AgreesWithNumerical) {
test_energy_gradient_hessian();
}
class HarmonicNeighborListTest : public HarmonicAtomListTest {
public:
virtual void setup_potential() {
pele::Array<size_t> nlist(natoms * (natoms - 1));
size_t count = 0;
for (size_t i = 0; i < natoms; ++i) {
for (size_t j = i + 1; j < natoms; ++j) {
nlist[count++] = i;
nlist[count++] = j;
}
}
assert(count == nlist.size());
pot = std::shared_ptr<pele::BasePotential>(
new pele::HarmonicNeighborList(k, nlist));
}
};
TEST_F(HarmonicNeighborListTest, Energy_Works) { test_energy(); }
TEST_F(HarmonicNeighborListTest, EnergyGradient_AgreesWithNumerical) {
test_energy_gradient();
}
TEST_F(HarmonicNeighborListTest, EnergyGradientHessian_AgreesWithNumerical) {
test_energy_gradient_hessian();
}