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