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