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

  45. public class MidpointFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {

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

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public T[] getC() {
  57.         final T[] c = MathArrays.buildArray(getField(), 1);
  58.         c[0] = getField().getOne().multiply(0.5);
  59.         return c;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public T[][] getA() {
  64.         final T[][] a = MathArrays.buildArray(getField(), 1, 1);
  65.         a[0][0] = fraction(1, 2);
  66.         return a;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public T[] getB() {
  71.         final T[] b = MathArrays.buildArray(getField(), 2);
  72.         b[0] = getField().getZero();
  73.         b[1] = getField().getOne();
  74.         return b;
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     protected MidpointFieldStepInterpolator<T>
  79.         createInterpolator(final boolean forward, T[][] yDotK,
  80.                            final FieldODEStateAndDerivative<T> globalPreviousState,
  81.                            final FieldODEStateAndDerivative<T> globalCurrentState,
  82.                            final FieldEquationsMapper<T> mapper) {
  83.         return new MidpointFieldStepInterpolator<>(getField(), forward, yDotK,
  84.                                                     globalPreviousState, globalCurrentState,
  85.                                                     globalPreviousState, globalCurrentState,
  86.                                                     mapper);
  87.     }
  88. }