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