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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3984
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
|
30 #include <iomanip> |
|
31 #include <iostream> |
|
32 |
|
33 #include "ODESSA.h" |
|
34 #include "lo-mappers.h" |
|
35 |
|
36 #include "defun-dld.h" |
|
37 #include "error.h" |
|
38 #include "gripes.h" |
|
39 #include "oct-obj.h" |
|
40 #include "ov-fcn.h" |
|
41 #include "pager.h" |
|
42 #include "pr-output.h" |
|
43 #include "unwind-prot.h" |
|
44 #include "utils.h" |
|
45 #include "variables.h" |
|
46 #include "parse.h" |
|
47 |
3998
|
48 #include "ODESSA-opts.cc" |
|
49 |
3984
|
50 // Global pointer for user defined function required by odessa. |
|
51 static octave_function *odessa_f; |
|
52 static octave_function *odessa_j; |
|
53 static octave_function *odessa_b; |
|
54 |
4140
|
55 // Have we warned about imaginary values returned from user function? |
|
56 static bool warned_fcn_imaginary = false; |
|
57 static bool warned_jac_imaginary = false; |
|
58 static bool warned_b_imaginary = false; |
|
59 |
3984
|
60 // Is this a recursive call? |
|
61 static int call_depth = 0; |
|
62 |
|
63 static ColumnVector |
4141
|
64 odessa_user_f (const ColumnVector& x, double t, const ColumnVector& theta) |
3984
|
65 { |
|
66 ColumnVector retval; |
|
67 |
|
68 octave_value_list args; |
|
69 |
|
70 int n = x.length (); |
|
71 int npar = theta.length (); |
|
72 |
|
73 if (npar > 1) |
|
74 args(2) = theta; |
|
75 else if (npar == 1) |
|
76 args(2) = theta(0); |
|
77 else |
|
78 args(2) = Matrix (); |
|
79 |
4141
|
80 args(1) = t; |
|
81 |
3984
|
82 if (n > 1) |
4141
|
83 args(0) = x; |
3984
|
84 else if (n == 1) |
4141
|
85 args(0) = x(0); |
3984
|
86 else |
4141
|
87 args(0) = Matrix (); |
3984
|
88 |
|
89 if (odessa_f) |
|
90 { |
|
91 octave_value_list tmp = odessa_f->do_multi_index_op (1, args); |
|
92 |
|
93 if (error_state) |
|
94 { |
|
95 gripe_user_supplied_eval ("odessa"); |
|
96 return retval; |
|
97 } |
|
98 |
|
99 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
100 { |
4140
|
101 if (! warned_fcn_imaginary && tmp(0).is_complex_type ()) |
|
102 { |
|
103 warning ("odessa: ignoring imaginary part returned from user-supplied function"); |
|
104 warned_fcn_imaginary = true; |
|
105 } |
|
106 |
3984
|
107 retval = ColumnVector (tmp(0).vector_value ()); |
|
108 |
|
109 if (error_state || retval.length () == 0) |
|
110 gripe_user_supplied_eval ("odessa"); |
|
111 } |
|
112 else |
|
113 gripe_user_supplied_eval ("odessa"); |
|
114 } |
|
115 |
|
116 return retval; |
|
117 } |
|
118 |
|
119 static Matrix |
4141
|
120 odessa_user_j (const ColumnVector& x, double t, const ColumnVector& theta) |
3984
|
121 { |
|
122 Matrix retval; |
|
123 |
4141
|
124 if (odessa_j) |
3984
|
125 { |
|
126 octave_value_list args; |
|
127 |
|
128 int n = x.length (); |
|
129 int npar = theta.length (); |
|
130 |
|
131 if (npar > 1) |
|
132 args(2) = theta; |
|
133 else if (npar == 1) |
|
134 args(2) = theta(0); |
|
135 else |
|
136 args(2) = Matrix (); |
|
137 |
4187
|
138 args(1) = t; |
3984
|
139 |
4141
|
140 if (n > 1) |
|
141 args(0) = x; |
|
142 else if (n == 1) |
|
143 args(0) = x(0); |
|
144 else |
|
145 args(0) = Matrix (); |
|
146 |
|
147 octave_value_list tmp = odessa_j->do_multi_index_op (1, args); |
3984
|
148 |
|
149 if (error_state) |
|
150 { |
|
151 gripe_user_supplied_eval ("odessa"); |
|
152 return retval; |
|
153 } |
|
154 |
|
155 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
156 { |
4140
|
157 if (! warned_jac_imaginary && tmp(0).is_complex_type ()) |
|
158 { |
|
159 warning ("odessa: ignoring imaginary part returned from user-supplied jacobian function"); |
|
160 warned_jac_imaginary = true; |
|
161 } |
|
162 |
3984
|
163 retval = tmp(0).matrix_value (); |
|
164 |
|
165 if (error_state || retval.length () == 0) |
|
166 gripe_user_supplied_eval ("odessa"); |
|
167 } |
|
168 else |
|
169 gripe_user_supplied_eval ("odessa"); |
|
170 } |
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
|
175 static ColumnVector |
4141
|
176 odessa_user_b (const ColumnVector& x, double t, |
3984
|
177 const ColumnVector& theta, int column) |
|
178 { |
|
179 ColumnVector retval; |
|
180 |
|
181 if (odessa_b) |
|
182 { |
|
183 octave_value_list args; |
|
184 |
|
185 int n = x.length (); |
|
186 int npar = theta.length (); |
|
187 |
|
188 args(3) = static_cast<double> (column); |
|
189 |
|
190 if (npar > 1) |
|
191 args(2) = theta; |
|
192 else if (npar == 1) |
|
193 args(2) = theta(0); |
|
194 else |
|
195 args(2) = Matrix (); |
|
196 |
4141
|
197 args(1) = t; |
|
198 |
3984
|
199 if (n > 1) |
4141
|
200 args(0) = x; |
3984
|
201 else if (n == 1) |
4141
|
202 args(0) = x(0); |
3984
|
203 else |
4141
|
204 args(0) = Matrix (); |
3984
|
205 |
|
206 octave_value_list tmp = odessa_b->do_multi_index_op (1, args); |
|
207 |
|
208 if (error_state) |
|
209 { |
|
210 gripe_user_supplied_eval ("odessa"); |
|
211 return retval; |
|
212 } |
|
213 |
|
214 if (tmp.length () > 0 && tmp(0).is_defined ()) |
|
215 { |
4140
|
216 if (! warned_b_imaginary && tmp(0).is_complex_type ()) |
|
217 { |
|
218 warning ("odessa: ignoring imaginary part returned from user-supplied inhomogeneity function"); |
|
219 warned_b_imaginary = true; |
|
220 } |
|
221 |
3984
|
222 retval = ColumnVector (tmp(0).vector_value ()); |
|
223 |
|
224 if (error_state || retval.length () == 0) |
|
225 gripe_user_supplied_eval ("odessa"); |
|
226 } |
|
227 else |
|
228 gripe_user_supplied_eval ("odessa"); |
|
229 } |
|
230 |
|
231 return retval; |
|
232 } |
|
233 |
4233
|
234 static octave_value |
3984
|
235 make_list (const Array<Matrix>& m_array) |
|
236 { |
|
237 octave_value_list retval; |
|
238 |
|
239 int len = m_array.length (); |
|
240 |
|
241 retval.resize (len); |
|
242 |
|
243 for (int i = 0; i < len; i++) |
|
244 retval(i) = m_array(i); |
|
245 |
4233
|
246 return octave_value (retval); |
3984
|
247 } |
|
248 |
|
249 #define ODESSA_ABORT() \ |
|
250 do \ |
|
251 { \ |
|
252 unwind_protect::run_frame ("Fodessa"); \ |
|
253 return retval; \ |
|
254 } \ |
|
255 while (0) |
|
256 |
|
257 #define ODESSA_ABORT1(msg) \ |
|
258 do \ |
|
259 { \ |
|
260 ::error ("odessa: " msg); \ |
|
261 ODESSA_ABORT (); \ |
|
262 } \ |
|
263 while (0) |
|
264 |
|
265 #define ODESSA_ABORT2(fmt, arg) \ |
|
266 do \ |
|
267 { \ |
|
268 ::error ("odessa: " fmt, arg); \ |
|
269 ODESSA_ABORT (); \ |
|
270 } \ |
|
271 while (0) |
|
272 |
3997
|
273 DEFUN_DLD (odessa, args, nargout, |
4115
|
274 "-*- texinfo -*-\n\ |
|
275 @deftypefn {Loadable Function} {[@var{x}, @var{sx}, @var{istate}, @var{msg}]} odessa (@var{fcn}, @var{x_0}, @var{p}, @var{sx_0}, @var{t}, @var{t_crit})\n\ |
|
276 Solve the set of differential equations\n\ |
|
277 @tex\n\ |
|
278 $$ {dx \\over dt} = f (x, t; p) $$\n\ |
|
279 with\n\ |
|
280 $$ x(t_0) = x_0 $$\n\ |
|
281 @end tex\n\ |
|
282 @ifinfo\n\ |
|
283 \n\ |
|
284 @example\n\ |
|
285 dx\n\ |
|
286 -- = f(x, t; p)\n\ |
|
287 dt\n\ |
|
288 @end example\n\ |
|
289 \n\ |
|
290 with\n\ |
|
291 \n\ |
|
292 @example\n\ |
|
293 x(t_0) = x_0\n\ |
|
294 @end example\n\ |
|
295 \n\ |
|
296 @end ifinfo\n\ |
|
297 and simultaneously compute the first-order sensitivity coefficients\n\ |
|
298 given by\n\ |
|
299 \n\ |
|
300 @example\n\ |
|
301 s'(t) = j(t)*s(t) + df/dp\n\ |
|
302 @end example\n\ |
|
303 \n\ |
|
304 in which\n\ |
|
305 \n\ |
|
306 @example\n\ |
|
307 s(t) = dx(t)/dp (sensitivity functions)\n\ |
|
308 s'(t) = d(dx(t)/dp)/dt\n\ |
|
309 j(t) = df(x,t;p)/dx(t) (Jacobian matrix)\n\ |
|
310 df/dp = df(x,t;p)/dp (inhomogeneity matrix)\n\ |
|
311 @end example\n\ |
|
312 \n\ |
|
313 The solution is returned in the matrix @var{x}, with each row\n\ |
|
314 corresponding to an element of the vector @var{t}. The first element\n\ |
|
315 of @var{t} should be @math{t_0} and should correspond to the initial\n\ |
|
316 state of the system @var{x_0}, so that the first row of the output\n\ |
|
317 is @var{x_0}.\n\ |
|
318 \n\ |
|
319 The sensitivities are returned in a list of matrices, @var{sx},\n\ |
|
320 with each element of the list corresponding to an element of the\n\ |
|
321 vector @var{t}.\n\ |
|
322 \n\ |
|
323 The first argument, @var{fcn}, is a string that names the function to\n\ |
|
324 call to compute the vector of right hand sides for the set of equations.\n\ |
|
325 The function must have the form\n\ |
|
326 \n\ |
|
327 @example\n\ |
4141
|
328 @var{xdot} = f (@var{x}, @var{t}, @var{p})\n\ |
4115
|
329 @end example\n\ |
3984
|
330 \n\ |
4115
|
331 @noindent\n\ |
|
332 in which @var{xdot} and @var{x} are vectors and @var{t} is a scalar.\n\ |
|
333 \n\ |
4141
|
334 The variable @var{p} is a vector of parameters.\n\ |
|
335 \n\ |
4115
|
336 The @var{fcn} argument may also be an array of strings\n\ |
|
337 \n\ |
|
338 @example\n\ |
|
339 [\"f\"; \"j\"; \"b\"]\n\ |
|
340 @end example\n\ |
|
341 \n\ |
|
342 in which the first element names the function @math{f} described\n\ |
|
343 above, the second element names a function to compute the Jacobian\n\ |
4117
|
344 of @math{f}, and the third element names a function to compute the\n\ |
4115
|
345 inhomogeneity matrix.\n\ |
|
346 \n\ |
|
347 The Jacobian function must have the form\n\ |
|
348 \n\ |
|
349 @example\n\ |
4141
|
350 @var{jac} = j (@var{x}, @var{t}, @var{p})\n\ |
4115
|
351 @end example\n\ |
|
352 \n\ |
4141
|
353 in which @var{x}, @var{t}, and @var{p} have the same meanings as\n\ |
|
354 above for the function @var{f}, and @var{jac} is the matrix of\n\ |
|
355 partial derivatives\n\ |
4115
|
356 @tex\n\ |
|
357 $$ J = {\\partial f_i \\over \\partial x_j} $$\n\ |
|
358 @end tex\n\ |
|
359 @ifinfo\n\ |
3984
|
360 \n\ |
4115
|
361 @example\n\ |
|
362 df_i\n\ |
|
363 jac = ----\n\ |
|
364 dx_j\n\ |
|
365 @end example\n\ |
|
366 \n\ |
|
367 @end ifinfo\n\ |
|
368 \n\ |
|
369 The function @var{b} must have the form\n\ |
|
370 \n\ |
|
371 @example\n\ |
4141
|
372 @var{dfdp} = b (@var{x}, @var{t}, @var{p}, @var{c})\n\ |
|
373 @end example\n\ |
|
374 \n\ |
|
375 in which @var{x}, @var{t}, and @var{p} have the same meanings as\n\ |
|
376 above for the function @var{f}, @var{c} indicates which partial\n\ |
|
377 derivatives to return in @var{dfdp}. For example, if @var{c} is 2,\n\ |
|
378 you should return the vector\n\ |
|
379 \n\ |
|
380 @example\n\ |
|
381 df_i\n\ |
|
382 dfdp = ----, i = 1:length(x)\n\ |
|
383 dp_2\n\ |
4115
|
384 @end example\n\ |
|
385 \n\ |
|
386 The second argument, @var{x_0}, specifies the intial state of the system.\n\ |
|
387 \n\ |
|
388 The third argument, @var{p}, specifies the set of parameters.\n\ |
3984
|
389 \n\ |
4115
|
390 The fourth argument, @var{sx_0} specifies the initial values of the\n\ |
|
391 sensitivities.\n\ |
|
392 \n\ |
|
393 The sixth argument is optional, and may be used to specify a set of\n\ |
|
394 times that the ODE solver should not integrate past. It is useful for\n\ |
|
395 avoiding difficulties with singularities and points where there is a\n\ |
|
396 discontinuity in the derivative.\n\ |
|
397 \n\ |
|
398 After a successful computation, the value of @var{istate} will be 2\n\ |
|
399 (consistent with the Fortran version of @sc{Odessa}).\n\ |
|
400 \n\ |
|
401 If the computation is not successful, @var{istate} will be something\n\ |
|
402 other than 2 and @var{msg} will contain additional information.\n\ |
|
403 \n\ |
4888
|
404 You can use the function @code{odessa_options} to set optional\n\ |
|
405 parameters for @code{odessa}.\n\ |
4115
|
406 @end deftypefn\n\ |
|
407 @seealso{daspk, dassl, dasrt, lsode}") |
3984
|
408 { |
|
409 octave_value_list retval; |
|
410 |
4140
|
411 warned_fcn_imaginary = false; |
|
412 warned_jac_imaginary = false; |
|
413 warned_b_imaginary = false; |
|
414 |
3984
|
415 unwind_protect::begin_frame ("Fodessa"); |
|
416 |
|
417 unwind_protect_int (call_depth); |
|
418 call_depth++; |
|
419 |
|
420 if (call_depth > 1) |
|
421 ODESSA_ABORT1 ("invalid recursive call"); |
|
422 |
|
423 int nargin = args.length (); |
|
424 |
|
425 if (nargin < 5 || nargin > 6) |
|
426 { |
|
427 print_usage ("odessa"); |
|
428 unwind_protect::run_frame ("Fodessa"); |
|
429 return retval; |
|
430 } |
|
431 |
|
432 odessa_f = 0; |
|
433 odessa_j = 0; |
|
434 odessa_b = 0; |
|
435 |
|
436 octave_value f_arg = args(0); |
|
437 |
4141
|
438 int nr = f_arg.rows (); |
|
439 |
|
440 if (nr == 1) |
3984
|
441 { |
4141
|
442 odessa_f = extract_function |
|
443 (f_arg, "odessa", "__odessa_fcn__", |
|
444 "function xdot = __odessa_fcn__ (x, t, p) xdot = ", |
|
445 "; endfunction"); |
|
446 } |
|
447 else if (nr == 2 || nr == 3) |
|
448 { |
|
449 string_vector tmp = f_arg.all_strings (); |
|
450 |
|
451 if (! error_state) |
3984
|
452 { |
4141
|
453 odessa_f = extract_function |
|
454 (tmp(0), "odessa", "__odessa_fcn__", |
|
455 "function xdot = __odessa_fcn__ (x, t, p) xdot = ", |
|
456 "; endfunction"); |
3984
|
457 |
4141
|
458 if (odessa_f) |
3984
|
459 { |
4141
|
460 odessa_j = extract_function |
|
461 (tmp(1), "odessa", "__odessa_jac__", |
|
462 "function xdot = __odessa_jac__ (x, t, p) jac = ", |
|
463 "; endfunction"); |
|
464 |
|
465 if (odessa_j && nr == 3) |
|
466 { |
|
467 odessa_b = extract_function |
4152
|
468 (tmp(2), "odessa", "__odessa_b__", |
4141
|
469 "function dfdp = __odessa_b__ (x, t, p, c) dfdp = ", |
|
470 "; endfunction"); |
|
471 |
|
472 if (! odessa_b) |
|
473 odessa_j = 0; |
|
474 } |
|
475 |
|
476 if (! odessa_j) |
|
477 odessa_f = 0; |
3984
|
478 } |
|
479 } |
4141
|
480 } |
3984
|
481 |
4141
|
482 if (error_state || ! odessa_f) |
|
483 ODESSA_ABORT1 |
|
484 ("expecting function name as argument 1"); |
3984
|
485 |
|
486 ColumnVector state (args(1).vector_value ()); |
|
487 |
|
488 if (error_state) |
|
489 ODESSA_ABORT1 ("expecting state vector as argument 2"); |
|
490 |
|
491 bool have_parameters = false; |
|
492 |
|
493 ColumnVector theta; |
|
494 Matrix sensitivity_guess; |
|
495 |
|
496 if (nargin == 5 || nargin == 6) |
|
497 { |
|
498 octave_value theta_arg = args(2); |
|
499 |
|
500 if (! theta_arg.is_empty ()) |
|
501 { |
|
502 theta = ColumnVector (theta_arg.vector_value ()); |
|
503 |
|
504 if (error_state) |
|
505 ODESSA_ABORT1 |
|
506 ("expecting parameter vector as argument 3"); |
|
507 } |
|
508 |
|
509 have_parameters = (theta.length () > 0); |
|
510 |
|
511 if (have_parameters) |
|
512 { |
|
513 sensitivity_guess = args(3).matrix_value (); |
|
514 |
|
515 if (error_state) |
|
516 ODESSA_ABORT1 |
|
517 ("expecting sensitivity guess as argument 4"); |
|
518 |
|
519 if (sensitivity_guess.rows () != state.length () |
|
520 || sensitivity_guess.columns () != theta.length ()) |
|
521 ODESSA_ABORT1 |
|
522 ("incorrect dimension for sensitivity guess"); |
|
523 } |
|
524 } |
|
525 |
|
526 ColumnVector out_times (args(4).vector_value ()); |
|
527 |
|
528 if (error_state) |
|
529 ODESSA_ABORT1 |
|
530 ("expecting output time vector as %s argument 5"); |
|
531 |
|
532 ColumnVector crit_times; |
|
533 |
|
534 bool crit_times_set = false; |
|
535 |
4628
|
536 if (nargin == 6) |
3984
|
537 { |
|
538 crit_times = ColumnVector (args(5).vector_value ()); |
|
539 |
|
540 if (error_state) |
|
541 ODESSA_ABORT1 |
|
542 ("expecting critical time vector as argument 6"); |
|
543 |
|
544 crit_times_set = true; |
|
545 } |
|
546 |
|
547 ODESFunc func (odessa_user_f); |
|
548 |
|
549 if (odessa_j) |
|
550 func.set_jsub_function (odessa_user_j); |
|
551 |
|
552 if (odessa_b) |
|
553 func.set_bsub_function (odessa_user_b); |
|
554 |
|
555 double tzero = out_times (0); |
|
556 |
|
557 ODESSA_result output; |
|
558 |
3997
|
559 ODESSA ode = have_parameters |
|
560 ? ODESSA (state, theta, sensitivity_guess, tzero, func) |
|
561 : ODESSA (state, tzero, func); |
3984
|
562 |
4122
|
563 ode.set_options (odessa_opts); |
3984
|
564 |
3997
|
565 if (crit_times_set) |
|
566 output = ode.integrate (out_times, crit_times); |
3984
|
567 else |
3997
|
568 output = ode.integrate (out_times); |
3984
|
569 |
|
570 if (! error_state) |
|
571 { |
3997
|
572 int k = have_parameters ? 3 : 2; |
|
573 |
|
574 std::string msg = ode.error_message (); |
|
575 |
|
576 retval(k--) = msg; |
|
577 retval(k--) = static_cast<double> (ode.integration_state ()); |
|
578 |
|
579 if (ode.integration_ok ()) |
3984
|
580 { |
3997
|
581 if (have_parameters) |
|
582 retval(1) = make_list (output.state_sensitivity ()); |
|
583 |
|
584 retval(0) = output.state (); |
3984
|
585 } |
3997
|
586 else |
|
587 { |
|
588 if (have_parameters) |
|
589 retval(1) = Matrix (); |
|
590 |
|
591 retval(0) = Matrix (); |
|
592 |
|
593 if ((have_parameters && nargout < 3) || nargout < 2) |
|
594 error ("odessa: %s", msg.c_str ()); |
|
595 } |
3984
|
596 } |
|
597 |
|
598 unwind_protect::run_frame ("Fodessa"); |
|
599 |
|
600 return retval; |
|
601 } |
|
602 |
|
603 /* |
|
604 ;;; Local Variables: *** |
|
605 ;;; mode: C++ *** |
|
606 ;;; End: *** |
|
607 */ |