1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.ode.nonstiff;
19
20 import org.apache.commons.math4.legacy.core.Field;
21 import org.apache.commons.math4.legacy.core.RealFieldElement;
22 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
23 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
24 import org.apache.commons.math4.legacy.exception.NoBracketingException;
25 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
26 import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
27 import org.apache.commons.math4.legacy.ode.FieldExpandableODE;
28 import org.apache.commons.math4.legacy.ode.FieldODEState;
29 import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
30 import org.apache.commons.math4.legacy.core.MathArrays;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldElement<T>>
69 extends AdaptiveStepsizeFieldIntegrator<T>
70 implements FieldButcherArrayProvider<T> {
71
72
73 private final int fsal;
74
75
76 private final T[] c;
77
78
79 private final T[][] a;
80
81
82 private final T[] b;
83
84
85 private final T exp;
86
87
88 private T safety;
89
90
91 private T minReduction;
92
93
94 private T maxGrowth;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 protected EmbeddedRungeKuttaFieldIntegrator(final Field<T> field, final String name, final int fsal,
111 final double minStep, final double maxStep,
112 final double scalAbsoluteTolerance,
113 final double scalRelativeTolerance) {
114
115 super(field, name, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
116
117 this.fsal = fsal;
118 this.c = getC();
119 this.a = getA();
120 this.b = getB();
121
122 exp = field.getOne().divide(-getOrder());
123
124
125 setSafety(field.getZero().add(0.9));
126 setMinReduction(field.getZero().add(0.2));
127 setMaxGrowth(field.getZero().add(10.0));
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 protected EmbeddedRungeKuttaFieldIntegrator(final Field<T> field, final String name, final int fsal,
143 final double minStep, final double maxStep,
144 final double[] vecAbsoluteTolerance,
145 final double[] vecRelativeTolerance) {
146
147 super(field, name, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
148
149 this.fsal = fsal;
150 this.c = getC();
151 this.a = getA();
152 this.b = getB();
153
154 exp = field.getOne().divide(-getOrder());
155
156
157 setSafety(field.getZero().add(0.9));
158 setMinReduction(field.getZero().add(0.2));
159 setMaxGrowth(field.getZero().add(10.0));
160 }
161
162
163
164
165
166
167 protected T fraction(final int p, final int q) {
168 return getField().getOne().multiply(p).divide(q);
169 }
170
171
172
173
174
175
176 protected T fraction(final double p, final double q) {
177 return getField().getOne().multiply(p).divide(q);
178 }
179
180
181
182
183
184
185
186
187
188 protected abstract RungeKuttaFieldStepInterpolator<T> createInterpolator(boolean forward, T[][] yDotK,
189 FieldODEStateAndDerivative<T> globalPreviousState,
190 FieldODEStateAndDerivative<T> globalCurrentState,
191 FieldEquationsMapper<T> mapper);
192
193
194
195 public abstract int getOrder();
196
197
198
199
200 public T getSafety() {
201 return safety;
202 }
203
204
205
206
207 public void setSafety(final T safety) {
208 this.safety = safety;
209 }
210
211
212 @Override
213 public FieldODEStateAndDerivative<T> integrate(final FieldExpandableODE<T> equations,
214 final FieldODEState<T> initialState, final T finalTime)
215 throws NumberIsTooSmallException, DimensionMismatchException,
216 MaxCountExceededException, NoBracketingException {
217
218 sanityChecks(initialState, finalTime);
219 final T t0 = initialState.getTime();
220 final T[] y0 = equations.getMapper().mapState(initialState);
221 setStepStart(initIntegration(equations, t0, y0, finalTime));
222 final boolean forward = finalTime.subtract(initialState.getTime()).getReal() > 0;
223
224
225 final int stages = c.length + 1;
226 T[] y = y0;
227 final T[][] yDotK = MathArrays.buildArray(getField(), stages, -1);
228 final T[] yTmp = MathArrays.buildArray(getField(), y0.length);
229
230
231 T hNew = getField().getZero();
232 boolean firstTime = true;
233
234
235 setIsLastStep(false);
236 do {
237
238
239 T error = getField().getZero().add(10);
240 while (error.subtract(1.0).getReal() >= 0) {
241
242
243 y = equations.getMapper().mapState(getStepStart());
244 yDotK[0] = equations.getMapper().mapDerivative(getStepStart());
245
246 if (firstTime) {
247 final T[] scale = MathArrays.buildArray(getField(), mainSetDimension);
248 if (vecAbsoluteTolerance == null) {
249 for (int i = 0; i < scale.length; ++i) {
250 scale[i] = y[i].abs().multiply(scalRelativeTolerance).add(scalAbsoluteTolerance);
251 }
252 } else {
253 for (int i = 0; i < scale.length; ++i) {
254 scale[i] = y[i].abs().multiply(vecRelativeTolerance[i]).add(vecAbsoluteTolerance[i]);
255 }
256 }
257 hNew = initializeStep(forward, getOrder(), scale, getStepStart(), equations.getMapper());
258 firstTime = false;
259 }
260
261 setStepSize(hNew);
262 if (forward) {
263 if (getStepStart().getTime().add(getStepSize()).subtract(finalTime).getReal() >= 0) {
264 setStepSize(finalTime.subtract(getStepStart().getTime()));
265 }
266 } else {
267 if (getStepStart().getTime().add(getStepSize()).subtract(finalTime).getReal() <= 0) {
268 setStepSize(finalTime.subtract(getStepStart().getTime()));
269 }
270 }
271
272
273 for (int k = 1; k < stages; ++k) {
274
275 for (int j = 0; j < y0.length; ++j) {
276 T sum = yDotK[0][j].multiply(a[k-1][0]);
277 for (int l = 1; l < k; ++l) {
278 sum = sum.add(yDotK[l][j].multiply(a[k-1][l]));
279 }
280 yTmp[j] = y[j].add(getStepSize().multiply(sum));
281 }
282
283 yDotK[k] = computeDerivatives(getStepStart().getTime().add(getStepSize().multiply(c[k-1])), yTmp);
284 }
285
286
287 for (int j = 0; j < y0.length; ++j) {
288 T sum = yDotK[0][j].multiply(b[0]);
289 for (int l = 1; l < stages; ++l) {
290 sum = sum.add(yDotK[l][j].multiply(b[l]));
291 }
292 yTmp[j] = y[j].add(getStepSize().multiply(sum));
293 }
294
295
296 error = estimateError(yDotK, y, yTmp, getStepSize());
297 if (error.subtract(1.0).getReal() >= 0) {
298
299 final T factor = RealFieldElement.min(maxGrowth,
300 RealFieldElement.max(minReduction, safety.multiply(error.pow(exp))));
301 hNew = filterStep(getStepSize().multiply(factor), forward, false);
302 }
303 }
304 final T stepEnd = getStepStart().getTime().add(getStepSize());
305 final T[] yDotTmp = (fsal >= 0) ? yDotK[fsal] : computeDerivatives(stepEnd, yTmp);
306 final FieldODEStateAndDerivative<T> stateTmp = new FieldODEStateAndDerivative<>(stepEnd, yTmp, yDotTmp);
307
308
309 System.arraycopy(yTmp, 0, y, 0, y0.length);
310 setStepStart(acceptStep(createInterpolator(forward, yDotK, getStepStart(), stateTmp, equations.getMapper()),
311 finalTime));
312
313 if (!isLastStep()) {
314
315
316 final T factor = RealFieldElement.min(maxGrowth,
317 RealFieldElement.max(minReduction, safety.multiply(error.pow(exp))));
318 final T scaledH = getStepSize().multiply(factor);
319 final T nextT = getStepStart().getTime().add(scaledH);
320 final boolean nextIsLast = forward ?
321 nextT.subtract(finalTime).getReal() >= 0 :
322 nextT.subtract(finalTime).getReal() <= 0;
323 hNew = filterStep(scaledH, forward, nextIsLast);
324
325 final T filteredNextT = getStepStart().getTime().add(hNew);
326 final boolean filteredNextIsLast = forward ?
327 filteredNextT.subtract(finalTime).getReal() >= 0 :
328 filteredNextT.subtract(finalTime).getReal() <= 0;
329 if (filteredNextIsLast) {
330 hNew = finalTime.subtract(getStepStart().getTime());
331 }
332 }
333 } while (!isLastStep());
334
335 final FieldODEStateAndDerivative<T> finalState = getStepStart();
336 resetInternalState();
337 return finalState;
338 }
339
340
341
342
343 public T getMinReduction() {
344 return minReduction;
345 }
346
347
348
349
350 public void setMinReduction(final T minReduction) {
351 this.minReduction = minReduction;
352 }
353
354
355
356
357 public T getMaxGrowth() {
358 return maxGrowth;
359 }
360
361
362
363
364 public void setMaxGrowth(final T maxGrowth) {
365 this.maxGrowth = maxGrowth;
366 }
367
368
369
370
371
372
373
374
375 protected abstract T estimateError(T[][] yDotK, T[] y0, T[] y1, T h);
376 }