Debian Codegraph @

external/gsl/gsl_ode-initval__rk4imp.c

raw symbols SCIP
1 /* ode-initval/rk4imp.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 /* Runge-Kutta 4, Gaussian implicit */
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 Method coefficients can also be found in it.
29 */
30
31 #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/
32 #include <stdlib.h>
33 #include <string.h>
34 #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/
35 #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/
36 #include "gsl_odeiv.h"cxx . . $ `<file>/external/gsl/gsl_odeiv.h`/
37
38 #include "gsl_ode-initval__odeiv_util.h"cxx . . $ `<file>/external/gsl/gsl_ode-initval__odeiv_util.h`/
39
40 typedef structcxx . . $ $anonymous_type_1761ce527bacc9cc_0#
41 {
42 double *k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.;
43 double *k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.;
44 double *ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.;
45 double *ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.;
46 double *y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0.;
47 double *y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig.;
48 double *y_onestepcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y_onestep.;
49 }
50 rk4imp_state_tcxx . . $ rk4imp_state_t#;
51
52 static void *
53 rk4imp_alloccxx . . $ rk4imp_alloc(bfcfa4d6b7f7ef64). (size_t dim)
54 {
55 rk4imp_state_tcxx . . $ rk4imp_state_t# *state = (rk4imp_state_tcxx . . $ rk4imp_state_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (rk4imp_state_tcxx . . $ rk4imp_state_t#));
56
57 if (state == 0)
58 {
59 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for rk4imp_state",
60 GSL_ENOMEM);
61 }
62
63 state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
64
65 if (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu. == 0)
66 {
67 freecxx . . $ free(5d22bdacc48458e8). (state);
68 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for k1nu", GSL_ENOMEM);
69 }
70
71 state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
72
73 if (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu. == 0)
74 {
75 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
76 freecxx . . $ free(5d22bdacc48458e8). (state);
77 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for k2nu", GSL_ENOMEM);
78 }
79
80 state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
81
82 if (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1. == 0)
83 {
84 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
85 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
86 freecxx . . $ free(5d22bdacc48458e8). (state);
87 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for ytmp1", GSL_ENOMEM);
88 }
89
90 state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
91
92 if (state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2. == 0)
93 {
94 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.);
95 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
96 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
97 freecxx . . $ free(5d22bdacc48458e8). (state);
98 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for ytmp2", GSL_ENOMEM);
99 }
100
101 state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
102
103 if (state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0. == 0)
104 {
105 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.);
106 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.);
107 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
108 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
109 freecxx . . $ free(5d22bdacc48458e8). (state);
110 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0", GSL_ENOMEM);
111 }
112
113 state->y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
114
115 if (state->y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig. == 0)
116 {
117 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0.);
118 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.);
119 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.);
120 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
121 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
122 freecxx . . $ free(5d22bdacc48458e8). (state);
123 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0_orig", GSL_ENOMEM);
124 }
125
126 state->y_onestepcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y_onestep. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
127
128 if (state->y_onestepcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y_onestep. == 0)
129 {
130 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig.);
131 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0.);
132 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.);
133 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.);
134 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
135 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
136 freecxx . . $ free(5d22bdacc48458e8). (state);
137 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y_onestep", GSL_ENOMEM);
138 }
139
140 return state;
141 }
142
143 static int
144 rk4imp_stepcxx . . $ rk4imp_step(aea9b861d98cb18c). (double *y, rk4imp_state_tcxx . . $ rk4imp_state_t# *state,
145 const double h, const double t,
146 const size_t dim, const gsl_odeiv_systemcxx . . $ gsl_odeiv_system# *sys)
147 {
148 /* Makes a Runge-Kutta 4th order implicit advance with step size h.
149 y0 is initial values of variables y.
150
151 The implicit matrix equations to solve are:
152
153 Y1 = y0 + h * a11 * f(t + h * c1, Y1) + h * a12 * f(t + h * c2, Y2)
154 Y2 = y0 + h * a21 * f(t + h * c1, Y1) + h * a22 * f(t + h * c2, Y2)
155
156 y = y0 + h * b1 * f(t + h * c1, Y1) + h * b2 * f(t + h * c2, Y2)
157
158 with constant coefficients a, b and c. For this method
159 they are: b=[0.5 0.5] c=[(3-sqrt(3))/6 (3+sqrt(3))/6]
160 a11=1/4, a12=(3-2*sqrt(3))/12, a21=(3+2*sqrt(3))/12 and a22=1/4
161 */
162
163 const double ir3 = 1.0 / M_SQRT3cxx . . $ `external/gsl/gsl_math.h:51:9`!;
164 const int iter_steps = 3;
165 int nu;
166 size_t i;
167
168 double *const k1nu = state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.;
169 double *const k2nu = state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.;
170 double *const ytmp1 = state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.;
171 double *const ytmp2 = state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.;
172
173 /* iterative solution of Y1 and Y2.
174
175 Note: This method does not check for convergence of the
176 iterative solution!
177 */
178
179 for (nu = 0; nu < iter_steps; nu++)
180 {
181 for (i = 0; i < dim; i++)
182 {
183 ytmp1[i] =
184 y[i] + h * (0.25 * k1nu[i] + 0.5 * (0.5 - ir3) * k2nu[i]);
185 ytmp2[i] =
186 y[i] + h * (0.25 * k2nu[i] + 0.5 * (0.5 + ir3) * k1nu[i]);
187 }
188 {
189 int s =
190 GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + 0.5 * h * (1.0 - ir3), ytmp1, k1nu);
191
192 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
193 {
194 return s;
195 }
196 }
197 {
198 int s =
199 GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + 0.5 * h * (1.0 + ir3), ytmp2, k2nu);
200
201 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
202 {
203 return s;
204 }
205 }
206 }
207
208 /* assignment */
209
210 for (i = 0; i < dim; i++)
211 {
212 const double d_i = 0.5 * (k1nu[i] + k2nu[i]);
213 y[i] += h * d_i;
214 }
215
216 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
217 }
218
219 static int
220 rk4imp_applycxx . . $ rk4imp_apply(46f97775c18f9353). (void *vstate,
221 size_t dim,
222 double t,
223 double h,
224 double y[],
225 double yerr[],
226 const double dydt_in[],
227 double dydt_out[],
228 const gsl_odeiv_systemcxx . . $ gsl_odeiv_system# * sys)
229 {
230 rk4imp_state_tcxx . . $ rk4imp_state_t# *state = (rk4imp_state_tcxx . . $ rk4imp_state_t# *) vstate;
231
232 size_t i;
233
234 double *y0 = state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0.;
235 double *y0_orig = state->y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig.;
236 double *y_onestep = state->y_onestepcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y_onestep.;
237 double *k1nu = state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.;
238 double *k2nu = state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.;
239
240 /* Initialization step */
241 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
242
243 /* Save initial values in case of failure */
244 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0_orig, y, dim);
245
246 if (dydt_in != 0)
247 {
248 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (k1nu, dydt_in, dim);
249 }
250 else
251 {
252 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t, y, k1nu);
253
254 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
255 {
256 return s;
257 }
258 }
259
260 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (k2nu, k1nu, dim);
261
262 /* First traverse h with one step (save to y_onestep) */
263
264 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y_onestep, y, dim);
265
266 {
267 int s = rk4imp_stepcxx . . $ rk4imp_step(aea9b861d98cb18c). (y_onestep, state, h, t, dim, sys);
268
269 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
270 {
271 return s;
272 }
273 }
274
275 /* Then with two steps with half step length (save to y) */
276
277 {
278 int s = rk4imp_stepcxx . . $ rk4imp_step(aea9b861d98cb18c). (y, state, h/2.0, t, dim, sys);
279
280 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
281 {
282 /* Restore original y vector */
283 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
284 return s;
285 }
286 }
287
288 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
289
290 {
291 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h/2.0, y, k1nu);
292
293 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
294 {
295 /* Restore original y vector */
296 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
297 return s;
298 }
299 }
300
301 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (k2nu, k1nu, dim);
302
303 {
304 int s = rk4imp_stepcxx . . $ rk4imp_step(aea9b861d98cb18c). (y, state, h/2.0, t + h/2.0, dim, sys);
305
306 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
307 {
308 /* Restore original y vector */
309 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
310 return s;
311 }
312 }
313
314 /* Derivatives at output */
315
316 if (dydt_out != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
317 {
318 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h, y, dydt_out);
319
320 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.) {
321 /* Restore original y vector */
322 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
323 return s;
324 }
325 }
326
327 /* Error estimation */
328
329 /* Denominator in step doubling error equation
330 * yerr = 0.5 * | y(onestep) - y(twosteps) | / (2^order - 1)
331 */
332
333 for (i = 0; i < dim; i++)
334 {
335 yerr[i] = 8.0 * 0.5 * (y[i] - y_onestep[i]) / 15.0;
336 }
337
338 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
339 }
340
341 static int
342 rk4imp_resetcxx . . $ rk4imp_reset(c0a5f96701d3f89). (void *vstate, size_t dim)
343 {
344 rk4imp_state_tcxx . . $ rk4imp_state_t# *state = (rk4imp_state_tcxx . . $ rk4imp_state_t# *) vstate;
345
346 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y_onestep, dim);
347 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y0_orig, dim);
348 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y0, dim);
349 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->k1nu, dim);
350 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->k2nu, dim);
351 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->ytmp1, dim);
352 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->ytmp2, dim);
353
354 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
355 }
356
357 static unsigned int
358 rk4imp_ordercxx . . $ rk4imp_order(6769d0e399bf81e3). (void *vstate)
359 {
360 return 4;
361 }
362
363 static void
364 rk4imp_freecxx . . $ rk4imp_free(5d22bdacc48458e8). (void *vstate)
365 {
366 rk4imp_state_tcxx . . $ rk4imp_state_t# *state = (rk4imp_state_tcxx . . $ rk4imp_state_t# *) vstate;
367
368 freecxx . . $ free(5d22bdacc48458e8). (state->y_onestepcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y_onestep.);
369 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0_orig.);
370 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_1761ce527bacc9cc_0#y0.);
371 freecxx . . $ free(5d22bdacc48458e8). (state->k1nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k1nu.);
372 freecxx . . $ free(5d22bdacc48458e8). (state->k2nucxx . . $ $anonymous_type_1761ce527bacc9cc_0#k2nu.);
373 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp1cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp1.);
374 freecxx . . $ free(5d22bdacc48458e8). (state->ytmp2cxx . . $ $anonymous_type_1761ce527bacc9cc_0#ytmp2.);
375 freecxx . . $ free(5d22bdacc48458e8). (state);
376 }
377
378 static const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# rk4imp_typecxx . . $ rk4imp_type. = { "rk4imp", /* name */
379 1, /* can use dydt_in? */
380 1, /* gives exact dydt_out? */
381 &rk4imp_alloccxx . . $ rk4imp_alloc(bfcfa4d6b7f7ef64).,
382 &rk4imp_applycxx . . $ rk4imp_apply(46f97775c18f9353).,
383 &rk4imp_resetcxx . . $ rk4imp_reset(c0a5f96701d3f89).,
384 &rk4imp_ordercxx . . $ rk4imp_order(6769d0e399bf81e3).,
385 &rk4imp_freecxx . . $ rk4imp_free(5d22bdacc48458e8).
386 };
387
388 const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# *gsl_odeiv_step_rk4impcxx . . $ gsl_odeiv_step_rk4imp. = &rk4imp_typecxx . . $ rk4imp_type.;