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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode.nonstiff;
24  
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.Field;
27  import org.hipparchus.ode.FieldEquationsMapper;
28  import org.hipparchus.ode.FieldODEStateAndDerivative;
29  import org.hipparchus.util.MathArrays;
30  
31  /**
32   * This class implements the classical fourth order Runge-Kutta
33   * integrator for Ordinary Differential Equations (it is the most
34   * often used Runge-Kutta method).
35   *
36   * <p>This method is an explicit Runge-Kutta method, its Butcher-array
37   * is the following one :</p>
38   * <pre>
39   *    0  |  0    0    0    0
40   *   1/2 | 1/2   0    0    0
41   *   1/2 |  0   1/2   0    0
42   *    1  |  0    0    1    0
43   *       |--------------------
44   *       | 1/6  1/3  1/3  1/6
45   * </pre>
46   *
47   * @see EulerFieldIntegrator
48   * @see GillFieldIntegrator
49   * @see MidpointFieldIntegrator
50   * @see ThreeEighthesFieldIntegrator
51   * @see LutherFieldIntegrator
52   * @param <T> the type of the field elements
53   */
54  
55  public class ClassicalRungeKuttaFieldIntegrator<T extends CalculusFieldElement<T>>
56      extends RungeKuttaFieldIntegrator<T> {
57  
58      /** Name of integration scheme. */
59      public static final String METHOD_NAME = ClassicalRungeKuttaIntegrator.METHOD_NAME;
60  
61      /** Simple constructor.
62       * Build a fourth-order Runge-Kutta integrator with the given step.
63       * @param field field to which the time and state vector elements belong
64       * @param step integration step
65       */
66      public ClassicalRungeKuttaFieldIntegrator(final Field<T> field, final T step) {
67          super(field, METHOD_NAME, step);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public T[] getC() {
73          final T[] c = MathArrays.buildArray(getField(), 3);
74          c[0] = getField().getOne().newInstance(0.5);
75          c[1] = c[0];
76          c[2] = getField().getOne();
77          return c;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public T[][] getA() {
83          final T[][] a = MathArrays.buildArray(getField(), 3, -1);
84          for (int i = 0; i < a.length; ++i) {
85              a[i] = MathArrays.buildArray(getField(), i + 1);
86          }
87          a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 2);
88          a[1][0] = getField().getZero();
89          a[1][1] = a[0][0];
90          a[2][0] = getField().getZero();
91          a[2][1] = getField().getZero();
92          a[2][2] = getField().getOne();
93          return a;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public T[] getB() {
99          final T[] b = MathArrays.buildArray(getField(), 4);
100         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 6);
101         b[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 3);
102         b[2] = b[1];
103         b[3] = b[0];
104         return b;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     protected ClassicalRungeKuttaFieldStateInterpolator<T>
110         createInterpolator(final boolean forward, T[][] yDotK,
111                            final FieldODEStateAndDerivative<T> globalPreviousState,
112                            final FieldODEStateAndDerivative<T> globalCurrentState,
113                            final FieldEquationsMapper<T> mapper) {
114         return new ClassicalRungeKuttaFieldStateInterpolator<T>(getField(), forward, yDotK,
115                                                                globalPreviousState, globalCurrentState,
116                                                                globalPreviousState, globalCurrentState,
117                                                                mapper);
118     }
119 
120 }