antioch-0.4.0
gsl_spliner_test_base.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_GSL_SPLINER_TEST_BASE_H
28 #define ANTIOCH_GSL_SPLINER_TEST_BASE_H
29 
30 #include "antioch_config.h"
31 
32 #ifdef ANTIOCH_HAVE_GSL
33 #ifdef ANTIOCH_HAVE_CPPUNIT
34 
35 #include <cppunit/extensions/HelperMacros.h>
36 #include <cppunit/TestCase.h>
37 
38 namespace AntiochTesting
39 {
40  template<typename PairScalars>
41  struct GSLSplinerTestFunction
42  {
43  typedef typename Antioch::value_type<PairScalars>::type Scalar;
44 
45  virtual PairScalars operator()( const PairScalars x ) =0;
46 
47  void init( Scalar x_min, Scalar x_max )
48  {
49  _x_min = x_min;
50  _x_max = x_max;
51  };
52 
53  protected:
54  Scalar _x_min, _x_max;
55  };
56 
57  template<typename Scalar>
58  struct ConstantTestFunction : public GSLSplinerTestFunction<Scalar>
59  {
60  virtual Scalar operator()( const Scalar x )
61  {
62  // Scalar may actually be a PairScalar, so do this to handle those cases too.
63  return Antioch::constant_clone(x,10);
64  }
65  };
66 
67  template<typename Scalar>
68  struct LinearTestFunction : public GSLSplinerTestFunction<Scalar>
69  {
70  virtual Scalar operator()( const Scalar x )
71  {
72  // Scalar may actually be a PairScalar, so do this to handle those cases too.
73  Scalar ten = Antioch::constant_clone(x,10);
74  Scalar five = Antioch::constant_clone(x,5);
75 
76  return ten + five * x;
77  }
78  };
79 
80  template<typename Scalar>
81  struct CubicTestFunction : public GSLSplinerTestFunction<Scalar>
82  {
83  virtual Scalar operator()( const Scalar x )
84  {
85  // Scalar may actually be a PairScalar, so do this to handle those cases too.
86  Scalar one = Antioch::constant_clone(x,1);
87  Scalar two = Antioch::constant_clone(x,2);
88  Scalar three = Antioch::constant_clone(x,3);
89  Scalar xmin = Antioch::constant_clone(x,this->_x_min);
90  Scalar xmax = Antioch::constant_clone(x,this->_x_max);
91 
92  Scalar t = (x-xmin)/(xmax-xmin);
93 
94  // Constructing cubit hermite that interpolates xmin/xmax
95  // and has second derivatives of 0 at xmin,xmax
96  // Turns out you need first derivatives = 1 at the end points
97  Scalar t2 = t*t;
98  Scalar t3 = t*t*t;
99  Scalar h00 = two*t3 - three*t2 + one;
100  Scalar h10 = t3 - two*t2 + t;
101  Scalar h01 = -two*t3 + three*t2;
102  Scalar h11 = t3 - t2;
103 
104  return h00*xmin + h10*(xmax-xmin) + h01*xmax + h11*(xmax-xmin);
105  }
106  };
107 
108  template<typename Scalar>
109  class GSLSplinerTestBase : public CppUnit::TestCase
110  {
111  public:
112 
113  void init_data()
114  {
115  _n_data = 40;
116  _n_test = 39;
117  _x_ref.resize(_n_data);
118  _y_ref.resize(_n_data);
119  _x_min = -5.0L;
120  _x_max = 8.0L;
121  }
122 
123  void fill_ref(std::vector<Scalar>& x_ref, std::vector<Scalar>& y_ref,
124  unsigned int n_data, const Scalar& x_min, const Scalar& x_max,
125  GSLSplinerTestFunction<Scalar>& exact_func)
126  {
127  for(unsigned int i = 0; i < n_data; i++)
128  {
129  x_ref[i] = x_min + (Scalar)(i) * (x_max - x_min) / (Scalar)(n_data-1);
130  y_ref[i] = exact_func(x_ref[i]);
131  }
132  }
133 
134  protected:
135 
136  unsigned int _n_data;
137  unsigned int _n_test;
138  Scalar _x_min;
139  Scalar _x_max;
140  std::vector<Scalar> _x_ref;
141  std::vector<Scalar> _y_ref;
142  };
143 
144 } // end namespace AntiochTesting
145 
146 #endif // ANTIOCH_HAVE_CPPUNIT
147 #endif // ANTIOCH_HAVE_GSL
148 
149 #endif // ANTIOCH_GSL_SPLINER_TEST_BASE_H
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > constant_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex, const Scalar &value)
Definition: eigen_utils.h:181

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8