7
|
1 // f-lsode.cc -*- C++ -*- |
1
|
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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
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" |
7
|
35 #include "f-lsode.h" |
1
|
36 |
|
37 // Global pointer for user defined function required by lsode. |
|
38 static tree *lsode_fcn; |
|
39 |
|
40 #ifdef WITH_DLD |
|
41 tree_constant * |
162
|
42 builtin_lsode_2 (const tree_constant *args, int nargin, int nargout) |
1
|
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); |
258
|
78 |
1
|
79 delete [] args; |
258
|
80 |
|
81 if (error_state) |
|
82 { |
|
83 gripe_user_supplied_eval ("lsode"); |
|
84 return retval; |
|
85 } |
|
86 |
1
|
87 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
88 { |
|
89 retval = tmp[0].to_vector (); |
258
|
90 |
1
|
91 delete [] tmp; |
258
|
92 |
|
93 if (retval.length () == 0) |
|
94 gripe_user_supplied_eval ("lsode"); |
1
|
95 } |
|
96 else |
|
97 { |
|
98 delete [] tmp; |
|
99 gripe_user_supplied_eval ("lsode"); |
|
100 } |
|
101 } |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 tree_constant * |
162
|
107 lsode (const tree_constant *args, int nargin, int nargout) |
1
|
108 { |
|
109 // Assumes that we have been given the correct number of arguments. |
|
110 |
|
111 tree_constant *retval = NULL_TREE_CONST; |
|
112 |
|
113 lsode_fcn = is_valid_function (args[1], "lsode", 1); |
|
114 if (lsode_fcn == NULL_TREE |
|
115 || takes_correct_nargs (lsode_fcn, 3, "lsode", 1) != 1) |
|
116 return retval; |
|
117 |
|
118 ColumnVector state = args[2].to_vector (); |
|
119 ColumnVector out_times = args[3].to_vector (); |
|
120 ColumnVector crit_times; |
|
121 int crit_times_set = 0; |
|
122 if (nargin > 4) |
|
123 { |
|
124 crit_times = args[4].to_vector (); |
|
125 crit_times_set = 1; |
|
126 } |
|
127 |
|
128 double tzero = out_times.elem (0); |
|
129 int nsteps = out_times.capacity (); |
|
130 |
|
131 ODEFunc func (lsode_user_function); |
|
132 ODE ode (state, tzero, func); |
|
133 |
|
134 int nstates = state.capacity (); |
|
135 Matrix output (nsteps, nstates + 1); |
|
136 |
|
137 if (crit_times_set) |
|
138 output = ode.integrate (out_times, crit_times); |
|
139 else |
|
140 output = ode.integrate (out_times); |
|
141 |
|
142 retval = new tree_constant [2]; |
|
143 retval[0] = tree_constant (output); |
|
144 return retval; |
|
145 } |
|
146 |
|
147 /* |
|
148 ;;; Local Variables: *** |
|
149 ;;; mode: C++ *** |
|
150 ;;; page-delimiter: "^/\\*" *** |
|
151 ;;; End: *** |
|
152 */ |