ClassicalRungeKuttaFieldIntegrator.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 classical fourth order Runge-Kutta
  25.  * integrator for Ordinary Differential Equations (it is the most
  26.  * often used Runge-Kutta method).
  27.  *
  28.  * <p>This method is an explicit Runge-Kutta method, its Butcher-array
  29.  * is the following one :
  30.  * <pre>
  31.  *    0  |  0    0    0    0
  32.  *   1/2 | 1/2   0    0    0
  33.  *   1/2 |  0   1/2   0    0
  34.  *    1  |  0    0    1    0
  35.  *       |--------------------
  36.  *       | 1/6  1/3  1/3  1/6
  37.  * </pre>
  38.  *
  39.  * @see EulerFieldIntegrator
  40.  * @see GillFieldIntegrator
  41.  * @see MidpointFieldIntegrator
  42.  * @see ThreeEighthesFieldIntegrator
  43.  * @see LutherFieldIntegrator
  44.  * @param <T> the type of the field elements
  45.  * @since 3.6
  46.  */

  47. public class ClassicalRungeKuttaFieldIntegrator<T extends RealFieldElement<T>>
  48.     extends RungeKuttaFieldIntegrator<T> {

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

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

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public T[][] getA() {
  69.         final T[][] a = MathArrays.buildArray(getField(), 3, -1);
  70.         for (int i = 0; i < a.length; ++i) {
  71.             a[i] = MathArrays.buildArray(getField(), i + 1);
  72.         }
  73.         a[0][0] = fraction(1, 2);
  74.         a[1][0] = getField().getZero();
  75.         a[1][1] = a[0][0];
  76.         a[2][0] = getField().getZero();
  77.         a[2][1] = getField().getZero();
  78.         a[2][2] = getField().getOne();
  79.         return a;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public T[] getB() {
  84.         final T[] b = MathArrays.buildArray(getField(), 4);
  85.         b[0] = fraction(1, 6);
  86.         b[1] = fraction(1, 3);
  87.         b[2] = b[1];
  88.         b[3] = b[0];
  89.         return b;
  90.     }

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