1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.hipparchus.ode.nonstiff;
19
20 import org.hipparchus.CalculusFieldElement;
21 import org.hipparchus.exception.LocalizedCoreFormats;
22 import org.hipparchus.exception.MathIllegalArgumentException;
23 import org.hipparchus.ode.LocalizedODEFormats;
24 import org.hipparchus.util.FastMath;
25
26
27
28
29
30 public class StepsizeHelper {
31
32
33 private double scalAbsoluteTolerance;
34
35
36 private double scalRelativeTolerance;
37
38
39 private double[] vecAbsoluteTolerance;
40
41
42 private double[] vecRelativeTolerance;
43
44
45 private int mainSetDimension;
46
47
48 private double initialStep;
49
50
51 private double minStep;
52
53
54 private double maxStep;
55
56
57
58
59
60
61
62
63
64
65
66 public StepsizeHelper(final double minStep, final double maxStep,
67 final double scalAbsoluteTolerance,
68 final double scalRelativeTolerance) {
69 this.minStep = FastMath.abs(minStep);
70 this.maxStep = FastMath.abs(maxStep);
71 this.initialStep = -1;
72
73 this.scalAbsoluteTolerance = scalAbsoluteTolerance;
74 this.scalRelativeTolerance = scalRelativeTolerance;
75 this.vecAbsoluteTolerance = null;
76 this.vecRelativeTolerance = null;
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public StepsizeHelper(final double minStep, final double maxStep,
90 final double[] vecAbsoluteTolerance,
91 final double[] vecRelativeTolerance) {
92
93 this.minStep = FastMath.abs(minStep);
94 this.maxStep = FastMath.abs(maxStep);
95 this.initialStep = -1;
96
97 this.scalAbsoluteTolerance = 0;
98 this.scalRelativeTolerance = 0;
99 this.vecAbsoluteTolerance = vecAbsoluteTolerance.clone();
100 this.vecRelativeTolerance = vecRelativeTolerance.clone();
101
102 }
103
104
105
106
107
108
109 protected void setMainSetDimension(final int mainSetDimension) throws MathIllegalArgumentException {
110 this.mainSetDimension = mainSetDimension;
111
112 if (vecAbsoluteTolerance != null && vecAbsoluteTolerance.length != mainSetDimension) {
113 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
114 mainSetDimension, vecAbsoluteTolerance.length);
115 }
116
117 if (vecRelativeTolerance != null && vecRelativeTolerance.length != mainSetDimension) {
118 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
119 mainSetDimension, vecRelativeTolerance.length);
120 }
121 }
122
123
124
125
126 public int getMainSetDimension() {
127 return mainSetDimension;
128 }
129
130
131
132
133
134 public double getRelativeTolerance(final int i) {
135 return vecAbsoluteTolerance == null ? scalRelativeTolerance : vecRelativeTolerance[i];
136 }
137
138
139
140
141
142
143 public double getTolerance(final int i, final double scale) {
144 return vecAbsoluteTolerance == null ?
145 scalAbsoluteTolerance + scalRelativeTolerance * scale :
146 vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * scale;
147 }
148
149
150
151
152
153
154
155 public <T extends CalculusFieldElement<T>> T getTolerance(final int i, final T scale) {
156 return vecAbsoluteTolerance == null ?
157 scale.multiply(scalRelativeTolerance).add(scalAbsoluteTolerance) :
158 scale.multiply(vecRelativeTolerance[i]).add(vecAbsoluteTolerance[i]);
159 }
160
161
162
163
164
165
166
167
168
169
170 public double filterStep(final double h, final boolean forward, final boolean acceptSmall)
171 throws MathIllegalArgumentException {
172
173 double filteredH = h;
174 if (FastMath.abs(h) < minStep) {
175 if (acceptSmall) {
176 filteredH = forward ? minStep : -minStep;
177 } else {
178 throw new MathIllegalArgumentException(LocalizedODEFormats.MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION,
179 FastMath.abs(h), minStep, true);
180 }
181 }
182
183 if (filteredH > maxStep) {
184 filteredH = maxStep;
185 } else if (filteredH < -maxStep) {
186 filteredH = -maxStep;
187 }
188
189 return filteredH;
190
191 }
192
193
194
195
196
197
198
199
200
201
202
203 public <T extends CalculusFieldElement<T>> T filterStep(final T h, final boolean forward, final boolean acceptSmall)
204 throws MathIllegalArgumentException {
205
206 T filteredH = h;
207 if (h.abs().subtract(minStep).getReal() < 0) {
208 if (acceptSmall) {
209 filteredH = h.getField().getZero().add(forward ? minStep : -minStep);
210 } else {
211 throw new MathIllegalArgumentException(LocalizedODEFormats.MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION,
212 FastMath.abs(h.getReal()), minStep, true);
213 }
214 }
215
216 if (filteredH.subtract(maxStep).getReal() > 0) {
217 filteredH = h.getField().getZero().newInstance(maxStep);
218 } else if (filteredH.add(maxStep).getReal() < 0) {
219 filteredH = h.getField().getZero().newInstance(-maxStep);
220 }
221
222 return filteredH;
223
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237 public void setInitialStepSize(final double initialStepSize) {
238 if ((initialStepSize < minStep) || (initialStepSize > maxStep)) {
239 initialStep = -1.0;
240 } else {
241 initialStep = initialStepSize;
242 }
243 }
244
245
246
247
248 public double getInitialStep() {
249 return initialStep;
250 }
251
252
253
254
255 public double getMinStep() {
256 return minStep;
257 }
258
259
260
261
262 public double getMaxStep() {
263 return maxStep;
264 }
265
266
267
268
269 public double getDummyStepsize() {
270 return FastMath.sqrt(minStep * maxStep);
271 }
272
273 }