Debian Codegraph @

external/gsl/gsl_ode-initval__gear2.c

raw symbols SCIP
1 /* ode-initval/gear2.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 2 */
21
22 /* Author: G. Jungman
23 */
24 #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/
25 #include <stdlib.h>
26 #include <string.h>
27 #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/
28 #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/
29 #include "gsl_ode-initval__odeiv_util.h"cxx . . $ `<file>/external/gsl/gsl_ode-initval__odeiv_util.h`/
30 #include "gsl_odeiv.h"cxx . . $ `<file>/external/gsl/gsl_odeiv.h`/
31
32
33 /* gear2 state object */
34 typedef structcxx . . $ $anonymous_type_803fe5c260ceba4c_0#
35 {
36 int primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#primed.; /* flag indicating that yim1 is ready */
37 double t_primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#t_primed.; /* system was primed for this value of t */
38 double last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h.; /* last step size */
39 gsl_odeiv_stepcxx . . $ gsl_odeiv_step# *primercxx . . $ $anonymous_type_803fe5c260ceba4c_0#primer.; /* stepper to use for priming */
40 double *yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.; /* y_{i-1} */
41 double *kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.; /* work space */
42 double *y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.; /* work space */
43 double *y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig.;
44 double *y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep.;
45 int stuttercxx . . $ $anonymous_type_803fe5c260ceba4c_0#stutter.;
46 }
47 gear2_state_tcxx . . $ gear2_state_t#;
48
49 static void *
50 gear2_alloccxx . . $ gear2_alloc(bfcfa4d6b7f7ef64). (size_t dim)
51 {
52 gear2_state_tcxx . . $ gear2_state_t# *state = (gear2_state_tcxx . . $ gear2_state_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (gear2_state_tcxx . . $ gear2_state_t#));
53
54 if (state == 0)
55 {
56 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for gear2_state", GSL_ENOMEM);
57 }
58
59 state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
60
61 if (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1. == 0)
62 {
63 freecxx . . $ free(5d22bdacc48458e8). (state);
64 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for yim1", GSL_ENOMEM);
65 }
66
67 state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
68
69 if (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k. == 0)
70 {
71 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
72 freecxx . . $ free(5d22bdacc48458e8). (state);
73 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for k", GSL_ENOMEM);
74 }
75
76 state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
77
78 if (state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0. == 0)
79 {
80 freecxx . . $ free(5d22bdacc48458e8). (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.);
81 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
82 freecxx . . $ free(5d22bdacc48458e8). (state);
83 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0", GSL_ENOMEM);
84 }
85
86 state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
87
88 if (state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig. == 0)
89 {
90 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.);
91 freecxx . . $ free(5d22bdacc48458e8). (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.);
92 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
93 freecxx . . $ free(5d22bdacc48458e8). (state);
94 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0_orig", GSL_ENOMEM);
95 }
96
97 state->y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
98
99 if (state->y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep. == 0)
100 {
101 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig.);
102 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.);
103 freecxx . . $ free(5d22bdacc48458e8). (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.);
104 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
105 freecxx . . $ free(5d22bdacc48458e8). (state);
106 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0_orig", GSL_ENOMEM);
107 }
108
109 state->primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#primed. = 0;
110 state->primercxx . . $ $anonymous_type_803fe5c260ceba4c_0#primer. = gsl_odeiv_step_alloccxx . . $ gsl_odeiv_step_alloc(f35891c0b7f78d81). (gsl_odeiv_step_rk4impcxx . . $ gsl_odeiv_step_rk4imp., dim);
111
112 if (state->primercxx . . $ $anonymous_type_803fe5c260ceba4c_0#primer. == 0)
113 {
114 freecxx . . $ free(5d22bdacc48458e8). (state->y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep.);
115 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig.);
116 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.);
117 freecxx . . $ free(5d22bdacc48458e8). (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.);
118 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
119 freecxx . . $ free(5d22bdacc48458e8). (state);
120 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for primer", GSL_ENOMEM);
121 }
122
123 state->last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h. = 0.0;
124
125 return state;
126 }
127
128 static int
129 gear2_stepcxx . . $ gear2_step(7b673b29aa8d5e27). (double *y, gear2_state_tcxx . . $ gear2_state_t# * state,
130 const double h, const double t,
131 const size_t dim, const gsl_odeiv_systemcxx . . $ gsl_odeiv_system# * sys)
132 {
133 /* Makes a Gear2 advance with step size h.
134 y0 is the initial values of variables y.
135 The implicit matrix equations to solve are:
136 k = y0 + h * f(t + h, k)
137 y = y0 + h * f(t + h, k)
138 */
139
140 const int iter_steps = 3;
141 int nu;
142 size_t i;
143 double *y0 = state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.;
144 double *yim1 = state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.;
145 double *k = state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.;
146
147 /* Iterative solution of k = y0 + h * f(t + h, k)
148 Note: This method does not check for convergence of the
149 iterative solution!
150 */
151
152 for (nu = 0; nu < iter_steps; nu++)
153 {
154 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h, y, k);
155
156 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
157 {
158 return s;
159 }
160
161 for (i = 0; i < dim; i++)
162 {
163 y[i] = ((4.0 * y0[i] - yim1[i]) + 2.0 * h * k[i]) / 3.0;
164 }
165 }
166
167 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
168 }
169
170 static int
171 gear2_applycxx . . $ gear2_apply(46f97775c18f9353). (void *vstate,
172 size_t dim,
173 double t,
174 double h,
175 double y[],
176 double yerr[],
177 const double dydt_in[],
178 double dydt_out[], const gsl_odeiv_systemcxx . . $ gsl_odeiv_system# * sys)
179 {
180 gear2_state_tcxx . . $ gear2_state_t# *state = (gear2_state_tcxx . . $ gear2_state_t# *) vstate;
181
182 state->stuttercxx . . $ $anonymous_type_803fe5c260ceba4c_0#stutter. = 0;
183
184 if (state->primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#primed. == 0 || t == state->t_primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#t_primed. || h != state->last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h.)
185 {
186 /* Execute a single-step method to prime the process. Note that
187 * we do this if the step size changes, so frequent step size
188 * changes will cause the method to stutter.
189 *
190 * Note that we reuse this method if the time has not changed,
191 * which can occur when the adaptive driver is attempting to find
192 * an appropriate step-size on its first iteration */
193
194 int status;
195 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (state->yim1, y, dim);
196
197 status =
198 gsl_odeiv_step_applycxx . . $ gsl_odeiv_step_apply(abc4fc936a94a578). (state->primercxx . . $ $anonymous_type_803fe5c260ceba4c_0#primer., t, h, y, yerr, dydt_in, dydt_out,
199 sys);
200
201 /* Make note of step size and indicate readiness for a Gear step. */
202
203 state->primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#primed. = 1;
204 state->t_primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#t_primed. = t;
205 state->last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h. = h;
206 state->stuttercxx . . $ $anonymous_type_803fe5c260ceba4c_0#stutter. = 1;
207
208 return status;
209 }
210 else
211 {
212 /* We have a previous y value in the buffer, and the step
213 * sizes match, so we go ahead with the Gear step.
214 */
215
216 double *const k = state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.;
217 double *const y0 = state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.;
218 double *const y0_orig = state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig.;
219 double *const yim1 = state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.;
220 double *y_onestep = state->y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep.;
221
222 int s;
223 size_t i;
224
225 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
226
227 /* iterative solution */
228
229 if (dydt_out != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
230 {
231 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (k, dydt_out, dim);
232 }
233
234 /* First traverse h with one step (save to y_onestep) */
235
236 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y_onestep, y, dim);
237
238 s = gear2_stepcxx . . $ gear2_step(7b673b29aa8d5e27). (y_onestep, state, h, t, dim, sys);
239
240 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
241 {
242 return s;
243 }
244
245 /* Then with two steps with half step length (save to y) */
246
247 s = gear2_stepcxx . . $ gear2_step(7b673b29aa8d5e27). (y, state, h / 2.0, t, dim, sys);
248
249 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
250 {
251 /* Restore original y vector */
252 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
253 return s;
254 }
255
256 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
257
258 s = gear2_stepcxx . . $ gear2_step(7b673b29aa8d5e27). (y, state, h / 2.0, t + h / 2.0, dim, sys);
259
260 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
261 {
262 /* Restore original y vector */
263 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
264 return s;
265 }
266
267 /* Cleanup update */
268
269 if (dydt_out != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
270 {
271 s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h, y, dydt_out);
272
273 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
274 {
275 /* Restore original y vector */
276 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
277 return s;
278 }
279 }
280
281 /* Estimate error and update the state buffer. */
282
283 for (i = 0; i < dim; i++)
284 {
285 yerr[i] = 4.0 * (y[i] - y_onestep[i]);
286 yim1[i] = y0[i];
287 }
288
289 /* Make note of step size. */
290 state->last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h. = h;
291
292 return 0;
293 }
294 }
295
296 static int
297 gear2_resetcxx . . $ gear2_reset(c0a5f96701d3f89). (void *vstate, size_t dim)
298 {
299 gear2_state_tcxx . . $ gear2_state_t# *state = (gear2_state_tcxx . . $ gear2_state_t# *) vstate;
300
301 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->yim1, dim);
302 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->k, dim);
303 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y0, dim);
304
305 state->primedcxx . . $ $anonymous_type_803fe5c260ceba4c_0#primed. = 0;
306 state->last_hcxx . . $ $anonymous_type_803fe5c260ceba4c_0#last_h. = 0.0;
307 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
308 }
309
310 static unsigned int
311 gear2_ordercxx . . $ gear2_order(6769d0e399bf81e3). (void *vstate)
312 {
313 return 3;
314 }
315
316 static void
317 gear2_freecxx . . $ gear2_free(5d22bdacc48458e8). (void *vstate)
318 {
319 gear2_state_tcxx . . $ gear2_state_t# *state = (gear2_state_tcxx . . $ gear2_state_t# *) vstate;
320
321 freecxx . . $ free(5d22bdacc48458e8). (state->yim1cxx . . $ $anonymous_type_803fe5c260ceba4c_0#yim1.);
322 freecxx . . $ free(5d22bdacc48458e8). (state->kcxx . . $ $anonymous_type_803fe5c260ceba4c_0#k.);
323 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0.);
324 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y0_orig.);
325 freecxx . . $ free(5d22bdacc48458e8). (state->y_onestepcxx . . $ $anonymous_type_803fe5c260ceba4c_0#y_onestep.);
326 gsl_odeiv_step_freecxx . . $ gsl_odeiv_step_free(70b0036a14f7e2b6). (state->primercxx . . $ $anonymous_type_803fe5c260ceba4c_0#primer.);
327
328 freecxx . . $ free(5d22bdacc48458e8). (state);
329 }
330
331 static const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# gear2_typecxx . . $ gear2_type. = { "gear2", /* name */
332 1, /* can use dydt_in */
333 0, /* gives exact dydt_out */
334 &gear2_alloccxx . . $ gear2_alloc(bfcfa4d6b7f7ef64).,
335 &gear2_applycxx . . $ gear2_apply(46f97775c18f9353).,
336 &gear2_resetcxx . . $ gear2_reset(c0a5f96701d3f89).,
337 &gear2_ordercxx . . $ gear2_order(6769d0e399bf81e3).,
338 &gear2_freecxx . . $ gear2_free(5d22bdacc48458e8).
339 };
340
341 const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# *gsl_odeiv_step_gear2cxx . . $ gsl_odeiv_step_gear2. = &gear2_typecxx . . $ gear2_type.;