Home | History | Annotate | Download | only in sampling
      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.sampling;
     19 
     20 import java.io.IOException;
     21 import java.io.ObjectInput;
     22 import java.io.ObjectOutput;
     23 
     24 /** This class is a step interpolator that does nothing.
     25  *
     26  * <p>This class is used when the {@link StepHandler "step handler"}
     27  * set up by the user does not need step interpolation. It does not
     28  * recompute the state when {@link AbstractStepInterpolator#setInterpolatedTime
     29  * setInterpolatedTime} is called. This implies the interpolated state
     30  * is always the state at the end of the current step.</p>
     31  *
     32  * @see StepHandler
     33  *
     34  * @version $Revision: 1037327 $ $Date: 2010-11-20 21:57:37 +0100 (sam. 20 nov. 2010) $
     35  * @since 1.2
     36  */
     37 
     38 public class DummyStepInterpolator
     39   extends AbstractStepInterpolator {
     40 
     41   /** Serializable version identifier. */
     42   private static final long serialVersionUID = 1708010296707839488L;
     43 
     44   /** Current derivative. */
     45   private double[] currentDerivative;
     46 
     47   /** Simple constructor.
     48    * This constructor builds an instance that is not usable yet, the
     49    * <code>AbstractStepInterpolator.reinitialize</code> protected method
     50    * should be called before using the instance in order to initialize
     51    * the internal arrays. This constructor is used only in order to delay
     52    * the initialization in some cases. As an example, the {@link
     53    * org.apache.commons.math.ode.nonstiff.EmbeddedRungeKuttaIntegrator} uses
     54    * the prototyping design pattern to create the step interpolators by
     55    * cloning an uninitialized model and latter initializing the copy.
     56    */
     57   public DummyStepInterpolator() {
     58     super();
     59     currentDerivative = null;
     60   }
     61 
     62   /** Simple constructor.
     63    * @param y reference to the integrator array holding the state at
     64    * the end of the step
     65    * @param yDot reference to the integrator array holding the state
     66    * derivative at some arbitrary point within the step
     67    * @param forward integration direction indicator
     68    */
     69   public DummyStepInterpolator(final double[] y, final double[] yDot, final boolean forward) {
     70     super(y, forward);
     71     currentDerivative = yDot;
     72   }
     73 
     74   /** Copy constructor.
     75    * @param interpolator interpolator to copy from. The copy is a deep
     76    * copy: its arrays are separated from the original arrays of the
     77    * instance
     78    */
     79   public DummyStepInterpolator(final DummyStepInterpolator interpolator) {
     80     super(interpolator);
     81     currentDerivative = interpolator.currentDerivative.clone();
     82   }
     83 
     84   /** Really copy the finalized instance.
     85    * @return a copy of the finalized instance
     86    */
     87   @Override
     88   protected StepInterpolator doCopy() {
     89     return new DummyStepInterpolator(this);
     90   }
     91 
     92   /** Compute the state at the interpolated time.
     93    * In this class, this method does nothing: the interpolated state
     94    * is always the state at the end of the current step.
     95    * @param theta normalized interpolation abscissa within the step
     96    * (theta is zero at the previous time step and one at the current time step)
     97    * @param oneMinusThetaH time gap between the interpolated time and
     98    * the current time
     99    */
    100   @Override
    101   protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) {
    102       System.arraycopy(currentState,      0, interpolatedState,       0, currentState.length);
    103       System.arraycopy(currentDerivative, 0, interpolatedDerivatives, 0, currentDerivative.length);
    104   }
    105 
    106   /** Write the instance to an output channel.
    107    * @param out output channel
    108    * @exception IOException if the instance cannot be written
    109    */
    110   @Override
    111   public void writeExternal(final ObjectOutput out)
    112     throws IOException {
    113 
    114       // save the state of the base class
    115     writeBaseExternal(out);
    116 
    117     if (currentDerivative != null) {
    118         for (int i = 0; i < currentDerivative.length; ++i) {
    119             out.writeDouble(currentDerivative[i]);
    120         }
    121     }
    122 
    123   }
    124 
    125   /** Read the instance from an input channel.
    126    * @param in input channel
    127    * @exception IOException if the instance cannot be read
    128    */
    129   @Override
    130   public void readExternal(final ObjectInput in)
    131     throws IOException {
    132 
    133     // read the base class
    134     final double t = readBaseExternal(in);
    135 
    136     if (currentState == null) {
    137         currentDerivative = null;
    138     } else {
    139         currentDerivative  = new double[currentState.length];
    140         for (int i = 0; i < currentDerivative.length; ++i) {
    141             currentDerivative[i] = in.readDouble();
    142         }
    143     }
    144 
    145     // we can now set the interpolated time and state
    146     setInterpolatedTime(t);
    147 
    148   }
    149 
    150 }
    151