antioch-0.4.0
gsl_spliner_vec_test.C
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 #include "antioch_config.h"
28 
29 #ifdef ANTIOCH_HAVE_GSL
30 #ifdef ANTIOCH_HAVE_CPPUNIT
31 
32 // C++
33 #include <limits>
34 #include <cmath>
35 
36 #ifdef ANTIOCH_HAVE_EIGEN
37 #include "Eigen/Dense"
38 #endif
39 
40 #ifdef ANTIOCH_HAVE_METAPHYSICL
41 #include "metaphysicl/numberarray.h"
42 #endif
43 
44 #ifdef ANTIOCH_HAVE_VEXCL
45 #include "vexcl/vexcl.hpp"
46 #endif
47 
48 // Antioch
54 #include "antioch/gsl_spliner.h"
55 #include "antioch/vector_utils.h"
56 #include "antioch/eigen_utils.h"
58 #include "antioch/vexcl_utils.h"
59 #include "antioch/valarray_utils.h"
60 
61 // Base class
62 #include "gsl_spliner_test_base.h"
63 
64 namespace AntiochTesting
65 {
66  template<typename PairScalars>
67  class GSLSplinerVecTest : public GSLSplinerTestBase<typename Antioch::value_type<PairScalars>::type>
68  {
69  public:
70 
71  typedef typename Antioch::value_type<PairScalars>::type Scalar;
72 
73  void init_vec_data()
74  {
75  this->init_data();
76  }
77 
78  // Helper function
79  template<typename VecFunctionType, typename ScalarFunctionType>
80  void run_manually_inited_test( Scalar tol )
81  {
82  ScalarFunctionType exact_func;
83  exact_func.init(this->_x_min, this->_x_max);
84 
85  this->fill_ref(this->_x_ref,this->_y_ref,this->_n_data, this->_x_min, this->_x_max, exact_func );
86 
87  Antioch::GSLSpliner spline;
88  spline.spline_init(this->_x_ref, this->_y_ref);
89 
90  VecFunctionType vec_exact_func;
91  vec_exact_func.init(this->_x_min, this->_x_max);
92 
93  this->compare_values( tol, spline, vec_exact_func );
94  }
95 
96  // Helper function
97  template<typename VecFunctionType, typename ScalarFunctionType>
98  void run_constructor_inited_test( Scalar tol )
99  {
100  ScalarFunctionType exact_func;
101  exact_func.init(this->_x_min, this->_x_max);
102 
103  this->fill_ref(this->_x_ref,this->_y_ref,this->_n_data, this->_x_min, this->_x_max, exact_func );
104 
105  Antioch::GSLSpliner spline(this->_x_ref, this->_y_ref);
106 
107  VecFunctionType vec_exact_func;
108  vec_exact_func.init(this->_x_min, this->_x_max);
109 
110  this->compare_values( tol, spline, vec_exact_func );
111  }
112 
113  // Helper function
114  void compare_values( Scalar tol,
115  Antioch::GSLSpliner& spline,
116  GSLSplinerTestFunction<PairScalars>& exact_func)
117  {
118  // Construct from example to avoid resizing issues
119  PairScalars x = *(this->_example);
120 
121  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
122  {
123  x[2*tuple] = -3.5;
124  x[2*tuple+1] = 5.1;
125  }
126 
127  const PairScalars gsl_value = spline.interpolated_value(x);
128 
129  const PairScalars exact_value = exact_func(x);
130 
131  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
132  {
133  CPPUNIT_ASSERT_DOUBLES_EQUAL( gsl_value[2*tuple],
134  exact_value[2*tuple],
135  tol );
136  }
137  }
138 
139  void test_manually_inited_spline_constant_func()
140  {
141  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 10;
142 
143  this->run_manually_inited_test<ConstantTestFunction<PairScalars>,ConstantTestFunction<Scalar> >(tol);
144  }
145 
146  void test_constructor_inited_spline_constant_func()
147  {
148  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 10;
149 
150  this->run_constructor_inited_test<ConstantTestFunction<PairScalars>,ConstantTestFunction<Scalar> >(tol);
151  }
152 
153  void test_manually_inited_spline_linear_func()
154  {
155  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 50;
156 
157  this->run_manually_inited_test<LinearTestFunction<PairScalars>,LinearTestFunction<Scalar> >(tol);
158  }
159 
160  void test_constructor_inited_spline_linear_func()
161  {
162  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 50;
163 
164  this->run_constructor_inited_test<LinearTestFunction<PairScalars>,LinearTestFunction<Scalar> >(tol);
165  }
166 
167  void test_manually_inited_spline_cubic_func()
168  {
169  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 50;
170 
171  this->run_manually_inited_test<CubicTestFunction<PairScalars>,CubicTestFunction<Scalar> >(tol);
172  }
173 
174  void test_constructor_inited_spline_cubic_func()
175  {
176  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 50;
177 
178  this->run_constructor_inited_test<CubicTestFunction<PairScalars>,CubicTestFunction<Scalar> >(tol);
179  }
180 
181  protected:
182  // Should be new'd/deleted in subclasses for each PairScalar type
183  PairScalars* _example;
184  };
185 
186  //----------------------------------------------------------------------
187  // valarray tests
188  //----------------------------------------------------------------------
189  template <typename Scalar>
190  class GslSplinerValarrayTest : public GSLSplinerVecTest<std::valarray<Scalar> >
191  {
192  public:
193 
194  virtual void setUp()
195  {
196  this->init_vec_data();
197  this->_example = new std::valarray<Scalar>(2*ANTIOCH_N_TUPLES);
198  }
199 
200  virtual void tearDown()
201  {
202  delete this->_example;
203  }
204 
205  };
206 
207  class GslSplinerValarrayFloatTest : public GslSplinerValarrayTest<float>
208  {
209  CPPUNIT_TEST_SUITE( GslSplinerValarrayFloatTest );
210 
211  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
212  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
213  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
214  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
215  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
216  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
217 
218  CPPUNIT_TEST_SUITE_END();
219  };
220 
221  class GslSplinerValarrayDoubleTest : public GslSplinerValarrayTest<double>
222  {
223  CPPUNIT_TEST_SUITE( GslSplinerValarrayDoubleTest );
224 
225  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
226  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
227  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
228  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
229  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
230  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
231 
232  CPPUNIT_TEST_SUITE_END();
233  };
234 
235  class GslSplinerValarrayLongDoubleTest : public GslSplinerValarrayTest<long double>
236  {
237  CPPUNIT_TEST_SUITE( GslSplinerValarrayLongDoubleTest );
238 
239  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
240  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
241  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
242  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
243  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
244  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
245 
246  CPPUNIT_TEST_SUITE_END();
247  };
248 
249  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerValarrayFloatTest );
250  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerValarrayDoubleTest );
251  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerValarrayLongDoubleTest );
252 
253  //----------------------------------------------------------------------
254  // Eigen tests
255  //----------------------------------------------------------------------
256 #ifdef ANTIOCH_HAVE_EIGEN
257 
258  template <typename Scalar>
259  class GslSplinerEigenTest : public GSLSplinerVecTest<Eigen::Array<Scalar,2*ANTIOCH_N_TUPLES,1> >
260  {
261  public:
262  virtual void setUp()
263  {
264  this->init_vec_data();
265  this->_example = new Eigen::Array<Scalar, 2*ANTIOCH_N_TUPLES, 1>();
266  }
267 
268  virtual void tearDown()
269  {
270  delete this->_example;
271  }
272  };
273 
274  class GslSplinerEigenFloatTest : public GslSplinerEigenTest<float>
275  {
276  CPPUNIT_TEST_SUITE( GslSplinerEigenFloatTest );
277 
278  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
279  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
280  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
281  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
282  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
283  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
284 
285  CPPUNIT_TEST_SUITE_END();
286  };
287 
288  class GslSplinerEigenDoubleTest : public GslSplinerEigenTest<double>
289  {
290  CPPUNIT_TEST_SUITE( GslSplinerEigenDoubleTest );
291 
292  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
293  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
294  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
295  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
296  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
297  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
298 
299  CPPUNIT_TEST_SUITE_END();
300  };
301 
302  class GslSplinerEigenLongDoubleTest : public GslSplinerEigenTest<long double>
303  {
304  CPPUNIT_TEST_SUITE( GslSplinerEigenLongDoubleTest );
305 
306  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
307  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
308  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
309  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
310  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
311  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
312 
313  CPPUNIT_TEST_SUITE_END();
314  };
315 
316  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerEigenFloatTest );
317  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerEigenDoubleTest );
318  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerEigenLongDoubleTest );
319 
320 #endif // ANTIOCH_HAVE_EIGEN
321 
322 
323  //----------------------------------------------------------------------
324  // MetaPhysicL tests
325  //----------------------------------------------------------------------
326 #ifdef ANTIOCH_HAVE_METAPHYSICL
327 
328  template <typename Scalar>
329  class GslSplinerMetaPhysicLTest : public GSLSplinerVecTest<MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES,Scalar> >
330  {
331  public:
332  virtual void setUp()
333  {
334  this->init_vec_data();
335  this->_example = new MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES,Scalar>(0);
336  }
337 
338  virtual void tearDown()
339  {
340  delete this->_example;
341  }
342  };
343 
344  class GslSplinerMetaPhysicLFloatTest : public GslSplinerMetaPhysicLTest<float>
345  {
346  CPPUNIT_TEST_SUITE( GslSplinerMetaPhysicLFloatTest );
347 
348  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
349  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
350  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
351  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
352  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
353  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
354 
355  CPPUNIT_TEST_SUITE_END();
356  };
357 
358  class GslSplinerMetaPhysicLDoubleTest : public GslSplinerMetaPhysicLTest<double>
359  {
360  CPPUNIT_TEST_SUITE( GslSplinerMetaPhysicLDoubleTest );
361 
362  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
363  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
364  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
365  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
366  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
367  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
368 
369  CPPUNIT_TEST_SUITE_END();
370  };
371 
372  class GslSplinerMetaPhysicLLongDoubleTest : public GslSplinerMetaPhysicLTest<long double>
373  {
374  CPPUNIT_TEST_SUITE( GslSplinerMetaPhysicLLongDoubleTest );
375 
376  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
377  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
378  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
379  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
380  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
381  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
382 
383  CPPUNIT_TEST_SUITE_END();
384  };
385 
386  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerMetaPhysicLFloatTest );
387  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerMetaPhysicLDoubleTest );
388  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerMetaPhysicLLongDoubleTest );
389 
390 #endif // ANTIOCH_HAVE_METAPHYSICL
391 
392 
393  //----------------------------------------------------------------------
394  // VexCL tests
395  //----------------------------------------------------------------------
396 #ifdef ANTIOCH_HAVE_VEXCL
397 
398  class GslSplinerVexCLFloatTest : public GSLSplinerVecTest<vex::vector<float> >
399  {
400  CPPUNIT_TEST_SUITE( GslSplinerVexCLFloatTest );
401 
402  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
403  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
404  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
405  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
406  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
407  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
408 
409  CPPUNIT_TEST_SUITE_END();
410 
411  private:
412 
413  vex::Context* _ctx;
414 
415  public:
416 
417  virtual void setUp()
418  {
419  this->init_vec_data();
420  _ctx = new vex::Context(vex::Filter::All);
421  this->_example = new vex::vector<float>(*_ctx, 2*ANTIOCH_N_TUPLES);
422  }
423 
424  virtual void tearDown()
425  {
426  delete _ctx;
427  delete this->_example;
428  }
429  };
430 
431  class GslSplinerVexCLDoubleTest : public GSLSplinerVecTest<vex::vector<double> >
432  {
433  CPPUNIT_TEST_SUITE( GslSplinerVexCLDoubleTest );
434 
435  CPPUNIT_TEST( test_manually_inited_spline_constant_func );
436  CPPUNIT_TEST( test_constructor_inited_spline_constant_func );
437  CPPUNIT_TEST( test_manually_inited_spline_linear_func );
438  CPPUNIT_TEST( test_constructor_inited_spline_linear_func );
439  CPPUNIT_TEST( test_manually_inited_spline_cubic_func );
440  CPPUNIT_TEST( test_constructor_inited_spline_cubic_func );
441 
442  CPPUNIT_TEST_SUITE_END();
443 
444  private:
445 
446  vex::Context* _ctx;
447 
448  public:
449 
450  virtual void setUp()
451  {
452  this->init_vec_data();
453  _ctx = new vex::Context(vex::Filter::DoublePrecision);
454  this->_example = new vex::vector<double>(*_ctx, 2*ANTIOCH_N_TUPLES);
455  }
456 
457  virtual void tearDown()
458  {
459  delete _ctx;
460  delete this->_example;
461  }
462  };
463 
464  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerVexCLFloatTest );
465  CPPUNIT_TEST_SUITE_REGISTRATION( GslSplinerVexCLDoubleTest );
466 
467 #endif // ANTIOCH_HAVE_VEXCL
468 
469 } // end namespace AntiochTesting
470 
471 #endif // ANTIOCH_HAVE_CPPUNIT
472 #endif // ANTIOCH_HAVE_GSL
CPPUNIT_TEST_SUITE_REGISTRATION(ArrheniusRateEigenFloatTest)

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