GillFieldIntegrator.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.  *      http://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.apache.commons.math4.legacy.ode.nonstiff;

  18. import org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.RealFieldElement;
  20. import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
  21. import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
  22. import org.apache.commons.math4.legacy.core.MathArrays;


  23. /**
  24.  * This class implements the Gill fourth order Runge-Kutta
  25.  * integrator for Ordinary Differential Equations .

  26.  * <p>This method is an explicit Runge-Kutta method, its Butcher-array
  27.  * is the following one :
  28.  * <pre>
  29.  *    0  |    0        0       0      0
  30.  *   1/2 |   1/2       0       0      0
  31.  *   1/2 | (q-1)/2  (2-q)/2    0      0
  32.  *    1  |    0       -q/2  (2+q)/2   0
  33.  *       |-------------------------------
  34.  *       |   1/6    (2-q)/6 (2+q)/6  1/6
  35.  * </pre>
  36.  * where q = sqrt(2)
  37.  *
  38.  * @see EulerFieldIntegrator
  39.  * @see ClassicalRungeKuttaFieldIntegrator
  40.  * @see MidpointFieldIntegrator
  41.  * @see ThreeEighthesFieldIntegrator
  42.  * @see LutherFieldIntegrator
  43.  * @param <T> the type of the field elements
  44.  * @since 3.6
  45.  */

  46. public class GillFieldIntegrator<T extends RealFieldElement<T>>
  47.     extends RungeKuttaFieldIntegrator<T> {

  48.     /** Simple constructor.
  49.      * Build a fourth-order Gill integrator with the given step.
  50.      * @param field field to which the time and state vector elements belong
  51.      * @param step integration step
  52.      */
  53.     public GillFieldIntegrator(final Field<T> field, final T step) {
  54.         super(field, "Gill", step);
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public T[] getC() {
  59.         final T[] c = MathArrays.buildArray(getField(), 3);
  60.         c[0] = fraction(1, 2);
  61.         c[1] = c[0];
  62.         c[2] = getField().getOne();
  63.         return c;
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public T[][] getA() {

  68.         final T two     = getField().getZero().add(2);
  69.         final T sqrtTwo = two.sqrt();

  70.         final T[][] a = MathArrays.buildArray(getField(), 3, -1);
  71.         for (int i = 0; i < a.length; ++i) {
  72.             a[i] = MathArrays.buildArray(getField(), i + 1);
  73.         }
  74.         a[0][0] = fraction(1, 2);
  75.         a[1][0] = sqrtTwo.subtract(1).multiply(0.5);
  76.         a[1][1] = sqrtTwo.subtract(2).multiply(-0.5);
  77.         a[2][0] = getField().getZero();
  78.         a[2][1] = sqrtTwo.multiply(-0.5);
  79.         a[2][2] = sqrtTwo.add(2).multiply(0.5);
  80.         return a;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public T[] getB() {

  85.         final T two     = getField().getZero().add(2);
  86.         final T sqrtTwo = two.sqrt();

  87.         final T[] b = MathArrays.buildArray(getField(), 4);
  88.         b[0] = fraction(1, 6);
  89.         b[1] = sqrtTwo.subtract(2).divide(-6);
  90.         b[2] = sqrtTwo.add(2).divide(6);
  91.         b[3] = b[0];

  92.         return b;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     protected GillFieldStepInterpolator<T>
  97.         createInterpolator(final boolean forward, T[][] yDotK,
  98.                            final FieldODEStateAndDerivative<T> globalPreviousState,
  99.                            final FieldODEStateAndDerivative<T> globalCurrentState,
  100.                            final FieldEquationsMapper<T> mapper) {
  101.         return new GillFieldStepInterpolator<>(getField(), forward, yDotK,
  102.                                                 globalPreviousState, globalCurrentState,
  103.                                                 globalPreviousState, globalCurrentState,
  104.                                                 mapper);
  105.     }
  106. }