1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode.sampling;
19
20 import java.io.IOException;
21 import java.io.ObjectInput;
22 import java.io.ObjectOutput;
23 import java.util.Arrays;
24
25 import org.apache.commons.math.linear.Array2DRowRealMatrix;
26 import org.apache.commons.math.ode.DerivativeException;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class NordsieckStepInterpolator extends AbstractStepInterpolator {
41
42
43 private static final long serialVersionUID = -7179861704951334960L;
44
45
46 private double scalingH;
47
48
49
50
51
52
53
54 private double referenceTime;
55
56
57 private double[] scaled;
58
59
60 private Array2DRowRealMatrix nordsieck;
61
62
63 protected double[] stateVariation;
64
65
66
67
68
69
70
71
72 public NordsieckStepInterpolator() {
73 }
74
75
76
77
78
79
80 public NordsieckStepInterpolator(final NordsieckStepInterpolator interpolator) {
81 super(interpolator);
82 scalingH = interpolator.scalingH;
83 referenceTime = interpolator.referenceTime;
84 if (interpolator.scaled != null) {
85 scaled = interpolator.scaled.clone();
86 }
87 if (interpolator.nordsieck != null) {
88 nordsieck = new Array2DRowRealMatrix(interpolator.nordsieck.getDataRef(), true);
89 }
90 if (interpolator.stateVariation != null) {
91 stateVariation = interpolator.stateVariation.clone();
92 }
93 }
94
95
96 @Override
97 protected StepInterpolator doCopy() {
98 return new NordsieckStepInterpolator(this);
99 }
100
101
102
103
104
105
106
107
108 @Override
109 public void reinitialize(final double[] y, final boolean forward) {
110 super.reinitialize(y, forward);
111 stateVariation = new double[y.length];
112 }
113
114
115
116
117
118
119
120
121
122
123
124 public void reinitialize(final double referenceTime, final double scalingH,
125 final double[] scaled, final Array2DRowRealMatrix nordsieck) {
126 this.referenceTime = referenceTime;
127 this.scalingH = scalingH;
128 this.scaled = scaled;
129 this.nordsieck = nordsieck;
130
131
132 setInterpolatedTime(getInterpolatedTime());
133
134 }
135
136
137
138
139
140
141 public void rescale(final double scalingH) {
142
143 final double ratio = scalingH / this.scalingH;
144 for (int i = 0; i < scaled.length; ++i) {
145 scaled[i] *= ratio;
146 }
147
148 final double[][] nData = nordsieck.getDataRef();
149 double power = ratio;
150 for (int i = 0; i < nData.length; ++i) {
151 power *= ratio;
152 final double[] nDataI = nData[i];
153 for (int j = 0; j < nDataI.length; ++j) {
154 nDataI[j] *= power;
155 }
156 }
157
158 this.scalingH = scalingH;
159
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public double[] getInterpolatedStateVariation()
176 throws DerivativeException {
177
178
179 getInterpolatedState();
180 return stateVariation;
181 }
182
183
184 @Override
185 protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) {
186
187 final double x = interpolatedTime - referenceTime;
188 final double normalizedAbscissa = x / scalingH;
189
190 Arrays.fill(stateVariation, 0.0);
191 Arrays.fill(interpolatedDerivatives, 0.0);
192
193
194
195 final double[][] nData = nordsieck.getDataRef();
196 for (int i = nData.length - 1; i >= 0; --i) {
197 final int order = i + 2;
198 final double[] nDataI = nData[i];
199 final double power = Math.pow(normalizedAbscissa, order);
200 for (int j = 0; j < nDataI.length; ++j) {
201 final double d = nDataI[j] * power;
202 stateVariation[j] += d;
203 interpolatedDerivatives[j] += order * d;
204 }
205 }
206
207 for (int j = 0; j < currentState.length; ++j) {
208 stateVariation[j] += scaled[j] * normalizedAbscissa;
209 interpolatedState[j] = currentState[j] + stateVariation[j];
210 interpolatedDerivatives[j] =
211 (interpolatedDerivatives[j] + scaled[j] * normalizedAbscissa) / x;
212 }
213
214 }
215
216
217 @Override
218 public void writeExternal(final ObjectOutput out)
219 throws IOException {
220
221
222 writeBaseExternal(out);
223
224
225 out.writeDouble(scalingH);
226 out.writeDouble(referenceTime);
227
228 final int n = (currentState == null) ? -1 : currentState.length;
229 if (scaled == null) {
230 out.writeBoolean(false);
231 } else {
232 out.writeBoolean(true);
233 for (int j = 0; j < n; ++j) {
234 out.writeDouble(scaled[j]);
235 }
236 }
237
238 if (nordsieck == null) {
239 out.writeBoolean(false);
240 } else {
241 out.writeBoolean(true);
242 out.writeObject(nordsieck);
243 }
244
245
246
247 }
248
249
250 @Override
251 public void readExternal(final ObjectInput in)
252 throws IOException, ClassNotFoundException {
253
254
255 final double t = readBaseExternal(in);
256
257
258 scalingH = in.readDouble();
259 referenceTime = in.readDouble();
260
261 final int n = (currentState == null) ? -1 : currentState.length;
262 final boolean hasScaled = in.readBoolean();
263 if (hasScaled) {
264 scaled = new double[n];
265 for (int j = 0; j < n; ++j) {
266 scaled[j] = in.readDouble();
267 }
268 } else {
269 scaled = null;
270 }
271
272 final boolean hasNordsieck = in.readBoolean();
273 if (hasNordsieck) {
274 nordsieck = (Array2DRowRealMatrix) in.readObject();
275 } else {
276 nordsieck = null;
277 }
278
279 if (hasScaled && hasNordsieck) {
280
281 stateVariation = new double[n];
282 setInterpolatedTime(t);
283 } else {
284 stateVariation = null;
285 }
286
287 }
288
289 }