View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.ode.nonstiff;
19  
20  import org.hipparchus.ode.EquationsMapper;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  
23  /**
24   * This class implements a step interpolator for the 3/8 fourth
25   * order Runge-Kutta integrator.
26   *
27   * <p>This interpolator allows to compute dense output inside the last
28   * step computed. The interpolation equation is consistent with the
29   * integration scheme :</p>
30   * <ul>
31   *   <li>Using reference point at step start:<br>
32   *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
33   *                      + &theta; (h/8) [ (8 - 15 &theta; +  8 &theta;<sup>2</sup>) y'<sub>1</sub>
34   *                                     +  3 * (15 &theta; - 12 &theta;<sup>2</sup>) y'<sub>2</sub>
35   *                                     +        3 &theta;                           y'<sub>3</sub>
36   *                                     +      (-3 &theta; +  4 &theta;<sup>2</sup>) y'<sub>4</sub>
37   *                                    ]
38   *   </li>
39   *   <li>Using reference point at step end:<br>
40   *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
41   *                      - (1 - &theta;) (h/8) [(1 - 7 &theta; + 8 &theta;<sup>2</sup>) y'<sub>1</sub>
42   *                                         + 3 (1 +   &theta; - 4 &theta;<sup>2</sup>) y'<sub>2</sub>
43   *                                         + 3 (1 +   &theta;)                         y'<sub>3</sub>
44   *                                         +   (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
45   *                                          ]
46   *   </li>
47   * </ul>
48   *
49   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
50   * evaluations of the derivatives already computed during the
51   * step.</p>
52   *
53   * @see ThreeEighthesIntegrator
54   */
55  
56  class ThreeEighthesStateInterpolator
57      extends RungeKuttaStateInterpolator {
58  
59      /** Serializable version identifier. */
60      private static final long serialVersionUID = 20160328L;
61  
62      /** Simple constructor.
63       * @param forward integration direction indicator
64       * @param yDotK slopes at the intermediate points
65       * @param globalPreviousState start of the global step
66       * @param globalCurrentState end of the global step
67       * @param softPreviousState start of the restricted step
68       * @param softCurrentState end of the restricted step
69       * @param mapper equations mapper for the all equations
70       */
71      ThreeEighthesStateInterpolator(final boolean forward,
72                                     final double[][] yDotK,
73                                     final ODEStateAndDerivative globalPreviousState,
74                                     final ODEStateAndDerivative globalCurrentState,
75                                     final ODEStateAndDerivative softPreviousState,
76                                     final ODEStateAndDerivative softCurrentState,
77                                     final EquationsMapper mapper) {
78          super(forward, yDotK,
79                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
80                mapper);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      protected ThreeEighthesStateInterpolator create(final boolean newForward, final double[][] newYDotK,
86                                                      final ODEStateAndDerivative newGlobalPreviousState,
87                                                      final ODEStateAndDerivative newGlobalCurrentState,
88                                                      final ODEStateAndDerivative newSoftPreviousState,
89                                                      final ODEStateAndDerivative newSoftCurrentState,
90                                                      final EquationsMapper newMapper) {
91          return new ThreeEighthesStateInterpolator(newForward, newYDotK,
92                                                    newGlobalPreviousState, newGlobalCurrentState,
93                                                    newSoftPreviousState, newSoftCurrentState,
94                                                    newMapper);
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
100                                                                            final double time, final double theta,
101                                                                            final double thetaH, final double oneMinusThetaH) {
102 
103         final double coeffDot3  = 0.75 * theta;
104         final double coeffDot1  = coeffDot3 * (4 * theta - 5) + 1;
105         final double coeffDot2  = coeffDot3 * (5 - 6 * theta);
106         final double coeffDot4  = coeffDot3 * (2 * theta - 1);
107         final double[] interpolatedState;
108         final double[] interpolatedDerivatives;
109 
110         if (getGlobalPreviousState() != null && theta <= 0.5) {
111             final double s          = thetaH / 8.0;
112             final double fourTheta2 = 4 * theta * theta;
113             final double coeff1     = s * (8 - 15 * theta + 2 * fourTheta2);
114             final double coeff2     = 3 * s * (5 * theta - fourTheta2);
115             final double coeff3     = 3 * s * theta;
116             final double coeff4     = s * (-3 * theta + fourTheta2);
117             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
118             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
119         } else {
120             final double s          = oneMinusThetaH / -8.0;
121             final double fourTheta2 = 4 * theta * theta;
122             final double coeff1     = s * (1 - 7 * theta + 2 * fourTheta2);
123             final double coeff2     = 3 * s * (1 + theta - fourTheta2);
124             final double coeff3     = 3 * s * (1 + theta);
125             final double coeff4     = s * (1 + theta + fourTheta2);
126             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
127             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
128         }
129 
130         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
131 
132     }
133 
134 }