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/**
028 * This class implements the Gill fourth order Runge-Kutta
029 * integrator for Ordinary Differential Equations .
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 | (q-1)/2  (2-q)/2    0      0
037 *    1  |    0       -q/2  (2+q)/2   0
038 *       |-------------------------------
039 *       |   1/6    (2-q)/6 (2+q)/6  1/6
040 * </pre>
041 * where q = sqrt(2)
042 *
043 * @see EulerFieldIntegrator
044 * @see ClassicalRungeKuttaFieldIntegrator
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 GillFieldIntegrator<T extends RealFieldElement<T>>
053    extends RungeKuttaFieldIntegrator<T> {
054
055    /** Simple constructor.
056     * Build a fourth-order Gill 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 GillFieldIntegrator(final Field<T> field, final T step) {
061        super(field, "Gill", step);
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public T[] getC() {
067        final T[] c = MathArrays.buildArray(getField(), 3);
068        c[0] = fraction(1, 2);
069        c[1] = c[0];
070        c[2] = getField().getOne();
071        return c;
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public T[][] getA() {
077
078        final T two     = getField().getZero().add(2);
079        final T sqrtTwo = two.sqrt();
080
081        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
082        for (int i = 0; i < a.length; ++i) {
083            a[i] = MathArrays.buildArray(getField(), i + 1);
084        }
085        a[0][0] = fraction(1, 2);
086        a[1][0] = sqrtTwo.subtract(1).multiply(0.5);
087        a[1][1] = sqrtTwo.subtract(2).multiply(-0.5);
088        a[2][0] = getField().getZero();
089        a[2][1] = sqrtTwo.multiply(-0.5);
090        a[2][2] = sqrtTwo.add(2).multiply(0.5);
091        return a;
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public T[] getB() {
097
098        final T two     = getField().getZero().add(2);
099        final T sqrtTwo = two.sqrt();
100
101        final T[] b = MathArrays.buildArray(getField(), 4);
102        b[0] = fraction(1, 6);
103        b[1] = sqrtTwo.subtract(2).divide(-6);
104        b[2] = sqrtTwo.add(2).divide(6);
105        b[3] = b[0];
106
107        return b;
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    protected GillFieldStepInterpolator<T>
113        createInterpolator(final boolean forward, T[][] yDotK,
114                           final FieldODEStateAndDerivative<T> globalPreviousState,
115                           final FieldODEStateAndDerivative<T> globalCurrentState,
116                           final FieldEquationsMapper<T> mapper) {
117        return new GillFieldStepInterpolator<>(getField(), forward, yDotK,
118                                                globalPreviousState, globalCurrentState,
119                                                globalPreviousState, globalCurrentState,
120                                                mapper);
121    }
122}