~jan.greis/maus/1811

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Interface/MathUtilsTest.cpp

  • Committer: Chris Rogers
  • Date: 2011-05-11 09:33:14 UTC
  • mfrom: (340.1.92 devel)
  • mto: This revision was merged to the branch mainline in revision 490.
  • Revision ID: chris.rogers@stfc.ac.uk-20110511093314-fcxrl52ve3pwnr6s
Virtual Planes and cpplint stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 Chris Rogers
2
 
//
3
 
// This file is a part of G4MICE
4
 
//
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.
9
 
//
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.
14
 
//
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/>.
18
 
 
19
 
#include "gtest/gtest.h" 
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 */
20
17
 
21
18
#include <math.h>
22
19
 
 
20
#include <vector>
 
21
 
 
22
#include "gtest/gtest.h"
 
23
 
23
24
#include "src/common/Interface/MathUtils.hh"
24
25
#include "src/common/Interface/STLUtils.hh"
25
26
 
96
97
// compare numerical derivative with analytical derivative
97
98
 
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;
114
 
      ASSERT_NEAR( y,
115
 
                  ((object.*function)(x+dx, n-1)
116
 
                  -(object.*function)(x-dx, n-1))/2./dx, dy_tol) << n << " " << x;
 
115
      ASSERT_NEAR(y,
 
116
                 ((object.*function)(x+dx, n-1)
 
117
                 -(object.*function)(x-dx, n-1))/2./dx, dy_tol) << n << " " << x;
117
118
    }
118
119
  }
119
120
}
191
192
  ASSERT_EQ(htv.size(), size_t(0));
192
193
}
193
194
 
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);
204
205
}
205
206
 
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);
213
214
}
214
215
 
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++)