View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.ode.nonstiff;
19  
20  import org.hipparchus.ode.EquationsMapper;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  import org.hipparchus.util.FastMath;
23  
24  /**
25   * This class implements a step interpolator for the Gill fourth
26   * order Runge-Kutta integrator.
27   *
28   * <p>This interpolator allows to compute dense output inside the last
29   * step computed. The interpolation equation is consistent with the
30   * integration scheme :</p>
31   * <ul>
32   *   <li>Using reference point at step start:<br>
33   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
34   *                    + &theta; (h/6) [ (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
35   *                                    + (    6 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
36   *                                    + (  - 3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
37   *                                    ]
38   *   </li>
39   *   <li>Using reference point at step start:<br>
40   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
41   *                    - (1 - &theta;) (h/6) [ (1 - 5 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
42   *                                          + (2 + 2 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
43   *                                          + (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
44   *                                          ]
45   *   </li>
46   * </ul>
47   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub>
48   * are the four evaluations of the derivatives already computed during
49   * the step.</p>
50   *
51   * @see GillIntegrator
52   */
53  
54  class GillStateInterpolator
55      extends RungeKuttaStateInterpolator {
56  
57      /** First Gill coefficient. */
58      private static final double ONE_MINUS_INV_SQRT_2 = 1 - FastMath.sqrt(0.5);
59  
60      /** Second Gill coefficient. */
61      private static final double ONE_PLUS_INV_SQRT_2 = 1 + FastMath.sqrt(0.5);
62  
63      /** Serializable version identifier. */
64      private static final long serialVersionUID = 20160328L;
65  
66      /** Simple constructor.
67       * @param forward integration direction indicator
68       * @param yDotK slopes at the intermediate points
69       * @param globalPreviousState start of the global step
70       * @param globalCurrentState end of the global step
71       * @param softPreviousState start of the restricted step
72       * @param softCurrentState end of the restricted step
73       * @param mapper equations mapper for the all equations
74       */
75      GillStateInterpolator(final boolean forward,
76                            final double[][] yDotK,
77                            final ODEStateAndDerivative globalPreviousState,
78                            final ODEStateAndDerivative globalCurrentState,
79                            final ODEStateAndDerivative softPreviousState,
80                            final ODEStateAndDerivative softCurrentState,
81                            final EquationsMapper mapper) {
82          super(forward, yDotK,
83                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
84                mapper);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      protected GillStateInterpolator create(final boolean newForward, final double[][] newYDotK,
90                                             final ODEStateAndDerivative newGlobalPreviousState,
91                                             final ODEStateAndDerivative newGlobalCurrentState,
92                                             final ODEStateAndDerivative newSoftPreviousState,
93                                             final ODEStateAndDerivative newSoftCurrentState,
94                                             final EquationsMapper newMapper) {
95          return new GillStateInterpolator(newForward, newYDotK,
96                                           newGlobalPreviousState, newGlobalCurrentState,
97                                           newSoftPreviousState, newSoftCurrentState,
98                                           newMapper);
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
104                                                                            final double time, final double theta,
105                                                                            final double thetaH, final double oneMinusThetaH) {
106 
107         final double twoTheta   = 2 * theta;
108         final double fourTheta2 = twoTheta * twoTheta;
109         final double coeffDot1  = theta * (twoTheta - 3) + 1;
110         final double cDot23     = twoTheta * (1 - theta);
111         final double coeffDot2  = cDot23  * ONE_MINUS_INV_SQRT_2;
112         final double coeffDot3  = cDot23  * ONE_PLUS_INV_SQRT_2;
113         final double coeffDot4  = theta * (twoTheta - 1);
114 
115         final double[] interpolatedState;
116         final double[] interpolatedDerivatives;
117         if (getGlobalPreviousState() != null && theta <= 0.5) {
118             final double s         = thetaH / 6.0;
119             final double c23       = s * (6 * theta - fourTheta2);
120             final double coeff1    = s * (6 - 9 * theta + fourTheta2);
121             final double coeff2    = c23  * ONE_MINUS_INV_SQRT_2;
122             final double coeff3    = c23  * ONE_PLUS_INV_SQRT_2;
123             final double coeff4    = s * (-3 * theta + fourTheta2);
124             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
125             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3 , coeffDot4);
126         } else {
127             final double s      = oneMinusThetaH / -6.0;
128             final double c23    = s * (2 + twoTheta - fourTheta2);
129             final double coeff1 = s * (1 - 5 * theta + fourTheta2);
130             final double coeff2 = c23  * ONE_MINUS_INV_SQRT_2;
131             final double coeff3 = c23  * ONE_PLUS_INV_SQRT_2;
132             final double coeff4 = s * (1 + theta + fourTheta2);
133             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
134             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3 , coeffDot4);
135         }
136 
137         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
138 
139     }
140 
141 }