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;
24
25 import org.hipparchus.CalculusFieldElement;
26 import org.hipparchus.ode.sampling.FieldODEStateInterpolator;
27 import org.hipparchus.ode.sampling.FieldODEStepHandler;
28 import org.hipparchus.util.MathUtils;
29
30
31
32
33
34
35 public class TestFieldProblemHandler<T extends CalculusFieldElement<T>>
36 implements FieldODEStepHandler<T> {
37
38
39 private TestFieldProblemAbstract<T> problem;
40
41
42 private T maxValueError;
43 private T maxTimeError;
44
45
46 private T lastError;
47
48
49 private T lastTime;
50
51
52 private FieldODEIntegrator<T> integrator;
53
54
55 private T expectedStepStart;
56
57
58
59
60
61
62 public TestFieldProblemHandler(TestFieldProblemAbstract<T> problem, FieldODEIntegrator<T> integrator) {
63 this.problem = problem;
64 this.integrator = integrator;
65 maxValueError = problem.getField().getZero();
66 maxTimeError = problem.getField().getZero();
67 lastError = problem.getField().getZero();
68 expectedStepStart = null;
69 }
70
71 public void init(FieldODEStateAndDerivative<T> state0, T t) {
72 maxValueError = problem.getField().getZero();
73 maxTimeError = problem.getField().getZero();
74 lastError = problem.getField().getZero();
75 expectedStepStart = null;
76 }
77
78 public void handleStep(FieldODEStateInterpolator<T> interpolator) {
79
80 T start = interpolator.getPreviousState().getTime();
81 if (start.subtract(problem.getInitialState().getTime()).divide(integrator.getCurrentSignedStepsize()).norm() > 0.001) {
82
83
84 if (expectedStepStart != null) {
85
86
87 T stepError = MathUtils.max(maxTimeError, start.subtract(expectedStepStart).abs());
88 for (T eventTime : problem.getTheoreticalEventsTimes()) {
89 stepError = MathUtils.min(stepError, start.subtract(eventTime).abs());
90 }
91 maxTimeError = MathUtils.max(maxTimeError, stepError);
92 }
93 expectedStepStart = interpolator.getCurrentState().getTime();
94 }
95
96 T pT = interpolator.getPreviousState().getTime();
97 T cT = interpolator.getCurrentState().getTime();
98 T[] errorScale = problem.getErrorScale();
99
100
101 for (int k = 0; k <= 20; ++k) {
102
103 T time = pT.add(cT.subtract(pT).multiply(k).divide(20));
104 T[] interpolatedY = interpolator.getInterpolatedState(time).getPrimaryState();
105 T[] theoreticalY = problem.computeTheoreticalState(time);
106
107
108 for (int i = 0; i < interpolatedY.length; ++i) {
109 T error = errorScale[i].multiply(interpolatedY[i].subtract(theoreticalY[i]).norm());
110 maxValueError = MathUtils.max(error, maxValueError);
111 }
112 }
113 }
114
115 public void finish(FieldODEStateAndDerivative<T> finalState) {
116 T[] theoreticalY = problem.computeTheoreticalState(finalState.getTime());
117 for (int i = 0; i < finalState.getCompleteState().length; ++i) {
118 T error = finalState.getCompleteState()[i].subtract(theoreticalY[i]).abs();
119 lastError = MathUtils.max(error, lastError);
120 }
121 lastTime = finalState.getTime();
122 }
123
124
125
126
127
128 public T getMaximalValueError() {
129 return maxValueError;
130 }
131
132
133
134
135
136 public T getMaximalTimeError() {
137 return maxTimeError;
138 }
139
140
141
142
143
144 public T getLastError() {
145 return lastError;
146 }
147
148
149
150
151
152 public T getLastTime() {
153 return lastTime;
154 }
155
156 }