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 second order
27   * Runge-Kutta integrator.
28   *
29   * <p>This interpolator computes 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>) + &theta; h [(1 - &theta;) y'<sub>1</sub> + &theta; y'<sub>2</sub>]
35   *   </li>
36   *   <li>Using reference point at step end:<br>
37   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) + (1-&theta;) h [&theta; y'<sub>1</sub> - (1+&theta;) y'<sub>2</sub>]
38   *   </li>
39   * </ul>
40   *
41   * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
42   * evaluations of the derivatives already computed during the
43   * step.</p>
44   *
45   * @see MidpointFieldIntegrator
46   * @param <T> the type of the field elements
47   * @since 3.6
48   */
49  
50  class MidpointFieldStepInterpolator<T extends RealFieldElement<T>>
51      extends RungeKuttaFieldStepInterpolator<T> {
52  
53      /** Simple constructor.
54       * @param field field to which the time and state vector elements belong
55       * @param forward integration direction indicator
56       * @param yDotK slopes at the intermediate points
57       * @param globalPreviousState start of the global step
58       * @param globalCurrentState end of the global step
59       * @param softPreviousState start of the restricted step
60       * @param softCurrentState end of the restricted step
61       * @param mapper equations mapper for the all equations
62       */
63      MidpointFieldStepInterpolator(final Field<T> field, final boolean forward,
64                                               final T[][] yDotK,
65                                               final FieldODEStateAndDerivative<T> globalPreviousState,
66                                               final FieldODEStateAndDerivative<T> globalCurrentState,
67                                               final FieldODEStateAndDerivative<T> softPreviousState,
68                                               final FieldODEStateAndDerivative<T> softCurrentState,
69                                               final FieldEquationsMapper<T> mapper) {
70          super(field, forward, yDotK,
71                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
72                mapper);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      protected MidpointFieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
78                                                        final FieldODEStateAndDerivative<T> newGlobalPreviousState,
79                                                        final FieldODEStateAndDerivative<T> newGlobalCurrentState,
80                                                        final FieldODEStateAndDerivative<T> newSoftPreviousState,
81                                                        final FieldODEStateAndDerivative<T> newSoftCurrentState,
82                                                        final FieldEquationsMapper<T> newMapper) {
83          return new MidpointFieldStepInterpolator<>(newField, newForward, newYDotK,
84                                                      newGlobalPreviousState, newGlobalCurrentState,
85                                                      newSoftPreviousState, newSoftCurrentState,
86                                                      newMapper);
87      }
88  
89      /** {@inheritDoc} */
90      @SuppressWarnings("unchecked")
91      @Override
92      protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
93                                                                                     final T time, final T theta,
94                                                                                     final T thetaH, final T oneMinusThetaH) {
95  
96          final T coeffDot2 = theta.multiply(2);
97          final T coeffDot1 = time.getField().getOne().subtract(coeffDot2);
98          final T[] interpolatedState;
99          final T[] interpolatedDerivatives;
100 
101         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
102             final T coeff1 = theta.multiply(oneMinusThetaH);
103             final T coeff2 = theta.multiply(thetaH);
104             interpolatedState       = previousStateLinearCombination(coeff1, coeff2);
105             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
106         } else {
107             final T coeff1 = oneMinusThetaH.multiply(theta);
108             final T coeff2 = oneMinusThetaH.multiply(theta.add(1)).negate();
109             interpolatedState       = currentStateLinearCombination(coeff1, coeff2);
110             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
111         }
112 
113         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
114     }
115 }