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 the classical fourth order Runge-Kutta
28   * integrator for Ordinary Differential Equations (it is the most
29   * often used Runge-Kutta method).
30   *
31   * <p>This method is an explicit Runge-Kutta method, its Butcher-array
32   * is the following one :
33   * <pre>
34   *    0  |  0    0    0    0
35   *   1/2 | 1/2   0    0    0
36   *   1/2 |  0   1/2   0    0
37   *    1  |  0    0    1    0
38   *       |--------------------
39   *       | 1/6  1/3  1/3  1/6
40   * </pre>
41   *
42   * @see EulerFieldIntegrator
43   * @see GillFieldIntegrator
44   * @see MidpointFieldIntegrator
45   * @see ThreeEighthesFieldIntegrator
46   * @see LutherFieldIntegrator
47   * @param <T> the type of the field elements
48   * @since 3.6
49   */
50  
51  public class ClassicalRungeKuttaFieldIntegrator<T extends RealFieldElement<T>>
52      extends RungeKuttaFieldIntegrator<T> {
53  
54      /** Simple constructor.
55       * Build a fourth-order Runge-Kutta integrator with the given step.
56       * @param field field to which the time and state vector elements belong
57       * @param step integration step
58       */
59      public ClassicalRungeKuttaFieldIntegrator(final Field<T> field, final T step) {
60          super(field, "classical Runge-Kutta", step);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public T[] getC() {
66          final T[] c = MathArrays.buildArray(getField(), 3);
67          c[0] = getField().getOne().multiply(0.5);
68          c[1] = c[0];
69          c[2] = getField().getOne();
70          return c;
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public T[][] getA() {
76          final T[][] a = MathArrays.buildArray(getField(), 3, -1);
77          for (int i = 0; i < a.length; ++i) {
78              a[i] = MathArrays.buildArray(getField(), i + 1);
79          }
80          a[0][0] = fraction(1, 2);
81          a[1][0] = getField().getZero();
82          a[1][1] = a[0][0];
83          a[2][0] = getField().getZero();
84          a[2][1] = getField().getZero();
85          a[2][2] = getField().getOne();
86          return a;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public T[] getB() {
92          final T[] b = MathArrays.buildArray(getField(), 4);
93          b[0] = fraction(1, 6);
94          b[1] = fraction(1, 3);
95          b[2] = b[1];
96          b[3] = b[0];
97          return b;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     protected ClassicalRungeKuttaFieldStepInterpolator<T>
103         createInterpolator(final boolean forward, T[][] yDotK,
104                            final FieldODEStateAndDerivative<T> globalPreviousState,
105                            final FieldODEStateAndDerivative<T> globalCurrentState,
106                            final FieldEquationsMapper<T> mapper) {
107         return new ClassicalRungeKuttaFieldStepInterpolator<>(getField(), forward, yDotK,
108                                                                globalPreviousState, globalCurrentState,
109                                                                globalPreviousState, globalCurrentState,
110                                                                mapper);
111     }
112 }