View Javadoc
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  import org.hipparchus.ode.sampling.AbstractFieldODEStateInterpolator;
30  import org.hipparchus.util.MathArrays;
31  
32  /** This class represents an interpolator over the last step during an
33   * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
34   *
35   * @see RungeKuttaFieldIntegrator
36   * @see EmbeddedRungeKuttaFieldIntegrator
37   *
38   * @param <T> the type of the field elements
39   */
40  
41  abstract class RungeKuttaFieldStateInterpolator<T extends CalculusFieldElement<T>>
42      extends AbstractFieldODEStateInterpolator<T> {
43  
44      /** Field to which the time and state vector elements belong. */
45      private final Field<T> field;
46  
47      /** Slopes at the intermediate points. */
48      private final T[][] yDotK;
49  
50      /** Simple constructor.
51       * @param field field to which the time and state vector elements belong
52       * @param forward integration direction indicator
53       * @param yDotK slopes at the intermediate points
54       * @param globalPreviousState start of the global step
55       * @param globalCurrentState end of the global step
56       * @param softPreviousState start of the restricted step
57       * @param softCurrentState end of the restricted step
58       * @param mapper equations mapper for the all equations
59       */
60      protected RungeKuttaFieldStateInterpolator(final Field<T> field, final boolean forward,
61                                                 final T[][] yDotK,
62                                                 final FieldODEStateAndDerivative<T> globalPreviousState,
63                                                 final FieldODEStateAndDerivative<T> globalCurrentState,
64                                                 final FieldODEStateAndDerivative<T> softPreviousState,
65                                                 final FieldODEStateAndDerivative<T> softCurrentState,
66                                                 final FieldEquationsMapper<T> mapper) {
67          super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
68          this.field = field;
69          this.yDotK = MathArrays.buildArray(field, yDotK.length, -1);
70          for (int i = 0; i < yDotK.length; ++i) {
71              this.yDotK[i] = yDotK[i].clone();
72          }
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      protected RungeKuttaFieldStateInterpolator<T> create(boolean newForward,
78                                                           FieldODEStateAndDerivative<T> newGlobalPreviousState,
79                                                           FieldODEStateAndDerivative<T> newGlobalCurrentState,
80                                                           FieldODEStateAndDerivative<T> newSoftPreviousState,
81                                                           FieldODEStateAndDerivative<T> newSoftCurrentState,
82                                                           FieldEquationsMapper<T> newMapper) {
83          return create(field, newForward, yDotK,
84                        newGlobalPreviousState, newGlobalCurrentState,
85                        newSoftPreviousState, newSoftCurrentState,
86                        newMapper);
87      }
88  
89      /** Create a new instance.
90       * @param newField field to which the time and state vector elements belong
91       * @param newForward integration direction indicator
92       * @param newYDotK slopes at the intermediate points
93       * @param newGlobalPreviousState start of the global step
94       * @param newGlobalCurrentState end of the global step
95       * @param newSoftPreviousState start of the restricted step
96       * @param newSoftCurrentState end of the restricted step
97       * @param newMapper equations mapper for the all equations
98       * @return a new instance
99       */
100     protected abstract RungeKuttaFieldStateInterpolator<T> create(Field<T> newField, boolean newForward, T[][] newYDotK,
101                                                                   FieldODEStateAndDerivative<T> newGlobalPreviousState,
102                                                                   FieldODEStateAndDerivative<T> newGlobalCurrentState,
103                                                                   FieldODEStateAndDerivative<T> newSoftPreviousState,
104                                                                   FieldODEStateAndDerivative<T> newSoftCurrentState,
105                                                                   FieldEquationsMapper<T> newMapper);
106 
107     /** Compute a state by linear combination added to previous state.
108      * @param coefficients coefficients to apply to the method staged derivatives
109      * @return combined state
110      */
111     @SafeVarargs
112     protected final T[] previousStateLinearCombination(final T ... coefficients) {
113         return combine(getGlobalPreviousState().getCompleteState(),
114                        coefficients);
115     }
116 
117     /** Compute a state by linear combination added to current state.
118      * @param coefficients coefficients to apply to the method staged derivatives
119      * @return combined state
120      */
121     @SuppressWarnings("unchecked")
122     protected T[] currentStateLinearCombination(final T ... coefficients) {
123         return combine(getGlobalCurrentState().getCompleteState(),
124                        coefficients);
125     }
126 
127     /** Compute a state derivative by linear combination.
128      * @param coefficients coefficients to apply to the method staged derivatives
129      * @return combined state
130      */
131     @SuppressWarnings("unchecked")
132     protected T[] derivativeLinearCombination(final T ... coefficients) {
133         return combine(MathArrays.buildArray(field, yDotK[0].length), coefficients);
134     }
135 
136     /** Linearly combine arrays.
137      * @param a array to add to
138      * @param coefficients coefficients to apply to the method staged derivatives
139      * @return a itself, as a conveniency for fluent API
140      */
141     @SuppressWarnings("unchecked")
142     private T[] combine(final T[] a, final T ... coefficients) {
143         for (int i = 0; i < a.length; ++i) {
144             for (int k = 0; k < coefficients.length; ++k) {
145                 a[i] = a[i].add(coefficients[k].multiply(yDotK[k][i]));
146             }
147         }
148         return a;
149     }
150 
151 }