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 classical 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/6) [  (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
34   *                                     + (    6 &theta; - 4 &theta;<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
35   *                                     + (   -3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
36   *                                    ]
37   *   </li>
38   *   <li>Using reference point at step end:<br>
39   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
40   *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
41   *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
42   *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
43   *                                        ]
44   *   </li>
45   * </ul>
46   *
47   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
48   * evaluations of the derivatives already computed during the
49   * step.</p>
50   *
51   * @see ClassicalRungeKuttaIntegrator
52   */
53  
54  class ClassicalRungeKuttaStateInterpolator
55      extends RungeKuttaStateInterpolator {
56  
57      /** Serializable version identifier. */
58      private static final long serialVersionUID = 20160328L;
59  
60      /** Simple constructor.
61       * @param forward integration direction indicator
62       * @param yDotK slopes at the intermediate points
63       * @param globalPreviousState start of the global step
64       * @param globalCurrentState end of the global step
65       * @param softPreviousState start of the restricted step
66       * @param softCurrentState end of the restricted step
67       * @param mapper equations mapper for the all equations
68       */
69      ClassicalRungeKuttaStateInterpolator(final boolean forward,
70                                           final double[][] yDotK,
71                                           final ODEStateAndDerivative globalPreviousState,
72                                           final ODEStateAndDerivative globalCurrentState,
73                                           final ODEStateAndDerivative softPreviousState,
74                                           final ODEStateAndDerivative softCurrentState,
75                                           final EquationsMapper mapper) {
76          super(forward, yDotK,
77                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
78                mapper);
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      protected ClassicalRungeKuttaStateInterpolator 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 ClassicalRungeKuttaStateInterpolator(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 oneMinusTheta             = 1.0 - theta;
102         final double oneMinus2Theta            = 1.0 - theta * 2.0;
103         final double coeffDot1                 = oneMinusTheta * oneMinus2Theta;
104         final double coeffDot23                =  theta * oneMinusTheta * 2;
105         final double coeffDot4                 = -theta * oneMinus2Theta;
106         final double[] interpolatedState;
107         final double[] interpolatedDerivatives;
108 
109         if (getGlobalPreviousState() != null && theta <= 0.5) {
110             final double fourTheta2      = theta * theta * 4;
111             final double s               = thetaH / 6.0;
112             final double coeff1          = s * (fourTheta2 - theta * 9 + 6);
113             final double coeff23         = s * (theta * 6 - fourTheta2);
114             final double coeff4          = s * (fourTheta2 - theta * 3);
115             interpolatedState       = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
116             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
117         } else {
118             final double fourTheta       = theta * 4;
119             final double s               = oneMinusThetaH / 6.0;
120             final double coeff1          = s * (theta * (-fourTheta + 5) - 1);
121             final double coeff23         = s * (theta * ( fourTheta - 2) - 2);
122             final double coeff4          = s * (theta * (-fourTheta - 1) - 1);
123             interpolatedState       = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
124             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
125         }
126 
127         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
128 
129     }
130 
131 }