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