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 Gill 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/6) [ (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
41   *                                    + (    6 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
42   *                                    + (  - 3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
43   *                                    ]
44   *   </li>
45   *   <li>Using reference point at step start:<br>
46   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
47   *                    - (1 - &theta;) (h/6) [ (1 - 5 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
48   *                                          + (2 + 2 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
49   *                                          + (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
50   *                                          ]
51   *   </li>
52   * </ul>
53   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub>
54   * are the four evaluations of the derivatives already computed during
55   * the step.</p>
56   *
57   * @see GillFieldIntegrator
58   * @param <T> the type of the field elements
59   */
60  
61  class GillFieldStateInterpolator<T extends CalculusFieldElement<T>>
62      extends RungeKuttaFieldStateInterpolator<T> {
63  
64      /** First Gill coefficient. */
65      private final T one_minus_inv_sqrt_2;
66  
67      /** Second Gill coefficient. */
68      private final T one_plus_inv_sqrt_2;
69  
70      /** Simple constructor.
71       * @param field field to which the time and state vector elements belong
72       * @param forward integration direction indicator
73       * @param yDotK slopes at the intermediate points
74       * @param globalPreviousState start of the global step
75       * @param globalCurrentState end of the global step
76       * @param softPreviousState start of the restricted step
77       * @param softCurrentState end of the restricted step
78       * @param mapper equations mapper for the all equations
79       */
80      GillFieldStateInterpolator(final Field<T> field, final boolean forward,
81                                 final T[][] yDotK,
82                                 final FieldODEStateAndDerivative<T> globalPreviousState,
83                                 final FieldODEStateAndDerivative<T> globalCurrentState,
84                                 final FieldODEStateAndDerivative<T> softPreviousState,
85                                 final FieldODEStateAndDerivative<T> softCurrentState,
86                                 final FieldEquationsMapper<T> mapper) {
87          super(field, forward, yDotK,
88                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
89                mapper);
90          final T sqrt = field.getZero().add(0.5).sqrt();
91          one_minus_inv_sqrt_2 = field.getOne().subtract(sqrt);
92          one_plus_inv_sqrt_2  = field.getOne().add(sqrt);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      protected GillFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
98                                                     final FieldODEStateAndDerivative<T> newGlobalPreviousState,
99                                                     final FieldODEStateAndDerivative<T> newGlobalCurrentState,
100                                                    final FieldODEStateAndDerivative<T> newSoftPreviousState,
101                                                    final FieldODEStateAndDerivative<T> newSoftCurrentState,
102                                                    final FieldEquationsMapper<T> newMapper) {
103         return new GillFieldStateInterpolator<T>(newField, newForward, newYDotK,
104                                                  newGlobalPreviousState, newGlobalCurrentState,
105                                                  newSoftPreviousState, newSoftCurrentState,
106                                                  newMapper);
107     }
108 
109     /** {@inheritDoc} */
110     @SuppressWarnings("unchecked")
111     @Override
112     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
113                                                                                    final T time, final T theta,
114                                                                                    final T thetaH, final T oneMinusThetaH) {
115 
116         final T one        = time.getField().getOne();
117         final T twoTheta   = theta.multiply(2);
118         final T fourTheta2 = twoTheta.multiply(twoTheta);
119         final T coeffDot1  = theta.multiply(twoTheta.subtract(3)).add(1);
120         final T cDot23     = twoTheta.multiply(one.subtract(theta));
121         final T coeffDot2  = cDot23.multiply(one_minus_inv_sqrt_2);
122         final T coeffDot3  = cDot23.multiply(one_plus_inv_sqrt_2);
123         final T coeffDot4  = theta.multiply(twoTheta.subtract(1));
124         final T[] interpolatedState;
125         final T[] interpolatedDerivatives;
126 
127         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
128             final T s               = thetaH.divide(6.0);
129             final T c23             = s.multiply(theta.multiply(6).subtract(fourTheta2));
130             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
131             final T coeff2          = c23.multiply(one_minus_inv_sqrt_2);
132             final T coeff3          = c23.multiply(one_plus_inv_sqrt_2);
133             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
134             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
135             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
136         } else {
137             final T s      = oneMinusThetaH.divide(-6.0);
138             final T c23    = s.multiply(twoTheta.add(2).subtract(fourTheta2));
139             final T coeff1 = s.multiply(fourTheta2.subtract(theta.multiply(5)).add(1));
140             final T coeff2 = c23.multiply(one_minus_inv_sqrt_2);
141             final T coeff3 = c23.multiply(one_plus_inv_sqrt_2);
142             final T coeff4 = s.multiply(fourTheta2.add(theta).add(1));
143             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
144             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
145         }
146 
147         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
148 
149     }
150 
151 }