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