3984
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 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 the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 |
|
33 #include <cstdlib> |
|
34 #include <cfloat> |
|
35 #include <cmath> |
|
36 |
|
37 // For instantiating the Array<Matrix> object. |
|
38 #include "Array.h" |
|
39 #include "Array.cc" |
|
40 |
|
41 #include "ODESSA.h" |
|
42 #include "f77-fcn.h" |
|
43 #include "lo-error.h" |
|
44 #include "error.h" |
|
45 #include "gripes.h" |
|
46 #include "oct-obj.h" |
|
47 #include "ov-fcn.h" |
|
48 #include "pager.h" |
|
49 #include "parse.h" |
|
50 #include "unwind-prot.h" |
|
51 #include "utils.h" |
|
52 #include "variables.h" |
|
53 |
|
54 #ifndef F77_FUNC |
|
55 #define F77_FUNC(x, X) F77_FCN (x, X) |
|
56 #endif |
|
57 void |
|
58 ODESSA_options::set_integration_method (const std::string& val) |
|
59 { |
|
60 if (val == "stiff" || val == "bdf") |
|
61 x_integration_method = "stiff"; |
|
62 else if (val == "non-stiff" || val == "adams") |
|
63 x_integration_method = "non-stiff"; |
|
64 else |
|
65 (*current_liboctave_error_handler) |
|
66 ("odessa_options: method must be \"stiff\", \"bdf\", \"non-stiff\", or \"adams\""); |
|
67 } |
|
68 |
|
69 typedef int (*odessa_fcn_ptr) (int*, const double&, double*, |
3991
|
70 double*, double*); |
3984
|
71 |
|
72 typedef int (*odessa_jac_ptr) (int*, const double&, double*, |
3991
|
73 double*, const int&, const int&, |
|
74 double*, const int&); |
3984
|
75 |
|
76 typedef int (*odessa_dfdp_ptr) (int*, const double&, double*, |
3991
|
77 double*, double*, const int&); |
3984
|
78 |
|
79 |
|
80 extern "C" |
|
81 int F77_FUNC (odessa, ODESSA) (odessa_fcn_ptr, odessa_dfdp_ptr, int*, |
|
82 double*, double*, double&, double&, |
|
83 int&, double&, const double*, int&, |
|
84 int&, int*, double*, int&, int*, int&, |
|
85 odessa_jac_ptr, int&); |
|
86 |
|
87 template class Array<Matrix>; |
|
88 |
|
89 static ODESFunc::ODES_fsub user_fsub; |
|
90 static ODESFunc::ODES_bsub user_bsub; |
|
91 static ODESFunc::ODES_jsub user_jsub; |
|
92 |
|
93 |
|
94 static int |
|
95 odessa_f (int* neq, const double& t, double *state, |
|
96 double *par, double *fval) |
|
97 { |
|
98 int n = neq[0]; |
|
99 int n_par = neq[1]; |
|
100 |
|
101 // Load the state and parameter arrays as Octave objects |
|
102 |
|
103 ColumnVector tmp_state (n); |
|
104 for (int i = 0; i < n; i++) |
|
105 tmp_state(i) = state[i]; |
|
106 |
|
107 ColumnVector tmp_param (n_par); |
|
108 for (int i = 0; i < n_par; i++) |
|
109 tmp_param(i) = par[i]; |
|
110 |
|
111 ColumnVector tmp_fval = user_fsub (t, tmp_state, tmp_param); |
|
112 |
|
113 // Return the function value as a C array object |
|
114 |
|
115 for (int i = 0; i < n; i++) |
|
116 { |
|
117 fval[i] = tmp_fval(i); |
|
118 } |
|
119 |
|
120 return 0; |
|
121 } |
|
122 |
|
123 static int |
|
124 odessa_j (int* neq, const double& t, double *state, |
|
125 double *par, const int& ml, const int& mu, double *pd, const |
|
126 int& nrowpd) |
|
127 { |
|
128 int n = neq[0]; |
|
129 int n_par = neq[1]; |
|
130 |
|
131 // Load the state and parameter arrays as Octave objects |
|
132 ColumnVector tmp_state (n); |
|
133 for (int i = 0; i < n; i++) |
|
134 tmp_state(i) = state[i]; |
|
135 |
|
136 ColumnVector tmp_param (n_par); |
|
137 for (int i = 0; i < n_par; i++) |
|
138 tmp_param(i) = par[i]; |
|
139 |
|
140 Matrix tmp_fval = user_jsub (t, tmp_state, tmp_param); |
|
141 |
|
142 for (int j = 0; j < n; j++) |
|
143 for (int i = 0; i < nrowpd; i++) |
|
144 pd[nrowpd*j+i] = tmp_fval(i,j); |
|
145 |
|
146 return 0; |
|
147 } |
|
148 |
|
149 static int |
|
150 odessa_b (int* neq, const double& t, double *state, |
|
151 double *par, double *dfdp, const int& jpar) |
|
152 |
|
153 { |
|
154 int n = neq[0]; |
|
155 int n_par = neq[1]; |
|
156 |
|
157 // Load the state and parameter arrays as Octave objects |
|
158 ColumnVector tmp_state (n); |
|
159 for (int i = 0; i < n; i++) |
|
160 tmp_state(i) = state[i]; |
|
161 |
|
162 ColumnVector tmp_param (n_par); |
|
163 for (int i = 0; i < n_par; i++) |
|
164 tmp_param(i) = par[i]; |
|
165 |
|
166 ColumnVector tmp_fval = user_bsub (t, tmp_state, tmp_param, jpar); |
|
167 |
|
168 for (int i = 0; i < n; i++) |
|
169 dfdp[i] = tmp_fval(i); |
|
170 |
|
171 return 0; |
|
172 } |
|
173 |
|
174 ODESSA::ODESSA (void) : ODES (), ODESSA_options () |
|
175 { |
|
176 neq.resize(2); |
|
177 n = size (); |
|
178 |
|
179 iopt.resize(4); |
|
180 |
|
181 istate = 1; |
|
182 itask = 1; |
|
183 iopt(0) = 0; |
|
184 isopt = 0; |
|
185 iopt(1) = isopt; |
|
186 npar = 0; |
|
187 neq(0) = n; |
|
188 neq(1) = npar; |
|
189 |
|
190 sanity_checked = false; |
|
191 } |
|
192 |
|
193 ODESSA::ODESSA (const ColumnVector& state, double time, ODESFunc& f) |
|
194 : ODES (state, time, f), ODESSA_options () |
|
195 { |
|
196 neq.resize(2); |
|
197 n = size (); |
|
198 |
|
199 iopt.resize(4); |
|
200 istate = 1; |
|
201 itask = 1; |
|
202 iopt(0) = 0; |
|
203 isopt = 0; |
|
204 iopt(1) = isopt; |
|
205 |
|
206 sanity_checked = false; |
|
207 |
|
208 npar = 0; |
|
209 neq(0) = n; |
|
210 neq(1) = npar; |
|
211 |
|
212 y.resize (n, 1, 0.0); |
|
213 } |
|
214 |
|
215 ODESSA::ODESSA (const ColumnVector& state, const ColumnVector& theta, |
|
216 const Matrix& sensitivity_guess, double time, ODESFunc& f) |
|
217 : ODES (state, theta, time, f) |
|
218 { |
|
219 initialized = false; |
|
220 |
|
221 neq.resize(2); |
|
222 n = state.length(); |
|
223 npar = theta.length(); |
|
224 |
|
225 neq(0) = n; |
|
226 neq(1) = npar; |
|
227 |
|
228 sx0 = sensitivity_guess; |
|
229 par.resize (npar); |
|
230 |
|
231 for (int i = 0; i < npar; i++) |
|
232 { |
|
233 par(i) = theta(i); |
|
234 } |
|
235 |
|
236 sanity_checked = false; |
|
237 |
|
238 npar = theta.length (); |
|
239 |
|
240 iopt.resize(4); |
|
241 istate = 1; |
|
242 itask = 1; |
|
243 iopt(0) = 0; |
|
244 isopt = 1; |
|
245 iopt(1) = isopt; |
|
246 |
|
247 y.resize (n, npar+1, 0.0); |
|
248 } |
|
249 |
|
250 void |
|
251 ODESSA::integrate (double tout) |
|
252 { |
|
253 ODESSA_result retval; |
|
254 if (! initialized) |
|
255 { |
|
256 |
|
257 for (int i = 0; i < n; i++) |
|
258 y(i,0) = x(i); |
|
259 |
|
260 if (npar > 0) |
|
261 { |
|
262 for (int j = 0; j < npar; j++) |
|
263 for (int i = 0; i < n; i++) |
|
264 y(i,j+1) = sx0(i,j); |
|
265 } |
|
266 |
|
267 integration_error = false; |
|
268 |
|
269 user_fsub = ODESFunc::fsub_function (); |
|
270 user_bsub = ODESFunc::bsub_function (); |
|
271 user_jsub = ODESFunc::jsub_function (); |
|
272 |
|
273 int idf; |
|
274 |
|
275 if (user_bsub) |
|
276 idf = 1; |
|
277 else |
|
278 idf = 0; |
|
279 |
|
280 iopt(2) = idf; |
|
281 |
|
282 |
|
283 if (restart) |
|
284 { |
|
285 restart = false; |
|
286 istate = 1; |
|
287 } |
|
288 |
|
289 if (integration_method () == "stiff") |
|
290 { |
|
291 if (user_jsub) |
|
292 method_flag = 21; |
|
293 else |
|
294 method_flag = 22; |
|
295 if (isopt) |
|
296 { |
|
297 liw = 21 + n + npar; |
|
298 lrw = 22 + 8*(npar+1)*n + n*n + n; |
|
299 } |
|
300 else |
|
301 { |
|
302 liw = 20 + n; |
|
303 lrw = 22 + 9*n + n*n; |
|
304 } |
|
305 } |
|
306 else |
|
307 { |
|
308 if (isopt) |
|
309 { |
|
310 if (user_jsub) |
|
311 method_flag = 11; |
|
312 else |
|
313 method_flag = 12; |
|
314 liw = 21 + n + npar; |
|
315 lrw = 22 + 15*(npar+1)*n + n*n + n; |
|
316 } |
|
317 else |
|
318 { |
|
319 method_flag = 10; |
|
320 liw = 20 + n; |
|
321 lrw = 22 + 16 * n; |
|
322 } |
|
323 } |
|
324 |
|
325 if (iwork.length () != liw) |
|
326 { |
|
327 iwork.resize (liw); |
|
328 |
|
329 for (int i = 4; i < 10; i++) |
|
330 iwork.elem (i) = 0; |
|
331 } |
|
332 |
|
333 if (rwork.length () != lrw) |
|
334 { |
|
335 rwork.resize (lrw); |
|
336 |
|
337 for (int i = 4; i < 10; i++) |
|
338 rwork.elem (i) = 0.0; |
|
339 } |
|
340 |
|
341 initialized = true; |
|
342 } |
|
343 integration_error = false; |
|
344 |
|
345 // NOTE: this won't work if LSODE passes copies of the state vector. |
|
346 // In that case we have to create a temporary vector object |
|
347 // and copy. |
|
348 |
|
349 |
|
350 if (! sanity_checked) |
|
351 { |
|
352 ColumnVector fval = user_fsub (t, x, theta); |
|
353 |
|
354 if (fval.length () != x.length ()) |
|
355 { |
|
356 (*current_liboctave_error_handler) |
|
357 ("odessa: inconsistent sizes for state and residual vectors"); |
|
358 |
|
359 integration_error = true; |
|
360 return; |
|
361 } |
|
362 |
|
363 sanity_checked = true; |
|
364 } |
|
365 |
|
366 if (stop_time_set) |
|
367 { |
|
368 itask = 4; |
|
369 rwork.elem (0) = stop_time; |
|
370 iopt(0) = 1; |
|
371 } |
|
372 else |
|
373 { |
|
374 itask = 1; |
|
375 } |
|
376 |
|
377 double rel_tol = relative_tolerance (); |
|
378 |
|
379 Array<double> abs_tol = absolute_tolerance (); //note; this should |
|
380 // be a matrix, not a vector |
|
381 |
|
382 int abs_tol_len = abs_tol.length (); |
|
383 |
|
384 int itol; |
|
385 |
|
386 if (abs_tol_len == 1) |
|
387 itol = 1; |
|
388 else if (abs_tol_len == n) |
|
389 itol = 2; |
|
390 else |
|
391 { |
|
392 (*current_liboctave_error_handler) |
3996
|
393 ("odessa: inconsistent sizes for state and absolute tolerance vectors"); |
3984
|
394 |
|
395 integration_error = 1; |
|
396 return; |
|
397 } |
|
398 |
|
399 if (initial_step_size () >= 0.0) |
|
400 { |
|
401 rwork.elem (4) = initial_step_size (); |
|
402 iopt(0) = 1; |
|
403 } |
|
404 |
|
405 if (maximum_step_size () >= 0.0) |
|
406 { |
|
407 rwork.elem (5) = maximum_step_size (); |
|
408 iopt(0) = 1; |
|
409 } |
|
410 |
|
411 if (minimum_step_size () >= 0.0) |
|
412 { |
|
413 rwork.elem (6) = minimum_step_size (); |
|
414 iopt(0) = 1; |
|
415 } |
|
416 |
|
417 if (step_limit () > 0) |
|
418 { |
|
419 iwork.elem (5) = step_limit (); |
|
420 iopt(0) = 1; |
|
421 } |
|
422 |
|
423 py = y.fortran_vec (); |
|
424 piwork = iwork.fortran_vec (); |
|
425 prwork = rwork.fortran_vec (); |
|
426 ppar = par.fortran_vec (); |
|
427 piopt = iopt.fortran_vec (); |
|
428 pneq = neq.fortran_vec (); |
|
429 |
|
430 const double *pabs_tol = abs_tol.fortran_vec (); |
|
431 |
|
432 F77_XFCN (odessa, ODESSA, (odessa_f,odessa_b,pneq,py,ppar,t, tout, |
|
433 itol,rel_tol,pabs_tol,itask,istate, |
|
434 piopt,prwork,lrw,piwork,liw,odessa_j, |
|
435 method_flag)); |
|
436 |
|
437 if (f77_exception_encountered) |
|
438 { |
|
439 integration_error = true; |
|
440 (*current_liboctave_error_handler) ("unrecoverable error in odessa"); |
|
441 } |
|
442 else |
|
443 { |
|
444 switch (istate) |
|
445 { |
3996
|
446 case 1: // prior to initial integration step. |
|
447 case 2: // odessa was successful. |
|
448 t = tout; |
|
449 break; |
|
450 |
|
451 case -1: // excess work done on this call (perhaps wrong mf). |
|
452 case -2: // excess accuracy requested (tolerances too small). |
|
453 case -3: // illegal input detected (see printed message). |
|
454 case -4: // repeated error test failures (check all inputs). |
3984
|
455 case -5: // repeated convergence failures (perhaps bad jacobian |
|
456 // supplied or wrong choice of mf or tolerances). |
3996
|
457 case -6: // error weight became zero during problem. (solution |
|
458 // component i vanished, and atol or atol(i) = 0.) |
|
459 case -13: // Return requested in user-supplied function. |
3984
|
460 integration_error = 1; |
|
461 break; |
|
462 |
3996
|
463 default: |
|
464 integration_error = 1; |
3984
|
465 (*current_liboctave_error_handler) |
3996
|
466 ("unrecognized value of istate (= %d) returned from odessa", |
|
467 istate); |
3984
|
468 break; |
|
469 } |
|
470 } |
|
471 } |
|
472 |
|
473 std::string |
|
474 ODESSA::error_message (void) const |
|
475 { |
|
476 std::string retval; |
|
477 |
|
478 switch (istate) |
|
479 { |
|
480 case 1: |
3996
|
481 retval = "prior to initial integration step"; |
3984
|
482 break; |
|
483 |
|
484 case 2: |
|
485 retval = "successful exit"; |
|
486 break; |
|
487 |
|
488 case 3: |
|
489 retval = "prior to continuation call with modified parameters"; |
|
490 break; |
|
491 |
3996
|
492 case -1: |
|
493 retval = "excess work on this call (perhaps wrong integration method)"; |
|
494 break; |
|
495 |
|
496 case -2: |
|
497 retval = "excess accuracy requested (tolerances too small)"; |
|
498 break; |
|
499 |
|
500 case -3: |
|
501 retval = "invalid input detected (see printed message)"; |
|
502 break; |
|
503 |
|
504 case -4: |
|
505 retval = "repeated error test failures (check all inputs)"; |
|
506 break; |
|
507 |
|
508 case -5: |
|
509 retval = "repeated convergence failures (perhaps bad jacobian\ |
|
510 supplied or wrong choice of integration method or tolerances)"; |
|
511 break; |
|
512 |
|
513 case -6: |
|
514 retval = "Error weight became zero during problem.\ |
|
515 (solution component i vanished, and atol or atol(i) == 0)"; |
|
516 break; |
|
517 |
|
518 case -13: |
|
519 retval = "return requested in user-supplied function"; |
|
520 break; |
|
521 |
3984
|
522 default: |
|
523 retval = "unknown error state"; |
|
524 break; |
|
525 } |
|
526 |
|
527 return retval; |
|
528 } |
|
529 |
|
530 |
|
531 ODESSA_result |
|
532 ODESSA::integrate (const ColumnVector& tout) |
|
533 { |
|
534 ODESSA_result retval; |
|
535 |
|
536 Matrix x_out; |
|
537 |
|
538 Array<Matrix> x_s_out; |
|
539 |
|
540 int n_out = tout.capacity (); |
|
541 |
|
542 if (n_out > 0 && n > 0) |
|
543 { |
|
544 x_out.resize (n_out, n); |
|
545 |
|
546 x_s_out.resize (npar, Matrix (n_out, n, 0.0)); |
|
547 |
|
548 for (int j = 0; j < n_out; j++) |
|
549 { |
|
550 integrate (tout(j)); |
|
551 |
|
552 if (integration_error) |
|
553 { |
|
554 retval = ODESSA_result (x_out, x_s_out); |
|
555 return retval; |
|
556 } |
|
557 |
|
558 for (int i = 0; i < n; i++) |
|
559 { |
|
560 x_out(j,i) = y(i,0); |
|
561 |
|
562 for (int k = 0; k < npar; k++) |
|
563 { |
|
564 x_s_out(k)(j,i) = y(i,k+1); |
|
565 } |
|
566 } |
|
567 } |
|
568 } |
|
569 |
|
570 retval = ODESSA_result (x_out, x_s_out); |
|
571 |
|
572 return retval; |
|
573 } |
|
574 |
|
575 ODESSA_result |
|
576 ODESSA::integrate (const ColumnVector& tout, const ColumnVector& tcrit) |
|
577 { |
|
578 ODESSA_result retval; |
|
579 |
|
580 Matrix x_out; |
|
581 |
|
582 Array<Matrix> x_s_out; |
|
583 |
|
584 int n_out = tout.capacity (); |
|
585 |
|
586 if (n_out > 0 && n > 0) |
|
587 { |
|
588 x_out.resize (n_out, n); |
|
589 |
|
590 x_s_out.resize (npar, Matrix (n_out, n, 0.0)); |
|
591 |
|
592 int n_crit = tcrit.capacity (); |
|
593 |
|
594 if (n_crit > 0) |
|
595 { |
|
596 int i_crit = 0; |
|
597 int i_out = 0; |
|
598 double next_crit = tcrit(0); |
|
599 double next_out; |
|
600 while (i_out < n_out) |
|
601 { |
|
602 bool do_restart = false; |
|
603 |
|
604 next_out = tout(i_out); |
|
605 if (i_crit < n_crit) |
|
606 next_crit = tcrit(i_crit); |
|
607 |
|
608 int save_output; |
|
609 double t_out; |
|
610 |
|
611 if (next_crit == next_out) |
|
612 { |
|
613 set_stop_time (next_crit); |
|
614 t_out = next_out; |
|
615 save_output = true; |
|
616 i_out++; |
|
617 i_crit++; |
|
618 do_restart = true; |
|
619 } |
|
620 else if (next_crit < next_out) |
|
621 { |
|
622 if (i_crit < n_crit) |
|
623 { |
|
624 set_stop_time (next_crit); |
|
625 t_out = next_crit; |
|
626 save_output = false; |
|
627 i_crit++; |
|
628 do_restart = true; |
|
629 } |
|
630 else |
|
631 { |
|
632 clear_stop_time (); |
|
633 t_out = next_out; |
|
634 save_output = true; |
|
635 i_out++; |
|
636 } |
|
637 } |
|
638 else |
|
639 { |
|
640 set_stop_time (next_crit); |
|
641 t_out = next_out; |
|
642 save_output = true; |
|
643 i_out++; |
|
644 } |
|
645 integrate (t_out); |
|
646 if (integration_error) |
|
647 { |
|
648 retval = ODESSA_result (x_out, x_s_out); |
|
649 return retval; |
|
650 } |
|
651 if (save_output) |
|
652 { |
|
653 for (int i = 0; i < n; i++) |
|
654 { |
|
655 x_out(i_out-1,i) = y(i,0); |
|
656 |
|
657 for (int k = 0; k < npar; k++) |
|
658 { |
|
659 x_s_out(k)(i_out-1,i) = y(i,k+1); |
|
660 } |
|
661 } |
|
662 } |
|
663 |
|
664 if (do_restart) |
|
665 force_restart (); |
|
666 } |
|
667 |
|
668 retval = ODESSA_result (x_out, x_s_out); |
|
669 } |
|
670 else |
|
671 { |
|
672 retval = integrate (tout); |
|
673 |
|
674 if (integration_error) |
|
675 return retval; |
|
676 } |
|
677 } |
|
678 |
|
679 return retval; |
|
680 } |
|
681 |
|
682 /* |
|
683 ;;; Local Variables: *** |
|
684 ;;; mode: C++ *** |
|
685 ;;; End: *** |
|
686 */ |