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 a second order Runge-Kutta integrator for
028 * Ordinary Differential Equations.
029 *
030 * <p>This method is an explicit Runge-Kutta method, its Butcher-array
031 * is the following one :
032 * <pre>
033 *    0  |  0    0
034 *   1/2 | 1/2   0
035 *       |----------
036 *       |  0    1
037 * </pre>
038 * </p>
039 *
040 * @see EulerFieldIntegrator
041 * @see ClassicalRungeKuttaFieldIntegrator
042 * @see GillFieldIntegrator
043 * @see ThreeEighthesFieldIntegrator
044 * @see LutherFieldIntegrator
045 *
046 * @param <T> the type of the field elements
047 * @since 3.6
048 */
049
050public class MidpointFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
051
052    /** Simple constructor.
053     * Build a midpoint integrator with the given step.
054     * @param field field to which the time and state vector elements belong
055     * @param step integration step
056     */
057    public MidpointFieldIntegrator(final Field<T> field, final T step) {
058        super(field, "midpoint", step);
059    }
060
061    /** {@inheritDoc} */
062    public T[] getC() {
063        final T[] c = MathArrays.buildArray(getField(), 1);
064        c[0] = getField().getOne().multiply(0.5);
065        return c;
066    }
067
068    /** {@inheritDoc} */
069    public T[][] getA() {
070        final T[][] a = MathArrays.buildArray(getField(), 1, 1);
071        a[0][0] = fraction(1, 2);
072        return a;
073    }
074
075    /** {@inheritDoc} */
076    public T[] getB() {
077        final T[] b = MathArrays.buildArray(getField(), 2);
078        b[0] = getField().getZero();
079        b[1] = getField().getOne();
080        return b;
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    protected MidpointFieldStepInterpolator<T>
086        createInterpolator(final boolean forward, T[][] yDotK,
087                           final FieldODEStateAndDerivative<T> globalPreviousState,
088                           final FieldODEStateAndDerivative<T> globalCurrentState,
089                           final FieldEquationsMapper<T> mapper) {
090        return new MidpointFieldStepInterpolator<T>(getField(), forward, yDotK,
091                                                    globalPreviousState, globalCurrentState,
092                                                    globalPreviousState, globalCurrentState,
093                                                    mapper);
094    }
095
096}