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.exception.DimensionMismatchException;
023import org.apache.commons.math3.exception.MaxCountExceededException;
024import org.apache.commons.math3.exception.NoBracketingException;
025import org.apache.commons.math3.exception.NumberIsTooSmallException;
026import org.apache.commons.math3.linear.Array2DRowFieldMatrix;
027import org.apache.commons.math3.ode.FieldExpandableODE;
028import org.apache.commons.math3.ode.FieldODEState;
029import org.apache.commons.math3.ode.FieldODEStateAndDerivative;
030import org.apache.commons.math3.ode.MultistepFieldIntegrator;
031
032
033/** Base class for {@link AdamsBashforthFieldIntegrator Adams-Bashforth} and
034 * {@link AdamsMoultonFieldIntegrator Adams-Moulton} integrators.
035 * @param <T> the type of the field elements
036 * @since 3.6
037 */
038public abstract class AdamsFieldIntegrator<T extends RealFieldElement<T>> extends MultistepFieldIntegrator<T> {
039
040    /** Transformer. */
041    private final AdamsNordsieckFieldTransformer<T> transformer;
042
043    /**
044     * Build an Adams integrator with the given order and step control parameters.
045     * @param field field to which the time and state vector elements belong
046     * @param name name of the method
047     * @param nSteps number of steps of the method excluding the one being computed
048     * @param order order of the method
049     * @param minStep minimal step (sign is irrelevant, regardless of
050     * integration direction, forward or backward), the last step can
051     * be smaller than this
052     * @param maxStep maximal step (sign is irrelevant, regardless of
053     * integration direction, forward or backward), the last step can
054     * be smaller than this
055     * @param scalAbsoluteTolerance allowed absolute error
056     * @param scalRelativeTolerance allowed relative error
057     * @exception NumberIsTooSmallException if order is 1 or less
058     */
059    public AdamsFieldIntegrator(final Field<T> field, final String name,
060                                final int nSteps, final int order,
061                                final double minStep, final double maxStep,
062                                final double scalAbsoluteTolerance,
063                                final double scalRelativeTolerance)
064        throws NumberIsTooSmallException {
065        super(field, name, nSteps, order, minStep, maxStep,
066              scalAbsoluteTolerance, scalRelativeTolerance);
067        transformer = AdamsNordsieckFieldTransformer.getInstance(field, nSteps);
068    }
069
070    /**
071     * Build an Adams integrator with the given order and step control parameters.
072     * @param field field to which the time and state vector elements belong
073     * @param name name of the method
074     * @param nSteps number of steps of the method excluding the one being computed
075     * @param order order of the method
076     * @param minStep minimal step (sign is irrelevant, regardless of
077     * integration direction, forward or backward), the last step can
078     * be smaller than this
079     * @param maxStep maximal step (sign is irrelevant, regardless of
080     * integration direction, forward or backward), the last step can
081     * be smaller than this
082     * @param vecAbsoluteTolerance allowed absolute error
083     * @param vecRelativeTolerance allowed relative error
084     * @exception IllegalArgumentException if order is 1 or less
085     */
086    public AdamsFieldIntegrator(final Field<T> field, final String name,
087                                final int nSteps, final int order,
088                                final double minStep, final double maxStep,
089                                final double[] vecAbsoluteTolerance,
090                                final double[] vecRelativeTolerance)
091        throws IllegalArgumentException {
092        super(field, name, nSteps, order, minStep, maxStep,
093              vecAbsoluteTolerance, vecRelativeTolerance);
094        transformer = AdamsNordsieckFieldTransformer.getInstance(field, nSteps);
095    }
096
097    /** {@inheritDoc} */
098    public abstract FieldODEStateAndDerivative<T> integrate(final FieldExpandableODE<T> equations,
099                                                            final FieldODEState<T> initialState,
100                                                            final T finalTime)
101        throws NumberIsTooSmallException, DimensionMismatchException,
102               MaxCountExceededException, NoBracketingException;
103
104    /** {@inheritDoc} */
105    @Override
106    protected Array2DRowFieldMatrix<T> initializeHighOrderDerivatives(final T h, final T[] t,
107                                                                      final T[][] y,
108                                                                      final T[][] yDot) {
109        return transformer.initializeHighOrderDerivatives(h, t, y, yDot);
110    }
111
112    /** Update the high order scaled derivatives for Adams integrators (phase 1).
113     * <p>The complete update of high order derivatives has a form similar to:
114     * <pre>
115     * r<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub>
116     * </pre>
117     * this method computes the P<sup>-1</sup> A P r<sub>n</sub> part.</p>
118     * @param highOrder high order scaled derivatives
119     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
120     * @return updated high order derivatives
121     * @see #updateHighOrderDerivativesPhase2(RealFieldElement[], RealFieldElement[], Array2DRowFieldMatrix)
122     */
123    public Array2DRowFieldMatrix<T> updateHighOrderDerivativesPhase1(final Array2DRowFieldMatrix<T> highOrder) {
124        return transformer.updateHighOrderDerivativesPhase1(highOrder);
125    }
126
127    /** Update the high order scaled derivatives Adams integrators (phase 2).
128     * <p>The complete update of high order derivatives has a form similar to:
129     * <pre>
130     * r<sub>n+1</sub> = (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u + P<sup>-1</sup> A P r<sub>n</sub>
131     * </pre>
132     * this method computes the (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u part.</p>
133     * <p>Phase 1 of the update must already have been performed.</p>
134     * @param start first order scaled derivatives at step start
135     * @param end first order scaled derivatives at step end
136     * @param highOrder high order scaled derivatives, will be modified
137     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
138     * @see #updateHighOrderDerivativesPhase1(Array2DRowFieldMatrix)
139     */
140    public void updateHighOrderDerivativesPhase2(final T[] start, final T[] end,
141                                                 final Array2DRowFieldMatrix<T> highOrder) {
142        transformer.updateHighOrderDerivativesPhase2(start, end, highOrder);
143    }
144
145}