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