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) Dormand-Prince integrator for Ordinary
35   * 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. However, since this
42   * is an <i>fsal</i>, the last evaluation of one step is the same as
43   * the first evaluation of the next step and hence can be avoided. So
44   * the cost is really 6 functions evaluations per step.</p>
45   *
46   * <p>This method has been published (whithout the continuous output
47   * that was added by Shampine in 1986) in the following article :</p>
48   * <pre>
49   *  A family of embedded Runge-Kutta formulae
50   *  J. R. Dormand and P. J. Prince
51   *  Journal of Computational and Applied Mathematics
52   *  volume 6, no 1, 1980, pp. 19-26
53   * </pre>
54   *
55   * @param <T> the type of the field elements
56   */
57  
58  public class DormandPrince54FieldIntegrator<T extends CalculusFieldElement<T>>
59      extends EmbeddedRungeKuttaFieldIntegrator<T> {
60  
61      /** Name of integration scheme. */
62      public static final String METHOD_NAME = DormandPrince54Integrator.METHOD_NAME;
63  
64      /** Simple constructor.
65       * Build a fifth order Dormand-Prince integrator with the given step bounds
66       * @param field field to which the time and state vector elements belong
67       * @param minStep minimal step (sign is irrelevant, regardless of
68       * integration direction, forward or backward), the last step can
69       * be smaller than this
70       * @param maxStep maximal step (sign is irrelevant, regardless of
71       * integration direction, forward or backward), the last step can
72       * be smaller than this
73       * @param scalAbsoluteTolerance allowed absolute error
74       * @param scalRelativeTolerance allowed relative error
75       */
76      public DormandPrince54FieldIntegrator(final Field<T> field,
77                                            final double minStep, final double maxStep,
78                                            final double scalAbsoluteTolerance,
79                                            final double scalRelativeTolerance) {
80          super(field, METHOD_NAME, 6,
81                minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
82      }
83  
84      /** Simple constructor.
85       * Build a fifth order Dormand-Prince integrator with the given step bounds
86       * @param field field to which the time and state vector elements belong
87       * @param minStep minimal step (sign is irrelevant, regardless of
88       * integration direction, forward or backward), the last step can
89       * be smaller than this
90       * @param maxStep maximal step (sign is irrelevant, regardless of
91       * integration direction, forward or backward), the last step can
92       * be smaller than this
93       * @param vecAbsoluteTolerance allowed absolute error
94       * @param vecRelativeTolerance allowed relative error
95       */
96      public DormandPrince54FieldIntegrator(final Field<T> field,
97                                            final double minStep, final double maxStep,
98                                            final double[] vecAbsoluteTolerance,
99                                            final double[] vecRelativeTolerance) {
100         super(field, DormandPrince54Integrator.METHOD_NAME, 6,
101               minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public T[] getC() {
107         final T[] c = MathArrays.buildArray(getField(), 6);
108         c[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1,  5);
109         c[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 3, 10);
110         c[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),4,  5);
111         c[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),8,  9);
112         c[4] = getField().getOne();
113         c[5] = getField().getOne();
114         return c;
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public T[][] getA() {
120         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
121         for (int i = 0; i < a.length; ++i) {
122             a[i] = MathArrays.buildArray(getField(), i + 1);
123         }
124         a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     5);
125         a[1][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      3,    40);
126         a[1][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      9,    40);
127         a[2][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     44,    45);
128         a[2][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -56,    15);
129         a[2][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     32,     9);
130         a[3][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  19372,  6561);
131         a[3][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), -25360,  2187);
132         a[3][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  64448,  6561);
133         a[3][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   -212,   729);
134         a[4][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   9017,  3168);
135         a[4][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   -355,    33);
136         a[4][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  46732,  5247);
137         a[4][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     49,   176);
138         a[4][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -5103, 18656);
139         a[5][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     35,   384);
140         a[5][1] = getField().getZero();
141         a[5][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    500,  1113);
142         a[5][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    125,   192);
143         a[5][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -2187,  6784);
144         a[5][5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     11,    84);
145         return a;
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public T[] getB() {
151         final T[] b = MathArrays.buildArray(getField(), 7);
152         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    35,   384);
153         b[1] = getField().getZero();
154         b[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   500, 1113);
155         b[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   125,  192);
156         b[4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), -2187, 6784);
157         b[5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    11,   84);
158         b[6] = getField().getZero();
159         return b;
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     protected DormandPrince54FieldStateInterpolator<T>
165         createInterpolator(final boolean forward, T[][] yDotK,
166                            final FieldODEStateAndDerivative<T> globalPreviousState,
167                            final FieldODEStateAndDerivative<T> globalCurrentState, final FieldEquationsMapper<T> mapper) {
168         return new DormandPrince54FieldStateInterpolator<T>(getField(), forward, yDotK,
169                                                            globalPreviousState, globalCurrentState,
170                                                            globalPreviousState, globalCurrentState,
171                                                            mapper);
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public int getOrder() {
177         return 5;
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     protected double estimateError(final T[][] yDotK, final T[] y0, final T[] y1, final T h) {
183 
184         final StepsizeHelper helper = getStepSizeHelper();
185         double error = 0;
186 
187         for (int j = 0; j < helper.getMainSetDimension(); ++j) {
188             final double errSum = DormandPrince54Integrator.E1 * yDotK[0][j].getReal() +  DormandPrince54Integrator.E3 * yDotK[2][j].getReal() +
189                                   DormandPrince54Integrator.E4 * yDotK[3][j].getReal() +  DormandPrince54Integrator.E5 * yDotK[4][j].getReal() +
190                                   DormandPrince54Integrator.E6 * yDotK[5][j].getReal() +  DormandPrince54Integrator.E7 * yDotK[6][j].getReal();
191             final double tol = helper.getTolerance(j, FastMath.max(FastMath.abs(y0[j].getReal()), FastMath.abs(y1[j].getReal())));
192             final double ratio  = h.getReal() * errSum / tol;
193             error += ratio * ratio;
194         }
195 
196         return FastMath.sqrt(error / helper.getMainSetDimension());
197 
198     }
199 
200 }