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