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.  *      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;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.ode.FieldEquationsMapper;
  25. import org.hipparchus.ode.FieldODEStateAndDerivative;
  26. import org.hipparchus.ode.nonstiff.interpolators.ClassicalRungeKuttaFieldStateInterpolator;
  27. import org.hipparchus.util.MathArrays;

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

  51. public class ClassicalRungeKuttaFieldIntegrator<T extends CalculusFieldElement<T>>
  52.     extends FixedStepRungeKuttaFieldIntegrator<T> {

  53.     /** Name of integration scheme. */
  54.     public static final String METHOD_NAME = ClassicalRungeKuttaIntegrator.METHOD_NAME;

  55.     /** Simple constructor.
  56.      * Build a fourth-order Runge-Kutta integrator with the given step.
  57.      * @param field field to which the time and state vector elements belong
  58.      * @param step integration step
  59.      */
  60.     public ClassicalRungeKuttaFieldIntegrator(final Field<T> field, final T step) {
  61.         super(field, METHOD_NAME, step);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public T[] getC() {
  66.         final T[] c = MathArrays.buildArray(getField(), 3);
  67.         c[0] = getField().getOne().newInstance(0.5);
  68.         c[1] = c[0];
  69.         c[2] = getField().getOne();
  70.         return c;
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public T[][] getA() {
  75.         final T[][] a = MathArrays.buildArray(getField(), 3, -1);
  76.         for (int i = 0; i < a.length; ++i) {
  77.             a[i] = MathArrays.buildArray(getField(), i + 1);
  78.         }
  79.         a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 2);
  80.         a[1][0] = getField().getZero();
  81.         a[1][1] = a[0][0];
  82.         a[2][0] = getField().getZero();
  83.         a[2][1] = getField().getZero();
  84.         a[2][2] = getField().getOne();
  85.         return a;
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public T[] getB() {
  90.         final T[] b = MathArrays.buildArray(getField(), 4);
  91.         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 6);
  92.         b[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 3);
  93.         b[2] = b[1];
  94.         b[3] = b[0];
  95.         return b;
  96.     }

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

  109. }