OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestGSLIntegration.cpp
Go to the documentation of this file.
1
39#include <gtest/gtest.h>
40#include <cmath>
42
43class GSLIntegrationTest : public ::testing::Test {
44protected:
45 void SetUp() override {
46 // Test setup
47 }
48};
49
50// Test function: f(x) = x
51double linear_func(double x, void* /* params */) { return x; }
52
53// Test function: f(x) = x^2
54double quadratic_func(double x, void* /* params */) { return x * x; }
55
56// Test function: f(x) = sin(x)
57double sin_func(double x, void* /* params */) { return std::sin(x); }
58
59// Test function: f(x) = exp(x)
60double exp_func(double x, void* /* params */) { return std::exp(x); }
61
62TEST_F(GSLIntegrationTest, LinearFunction) {
65 F.params = nullptr;
66
68
69 double result, error;
70 int status = gsl_integration_qag(
71 &F, 0.0, 1.0, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
72
73 EXPECT_EQ(status, 0);
74 EXPECT_NEAR(result, 0.5, 1e-6); // ∫x dx from 0 to 1 = 0.5
75
77}
78
79TEST_F(GSLIntegrationTest, QuadraticFunction) {
82 F.params = nullptr;
83
85
86 double result, error;
87 int status = gsl_integration_qag(
88 &F, 0.0, 2.0, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
89
90 EXPECT_EQ(status, 0);
91 EXPECT_NEAR(result, 8.0 / 3.0, 1e-6); // ∫x^2 dx from 0 to 2 = 8/3
92
94}
95
99 F.params = nullptr;
100
102
103 double result, error;
104 int status = gsl_integration_qag(
105 &F, 0.0, M_PI, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
106
107 EXPECT_EQ(status, 0);
108 EXPECT_NEAR(result, 2.0, 1e-6); // ∫sin(x) dx from 0 to π = 2
109
111}
112
114 gsl_function F;
115 F.function = exp_func;
116 F.params = nullptr;
117
119
120 double result, error;
121 int status = gsl_integration_qag(
122 &F, 0.0, 1.0, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
123
124 EXPECT_EQ(status, 0);
125 EXPECT_NEAR(result, std::exp(1.0) - 1.0, 1e-6); // ∫e^x dx from 0 to 1 = e - 1
126
128}
129
130TEST_F(GSLIntegrationTest, NegativeRange) {
131 gsl_function F;
133 F.params = nullptr;
134
136
137 double result, error;
138 int status = gsl_integration_qag(
139 &F, -1.0, 1.0, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
140
141 EXPECT_EQ(status, 0);
142 EXPECT_NEAR(result, 0.0, 1e-6); // ∫x dx from -1 to 1 = 0
143
145}
146
148 gsl_function F;
150 F.params = nullptr;
151
153
154 double result, error;
155 int status = gsl_integration_qag(
156 &F, 0.0, 0.1, 1e-10, 1e-10, 1000, GSL_INTEG_GAUSS15, w, &result, &error);
157
158 EXPECT_EQ(status, 0);
159 EXPECT_NEAR(result, 0.1 * 0.1 * 0.1 / 3.0, 1e-6);
160
162}
gsl_integration_workspace * gsl_integration_workspace_alloc(size_t n)
Allocate integration workspace with limit .
int gsl_integration_qag(const gsl_function *f, double a, double b, double epsabs, double epsrel, size_t, int, gsl_integration_workspace *, double *result, double *abserr)
Adaptive integration on using Simpson refinement.
constexpr int GSL_INTEG_GAUSS15
Integration rule identifiers (accepted but not used in this implementation).
void gsl_integration_workspace_free(gsl_integration_workspace *w)
Free a workspace allocated by gsl_integration_workspace_alloc.
double linear_func(double x, void *)
double sin_func(double x, void *)
double exp_func(double x, void *)
TEST_F(GSLIntegrationTest, LinearFunction)
double quadratic_func(double x, void *)
Function wrapper used by integration routines.
double(* function)(double x, void *params)
Workspace for adaptive integration routines.