1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.ode.sampling;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.IOException;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.ObjectInput;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.ObjectOutput;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** This class is a step interpolator that does nothing.
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This class is used when the {@link StepHandler "step handler"}
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * set up by the user does not need step interpolation. It does not
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * recompute the state when {@link AbstractStepInterpolator#setInterpolatedTime
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * setInterpolatedTime} is called. This implies the interpolated state
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * is always the state at the end of the current step.</p>
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see StepHandler
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1037327 $ $Date: 2010-11-20 21:57:37 +0100 (sam. 20 nov. 2010) $
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class DummyStepInterpolator
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  extends AbstractStepInterpolator {
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Serializable version identifier. */
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final long serialVersionUID = 1708010296707839488L;
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Current derivative. */
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private double[] currentDerivative;
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * This constructor builds an instance that is not usable yet, the
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * <code>AbstractStepInterpolator.reinitialize</code> protected method
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * should be called before using the instance in order to initialize
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * the internal arrays. This constructor is used only in order to delay
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * the initialization in some cases. As an example, the {@link
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * org.apache.commons.math.ode.nonstiff.EmbeddedRungeKuttaIntegrator} uses
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * the prototyping design pattern to create the step interpolators by
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * cloning an uninitialized model and latter initializing the copy.
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public DummyStepInterpolator() {
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super();
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    currentDerivative = null;
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param y reference to the integrator array holding the state at
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * the end of the step
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param yDot reference to the integrator array holding the state
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * derivative at some arbitrary point within the step
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param forward integration direction indicator
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public DummyStepInterpolator(final double[] y, final double[] yDot, final boolean forward) {
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(y, forward);
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    currentDerivative = yDot;
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Copy constructor.
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param interpolator interpolator to copy from. The copy is a deep
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * copy: its arrays are separated from the original arrays of the
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * instance
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public DummyStepInterpolator(final DummyStepInterpolator interpolator) {
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(interpolator);
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    currentDerivative = interpolator.currentDerivative.clone();
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Really copy the finalized instance.
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return a copy of the finalized instance
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  protected StepInterpolator doCopy() {
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return new DummyStepInterpolator(this);
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Compute the state at the interpolated time.
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * In this class, this method does nothing: the interpolated state
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * is always the state at the end of the current step.
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param theta normalized interpolation abscissa within the step
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * (theta is zero at the previous time step and one at the current time step)
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param oneMinusThetaH time gap between the interpolated time and
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * the current time
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) {
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      System.arraycopy(currentState,      0, interpolatedState,       0, currentState.length);
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      System.arraycopy(currentDerivative, 0, interpolatedDerivatives, 0, currentDerivative.length);
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Write the instance to an output channel.
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param out output channel
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @exception IOException if the instance cannot be written
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public void writeExternal(final ObjectOutput out)
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    throws IOException {
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      // save the state of the base class
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    writeBaseExternal(out);
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    if (currentDerivative != null) {
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < currentDerivative.length; ++i) {
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            out.writeDouble(currentDerivative[i]);
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Read the instance from an input channel.
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param in input channel
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @exception IOException if the instance cannot be read
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public void readExternal(final ObjectInput in)
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    throws IOException {
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    // read the base class
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    final double t = readBaseExternal(in);
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    if (currentState == null) {
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        currentDerivative = null;
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    } else {
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        currentDerivative  = new double[currentState.length];
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < currentDerivative.length; ++i) {
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            currentDerivative[i] = in.readDouble();
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    // we can now set the interpolated time and state
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    setInterpolatedTime(t);
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
151