ODEStateAndDerivative.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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.  *      https://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. package org.hipparchus.ode;

  18. /** Container for time, main and secondary state vectors as well as their derivatives.

  19.  * @see OrdinaryDifferentialEquation
  20.  * @see SecondaryODE
  21.  * @see ODEIntegrator
  22.  */

  23. public class ODEStateAndDerivative extends ODEState {

  24.     /** Serializable UID. */
  25.     private static final long serialVersionUID = 20160408L;

  26.     /** Derivative of the primary state at time. */
  27.     private final double[] primaryDerivative;

  28.     /** Derivative of the secondary state at time. */
  29.     private final double[][] secondaryDerivative;

  30.     /** Simple constructor.
  31.      * <p>Calling this constructor is equivalent to call {@link
  32.      * #ODEStateAndDerivative(double, double[], double[],
  33.      * double[][], double[][]) ODEStateAndDerivative(time, state,
  34.      * derivative, null, null)}.</p>
  35.      * @param time time
  36.      * @param primaryState primary state at time
  37.      * @param primaryDerivative derivative of the primary state at time
  38.      */
  39.     public ODEStateAndDerivative(double time, double[] primaryState, double[] primaryDerivative) {
  40.         this(time, primaryState, primaryDerivative, null, null);
  41.     }

  42.     /** Simple constructor.
  43.      * @param time time
  44.      * @param primaryState primary state at time
  45.      * @param primaryDerivative derivative of the primary state at time
  46.      * @param secondaryState state at time (may be null)
  47.      * @param secondaryDerivative derivative of the state at time (may be null)
  48.      */
  49.     public ODEStateAndDerivative(double time, double[] primaryState, double[] primaryDerivative,
  50.                                  double[][] secondaryState, double[][] secondaryDerivative) {
  51.         super(time, primaryState, secondaryState);
  52.         this.primaryDerivative   = primaryDerivative.clone();
  53.         this.secondaryDerivative = copy(secondaryDerivative);
  54.     }

  55.     /** Get derivative of the primary state at time.
  56.      * @return derivative of the primary state at time
  57.      * @see #getSecondaryDerivative(int)
  58.      * @see #getCompleteDerivative()
  59.      */
  60.     public double[] getPrimaryDerivative() {
  61.         return primaryDerivative.clone();
  62.     }

  63.     /** Get derivative of the secondary state at time.
  64.      * @param index index of the secondary set as returned
  65.      * by {@link ExpandableODE#addSecondaryEquations(SecondaryODE)}
  66.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  67.      * @return derivative of the secondary state at time
  68.      * @see #getPrimaryDerivative()
  69.      * @see #getCompleteDerivative()
  70.      */
  71.     public double[] getSecondaryDerivative(final int index) {
  72.         return index == 0 ? primaryDerivative.clone() : secondaryDerivative[index - 1].clone();
  73.     }

  74.     /** Get complete derivative at time.
  75.      * @return complete derivative at time, starting with
  76.      * {@link #getPrimaryDerivative() primary derivative}, followed
  77.      * by all {@link #getSecondaryDerivative(int) secondary derivatives} in
  78.      * increasing index order
  79.      * @see #getPrimaryDerivative()
  80.      * @see #getSecondaryDerivative(int)
  81.      */
  82.     public double[] getCompleteDerivative() {
  83.         final double[] completeDerivative = new double[getCompleteStateDimension()];
  84.         System.arraycopy(primaryDerivative, 0, completeDerivative, 0, primaryDerivative.length);
  85.         int offset = primaryDerivative.length;
  86.         if (secondaryDerivative != null) {
  87.             for (int index = 0; index < secondaryDerivative.length; ++index) {
  88.                 System.arraycopy(secondaryDerivative[index], 0,
  89.                                  completeDerivative, offset,
  90.                                  secondaryDerivative[index].length);
  91.                 offset += secondaryDerivative[index].length;
  92.             }
  93.         }
  94.         return completeDerivative;
  95.     }

  96. }