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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode.nonstiff;
24  
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.Field;
27  import org.hipparchus.ode.FieldEquationsMapper;
28  import org.hipparchus.ode.FieldODEStateAndDerivative;
29  
30  /**
31   * This class implements a step interpolator for second order
32   * Runge-Kutta integrator.
33   *
34   * <p>This interpolator computes dense output inside the last
35   * step computed. The interpolation equation is consistent with the
36   * integration scheme :</p>
37   * <ul>
38   *   <li>Using reference point at step start:<br>
39   *   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>]
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) + (1-&theta;) h [&theta; y'<sub>1</sub> - (1+&theta;) y'<sub>2</sub>]
43   *   </li>
44   * </ul>
45   *
46   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
47   * evaluations of the derivatives already computed during the
48   * step.</p>
49   *
50   * @see MidpointFieldIntegrator
51   * @param <T> the type of the field elements
52   */
53  
54  class MidpointFieldStateInterpolator<T extends CalculusFieldElement<T>>
55      extends RungeKuttaFieldStateInterpolator<T> {
56  
57      /** Simple constructor.
58       * @param field field to which the time and state vector elements belong
59       * @param forward integration direction indicator
60       * @param yDotK slopes at the intermediate points
61       * @param globalPreviousState start of the global step
62       * @param globalCurrentState end of the global step
63       * @param softPreviousState start of the restricted step
64       * @param softCurrentState end of the restricted step
65       * @param mapper equations mapper for the all equations
66       */
67      MidpointFieldStateInterpolator(final Field<T> field, final boolean forward,
68                                     final T[][] yDotK,
69                                     final FieldODEStateAndDerivative<T> globalPreviousState,
70                                     final FieldODEStateAndDerivative<T> globalCurrentState,
71                                     final FieldODEStateAndDerivative<T> softPreviousState,
72                                     final FieldODEStateAndDerivative<T> softCurrentState,
73                                     final FieldEquationsMapper<T> mapper) {
74          super(field, forward, yDotK,
75                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
76                mapper);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      protected MidpointFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
82                                                         final FieldODEStateAndDerivative<T> newGlobalPreviousState,
83                                                         final FieldODEStateAndDerivative<T> newGlobalCurrentState,
84                                                         final FieldODEStateAndDerivative<T> newSoftPreviousState,
85                                                         final FieldODEStateAndDerivative<T> newSoftCurrentState,
86                                                         final FieldEquationsMapper<T> newMapper) {
87          return new MidpointFieldStateInterpolator<T>(newField, newForward, newYDotK,
88                                                       newGlobalPreviousState, newGlobalCurrentState,
89                                                       newSoftPreviousState, newSoftCurrentState,
90                                                       newMapper);
91      }
92  
93      /** {@inheritDoc} */
94      @SuppressWarnings("unchecked")
95      @Override
96      protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
97                                                                                     final T time, final T theta,
98                                                                                     final T thetaH, final T oneMinusThetaH) {
99  
100         final T coeffDot2 = theta.multiply(2);
101         final T coeffDot1 = time.getField().getOne().subtract(coeffDot2);
102         final T[] interpolatedState;
103         final T[] interpolatedDerivatives;
104 
105         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
106             final T coeff1 = theta.multiply(oneMinusThetaH);
107             final T coeff2 = theta.multiply(thetaH);
108             interpolatedState       = previousStateLinearCombination(coeff1, coeff2);
109             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
110         } else {
111             final T coeff1 = oneMinusThetaH.multiply(theta);
112             final T coeff2 = oneMinusThetaH.multiply(theta.add(1)).negate();
113             interpolatedState       = currentStateLinearCombination(coeff1, coeff2);
114             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
115         }
116 
117         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
118 
119     }
120 
121 }