View Javadoc
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  
18  package org.apache.commons.math4.legacy.ode.nonstiff;
19  
20  import org.apache.commons.math4.legacy.core.Field;
21  import org.apache.commons.math4.legacy.core.RealFieldElement;
22  import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23  import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24  import org.apache.commons.math4.legacy.core.MathArrays;
25  
26  /**
27   * This class implements a second order Runge-Kutta integrator for
28   * Ordinary Differential Equations.
29   *
30   * <p>This method is an explicit Runge-Kutta method, its Butcher-array
31   * is the following one :
32   * <pre>
33   *    0  |  0    0
34   *   1/2 | 1/2   0
35   *       |----------
36   *       |  0    1
37   * </pre>
38   *
39   * @see EulerFieldIntegrator
40   * @see ClassicalRungeKuttaFieldIntegrator
41   * @see GillFieldIntegrator
42   * @see ThreeEighthesFieldIntegrator
43   * @see LutherFieldIntegrator
44   *
45   * @param <T> the type of the field elements
46   * @since 3.6
47   */
48  
49  public class MidpointFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
50  
51      /** Simple constructor.
52       * Build a midpoint integrator with the given step.
53       * @param field field to which the time and state vector elements belong
54       * @param step integration step
55       */
56      public MidpointFieldIntegrator(final Field<T> field, final T step) {
57          super(field, "midpoint", step);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public T[] getC() {
63          final T[] c = MathArrays.buildArray(getField(), 1);
64          c[0] = getField().getOne().multiply(0.5);
65          return c;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public T[][] getA() {
71          final T[][] a = MathArrays.buildArray(getField(), 1, 1);
72          a[0][0] = fraction(1, 2);
73          return a;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public T[] getB() {
79          final T[] b = MathArrays.buildArray(getField(), 2);
80          b[0] = getField().getZero();
81          b[1] = getField().getOne();
82          return b;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      protected MidpointFieldStepInterpolator<T>
88          createInterpolator(final boolean forward, T[][] yDotK,
89                             final FieldODEStateAndDerivative<T> globalPreviousState,
90                             final FieldODEStateAndDerivative<T> globalCurrentState,
91                             final FieldEquationsMapper<T> mapper) {
92          return new MidpointFieldStepInterpolator<>(getField(), forward, yDotK,
93                                                      globalPreviousState, globalCurrentState,
94                                                      globalPreviousState, globalCurrentState,
95                                                      mapper);
96      }
97  }