    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.math3.ode;
    019
    020
    021    /** This class converts second order differential equations to first
    022     * order ones.
    023     *
    024     * <p>This class is a wrapper around a {@link
    025     * SecondOrderDifferentialEquations} which allow to use a {@link
    026     * FirstOrderIntegrator} to integrate it.</p>
    027     *
    028     * <p>The transformation is done by changing the n dimension state
    029     * vector to a 2n dimension vector, where the first n components are
    030     * the initial state variables and the n last components are their
    031     * first time derivative. The first time derivative of this state
    032     * vector then really contains both the first and second time
    033     * derivative of the initial state vector, which can be handled by the
    034     * underlying second order equations set.</p>
    035     *
    036     * <p>One should be aware that the data is duplicated during the
    037     * transformation process and that for each call to {@link
    038     * #computeDerivatives computeDerivatives}, this wrapper does copy 4n
    039     * scalars : 2n before the call to {@link
    040     * SecondOrderDifferentialEquations#computeSecondDerivatives
    041     * computeSecondDerivatives} in order to dispatch the y state vector
    042     * into z and zDot, and 2n after the call to gather zDot and zDDot
    043     * into yDot. Since the underlying problem by itself perhaps also
    044     * needs to copy data and dispatch the arrays into domain objects,
    045     * this has an impact on both memory and CPU usage. The only way to
    046     * avoid this duplication is to perform the transformation at the
    047     * problem level, i.e. to implement the problem as a first order one
    048     * and then avoid using this class.</p>
    049     *
    050     * @see FirstOrderIntegrator
    051     * @see FirstOrderDifferentialEquations
    052     * @see SecondOrderDifferentialEquations
    053     * @version $Id: FirstOrderConverter.java 1416643 2012-12-03 19:37:14Z tn $
    054     * @since 1.2
    055     */
    056
    057    public class FirstOrderConverter implements FirstOrderDifferentialEquations {
    058
    059        /** Underlying second order equations set. */
    060        private final SecondOrderDifferentialEquations equations;
    061
    062        /** second order problem dimension. */
    063        private final int dimension;
    064
    065        /** state vector. */
    066        private final double[] z;
    067
    068        /** first time derivative of the state vector. */
    069        private final double[] zDot;
    070
    071        /** second time derivative of the state vector. */
    072        private final double[] zDDot;
    073
    074      /** Simple constructor.
    075       * Build a converter around a second order equations set.
    076       * @param equations second order equations set to convert
    077       */
    078      public FirstOrderConverter (final SecondOrderDifferentialEquations equations) {
    079          this.equations = equations;
    080          dimension      = equations.getDimension();
    081          z              = new double[dimension];
    082          zDot           = new double[dimension];
    083          zDDot          = new double[dimension];
    084      }
    085
    086      /** Get the dimension of the problem.
    087       * <p>The dimension of the first order problem is twice the
    088       * dimension of the underlying second order problem.</p>
    089       * @return dimension of the problem
    090       */
    091      public int getDimension() {
    092        return 2 * dimension;
    093      }
    094
    095      /** Get the current time derivative of the state vector.
    096       * @param t current value of the independent <I>time</I> variable
    097       * @param y array containing the current value of the state vector
    098       * @param yDot placeholder array where to put the time derivative of the state vector
    099       */
    100      public void computeDerivatives(final double t, final double[] y, final double[] yDot) {
    101
    102        // split the state vector in two
    103        System.arraycopy(y, 0,         z,    0, dimension);
    104        System.arraycopy(y, dimension, zDot, 0, dimension);
    105
    106        // apply the underlying equations set
    107        equations.computeSecondDerivatives(t, z, zDot, zDDot);
    108
    109        // build the result state derivative
    110        System.arraycopy(zDot,  0, yDot, 0,         dimension);
    111        System.arraycopy(zDDot, 0, yDot, dimension, dimension);
    112
    113      }
    114
    115    }