View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.ode.nonstiff;
19  
20  import org.hipparchus.ode.EquationsMapper;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  
23  /**
24   * This class implements a step interpolator for second order
25   * Runge-Kutta integrator.
26   *
27   * <p>This interpolator computes dense output inside the last
28   * step computed. The interpolation equation is consistent with the
29   * integration scheme :</p>
30   * <ul>
31   *   <li>Using reference point at step start:<br>
32   *   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>]
33   *   </li>
34   *   <li>Using reference point at step end:<br>
35   *   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>]
36   *   </li>
37   * </ul>
38   *
39   * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
40   * evaluations of the derivatives already computed during the
41   * step.</p>
42   *
43   * @see MidpointIntegrator
44   */
45  
46  class MidpointStateInterpolator
47      extends RungeKuttaStateInterpolator {
48  
49      /** Serializable version identifier. */
50      private static final long serialVersionUID = 20160328L;
51  
52      /** Simple constructor.
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      MidpointStateInterpolator(final boolean forward,
62                                final double[][] yDotK,
63                                final ODEStateAndDerivative globalPreviousState,
64                                final ODEStateAndDerivative globalCurrentState,
65                                final ODEStateAndDerivative softPreviousState,
66                                final ODEStateAndDerivative softCurrentState,
67                                final EquationsMapper mapper) {
68          super(forward, yDotK,
69                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
70                mapper);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected MidpointStateInterpolator create(final boolean newForward, final double[][] newYDotK,
76                                                 final ODEStateAndDerivative newGlobalPreviousState,
77                                                 final ODEStateAndDerivative newGlobalCurrentState,
78                                                 final ODEStateAndDerivative newSoftPreviousState,
79                                                 final ODEStateAndDerivative newSoftCurrentState,
80                                                 final EquationsMapper newMapper) {
81          return new MidpointStateInterpolator(newForward, newYDotK,
82                                               newGlobalPreviousState, newGlobalCurrentState,
83                                               newSoftPreviousState, newSoftCurrentState,
84                                               newMapper);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
90                                                                             final double time, final double theta,
91                                                                             final double thetaH, final double oneMinusThetaH) {
92          final double coeffDot2 = 2 * theta;
93          final double coeffDot1 = 1 - coeffDot2;
94  
95          final double[] interpolatedState;
96          final double[] interpolatedDerivatives;
97          if (getGlobalPreviousState() != null && theta <= 0.5) {
98  
99              final double coeff1     = theta * oneMinusThetaH;
100             final double coeff2     = theta * thetaH;
101             interpolatedState       = previousStateLinearCombination(coeff1, coeff2);
102             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
103         } else {
104             final double coeff1     =  oneMinusThetaH * theta;
105             final double coeff2     = -oneMinusThetaH * (1.0 + theta);
106             interpolatedState       = currentStateLinearCombination(coeff1, coeff2);
107             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
108         }
109 
110         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
111 
112     }
113 
114 }