1 /*! 2 * \file tests/Math/RungeKutta/include/runge_kutta2-header.hxx 3 * \brief 4 * 5 * \author Thomas Helfer 6 * \date 05/02/2008 7 * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights 8 * reserved. 9 * This project is publicly released under either the GNU GPL Licence 10 * or the CECILL-A licence. A copy of thoses licences are delivered 11 * with the sources of TFEL. CEA or EDF may also distribute this 12 * project under specific licensing conditions. 13 */ 14 15 #ifndef LIB_TFEL_RUNGE_KUTTA2_HEADER_HXX 16 #define LIB_TFEL_RUNGE_KUTTA2_HEADER_HXX 17 18 #include<cstdlib> 19 #include<fstream> 20 #include<string> 21 22 #include"TFEL/Math/tvector.hxx" 23 #include"TFEL/Math/RungeKutta2.hxx" 24 25 template<typename T> 26 struct Name; 27 28 template<> 29 struct Name<long double> 30 { getNameName31 static std::string getName() 32 { 33 return "long double"; 34 } 35 }; 36 37 template<> 38 struct Name<double> 39 { getNameName40 static std::string getName() 41 { 42 return "double"; 43 } 44 }; 45 46 template<> 47 struct Name<float> 48 { getNameName49 static std::string getName() 50 { 51 return "float"; 52 } 53 }; 54 55 template<typename T> 56 class VanDerPol 57 : public tfel::math::RungeKutta2<2,T,VanDerPol<T> > 58 { 59 T mu; 60 61 typedef tfel::math::tvector<2,T> vector; 62 63 public: 64 set_mu(const T mu_)65 void set_mu(const T mu_) 66 { 67 this->mu=mu_; 68 } 69 computeF(const T,const vector & x)70 TFEL_MATH_INLINE void computeF(const T, const vector& x) 71 { 72 (this->f)(0) = x(1); 73 (this->f)(1) = -x(0)+mu*x(1)*(1-x(0)*x(0)); 74 } 75 76 }; 77 78 template<typename T> 79 void test(); 80 81 template<typename T> test()82void test(){ 83 84 using namespace std; 85 using namespace tfel::math; 86 87 string name("rk2_"); 88 name += Name<T>::getName(); 89 name += ".txt"; 90 91 ofstream out(name.c_str()); 92 VanDerPol<T> rk; 93 tvector<2,T> y; 94 T begin; 95 T end; 96 97 y(0) = T{1}; 98 y(1) = T{0}; 99 100 rk.set_mu(10.f); 101 rk.set_h(1.e-2f); 102 rk.set_y(y); 103 104 begin = 0.; 105 end = 100.; 106 107 rk.set_t(begin); 108 109 out.precision(15); 110 while(rk.get_t() < end){ 111 rk.increm(); 112 out << rk.get_t() << " " << (rk.get_y())(0) << endl; 113 } 114 } 115 116 #endif /* LIB_TFEL_RUNGE_KUTTA2_HEADER_HXX */ 117