1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math3.ode;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.math3.exception.DimensionMismatchException;
23 import org.apache.commons.math3.exception.MaxCountExceededException;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class ExpandableStatefulODE {
50
51
52 private final FirstOrderDifferentialEquations primary;
53
54
55 private final EquationsMapper primaryMapper;
56
57
58 private double time;
59
60
61 private final double[] primaryState;
62
63
64 private final double[] primaryStateDot;
65
66
67 private List<SecondaryComponent> components;
68
69
70
71
72 public ExpandableStatefulODE(final FirstOrderDifferentialEquations primary) {
73 final int n = primary.getDimension();
74 this.primary = primary;
75 this.primaryMapper = new EquationsMapper(0, n);
76 this.time = Double.NaN;
77 this.primaryState = new double[n];
78 this.primaryStateDot = new double[n];
79 this.components = new ArrayList<ExpandableStatefulODE.SecondaryComponent>();
80 }
81
82
83
84
85 public FirstOrderDifferentialEquations getPrimary() {
86 return primary;
87 }
88
89
90
91
92
93
94
95 public int getTotalDimension() {
96 if (components.isEmpty()) {
97
98 return primaryMapper.getDimension();
99 } else {
100
101 final EquationsMapper lastMapper = components.get(components.size() - 1).mapper;
102 return lastMapper.getFirstIndex() + lastMapper.getDimension();
103 }
104 }
105
106
107
108
109
110
111
112
113 public void computeDerivatives(final double t, final double[] y, final double[] yDot)
114 throws MaxCountExceededException, DimensionMismatchException {
115
116
117 primaryMapper.extractEquationData(y, primaryState);
118 primary.computeDerivatives(t, primaryState, primaryStateDot);
119
120
121 for (final SecondaryComponent component : components) {
122 component.mapper.extractEquationData(y, component.state);
123 component.equation.computeDerivatives(t, primaryState, primaryStateDot,
124 component.state, component.stateDot);
125 component.mapper.insertEquationData(component.stateDot, yDot);
126 }
127
128 primaryMapper.insertEquationData(primaryStateDot, yDot);
129
130 }
131
132
133
134
135
136 public int addSecondaryEquations(final SecondaryEquations secondary) {
137
138 final int firstIndex;
139 if (components.isEmpty()) {
140
141 components = new ArrayList<ExpandableStatefulODE.SecondaryComponent>();
142 firstIndex = primary.getDimension();
143 } else {
144 final SecondaryComponent last = components.get(components.size() - 1);
145 firstIndex = last.mapper.getFirstIndex() + last.mapper.getDimension();
146 }
147
148 components.add(new SecondaryComponent(secondary, firstIndex));
149
150 return components.size() - 1;
151
152 }
153
154
155
156
157
158 public EquationsMapper getPrimaryMapper() {
159 return primaryMapper;
160 }
161
162
163
164
165
166 public EquationsMapper[] getSecondaryMappers() {
167 final EquationsMapper[] mappers = new EquationsMapper[components.size()];
168 for (int i = 0; i < mappers.length; ++i) {
169 mappers[i] = components.get(i).mapper;
170 }
171 return mappers;
172 }
173
174
175
176
177 public void setTime(final double time) {
178 this.time = time;
179 }
180
181
182
183
184 public double getTime() {
185 return time;
186 }
187
188
189
190
191
192
193 public void setPrimaryState(final double[] primaryState) throws DimensionMismatchException {
194
195
196 if (primaryState.length != this.primaryState.length) {
197 throw new DimensionMismatchException(primaryState.length, this.primaryState.length);
198 }
199
200
201 System.arraycopy(primaryState, 0, this.primaryState, 0, primaryState.length);
202
203 }
204
205
206
207
208 public double[] getPrimaryState() {
209 return primaryState.clone();
210 }
211
212
213
214
215 public double[] getPrimaryStateDot() {
216 return primaryStateDot.clone();
217 }
218
219
220
221
222
223
224
225
226 public void setSecondaryState(final int index, final double[] secondaryState)
227 throws DimensionMismatchException {
228
229
230 double[] localArray = components.get(index).state;
231
232
233 if (secondaryState.length != localArray.length) {
234 throw new DimensionMismatchException(secondaryState.length, localArray.length);
235 }
236
237
238 System.arraycopy(secondaryState, 0, localArray, 0, secondaryState.length);
239
240 }
241
242
243
244
245
246
247 public double[] getSecondaryState(final int index) {
248 return components.get(index).state.clone();
249 }
250
251
252
253
254
255
256 public double[] getSecondaryStateDot(final int index) {
257 return components.get(index).stateDot.clone();
258 }
259
260
261
262
263
264
265 public void setCompleteState(final double[] completeState)
266 throws DimensionMismatchException {
267
268
269 if (completeState.length != getTotalDimension()) {
270 throw new DimensionMismatchException(completeState.length, getTotalDimension());
271 }
272
273
274 primaryMapper.extractEquationData(completeState, primaryState);
275 for (final SecondaryComponent component : components) {
276 component.mapper.extractEquationData(completeState, component.state);
277 }
278
279 }
280
281
282
283
284
285
286 public double[] getCompleteState() throws DimensionMismatchException {
287
288
289 double[] completeState = new double[getTotalDimension()];
290
291
292 primaryMapper.insertEquationData(primaryState, completeState);
293 for (final SecondaryComponent component : components) {
294 component.mapper.insertEquationData(component.state, completeState);
295 }
296
297 return completeState;
298
299 }
300
301
302 private static class SecondaryComponent {
303
304
305 private final SecondaryEquations equation;
306
307
308 private final EquationsMapper mapper;
309
310
311 private final double[] state;
312
313
314 private final double[] stateDot;
315
316
317
318
319
320 public SecondaryComponent(final SecondaryEquations equation, final int firstIndex) {
321 final int n = equation.getDimension();
322 this.equation = equation;
323 mapper = new EquationsMapper(firstIndex, n);
324 state = new double[n];
325 stateDot = new double[n];
326 }
327
328 }
329
330 }