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