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.math.ode.nonstiff;
19  
20  import org.apache.commons.math.ode.DerivativeException;
21  import org.apache.commons.math.ode.sampling.AbstractStepInterpolator;
22  import org.apache.commons.math.ode.sampling.StepInterpolator;
23  
24  /**
25   * This class implements a step interpolator for the 3/8 fourth
26   * order Runge-Kutta integrator.
27   *
28   * <p>This interpolator allows to compute dense output inside the last
29   * step computed. The interpolation equation is consistent with the
30   * integration scheme :
31   *
32   * <pre>
33   *   y(t_n + theta h) = y (t_n + h)
34   *                    - (1 - theta) (h/8) [ (1 - 7 theta + 8 theta^2) y'_1
35   *                                      + 3 (1 +   theta - 4 theta^2) y'_2
36   *                                      + 3 (1 +   theta)             y'_3
37   *                                      +   (1 +   theta + 4 theta^2) y'_4
38   *                                        ]
39   * </pre>
40   *
41   * where theta belongs to [0 ; 1] and where y'_1 to y'_4 are the four
42   * evaluations of the derivatives already computed during the
43   * step.</p>
44   *
45   * @see ThreeEighthesIntegrator
46   * @version $Revision: 782432 $ $Date: 2009-06-07 15:08:26 -0400 (Sun, 07 Jun 2009) $
47   * @since 1.2
48   */
49  
50  class ThreeEighthesStepInterpolator
51    extends RungeKuttaStepInterpolator {
52      
53    /** Simple constructor.
54     * This constructor builds an instance that is not usable yet, the
55     * {@link AbstractStepInterpolator#reinitialize} method should be called
56     * before using the instance in order to initialize the internal arrays. This
57     * constructor is used only in order to delay the initialization in
58     * some cases. The {@link RungeKuttaIntegrator} class uses the
59     * prototyping design pattern to create the step interpolators by
60     * cloning an uninitialized model and latter initializing the copy.
61     */
62    public ThreeEighthesStepInterpolator() {
63    }
64  
65    /** Copy constructor.
66     * @param interpolator interpolator to copy from. The copy is a deep
67     * copy: its arrays are separated from the original arrays of the
68     * instance
69     */
70    public ThreeEighthesStepInterpolator(final ThreeEighthesStepInterpolator interpolator) {
71      super(interpolator);
72    }
73  
74    /** {@inheritDoc} */
75    @Override
76    protected StepInterpolator doCopy() {
77      return new ThreeEighthesStepInterpolator(this);
78    }
79  
80  
81    /** {@inheritDoc} */
82    @Override
83    protected void computeInterpolatedStateAndDerivatives(final double theta,
84                                            final double oneMinusThetaH)
85        throws DerivativeException {
86  
87        final double fourTheta2 = 4 * theta * theta;
88        final double s          = oneMinusThetaH / 8.0;
89        final double coeff1     = s * (1 - 7 * theta + 2 * fourTheta2);
90        final double coeff2     = 3 * s * (1 + theta - fourTheta2);
91        final double coeff3     = 3 * s * (1 + theta);
92        final double coeff4     = s * (1 + theta + fourTheta2);
93        final double coeffDot3  = 0.75 * theta;
94        final double coeffDot1  = coeffDot3 * (4 * theta - 5) + 1;
95        final double coeffDot2  = coeffDot3 * (5 - 6 * theta);
96        final double coeffDot4  = coeffDot3 * (2 * theta - 1);
97  
98        for (int i = 0; i < interpolatedState.length; ++i) {
99            final double yDot1 = yDotK[0][i];
100           final double yDot2 = yDotK[1][i];
101           final double yDot3 = yDotK[2][i];
102           final double yDot4 = yDotK[3][i];
103           interpolatedState[i] =
104               currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4;
105           interpolatedDerivatives[i] =
106               coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
107 
108       }
109 
110   }
111 
112   /** Serializable version identifier */
113   private static final long serialVersionUID = -3345024435978721931L;
114 
115 }