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.ClassicalRungeKuttaFieldIntegrator;
30  
31  /**
32   * This class implements a step interpolator for the classical fourth
33   * order Runge-Kutta integrator.
34   *
35   * <p>This interpolator allows to compute dense output inside the last
36   * step computed. The interpolation equation is consistent with the
37   * integration scheme :</p>
38   * <ul>
39   *   <li>Using reference point at step start:<br>
40   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
41   *                    + &theta; (h/6) [  (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
42   *                                     + (    6 &theta; - 4 &theta;<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
43   *                                     + (   -3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
44   *                                    ]
45   *   </li>
46   *   <li>Using reference point at step end:<br>
47   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
48   *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
49   *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
50   *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
51   *                                        ]
52   *   </li>
53   * </ul>
54   *
55   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
56   * evaluations of the derivatives already computed during the
57   * step.</p>
58   *
59   * @see ClassicalRungeKuttaFieldIntegrator
60   * @param <T> the type of the field elements
61   */
62  
63  public class ClassicalRungeKuttaFieldStateInterpolator<T extends CalculusFieldElement<T>>
64      extends RungeKuttaFieldStateInterpolator<T> {
65  
66      /** Simple constructor.
67       * @param field field to which the time and state vector elements belong
68       * @param forward integration direction indicator
69       * @param yDotK slopes at the intermediate points
70       * @param globalPreviousState start of the global step
71       * @param globalCurrentState end of the global step
72       * @param softPreviousState start of the restricted step
73       * @param softCurrentState end of the restricted step
74       * @param mapper equations mapper for the all equations
75       */
76      public ClassicalRungeKuttaFieldStateInterpolator(final Field<T> field, final boolean forward, final T[][] yDotK,
77                                                       final FieldODEStateAndDerivative<T> globalPreviousState,
78                                                       final FieldODEStateAndDerivative<T> globalCurrentState,
79                                                       final FieldODEStateAndDerivative<T> softPreviousState,
80                                                       final FieldODEStateAndDerivative<T> softCurrentState,
81                                                       final FieldEquationsMapper<T> mapper) {
82          super(field, forward, yDotK, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
83                  mapper);
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      protected ClassicalRungeKuttaFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
89                                                                    final FieldODEStateAndDerivative<T> newGlobalPreviousState,
90                                                                    final FieldODEStateAndDerivative<T> newGlobalCurrentState,
91                                                                    final FieldODEStateAndDerivative<T> newSoftPreviousState,
92                                                                    final FieldODEStateAndDerivative<T> newSoftCurrentState,
93                                                                    final FieldEquationsMapper<T> newMapper) {
94          return new ClassicalRungeKuttaFieldStateInterpolator<>(newField, newForward, newYDotK,
95                                                                  newGlobalPreviousState, newGlobalCurrentState,
96                                                                  newSoftPreviousState, newSoftCurrentState,
97                                                                  newMapper);
98      }
99  
100     /** {@inheritDoc} */
101     @SuppressWarnings("unchecked")
102     @Override
103     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
104                                                                                    final T time, final T theta,
105                                                                                    final T thetaH, final T oneMinusThetaH) {
106 
107         final T one                       = time.getField().getOne();
108         final T oneMinusTheta             = one.subtract(theta);
109         final T oneMinus2Theta            = one.subtract(theta.multiply(2));
110         final T coeffDot1                 = oneMinusTheta.multiply(oneMinus2Theta);
111         final T coeffDot23                = theta.multiply(oneMinusTheta).multiply(2);
112         final T coeffDot4                 = theta.multiply(oneMinus2Theta).negate();
113         final T[] interpolatedState;
114         final T[] interpolatedDerivatives;
115 
116         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
117             final T fourTheta2      = theta.multiply(theta).multiply(4);
118             final T s               = thetaH.divide(6.0);
119             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
120             final T coeff23         = s.multiply(theta.multiply(6).subtract(fourTheta2));
121             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
122             interpolatedState       = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
123             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
124         } else {
125             final T fourTheta       = theta.multiply(4);
126             final T s               = oneMinusThetaH.divide(6);
127             final T coeff1          = s.multiply(theta.multiply(fourTheta.negate().add(5)).subtract(1));
128             final T coeff23         = s.multiply(theta.multiply(fourTheta.subtract(2)).subtract(2));
129             final T coeff4          = s.multiply(theta.multiply(fourTheta.negate().subtract(1)).subtract(1));
130             interpolatedState       = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
131             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
132         }
133 
134         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
135 
136     }
137 
138 }