Debian Codegraph @

external/gsl/gsl_ode-initval__rk2imp.c

raw symbols SCIP
1 /* ode-initval/rk2imp.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 2, Gaussian implicit. Also known as the implicit
21 midpoint rule. */
22
23 /* Author: G. Jungman */
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 "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_b0cd5b9b6ee228bc_0#
41 {
42 double *Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.;
43 double *y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.;
44 double *ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.;
45 double *y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep.;
46 double *y0_origcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0_orig.;
47 }
48 rk2imp_state_tcxx . . $ rk2imp_state_t#;
49
50 static void *
51 rk2imp_alloccxx . . $ rk2imp_alloc(bfcfa4d6b7f7ef64). (size_t dim)
52 {
53 rk2imp_state_tcxx . . $ rk2imp_state_t# *state = (rk2imp_state_tcxx . . $ rk2imp_state_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (rk2imp_state_tcxx . . $ rk2imp_state_t#));
54
55 if (state == 0)
56 {
57 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for rk2imp_state",
58 GSL_ENOMEM);
59 }
60
61 state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
62
63 if (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1. == 0)
64 {
65 freecxx . . $ free(5d22bdacc48458e8). (state);
66 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for Y1", GSL_ENOMEM);
67 }
68
69 state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
70
71 if (state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp. == 0)
72 {
73 freecxx . . $ free(5d22bdacc48458e8). (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.);
74 freecxx . . $ free(5d22bdacc48458e8). (state);
75 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for ytmp", GSL_ENOMEM);
76 }
77
78 state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
79
80 if (state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0. == 0)
81 {
82 freecxx . . $ free(5d22bdacc48458e8). (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.);
83 freecxx . . $ free(5d22bdacc48458e8). (state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.);
84 freecxx . . $ free(5d22bdacc48458e8). (state);
85 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0", GSL_ENOMEM);
86 }
87
88 state->y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
89
90 if (state->y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep. == 0)
91 {
92 freecxx . . $ free(5d22bdacc48458e8). (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.);
93 freecxx . . $ free(5d22bdacc48458e8). (state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.);
94 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.);
95 freecxx . . $ free(5d22bdacc48458e8). (state);
96 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y_onestep", GSL_ENOMEM);
97 }
98
99 state->y0_origcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0_orig. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (dim * sizeof (double));
100
101 if (state->y0_origcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0_orig. == 0)
102 {
103 freecxx . . $ free(5d22bdacc48458e8). (state->y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep.);
104 freecxx . . $ free(5d22bdacc48458e8). (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.);
105 freecxx . . $ free(5d22bdacc48458e8). (state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.);
106 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.);
107 freecxx . . $ free(5d22bdacc48458e8). (state);
108 GSL_ERROR_NULLcxx . . $ `external/gsl/gsl_errno.h:129:9`! ("failed to allocate space for y0_orig", GSL_ENOMEM);
109 }
110
111 return state;
112 }
113
114 static int
115 rk2imp_stepcxx . . $ rk2imp_step(7fbaa4625337a84d). (double *y, rk2imp_state_tcxx . . $ rk2imp_state_t# *state,
116 const double h, const double t,
117 const size_t dim, const gsl_odeiv_systemcxx . . $ gsl_odeiv_system# *sys)
118 {
119 /* Makes a Runge-Kutta 2nd order implicit advance with step size h.
120 y0 is initial values of variables y.
121
122 The implicit matrix equations to solve are:
123
124 Y1 = y0 + h/2 * f(t + h/2, Y1)
125
126 y = y0 + h * f(t + h/2, Y1)
127 */
128
129 const double *y0 = state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.;
130 double *Y1 = state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.;
131 double *ytmp = state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.;
132 int max_iter=3;
133 int nu;
134 size_t i;
135
136 /* iterative solution of Y1 = y0 + h/2 * f(t + h/2, Y1)
137 Y1 should include initial values at call.
138
139 Note: This method does not check for convergence of the
140 iterative solution!
141 */
142
143 for (nu = 0; nu < max_iter; nu++)
144 {
145 for (i = 0; i < dim; i++)
146 {
147 ytmp[i] = y0[i] + 0.5 * h * Y1[i];
148 }
149
150 {
151 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + 0.5 * h, ytmp, Y1);
152
153 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
154 {
155 return s;
156 }
157 }
158 }
159
160 /* assignment */
161
162 for (i = 0; i < dim; i++)
163 {
164 y[i] = y0[i] + h * Y1[i];
165 }
166
167 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
168 }
169
170 static int
171 rk2imp_applycxx . . $ rk2imp_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 rk2imp_state_tcxx . . $ rk2imp_state_t# *state = (rk2imp_state_tcxx . . $ rk2imp_state_t# *) vstate;
181
182 size_t i;
183
184 double *Y1 = state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.;
185 double *y0 = state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.;
186 double *y_onestep = state->y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep.;
187 double *y0_orig = state->y0_origcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0_orig.;
188
189 /* Error estimation is done by step doubling procedure */
190
191 /* initialization step */
192
193 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
194
195 /* Save initial values for possible failures */
196 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0_orig, y, dim);
197
198 if (dydt_in != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
199 {
200 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (Y1, dydt_in, dim);
201 }
202
203 else
204 {
205 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t, y, Y1);
206
207 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
208 {
209 return s;
210 }
211 }
212
213 /* First traverse h with one step (save to y_onestep) */
214
215 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y_onestep, y, dim);
216
217 {
218 int s = rk2imp_stepcxx . . $ rk2imp_step(7fbaa4625337a84d). (y_onestep, state, h, t, dim, sys);
219
220 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
221 {
222 return s;
223 }
224 }
225
226 /* Then with two steps with half step length (save to y) */
227
228 {
229 int s = rk2imp_stepcxx . . $ rk2imp_step(7fbaa4625337a84d). (y, state, h / 2.0, t, dim, sys);
230
231 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
232 {
233 /* Restore original y vector */
234 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
235
236 return s;
237 }
238 }
239
240 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y0, y, dim);
241
242 {
243 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h / 2.0, y, Y1);
244
245 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
246 {
247 /* Restore original y vector */
248 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
249
250 return s;
251 }
252 }
253
254 {
255 int s = rk2imp_stepcxx . . $ rk2imp_step(7fbaa4625337a84d). (y, state, h / 2.0, t + h / 2.0, dim, sys);
256
257 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
258 {
259 /* Restore original y vector */
260 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
261
262 return s;
263 }
264 }
265
266 /* Derivatives at output */
267
268 if (dydt_out != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
269 {
270 int s = GSL_ODEIV_FN_EVALcxx . . $ `external/gsl/gsl_odeiv.h:67:9`! (sys, t + h, y, dydt_out);
271
272 if (s != GSL_SUCCESScxx . . $ GSL_SUCCESS.)
273 {
274 /* Restore original y vector */
275 DBL_MEMCPYcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:1:9`! (y, y0_orig, dim);
276
277 return s;
278 }
279 }
280
281 /* Error estimation */
282
283 for (i = 0; i < dim; i++)
284 {
285 yerr[i] = 4.0 * (y[i] - y_onestep[i]) / 3.0;
286 }
287
288 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
289 }
290
291 static int
292 rk2imp_resetcxx . . $ rk2imp_reset(c0a5f96701d3f89). (void *vstate, size_t dim)
293 {
294 rk2imp_state_tcxx . . $ rk2imp_state_t# *state = (rk2imp_state_tcxx . . $ rk2imp_state_t# *) vstate;
295
296 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->Y1, dim);
297 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->ytmp, dim);
298 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y0, dim);
299 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y_onestep, dim);
300 DBL_ZERO_MEMSETcxx . . $ `external/gsl/gsl_ode-initval__odeiv_util.h:2:9`! (state->y0_orig, dim);
301
302 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
303 }
304
305 static unsigned int
306 rk2imp_ordercxx . . $ rk2imp_order(6769d0e399bf81e3). (void *vstate)
307 {
308 return 2;
309 }
310
311 static void
312 rk2imp_freecxx . . $ rk2imp_free(5d22bdacc48458e8). (void *vstate)
313 {
314 rk2imp_state_tcxx . . $ rk2imp_state_t# *state = (rk2imp_state_tcxx . . $ rk2imp_state_t# *) vstate;
315
316 freecxx . . $ free(5d22bdacc48458e8). (state->Y1cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#Y1.);
317 freecxx . . $ free(5d22bdacc48458e8). (state->ytmpcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#ytmp.);
318 freecxx . . $ free(5d22bdacc48458e8). (state->y0cxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0.);
319 freecxx . . $ free(5d22bdacc48458e8). (state->y_onestepcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y_onestep.);
320 freecxx . . $ free(5d22bdacc48458e8). (state->y0_origcxx . . $ $anonymous_type_b0cd5b9b6ee228bc_0#y0_orig.);
321 freecxx . . $ free(5d22bdacc48458e8). (state);
322 }
323
324 static const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# rk2imp_typecxx . . $ rk2imp_type. = { "rk2imp", /* name */
325 1, /* can use dydt_in */
326 1, /* gives exact dydt_out */
327 &rk2imp_alloccxx . . $ rk2imp_alloc(bfcfa4d6b7f7ef64).,
328 &rk2imp_applycxx . . $ rk2imp_apply(46f97775c18f9353).,
329 &rk2imp_resetcxx . . $ rk2imp_reset(c0a5f96701d3f89).,
330 &rk2imp_ordercxx . . $ rk2imp_order(6769d0e399bf81e3).,
331 &rk2imp_freecxx . . $ rk2imp_free(5d22bdacc48458e8).
332 };
333
334 const gsl_odeiv_step_typecxx . . $ gsl_odeiv_step_type# *gsl_odeiv_step_rk2impcxx . . $ gsl_odeiv_step_rk2imp. = &rk2imp_typecxx . . $ rk2imp_type.;