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/**
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)</p>
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    public T[] getC() {
066        final T[] c = MathArrays.buildArray(getField(), 3);
067        c[0] = fraction(1, 2);
068        c[1] = c[0];
069        c[2] = getField().getOne();
070        return c;
071    }
072
073    /** {@inheritDoc} */
074    public T[][] getA() {
075
076        final T two     = getField().getZero().add(2);
077        final T sqrtTwo = two.sqrt();
078
079        final T[][] a = MathArrays.buildArray(getField(), 3, -1);
080        for (int i = 0; i < a.length; ++i) {
081            a[i] = MathArrays.buildArray(getField(), i + 1);
082        }
083        a[0][0] = fraction(1, 2);
084        a[1][0] = sqrtTwo.subtract(1).multiply(0.5);
085        a[1][1] = sqrtTwo.subtract(2).multiply(-0.5);
086        a[2][0] = getField().getZero();
087        a[2][1] = sqrtTwo.multiply(-0.5);
088        a[2][2] = sqrtTwo.add(2).multiply(0.5);
089        return a;
090    }
091
092    /** {@inheritDoc} */
093    public T[] getB() {
094
095        final T two     = getField().getZero().add(2);
096        final T sqrtTwo = two.sqrt();
097
098        final T[] b = MathArrays.buildArray(getField(), 4);
099        b[0] = fraction(1, 6);
100        b[1] = sqrtTwo.subtract(2).divide(-6);
101        b[2] = sqrtTwo.add(2).divide(6);
102        b[3] = b[0];
103
104        return b;
105
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    protected GillFieldStepInterpolator<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 GillFieldStepInterpolator<T>(getField(), forward, yDotK,
116                                                globalPreviousState, globalCurrentState,
117                                                globalPreviousState, globalCurrentState,
118                                                mapper);
119    }
120
121}