View Javadoc
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;
19  
20  import org.hipparchus.ode.EquationsMapper;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  import org.hipparchus.ode.sampling.AbstractODEStateInterpolator;
23  
24  /** This class represents an interpolator over the last step during an
25   * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
26   *
27   * @see RungeKuttaIntegrator
28   * @see EmbeddedRungeKuttaIntegrator
29   *
30   */
31  
32  abstract class RungeKuttaStateInterpolator extends AbstractODEStateInterpolator {
33  
34      /** Serializable UID. */
35      private static final long serialVersionUID = 20160328L;
36  
37      /** Slopes at the intermediate points */
38      protected double[][] yDotK;
39  
40      /** Simple constructor.
41       * @param forward integration direction indicator
42       * @param yDotK slopes at the intermediate points
43       * @param globalPreviousState start of the global step
44       * @param globalCurrentState end of the global step
45       * @param softPreviousState start of the restricted step
46       * @param softCurrentState end of the restricted step
47       * @param mapper equations mapper for the all equations
48       */
49      protected RungeKuttaStateInterpolator(final boolean forward,
50                                            final double[][] yDotK,
51                                            final ODEStateAndDerivative globalPreviousState,
52                                            final ODEStateAndDerivative globalCurrentState,
53                                            final ODEStateAndDerivative softPreviousState,
54                                            final ODEStateAndDerivative softCurrentState,
55                                            final EquationsMapper mapper) {
56          super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
57          this.yDotK = new double[yDotK.length][];
58          for (int i = 0; i < yDotK.length; ++i) {
59              this.yDotK[i] = yDotK[i].clone();
60          }
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      protected RungeKuttaStateInterpolator create(boolean newForward,
66                                                   ODEStateAndDerivative newGlobalPreviousState,
67                                                   ODEStateAndDerivative newGlobalCurrentState,
68                                                   ODEStateAndDerivative newSoftPreviousState,
69                                                   ODEStateAndDerivative newSoftCurrentState,
70                                                   EquationsMapper newMapper) {
71          return create(newForward, yDotK,
72                        newGlobalPreviousState, newGlobalCurrentState,
73                        newSoftPreviousState, newSoftCurrentState,
74                        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 }