OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestSplineTimeDependence.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2026, Paul Scherrer Institute, Villigen PSI, Switzerland
3// All rights reserved
4//
5// This file is part of OPAL.
6//
7// OPAL is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// You should have received a copy of the GNU General License
13// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
14//
15
19#include "gtest/gtest.h"
20
21class TestSplineTimeDependence : public testing::Test {
22public:
24 for (size_t i = 0; i < 10; ++i) {
25 times_m[i] = static_cast<double>(i * i + i);
26 values_m[i] = times_m[i]; // x^2
27 }
28 }
29
30 std::vector<double> times_m;
31 std::vector<double> values_m;
32};
33
35 try {
36 SplineTimeDependence timeDep(1, times_m, values_m);
37 for (size_t i = 0; i < values_m.size(); ++i) {
38 double test = timeDep.getValue(times_m[i]);
39 EXPECT_NEAR(test, values_m[i], 1e-9) << "Index " << i;
40 }
41 SplineTimeDependence* timeDepClone = timeDep.clone();
42 for (size_t i = 0; i < values_m.size(); ++i) {
43 double test = timeDep.getValue(times_m[i]);
44 EXPECT_NEAR(test, values_m[i], 1e-9) << "Index " << i;
45 }
46 delete timeDepClone;
47 EXPECT_THROW(SplineTimeDependence(0, times_m, values_m), std::invalid_argument);
48 EXPECT_THROW(SplineTimeDependence(2, times_m, values_m), std::invalid_argument);
49 times_m.push_back(1.);
50 EXPECT_THROW(SplineTimeDependence(1, times_m, values_m), std::invalid_argument);
51 times_m = std::vector(1, 0.);
52 values_m = std::vector(1, 0.);
53 EXPECT_THROW(SplineTimeDependence(1, times_m, values_m), std::invalid_argument);
54 times_m = {0., 1.};
55 values_m = std::vector(2, 0.);
56 EXPECT_NO_THROW(SplineTimeDependence(1, times_m, values_m));
57 times_m = {0., 1., 2.};
58 values_m = std::vector(3, 0.);
59 EXPECT_THROW(SplineTimeDependence(3, times_m, values_m), std::invalid_argument);
60 times_m = {0., 1., 2., 3.};
61 values_m = std::vector(4, 0.);
62 EXPECT_NO_THROW(SplineTimeDependence(3, times_m, values_m));
63 times_m = {0., 0., 2., 3.};
64 values_m = std::vector(4, 0.);
65 EXPECT_THROW(SplineTimeDependence(3, times_m, values_m), std::invalid_argument);
66 } catch (GeneralOpalException& exc) {
67 EXPECT_TRUE(false) << "Should not have thrown an exception:\n " << exc.what() << "\n "
68 << exc.where();
69 }
70}
71
72TEST_F(TestSplineTimeDependence, LinearLookupTest) {
73 SplineTimeDependence timeDep(1, times_m, values_m);
74 double test_x = (times_m[2] + times_m[3]) / 2.;
75 const double ref_y = (values_m[2] + values_m[3]) / 2.;
76 EXPECT_NEAR(timeDep.getValue(test_x), ref_y, 1e-9);
77 test_x = times_m[0] - (times_m[1] - times_m[0]) / 2.;
78 EXPECT_THROW(timeDep.getValue(test_x), std::invalid_argument);
79 EXPECT_THROW(timeDep.getIntegral(test_x), std::invalid_argument);
80 test_x = times_m[9] + (times_m[9] - times_m[8]) / 2.;
81 EXPECT_THROW(timeDep.getValue(test_x), std::invalid_argument);
82 EXPECT_THROW(timeDep.getIntegral(test_x), std::invalid_argument);
83}
84
86 // if I give it a quadratic or cubic, it gets the wrong answer!!
87 // I am sure that it is doing the right thing though... ahem
88 SplineTimeDependence timeDep(1, times_m, values_m);
89 for (size_t i = 0; i < times_m.size() - 1; ++i) {
90 const double test_x = (times_m[i] + times_m[i + 1]) / 2.;
91 const double ref_y = test_x;
92 const double test_y = timeDep.getValue(test_x);
93 EXPECT_NEAR(test_y, ref_y, 1e-9);
94 }
95}
96
98 SplineTimeDependence timeDep(1, times_m, values_m);
99 EXPECT_DOUBLE_EQ(timeDep.getIntegral(times_m[0]), 0.0);
100 EXPECT_DOUBLE_EQ(timeDep.getIntegral(times_m[1]), 2.0);
101 EXPECT_DOUBLE_EQ(timeDep.getIntegral(times_m[2]), 18.0);
102 EXPECT_DOUBLE_EQ(timeDep.getIntegral(times_m[3]), 72.0);
103}
104
106 int argc = 0;
107 char** argv = nullptr;
108 ippl::initialize(argc, argv);
109 std::stringstream ss;
110 Inform inform("Test", ss);
111 inform.setOutputLevel(5);
112 SplineTimeDependence timeDep(1, times_m, values_m);
113 inform << timeDep << endl;
114 EXPECT_STREQ(
115 "Test> SplineTimeDependence of order 1 with 10 entries\nTest> \n", ss.str().c_str());
116 ss.str("");
117 SplineTimeDependence timeDep2;
118 inform << timeDep2 << endl;
119 EXPECT_STREQ("Test> Uninitialised SplineTimeDependence\nTest> \n", ss.str().c_str());
120 ippl::finalize();
121}
TEST_F(TestSplineTimeDependence, ConstructorTest)
virtual const std::string & what() const
Return the message string for the exception.
virtual const std::string & where() const
Return the name of the method or function which detected the exception.
SplineTimeDependence * clone() override
double getValue(double time) override
double getIntegral(double time) override