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