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