View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * This class implements the 5(4) Higham and Hall integrator for
29   * Ordinary Differential Equations.
30   *
31   * <p>This integrator is an embedded Runge-Kutta integrator
32   * of order 5(4) used in local extrapolation mode (i.e. the solution
33   * is computed using the high order formula) with stepsize control
34   * (and automatic step initialization) and continuous output. This
35   * method uses 7 functions evaluations per step.</p>
36   *
37   * @param <T> the type of the field elements
38   * @since 3.6
39   */
40  
41  public class HighamHall54FieldIntegrator<T extends RealFieldElement<T>>
42      extends EmbeddedRungeKuttaFieldIntegrator<T> {
43  
44      /** Integrator method name. */
45      private static final String METHOD_NAME = "Higham-Hall 5(4)";
46  
47      /** Error weights Butcher array. */
48      private final T[] e ;
49  
50      /** Simple constructor.
51       * Build a fifth order Higham and Hall integrator with the given step bounds
52       * @param field field to which the time and state vector elements belong
53       * @param minStep minimal step (sign is irrelevant, regardless of
54       * integration direction, forward or backward), the last step can
55       * be smaller than this
56       * @param maxStep maximal step (sign is irrelevant, regardless of
57       * integration direction, forward or backward), the last step can
58       * be smaller than this
59       * @param scalAbsoluteTolerance allowed absolute error
60       * @param scalRelativeTolerance allowed relative error
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      /** Simple constructor.
79       * Build a fifth order Higham and Hall integrator with the given step bounds
80       * @param field field to which the time and state vector elements belong
81       * @param minStep minimal step (sign is irrelevant, regardless of
82       * integration direction, forward or backward), the last step can
83       * be smaller than this
84       * @param maxStep maximal step (sign is irrelevant, regardless of
85       * integration direction, forward or backward), the last step can
86       * be smaller than this
87       * @param vecAbsoluteTolerance allowed absolute error
88       * @param vecRelativeTolerance allowed relative error
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
177     @Override
178     public int getOrder() {
179         return 5;
180     }
181 
182     /** {@inheritDoc} */
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 }