FieldODEState.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.

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

  31. public class FieldODEState<T extends CalculusFieldElement<T>> {

  32.     /** Time. */
  33.     private final T time;

  34.     /** Primary state at time. */
  35.     private final T[] primaryState;

  36.     /** Secondary state at time. */
  37.     private final T[][] secondaryState;

  38.     /** Complete dimension. */
  39.     private final int completeDimension;

  40.     /** Simple constructor.
  41.      * <p>Calling this constructor is equivalent to call {@link
  42.      * #FieldODEState(CalculusFieldElement, CalculusFieldElement[], CalculusFieldElement[][])
  43.      * FieldODEState(time, state, null)}.</p>
  44.      * @param time time
  45.      * @param primaryState primary state at time
  46.      */
  47.     public FieldODEState(T time, T[] primaryState) {
  48.         this(time, primaryState, null);
  49.     }

  50.     /** Simple constructor.
  51.      * @param time time
  52.      * @param primaryState primary state at time
  53.      * @param secondaryState secondary state at time (may be null)
  54.      */
  55.     public FieldODEState(T time, T[] primaryState, T[][] secondaryState) {

  56.         this.time           = time;
  57.         this.primaryState   = primaryState.clone();
  58.         this.secondaryState = copy(secondaryState);

  59.         // compute once and for all the complete dimension
  60.         int dimension = primaryState.length;
  61.         if (secondaryState != null) {
  62.             for (final T[] secondary : secondaryState) {
  63.                 dimension += secondary.length;
  64.             }
  65.         }
  66.         this.completeDimension = dimension;

  67.     }

  68.     /** Copy a two-dimensions array.
  69.      * @param original original array (may be null)
  70.      * @return copied array or null if original array was null
  71.      */
  72.     protected T[][] copy(final T[][] original) {

  73.         // special handling of null arrays
  74.         if (original == null) {
  75.             return null; // NOPMD
  76.         }

  77.         // allocate the array
  78.         final T[][] copied = MathArrays.buildArray(time.getField(), original.length, -1);

  79.         // copy content
  80.         for (int i = 0; i < original.length; ++i) {
  81.             copied[i] = original[i].clone();
  82.         }

  83.         return copied;

  84.     }

  85.     /** Get time.
  86.      * @return time
  87.      */
  88.     public T getTime() {
  89.         return time;
  90.     }

  91.     /** Get primary state dimension.
  92.      * @return primary state dimension
  93.      * @see #getSecondaryStateDimension(int)
  94.      * @see #getCompleteStateDimension()
  95.      */
  96.     public int getPrimaryStateDimension() {
  97.         return primaryState.length;
  98.     }

  99.     /** Get primary state at time.
  100.      * @return primary state at time
  101.      * @see #getSecondaryState(int)
  102.      * @see #getCompleteState()
  103.      */
  104.     public T[] getPrimaryState() {
  105.         return primaryState.clone();
  106.     }

  107.     /** Get the number of secondary states.
  108.      * @return number of secondary states.
  109.      */
  110.     public int getNumberOfSecondaryStates() {
  111.         return secondaryState == null ? 0 : secondaryState.length;
  112.     }

  113.     /** Get secondary state dimension.
  114.      * @param index index of the secondary set as returned
  115.      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryODE)}
  116.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  117.      * @return secondary state dimension
  118.      */
  119.     public int getSecondaryStateDimension(final int index) {
  120.         return index == 0 ? primaryState.length : secondaryState[index - 1].length;
  121.     }

  122.     /** Get secondary state at time.
  123.      * @param index index of the secondary set as returned
  124.      * by {@link FieldExpandableODE#addSecondaryEquations(FieldSecondaryODE)}
  125.      * (beware index 0 corresponds to primary state, secondary states start at 1)
  126.      * @return secondary state at time
  127.      */
  128.     public T[] getSecondaryState(final int index) {
  129.         return index == 0 ? primaryState.clone() : secondaryState[index - 1].clone();
  130.     }

  131.     /** Return the dimension of the complete set of equations.
  132.      * <p>
  133.      * The complete set of equations correspond to the primary set plus all secondary sets.
  134.      * </p>
  135.      * @return dimension of the complete set of equations
  136.      */
  137.     public int getCompleteStateDimension() {
  138.         return completeDimension;
  139.     }

  140.     /** Get complete state at time.
  141.      * @return complete state at time, starting with
  142.      * {@link #getPrimaryState() primary state}, followed
  143.      * by all {@link #getSecondaryState(int) secondary states} in
  144.      * increasing index order
  145.      * @see #getPrimaryState()
  146.      * @see #getSecondaryState(int)
  147.      */
  148.     public T[] getCompleteState() {
  149.         final T[] completeState = MathArrays.buildArray(time.getField(), getCompleteStateDimension());
  150.         System.arraycopy(primaryState, 0, completeState, 0, primaryState.length);
  151.         int offset = primaryState.length;
  152.         if (secondaryState != null) {
  153.             for (int index = 0; index < secondaryState.length; ++index) {
  154.                 System.arraycopy(secondaryState[index], 0,
  155.                                  completeState, offset,
  156.                                  secondaryState[index].length);
  157.                 offset += secondaryState[index].length;
  158.             }
  159.         }
  160.         return completeState;
  161.     }

  162. }