1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.apache.commons.math4.legacy.ode.nonstiff;
19  
20  import org.apache.commons.math4.legacy.core.Field;
21  import org.apache.commons.math4.legacy.core.RealFieldElement;
22  import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23  import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24  import org.apache.commons.math4.legacy.core.MathArrays;
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  public class HighamHall54FieldIntegrator<T extends RealFieldElement<T>>
42      extends EmbeddedRungeKuttaFieldIntegrator<T> {
43  
44      
45      private static final String METHOD_NAME = "Higham-Hall 5(4)";
46  
47      
48      private final T[] e ;
49  
50      
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62      public HighamHall54FieldIntegrator(final Field<T> field,
63                                         final double minStep, final double maxStep,
64                                         final double scalAbsoluteTolerance,
65                                         final double scalRelativeTolerance) {
66          super(field, METHOD_NAME, -1,
67                minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
68          e = MathArrays.buildArray(field, 7);
69          e[0] = fraction(-1,  20);
70          e[1] = field.getZero();
71          e[2] = fraction(81, 160);
72          e[3] = fraction(-6,   5);
73          e[4] = fraction(25,  32);
74          e[5] = fraction( 1,  16);
75          e[6] = fraction(-1,  10);
76      }
77  
78      
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90      public HighamHall54FieldIntegrator(final Field<T> field,
91                                         final double minStep, final double maxStep,
92                                         final double[] vecAbsoluteTolerance,
93                                         final double[] vecRelativeTolerance) {
94          super(field, METHOD_NAME, -1,
95                minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
96          e = MathArrays.buildArray(field, 7);
97          e[0] = fraction(-1,  20);
98          e[1] = field.getZero();
99          e[2] = fraction(81, 160);
100         e[3] = fraction(-6,   5);
101         e[4] = fraction(25,  32);
102         e[5] = fraction( 1,  16);
103         e[6] = fraction(-1,  10);
104     }
105 
106     
107     @Override
108     public T[] getC() {
109         final T[] c = MathArrays.buildArray(getField(), 6);
110         c[0] = fraction(2, 9);
111         c[1] = fraction(1, 3);
112         c[2] = fraction(1, 2);
113         c[3] = fraction(3, 5);
114         c[4] = getField().getOne();
115         c[5] = getField().getOne();
116         return c;
117     }
118 
119     
120     @Override
121     public T[][] getA() {
122         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
123         for (int i = 0; i < a.length; ++i) {
124             a[i] = MathArrays.buildArray(getField(), i + 1);
125         }
126         a[0][0] = fraction(     2,     9);
127         a[1][0] = fraction(     1,    12);
128         a[1][1] = fraction(     1,     4);
129         a[2][0] = fraction(     1,     8);
130         a[2][1] = getField().getZero();
131         a[2][2] = fraction(     3,     8);
132         a[3][0] = fraction(    91,   500);
133         a[3][1] = fraction(   -27,   100);
134         a[3][2] = fraction(    78,   125);
135         a[3][3] = fraction(     8,   125);
136         a[4][0] = fraction(   -11,    20);
137         a[4][1] = fraction(    27,    20);
138         a[4][2] = fraction(    12,     5);
139         a[4][3] = fraction(   -36,     5);
140         a[4][4] = fraction(     5,     1);
141         a[5][0] = fraction(     1,    12);
142         a[5][1] = getField().getZero();
143         a[5][2] = fraction(    27,    32);
144         a[5][3] = fraction(    -4,     3);
145         a[5][4] = fraction(   125,    96);
146         a[5][5] = fraction(     5,    48);
147         return a;
148     }
149 
150     
151     @Override
152     public T[] getB() {
153         final T[] b = MathArrays.buildArray(getField(), 7);
154         b[0] = fraction(  1, 12);
155         b[1] = getField().getZero();
156         b[2] = fraction( 27, 32);
157         b[3] = fraction( -4,  3);
158         b[4] = fraction(125, 96);
159         b[5] = fraction(  5, 48);
160         b[6] = getField().getZero();
161         return b;
162     }
163 
164     
165     @Override
166     protected HighamHall54FieldStepInterpolator<T>
167         createInterpolator(final boolean forward, T[][] yDotK,
168                            final FieldODEStateAndDerivative<T> globalPreviousState,
169                            final FieldODEStateAndDerivative<T> globalCurrentState, final FieldEquationsMapper<T> mapper) {
170         return new HighamHall54FieldStepInterpolator<>(getField(), forward, yDotK,
171                                                         globalPreviousState, globalCurrentState,
172                                                         globalPreviousState, globalCurrentState,
173                                                         mapper);
174     }
175 
176     
177     @Override
178     public int getOrder() {
179         return 5;
180     }
181 
182     
183     @Override
184     protected T estimateError(final T[][] yDotK, final T[] y0, final T[] y1, final T h) {
185 
186         T error = getField().getZero();
187 
188         for (int j = 0; j < mainSetDimension; ++j) {
189             T errSum = yDotK[0][j].multiply(e[0]);
190             for (int l = 1; l < e.length; ++l) {
191                 errSum = errSum.add(yDotK[l][j].multiply(e[l]));
192             }
193 
194             final T yScale = RealFieldElement.max(y0[j].abs(), y1[j].abs());
195             final T tol    = (vecAbsoluteTolerance == null) ?
196                              yScale.multiply(scalRelativeTolerance).add(scalAbsoluteTolerance) :
197                              yScale.multiply(vecRelativeTolerance[j]).add(vecAbsoluteTolerance[j]);
198             final T ratio  = h.multiply(errSum).divide(tol);
199             error = error.add(ratio.multiply(ratio));
200         }
201 
202         return error.divide(mainSetDimension).sqrt();
203     }
204 }