ode_solvers.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * ode_solver.h
  3. *
  4. * This file is part of the Izhikevich console application.
  5. *
  6. * Copyright (C) 2016, Author: Guido Trensch
  7. *
  8. * The Izhikevich console application is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * It is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this application. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include <math.h>
  23. #include <gsl/gsl_errno.h>
  24. #include <gsl/gsl_matrix.h>
  25. #include <gsl/gsl_odeiv.h>
  26. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  27. // = 2D ODE SOLVER: STANDARD EULER
  28. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  29. void ode2dSolver_StandardEuler( PFUNCD pODE1dxdt // IN function pointers to the 2d coupled ODE system
  30. , PFUNCD pODE2dydt // IN
  31. , float h // IN step width
  32. , float x_t // IN x(t)
  33. , float y_t // IN y(t)
  34. , float* x_tplus1 // OUT x(t+1)
  35. , float* y_tplus1 // OUT y(t+1)
  36. , void* optParm = NULL ) // IN / OUT Optional parameters
  37. {
  38. // NOTE: There is a problem with the function pointer return values.
  39. // It surprisingly works if all data types are double instead of float !!!
  40. // To circumvent the problem the functions are called directly.
  41. // *x_tplus1 = x_t + h * pODE1dxdt( x_t, y_t, optParm );
  42. *x_tplus1 = x_t + h * dvdt( x_t, y_t, optParm );
  43. // *y_tplus1 = y_t + h * pODE2dydt( x_t, y_t, optParm );
  44. *y_tplus1 = y_t + h * dudt( x_t, y_t );
  45. return;
  46. }
  47. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  48. // = 2D ODE SOLVER: SYMPLECTIC EULER
  49. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  50. void ode2dSolver_SymplecticEuler( PFUNCD pODE1dxdt // IN function pointers to the 2d coupled ODE system
  51. , PFUNCD pODE2dydt // IN
  52. , float h // IN step width
  53. , float x_t // IN x(t)
  54. , float y_t // IN y(t)
  55. , float* x_tplus1 // OUT x(t+1)
  56. , float* y_tplus1 // OUT y(t+1)
  57. , void* optParm = NULL ) // IN / OUT Optional parameters
  58. {
  59. // NOTE: There is a problem with the function pointer return values.
  60. // It surprisingly works if all data types are double instead of float !!!
  61. // To circumvent the problem the functions are called directly.
  62. // *x_tplus1 = x_t + h * pODE1dxdt( x_t, y_t, optParm );
  63. *x_tplus1 = x_t + h * dvdt( x_t, y_t, optParm );
  64. // *y_tplus1 = y_t + h * pODE2dydt( *x_tplus1, *y_tplus1, optParm ); // symplectic
  65. *y_tplus1 = y_t + h * dudt( *x_tplus1, *y_tplus1 ); // symplectic
  66. return;
  67. }
  68. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  69. // = EXPLIZIT SOLVER FOR THE IZHIKEVICH ODEs
  70. // = SpiNNaker implementation
  71. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  72. void explizitSpiNNaker( float h
  73. , float* v
  74. , float* u
  75. , float i )
  76. {
  77. float lastV1 = *v;
  78. float lastU1 = *u;
  79. float a = IZHIKEVICH_A;
  80. float b = IZHIKEVICH_B;
  81. float input_this_timestep = i;
  82. float pre_alph = (140.0) + input_this_timestep - lastU1;
  83. float alpha = pre_alph + ( (5.0) + (0.0400) * lastV1) * lastV1;
  84. float eta = lastV1 + 0.5*(h * alpha);
  85. float beta = 0.5*(h * (b * lastV1 - lastU1) * a);
  86. *v += h * (pre_alph - beta + ( (5.0) + (0.0400) * eta) * eta);
  87. *u += a * h * (-lastU1 - beta + b * eta);
  88. threshold( v, u );
  89. return;
  90. }
  91. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  92. // = EXPLIZIT SOLVER FOR THE IZHIKEVICH ODEs
  93. // = explizit euler ( NEST implementation 1 )
  94. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  95. void explizitNEST1( float h
  96. , float* v
  97. , float* u
  98. , float i )
  99. {
  100. float v_old = *v;
  101. float u_old = *u;
  102. float a = IZHIKEVICH_A;
  103. float b = IZHIKEVICH_B;
  104. *v += h * ( 0.04 * v_old * v_old + 5.0 * v_old + 140.0 - u_old + i );
  105. *u += h * a * ( b * v_old - u_old );
  106. threshold( v, u );
  107. return;
  108. }
  109. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  110. // = EXPLIZIT SOLVER FOR THE IZHIKEVICH ODEs
  111. // = Izhikevich numerics from 2003 paper ( NEST implementation 2 )
  112. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  113. void explizitNEST2( float h
  114. , float* v
  115. , float* u
  116. , float i )
  117. {
  118. float v_old = *v;
  119. float u_old = *u;
  120. float a = IZHIKEVICH_A;
  121. float b = IZHIKEVICH_B;
  122. *v += h * 0.5 * ( 0.04 * v_old * v_old + 5.0 * v_old + 140.0 - u_old + i ); // for numetrical stablility
  123. *v += h * 0.5 * ( 0.04 * v_old * v_old + 5.0 * v_old + 140.0 - u_old + i ); // see Izhikevich 2003
  124. *u += h * a * ( b * v_old - u_old );
  125. threshold( v, u );
  126. return;
  127. }
  128. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  129. // = GNU Scientific Library
  130. // = Based on the code generated by NESTML
  131. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  132. int gslODESolver_EquationDynamics( double t, const double y[], double f[], void* params );
  133. const int V_m_INDEX = 0;
  134. const int U_m_INDEX = 1;
  135. float param_I_e = 0;
  136. static int countGSL = 0;
  137. // gsl_odeiv.h
  138. // typedef struct {
  139. // int (* function) (float t, const float y[], float dydt[], void * params);
  140. // int (* jacobian) (float t, const float y[], float * dfdy, float dfdt[], void * params);
  141. // size_t dimension;
  142. // void * params;
  143. // }
  144. // gsl_odeiv_system;
  145. gsl_odeiv_system systemOfODEs = { 0 };
  146. gsl_odeiv_step* pSteppingFunction = NULL;
  147. gsl_odeiv_control* pControlObject = NULL;
  148. gsl_odeiv_evolve* pEvolutionFunction = NULL;
  149. void gslODESolver_Integrate( float h
  150. , float* v
  151. , float* u
  152. , float i )
  153. {
  154. int gslRc = GSL_SUCCESS;
  155. double t = 0;
  156. double t1 = h;
  157. double stateVector[2];
  158. double stepSize = h; // stepSize will be adjusted by the evolve-function
  159. param_I_e = i;
  160. stateVector[V_m_INDEX] = *v;
  161. stateVector[U_m_INDEX] = *u;
  162. while( t < t1 ) { // There are 3 loops !!!
  163. // - The outer loop of the simulation.
  164. // - This loop for t < stepSize < t1. Where stepSize is adjusted each iteration.
  165. // - The loop within evolve calling the ODEs "systemOfODEs.function = &gslODESolver_EquationDynamics"
  166. gslRc = gsl_odeiv_evolve_apply( pEvolutionFunction
  167. , pControlObject // accuracy
  168. , pSteppingFunction // the algorithm
  169. , &systemOfODEs // equation
  170. , &t
  171. , t1
  172. , &stepSize
  173. , stateVector ); // needs to be updated in the equations, it is reset in between
  174. // It won't work to pass parameters, like synapse currents, to the equations
  175. // using the
  176. if( gslRc != GSL_SUCCESS ) {
  177. printf( "ERROR: gsl error\n" );
  178. exit( -1 );
  179. }
  180. // Threshold
  181. if( stateVector[V_m_INDEX] >= IZHIKEVICH_THR ) {
  182. stateVector[V_m_INDEX] = IZHIKEVICH_C;
  183. stateVector[U_m_INDEX] += IZHIKEVICH_D;
  184. }
  185. }
  186. *v = stateVector[V_m_INDEX];
  187. *u = stateVector[U_m_INDEX];
  188. return;
  189. }
  190. void gslODESolver_Init()
  191. {
  192. pSteppingFunction = gsl_odeiv_step_alloc( gsl_odeiv_step_rkf45, 2 ); // instance of a stepping function for a two dimensinal system
  193. pControlObject = gsl_odeiv_control_y_new( 1e-6, 0.0 ); // create a new control object which will keep the local error on each step
  194. // within an absolute error 1e-6 and relative error of 0.0 with respect to
  195. // the solution y_i(t)
  196. pEvolutionFunction = gsl_odeiv_evolve_alloc ( 2 ); // instance of an evolution function for a two dimensinal system
  197. systemOfODEs.function = &gslODESolver_EquationDynamics;
  198. systemOfODEs.jacobian = NULL;
  199. systemOfODEs.dimension = 2;
  200. systemOfODEs.params = &param_I_e;
  201. return;
  202. }
  203. int gslODESolver_EquationDynamics( double t, const double y[], double f[], void* params )
  204. {
  205. float I_e = *(float*)params;
  206. f[ V_m_INDEX ] = 0.04 * y[V_m_INDEX] * y[V_m_INDEX] + 5.0 * y[V_m_INDEX] + 140 - y[U_m_INDEX] + I_e;
  207. f[ U_m_INDEX ] = IZHIKEVICH_A * ( IZHIKEVICH_B * y[V_m_INDEX] - y[U_m_INDEX] );
  208. countGSL++;
  209. printf( "countGSL: %d\n", countGSL );
  210. return GSL_SUCCESS;
  211. }
  212. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  213. // = Heun
  214. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  215. void ode2dSolver_Heun( PFUNCD pODE1dxdt // IN function pointers to the 2d coupled ODE system
  216. , PFUNCD pODE2dydt // IN
  217. , float h // IN step width
  218. , float x_t // IN x(t)
  219. , float y_t // IN y(t)
  220. , float* x_tplus1 // OUT x(t+1)
  221. , float* y_tplus1 // OUT y(t+1)
  222. , void* optParm = NULL ) // IN / OUT Optional parameters
  223. {
  224. // NOTE: There is a problem with the function pointer return values.
  225. // It surprisingly works if all data types are double instead of float !!!
  226. // To circumvent the problem the functions are called directly.
  227. // float x_Euler = x_t + h * pODE1dxdt( x_t, y_t, optParm );
  228. float x_Euler = x_t + h * dvdt( x_t, y_t, optParm );
  229. // float y_Euler = y_t + h * pODE2dydt( x_t, y_t, optParm );
  230. float y_Euler = y_t + h * dudt( x_t, y_t );
  231. // *x_tplus1 = x_t + h * pODE1dxdt( 0.5 * (x_t + x_Euler), 0.5 * (y_t + y_Euler), optParm );
  232. *x_tplus1 = x_t + h * dvdt( 0.5 * (x_t + x_Euler), 0.5 * (y_t + y_Euler), optParm );
  233. // *y_tplus1 = y_t + h * pODE2dydt( 0.5 * (x_t + x_Euler), 0.5 * (y_t + y_Euler), optParm );
  234. *y_tplus1 = y_t + h * dudt( 0.5 * (x_t + x_Euler), 0.5 * (y_t + y_Euler) );
  235. return;
  236. }
  237. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  238. // = Standard Euler with adaptive stepsize
  239. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  240. void ode2dSolver_AdaptiveEuler( PFUNCD pODE1dxdt // IN function pointers to the 2d coupled ODE system
  241. , PFUNCD pODE2dydt // IN
  242. , float h // IN step width
  243. , float x_t // IN x(t)
  244. , float y_t // IN y(t)
  245. , float* x_tplus1 // OUT x(t+1)
  246. , float* y_tplus1 // OUT y(t+1)
  247. , void* optParm = NULL ) // IN / OUT Optional parameters
  248. {
  249. float t = 0;
  250. float stepSize = h; // initial stepsize, will be adjusted in every step
  251. float x_interm = x_t;
  252. float y_interm = y_t;
  253. // determine integration step size
  254. float error_accepted = 0.2;
  255. float error_calculated = 0;
  256. int k = 1;
  257. float x_hi;
  258. static float simulationTime_ms = 0;
  259. int count1 = 0;
  260. int count2 = 0;
  261. static int count3 = 0;
  262. static int count4 = 0;
  263. float tempError[32] = { 0.0 };
  264. int index = 0;
  265. int i = 0;
  266. int fSpike = FALSE;
  267. if( count4 == 0 ) {
  268. printf( "[INITIAL VALUES] h: %11.6f x_t: %11.6f y_t: %11.6f \n", h, x_t, y_t );
  269. }
  270. do {
  271. stepSize = h/k;
  272. x_hi = x_t + stepSize * dvdt( x_t, y_t, optParm );
  273. error_calculated = fabs(fabs(x_hi) - fabs(x_t));
  274. if( index == 31 ) {
  275. tempError[ index ] = 99.99;
  276. }
  277. else {
  278. tempError[ index ] = error_calculated;
  279. index++;
  280. }
  281. k *= 2;
  282. count1++;
  283. } while(!(( error_calculated < error_accepted ) || ( stepSize <= 0.001 )));
  284. // if( stepSize < 0.001 ) stepSize = 0.001;
  285. while( t < h ) {
  286. x_interm = x_interm + stepSize * dvdt( x_interm, y_interm, optParm );
  287. y_interm = y_interm + stepSize * dudt( x_interm, y_interm );
  288. fSpike = threshold( &x_interm, &y_interm );
  289. t += stepSize;
  290. count2++;
  291. count3++;
  292. if( fSpike ) {
  293. printf( "[%8.3f] > > > S P I K E < < < + %11.6f ms \n", simulationTime_ms, t );
  294. // break;
  295. }
  296. // t = roundf( t * 1000 ) / 1000;
  297. };
  298. #if( 0 )
  299. printf( "[%8.3f] V: %11.6f U: %11.6f stepSize: %11.6f CNT(find stepSize): %6d CNT(calc): %6d CNT(total): %6d "
  300. , simulationTime_ms, x_interm, y_interm, stepSize, count1, count2, count3 );
  301. printf( " ERRORs: " );
  302. for( i = 0; i < index; ++i ) {
  303. printf( "%11.6f, ", tempError[ i ] );
  304. }
  305. printf( "\n" );
  306. #endif
  307. *x_tplus1 = x_interm;
  308. *y_tplus1 = y_interm;
  309. count4++;
  310. simulationTime_ms += h;
  311. return;
  312. }