1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.commons.math.ode.nonstiff;
19
20import org.apache.commons.math.linear.Array2DRowRealMatrix;
21import org.apache.commons.math.ode.DerivativeException;
22import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
23import org.apache.commons.math.ode.IntegratorException;
24import org.apache.commons.math.ode.MultistepIntegrator;
25
26
27/** Base class for {@link AdamsBashforthIntegrator Adams-Bashforth} and
28 * {@link AdamsMoultonIntegrator Adams-Moulton} integrators.
29 * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 févr. 2011) $
30 * @since 2.0
31 */
32public abstract class AdamsIntegrator extends MultistepIntegrator {
33
34    /** Transformer. */
35    private final AdamsNordsieckTransformer transformer;
36
37    /**
38     * Build an Adams integrator with the given order and step control prameters.
39     * @param name name of the method
40     * @param nSteps number of steps of the method excluding the one being computed
41     * @param order order of the method
42     * @param minStep minimal step (must be positive even for backward
43     * integration), the last step can be smaller than this
44     * @param maxStep maximal step (must be positive even for backward
45     * integration)
46     * @param scalAbsoluteTolerance allowed absolute error
47     * @param scalRelativeTolerance allowed relative error
48     * @exception IllegalArgumentException if order is 1 or less
49     */
50    public AdamsIntegrator(final String name, final int nSteps, final int order,
51                           final double minStep, final double maxStep,
52                           final double scalAbsoluteTolerance,
53                           final double scalRelativeTolerance)
54        throws IllegalArgumentException {
55        super(name, nSteps, order, minStep, maxStep,
56              scalAbsoluteTolerance, scalRelativeTolerance);
57        transformer = AdamsNordsieckTransformer.getInstance(nSteps);
58    }
59
60    /**
61     * Build an Adams integrator with the given order and step control parameters.
62     * @param name name of the method
63     * @param nSteps number of steps of the method excluding the one being computed
64     * @param order order of the method
65     * @param minStep minimal step (must be positive even for backward
66     * integration), the last step can be smaller than this
67     * @param maxStep maximal step (must be positive even for backward
68     * integration)
69     * @param vecAbsoluteTolerance allowed absolute error
70     * @param vecRelativeTolerance allowed relative error
71     * @exception IllegalArgumentException if order is 1 or less
72     */
73    public AdamsIntegrator(final String name, final int nSteps, final int order,
74                           final double minStep, final double maxStep,
75                           final double[] vecAbsoluteTolerance,
76                           final double[] vecRelativeTolerance)
77        throws IllegalArgumentException {
78        super(name, nSteps, order, minStep, maxStep,
79              vecAbsoluteTolerance, vecRelativeTolerance);
80        transformer = AdamsNordsieckTransformer.getInstance(nSteps);
81    }
82
83    /** {@inheritDoc} */
84    @Override
85    public abstract double integrate(final FirstOrderDifferentialEquations equations,
86                                     final double t0, final double[] y0,
87                                     final double t, final double[] y)
88        throws DerivativeException, IntegratorException;
89
90    /** {@inheritDoc} */
91    @Override
92    protected Array2DRowRealMatrix initializeHighOrderDerivatives(final double[] first,
93                                                        final double[][] multistep) {
94        return transformer.initializeHighOrderDerivatives(first, multistep);
95    }
96
97    /** Update the high order scaled derivatives for Adams integrators (phase 1).
98     * <p>The complete update of high order derivatives has a form similar to:
99     * <pre>
100     * 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>
101     * </pre>
102     * this method computes the P<sup>-1</sup> A P r<sub>n</sub> part.</p>
103     * @param highOrder high order scaled derivatives
104     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
105     * @return updated high order derivatives
106     * @see #updateHighOrderDerivativesPhase2(double[], double[], Array2DRowRealMatrix)
107     */
108    public Array2DRowRealMatrix updateHighOrderDerivativesPhase1(final Array2DRowRealMatrix highOrder) {
109        return transformer.updateHighOrderDerivativesPhase1(highOrder);
110    }
111
112    /** Update the high order scaled derivatives Adams integrators (phase 2).
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 (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u part.</p>
118     * <p>Phase 1 of the update must already have been performed.</p>
119     * @param start first order scaled derivatives at step start
120     * @param end first order scaled derivatives at step end
121     * @param highOrder high order scaled derivatives, will be modified
122     * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
123     * @see #updateHighOrderDerivativesPhase1(Array2DRowRealMatrix)
124     */
125    public void updateHighOrderDerivativesPhase2(final double[] start,
126                                                 final double[] end,
127                                                 final Array2DRowRealMatrix highOrder) {
128        transformer.updateHighOrderDerivativesPhase2(start, end, highOrder);
129    }
130
131}
132