1 /*
2 * Licensed to the Apache Software Foundation (ASF) 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 ASF 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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.ode.nonstiff;
24
25 import org.hipparchus.CalculusFieldElement;
26 import org.hipparchus.Field;
27 import org.hipparchus.ode.FieldEquationsMapper;
28 import org.hipparchus.ode.FieldODEStateAndDerivative;
29
30 /**
31 * This class implements a step interpolator for the classical fourth
32 * order Runge-Kutta integrator.
33 *
34 * <p>This interpolator allows to compute dense output inside the last
35 * step computed. The interpolation equation is consistent with the
36 * integration scheme :</p>
37 * <ul>
38 * <li>Using reference point at step start:<br>
39 * y(t<sub>n</sub> + θ h) = y (t<sub>n</sub>)
40 * + θ (h/6) [ (6 - 9 θ + 4 θ<sup>2</sup>) y'<sub>1</sub>
41 * + ( 6 θ - 4 θ<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
42 * + ( -3 θ + 4 θ<sup>2</sup>) y'<sub>4</sub>
43 * ]
44 * </li>
45 * <li>Using reference point at step end:<br>
46 * y(t<sub>n</sub> + θ h) = y (t<sub>n</sub> + h)
47 * + (1 - θ) (h/6) [ (-4 θ^2 + 5 θ - 1) y'<sub>1</sub>
48 * +(4 θ^2 - 2 θ - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
49 * -(4 θ^2 + θ + 1) y'<sub>4</sub>
50 * ]
51 * </li>
52 * </ul>
53 *
54 * <p>where θ belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
55 * evaluations of the derivatives already computed during the
56 * step.</p>
57 *
58 * @see ClassicalRungeKuttaFieldIntegrator
59 * @param <T> the type of the field elements
60 */
61
62 class ClassicalRungeKuttaFieldStateInterpolator<T extends CalculusFieldElement<T>>
63 extends RungeKuttaFieldStateInterpolator<T> {
64
65 /** Simple constructor.
66 * @param field field to which the time and state vector elements belong
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 ClassicalRungeKuttaFieldStateInterpolator(final Field<T> field, final boolean forward,
76 final T[][] yDotK,
77 final FieldODEStateAndDerivative<T> globalPreviousState,
78 final FieldODEStateAndDerivative<T> globalCurrentState,
79 final FieldODEStateAndDerivative<T> softPreviousState,
80 final FieldODEStateAndDerivative<T> softCurrentState,
81 final FieldEquationsMapper<T> mapper) {
82 super(field, forward, yDotK,
83 globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
84 mapper);
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 protected ClassicalRungeKuttaFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
90 final FieldODEStateAndDerivative<T> newGlobalPreviousState,
91 final FieldODEStateAndDerivative<T> newGlobalCurrentState,
92 final FieldODEStateAndDerivative<T> newSoftPreviousState,
93 final FieldODEStateAndDerivative<T> newSoftCurrentState,
94 final FieldEquationsMapper<T> newMapper) {
95 return new ClassicalRungeKuttaFieldStateInterpolator<T>(newField, newForward, newYDotK,
96 newGlobalPreviousState, newGlobalCurrentState,
97 newSoftPreviousState, newSoftCurrentState,
98 newMapper);
99 }
100
101 /** {@inheritDoc} */
102 @SuppressWarnings("unchecked")
103 @Override
104 protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
105 final T time, final T theta,
106 final T thetaH, final T oneMinusThetaH) {
107
108 final T one = time.getField().getOne();
109 final T oneMinusTheta = one.subtract(theta);
110 final T oneMinus2Theta = one.subtract(theta.multiply(2));
111 final T coeffDot1 = oneMinusTheta.multiply(oneMinus2Theta);
112 final T coeffDot23 = theta.multiply(oneMinusTheta).multiply(2);
113 final T coeffDot4 = theta.multiply(oneMinus2Theta).negate();
114 final T[] interpolatedState;
115 final T[] interpolatedDerivatives;
116
117 if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
118 final T fourTheta2 = theta.multiply(theta).multiply(4);
119 final T s = thetaH.divide(6.0);
120 final T coeff1 = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
121 final T coeff23 = s.multiply(theta.multiply(6).subtract(fourTheta2));
122 final T coeff4 = s.multiply(fourTheta2.subtract(theta.multiply(3)));
123 interpolatedState = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
124 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
125 } else {
126 final T fourTheta = theta.multiply(4);
127 final T s = oneMinusThetaH.divide(6);
128 final T coeff1 = s.multiply(theta.multiply(fourTheta.negate().add(5)).subtract(1));
129 final T coeff23 = s.multiply(theta.multiply(fourTheta.subtract(2)).subtract(2));
130 final T coeff4 = s.multiply(theta.multiply(fourTheta.negate().subtract(1)).subtract(1));
131 interpolatedState = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
132 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
133 }
134
135 return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
136
137 }
138
139 }