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.math3.ode.nonstiff;
019
020import org.apache.commons.math3.Field;
021import org.apache.commons.math3.RealFieldElement;
022import org.apache.commons.math3.ode.FieldEquationsMapper;
023import org.apache.commons.math3.ode.FieldODEStateAndDerivative;
024import org.apache.commons.math3.util.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 * </p>
042 *
043 * @see EulerFieldIntegrator
044 * @see GillFieldIntegrator
045 * @see MidpointFieldIntegrator
046 * @see ThreeEighthesFieldIntegrator
047 * @see LutherFieldIntegrator
048 * @param <T> the type of the field elements
049 * @since 3.6
050 */
051
052public class ClassicalRungeKuttaFieldIntegrator<T extends RealFieldElement<T>>
053    extends RungeKuttaFieldIntegrator<T> {
054
055    /** Simple constructor.
056     * Build a fourth-order Runge-Kutta integrator with the given step.
057     * @param field field to which the time and state vector elements belong
058     * @param step integration step
059     */
060    public ClassicalRungeKuttaFieldIntegrator(final Field<T> field, final T step) {
061        super(field, "classical Runge-Kutta", step);
062    }
063
064    /** {@inheritDoc} */
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    public T[][] getA() {
075        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
076        for (int i = 0; i < a.length; ++i) {
077            a[i] = MathArrays.buildArray(getField(), i + 1);
078        }
079        a[0][0] = fraction(1, 2);
080        a[1][0] = getField().getZero();
081        a[1][1] = a[0][0];
082        a[2][0] = getField().getZero();
083        a[2][1] = getField().getZero();
084        a[2][2] = getField().getOne();
085        return a;
086    }
087
088    /** {@inheritDoc} */
089    public T[] getB() {
090        final T[] b = MathArrays.buildArray(getField(), 4);
091        b[0] = fraction(1, 6);
092        b[1] = fraction(1, 3);
093        b[2] = b[1];
094        b[3] = b[0];
095        return b;
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    protected ClassicalRungeKuttaFieldStepInterpolator<T>
101        createInterpolator(final boolean forward, T[][] yDotK,
102                           final FieldODEStateAndDerivative<T> globalPreviousState,
103                           final FieldODEStateAndDerivative<T> globalCurrentState,
104                           final FieldEquationsMapper<T> mapper) {
105        return new ClassicalRungeKuttaFieldStepInterpolator<T>(getField(), forward, yDotK,
106                                                               globalPreviousState, globalCurrentState,
107                                                               globalPreviousState, globalCurrentState,
108                                                               mapper);
109    }
110
111}