Project
Loading...
Searching...
No Matches
testChebyshev3D.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
22
23#define BOOST_TEST_MODULE Test Chebyshev3D
24#define BOOST_TEST_MAIN
25#define BOOST_TEST_DYN_LINK
26#include <boost/test/unit_test.hpp>
27#include <cmath>
28#include <random>
30
32
33namespace
34{
35// A smooth, low-degree (≤3 per variable) vector function over the fit box, of
36// the kind a Chebyshev parameterization reproduces to ~float precision. Stands
37// in for a slowly-varying magnetic field B(x,y,z).
38void referenceField(float* in, float* out)
39{
40 const float x = in[0], y = in[1], z = in[2];
41 out[0] = 0.50f + 0.020f * x - 1.0e-4f * x * y + 3.0e-3f * z - 2.0e-6f * x * x * z;
42 out[1] = -0.30f + 0.015f * y + 5.0e-5f * y * z - 1.0e-3f * x;
43 out[2] = 5.00f - 4.0e-4f * x * x + 6.0e-4f * y * y + 1.0e-3f * x * y - 2.0e-6f * x * y * z;
44}
45} // namespace
46
47BOOST_AUTO_TEST_CASE(Chebyshev3D_eval_accuracy)
48{
49 const Float_t bmin[3] = {-40.f, -40.f, -200.f};
50 const Float_t bmax[3] = {40.f, 40.f, 200.f};
51 const Int_t np[3] = {7, 7, 7}; // > polynomial degree in every dimension
52 const Float_t fitPrec = 1.0e-5f;
53
54 Chebyshev3D cheb(referenceField, 3, bmin, bmax, np, fitPrec);
55
56 // Deterministic interior sampling (fixed seed -> no flakiness). Stay a hair
57 // inside the box so we never hit the boundary-clamping branch.
58 std::mt19937 rng(20260604u);
59 std::uniform_real_distribution<float> ux(bmin[0] + 1.f, bmax[0] - 1.f);
60 std::uniform_real_distribution<float> uy(bmin[1] + 1.f, bmax[1] - 1.f);
61 std::uniform_real_distribution<float> uz(bmin[2] + 1.f, bmax[2] - 1.f);
62
63 float maxAbsErr = 0.f; // |cheb - reference| (kernel reproduces the function)
64 float maxDimMismatch = 0.f; // |vector overload - per-dim overload|
65 float maxDoubleMismatch = 0.f; // |float overload - double overload|
66
67 for (int i = 0; i < 20000; ++i) {
68 float par[3] = {ux(rng), uy(rng), uz(rng)};
69 float ref[3];
70 referenceField(par, ref);
71
72 float res[3];
73 cheb.Eval(par, res);
74
75 double pard[3] = {par[0], par[1], par[2]};
76 double resd[3];
77 cheb.Eval(pard, resd);
78
79 for (int d = 0; d < 3; ++d) {
80 BOOST_REQUIRE(std::isfinite(res[d]));
81 maxAbsErr = std::max(maxAbsErr, std::abs(res[d] - ref[d]));
82 // Single-component overload must match the vector overload (same kernel).
83 maxDimMismatch = std::max(maxDimMismatch, std::abs(res[d] - cheb.Eval(par, d)));
84 // Double overload differs only by intermediate precision.
85 maxDoubleMismatch = std::max(maxDoubleMismatch, std::abs(static_cast<float>(resd[d]) - res[d]));
86 }
87 }
88
89 BOOST_TEST_MESSAGE("Chebyshev3D max |eval - reference| = " << maxAbsErr);
90 BOOST_TEST_MESSAGE("Chebyshev3D max vector-vs-perdim = " << maxDimMismatch);
91 BOOST_TEST_MESSAGE("Chebyshev3D max float-vs-double = " << maxDoubleMismatch);
92
93 // Reproduction of the known function: fit precision (1e-5) plus a little float
94 // slack from the three nested Clenshaw sums (observed ~1.4e-6). A broken
95 // recurrence misses this by orders of magnitude (coefficient-scale error / NaN).
96 BOOST_CHECK_SMALL(maxAbsErr, 1.0e-4f);
97 // The two float entry points share the kernel: expect bit-for-bit agreement.
98 BOOST_CHECK_SMALL(maxDimMismatch, 1.0e-6f);
99 // float vs double evaluation of the same coefficients: within float epsilon.
100 BOOST_CHECK_SMALL(maxDoubleMismatch, 1.0e-3f);
101}
int32_t i
uint32_t res
Definition RawData.h:0
void Eval(const Float_t *par, Float_t *res)
Evaluates Chebyshev parameterization for 3d->DimOut function.
GLint GLenum GLint x
Definition glcorearb.h:403
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
BOOST_AUTO_TEST_CASE(Chebyshev3D_eval_accuracy)