Home | History | Annotate | Download | only in nonstiff
      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.StepInterpolator;
     22 
     23 /**
     24  * This class implements a step interpolator for second order
     25  * Runge-Kutta integrator.
     26  *
     27  * <p>This interpolator computes dense output inside the last
     28  * step computed. The interpolation equation is consistent with the
     29  * integration scheme :
     30  *
     31  * <pre>
     32  *   y(t_n + theta h) = y (t_n + h) + (1-theta) h [theta y'_1 - (1+theta) y'_2]
     33  * </pre>
     34  *
     35  * where theta belongs to [0 ; 1] and where y'_1 and y'_2 are the two
     36  * evaluations of the derivatives already computed during the
     37  * step.</p>
     38  *
     39  * @see MidpointIntegrator
     40  * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 fvr. 2011) $
     41  * @since 1.2
     42  */
     43 
     44 class MidpointStepInterpolator
     45   extends RungeKuttaStepInterpolator {
     46 
     47     /** Serializable version identifier */
     48     private static final long serialVersionUID = -865524111506042509L;
     49 
     50   /** Simple constructor.
     51    * This constructor builds an instance that is not usable yet, the
     52    * {@link
     53    * org.apache.commons.math.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   public MidpointStepInterpolator() {
     62   }
     63 
     64   /** Copy constructor.
     65    * @param interpolator interpolator to copy from. The copy is a deep
     66    * copy: its arrays are separated from the original arrays of the
     67    * instance
     68    */
     69   public MidpointStepInterpolator(final MidpointStepInterpolator interpolator) {
     70     super(interpolator);
     71   }
     72 
     73   /** {@inheritDoc} */
     74   @Override
     75   protected StepInterpolator doCopy() {
     76     return new MidpointStepInterpolator(this);
     77   }
     78 
     79 
     80   /** {@inheritDoc} */
     81   @Override
     82   protected void computeInterpolatedStateAndDerivatives(final double theta,
     83                                           final double oneMinusThetaH)
     84     throws DerivativeException {
     85 
     86     final double coeff1    = oneMinusThetaH * theta;
     87     final double coeff2    = oneMinusThetaH * (1.0 + theta);
     88     final double coeffDot2 = 2 * theta;
     89     final double coeffDot1 = 1 - coeffDot2;
     90 
     91     for (int i = 0; i < interpolatedState.length; ++i) {
     92       final double yDot1 = yDotK[0][i];
     93       final double yDot2 = yDotK[1][i];
     94       interpolatedState[i] = currentState[i] + coeff1 * yDot1 - coeff2 * yDot2;
     95       interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2;
     96     }
     97 
     98   }
     99 
    100 }
    101