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.math3.ode.nonstiff;
19  
20  import org.apache.commons.math3.ode.sampling.StepInterpolator;
21  import org.apache.commons.math3.util.FastMath;
22  
23  /**
24   * This class implements a step interpolator for the Gill fourth
25   * order Runge-Kutta integrator.
26   *
27   * <p>This interpolator allows to compute dense output inside the last
28   * step computed. The interpolation equation is consistent with the
29   * integration scheme :
30   * <ul>
31   *   <li>Using reference point at step start:<br>
32   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
33   *                    + &theta; (h/6) [ (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
34   *                                    + (    6 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
35   *                                    + (  - 3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
36   *                                    ]
37   *   </li>
38   *   <li>Using reference point at step start:<br>
39   *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
40   *                    - (1 - &theta;) (h/6) [ (1 - 5 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
41   *                                          + (2 + 2 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
42   *                                          + (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
43   *                                          ]
44   *   </li>
45   * </ul>
46   * </p>
47   * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub>
48   * are the four evaluations of the derivatives already computed during
49   * the step.</p>
50   *
51   * @see GillIntegrator
52   * @since 1.2
53   */
54  
55  class GillStepInterpolator
56    extends RungeKuttaStepInterpolator {
57  
58      /** First Gill coefficient. */
59      private static final double ONE_MINUS_INV_SQRT_2 = 1 - FastMath.sqrt(0.5);
60  
61      /** Second Gill coefficient. */
62      private static final double ONE_PLUS_INV_SQRT_2 = 1 + FastMath.sqrt(0.5);
63  
64      /** Serializable version identifier. */
65      private static final long serialVersionUID = 20111120L;
66  
67    /** Simple constructor.
68     * This constructor builds an instance that is not usable yet, the
69     * {@link
70     * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize}
71     * method should be called before using the instance in order to
72     * initialize the internal arrays. This constructor is used only
73     * in order to delay the initialization in some cases. The {@link
74     * RungeKuttaIntegrator} class uses the prototyping design pattern
75     * to create the step interpolators by cloning an uninitialized model
76     * and later initializing the copy.
77     */
78    public GillStepInterpolator() {
79    }
80  
81    /** Copy constructor.
82     * @param interpolator interpolator to copy from. The copy is a deep
83     * copy: its arrays are separated from the original arrays of the
84     * instance
85     */
86    public GillStepInterpolator(final GillStepInterpolator interpolator) {
87      super(interpolator);
88    }
89  
90    /** {@inheritDoc} */
91    @Override
92    protected StepInterpolator doCopy() {
93      return new GillStepInterpolator(this);
94    }
95  
96  
97    /** {@inheritDoc} */
98    @Override
99    protected void computeInterpolatedStateAndDerivatives(final double theta,
100                                           final double oneMinusThetaH) {
101 
102     final double twoTheta   = 2 * theta;
103     final double fourTheta2 = twoTheta * twoTheta;
104     final double coeffDot1  = theta * (twoTheta - 3) + 1;
105     final double cDot23     = twoTheta * (1 - theta);
106     final double coeffDot2  = cDot23  * ONE_MINUS_INV_SQRT_2;
107     final double coeffDot3  = cDot23  * ONE_PLUS_INV_SQRT_2;
108     final double coeffDot4  = theta * (twoTheta - 1);
109 
110     if ((previousState != null) && (theta <= 0.5)) {
111         final double s         = theta * h / 6.0;
112         final double c23       = s * (6 * theta - fourTheta2);
113         final double coeff1    = s * (6 - 9 * theta + fourTheta2);
114         final double coeff2    = c23  * ONE_MINUS_INV_SQRT_2;
115         final double coeff3    = c23  * ONE_PLUS_INV_SQRT_2;
116         final double coeff4    = s * (-3 * theta + fourTheta2);
117         for (int i = 0; i < interpolatedState.length; ++i) {
118             final double yDot1 = yDotK[0][i];
119             final double yDot2 = yDotK[1][i];
120             final double yDot3 = yDotK[2][i];
121             final double yDot4 = yDotK[3][i];
122             interpolatedState[i] =
123                     previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4;
124             interpolatedDerivatives[i] =
125                     coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
126         }
127     } else {
128         final double s      = oneMinusThetaH / 6.0;
129         final double c23    = s * (2 + twoTheta - fourTheta2);
130         final double coeff1 = s * (1 - 5 * theta + fourTheta2);
131         final double coeff2 = c23  * ONE_MINUS_INV_SQRT_2;
132         final double coeff3 = c23  * ONE_PLUS_INV_SQRT_2;
133         final double coeff4 = s * (1 + theta + fourTheta2);
134         for (int i = 0; i < interpolatedState.length; ++i) {
135             final double yDot1 = yDotK[0][i];
136             final double yDot2 = yDotK[1][i];
137             final double yDot3 = yDotK[2][i];
138             final double yDot4 = yDotK[3][i];
139             interpolatedState[i] =
140                     currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4;
141             interpolatedDerivatives[i] =
142                     coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
143         }
144     }
145 
146   }
147 
148 }