AdamsFieldStateInterpolator.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.nonstiff.interpolators;

  22. import java.util.Arrays;

  23. import org.hipparchus.CalculusFieldElement;
  24. import org.hipparchus.linear.Array2DRowFieldMatrix;
  25. import org.hipparchus.ode.FieldEquationsMapper;
  26. import org.hipparchus.ode.FieldODEStateAndDerivative;
  27. import org.hipparchus.ode.nonstiff.AdamsBashforthFieldIntegrator;
  28. import org.hipparchus.ode.nonstiff.AdamsMoultonFieldIntegrator;
  29. import org.hipparchus.ode.sampling.AbstractFieldODEStateInterpolator;
  30. import org.hipparchus.util.MathArrays;

  31. /**
  32.  * This class implements an interpolator for Adams integrators using Nordsieck representation.
  33.  *
  34.  * <p>This interpolator computes dense output around the current point.
  35.  * The interpolation equation is based on Taylor series formulas.
  36.  *
  37.  * @see AdamsBashforthFieldIntegrator
  38.  * @see AdamsMoultonFieldIntegrator
  39.  * @param <T> the type of the field elements
  40.  */

  41. public class AdamsFieldStateInterpolator<T extends CalculusFieldElement<T>> extends AbstractFieldODEStateInterpolator<T> {

  42.     /** Step size used in the first scaled derivative and Nordsieck vector. */
  43.     private T scalingH;

  44.     /** Reference state.
  45.      * <p>Sometimes, the reference state is the same as globalPreviousState,
  46.      * sometimes it is the same as globalCurrentState, so we use a separate
  47.      * field to avoid any confusion.
  48.      * </p>
  49.      */
  50.     private final FieldODEStateAndDerivative<T> reference;

  51.     /** First scaled derivative. */
  52.     private final T[] scaled;

  53.     /** Nordsieck vector. */
  54.     private final Array2DRowFieldMatrix<T> nordsieck;

  55.     /** Simple constructor.
  56.      * @param stepSize step size used in the scaled and Nordsieck arrays
  57.      * @param reference reference state from which Taylor expansion are estimated
  58.      * @param scaled first scaled derivative
  59.      * @param nordsieck Nordsieck vector
  60.      * @param isForward integration direction indicator
  61.      * @param globalPreviousState start of the global step
  62.      * @param globalCurrentState end of the global step
  63.      * @param equationsMapper mapper for ODE equations primary and secondary components
  64.      */
  65.     public AdamsFieldStateInterpolator(final T stepSize, final FieldODEStateAndDerivative<T> reference,
  66.                                        final T[] scaled, final Array2DRowFieldMatrix<T> nordsieck,
  67.                                        final boolean isForward,
  68.                                        final FieldODEStateAndDerivative<T> globalPreviousState,
  69.                                        final FieldODEStateAndDerivative<T> globalCurrentState,
  70.                                        final FieldEquationsMapper<T> equationsMapper) {
  71.         this(stepSize, reference, scaled, nordsieck, isForward, globalPreviousState, globalCurrentState,
  72.                 globalPreviousState, globalCurrentState, equationsMapper);
  73.     }

  74.     /** Simple constructor.
  75.      * @param stepSize step size used in the scaled and Nordsieck arrays
  76.      * @param reference reference state from which Taylor expansion are estimated
  77.      * @param scaled first scaled derivative
  78.      * @param nordsieck Nordsieck vector
  79.      * @param isForward integration direction indicator
  80.      * @param globalPreviousState start of the global step
  81.      * @param globalCurrentState end of the global step
  82.      * @param softPreviousState start of the restricted step
  83.      * @param softCurrentState end of the restricted step
  84.      * @param equationsMapper mapper for ODE equations primary and secondary components
  85.      */
  86.     private AdamsFieldStateInterpolator(final T stepSize, final FieldODEStateAndDerivative<T> reference,
  87.                                         final T[] scaled, final Array2DRowFieldMatrix<T> nordsieck,
  88.                                         final boolean isForward,
  89.                                         final FieldODEStateAndDerivative<T> globalPreviousState,
  90.                                         final FieldODEStateAndDerivative<T> globalCurrentState,
  91.                                         final FieldODEStateAndDerivative<T> softPreviousState,
  92.                                         final FieldODEStateAndDerivative<T> softCurrentState,
  93.                                         final FieldEquationsMapper<T> equationsMapper) {
  94.         super(isForward, globalPreviousState, globalCurrentState,
  95.               softPreviousState, softCurrentState, equationsMapper);
  96.         this.scalingH  = stepSize;
  97.         this.reference = reference;
  98.         this.scaled    = scaled.clone();
  99.         this.nordsieck = new Array2DRowFieldMatrix<>(nordsieck.getData(), false);
  100.     }

  101.     /** Create a new instance.
  102.      * @param newForward integration direction indicator
  103.      * @param newGlobalPreviousState start of the global step
  104.      * @param newGlobalCurrentState end of the global step
  105.      * @param newSoftPreviousState start of the restricted step
  106.      * @param newSoftCurrentState end of the restricted step
  107.      * @param newMapper equations mapper for the all equations
  108.      * @return a new instance
  109.      */
  110.     @Override
  111.     protected AdamsFieldStateInterpolator<T> create(boolean newForward,
  112.                                                     FieldODEStateAndDerivative<T> newGlobalPreviousState,
  113.                                                     FieldODEStateAndDerivative<T> newGlobalCurrentState,
  114.                                                     FieldODEStateAndDerivative<T> newSoftPreviousState,
  115.                                                     FieldODEStateAndDerivative<T> newSoftCurrentState,
  116.                                                     FieldEquationsMapper<T> newMapper) {
  117.         return new AdamsFieldStateInterpolator<>(scalingH, reference, scaled, nordsieck,
  118.                                                   newForward,
  119.                                                   newGlobalPreviousState, newGlobalCurrentState,
  120.                                                   newSoftPreviousState, newSoftCurrentState,
  121.                                                   newMapper);

  122.     }

  123.     /** Get the first scaled derivative.
  124.      * @return first scaled derivative
  125.      */
  126.     public T[] getScaled() {
  127.         return scaled.clone();
  128.     }

  129.     /** Get the Nordsieck vector.
  130.      * @return Nordsieck vector
  131.      */
  132.     public Array2DRowFieldMatrix<T> getNordsieck() {
  133.         return new Array2DRowFieldMatrix<>(nordsieck.getData());
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> equationsMapper,
  138.                                                                                    final T time, final T theta,
  139.                                                                                    final T thetaH, final T oneMinusThetaH) {
  140.         return taylor(equationsMapper, reference, time, scalingH, scaled, nordsieck);
  141.     }

  142.     /** Estimate state by applying Taylor formula.
  143.      * @param equationsMapper mapper for ODE equations primary and secondary components
  144.      * @param reference reference state
  145.      * @param time time at which state must be estimated
  146.      * @param stepSize step size used in the scaled and Nordsieck arrays
  147.      * @param scaled first scaled derivative
  148.      * @param nordsieck Nordsieck vector
  149.      * @return estimated state
  150.      * @param <S> the type of the field elements
  151.      */
  152.     public static <S extends CalculusFieldElement<S>> FieldODEStateAndDerivative<S> taylor(final FieldEquationsMapper<S> equationsMapper,
  153.                                                                                            final FieldODEStateAndDerivative<S> reference,
  154.                                                                                            final S time, final S stepSize,
  155.                                                                                            final S[] scaled,
  156.                                                                                            final Array2DRowFieldMatrix<S> nordsieck) {

  157.         final S x = time.subtract(reference.getTime());
  158.         final S normalizedAbscissa = x.divide(stepSize);

  159.         S[] stateVariation = MathArrays.buildArray(time.getField(), scaled.length);
  160.         Arrays.fill(stateVariation, time.getField().getZero());
  161.         S[] estimatedDerivatives = MathArrays.buildArray(time.getField(), scaled.length);
  162.         Arrays.fill(estimatedDerivatives, time.getField().getZero());

  163.         // apply Taylor formula from high order to low order,
  164.         // for the sake of numerical accuracy
  165.         final S[][] nData = nordsieck.getDataRef();
  166.         for (int i = nData.length - 1; i >= 0; --i) {
  167.             final int order = i + 2;
  168.             final S[] nDataI = nData[i];
  169.             final S power = normalizedAbscissa.pow(order);
  170.             for (int j = 0; j < nDataI.length; ++j) {
  171.                 final S d = nDataI[j].multiply(power);
  172.                 stateVariation[j]          = stateVariation[j].add(d);
  173.                 estimatedDerivatives[j] = estimatedDerivatives[j].add(d.multiply(order));
  174.             }
  175.         }

  176.         S[] estimatedState = reference.getCompleteState();
  177.         for (int j = 0; j < stateVariation.length; ++j) {
  178.             stateVariation[j] = stateVariation[j].add(scaled[j].multiply(normalizedAbscissa));
  179.             estimatedState[j] = estimatedState[j].add(stateVariation[j]);
  180.             estimatedDerivatives[j] =
  181.                 estimatedDerivatives[j].add(scaled[j].multiply(normalizedAbscissa)).divide(x);
  182.         }

  183.         return equationsMapper.mapStateAndDerivative(time, estimatedState, estimatedDerivatives);

  184.     }

  185. }