OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestSinusoidalTimeDependence.cpp
Go to the documentation of this file.
1//
2// Tests for SinusoidalTimeDependence
3//
4// Copyright (c) 2025, Jon Thompson, STFC Rutherford Appleton Laboratory, Didcot, UK
5//
6// This file is part of OPAL.
7//
8// OPAL is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// You should have received a copy of the GNU General Public License
14// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15//
18#include "Ippl.h"
20#include "gtest/gtest.h"
21
22TEST(TestSinusoidalTimeDependence, SinusoidalTimeDependenceTest) {
23 // Check empty coefficients always returns 0.
24 SinusoidalTimeDependence time_dependence_1({}, {}, {}, {});
25 EXPECT_DOUBLE_EQ(time_dependence_1.getValue(0.1), 0.0);
26
27 // Check a sine wave value
28 SinusoidalTimeDependence time_dependence_2({8.0}, {}, {}, {});
29 EXPECT_DOUBLE_EQ(time_dependence_2.getValue(0.1), -0.47552825814757682);
30
31 // Check the amplitude
32 SinusoidalTimeDependence time_dependence_3({8.0}, {}, {2.0}, {});
33 EXPECT_DOUBLE_EQ(time_dependence_3.getValue(0.1), -0.95105651629515364);
34
35 // Check the phase offset
36 SinusoidalTimeDependence time_dependence_4({8.0}, {0.1}, {2.0}, {});
37 EXPECT_DOUBLE_EQ(time_dependence_4.getValue(0.1), -0.91545497277810161);
38
39 // Check the DC offset
40 SinusoidalTimeDependence time_dependence_5({8.0}, {0.1}, {2.0}, {-1.0});
41 EXPECT_DOUBLE_EQ(time_dependence_5.getValue(0.1), -1.91545497277810161);
42
43 // Check clone produces same result
44 SinusoidalTimeDependence* time_dependence_clone = time_dependence_5.clone();
45 EXPECT_DOUBLE_EQ(time_dependence_clone->getValue(0.1), -1.91545497277810161);
46 delete time_dependence_clone;
47}
48
49TEST(TestSinusoidalTimeDependence, TDMapTest) {
50 // throw on empty value
52
53 // set/get time dependence
54 SinusoidalTimeDependence time_dep({}, {}, {}, {});
55 std::shared_ptr<SinusoidalTimeDependence> td1(time_dep.clone());
57 EXPECT_EQ(AbstractTimeDependence::getTimeDependence("td1"), td1);
58 std::shared_ptr<SinusoidalTimeDependence> td2(time_dep.clone());
60 EXPECT_EQ(AbstractTimeDependence::getTimeDependence("td2"), td2);
61 EXPECT_EQ(AbstractTimeDependence::getTimeDependence("td1"), td1);
62 // set time dependence overwriting existing time dependence
63 // should overwrite, without memory leak
64 std::shared_ptr<SinusoidalTimeDependence> td3(time_dep.clone());
66 EXPECT_EQ(AbstractTimeDependence::getTimeDependence("td1"), td3);
67}
68
69TEST(TestSinusoidalTimeDependence, TDMapNameLookupTest) {
71 SinusoidalTimeDependence time_dep({}, {}, {}, {});
72 std::shared_ptr<SinusoidalTimeDependence> td1(time_dep.clone());
73 std::shared_ptr<SinusoidalTimeDependence> td2(time_dep.clone());
74 std::shared_ptr<SinusoidalTimeDependence> td3(time_dep.clone());
78 std::string name1 = AbstractTimeDependence::getName(td1);
79 EXPECT_EQ(name1, "td1");
80 std::string name2 = AbstractTimeDependence::getName(td2);
81 EXPECT_TRUE(name2 == "td2" || name2 == "td3");
83}
84
85TEST(TestSinusoidalTimeDependence, Integral) {
86 // Check empty coefficients always returns 0.
87 SinusoidalTimeDependence time_dependence_1({}, {}, {}, {});
88 EXPECT_NEAR(time_dependence_1.getIntegral(0.1), 0.0, 0.000001);
89
90 // Check a sine wave value
91 SinusoidalTimeDependence time_dependence_2({8.0}, {}, {}, {});
92 EXPECT_NEAR(time_dependence_2.getIntegral(0.1), 0.013746670117215259, 0.000001);
93
94 // Check the amplitude
95 SinusoidalTimeDependence time_dependence_3({8.0}, {}, {2.0}, {});
96 EXPECT_NEAR(time_dependence_3.getIntegral(0.1), 0.013746670117215259 * 2, 0.000001);
97
98 // Check the phase offset
99 SinusoidalTimeDependence time_dependence_4({8.0}, {0.1}, {2.0}, {});
100 EXPECT_NEAR(time_dependence_4.getIntegral(0.1), 0.02357815814417235, 0.000001);
101
102 // Check the DC offset
103 SinusoidalTimeDependence time_dependence_5({8.0}, {0.1}, {2.0}, {-1.0});
104 EXPECT_NEAR(time_dependence_5.getIntegral(0.1), 0.02357815814417235 - 0.1, 0.000001);
105}
106
107TEST(TestSinusoidalTimeDependence, Print) {
108 int argc = 0;
109 char** argv = nullptr;
110 ippl::initialize(argc, argv);
111 std::stringstream ss;
112 Inform inform("Test", ss);
113 inform.setOutputLevel(5);
114 SinusoidalTimeDependence timeDep({8.0, 9.0}, {0.1, 0.2}, {2.0, 3.0}, {-1.0, -2.0});
115 inform << timeDep << endl;
116 EXPECT_STREQ(
117 "Test> f=[8.000000e+00, 9.000000e+00], p=[1.000000e-01, 2.000000e-01], "
118 "a=[2.000000e+00, 3.000000e+00], o=[-1.000000e+00, -2.000000e+00\nTest> \n",
119 ss.str().c_str());
120 ippl::finalize();
121}
TEST(TestSinusoidalTimeDependence, SinusoidalTimeDependenceTest)
static void setTimeDependence(const std::string &name, std::shared_ptr< AbstractTimeDependence > time_dep)
static std::string getName(const std::shared_ptr< AbstractTimeDependence > &time_dep)
static std::shared_ptr< AbstractTimeDependence > getTimeDependence(const std::string &name)
SinusoidalTimeDependence * clone() override
double getValue(double time) override