1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class RungeKuttaIntegrator
46 implements FirstOrderIntegrator {
47
48
49
50
51
52
53
54
55
56
57 protected RungeKuttaIntegrator(double[] c, double[][] a, double[] b,
58 RungeKuttaStepInterpolator prototype,
59 double step) {
60 this.c = c;
61 this.a = a;
62 this.b = b;
63 this.prototype = prototype;
64 this.step = step;
65 handler = DummyStepHandler.getInstance();
66 switchesHandler = new SwitchingFunctionsHandler();
67 resetInternalState();
68 }
69
70
71
72
73 public abstract String getName();
74
75
76
77
78
79
80 public void setStepHandler (StepHandler handler) {
81 this.handler = handler;
82 }
83
84
85
86
87 public StepHandler getStepHandler() {
88 return handler;
89 }
90
91
92
93
94
95
96
97
98
99
100 public void addSwitchingFunction(SwitchingFunction function,
101 double maxCheckInterval,
102 double convergence,
103 int maxIterationCount) {
104 switchesHandler.add(function, maxCheckInterval, convergence, maxIterationCount);
105 }
106
107
108
109
110
111
112
113
114
115 private void sanityChecks(FirstOrderDifferentialEquations equations,
116 double t0, double[] y0, double t, double[] y)
117 throws IntegratorException {
118 if (equations.getDimension() != y0.length) {
119 throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," +
120 " initial state vector has dimension {1}",
121 new Object[] {
122 new Integer(equations.getDimension()),
123 new Integer(y0.length)
124 });
125 }
126 if (equations.getDimension() != y.length) {
127 throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," +
128 " final state vector has dimension {1}",
129 new Object[] {
130 new Integer(equations.getDimension()),
131 new Integer(y.length)
132 });
133 }
134 if (Math.abs(t - t0) <= 1.0e-12 * Math.max(Math.abs(t0), Math.abs(t))) {
135 throw new IntegratorException("too small integration interval: length = {0}",
136 new Object[] { new Double(Math.abs(t - t0)) });
137 }
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public void integrate(FirstOrderDifferentialEquations equations,
157 double t0, double[] y0,
158 double t, double[] y)
159 throws DerivativeException, IntegratorException {
160
161 sanityChecks(equations, t0, y0, t, y);
162 boolean forward = (t > t0);
163
164
165 int stages = c.length + 1;
166 if (y != y0) {
167 System.arraycopy(y0, 0, y, 0, y0.length);
168 }
169 double[][] yDotK = new double[stages][];
170 for (int i = 0; i < stages; ++i) {
171 yDotK [i] = new double[y0.length];
172 }
173 double[] yTmp = new double[y0.length];
174
175
176 AbstractStepInterpolator interpolator;
177 if (handler.requiresDenseOutput() || (! switchesHandler.isEmpty())) {
178 RungeKuttaStepInterpolator rki = (RungeKuttaStepInterpolator) prototype.copy();
179 rki.reinitialize(equations, yTmp, yDotK, forward);
180 interpolator = rki;
181 } else {
182 interpolator = new DummyStepInterpolator(yTmp, forward);
183 }
184 interpolator.storeTime(t0);
185
186
187 long nbStep = Math.max(1l, Math.abs(Math.round((t - t0) / step)));
188 boolean lastStep = false;
189 stepStart = t0;
190 stepSize = (t - t0) / nbStep;
191 handler.reset();
192 for (long i = 0; ! lastStep; ++i) {
193
194 interpolator.shift();
195
196 boolean needUpdate = false;
197 for (boolean loop = true; loop;) {
198
199
200 equations.computeDerivatives(stepStart, y, yDotK[0]);
201
202
203 for (int k = 1; k < stages; ++k) {
204
205 for (int j = 0; j < y0.length; ++j) {
206 double sum = a[k-1][0] * yDotK[0][j];
207 for (int l = 1; l < k; ++l) {
208 sum += a[k-1][l] * yDotK[l][j];
209 }
210 yTmp[j] = y[j] + stepSize * sum;
211 }
212
213 equations.computeDerivatives(stepStart + c[k-1] * stepSize, yTmp, yDotK[k]);
214
215 }
216
217
218 for (int j = 0; j < y0.length; ++j) {
219 double sum = b[0] * yDotK[0][j];
220 for (int l = 1; l < stages; ++l) {
221 sum += b[l] * yDotK[l][j];
222 }
223 yTmp[j] = y[j] + stepSize * sum;
224 }
225
226
227 interpolator.storeTime(stepStart + stepSize);
228 if (switchesHandler.evaluateStep(interpolator)) {
229 needUpdate = true;
230 stepSize = switchesHandler.getEventTime() - stepStart;
231 } else {
232 loop = false;
233 }
234
235 }
236
237
238 double nextStep = stepStart + stepSize;
239 System.arraycopy(yTmp, 0, y, 0, y0.length);
240 switchesHandler.stepAccepted(nextStep, y);
241 if (switchesHandler.stop()) {
242 lastStep = true;
243 } else {
244 lastStep = (i == (nbStep - 1));
245 }
246
247
248 interpolator.storeTime(nextStep);
249 handler.handleStep(interpolator, lastStep);
250 stepStart = nextStep;
251
252 if (switchesHandler.reset(stepStart, y) && ! lastStep) {
253
254
255 equations.computeDerivatives(stepStart, y, yDotK[0]);
256 }
257
258 if (needUpdate) {
259
260
261 nbStep = Math.max(1l, Math.abs(Math.round((t - stepStart) / step)));
262 stepSize = (t - stepStart) / nbStep;
263 i = -1;
264 }
265
266 }
267
268 resetInternalState();
269
270 }
271
272
273
274
275
276
277
278
279
280
281 public double getCurrentStepStart() {
282 return stepStart;
283 }
284
285
286
287
288
289
290
291
292
293
294 public double getCurrentSignedStepsize() {
295 return stepSize;
296 }
297
298
299 private void resetInternalState() {
300 stepStart = Double.NaN;
301 stepSize = Double.NaN;
302 }
303
304
305 private double[] c;
306
307
308 private double[][] a;
309
310
311 private double[] b;
312
313
314 private RungeKuttaStepInterpolator prototype;
315
316
317 private double step;
318
319
320 private StepHandler handler;
321
322
323 protected SwitchingFunctionsHandler switchesHandler;
324
325
326 private double stepStart;
327
328
329 private double stepSize;
330
331 }