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 second order
26   * Runge-Kutta integrator.
27   *
28   * <p>This interpolator computes 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) + (1-theta) h [theta y'_1 - (1+theta) y'_2]
34   * </pre>
35   *
36   * where theta belongs to [0 ; 1] and where y'_1 and y'_2 are the two
37   * evaluations of the derivatives already computed during the
38   * step.</p>
39   *
40   * @see MidpointIntegrator
41   * @version $Revision: 782432 $ $Date: 2009-06-07 15:08:26 -0400 (Sun, 07 Jun 2009) $
42   * @since 1.2
43   */
44  
45  class MidpointStepInterpolator
46    extends RungeKuttaStepInterpolator {
47      
48    /** Simple constructor.
49     * This constructor builds an instance that is not usable yet, the
50     * {@link AbstractStepInterpolator#reinitialize} method should be called
51     * before using the instance in order to initialize the internal arrays. This
52     * constructor is used only in order to delay the initialization in
53     * some cases. The {@link RungeKuttaIntegrator} class uses the
54     * prototyping design pattern to create the step interpolators by
55     * cloning an uninitialized model and latter initializing the copy.
56     */
57    public MidpointStepInterpolator() {
58    }
59  
60    /** Copy constructor.
61     * @param interpolator interpolator to copy from. The copy is a deep
62     * copy: its arrays are separated from the original arrays of the
63     * instance
64     */
65    public MidpointStepInterpolator(final MidpointStepInterpolator interpolator) {
66      super(interpolator);
67    }
68  
69    /** {@inheritDoc} */
70    @Override
71    protected StepInterpolator doCopy() {
72      return new MidpointStepInterpolator(this);
73    }
74  
75  
76    /** {@inheritDoc} */
77    @Override
78    protected void computeInterpolatedStateAndDerivatives(final double theta,
79                                            final double oneMinusThetaH)
80      throws DerivativeException {
81  
82      final double coeff1    = oneMinusThetaH * theta;
83      final double coeff2    = oneMinusThetaH * (1.0 + theta);
84      final double coeffDot2 = 2 * theta;
85      final double coeffDot1 = 1 - coeffDot2;
86  
87      for (int i = 0; i < interpolatedState.length; ++i) {
88        final double yDot1 = yDotK[0][i];
89        final double yDot2 = yDotK[1][i];
90        interpolatedState[i] = currentState[i] + coeff1 * yDot1 - coeff2 * yDot2;
91        interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2;
92      }
93  
94    }
95  
96    /** Serializable version identifier */
97    private static final long serialVersionUID = -865524111506042509L;
98  
99  }