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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode.nonstiff;
24  
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.Field;
27  import org.hipparchus.ode.FieldEquationsMapper;
28  import org.hipparchus.ode.FieldODEStateAndDerivative;
29  import org.hipparchus.util.FastMath;
30  import org.hipparchus.util.MathArrays;
31  
32  
33  /**
34   * This class implements the 5(4) Higham and Hall integrator for
35   * Ordinary Differential Equations.
36   *
37   * <p>This integrator is an embedded Runge-Kutta integrator
38   * of order 5(4) used in local extrapolation mode (i.e. the solution
39   * is computed using the high order formula) with stepsize control
40   * (and automatic step initialization) and continuous output. This
41   * method uses 7 functions evaluations per step.</p>
42   *
43   * @param <T> the type of the field elements
44   */
45  
46  public class HighamHall54FieldIntegrator<T extends CalculusFieldElement<T>>
47      extends EmbeddedRungeKuttaFieldIntegrator<T> {
48  
49      /** Name of integration scheme. */
50      public static final String METHOD_NAME = HighamHall54Integrator.METHOD_NAME;
51  
52      /** Simple constructor.
53       * Build a fifth order Higham and Hall integrator with the given step bounds
54       * @param field field to which the time and state vector elements belong
55       * @param minStep minimal step (sign is irrelevant, regardless of
56       * integration direction, forward or backward), the last step can
57       * be smaller than this
58       * @param maxStep maximal step (sign is irrelevant, regardless of
59       * integration direction, forward or backward), the last step can
60       * be smaller than this
61       * @param scalAbsoluteTolerance allowed absolute error
62       * @param scalRelativeTolerance allowed relative error
63       */
64      public HighamHall54FieldIntegrator(final Field<T> field,
65                                         final double minStep, final double maxStep,
66                                         final double scalAbsoluteTolerance,
67                                         final double scalRelativeTolerance) {
68          super(field, METHOD_NAME, -1,
69                minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
70      }
71  
72      /** Simple constructor.
73       * Build a fifth order Higham and Hall integrator with the given step bounds
74       * @param field field to which the time and state vector elements belong
75       * @param minStep minimal step (sign is irrelevant, regardless of
76       * integration direction, forward or backward), the last step can
77       * be smaller than this
78       * @param maxStep maximal step (sign is irrelevant, regardless of
79       * integration direction, forward or backward), the last step can
80       * be smaller than this
81       * @param vecAbsoluteTolerance allowed absolute error
82       * @param vecRelativeTolerance allowed relative error
83       */
84      public HighamHall54FieldIntegrator(final Field<T> field,
85                                         final double minStep, final double maxStep,
86                                         final double[] vecAbsoluteTolerance,
87                                         final double[] vecRelativeTolerance) {
88          super(field, HighamHall54Integrator.METHOD_NAME, -1,
89                minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public T[] getC() {
95          final T[] c = MathArrays.buildArray(getField(), 6);
96          c[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 2, 9);
97          c[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 3);
98          c[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 2);
99          c[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 3, 5);
100         c[4] = getField().getOne();
101         c[5] = getField().getOne();
102         return c;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public T[][] getA() {
108         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
109         for (int i = 0; i < a.length; ++i) {
110             a[i] = MathArrays.buildArray(getField(), i + 1);
111         }
112         a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      2,     9);
113         a[1][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,    12);
114         a[1][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     4);
115         a[2][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     8);
116         a[2][1] = getField().getZero();
117         a[2][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     3,     8);
118         a[3][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     91,   500);
119         a[3][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -27,   100);
120         a[3][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     78,   125);
121         a[3][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      8,   125);
122         a[4][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -11,    20);
123         a[4][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     27,    20);
124         a[4][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     12,     5);
125         a[4][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -36,     5);
126         a[4][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      5,     1);
127         a[5][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,    12);
128         a[5][1] = getField().getZero();
129         a[5][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     27,    32);
130         a[5][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     -4,     3);
131         a[5][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    125,    96);
132         a[5][5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      5,    48);
133         return a;
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public T[] getB() {
139         final T[] b = MathArrays.buildArray(getField(), 7);
140         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   1, 12);
141         b[1] = getField().getZero();
142         b[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  27, 32);
143         b[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -4,  3);
144         b[4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 125, 96);
145         b[5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   5, 48);
146         b[6] = getField().getZero();
147         return b;
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     protected HighamHall54FieldStateInterpolator<T>
153         createInterpolator(final boolean forward, T[][] yDotK,
154                            final FieldODEStateAndDerivative<T> globalPreviousState,
155                            final FieldODEStateAndDerivative<T> globalCurrentState, final FieldEquationsMapper<T> mapper) {
156         return new HighamHall54FieldStateInterpolator<T>(getField(), forward, yDotK,
157                                                         globalPreviousState, globalCurrentState,
158                                                         globalPreviousState, globalCurrentState,
159                                                         mapper);
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public int getOrder() {
165         return 5;
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     protected double estimateError(final T[][] yDotK, final T[] y0, final T[] y1, final T h) {
171 
172         final StepsizeHelper helper = getStepSizeHelper();
173         double error = 0;
174 
175         for (int j = 0; j < helper.getMainSetDimension(); ++j) {
176             double errSum = HighamHall54Integrator.STATIC_E[0] * yDotK[0][j].getReal();
177             for (int l = 1; l < HighamHall54Integrator.STATIC_E.length; ++l) {
178                 errSum += HighamHall54Integrator.STATIC_E[l] * yDotK[l][j].getReal();
179             }
180             final double tol   = helper.getTolerance(j, FastMath.max(FastMath.abs(y0[j].getReal()), FastMath.abs(y1[j].getReal())));
181             final double ratio = h.getReal() * errSum / tol;
182             error += ratio * ratio;
183 
184         }
185 
186         return FastMath.sqrt(error / helper.getMainSetDimension());
187 
188     }
189 
190 }