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.EmbeddedRungeKuttaIntegrator;
23 import org.hipparchus.ode.nonstiff.FixedStepRungeKuttaIntegrator;
24 import org.hipparchus.ode.sampling.AbstractODEStateInterpolator;
25
26 /** This class represents an interpolator over the last step during an
27 * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
28 *
29 * @see FixedStepRungeKuttaIntegrator
30 * @see EmbeddedRungeKuttaIntegrator
31 *
32 */
33
34 public abstract class RungeKuttaStateInterpolator extends AbstractODEStateInterpolator {
35
36 /** Serializable UID. */
37 private static final long serialVersionUID = 20160328L;
38
39 /** Slopes at the intermediate points */
40 protected double[][] yDotK;
41
42 /** Simple constructor.
43 * @param forward integration direction indicator
44 * @param yDotK slopes at the intermediate points
45 * @param globalPreviousState start of the global step
46 * @param globalCurrentState end of the global step
47 * @param softPreviousState start of the restricted step
48 * @param softCurrentState end of the restricted step
49 * @param mapper equations mapper for the all equations
50 */
51 protected RungeKuttaStateInterpolator(final boolean forward,
52 final double[][] yDotK,
53 final ODEStateAndDerivative globalPreviousState,
54 final ODEStateAndDerivative globalCurrentState,
55 final ODEStateAndDerivative softPreviousState,
56 final ODEStateAndDerivative softCurrentState,
57 final EquationsMapper mapper) {
58 super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
59 this.yDotK = new double[yDotK.length][];
60 for (int i = 0; i < yDotK.length; ++i) {
61 this.yDotK[i] = yDotK[i].clone();
62 }
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 protected RungeKuttaStateInterpolator create(boolean newForward,
68 ODEStateAndDerivative newGlobalPreviousState,
69 ODEStateAndDerivative newGlobalCurrentState,
70 ODEStateAndDerivative newSoftPreviousState,
71 ODEStateAndDerivative newSoftCurrentState,
72 EquationsMapper newMapper) {
73 return create(newForward, yDotK, newGlobalPreviousState, newGlobalCurrentState,
74 newSoftPreviousState, newSoftCurrentState, newMapper);
75 }
76
77 /** Create a new instance.
78 * @param newForward integration direction indicator
79 * @param newYDotK slopes at the intermediate points
80 * @param newGlobalPreviousState start of the global step
81 * @param newGlobalCurrentState end of the global step
82 * @param newSoftPreviousState start of the restricted step
83 * @param newSoftCurrentState end of the restricted step
84 * @param newMapper equations mapper for the all equations
85 * @return a new instance
86 */
87 protected abstract RungeKuttaStateInterpolator create(boolean newForward, double[][] newYDotK,
88 ODEStateAndDerivative newGlobalPreviousState,
89 ODEStateAndDerivative newGlobalCurrentState,
90 ODEStateAndDerivative newSoftPreviousState,
91 ODEStateAndDerivative newSoftCurrentState,
92 EquationsMapper newMapper);
93
94 /** Compute a state by linear combination added to previous state.
95 * @param coefficients coefficients to apply to the method staged derivatives
96 * @return combined state
97 */
98 protected final double[] previousStateLinearCombination(final double ... coefficients) {
99 return combine(getGlobalPreviousState().getCompleteState(),
100 coefficients);
101 }
102
103 /** Compute a state by linear combination added to current state.
104 * @param coefficients coefficients to apply to the method staged derivatives
105 * @return combined state
106 */
107 protected double[] currentStateLinearCombination(final double ... coefficients) {
108 return combine(getGlobalCurrentState().getCompleteState(),
109 coefficients);
110 }
111
112 /** Compute a state derivative by linear combination.
113 * @param coefficients coefficients to apply to the method staged derivatives
114 * @return combined state
115 */
116 protected double[] derivativeLinearCombination(final double ... coefficients) {
117 return combine(new double[yDotK[0].length], coefficients);
118 }
119
120 /** Linearly combine arrays.
121 * @param a array to add to
122 * @param coefficients coefficients to apply to the method staged derivatives
123 * @return a itself, as a conveniency for fluent API
124 */
125 private double[] combine(final double[] a, final double ... coefficients) {
126 for (int i = 0; i < a.length; ++i) {
127 for (int k = 0; k < coefficients.length; ++k) {
128 a[i] += coefficients[k] * yDotK[k][i];
129 }
130 }
131 return a;
132 }
133
134 }