PageRenderTime 33ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/idmclib/src/traj.c

http://idmclib.googlecode.com/
C | 159 lines | 113 code | 22 blank | 24 comment | 24 complexity | fb4e248e876254e0716edb81ba15aac4 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. iDMC C library
  3. Adapted from iDMC, Copyright (C) 2004-2006 Marji Lines and Alfredo Medio
  4. Copyright (C) 2006,2007 Marji Lines and Alfredo Medio.
  5. This program 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 2 of the License, or any
  8. later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Last modified: $Date: 2007-01-06 14:59:13 +0100 (Sat, 06 Jan 2007) $
  14. */
  15. #include <stdlib.h>
  16. #include <gsl/gsl_errno.h>
  17. #include <gsl/gsl_odeiv.h>
  18. #include "model.h"
  19. #include "traj.h"
  20. #define SAFE_FREE(p) if(p!=NULL) free(p)
  21. int idmc_traj_trajectory_alloc(idmc_model *model, double *parValues, double *varValues, idmc_traj_trajectory **out_ans) {
  22. idmc_traj_trajectory *ans;
  23. *out_ans = (idmc_traj_trajectory *) calloc(1, sizeof(idmc_traj_trajectory));
  24. if((*out_ans)==NULL)
  25. return IDMC_EMEM;
  26. ans = *out_ans;
  27. ans->model = idmc_model_clone(model);
  28. if(!(ans->model)) {
  29. idmc_traj_trajectory_free(ans);
  30. return IDMC_EMEM;
  31. }
  32. ans->par = (double*) malloc(ans->model->par_len * sizeof(double));
  33. if(!(ans->par)) {
  34. idmc_traj_trajectory_free(ans);
  35. return IDMC_EMEM;
  36. }
  37. memcpy(ans->par, parValues, model->par_len * sizeof(double));
  38. ans->var = (double*) malloc(ans->model->var_len * sizeof(double));
  39. if(!ans->var) {
  40. idmc_traj_trajectory_free(ans);
  41. return IDMC_EMEM;
  42. }
  43. memcpy(ans->var, varValues, model->var_len * sizeof(double));
  44. ans->step = 0;
  45. *out_ans = ans;
  46. return IDMC_OK;
  47. }
  48. void idmc_traj_trajectory_free(idmc_traj_trajectory *traj){
  49. if(traj->model != NULL)
  50. idmc_model_free(traj->model);
  51. SAFE_FREE(traj->par);
  52. SAFE_FREE(traj->var);
  53. SAFE_FREE(traj);
  54. }
  55. int idmc_traj_trajectory_step(idmc_traj_trajectory *t) {
  56. int result = idmc_model_f(t->model, t->par, t->var /*from point*/, t->var /*to point*/);
  57. (t->step)++;
  58. return result;
  59. }
  60. /*Continuous systems*/
  61. static int odeiv_function(double t, const double yy[], double dydt[], void * params);
  62. int idmc_traj_ctrajectory_alloc(idmc_model *model, double *parValues, double *varValues,
  63. double step_size, gsl_odeiv_step_type *step_function_code, idmc_traj_ctrajectory **ans) {
  64. idmc_traj_ctrajectory *trajectory;
  65. if(model->type[0] != 'C')
  66. return IDMC_EMATH;
  67. trajectory = (idmc_traj_ctrajectory *) malloc(sizeof (idmc_traj_ctrajectory));
  68. if (trajectory == NULL) {
  69. return IDMC_EMEM;
  70. }
  71. /* model */
  72. trajectory->model = idmc_model_clone(model);
  73. if(!trajectory->model) {
  74. idmc_traj_ctrajectory_free(trajectory);
  75. return IDMC_EMEM;
  76. }
  77. /* parameters */
  78. trajectory->par = malloc(model->par_len * sizeof(double));
  79. if (trajectory->par == NULL) {
  80. idmc_traj_ctrajectory_free(trajectory);
  81. return IDMC_EMEM;
  82. }
  83. memcpy(trajectory->par, parValues, model->par_len * sizeof(double));
  84. /* initial values */
  85. trajectory->var = (double*) malloc(model->var_len * sizeof(double));
  86. if (trajectory->var == NULL) {
  87. idmc_traj_ctrajectory_free(trajectory);
  88. return IDMC_EMEM;
  89. }
  90. memcpy(trajectory->var, varValues, model->var_len * sizeof(double));
  91. /* initial values */
  92. trajectory->error = (double*) malloc(model->var_len * sizeof(double));
  93. if (trajectory->error == NULL) {
  94. idmc_traj_ctrajectory_free(trajectory);
  95. return IDMC_EMEM;
  96. }
  97. trajectory->step_function_code = step_function_code;
  98. trajectory->step_function = gsl_odeiv_step_alloc(step_function_code, model->var_len);
  99. if (trajectory->step_function == NULL) {
  100. idmc_traj_ctrajectory_free(trajectory);
  101. return IDMC_EMEM;
  102. }
  103. trajectory->step_size = step_size;
  104. /* gsl ode system */
  105. trajectory->system.function = odeiv_function;
  106. trajectory->system.jacobian = NULL; // FIXME
  107. trajectory->system.dimension = model->var_len;
  108. trajectory->system.params = trajectory;
  109. *ans = trajectory;
  110. return IDMC_OK;
  111. }
  112. void idmc_traj_ctrajectory_free(idmc_traj_ctrajectory *trajectory) {
  113. if(trajectory->model!=NULL)
  114. idmc_model_free(trajectory->model);
  115. if (trajectory->step_function!=NULL)
  116. gsl_odeiv_step_free(trajectory->step_function);
  117. SAFE_FREE(trajectory->error);
  118. SAFE_FREE(trajectory->var);
  119. SAFE_FREE(trajectory->par);
  120. SAFE_FREE(trajectory);
  121. }
  122. int idmc_traj_ctrajectory_step(idmc_traj_ctrajectory *trajectory) {
  123. return gsl_odeiv_step_apply(trajectory->step_function, 0, trajectory->step_size,
  124. trajectory->var, trajectory->error, NULL, NULL, &trajectory->system);
  125. }
  126. static int odeiv_function(double t, const double yy[], double dydt[], void * params) {
  127. idmc_traj_ctrajectory *trajectory = params;
  128. int err;
  129. err = idmc_model_f(trajectory->model, trajectory->par, yy, dydt);
  130. if (err != IDMC_OK)
  131. return GSL_EFAILED;
  132. return GSL_SUCCESS;
  133. }