Skip to content

Commit f39fa23

Browse files
add tests for statistic interceptors
1 parent 671af87 commit f39fa23

File tree

3 files changed

+329
-0
lines changed

3 files changed

+329
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <filesystem>
4+
#include <iostream>
5+
#include <memory>
6+
7+
#include "simulation/SimulationUtils.h"
8+
#include "simulation/interceptors/diffusion_function/DiffusionFunctionInterceptor.h"
9+
10+
std::vector<Particle> createParticles1() {
11+
std::array<double, 3> x1 = {5.5, 4.8, 3.2};
12+
std::array<double, 3> v1 = {0.2, 0.1, 0};
13+
14+
std::array<double, 3> x2 = {5.7, 5.2, 7.4};
15+
std::array<double, 3> v2 = {-0.1, 0.2, 0};
16+
17+
std::array<double, 3> x3 = {4.2, 5.6, 5.8};
18+
std::array<double, 3> v3 = {0.1, -0.2, 0};
19+
20+
std::array<double, 3> x4 = {6.3, 6.2, 4};
21+
std::array<double, 3> v4 = {0.1, 0.1, 0};
22+
23+
return {Particle(x1, v1, 1, 0), Particle(x2, v2, 1, 0), Particle(x3, v3, 1, 0), Particle(x4, v4, 1, 0)};
24+
}
25+
26+
TEST(DiffusionInterceptor, IsZeroForNonMovingParticles) {
27+
Logger::logger->set_level(spdlog::level::warn);
28+
29+
auto particles = createParticles1();
30+
31+
SimulationParams params_lc = TEST_DEFAULT_PARAMS_LENNARD_JONES;
32+
33+
Simulation simulation_lc(particles, params_lc);
34+
35+
DiffusionFunctionInterceptor interceptor(1);
36+
37+
interceptor.onSimulationStart(simulation_lc);
38+
39+
interceptor(1, simulation_lc);
40+
41+
// check if csv file was created
42+
43+
std::string csv_file_name = "statistics/diffusion_function.csv";
44+
std::filesystem::path csv_file_path = params_lc.output_dir_path / csv_file_name;
45+
46+
ASSERT_TRUE(std::filesystem::exists(csv_file_path));
47+
48+
std::ifstream csv_file(csv_file_path);
49+
50+
std::string line;
51+
std::getline(csv_file, line);
52+
53+
ASSERT_EQ(line, "\"iteration\";\"var(t)\"");
54+
std::getline(csv_file, line);
55+
56+
ASSERT_EQ(line, "1;0");
57+
58+
std::getline(csv_file, line);
59+
60+
ASSERT_TRUE(csv_file.eof());
61+
}
62+
63+
TEST(DiffusionInterceptor, CalculatesCorrectDiffusion) {
64+
Logger::logger->set_level(spdlog::level::warn);
65+
66+
auto particles = createParticles1();
67+
68+
SimulationParams params_lc = TEST_DEFAULT_PARAMS_LENNARD_JONES;
69+
70+
Simulation simulation_lc(particles, params_lc);
71+
72+
DiffusionFunctionInterceptor interceptor(1);
73+
74+
interceptor.onSimulationStart(simulation_lc);
75+
76+
for (auto& p : particles) {
77+
p.setX({p.getX()[0] + 1, p.getX()[1] + 1, p.getX()[2] + 1});
78+
}
79+
80+
Simulation simulation_lc_new(particles, params_lc);
81+
82+
interceptor(1, simulation_lc_new);
83+
84+
// check if csv file was created
85+
86+
std::string csv_file_name = "statistics/diffusion_function.csv";
87+
std::filesystem::path csv_file_path = params_lc.output_dir_path / csv_file_name;
88+
89+
ASSERT_TRUE(std::filesystem::exists(csv_file_path));
90+
91+
std::ifstream csv_file(csv_file_path);
92+
93+
std::string line;
94+
std::getline(csv_file, line);
95+
96+
ASSERT_EQ(line, "\"iteration\";\"var(t)\"");
97+
std::getline(csv_file, line);
98+
99+
// Every particle moved by sqrt(3) therfore the variance is 3
100+
ASSERT_EQ(line, "1;3");
101+
102+
std::getline(csv_file, line);
103+
104+
ASSERT_TRUE(csv_file.eof());
105+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "simulation/interceptors/radial_distribution_function/RadialDistributionFunctionInterceptor.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include <filesystem>
6+
#include <iostream>
7+
#include <memory>
8+
9+
#include "simulation/SimulationUtils.h"
10+
11+
TEST(RadialDistributionFunction, IsCorrectForSmallGrid_OneBin) {
12+
Logger::logger->set_level(spdlog::level::warn);
13+
14+
std::vector<Particle> particles = {
15+
Particle({0, 0, 0}, {0, 0, 0}, 1, 0),
16+
Particle({1, 0, 0}, {0, 0, 0}, 1, 0),
17+
Particle({0, 1, 0}, {0, 0, 0}, 1, 0),
18+
Particle({1, 1, 0}, {0, 0, 0}, 1, 0),
19+
};
20+
21+
SimulationParams params_lc = TEST_DEFAULT_PARAMS_LENNARD_JONES;
22+
23+
Simulation simulation_lc(particles, params_lc);
24+
25+
RadialDistributionFunctionInterceptor interceptor(2, 1);
26+
27+
interceptor.onSimulationStart(simulation_lc);
28+
29+
interceptor(1, simulation_lc);
30+
31+
// check if csv file was created
32+
33+
std::string csv_file_name = "statistics/radial_distribution_function.csv";
34+
std::filesystem::path csv_file_path = params_lc.output_dir_path / csv_file_name;
35+
36+
ASSERT_TRUE(std::filesystem::exists(csv_file_path));
37+
38+
std::ifstream csv_file(csv_file_path);
39+
40+
std::string line;
41+
std::getline(csv_file, line);
42+
43+
ASSERT_EQ(line, "\"iteration\";\"bin_index (w= 2.000000)\";\"samples\";\"local_density\"");
44+
45+
std::getline(csv_file, line);
46+
47+
// Hand calculated for the small example that all particles are in a grid
48+
ASSERT_EQ(line, "0;0;6;0.179049");
49+
50+
std::getline(csv_file, line);
51+
52+
ASSERT_EQ(line, "1;0;6;0.179049");
53+
54+
std::getline(csv_file, line);
55+
56+
ASSERT_TRUE(csv_file.eof());
57+
}
58+
59+
TEST(RadialDistributionFunction, IsCorrectForSmallGrid_TwoBins) {
60+
Logger::logger->set_level(spdlog::level::warn);
61+
62+
std::vector<Particle> particles = {
63+
Particle({0, 0, 0}, {0, 0, 0}, 1, 0),
64+
Particle({1, 0, 0}, {0, 0, 0}, 1, 0),
65+
Particle({0, 1, 0}, {0, 0, 0}, 1, 0),
66+
Particle({1, 1, 0}, {0, 0, 0}, 1, 0),
67+
};
68+
69+
SimulationParams params_lc = TEST_DEFAULT_PARAMS_LENNARD_JONES;
70+
71+
Simulation simulation_lc(particles, params_lc);
72+
73+
RadialDistributionFunctionInterceptor interceptor(1.2, 1);
74+
75+
interceptor.onSimulationStart(simulation_lc);
76+
77+
interceptor(1, simulation_lc);
78+
79+
// check if csv file was created
80+
81+
std::string csv_file_name = "statistics/radial_distribution_function.csv";
82+
std::filesystem::path csv_file_path = params_lc.output_dir_path / csv_file_name;
83+
84+
ASSERT_TRUE(std::filesystem::exists(csv_file_path));
85+
86+
std::ifstream csv_file(csv_file_path);
87+
88+
std::string line;
89+
std::getline(csv_file, line);
90+
91+
ASSERT_EQ(line, "\"iteration\";\"bin_index (w= 1.200000)\";\"samples\";\"local_density\"");
92+
93+
std::getline(csv_file, line);
94+
95+
// Hand calculated for the small example that all particles are in a grid
96+
ASSERT_EQ(line, "0;0;4;0.552621");
97+
98+
std::getline(csv_file, line);
99+
100+
ASSERT_EQ(line, "0;1;2;0.039473");
101+
102+
std::getline(csv_file, line);
103+
104+
// Particles did not move, so the result should be the same
105+
ASSERT_EQ(line, "1;0;4;0.552621");
106+
107+
std::getline(csv_file, line);
108+
109+
ASSERT_EQ(line, "1;1;2;0.039473");
110+
111+
std::getline(csv_file, line);
112+
113+
ASSERT_TRUE(csv_file.eof());
114+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "simulation/interceptors/velocity_profile/VelocityProfileInterceptor.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include <filesystem>
6+
#include <iostream>
7+
#include <memory>
8+
9+
#include "simulation/SimulationUtils.h"
10+
11+
TEST(VelocityProfileInterceptor, TestBasicMeasurment) {
12+
Logger::logger->set_level(spdlog::level::warn);
13+
14+
std::vector<Particle> particles = {
15+
Particle({0, 0, 0}, {1, 2, 3}, 1, 0),
16+
Particle({1, 0, 0}, {1, 2, 3}, 1, 0),
17+
Particle({0, 1, 0}, {1, 2, 3}, 1, 0),
18+
Particle({1, 1, 0}, {1, 2, 3}, 1, 0),
19+
};
20+
21+
SimulationParams params_lc = TEST_DEFAULT_PARAMS_LENNARD_JONES;
22+
23+
Simulation simulation_lc(particles, params_lc);
24+
25+
std::pair<std::array<double, 3>, std::array<double, 3>> box =
26+
std::make_pair(std::array<double, 3>{-0.5, -0.5, -0.5}, std::array<double, 3>{1.5, 1.5, 1.5});
27+
28+
VelocityProfileInterceptor interceptor(box, 2, 1);
29+
30+
interceptor.onSimulationStart(simulation_lc);
31+
32+
interceptor(1, simulation_lc);
33+
34+
// check if csv file was created
35+
36+
std::string csv_file_name_x = "statistics/velocity_profile_x_axis.csv";
37+
std::filesystem::path csv_file_path_x = params_lc.output_dir_path / csv_file_name_x;
38+
39+
std::string csv_file_name_y = "statistics/velocity_profile_y_axis.csv";
40+
std::filesystem::path csv_file_path_y = params_lc.output_dir_path / csv_file_name_y;
41+
42+
std::string csv_file_name_z = "statistics/velocity_profile_z_axis.csv";
43+
std::filesystem::path csv_file_path_z = params_lc.output_dir_path / csv_file_name_z;
44+
45+
ASSERT_TRUE(std::filesystem::exists(csv_file_path_x));
46+
ASSERT_TRUE(std::filesystem::exists(csv_file_path_y));
47+
ASSERT_TRUE(std::filesystem::exists(csv_file_path_z));
48+
49+
std::ifstream csv_file_x(csv_file_path_x);
50+
std::ifstream csv_file_y(csv_file_path_y);
51+
std::ifstream csv_file_z(csv_file_path_z);
52+
53+
// check x. Values are trivial since every bin has the exact same particle velocities in it
54+
55+
std::string linex;
56+
std::getline(csv_file_x, linex);
57+
58+
ASSERT_EQ(linex, "\"iteration\";\"bin_index (w= 1.000000)\";\"avg_velocity_x\";\"avg_velocity_y\";\"avg_velocity_z\"");
59+
std::getline(csv_file_x, linex);
60+
61+
ASSERT_EQ(linex, "0;0;1;2;3");
62+
std::getline(csv_file_x, linex);
63+
64+
ASSERT_EQ(linex, "0;1;1;2;3");
65+
std::getline(csv_file_x, linex);
66+
67+
ASSERT_EQ(linex, "1;0;1;2;3");
68+
std::getline(csv_file_x, linex);
69+
70+
ASSERT_EQ(linex, "1;1;1;2;3");
71+
std::getline(csv_file_x, linex);
72+
73+
ASSERT_TRUE(csv_file_x.eof());
74+
75+
// check y. Values are the same as in x
76+
std::string liney;
77+
std::getline(csv_file_y, liney);
78+
79+
ASSERT_EQ(liney, "\"iteration\";\"bin_index (w= 1.000000)\";\"avg_velocity_x\";\"avg_velocity_y\";\"avg_velocity_z\"");
80+
std::getline(csv_file_y, liney);
81+
82+
ASSERT_EQ(liney, "0;0;1;2;3");
83+
std::getline(csv_file_y, liney);
84+
85+
ASSERT_EQ(liney, "0;1;1;2;3");
86+
std::getline(csv_file_y, liney);
87+
88+
ASSERT_EQ(liney, "1;0;1;2;3");
89+
std::getline(csv_file_y, liney);
90+
91+
ASSERT_EQ(liney, "1;1;1;2;3");
92+
std::getline(csv_file_y, liney);
93+
94+
ASSERT_TRUE(csv_file_y.eof());
95+
96+
// check z. Values are the same as in x
97+
std::string linez;
98+
std::getline(csv_file_z, linez);
99+
100+
ASSERT_EQ(linez, "\"iteration\";\"bin_index (w= 1.000000)\";\"avg_velocity_x\";\"avg_velocity_y\";\"avg_velocity_z\"");
101+
std::getline(csv_file_z, linez);
102+
103+
ASSERT_EQ(linez, "0;0;1;2;3");
104+
std::getline(csv_file_z, linez);
105+
106+
ASSERT_EQ(linez, "1;0;1;2;3");
107+
std::getline(csv_file_z, linez);
108+
109+
ASSERT_TRUE(csv_file_z.eof());
110+
}

0 commit comments

Comments
 (0)