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