1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.ode.nonstiff; 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.util.Arrays; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.linear.Array2DRowRealMatrix; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.linear.RealMatrixPreservingVisitor; 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.DerivativeException; 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.FirstOrderDifferentialEquations; 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.IntegratorException; 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.sampling.NordsieckStepInterpolator; 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.sampling.StepHandler; 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath; 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class implements implicit Adams-Moulton integrators for Ordinary 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Differential Equations. 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Adams-Moulton methods (in fact due to Adams alone) are implicit 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multistep ODE solvers. This implementation is a variation of the classical 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one: it uses adaptive stepsize to implement error control, whereas 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * classical implementations are fixed step size. The value of state vector 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * at step n+1 is a simple combination of the value at step n and of the 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * derivatives at steps n+1, n, n-1 ... Since y'<sub>n+1</sub> is needed to 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * compute y<sub>n+1</sub>,another method must be used to compute a first 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * estimate of y<sub>n+1</sub>, then compute y'<sub>n+1</sub>, then compute 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * a final estimate of y<sub>n+1</sub> using the following formulas. Depending 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * on the number k of previous steps one wants to use for computing the next 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value, different formulas are available for the final estimate:</p> 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 1: y<sub>n+1</sub> = y<sub>n</sub> + h y'<sub>n+1</sub></li> 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 2: y<sub>n+1</sub> = y<sub>n</sub> + h (y'<sub>n+1</sub>+y'<sub>n</sub>)/2</li> 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 3: y<sub>n+1</sub> = y<sub>n</sub> + h (5y'<sub>n+1</sub>+8y'<sub>n</sub>-y'<sub>n-1</sub>)/12</li> 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 4: y<sub>n+1</sub> = y<sub>n</sub> + h (9y'<sub>n+1</sub>+19y'<sub>n</sub>-5y'<sub>n-1</sub>+y'<sub>n-2</sub>)/24</li> 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>...</li> 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>A k-steps Adams-Moulton method is of order k+1.</p> 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <h3>Implementation details</h3> 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>We define scaled derivatives s<sub>i</sub>(n) at step n as: 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>1</sub>(n) = h y'<sub>n</sub> for first derivative 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>2</sub>(n) = h<sup>2</sup>/2 y''<sub>n</sub> for second derivative 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>3</sub>(n) = h<sup>3</sup>/6 y'''<sub>n</sub> for third derivative 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * ... 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>k</sub>(n) = h<sup>k</sup>/k! y(k)<sub>n</sub> for k<sup>th</sup> derivative 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The definitions above use the classical representation with several previous first 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * derivatives. Lets define 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * q<sub>n</sub> = [ s<sub>1</sub>(n-1) s<sub>1</sub>(n-2) ... s<sub>1</sub>(n-(k-1)) ]<sup>T</sup> 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (we omit the k index in the notation for clarity). With these definitions, 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Adams-Moulton methods can be written: 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 1: y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n+1)</li> 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 2: y<sub>n+1</sub> = y<sub>n</sub> + 1/2 s<sub>1</sub>(n+1) + [ 1/2 ] q<sub>n+1</sub></li> 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 3: y<sub>n+1</sub> = y<sub>n</sub> + 5/12 s<sub>1</sub>(n+1) + [ 8/12 -1/12 ] q<sub>n+1</sub></li> 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 4: y<sub>n+1</sub> = y<sub>n</sub> + 9/24 s<sub>1</sub>(n+1) + [ 19/24 -5/24 1/24 ] q<sub>n+1</sub></li> 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>...</li> 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p> 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Instead of using the classical representation with first derivatives only (y<sub>n</sub>, 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>1</sub>(n+1) and q<sub>n+1</sub>), our implementation uses the Nordsieck vector with 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * higher degrees scaled derivatives all taken at the same step (y<sub>n</sub>, s<sub>1</sub>(n) 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * and r<sub>n</sub>) where r<sub>n</sub> is defined as: 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * r<sub>n</sub> = [ s<sub>2</sub>(n), s<sub>3</sub>(n) ... s<sub>k</sub>(n) ]<sup>T</sup> 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (here again we omit the k index in the notation for clarity) 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Taylor series formulas show that for any index offset i, s<sub>1</sub>(n-i) can be 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * computed from s<sub>1</sub>(n), s<sub>2</sub>(n) ... s<sub>k</sub>(n), the formula being exact 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * for degree k polynomials. 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>1</sub>(n-i) = s<sub>1</sub>(n) + ∑<sub>j</sub> j (-i)<sup>j-1</sup> s<sub>j</sub>(n) 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The previous formula can be used with several values for i to compute the transform between 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * classical representation and Nordsieck vector. The transform between r<sub>n</sub> 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * and q<sub>n</sub> resulting from the Taylor series formulas above is: 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * q<sub>n</sub> = s<sub>1</sub>(n) u + P r<sub>n</sub> 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * where u is the [ 1 1 ... 1 ]<sup>T</sup> vector and P is the (k-1)×(k-1) matrix built 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * with the j (-i)<sup>j-1</sup> terms: 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -2 3 -4 5 ... ] 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -4 12 -32 80 ... ] 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * P = [ -6 27 -108 405 ... ] 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -8 48 -256 1280 ... ] 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ... ] 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Using the Nordsieck vector has several advantages: 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it greatly simplifies step interpolation as the interpolator mainly applies 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Taylor series formulas,</li> 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it simplifies step changes that occur when discrete events that truncate 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the step are triggered,</li> 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it allows to extend the methods in order to support adaptive stepsize.</li> 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p> 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The predicted Nordsieck vector at step n+1 is computed from the Nordsieck vector at step 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * n as follows: 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>Y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n) + u<sup>T</sup> r<sub>n</sub></li> 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>S<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, Y<sub>n+1</sub>)</li> 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>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></li> 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * where A is a rows shifting matrix (the lower left part is an identity matrix): 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 0 0 | 0 ] 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ---------------+---] 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 1 0 ... 0 0 | 0 ] 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * A = [ 0 1 ... 0 0 | 0 ] 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ... | 0 ] 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 1 0 | 0 ] 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 0 1 | 0 ] 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * From this predicted vector, the corrected vector is computed as follows: 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>y<sub>n+1</sub> = y<sub>n</sub> + S<sub>1</sub>(n+1) + [ -1 +1 -1 +1 ... ±1 ] r<sub>n+1</sub></li> 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>s<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, y<sub>n+1</sub>)</li> 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>r<sub>n+1</sub> = R<sub>n+1</sub> + (s<sub>1</sub>(n+1) - S<sub>1</sub>(n+1)) P<sup>-1</sup> u</li> 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * where the upper case Y<sub>n+1</sub>, S<sub>1</sub>(n+1) and R<sub>n+1</sub> represent the 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * predicted states whereas the lower case y<sub>n+1</sub>, s<sub>n+1</sub> and r<sub>n+1</sub> 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * represent the corrected states.</p> 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The P<sup>-1</sup>u vector and the P<sup>-1</sup> A P matrix do not depend on the state, 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * they only depend on k and therefore are precomputed once for all.</p> 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 févr. 2011) $ 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class AdamsMoultonIntegrator extends AdamsIntegrator { 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Integrator method name. */ 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private static final String METHOD_NAME = "Adams-Moulton"; 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Build an Adams-Moulton integrator with the given order and error control parameters. 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param nSteps number of steps of the method excluding the one being computed 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param minStep minimal step (must be positive even for backward 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration), the last step can be smaller than this 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param maxStep maximal step (must be positive even for backward 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration) 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param scalAbsoluteTolerance allowed absolute error 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param scalRelativeTolerance allowed relative error 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if order is 1 or less 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public AdamsMoultonIntegrator(final int nSteps, 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double minStep, final double maxStep, 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scalAbsoluteTolerance, 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scalRelativeTolerance) 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(METHOD_NAME, nSteps, nSteps + 1, minStep, maxStep, 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond scalAbsoluteTolerance, scalRelativeTolerance); 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Build an Adams-Moulton integrator with the given order and error control parameters. 184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param nSteps number of steps of the method excluding the one being computed 185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param minStep minimal step (must be positive even for backward 186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration), the last step can be smaller than this 187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param maxStep maximal step (must be positive even for backward 188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration) 189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param vecAbsoluteTolerance allowed absolute error 190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param vecRelativeTolerance allowed relative error 191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if order is 1 or less 192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public AdamsMoultonIntegrator(final int nSteps, 194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double minStep, final double maxStep, 195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] vecAbsoluteTolerance, 196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] vecRelativeTolerance) 197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(METHOD_NAME, nSteps, nSteps + 1, minStep, maxStep, 199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond vecAbsoluteTolerance, vecRelativeTolerance); 200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double integrate(final FirstOrderDifferentialEquations equations, 206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double t0, final double[] y0, 207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double t, final double[] y) 208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws DerivativeException, IntegratorException { 209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = y0.length; 211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond sanityChecks(equations, t0, y0, t, y); 212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setEquations(equations); 213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond resetEvaluations(); 214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean forward = t > t0; 215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // initialize working arrays 217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (y != y0) { 218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(y0, 0, y, 0, n); 219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] yDot = new double[y0.length]; 221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] yTmp = new double[y0.length]; 222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] predictedScaled = new double[y0.length]; 223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Array2DRowRealMatrix nordsieckTmp = null; 224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up two interpolators sharing the integrator arrays 226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final NordsieckStepInterpolator interpolator = new NordsieckStepInterpolator(); 227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(y, forward); 228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up integration control objects 230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (StepHandler handler : stepHandlers) { 231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond handler.reset(); 232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setStateInitialized(false); 234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // compute the initial Nordsieck vector using the configured starter integrator 236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond start(t0, y, t); 237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); 238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepStart); 239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double hNew = stepSize; 241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond isLastStep = false; 244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond do { 245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double error = 10; 247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond while (error >= 1.0) { 248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepSize = hNew; 250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // predict a first estimate of the state at step end (P in the PECE sequence) 252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double stepEnd = stepStart + stepSize; 253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.setInterpolatedTime(stepEnd); 254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(interpolator.getInterpolatedState(), 0, yTmp, 0, y0.length); 255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // evaluate a first estimate of the derivative (first E in the PECE sequence) 257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond computeDerivatives(stepEnd, yTmp, yDot); 258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // update Nordsieck vector 260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < y0.length; ++j) { 261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond predictedScaled[j] = stepSize * yDot[j]; 262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond nordsieckTmp = updateHighOrderDerivativesPhase1(nordsieck); 264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond updateHighOrderDerivativesPhase2(scaled, predictedScaled, nordsieckTmp); 265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // apply correction (C in the PECE sequence) 267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond error = nordsieckTmp.walkInOptimizedOrder(new Corrector(y, predictedScaled, yTmp)); 268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (error >= 1.0) { 270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // reject the step and attempt to reduce error by stepsize control 271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double factor = computeStepGrowShrinkFactor(error); 272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = filterStep(stepSize * factor, forward, false); 273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // evaluate a final estimate of the derivative (second E in the PECE sequence) 278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double stepEnd = stepStart + stepSize; 279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond computeDerivatives(stepEnd, yTmp, yDot); 280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // update Nordsieck vector 282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] correctedScaled = new double[y0.length]; 283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < y0.length; ++j) { 284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond correctedScaled[j] = stepSize * yDot[j]; 285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond updateHighOrderDerivativesPhase2(predictedScaled, correctedScaled, nordsieckTmp); 287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // discrete events handling 289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(yTmp, 0, y, 0, n); 290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepEnd, stepSize, correctedScaled, nordsieckTmp); 291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepStart); 292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.shift(); 293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepEnd); 294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepStart = acceptStep(interpolator, y, yDot, t); 295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond scaled = correctedScaled; 296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond nordsieck = nordsieckTmp; 297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (!isLastStep) { 299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // prepare next step 301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepStart); 302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (resetOccurred) { 304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // some events handler has triggered changes that 305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // invalidate the derivatives, we need to restart from scratch 306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond start(stepStart, y, t); 307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); 308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // stepsize control for next step 312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double factor = computeStepGrowShrinkFactor(error); 313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scaledH = stepSize * factor; 314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double nextT = stepStart + scaledH; 315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); 316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = filterStep(scaledH, forward, nextIsLast); 317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 318dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double filteredNextT = stepStart + hNew; 319dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t); 320dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (filteredNextIsLast) { 321dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = t - stepStart; 322dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 323dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 324dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 325dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 326dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 327dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } while (!isLastStep); 328dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 329dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double stopTime = stepStart; 330dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepStart = Double.NaN; 331dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepSize = Double.NaN; 332dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return stopTime; 333dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 334dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 335dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 336dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Corrector for current state in Adams-Moulton method. 337dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p> 338dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This visitor implements the Taylor series formula: 339dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 340dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n+1) + [ -1 +1 -1 +1 ... ±1 ] r<sub>n+1</sub> 341dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 342dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 343dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 344dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private class Corrector implements RealMatrixPreservingVisitor { 345dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 346dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Previous state. */ 347dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private final double[] previous; 348dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 349dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Current scaled first derivative. */ 350dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private final double[] scaled; 351dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 352dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Current state before correction. */ 353dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private final double[] before; 354dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 355dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Current state after correction. */ 356dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private final double[] after; 357dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 358dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Simple constructor. 359dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param previous previous state 360dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param scaled current scaled first derivative 361dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param state state to correct (will be overwritten after visit) 362dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 363dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public Corrector(final double[] previous, final double[] scaled, final double[] state) { 364dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond this.previous = previous; 365dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond this.scaled = scaled; 366dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond this.after = state; 367dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond this.before = state.clone(); 368dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 369dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 370dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 371dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void start(int rows, int columns, 372dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int startRow, int endRow, int startColumn, int endColumn) { 373dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond Arrays.fill(after, 0.0); 374dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 375dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 376dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 377dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public void visit(int row, int column, double value) { 378dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if ((row & 0x1) == 0) { 379dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond after[column] -= value; 380dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } else { 381dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond after[column] += value; 382dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 383dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 384dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 385dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 386dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * End visiting the Nordsieck vector. 387dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The correction is used to control stepsize. So its amplitude is 388dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * considered to be an error, which must be normalized according to 389dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * error control settings. If the normalized value is greater than 1, 390dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the correction was too large and the step must be rejected.</p> 391dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the normalized correction, if greater than 1, the step 392dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * must be rejected 393dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 394dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double end() { 395dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 396dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double error = 0; 397dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < after.length; ++i) { 398dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond after[i] += previous[i] + scaled[i]; 399dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (i < mainSetDimension) { 400dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double yScale = FastMath.max(FastMath.abs(previous[i]), FastMath.abs(after[i])); 401dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double tol = (vecAbsoluteTolerance == null) ? 402dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : 403dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * yScale); 404dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double ratio = (after[i] - before[i]) / tol; 405dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond error += ratio * ratio; 406dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 407dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 408dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 409dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return FastMath.sqrt(error / mainSetDimension); 410dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 411dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 412dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 413dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 414dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 415