Home | History | Annotate | Download | only in ode
      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;
     19 
     20 import org.apache.commons.math.ode.DerivativeException;
     21 
     22 /** This class converts second order differential equations to first
     23  * order ones.
     24  *
     25  * <p>This class is a wrapper around a {@link
     26  * SecondOrderDifferentialEquations} which allow to use a {@link
     27  * FirstOrderIntegrator} to integrate it.</p>
     28  *
     29  * <p>The transformation is done by changing the n dimension state
     30  * vector to a 2n dimension vector, where the first n components are
     31  * the initial state variables and the n last components are their
     32  * first time derivative. The first time derivative of this state
     33  * vector then really contains both the first and second time
     34  * derivative of the initial state vector, which can be handled by the
     35  * underlying second order equations set.</p>
     36  *
     37  * <p>One should be aware that the data is duplicated during the
     38  * transformation process and that for each call to {@link
     39  * #computeDerivatives computeDerivatives}, this wrapper does copy 4n
     40  * scalars : 2n before the call to {@link
     41  * SecondOrderDifferentialEquations#computeSecondDerivatives
     42  * computeSecondDerivatives} in order to dispatch the y state vector
     43  * into z and zDot, and 2n after the call to gather zDot and zDDot
     44  * into yDot. Since the underlying problem by itself perhaps also
     45  * needs to copy data and dispatch the arrays into domain objects,
     46  * this has an impact on both memory and CPU usage. The only way to
     47  * avoid this duplication is to perform the transformation at the
     48  * problem level, i.e. to implement the problem as a first order one
     49  * and then avoid using this class.</p>
     50  *
     51  * @see FirstOrderIntegrator
     52  * @see FirstOrderDifferentialEquations
     53  * @see SecondOrderDifferentialEquations
     54  * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 fvr. 2011) $
     55  * @since 1.2
     56  */
     57 
     58 public class FirstOrderConverter implements FirstOrderDifferentialEquations {
     59 
     60     /** Underlying second order equations set. */
     61     private final SecondOrderDifferentialEquations equations;
     62 
     63     /** second order problem dimension. */
     64     private final int dimension;
     65 
     66     /** state vector. */
     67     private final double[] z;
     68 
     69     /** first time derivative of the state vector. */
     70     private final double[] zDot;
     71 
     72     /** second time derivative of the state vector. */
     73     private final double[] zDDot;
     74 
     75   /** Simple constructor.
     76    * Build a converter around a second order equations set.
     77    * @param equations second order equations set to convert
     78    */
     79   public FirstOrderConverter (final SecondOrderDifferentialEquations equations) {
     80       this.equations = equations;
     81       dimension      = equations.getDimension();
     82       z              = new double[dimension];
     83       zDot           = new double[dimension];
     84       zDDot          = new double[dimension];
     85   }
     86 
     87   /** Get the dimension of the problem.
     88    * <p>The dimension of the first order problem is twice the
     89    * dimension of the underlying second order problem.</p>
     90    * @return dimension of the problem
     91    */
     92   public int getDimension() {
     93     return 2 * dimension;
     94   }
     95 
     96   /** Get the current time derivative of the state vector.
     97    * @param t current value of the independent <I>time</I> variable
     98    * @param y array containing the current value of the state vector
     99    * @param yDot placeholder array where to put the time derivative of the state vector
    100    * @throws DerivativeException this exception is propagated to the caller if the
    101    * underlying user function triggers one
    102    */
    103   public void computeDerivatives(final double t, final double[] y, final double[] yDot)
    104       throws DerivativeException {
    105 
    106     // split the state vector in two
    107     System.arraycopy(y, 0,         z,    0, dimension);
    108     System.arraycopy(y, dimension, zDot, 0, dimension);
    109 
    110     // apply the underlying equations set
    111     equations.computeSecondDerivatives(t, z, zDot, zDDot);
    112 
    113     // build the result state derivative
    114     System.arraycopy(zDot,  0, yDot, 0,         dimension);
    115     System.arraycopy(zDDot, 0, yDot, dimension, dimension);
    116 
    117   }
    118 
    119 }
    120