001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math4.legacy.ode.nonstiff;
019
020import org.apache.commons.math4.legacy.core.Field;
021import org.apache.commons.math4.legacy.core.RealFieldElement;
022import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
023import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
024import org.apache.commons.math4.legacy.core.MathArrays;
025
026/**
027 * This class implements the classical fourth order Runge-Kutta
028 * integrator for Ordinary Differential Equations (it is the most
029 * often used Runge-Kutta method).
030 *
031 * <p>This method is an explicit Runge-Kutta method, its Butcher-array
032 * is the following one :
033 * <pre>
034 *    0  |  0    0    0    0
035 *   1/2 | 1/2   0    0    0
036 *   1/2 |  0   1/2   0    0
037 *    1  |  0    0    1    0
038 *       |--------------------
039 *       | 1/6  1/3  1/3  1/6
040 * </pre>
041 *
042 * @see EulerFieldIntegrator
043 * @see GillFieldIntegrator
044 * @see MidpointFieldIntegrator
045 * @see ThreeEighthesFieldIntegrator
046 * @see LutherFieldIntegrator
047 * @param <T> the type of the field elements
048 * @since 3.6
049 */
050
051public class ClassicalRungeKuttaFieldIntegrator<T extends RealFieldElement<T>>
052    extends RungeKuttaFieldIntegrator<T> {
053
054    /** Simple constructor.
055     * Build a fourth-order Runge-Kutta integrator with the given step.
056     * @param field field to which the time and state vector elements belong
057     * @param step integration step
058     */
059    public ClassicalRungeKuttaFieldIntegrator(final Field<T> field, final T step) {
060        super(field, "classical Runge-Kutta", step);
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public T[] getC() {
066        final T[] c = MathArrays.buildArray(getField(), 3);
067        c[0] = getField().getOne().multiply(0.5);
068        c[1] = c[0];
069        c[2] = getField().getOne();
070        return c;
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public T[][] getA() {
076        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
077        for (int i = 0; i < a.length; ++i) {
078            a[i] = MathArrays.buildArray(getField(), i + 1);
079        }
080        a[0][0] = fraction(1, 2);
081        a[1][0] = getField().getZero();
082        a[1][1] = a[0][0];
083        a[2][0] = getField().getZero();
084        a[2][1] = getField().getZero();
085        a[2][2] = getField().getOne();
086        return a;
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public T[] getB() {
092        final T[] b = MathArrays.buildArray(getField(), 4);
093        b[0] = fraction(1, 6);
094        b[1] = fraction(1, 3);
095        b[2] = b[1];
096        b[3] = b[0];
097        return b;
098    }
099
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}