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.ClassicalRungeKuttaIntegrator;
23
24 /**
25 * This class implements a step interpolator for the classical 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> + θ h) = y (t<sub>n</sub>)
34 * + θ (h/6) [ (6 - 9 θ + 4 θ<sup>2</sup>) y'<sub>1</sub>
35 * + ( 6 θ - 4 θ<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
36 * + ( -3 θ + 4 θ<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> + θ h) = y (t<sub>n</sub> + h)
41 * + (1 - θ) (h/6) [ (-4 θ^2 + 5 θ - 1) y'<sub>1</sub>
42 * +(4 θ^2 - 2 θ - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
43 * -(4 θ^2 + θ + 1) y'<sub>4</sub>
44 * ]
45 * </li>
46 * </ul>
47 *
48 * <p>where θ belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
49 * evaluations of the derivatives already computed during the
50 * step.</p>
51 *
52 * @see ClassicalRungeKuttaIntegrator
53 */
54
55 public class ClassicalRungeKuttaStateInterpolator 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 public 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, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 protected ClassicalRungeKuttaStateInterpolator create(final boolean newForward, final double[][] newYDotK,
82 final ODEStateAndDerivative newGlobalPreviousState,
83 final ODEStateAndDerivative newGlobalCurrentState,
84 final ODEStateAndDerivative newSoftPreviousState,
85 final ODEStateAndDerivative newSoftCurrentState,
86 final EquationsMapper newMapper) {
87 return new ClassicalRungeKuttaStateInterpolator(newForward, newYDotK,
88 newGlobalPreviousState, newGlobalCurrentState,
89 newSoftPreviousState, newSoftCurrentState,
90 newMapper);
91 }
92
93 /** {@inheritDoc} */
94 @Override
95 protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
96 final double time, final double theta,
97 final double thetaH, final double oneMinusThetaH) {
98
99 final double oneMinusTheta = 1.0 - theta;
100 final double oneMinus2Theta = 1.0 - theta * 2.0;
101 final double coeffDot1 = oneMinusTheta * oneMinus2Theta;
102 final double coeffDot23 = theta * oneMinusTheta * 2;
103 final double coeffDot4 = -theta * oneMinus2Theta;
104 final double[] interpolatedState;
105 final double[] interpolatedDerivatives;
106
107 if (getGlobalPreviousState() != null && theta <= 0.5) {
108 final double fourTheta2 = theta * theta * 4;
109 final double s = thetaH / 6.0;
110 final double coeff1 = s * (fourTheta2 - theta * 9 + 6);
111 final double coeff23 = s * (theta * 6 - fourTheta2);
112 final double coeff4 = s * (fourTheta2 - theta * 3);
113 interpolatedState = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
114 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
115 } else {
116 final double fourTheta = theta * 4;
117 final double s = oneMinusThetaH / 6.0;
118 final double coeff1 = s * (theta * (-fourTheta + 5) - 1);
119 final double coeff23 = s * (theta * ( fourTheta - 2) - 2);
120 final double coeff4 = s * (theta * (-fourTheta - 1) - 1);
121 interpolatedState = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
122 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
123 }
124
125 return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
126
127 }
128
129 }