1
// Copyright 2011 Chris Rogers
3
// This file is a part of G4MICE
5
// G4MICE is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
10
// G4MICE is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with G4MICE in the doc folder. If not, see
17
// <http://www.gnu.org/licenses/>.
19
#include "gtest/gtest.h"
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
3
* MAUS is free software: you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation, either version 3 of the License, or
6
* (at your option) any later version.
8
* MAUS is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with MAUS. If not, see <http://www.gnu.org/licenses/>.
22
#include "gtest/gtest.h"
23
24
#include "src/common/Interface/MathUtils.hh"
24
25
#include "src/common/Interface/STLUtils.hh"
96
97
// compare numerical derivative with analytical derivative
98
99
// check that f' < tolerance in the range xmin -> xmax, for all derivatives up to
99
// max_n (where n=1 is the first derivative). (n+1)th numerical derivative is
100
// calculated from nth analytical derivative using standard formula
100
// max_n (where n=1 is the first derivative). (n+1)th numerical derivative is
101
// calculated from nth analytical derivative using standard formula
101
102
// f' = ( f(x+dx)-f(x-dx) )/2dx
102
103
// Deal with two cases:
103
104
// * where f is large, requiring large tolerance on numerical derivative
111
112
for (double x = xmin; x < xmax; x += (xmax-xmin)/100.) {
112
113
double y = (object.*function)(x, n);
113
114
if (y*dy_tol > dy_tol) dy_tol = y*dy_tol;
115
((object.*function)(x+dx, n-1)
116
-(object.*function)(x-dx, n-1))/2./dx, dy_tol) << n << " " << x;
116
((object.*function)(x+dx, n-1)
117
-(object.*function)(x-dx, n-1))/2./dx, dy_tol) << n << " " << x;
191
192
ASSERT_EQ(htv.size(), size_t(0));
194
// check that we calculate d^n/dx^n h correctly - calculate explicitly h for a
195
// check that we calculate d^n/dx^n h correctly - calculate explicitly h for a
195
196
// values and then compare numerical and analytical derivatives
196
197
TEST_F(EngeTest, HNTest) {
197
198
for (double x = -10.; x < 11; x++) {
203
204
test_deriv(_enge, &Enge::HN, 1e-3, -10., 10., 1e-6, 9);
206
// check that we calculate d^n/dx^n g correctly - calculate explicitly g for a
207
// check that we calculate d^n/dx^n g correctly - calculate explicitly g for a
207
208
// few x values and then compare numerical and analytical derivatives
208
209
TEST_F(EngeTest, GNTest) {
209
210
for (double x = -10; x < 11; x++) {
212
213
test_deriv(_enge, &Enge::GN, 1e-6, -10., 10., 1e-6, 9);
215
// check that we calculate d^n/dx^n enge correctly - calculate explicitly g for
216
// check that we calculate d^n/dx^n enge correctly - calculate explicitly g for
216
217
// a few x values and then compare numerical and analytical derivatives
217
218
TEST_F(EngeTest, GetEngeTest) {
218
219
for (double x = -10; x < 11; x++)