GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
DASRT.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2002-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <sstream>
28 
29 #include "DASRT.h"
30 #include "f77-fcn.h"
31 #include "lo-error.h"
32 #include "quit.h"
33 
34 typedef F77_INT (*dasrt_fcn_ptr) (const double&, const double*, const double*,
35  double*, F77_INT&, double*, F77_INT*);
36 
37 typedef F77_INT (*dasrt_jac_ptr) (const double&, const double*, const double*,
38  double*, const double&, double*, F77_INT*);
39 
40 typedef F77_INT (*dasrt_constr_ptr) (const F77_INT&, const double&,
41  const double*, const F77_INT&,
42  double*, double*, F77_INT*);
43 
44 extern "C"
45 {
46  F77_RET_T
48  F77_DBLE*, F77_DBLE*, const F77_DBLE&, F77_INT*,
49  const F77_DBLE*, const F77_DBLE*, F77_INT&,
50  F77_DBLE*, const F77_INT&, F77_INT*,
51  const F77_INT&, F77_DBLE*, F77_INT*,
53  F77_INT*);
54 }
55 
59 
60 static F77_INT nn;
61 
62 static F77_INT
63 ddasrt_f (const double& t, const double *state, const double *deriv,
64  double *delta, F77_INT& ires, double *, F77_INT *)
65 {
66  BEGIN_INTERRUPT_WITH_EXCEPTIONS;
67 
68  ColumnVector tmp_state (nn);
69  ColumnVector tmp_deriv (nn);
70 
71  for (F77_INT i = 0; i < nn; i++)
72  {
73  tmp_state(i) = state[i];
74  tmp_deriv(i) = deriv[i];
75  }
76 
77  octave_idx_type tmp_ires = ires;
78 
79  ColumnVector tmp_fval = (*user_fsub) (tmp_state, tmp_deriv, t, tmp_ires);
80 
81  ires = octave::to_f77_int (tmp_ires);
82 
83  if (tmp_fval.isempty ())
84  ires = -2;
85  else
86  {
87  for (F77_INT i = 0; i < nn; i++)
88  delta[i] = tmp_fval(i);
89  }
90 
91  END_INTERRUPT_WITH_EXCEPTIONS;
92 
93  return 0;
94 }
95 
96 F77_INT
97 ddasrt_j (const double& time, const double *state, const double *deriv,
98  double *pd, const double& cj, double *, F77_INT *)
99 {
100  BEGIN_INTERRUPT_WITH_EXCEPTIONS;
101 
102  // FIXME: would be nice to avoid copying the data.
103 
104  ColumnVector tmp_state (nn);
105  ColumnVector tmp_deriv (nn);
106 
107  for (F77_INT i = 0; i < nn; i++)
108  {
109  tmp_deriv.elem (i) = deriv[i];
110  tmp_state.elem (i) = state[i];
111  }
112 
113  Matrix tmp_pd = (*user_jsub) (tmp_state, tmp_deriv, time, cj);
114 
115  for (F77_INT j = 0; j < nn; j++)
116  for (F77_INT i = 0; i < nn; i++)
117  pd[nn * j + i] = tmp_pd.elem (i, j);
118 
119  END_INTERRUPT_WITH_EXCEPTIONS;
120 
121  return 0;
122 }
123 
124 static F77_INT
125 ddasrt_g (const F77_INT& neq, const double& t, const double *state,
126  const F77_INT& ng, double *gout, double *, F77_INT *)
127 {
128  BEGIN_INTERRUPT_WITH_EXCEPTIONS;
129 
130  F77_INT n = neq;
131 
132  ColumnVector tmp_state (n);
133  for (F77_INT i = 0; i < n; i++)
134  tmp_state(i) = state[i];
135 
136  ColumnVector tmp_fval = (*user_csub) (tmp_state, t);
137 
138  for (F77_INT i = 0; i < ng; i++)
139  gout[i] = tmp_fval(i);
140 
141  END_INTERRUPT_WITH_EXCEPTIONS;
142 
143  return 0;
144 }
145 
146 void
147 DASRT::integrate (double tout)
148 {
149  // I suppose this is the safe thing to do. If this is the first
150  // call, or if anything about the problem has changed, we should
151  // start completely fresh.
152 
153  if (! initialized || restart
154  || DAEFunc::reset || DAERTFunc::reset || DASRT_options::reset)
155  {
156  integration_error = false;
157 
158  initialized = true;
159 
160  info.resize (dim_vector (15, 1));
161 
162  for (F77_INT i = 0; i < 15; i++)
163  info(i) = 0;
164 
165  F77_INT n = octave::to_f77_int (size ());
166 
167  nn = n;
168 
169  // DAERTFunc
170 
172 
173  if (user_csub)
174  {
175  ColumnVector tmp = (*user_csub) (x, t);
176  ng = octave::to_f77_int (tmp.numel ());
177  }
178  else
179  ng = 0;
180 
181  F77_INT maxord = octave::to_f77_int (maximum_order ());
182  if (maxord >= 0)
183  {
184  if (maxord > 0 && maxord < 6)
185  {
186  info(8) = 1;
187  iwork(2) = maxord;
188  }
189  else
190  {
191  (*current_liboctave_error_handler)
192  ("dassl: invalid value for maximum order");
193  integration_error = true;
194  return;
195  }
196  }
197 
198  liw = 21 + n;
199  lrw = 50 + 9*n + n*n + 3*ng;
200 
201  iwork.resize (dim_vector (liw, 1));
202  rwork.resize (dim_vector (lrw, 1));
203 
204  info(0) = 0;
205 
206  if (stop_time_set)
207  {
208  info(3) = 1;
209  rwork(0) = stop_time;
210  }
211  else
212  info(3) = 0;
213 
214  restart = false;
215 
216  // DAEFunc
217 
220 
221  if (user_fsub)
222  {
223  octave_idx_type ires = 0;
224 
225  ColumnVector fval = (*user_fsub) (x, xdot, t, ires);
226 
227  if (fval.numel () != x.numel ())
228  {
229  (*current_liboctave_error_handler)
230  ("dasrt: inconsistent sizes for state and residual vectors");
231 
232  integration_error = true;
233  return;
234  }
235  }
236  else
237  {
238  (*current_liboctave_error_handler)
239  ("dasrt: no user supplied RHS subroutine!");
240 
241  integration_error = true;
242  return;
243  }
244 
245  info(4) = (user_jsub ? 1 : 0);
246 
247  DAEFunc::reset = false;
248 
249  jroot.resize (dim_vector (ng, 1), 1);
250 
251  DAERTFunc::reset = false;
252 
253  // DASRT_options
254 
255  double mss = maximum_step_size ();
256  if (mss >= 0.0)
257  {
258  rwork(1) = mss;
259  info(6) = 1;
260  }
261  else
262  info(6) = 0;
263 
264  double iss = initial_step_size ();
265  if (iss >= 0.0)
266  {
267  rwork(2) = iss;
268  info(7) = 1;
269  }
270  else
271  info(7) = 0;
272 
273  F77_INT sl = octave::to_f77_int (step_limit ());
274  if (sl >= 0)
275  {
276  info(11) = 1;
277  iwork(20) = sl;
278  }
279  else
280  info(11) = 0;
281 
282  abs_tol = absolute_tolerance ();
283  rel_tol = relative_tolerance ();
284 
285  F77_INT abs_tol_len = octave::to_f77_int (abs_tol.numel ());
286  F77_INT rel_tol_len = octave::to_f77_int (rel_tol.numel ());
287 
288  if (abs_tol_len == 1 && rel_tol_len == 1)
289  {
290  info.elem (1) = 0;
291  }
292  else if (abs_tol_len == n && rel_tol_len == n)
293  {
294  info.elem (1) = 1;
295  }
296  else
297  {
298  (*current_liboctave_error_handler)
299  ("dasrt: inconsistent sizes for tolerance arrays");
300 
301  integration_error = true;
302  return;
303  }
304 
305  DASRT_options::reset = false;
306  }
307 
308  double *px = x.fortran_vec ();
309  double *pxdot = xdot.fortran_vec ();
310 
311  F77_INT *pinfo = info.fortran_vec ();
312 
313  double *prel_tol = rel_tol.fortran_vec ();
314  double *pabs_tol = abs_tol.fortran_vec ();
315 
316  double *prwork = rwork.fortran_vec ();
317  F77_INT *piwork = iwork.fortran_vec ();
318 
319  F77_INT *pjroot = jroot.fortran_vec ();
320 
321  double *dummy = nullptr;
322  F77_INT *idummy = nullptr;
323 
324  F77_INT tmp_istate = octave::to_f77_int (istate);
325 
326  F77_XFCN (ddasrt, DDASRT, (ddasrt_f, nn, t, px, pxdot, tout, pinfo,
327  prel_tol, pabs_tol, tmp_istate, prwork, lrw,
328  piwork, liw, dummy, idummy, ddasrt_j,
329  ddasrt_g, ng, pjroot));
330 
331  istate = tmp_istate;
332 
333  switch (istate)
334  {
335  case 1: // A step was successfully taken in intermediate-output
336  // mode. The code has not yet reached TOUT.
337  case 2: // The integration to TOUT was successfully completed
338  // (T=TOUT) by stepping exactly to TOUT.
339  case 3: // The integration to TOUT was successfully completed
340  // (T=TOUT) by stepping past TOUT. Y(*) is obtained by
341  // interpolation. YPRIME(*) is obtained by interpolation.
342  t = tout;
343  break;
344 
345  case 4: // The integration was successfully completed
346  // by finding one or more roots of G at T.
347  break;
348 
349  case -1: // A large amount of work has been expended.
350  case -2: // The error tolerances are too stringent.
351  case -3: // The local error test cannot be satisfied because you
352  // specified a zero component in ATOL and the
353  // corresponding computed solution component is zero.
354  // Thus, a pure relative error test is impossible for
355  // this component.
356  case -6: // DDASRT had repeated error test failures on the last
357  // attempted step.
358  case -7: // The corrector could not converge.
359  case -8: // The matrix of partial derivatives is singular.
360  case -9: // The corrector could not converge. There were repeated
361  // error test failures in this step.
362  case -10: // The corrector could not converge because IRES was
363  // equal to minus one.
364  case -11: // IRES equal to -2 was encountered and control is being
365  // returned to the calling program.
366  case -12: // DASSL failed to compute the initial YPRIME.
367  case -33: // The code has encountered trouble from which it cannot
368  // recover. A message is printed explaining the trouble
369  // and control is returned to the calling program. For
370  // example, this occurs when invalid input is detected.
371  integration_error = true;
372  break;
373 
374  default:
375  integration_error = true;
376  (*current_liboctave_error_handler)
377  ("unrecognized value of istate (= %d) returned from ddasrt",
378  istate);
379  break;
380  }
381 }
382 
385 {
387 
388  Matrix x_out;
389  Matrix xdot_out;
390  ColumnVector t_out = tout;
391 
392  octave_idx_type n_out = tout.numel ();
393  F77_INT n = octave::to_f77_int (size ());
394 
395  if (n_out > 0 && n > 0)
396  {
397  x_out.resize (n_out, n);
398  xdot_out.resize (n_out, n);
399 
400  for (F77_INT i = 0; i < n; i++)
401  {
402  x_out(0,i) = x(i);
403  xdot_out(0,i) = xdot(i);
404  }
405 
406  for (octave_idx_type j = 1; j < n_out; j++)
407  {
408  integrate (tout(j));
409 
410  if (integration_error)
411  {
412  retval = DASRT_result (x_out, xdot_out, t_out);
413  return retval;
414  }
415 
416  if (istate == 4)
417  t_out(j) = t;
418  else
419  t_out(j) = tout(j);
420 
421  for (F77_INT i = 0; i < n; i++)
422  {
423  x_out(j,i) = x(i);
424  xdot_out(j,i) = xdot(i);
425  }
426 
427  if (istate == 4)
428  {
429  x_out.resize (j+1, n);
430  xdot_out.resize (j+1, n);
431  t_out.resize (j+1);
432  break;
433  }
434  }
435  }
436 
437  retval = DASRT_result (x_out, xdot_out, t_out);
438 
439  return retval;
440 }
441 
443 DASRT::integrate (const ColumnVector& tout, const ColumnVector& tcrit)
444 {
446 
447  Matrix x_out;
448  Matrix xdot_out;
449  ColumnVector t_outs = tout;
450 
451  octave_idx_type n_out = tout.numel ();
452  F77_INT n = octave::to_f77_int (size ());
453 
454  if (n_out > 0 && n > 0)
455  {
456  x_out.resize (n_out, n);
457  xdot_out.resize (n_out, n);
458 
459  octave_idx_type n_crit = tcrit.numel ();
460 
461  if (n_crit > 0)
462  {
463  octave_idx_type i_crit = 0;
464  octave_idx_type i_out = 1;
465  double next_crit = tcrit(0);
466  double next_out;
467  while (i_out < n_out)
468  {
469  bool do_restart = false;
470 
471  next_out = tout(i_out);
472  if (i_crit < n_crit)
473  next_crit = tcrit(i_crit);
474 
475  bool save_output = false;
476  double t_out;
477 
478  if (next_crit == next_out)
479  {
480  set_stop_time (next_crit);
481  t_out = next_out;
482  save_output = true;
483  i_out++;
484  i_crit++;
485  do_restart = true;
486  }
487  else if (next_crit < next_out)
488  {
489  if (i_crit < n_crit)
490  {
491  set_stop_time (next_crit);
492  t_out = next_crit;
493  save_output = false;
494  i_crit++;
495  do_restart = true;
496  }
497  else
498  {
499  clear_stop_time ();
500  t_out = next_out;
501  save_output = true;
502  i_out++;
503  }
504  }
505  else
506  {
507  set_stop_time (next_crit);
508  t_out = next_out;
509  save_output = true;
510  i_out++;
511  }
512 
513  integrate (t_out);
514 
515  if (integration_error)
516  {
517  retval = DASRT_result (x_out, xdot_out, t_outs);
518  return retval;
519  }
520 
521  if (istate == 4)
522  t_out = t;
523 
524  if (save_output)
525  {
526  for (F77_INT i = 0; i < n; i++)
527  {
528  x_out(i_out-1,i) = x(i);
529  xdot_out(i_out-1,i) = xdot(i);
530  }
531 
532  t_outs(i_out-1) = t_out;
533 
534  if (istate == 4)
535  {
536  x_out.resize (i_out, n);
537  xdot_out.resize (i_out, n);
538  t_outs.resize (i_out);
539  i_out = n_out;
540  }
541  }
542 
543  if (do_restart)
544  force_restart ();
545  }
546 
547  retval = DASRT_result (x_out, xdot_out, t_outs);
548  }
549  else
550  {
551  retval = integrate (tout);
552 
553  if (integration_error)
554  return retval;
555  }
556  }
557 
558  return retval;
559 }
560 
563 {
565 
566  std::ostringstream buf;
567  buf << t;
568  std::string t_curr = buf.str ();
569 
570  switch (istate)
571  {
572  case 1:
573  retval = "a step was successfully taken in intermediate-output mode.";
574  break;
575 
576  case 2:
577  retval = "integration completed by stepping exactly to TOUT";
578  break;
579 
580  case 3:
581  retval = "integration to tout completed by stepping past TOUT";
582  break;
583 
584  case 4:
585  retval = "integration completed by finding one or more roots of G at T";
586  break;
587 
588  case -1:
589  retval = "a large amount of work has been expended (t =" + t_curr + ')';
590  break;
591 
592  case -2:
593  retval = "the error tolerances are too stringent";
594  break;
595 
596  case -3:
597  retval = "error weight became zero during problem. (t = " + t_curr +
598  "; solution component i vanished, and atol or atol(i) == 0)";
599  break;
600 
601  case -6:
602  retval = "repeated error test failures on the last attempted step (t = "
603  + t_curr + ')';
604  break;
605 
606  case -7:
607  retval = "the corrector could not converge (t = " + t_curr + ')';
608  break;
609 
610  case -8:
611  retval = "the matrix of partial derivatives is singular (t = " + t_curr +
612  ')';
613  break;
614 
615  case -9:
616  retval = "the corrector could not converge (t = " + t_curr +
617  "; repeated test failures)";
618  break;
619 
620  case -10:
621  retval = "corrector could not converge because IRES was -1 (t = "
622  + t_curr + ')';
623  break;
624 
625  case -11:
626  retval = "return requested in user-supplied function (t = " + t_curr +
627  ')';
628  break;
629 
630  case -12:
631  retval = "failed to compute consistent initial conditions";
632  break;
633 
634  case -33:
635  retval = "unrecoverable error (see printed message)";
636  break;
637 
638  default:
639  retval = "unknown error state";
640  break;
641  }
642 
643  return retval;
644 }
std::string error_message(void) const
Definition: DASRT.cc:562
ColumnVector xdot
Definition: base-dae.h:77
F77_RET_T F77_FUNC(ddasrt, DDASRT)(dasrt_fcn_ptr
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:148
bool initialized
Definition: DASRT.h:108
bool isempty(void) const
Definition: Array.h:565
Array< octave_f77_int_type > iwork
Definition: DASRT.h:116
static F77_INT nn
Definition: DASRT.cc:60
F77_RET_T const F77_INT F77_DBLE F77_DBLE F77_DBLE const F77_DBLE F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE const F77_INT F77_INT const F77_INT F77_DBLE F77_INT const F77_INT F77_INT *static DAEFunc::DAERHSFunc user_fsub
Definition: DASRT.cc:47
const T * fortran_vec(void) const
Definition: Array.h:584
static DAERTFunc::DAERTConstrFunc user_csub
Definition: DASRT.cc:58
bool integration_error
Definition: base-de.h:115
Array< double > rel_tol
Definition: DASRT.h:122
Array< octave_f77_int_type > jroot
Definition: DASRT.h:117
static F77_INT ddasrt_g(const F77_INT &neq, const double &t, const double *state, const F77_INT &ng, double *gout, double *, F77_INT *)
Definition: DASRT.cc:125
octave_idx_type istate
Definition: base-de.h:117
T & elem(octave_idx_type n)
Definition: Array.h:488
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
Array< octave_f77_int_type > info
Definition: DASRT.h:115
DAERTConstrFunc constraint_function(void) const
Definition: DAERTFunc.h:68
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
subroutine DDASRT(RES, NEQ, T, Y, YPRIME, TOUT, INFO, RTOL, ATOL, IDID, RWORK, LRW, IWORK, LIW, RPAR, IPAR, JAC, G, NG, JROOT)
Definition: ddasrt.f:4
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)
octave_f77_int_type lrw
Definition: DASRT.h:111
F77_INT ddasrt_j(const double &time, const double *state, const double *deriv, double *pd, const double &cj, double *, F77_INT *)
Definition: DASRT.cc:97
bool reset
Definition: DAEFunc.h:101
octave_idx_type size(void) const
Definition: base-de.h:76
Matrix(* DAEJacFunc)(const ColumnVector &x, const ColumnVector &xdot, double t, double cj)
Definition: DAEFunc.h:44
virtual void force_restart(void)
Definition: base-de.h:95
void set_stop_time(double tt)
Definition: base-de.h:82
double t
Definition: base-de.h:107
DAEJacFunc jacobian_function(void) const
Definition: DAEFunc.h:82
void resize(const dim_vector &dv, const T &rfv)
Resizing (with fill).
Definition: Array.cc:1010
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
ColumnVector(* DAERTConstrFunc)(const ColumnVector &x, double t)
Definition: DAERTFunc.h:35
Definition: dMatrix.h:36
DASRT_result integrate(const ColumnVector &tout)
Definition: DASRT.cc:384
static F77_INT ddasrt_f(const double &t, const double *state, const double *deriv, double *delta, F77_INT &ires, double *, F77_INT *)
Definition: DASRT.cc:63
void clear_stop_time(void)
Definition: base-de.h:89
static uint32_t state[624]
Definition: randmtzig.cc:183
double stop_time
Definition: base-de.h:109
Array< double > rwork
Definition: DASRT.h:119
ColumnVector x
Definition: base-de.h:105
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
F77_INT(* dasrt_constr_ptr)(const F77_INT &, const double &, const double *, const F77_INT &, double *, double *, F77_INT *)
Definition: DASRT.cc:40
DAERHSFunc function(void) const
Definition: DAEFunc.h:73
bool reset
Definition: DAERTFunc.h:86
ColumnVector(* DAERHSFunc)(const ColumnVector &x, const ColumnVector &xdot, double t, octave_idx_type &ires)
Definition: DAEFunc.h:36
for i
Definition: data.cc:5264
Array< double > abs_tol
Definition: DASRT.h:121
F77_INT(* dasrt_fcn_ptr)(const double &, const double *, const double *, double *, F77_INT &, double *, F77_INT *)
Definition: DASRT.cc:34
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
bool restart
Definition: base-de.h:113
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
static DAEFunc::DAEJacFunc user_jsub
Definition: DASRT.cc:57
bool stop_time_set
Definition: base-de.h:111
F77_INT(* dasrt_jac_ptr)(const double &, const double *, const double *, double *, const double &, double *, F77_INT *)
Definition: DASRT.cc:37
octave_f77_int_type liw
Definition: DASRT.h:110
void resize(octave_idx_type n, const double &rfv=0)
Definition: dColVector.h:107
octave_f77_int_type ng
Definition: DASRT.h:113