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    *      http://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.apache.commons.math4.legacy.ode.nonstiff;
19  
20  import org.apache.commons.math4.legacy.core.Field;
21  import org.apache.commons.math4.legacy.core.RealFieldElement;
22  import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23  import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24  
25  /**
26   * This class implements a step interpolator for the Gill fourth
27   * order Runge-Kutta integrator.
28   *
29   * <p>This interpolator allows to compute dense output inside the last
30   * step computed. The interpolation equation is consistent with the
31   * integration scheme :
32   * <ul>
33   *   <li>Using reference point at step start:<br>
34   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
35   *                    + &theta; (h/6) [ (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
36   *                                    + (    6 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
37   *                                    + (  - 3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
38   *                                    ]
39   *   </li>
40   *   <li>Using reference point at step start:<br>
41   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
42   *                    - (1 - &theta;) (h/6) [ (1 - 5 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
43   *                                          + (2 + 2 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
44   *                                          + (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
45   *                                          ]
46   *   </li>
47   * </ul>
48   * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub>
49   * are the four evaluations of the derivatives already computed during
50   * the step.</p>
51   *
52   * @see GillFieldIntegrator
53   * @param <T> the type of the field elements
54   * @since 3.6
55   */
56  
57  class GillFieldStepInterpolator<T extends RealFieldElement<T>>
58    extends RungeKuttaFieldStepInterpolator<T> {
59  
60      /** First Gill coefficient. */
61      private final T one_minus_inv_sqrt_2;
62  
63      /** Second Gill coefficient. */
64      private final T one_plus_inv_sqrt_2;
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      GillFieldStepInterpolator(final Field<T> field, final boolean forward,
77                                final T[][] yDotK,
78                                final FieldODEStateAndDerivative<T> globalPreviousState,
79                                final FieldODEStateAndDerivative<T> globalCurrentState,
80                                final FieldODEStateAndDerivative<T> softPreviousState,
81                                final FieldODEStateAndDerivative<T> softCurrentState,
82                                final FieldEquationsMapper<T> mapper) {
83          super(field, forward, yDotK,
84                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
85                mapper);
86          final T sqrt = field.getZero().add(0.5).sqrt();
87          one_minus_inv_sqrt_2 = field.getOne().subtract(sqrt);
88          one_plus_inv_sqrt_2  = field.getOne().add(sqrt);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      protected GillFieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
94                                                    final FieldODEStateAndDerivative<T> newGlobalPreviousState,
95                                                    final FieldODEStateAndDerivative<T> newGlobalCurrentState,
96                                                    final FieldODEStateAndDerivative<T> newSoftPreviousState,
97                                                    final FieldODEStateAndDerivative<T> newSoftCurrentState,
98                                                    final FieldEquationsMapper<T> newMapper) {
99          return new GillFieldStepInterpolator<>(newField, newForward, newYDotK,
100                                                 newGlobalPreviousState, newGlobalCurrentState,
101                                                 newSoftPreviousState, newSoftCurrentState,
102                                                 newMapper);
103     }
104 
105     /** {@inheritDoc} */
106     @SuppressWarnings("unchecked")
107     @Override
108     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
109                                                                                    final T time, final T theta,
110                                                                                    final T thetaH, final T oneMinusThetaH) {
111 
112         final T one        = time.getField().getOne();
113         final T twoTheta   = theta.multiply(2);
114         final T fourTheta2 = twoTheta.multiply(twoTheta);
115         final T coeffDot1  = theta.multiply(twoTheta.subtract(3)).add(1);
116         final T cDot23     = twoTheta.multiply(one.subtract(theta));
117         final T coeffDot2  = cDot23.multiply(one_minus_inv_sqrt_2);
118         final T coeffDot3  = cDot23.multiply(one_plus_inv_sqrt_2);
119         final T coeffDot4  = theta.multiply(twoTheta.subtract(1));
120         final T[] interpolatedState;
121         final T[] interpolatedDerivatives;
122 
123         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
124             final T s               = thetaH.divide(6.0);
125             final T c23             = s.multiply(theta.multiply(6).subtract(fourTheta2));
126             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
127             final T coeff2          = c23.multiply(one_minus_inv_sqrt_2);
128             final T coeff3          = c23.multiply(one_plus_inv_sqrt_2);
129             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
130             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
131             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
132         } else {
133             final T s      = oneMinusThetaH.divide(-6.0);
134             final T c23    = s.multiply(twoTheta.add(2).subtract(fourTheta2));
135             final T coeff1 = s.multiply(fourTheta2.subtract(theta.multiply(5)).add(1));
136             final T coeff2 = c23.multiply(one_minus_inv_sqrt_2);
137             final T coeff3 = c23.multiply(one_plus_inv_sqrt_2);
138             final T coeff4 = s.multiply(fourTheta2.add(theta).add(1));
139             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
140             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
141         }
142 
143         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
144     }
145 }