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.ode.sampling.StepInterpolator;
21  
22  /**
23   * This class implements a linear interpolator for step.
24   *
25   * <p>This interpolator computes dense output inside the last
26   * step computed. The interpolation equation is consistent with the
27   * integration scheme :
28   * <ul>
29   *   <li>Using reference point at step start:<br>
30   *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>) + &theta; h y'
31   *   </li>
32   *   <li>Using reference point at step end:<br>
33   *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) - (1-&theta;) h y'
34   *   </li>
35   * </ul>
36   *
37   * where &theta; belongs to [0 ; 1] and where y' is the evaluation of
38   * the derivatives already computed during the step.
39   *
40   * @see EulerIntegrator
41   * @since 1.2
42   */
43  
44  class EulerStepInterpolator
45    extends RungeKuttaStepInterpolator {
46  
47    /** Serializable version identifier. */
48    private static final long serialVersionUID = 20111120L;
49  
50    /** Simple constructor.
51     * This constructor builds an instance that is not usable yet, the
52     * {@link
53     * org.apache.commons.math4.legacy.ode.sampling.AbstractStepInterpolator#reinitialize}
54     * method should be called before using the instance in order to
55     * initialize the internal arrays. This constructor is used only
56     * in order to delay the initialization in some cases. The {@link
57     * RungeKuttaIntegrator} class uses the prototyping design pattern
58     * to create the step interpolators by cloning an uninitialized model
59     * and later initializing the copy.
60     */
61    // CHECKSTYLE: stop RedundantModifier
62    // the public modifier here is needed for serialization
63    public EulerStepInterpolator() {
64    }
65    // CHECKSTYLE: resume RedundantModifier
66  
67    /** Copy constructor.
68     * @param interpolator interpolator to copy from. The copy is a deep
69     * copy: its arrays are separated from the original arrays of the
70     * instance
71     */
72    EulerStepInterpolator(final EulerStepInterpolator interpolator) {
73      super(interpolator);
74    }
75  
76    /** {@inheritDoc} */
77    @Override
78    protected StepInterpolator doCopy() {
79      return new EulerStepInterpolator(this);
80    }
81  
82  
83    /** {@inheritDoc} */
84    @Override
85    protected void computeInterpolatedStateAndDerivatives(final double theta,
86                                            final double oneMinusThetaH) {
87        if (previousState != null && theta <= 0.5) {
88            for (int i = 0; i < interpolatedState.length; ++i) {
89                interpolatedState[i] = previousState[i] + theta * h * yDotK[0][i];
90            }
91            System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length);
92        } else {
93            for (int i = 0; i < interpolatedState.length; ++i) {
94                interpolatedState[i] = currentState[i] - oneMinusThetaH * yDotK[0][i];
95            }
96            System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length);
97        }
98    }
99  }