View Javadoc
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  
18  package org.apache.commons.math4.legacy.ode.nonstiff;
19  
20  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
21  import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
22  import org.apache.commons.math4.legacy.exception.NoBracketingException;
23  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
24  import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
25  import org.apache.commons.math4.legacy.ode.ExpandableStatefulODE;
26  import org.apache.commons.math4.legacy.ode.MultistepIntegrator;
27  
28  
29  /** Base class for {@link AdamsBashforthIntegrator Adams-Bashforth} and
30   * {@link AdamsMoultonIntegrator Adams-Moulton} integrators.
31   * @since 2.0
32   */
33  public abstract class AdamsIntegrator extends MultistepIntegrator {
34  
35      /** Transformer. */
36      private final AdamsNordsieckTransformer transformer;
37  
38      /**
39       * Build an Adams integrator with the given order and step control parameters.
40       * @param name name of the method
41       * @param nSteps number of steps of the method excluding the one being computed
42       * @param order order of the method
43       * @param minStep minimal step (sign is irrelevant, regardless of
44       * integration direction, forward or backward), the last step can
45       * be smaller than this
46       * @param maxStep maximal step (sign is irrelevant, regardless of
47       * integration direction, forward or backward), the last step can
48       * be smaller than this
49       * @param scalAbsoluteTolerance allowed absolute error
50       * @param scalRelativeTolerance allowed relative error
51       * @exception NumberIsTooSmallException if order is 1 or less
52       */
53      public AdamsIntegrator(final String name, final int nSteps, final int order,
54                             final double minStep, final double maxStep,
55                             final double scalAbsoluteTolerance,
56                             final double scalRelativeTolerance)
57          throws NumberIsTooSmallException {
58          super(name, nSteps, order, minStep, maxStep,
59                scalAbsoluteTolerance, scalRelativeTolerance);
60          transformer = AdamsNordsieckTransformer.getInstance(nSteps);
61      }
62  
63      /**
64       * Build an Adams integrator with the given order and step control parameters.
65       * @param name name of the method
66       * @param nSteps number of steps of the method excluding the one being computed
67       * @param order order of the method
68       * @param minStep minimal step (sign is irrelevant, regardless of
69       * integration direction, forward or backward), the last step can
70       * be smaller than this
71       * @param maxStep maximal step (sign is irrelevant, regardless of
72       * integration direction, forward or backward), the last step can
73       * be smaller than this
74       * @param vecAbsoluteTolerance allowed absolute error
75       * @param vecRelativeTolerance allowed relative error
76       * @exception IllegalArgumentException if order is 1 or less
77       */
78      public AdamsIntegrator(final String name, final int nSteps, final int order,
79                             final double minStep, final double maxStep,
80                             final double[] vecAbsoluteTolerance,
81                             final double[] vecRelativeTolerance)
82          throws IllegalArgumentException {
83          super(name, nSteps, order, minStep, maxStep,
84                vecAbsoluteTolerance, vecRelativeTolerance);
85          transformer = AdamsNordsieckTransformer.getInstance(nSteps);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public abstract void integrate(ExpandableStatefulODE equations, double t)
91          throws NumberIsTooSmallException, DimensionMismatchException,
92                 MaxCountExceededException, NoBracketingException;
93  
94      /** {@inheritDoc} */
95      @Override
96      protected Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t,
97                                                                    final double[][] y,
98                                                                    final double[][] yDot) {
99          return transformer.initializeHighOrderDerivatives(h, t, y, yDot);
100     }
101 
102     /** Update the high order scaled derivatives for Adams integrators (phase 1).
103      * <p>The complete update of high order derivatives has a form similar to:
104      * <div style="white-space: pre"><code>
105      * 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>
106      * </code></div>
107      * this method computes the P<sup>-1</sup> A P r<sub>n</sub> part.
108      * @param highOrder high order scaled derivatives
109      * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
110      * @return updated high order derivatives
111      * @see #updateHighOrderDerivativesPhase2(double[], double[], Array2DRowRealMatrix)
112      */
113     public Array2DRowRealMatrix updateHighOrderDerivativesPhase1(final Array2DRowRealMatrix highOrder) {
114         return transformer.updateHighOrderDerivativesPhase1(highOrder);
115     }
116 
117     /** Update the high order scaled derivatives Adams integrators (phase 2).
118      * <p>The complete update of high order derivatives has a form similar to:
119      * <div style="white-space: pre"><code>
120      * 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>
121      * </code></div>
122      * this method computes the (s<sub>1</sub>(n) - s<sub>1</sub>(n+1)) P<sup>-1</sup> u part.
123      * <p>Phase 1 of the update must already have been performed.</p>
124      * @param start first order scaled derivatives at step start
125      * @param end first order scaled derivatives at step end
126      * @param highOrder high order scaled derivatives, will be modified
127      * (h<sup>2</sup>/2 y'', ... h<sup>k</sup>/k! y(k))
128      * @see #updateHighOrderDerivativesPhase1(Array2DRowRealMatrix)
129      */
130     public void updateHighOrderDerivativesPhase2(final double[] start,
131                                                  final double[] end,
132                                                  final Array2DRowRealMatrix highOrder) {
133         transformer.updateHighOrderDerivativesPhase2(start, end, highOrder);
134     }
135 }