1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.ode;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
23 import org.apache.commons.math4.legacy.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<>();
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 public int addSecondaryEquations(final SecondaryEquations secondary) {
136
137 final int firstIndex;
138 if (components.isEmpty()) {
139
140 components = new ArrayList<>();
141 firstIndex = primary.getDimension();
142 } else {
143 final SecondaryComponent last = components.get(components.size() - 1);
144 firstIndex = last.mapper.getFirstIndex() + last.mapper.getDimension();
145 }
146
147 components.add(new SecondaryComponent(secondary, firstIndex));
148
149 return components.size() - 1;
150 }
151
152
153
154
155
156 public EquationsMapper getPrimaryMapper() {
157 return primaryMapper;
158 }
159
160
161
162
163
164 public EquationsMapper[] getSecondaryMappers() {
165 final EquationsMapper[] mappers = new EquationsMapper[components.size()];
166 for (int i = 0; i < mappers.length; ++i) {
167 mappers[i] = components.get(i).mapper;
168 }
169 return mappers;
170 }
171
172
173
174
175 public void setTime(final double time) {
176 this.time = time;
177 }
178
179
180
181
182 public double getTime() {
183 return time;
184 }
185
186
187
188
189
190
191 public void setPrimaryState(final double[] primaryState) throws DimensionMismatchException {
192
193
194 if (primaryState.length != this.primaryState.length) {
195 throw new DimensionMismatchException(primaryState.length, this.primaryState.length);
196 }
197
198
199 System.arraycopy(primaryState, 0, this.primaryState, 0, primaryState.length);
200 }
201
202
203
204
205 public double[] getPrimaryState() {
206 return primaryState.clone();
207 }
208
209
210
211
212 public double[] getPrimaryStateDot() {
213 return primaryStateDot.clone();
214 }
215
216
217
218
219
220
221
222
223 public void setSecondaryState(final int index, final double[] secondaryState)
224 throws DimensionMismatchException {
225
226
227 double[] localArray = components.get(index).state;
228
229
230 if (secondaryState.length != localArray.length) {
231 throw new DimensionMismatchException(secondaryState.length, localArray.length);
232 }
233
234
235 System.arraycopy(secondaryState, 0, localArray, 0, secondaryState.length);
236 }
237
238
239
240
241
242
243 public double[] getSecondaryState(final int index) {
244 return components.get(index).state.clone();
245 }
246
247
248
249
250
251
252 public double[] getSecondaryStateDot(final int index) {
253 return components.get(index).stateDot.clone();
254 }
255
256
257
258
259
260
261 public void setCompleteState(final double[] completeState)
262 throws DimensionMismatchException {
263
264
265 if (completeState.length != getTotalDimension()) {
266 throw new DimensionMismatchException(completeState.length, getTotalDimension());
267 }
268
269
270 primaryMapper.extractEquationData(completeState, primaryState);
271 for (final SecondaryComponent component : components) {
272 component.mapper.extractEquationData(completeState, component.state);
273 }
274 }
275
276
277
278
279
280
281 public double[] getCompleteState() throws DimensionMismatchException {
282
283
284 double[] completeState = new double[getTotalDimension()];
285
286
287 primaryMapper.insertEquationData(primaryState, completeState);
288 for (final SecondaryComponent component : components) {
289 component.mapper.insertEquationData(component.state, completeState);
290 }
291
292 return completeState;
293 }
294
295
296 private static final class SecondaryComponent {
297
298
299 private final SecondaryEquations equation;
300
301
302 private final EquationsMapper mapper;
303
304
305 private final double[] state;
306
307
308 private final double[] stateDot;
309
310
311
312
313
314 SecondaryComponent(final SecondaryEquations equation, final int firstIndex) {
315 final int n = equation.getDimension();
316 this.equation = equation;
317 mapper = new EquationsMapper(firstIndex, n);
318 state = new double[n];
319 stateDot = new double[n];
320 }
321 }
322 }