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 * http://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.apache.commons.math4.legacy.ode.nonstiff;
19
20 import org.apache.commons.math4.legacy.core.Field;
21 import org.apache.commons.math4.legacy.core.RealFieldElement;
22 import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23 import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24
25 /**
26 * This class implements a step interpolator for the classical 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 :
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>) (y'<sub>2</sub> + y'<sub>3</sub>)
37 * + ( -3 θ + 4 θ<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> + θ h) = y (t<sub>n</sub> + h)
42 * + (1 - θ) (h/6) [ (-4 θ^2 + 5 θ - 1) y'<sub>1</sub>
43 * +(4 θ^2 - 2 θ - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
44 * -(4 θ^2 + θ + 1) y'<sub>4</sub>
45 * ]
46 * </li>
47 * </ul>
48 *
49 * where θ belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
50 * evaluations of the derivatives already computed during the
51 * step.</p>
52 *
53 * @see ClassicalRungeKuttaFieldIntegrator
54 * @param <T> the type of the field elements
55 * @since 3.6
56 */
57
58 class ClassicalRungeKuttaFieldStepInterpolator<T extends RealFieldElement<T>>
59 extends RungeKuttaFieldStepInterpolator<T> {
60
61 /** Simple constructor.
62 * @param field field to which the time and state vector elements belong
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 ClassicalRungeKuttaFieldStepInterpolator(final Field<T> field, final boolean forward,
72 final T[][] yDotK,
73 final FieldODEStateAndDerivative<T> globalPreviousState,
74 final FieldODEStateAndDerivative<T> globalCurrentState,
75 final FieldODEStateAndDerivative<T> softPreviousState,
76 final FieldODEStateAndDerivative<T> softCurrentState,
77 final FieldEquationsMapper<T> mapper) {
78 super(field, forward, yDotK,
79 globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
80 mapper);
81 }
82
83 /** {@inheritDoc} */
84 @Override
85 protected ClassicalRungeKuttaFieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
86 final FieldODEStateAndDerivative<T> newGlobalPreviousState,
87 final FieldODEStateAndDerivative<T> newGlobalCurrentState,
88 final FieldODEStateAndDerivative<T> newSoftPreviousState,
89 final FieldODEStateAndDerivative<T> newSoftCurrentState,
90 final FieldEquationsMapper<T> newMapper) {
91 return new ClassicalRungeKuttaFieldStepInterpolator<>(newField, newForward, newYDotK,
92 newGlobalPreviousState, newGlobalCurrentState,
93 newSoftPreviousState, newSoftCurrentState,
94 newMapper);
95 }
96
97 /** {@inheritDoc} */
98 @SuppressWarnings("unchecked")
99 @Override
100 protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
101 final T time, final T theta,
102 final T thetaH, final T oneMinusThetaH) {
103
104 final T one = time.getField().getOne();
105 final T oneMinusTheta = one.subtract(theta);
106 final T oneMinus2Theta = one.subtract(theta.multiply(2));
107 final T coeffDot1 = oneMinusTheta.multiply(oneMinus2Theta);
108 final T coeffDot23 = theta.multiply(oneMinusTheta).multiply(2);
109 final T coeffDot4 = theta.multiply(oneMinus2Theta).negate();
110 final T[] interpolatedState;
111 final T[] interpolatedDerivatives;
112
113 if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
114 final T fourTheta2 = theta.multiply(theta).multiply(4);
115 final T s = thetaH.divide(6.0);
116 final T coeff1 = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
117 final T coeff23 = s.multiply(theta.multiply(6).subtract(fourTheta2));
118 final T coeff4 = s.multiply(fourTheta2.subtract(theta.multiply(3)));
119 interpolatedState = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
120 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
121 } else {
122 final T fourTheta = theta.multiply(4);
123 final T s = oneMinusThetaH.divide(6);
124 final T coeff1 = s.multiply(theta.multiply(fourTheta.negate().add(5)).subtract(1));
125 final T coeff23 = s.multiply(theta.multiply(fourTheta.subtract(2)).subtract(2));
126 final T coeff4 = s.multiply(theta.multiply(fourTheta.negate().subtract(1)).subtract(1));
127 interpolatedState = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
128 interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
129 }
130
131 return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
132 }
133 }