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