GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
LSODE.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <cinttypes>
31#include <sstream>
32
33#include "LSODE.h"
34#include "f77-fcn.h"
35#include "lo-error.h"
36#include "quit.h"
37
38typedef F77_INT (*lsode_fcn_ptr) (const F77_INT&, const double&, double *,
39 double *, F77_INT&);
40
41typedef F77_INT (*lsode_jac_ptr) (const F77_INT&, const double&, double *,
42 const F77_INT&, const F77_INT&, double *,
43 const F77_INT&);
44
45extern "C"
46{
49 F77_DBLE&, F77_INT&, F77_DBLE&, const F77_DBLE *,
52 F77_INT&);
53}
54
58
59static F77_INT
60lsode_f (const F77_INT& neq, const double& time, double *, double *deriv,
62{
63 ColumnVector tmp_deriv;
64
65 // NOTE: this won't work if LSODE passes copies of the state vector.
66 // In that case we have to create a temporary vector object
67 // and copy.
68
69 tmp_deriv = (*user_fun) (*tmp_x, time);
70
71 if (tmp_deriv.isempty ())
72 ierr = -1;
73 else
74 {
75 for (F77_INT i = 0; i < neq; i++)
76 deriv[i] = tmp_deriv.elem (i);
77 }
78
79 return 0;
80}
81
82static F77_INT
83lsode_j (const F77_INT& neq, const double& time, double *, const F77_INT&,
84 const F77_INT&, double *pd, const F77_INT& nrowpd)
85{
86 Matrix tmp_jac (neq, neq);
87
88 // NOTE: this won't work if LSODE passes copies of the state vector.
89 // In that case we have to create a temporary vector object
90 // and copy.
91
92 tmp_jac = (*user_jac) (*tmp_x, time);
93
94 for (F77_INT j = 0; j < neq; j++)
95 for (F77_INT i = 0; i < neq; i++)
96 pd[nrowpd * j + i] = tmp_jac (i, j);
97
98 return 0;
99}
100
103{
104 ColumnVector retval;
105
106 static F77_INT nn = 0;
107
109 || LSODE_options::m_reset)
110 {
111 m_integration_error = false;
112
113 m_initialized = true;
114
115 m_istate = 1;
116
117 F77_INT n = octave::to_f77_int (size ());
118
119 nn = n;
120
121 octave_idx_type max_maxord = 0;
122
123 if (integration_method () == "stiff")
124 {
125 max_maxord = 5;
126
127 if (m_jac)
128 m_method_flag = 21;
129 else
130 m_method_flag = 22;
131
132 m_liw = 20 + n;
133 m_lrw = 22 + n * (9 + n);
134 }
135 else
136 {
137 max_maxord = 12;
138
139 m_method_flag = 10;
140
141 m_liw = 20;
142 m_lrw = 22 + 16 * n;
143 }
144
146
147 for (F77_INT i = 4; i < 9; i++)
148 m_iwork(i) = 0;
149
151
152 for (F77_INT i = 4; i < 9; i++)
153 m_rwork(i) = 0;
154
155 octave_idx_type maxord = maximum_order ();
156
157 if (maxord >= 0)
158 {
159 if (maxord > 0 && maxord <= max_maxord)
160 {
161 m_iwork(4) = octave::to_f77_int (maxord);
162 m_iopt = 1;
163 }
164 else
165 {
166 // FIXME: Should this be a warning?
167 (*current_liboctave_error_handler)
168 ("lsode: invalid value for maximum order");
169 m_integration_error = true;
170 return retval;
171 }
172 }
173
174 if (m_stop_time_set)
175 {
176 m_itask = 4;
177 m_rwork(0) = m_stop_time;
178 m_iopt = 1;
179 }
180 else
181 {
182 m_itask = 1;
183 }
184
185 m_restart = false;
186
187 // ODEFunc
188
189 // NOTE: this won't work if LSODE passes copies of the state vector.
190 // In that case we have to create a temporary vector object
191 // and copy.
192
193 tmp_x = &m_x;
194
195 user_fun = function ();
197
198 ColumnVector m_xdot = (*user_fun) (m_x, m_t);
199
200 if (m_x.numel () != m_xdot.numel ())
201 {
202 // FIXME: Should this be a warning?
203 (*current_liboctave_error_handler)
204 ("lsode: inconsistent sizes for state and derivative vectors");
205
206 m_integration_error = true;
207 return retval;
208 }
209
210 ODEFunc::m_reset = false;
211
212 // LSODE_options
213
214 m_rel_tol = relative_tolerance ();
215 m_abs_tol = absolute_tolerance ();
216
217 F77_INT abs_tol_len = octave::to_f77_int (m_abs_tol.numel ());
218
219 if (abs_tol_len == 1)
220 m_itol = 1;
221 else if (abs_tol_len == n)
222 m_itol = 2;
223 else
224 {
225 // FIXME: Should this be a warning?
226 (*current_liboctave_error_handler)
227 ("lsode: inconsistent sizes for state and absolute tolerance vectors");
228
229 m_integration_error = true;
230 return retval;
231 }
232
233 double iss = initial_step_size ();
234 if (iss >= 0.0)
235 {
236 m_rwork(4) = iss;
237 m_iopt = 1;
238 }
239
240 double maxss = maximum_step_size ();
241 if (maxss >= 0.0)
242 {
243 m_rwork(5) = maxss;
244 m_iopt = 1;
245 }
246
247 double minss = minimum_step_size ();
248 if (minss >= 0.0)
249 {
250 m_rwork(6) = minss;
251 m_iopt = 1;
252 }
253
254 F77_INT sl = octave::to_f77_int (step_limit ());
255 if (sl > 0)
256 {
257 m_iwork(5) = sl;
258 m_iopt = 1;
259 }
260
261 LSODE_options::m_reset = false;
262 }
263
264 double *px = m_x.fortran_vec ();
265
266 double *pabs_tol = m_abs_tol.fortran_vec ();
267
268 F77_INT *piwork = m_iwork.fortran_vec ();
269 double *prwork = m_rwork.fortran_vec ();
270
271 F77_INT tmp_istate = octave::to_f77_int (m_istate);
272
273 F77_XFCN (dlsode, DLSODE, (lsode_f, nn, px, m_t, tout, m_itol, m_rel_tol,
274 pabs_tol, m_itask, tmp_istate, m_iopt, prwork,
275 m_lrw, piwork, m_liw, lsode_j, m_method_flag));
276
277 m_istate = tmp_istate;
278
279 switch (m_istate)
280 {
281 case 1: // prior to initial integration step.
282 case 2: // lsode was successful.
283 retval = m_x;
284 m_t = tout;
285 break;
286
287 case -1: // excess work done on this call (perhaps wrong mf).
288 case -2: // excess accuracy requested (tolerances too small).
289 case -3: // invalid input detected (see printed message).
290 case -4: // repeated error test failures (check all inputs).
291 case -5: // repeated convergence failures (perhaps bad Jacobian
292 // supplied or wrong choice of mf or tolerances).
293 case -6: // error weight became zero during problem. (solution
294 // component i vanished, and atol or atol(i) = 0.)
295 case -13: // return requested in user-supplied function.
296 m_integration_error = true;
297 break;
298
299 default:
300 m_integration_error = true;
301 (*current_liboctave_error_handler)
302 ("unrecognized value of istate (= %" OCTAVE_IDX_TYPE_FORMAT ") "
303 "returned from lsode", m_istate);
304 break;
305 }
306
307 return retval;
308}
309
310std::string
312{
313 std::string retval;
314
315 std::ostringstream buf;
316 buf << m_t;
317 std::string t_curr = buf.str ();
318
319 switch (m_istate)
320 {
321 case 1:
322 retval = "prior to initial integration step";
323 break;
324
325 case 2:
326 retval = "successful exit";
327 break;
328
329 case 3:
330 retval = "prior to continuation call with modified parameters";
331 break;
332
333 case -1:
334 retval = "excess work on this call (t = " + t_curr +
335 "; perhaps wrong integration method)";
336 break;
337
338 case -2:
339 retval = "excess accuracy requested (tolerances too small)";
340 break;
341
342 case -3:
343 retval = "invalid input detected (see printed message)";
344 break;
345
346 case -4:
347 retval = "repeated error test failures (t = " + t_curr +
348 "; check all inputs)";
349 break;
350
351 case -5:
352 retval = "repeated convergence failures (t = " + t_curr +
353 "; perhaps bad Jacobian supplied or wrong choice of integration method or tolerances)";
354 break;
355
356 case -6:
357 retval = "error weight became zero during problem. (t = " + t_curr +
358 "; solution component i vanished, and atol or atol(i) == 0)";
359 break;
360
361 case -13:
362 retval = "return requested in user-supplied function (t = "
363 + t_curr + ')';
364 break;
365
366 default:
367 retval = "unknown error state";
368 break;
369 }
370
371 return retval;
372}
373
374Matrix
376{
377 Matrix retval;
378
379 octave_idx_type n_out = tout.numel ();
380 F77_INT n = octave::to_f77_int (size ());
381
382 if (n_out > 0 && n > 0)
383 {
384 retval.resize (n_out, n);
385
386 for (F77_INT i = 0; i < n; i++)
387 retval.elem (0, i) = m_x.elem (i);
388
389 for (octave_idx_type j = 1; j < n_out; j++)
390 {
391 ColumnVector x_next = do_integrate (tout.elem (j));
392
394 return retval;
395
396 for (F77_INT i = 0; i < n; i++)
397 retval.elem (j, i) = x_next.elem (i);
398 }
399 }
400
401 return retval;
402}
403
404Matrix
406{
407 Matrix retval;
408
409 octave_idx_type n_out = tout.numel ();
410 F77_INT n = octave::to_f77_int (size ());
411
412 if (n_out > 0 && n > 0)
413 {
414 retval.resize (n_out, n);
415
416 for (F77_INT i = 0; i < n; i++)
417 retval.elem (0, i) = m_x.elem (i);
418
419 octave_idx_type n_crit = tcrit.numel ();
420
421 if (n_crit > 0)
422 {
423 octave_idx_type i_crit = 0;
424 octave_idx_type i_out = 1;
425 double next_crit = tcrit.elem (0);
426 double next_out;
427 while (i_out < n_out)
428 {
429 bool do_restart = false;
430
431 next_out = tout.elem (i_out);
432 if (i_crit < n_crit)
433 next_crit = tcrit.elem (i_crit);
434
435 bool save_output = false;
436 double t_out;
437
438 if (next_crit == next_out)
439 {
440 set_stop_time (next_crit);
441 t_out = next_out;
442 save_output = true;
443 i_out++;
444 i_crit++;
445 do_restart = true;
446 }
447 else if (next_crit < next_out)
448 {
449 if (i_crit < n_crit)
450 {
451 set_stop_time (next_crit);
452 t_out = next_crit;
453 save_output = false;
454 i_crit++;
455 do_restart = true;
456 }
457 else
458 {
460 t_out = next_out;
461 save_output = true;
462 i_out++;
463 }
464 }
465 else
466 {
467 set_stop_time (next_crit);
468 t_out = next_out;
469 save_output = true;
470 i_out++;
471 }
472
473 ColumnVector x_next = do_integrate (t_out);
474
476 return retval;
477
478 if (save_output)
479 {
480 for (F77_INT i = 0; i < n; i++)
481 retval.elem (i_out-1, i) = x_next.elem (i);
482 }
483
484 if (do_restart)
485 force_restart ();
486 }
487 }
488 else
489 {
490 retval = do_integrate (tout);
491
493 return retval;
494 }
495 }
496
497 return retval;
498}
static F77_INT nn
Definition: DASPK.cc:66
F77_INT(* lsode_jac_ptr)(const F77_INT &, const double &, double *, const F77_INT &, const F77_INT &, double *, const F77_INT &)
Definition: LSODE.cc:41
static F77_INT lsode_f(const F77_INT &neq, const double &time, double *, double *deriv, F77_INT &ierr)
Definition: LSODE.cc:60
F77_INT(* lsode_fcn_ptr)(const F77_INT &, const double &, double *, double *, F77_INT &)
Definition: LSODE.cc:38
static ODEFunc::ODEJacFunc user_jac
Definition: LSODE.cc:56
F77_RET_T F77_FUNC(dlsode, DLSODE)(lsode_fcn_ptr
F77_RET_T F77_INT F77_DBLE F77_DBLE F77_DBLE F77_INT F77_DBLE const F77_DBLE F77_INT F77_INT F77_INT F77_DBLE F77_INT F77_INT F77_INT F77_INT &static ODEFunc::ODERHSFunc user_fun
Definition: LSODE.cc:55
static ColumnVector * tmp_x
Definition: LSODE.cc:57
static F77_INT lsode_j(const F77_INT &neq, const double &time, double *, const F77_INT &, const F77_INT &, double *pd, const F77_INT &nrowpd)
Definition: LSODE.cc:83
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:534
bool isempty(void) const
Size of the specified dimension.
Definition: Array.h:607
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1010
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array.cc:1744
Array< double > m_rwork
Definition: LSODE.h:75
Array< double > m_abs_tol
Definition: LSODE.h:79
octave_f77_int_type m_lrw
Definition: LSODE.h:72
octave_f77_int_type m_liw
Definition: LSODE.h:71
double m_rel_tol
Definition: LSODE.h:77
std::string error_message(void) const
Definition: LSODE.cc:311
ColumnVector do_integrate(double t)
Definition: LSODE.cc:102
octave_f77_int_type m_itol
Definition: LSODE.h:69
octave_f77_int_type m_method_flag
Definition: LSODE.h:66
bool m_initialized
Definition: LSODE.h:64
octave_f77_int_type m_iopt
Definition: LSODE.h:68
octave_f77_int_type m_itask
Definition: LSODE.h:67
Array< octave_f77_int_type > m_iwork
Definition: LSODE.h:74
Definition: dMatrix.h:42
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:158
ODEJacFunc m_jac
Definition: ODEFunc.h:87
ODERHSFunc function(void) const
Definition: ODEFunc.h:66
ColumnVector(* ODERHSFunc)(const ColumnVector &, double)
Definition: ODEFunc.h:38
Matrix(* ODEJacFunc)(const ColumnVector &, double)
Definition: ODEFunc.h:39
bool m_reset
Definition: ODEFunc.h:94
ODEJacFunc jacobian_function(void) const
Definition: ODEFunc.h:75
virtual void force_restart(void)
Definition: base-de.h:98
double m_stop_time
Definition: base-de.h:112
bool m_restart
Definition: base-de.h:116
double m_t
Definition: base-de.h:110
octave_idx_type size(void) const
Definition: base-de.h:79
octave_idx_type m_istate
Definition: base-de.h:120
bool m_stop_time_set
Definition: base-de.h:114
bool m_integration_error
Definition: base-de.h:118
ColumnVector m_x
Definition: base-de.h:108
void set_stop_time(double tt)
Definition: base-de.h:85
void clear_stop_time(void)
Definition: base-de.h:92
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
subroutine dlsode(F, NEQ, Y, T, TOUT, ITOL, RTOL, ATOL, ITASK, ISTATE, IOPT, RWORK, LRW, IWORK, LIW, JAC, MF)
Definition: dlsode.f:3
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:45
double F77_DBLE
Definition: f77-fcn.h:302
octave_f77_int_type F77_INT
Definition: f77-fcn.h:306
F77_RET_T(F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, const F77_INT &, const F77_INT &, const F77_INT &, F77_INT &, F77_INT &, F77_DBLE *, const F77_INT &, F77_DBLE *, const F77_INT &, F77_DBLE *, F77_DBLE *, F77_DBLE *, const F77_INT &, F77_DBLE *, const F77_INT &, F77_DBLE *, const F77_INT &, F77_DBLE *, F77_INT *, F77_INT &F77_CHAR_ARG_LEN_DECL F77_CHAR_ARG_LEN_DECL F77_CHAR_ARG_LEN_DECL)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT F77_INT & ierr