Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / ode-initval / gear1.c
1 /* ode-initval/gear1.c
2  * 
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
4  * 
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 3 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /* Gear 1. This is the implicit Euler a.k.a backward Euler method. */
21
22 /* Author:  G. Jungman
23  */
24
25 /* Error estimation by step doubling, see eg. Ascher, U.M., Petzold,
26    L.R., Computer methods for ordinary differential and
27    differential-algebraic equations, SIAM, Philadelphia, 1998.
28    The method is also described in eg. this reference.
29 */
30
31 #include <config.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <gsl/gsl_math.h>
35 #include <gsl/gsl_errno.h>
36 #include <gsl/gsl_odeiv.h>
37
38 #include "odeiv_util.h"
39
40 typedef struct
41 {
42   double *k;
43   double *y0;
44   double *y0_orig;
45   double *y_onestep;
46 }
47 gear1_state_t;
48
49 static void *
50 gear1_alloc (size_t dim)
51 {
52   gear1_state_t *state = (gear1_state_t *) malloc (sizeof (gear1_state_t));
53
54   if (state == 0)
55     {
56       GSL_ERROR_NULL ("failed to allocate space for gear1_state", GSL_ENOMEM);
57     }
58
59   state->k = (double *) malloc (dim * sizeof (double));
60
61   if (state->k == 0)
62     {
63       free (state);
64       GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM);
65     }
66
67   state->y0 = (double *) malloc (dim * sizeof (double));
68
69   if (state->y0 == 0)
70     {
71       free (state->k);
72       free (state);
73       GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
74     }
75
76   state->y0_orig = (double *) malloc (dim * sizeof (double));
77
78   if (state->y0_orig == 0)
79     {
80       free (state->y0);
81       free (state->k);
82       free (state);
83       GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
84     }
85
86   state->y_onestep = (double *) malloc (dim * sizeof (double));
87
88   if (state->y_onestep == 0)
89     {
90       free (state->y0_orig);
91       free (state->y0);
92       free (state->k);
93       free (state);
94       GSL_ERROR_NULL ("failed to allocate space for y_onestep", GSL_ENOMEM);
95     }
96
97   return state;
98 }
99
100 static int
101 gear1_step (double *y, gear1_state_t *state, 
102             const double h, const double t, 
103             const size_t dim, const gsl_odeiv_system *sys)
104 {
105   /* Makes an implicit Euler advance with step size h.
106      y0 is the initial values of variables y. 
107
108      The implicit matrix equations to solve are:
109
110      k = y0 + h * f(t + h, k)
111
112      y = y0 + h * f(t + h, k)
113   */
114
115   const int iter_steps = 3;
116   int nu;
117   size_t i;
118   double *y0 = state->y0;
119   double *k = state->k;
120
121   /* Iterative solution of k = y0 + h * f(t + h, k)
122
123      Note: This method does not check for convergence of the
124      iterative solution! 
125   */
126
127   for (nu = 0; nu < iter_steps; nu++) 
128     {
129       int s = GSL_ODEIV_FN_EVAL(sys, t + h, y, k);
130
131       if (s != GSL_SUCCESS)
132         {
133           return s;
134         }     
135
136       for (i=0; i<dim; i++) 
137         {
138           y[i] = y0[i] + h * k[i];
139         }
140     }
141   
142   return GSL_SUCCESS;
143 }
144
145 static int
146 gear1_apply(void * vstate,
147             size_t dim,
148             double t,
149             double h,
150             double y[],
151             double yerr[],
152             const double dydt_in[],
153             double dydt_out[],
154             const gsl_odeiv_system * sys)
155 {
156   gear1_state_t *state = (gear1_state_t *) vstate;
157
158   size_t i;
159
160   double *y0 = state->y0;
161   double *y0_orig = state->y0_orig;
162   double *y_onestep = state->y_onestep;
163
164   /* initialization */
165   DBL_MEMCPY(y0, y, dim);
166
167   /* Save initial values for possible failures */
168   DBL_MEMCPY (y0_orig, y, dim);
169
170   /* First traverse h with one step (save to y_onestep) */
171   DBL_MEMCPY (y_onestep, y, dim);
172
173   {
174     int s = gear1_step (y_onestep, state, h, t, dim, sys);
175
176     if (s != GSL_SUCCESS) 
177       {
178         return s;
179       }
180   }    
181
182  /* Then with two steps with half step length (save to y) */ 
183   {
184     int s = gear1_step (y, state, h / 2.0, t, dim, sys);
185
186     if (s != GSL_SUCCESS) 
187       {
188         /* Restore original y vector */
189         DBL_MEMCPY (y, y0_orig, dim);
190         return s;
191       }
192   }
193
194   DBL_MEMCPY (y0, y, dim);
195
196   {
197     int s = gear1_step (y, state, h / 2.0, t + h / 2.0, dim, sys);
198
199     if (s != GSL_SUCCESS) 
200       {
201         /* Restore original y vector */
202         DBL_MEMCPY (y, y0_orig, dim);
203         return s;
204       }
205   }
206   
207   /* Cleanup update */
208
209   if (dydt_out != NULL) 
210     {
211       int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
212       
213       if (s != GSL_SUCCESS)
214         {
215           /* Restore original y vector */
216           DBL_MEMCPY (y, y0_orig, dim);
217           return s;
218         } 
219     }
220   
221   /* Error estimation */
222
223   for (i = 0; i < dim; i++) 
224     {
225       yerr[i] = 4.0 * (y[i] - y_onestep[i]);
226     }
227
228   return GSL_SUCCESS;
229 }
230
231 static int
232 gear1_reset (void *vstate, size_t dim)
233 {
234   gear1_state_t *state = (gear1_state_t *) vstate;
235
236   DBL_ZERO_MEMSET (state->y_onestep, dim);
237   DBL_ZERO_MEMSET (state->y0_orig, dim);
238   DBL_ZERO_MEMSET (state->y0, dim);
239   DBL_ZERO_MEMSET (state->k, dim);
240   return GSL_SUCCESS;
241 }
242
243 static unsigned int
244 gear1_order (void *vstate)
245 {
246   gear1_state_t *state = (gear1_state_t *) vstate;
247   state = 0; /* prevent warnings about unused parameters */
248   return 1;
249 }
250
251 static void
252 gear1_free (void *vstate)
253 {
254   gear1_state_t *state = (gear1_state_t *) vstate;
255   free (state->y_onestep);
256   free (state->y0_orig);
257   free (state->y0);
258   free (state->k);
259   free (state);
260 }
261
262 static const gsl_odeiv_step_type gear1_type = { "gear1",        /* name */
263   0,                            /* can use dydt_in? */
264   1,                            /* gives exact dydt_out? */
265   &gear1_alloc,
266   &gear1_apply,
267   &gear1_reset,
268   &gear1_order,
269   &gear1_free
270 };
271
272 const gsl_odeiv_step_type *gsl_odeiv_step_gear1 = &gear1_type;