FieldODEStateAndDerivative.java

  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.  *      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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.ode;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.util.MathArrays;

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

  25.  * @see FieldOrdinaryDifferentialEquation
  26.  * @see FieldSecondaryODE
  27.  * @see FieldODEIntegrator
  28.  * @param <T> the type of the field elements
  29.  */

  30. public class FieldODEStateAndDerivative<T extends CalculusFieldElement<T>> extends FieldODEState<T> {

  31.     /** Derivative of the primary state at time. */
  32.     private final T[] primaryDerivative;

  33.     /** Derivative of the secondary state at time. */
  34.     private final T[][] secondaryDerivative;

  35.     /** Simple constructor.
  36.      * <p>Calling this constructor is equivalent to call {@link
  37.      * #FieldODEStateAndDerivative(CalculusFieldElement, CalculusFieldElement[], CalculusFieldElement[],
  38.      * CalculusFieldElement[][], CalculusFieldElement[][]) FieldODEStateAndDerivative(time, state,
  39.      * derivative, null, null)}.</p>
  40.      * @param time time
  41.      * @param primaryState primary state at time
  42.      * @param primaryDerivative derivative of the primary state at time
  43.      */
  44.     public FieldODEStateAndDerivative(T time, T[] primaryState, T[] primaryDerivative) {
  45.         this(time, primaryState, primaryDerivative, null, null);
  46.     }

  47.     /** Simple constructor.
  48.      * @param time time
  49.      * @param primaryState primary state at time
  50.      * @param primaryDerivative derivative of the primary state at time
  51.      * @param secondaryState state at time (may be null)
  52.      * @param secondaryDerivative derivative of the state at time (may be null)
  53.      */
  54.     public FieldODEStateAndDerivative(T time, T[] primaryState, T[] primaryDerivative,
  55.                                       T[][] secondaryState, T[][] secondaryDerivative) {
  56.         super(time, primaryState, secondaryState);
  57.         this.primaryDerivative   = primaryDerivative.clone();
  58.         this.secondaryDerivative = copy(secondaryDerivative);
  59.     }

  60.     /** Get derivative of the primary state at time.
  61.      * @return derivative of the primary state at time
  62.      * @see #getSecondaryDerivative(int)
  63.      * @see #getCompleteDerivative()
  64.      */
  65.     public T[] getPrimaryDerivative() {
  66.         return primaryDerivative.clone();
  67.     }

  68.     /** Get derivative of the secondary state at time.
  69.      * @param index index of the secondary set as returned
  70.      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryODE)}
  71.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  72.      * @return derivative of the secondary state at time
  73.      * @see #getPrimaryDerivative()
  74.      * @see #getCompleteDerivative()
  75.      */
  76.     public T[] getSecondaryDerivative(final int index) {
  77.         return index == 0 ? primaryDerivative.clone() : secondaryDerivative[index - 1].clone();
  78.     }

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

  101. }