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