1
|
1 // tc-lsode.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "ODE.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "variables.h" |
|
32 #include "gripes.h" |
|
33 #include "error.h" |
|
34 #include "utils.h" |
|
35 |
|
36 // Global pointer for user defined function required by lsode. |
|
37 static tree *lsode_fcn; |
|
38 |
|
39 |
|
40 #ifdef WITH_DLD |
|
41 tree_constant * |
|
42 builtin_lsode_2 (tree_constant *args, int nargin, int nargout) |
|
43 { |
|
44 return lsode (args, nargin, nargout); |
|
45 } |
|
46 #endif |
|
47 |
|
48 ColumnVector |
|
49 lsode_user_function (const ColumnVector& x, double t) |
|
50 { |
|
51 ColumnVector retval; |
|
52 |
|
53 int nstates = x.capacity (); |
|
54 |
|
55 // tree_constant name (lsode_fcn->name ()); |
|
56 tree_constant *args = new tree_constant [3]; |
|
57 // args[0] = name; |
|
58 args[2] = tree_constant (t); |
|
59 |
|
60 if (nstates > 1) |
|
61 { |
|
62 Matrix m (nstates, 1); |
|
63 for (int i = 0; i < nstates; i++) |
|
64 m (i, 0) = x.elem (i); |
|
65 tree_constant state (m); |
|
66 args[1] = state; |
|
67 } |
|
68 else |
|
69 { |
|
70 double d = x.elem (0); |
|
71 tree_constant state (d); |
|
72 args[1] = state; |
|
73 } |
|
74 |
|
75 if (lsode_fcn != NULL_TREE) |
|
76 { |
|
77 tree_constant *tmp = lsode_fcn->eval (args, 3, 1, 0); |
|
78 delete [] args; |
|
79 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
80 { |
|
81 retval = tmp[0].to_vector (); |
|
82 delete [] tmp; |
|
83 } |
|
84 else |
|
85 { |
|
86 delete [] tmp; |
|
87 gripe_user_supplied_eval ("lsode"); |
|
88 jump_to_top_level (); |
|
89 } |
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
|
95 tree_constant * |
|
96 lsode (tree_constant *args, int nargin, int nargout) |
|
97 { |
|
98 // Assumes that we have been given the correct number of arguments. |
|
99 |
|
100 tree_constant *retval = NULL_TREE_CONST; |
|
101 |
|
102 lsode_fcn = is_valid_function (args[1], "lsode", 1); |
|
103 if (lsode_fcn == NULL_TREE |
|
104 || takes_correct_nargs (lsode_fcn, 3, "lsode", 1) != 1) |
|
105 return retval; |
|
106 |
|
107 ColumnVector state = args[2].to_vector (); |
|
108 ColumnVector out_times = args[3].to_vector (); |
|
109 ColumnVector crit_times; |
|
110 int crit_times_set = 0; |
|
111 if (nargin > 4) |
|
112 { |
|
113 crit_times = args[4].to_vector (); |
|
114 crit_times_set = 1; |
|
115 } |
|
116 |
|
117 double tzero = out_times.elem (0); |
|
118 int nsteps = out_times.capacity (); |
|
119 |
|
120 ODEFunc func (lsode_user_function); |
|
121 ODE ode (state, tzero, func); |
|
122 |
|
123 int nstates = state.capacity (); |
|
124 Matrix output (nsteps, nstates + 1); |
|
125 |
|
126 if (crit_times_set) |
|
127 output = ode.integrate (out_times, crit_times); |
|
128 else |
|
129 output = ode.integrate (out_times); |
|
130 |
|
131 retval = new tree_constant [2]; |
|
132 retval[0] = tree_constant (output); |
|
133 return retval; |
|
134 } |
|
135 |
|
136 /* |
|
137 ;;; Local Variables: *** |
|
138 ;;; mode: C++ *** |
|
139 ;;; page-delimiter: "^/\\*" *** |
|
140 ;;; End: *** |
|
141 */ |