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 represents an interpolator over the last step during an
27   * ODE integration for the 5(4) Higham and Hall integrator.
28   *
29   * @see HighamHall54FieldIntegrator
30   *
31   * @param <T> the type of the field elements
32   * @since 3.6
33   */
34  
35  class HighamHall54FieldStepInterpolator<T extends RealFieldElement<T>>
36      extends RungeKuttaFieldStepInterpolator<T> {
37  
38      /** Simple constructor.
39       * @param field field to which the time and state vector elements belong
40       * @param forward integration direction indicator
41       * @param yDotK slopes at the intermediate points
42       * @param globalPreviousState start of the global step
43       * @param globalCurrentState end of the global step
44       * @param softPreviousState start of the restricted step
45       * @param softCurrentState end of the restricted step
46       * @param mapper equations mapper for the all equations
47       */
48      HighamHall54FieldStepInterpolator(final Field<T> field, final boolean forward,
49                                        final T[][] yDotK,
50                                        final FieldODEStateAndDerivative<T> globalPreviousState,
51                                        final FieldODEStateAndDerivative<T> globalCurrentState,
52                                        final FieldODEStateAndDerivative<T> softPreviousState,
53                                        final FieldODEStateAndDerivative<T> softCurrentState,
54                                        final FieldEquationsMapper<T> mapper) {
55          super(field, forward, yDotK,
56                globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
57                mapper);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      protected HighamHall54FieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
63                                                            final FieldODEStateAndDerivative<T> newGlobalPreviousState,
64                                                            final FieldODEStateAndDerivative<T> newGlobalCurrentState,
65                                                            final FieldODEStateAndDerivative<T> newSoftPreviousState,
66                                                            final FieldODEStateAndDerivative<T> newSoftCurrentState,
67                                                            final FieldEquationsMapper<T> newMapper) {
68          return new HighamHall54FieldStepInterpolator<>(newField, newForward, newYDotK,
69                                                          newGlobalPreviousState, newGlobalCurrentState,
70                                                          newSoftPreviousState, newSoftCurrentState,
71                                                          newMapper);
72      }
73  
74      /** {@inheritDoc} */
75      @SuppressWarnings("unchecked")
76      @Override
77      protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
78                                                                                     final T time, final T theta,
79                                                                                     final T thetaH, final T oneMinusThetaH) {
80  
81          final T bDot0 = theta.multiply(theta.multiply(theta.multiply( -10.0      ).add( 16.0       )).add(-15.0 /  2.0)).add(1);
82          final T bDot1 = time.getField().getZero();
83          final T bDot2 = theta.multiply(theta.multiply(theta.multiply( 135.0 / 2.0).add(-729.0 / 8.0)).add(459.0 / 16.0));
84          final T bDot3 = theta.multiply(theta.multiply(theta.multiply(-120.0      ).add( 152.0      )).add(-44.0       ));
85          final T bDot4 = theta.multiply(theta.multiply(theta.multiply( 125.0 / 2.0).add(-625.0 / 8.0)).add(375.0 / 16.0));
86          final T bDot5 = theta.multiply(  5.0 /  8.0).multiply(theta.multiply(2).subtract(1));
87          final T[] interpolatedState;
88          final T[] interpolatedDerivatives;
89  
90          if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
91              final T b0 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply( -5.0 / 2.0).add(  16.0 /  3.0)).add(-15.0 /  4.0)).add(1));
92              final T b1 = time.getField().getZero();
93              final T b2 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(135.0 / 8.0).add(-243.0 /  8.0)).add(459.0 / 32.0)));
94              final T b3 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(-30.0      ).add( 152.0 /  3.0)).add(-22.0       )));
95              final T b4 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(125.0 / 8.0).add(-625.0 / 24.0)).add(375.0 / 32.0)));
96              final T b5 = thetaH.multiply(theta.multiply(theta.multiply(                                   5.0 / 12.0 ).add( -5.0 / 16.0)));
97              interpolatedState       = previousStateLinearCombination(b0, b1, b2, b3, b4, b5);
98              interpolatedDerivatives = derivativeLinearCombination(bDot0, bDot1, bDot2, bDot3, bDot4, bDot5);
99          } else {
100             final T theta2 = theta.multiply(theta);
101             final T h      = thetaH.divide(theta);
102             final T b0 = h.multiply( theta.multiply(theta.multiply(theta.multiply(theta.multiply(-5.0 / 2.0).add( 16.0 / 3.0)).add( -15.0 /  4.0)).add(  1.0       )).add(  -1.0 / 12.0));
103             final T b1 = time.getField().getZero();
104             final T b2 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               135.0 / 8.0 ).add(-243.0 /  8.0)).add(459.0 / 32.0)).add( -27.0 / 32.0));
105             final T b3 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               -30.0       ).add( 152.0 /  3.0)).add(-22.0       )).add(  4.0  /  3.0));
106             final T b4 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               125.0 / 8.0 ).add(-625.0 / 24.0)).add(375.0 / 32.0)).add(-125.0 / 96.0));
107             final T b5 = h.multiply(theta2.multiply(theta.multiply(                                                                   5.0 / 12.0 ).add(-5.0  / 16.0)).add(  -5.0 / 48.0));
108             interpolatedState       = currentStateLinearCombination(b0, b1, b2, b3, b4, b5);
109             interpolatedDerivatives = derivativeLinearCombination(bDot0, bDot1, bDot2, bDot3, bDot4, bDot5);
110         }
111 
112         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
113     }
114 }