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