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 org.apache.commons.math.linear.Array2DRowRealMatrix; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.DerivativeException; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.FirstOrderDifferentialEquations; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.IntegratorException; 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.sampling.NordsieckStepInterpolator; 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.ode.sampling.StepHandler; 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath; 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class implements explicit Adams-Bashforth integrators for Ordinary 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Differential Equations. 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Adams-Bashforth methods (in fact due to Adams alone) are explicit 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multistep ODE solvers. This implementation is a variation of the classical 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one: it uses adaptive stepsize to implement error control, whereas 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * classical implementations are fixed step size. The value of state vector 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * at step n+1 is a simple combination of the value at step n and of the 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * derivatives at steps n, n-1, n-2 ... Depending on the number k of previous 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * steps one wants to use for computing the next value, different formulas 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * are available:</p> 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 1: y<sub>n+1</sub> = y<sub>n</sub> + h y'<sub>n</sub></li> 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 2: y<sub>n+1</sub> = y<sub>n</sub> + h (3y'<sub>n</sub>-y'<sub>n-1</sub>)/2</li> 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 3: y<sub>n+1</sub> = y<sub>n</sub> + h (23y'<sub>n</sub>-16y'<sub>n-1</sub>+5y'<sub>n-2</sub>)/12</li> 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 4: y<sub>n+1</sub> = y<sub>n</sub> + h (55y'<sub>n</sub>-59y'<sub>n-1</sub>+37y'<sub>n-2</sub>-9y'<sub>n-3</sub>)/24</li> 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>...</li> 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>A k-steps Adams-Bashforth method is of order k.</p> 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <h3>Implementation details</h3> 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>We define scaled derivatives s<sub>i</sub>(n) at step n as: 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>1</sub>(n) = h y'<sub>n</sub> for first derivative 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>2</sub>(n) = h<sup>2</sup>/2 y''<sub>n</sub> for second derivative 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>3</sub>(n) = h<sup>3</sup>/6 y'''<sub>n</sub> for third derivative 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * ... 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>k</sub>(n) = h<sup>k</sup>/k! y(k)<sub>n</sub> for k<sup>th</sup> derivative 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The definitions above use the classical representation with several previous first 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * derivatives. Lets define 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 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> 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (we omit the k index in the notation for clarity). With these definitions, 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Adams-Bashforth methods can be written: 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 1: y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n)</li> 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 2: y<sub>n+1</sub> = y<sub>n</sub> + 3/2 s<sub>1</sub>(n) + [ -1/2 ] q<sub>n</sub></li> 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 3: y<sub>n+1</sub> = y<sub>n</sub> + 23/12 s<sub>1</sub>(n) + [ -16/12 5/12 ] q<sub>n</sub></li> 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>k = 4: y<sub>n+1</sub> = y<sub>n</sub> + 55/24 s<sub>1</sub>(n) + [ -59/24 37/24 -9/24 ] q<sub>n</sub></li> 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>...</li> 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p> 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Instead of using the classical representation with first derivatives only (y<sub>n</sub>, 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * s<sub>1</sub>(n) and q<sub>n</sub>), our implementation uses the Nordsieck vector with 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * higher degrees scaled derivatives all taken at the same step (y<sub>n</sub>, s<sub>1</sub>(n) 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * and r<sub>n</sub>) where r<sub>n</sub> is defined as: 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * r<sub>n</sub> = [ s<sub>2</sub>(n), s<sub>3</sub>(n) ... s<sub>k</sub>(n) ]<sup>T</sup> 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (here again we omit the k index in the notation for clarity) 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Taylor series formulas show that for any index offset i, s<sub>1</sub>(n-i) can be 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * computed from s<sub>1</sub>(n), s<sub>2</sub>(n) ... s<sub>k</sub>(n), the formula being exact 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * for degree k polynomials. 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 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) 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The previous formula can be used with several values for i to compute the transform between 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * classical representation and Nordsieck vector. The transform between r<sub>n</sub> 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * and q<sub>n</sub> resulting from the Taylor series formulas above is: 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * q<sub>n</sub> = s<sub>1</sub>(n) u + P r<sub>n</sub> 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre> 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * where u is the [ 1 1 ... 1 ]<sup>T</sup> vector and P is the (k-1)×(k-1) matrix built 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * with the j (-i)<sup>j-1</sup> terms: 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -2 3 -4 5 ... ] 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -4 12 -32 80 ... ] 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * P = [ -6 27 -108 405 ... ] 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ -8 48 -256 1280 ... ] 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ... ] 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Using the Nordsieck vector has several advantages: 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it greatly simplifies step interpolation as the interpolator mainly applies 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Taylor series formulas,</li> 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it simplifies step changes that occur when discrete events that truncate 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the step are triggered,</li> 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>it allows to extend the methods in order to support adaptive stepsize.</li> 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p> 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The Nordsieck vector at step n+1 is computed from the Nordsieck vector at step n as follows: 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>y<sub>n+1</sub> = y<sub>n</sub> + s<sub>1</sub>(n) + u<sup>T</sup> r<sub>n</sub></li> 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>s<sub>1</sub>(n+1) = h f(t<sub>n+1</sub>, y<sub>n+1</sub>)</li> 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <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> 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * where A is a rows shifting matrix (the lower left part is an identity matrix): 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre> 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 0 0 | 0 ] 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ---------------+---] 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 1 0 ... 0 0 | 0 ] 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * A = [ 0 1 ... 0 0 | 0 ] 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ ... | 0 ] 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 1 0 | 0 ] 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * [ 0 0 ... 0 1 | 0 ] 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p> 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The P<sup>-1</sup>u vector and the P<sup>-1</sup> A P matrix do not depend on the state, 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * they only depend on k and therefore are precomputed once for all.</p> 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 févr. 2011) $ 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class AdamsBashforthIntegrator extends AdamsIntegrator { 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Integrator method name. */ 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private static final String METHOD_NAME = "Adams-Bashforth"; 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Build an Adams-Bashforth integrator with the given order and step control parameters. 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param nSteps number of steps of the method excluding the one being computed 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param minStep minimal step (must be positive even for backward 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration), the last step can be smaller than this 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param maxStep maximal step (must be positive even for backward 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration) 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param scalAbsoluteTolerance allowed absolute error 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param scalRelativeTolerance allowed relative error 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if order is 1 or less 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public AdamsBashforthIntegrator(final int nSteps, 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double minStep, final double maxStep, 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scalAbsoluteTolerance, 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scalRelativeTolerance) 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(METHOD_NAME, nSteps, nSteps, minStep, maxStep, 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond scalAbsoluteTolerance, scalRelativeTolerance); 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Build an Adams-Bashforth integrator with the given order and step control parameters. 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param nSteps number of steps of the method excluding the one being computed 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param minStep minimal step (must be positive even for backward 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration), the last step can be smaller than this 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param maxStep maximal step (must be positive even for backward 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * integration) 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param vecAbsoluteTolerance allowed absolute error 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param vecRelativeTolerance allowed relative error 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception IllegalArgumentException if order is 1 or less 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public AdamsBashforthIntegrator(final int nSteps, 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double minStep, final double maxStep, 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] vecAbsoluteTolerance, 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] vecRelativeTolerance) 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws IllegalArgumentException { 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond super(METHOD_NAME, nSteps, nSteps, minStep, maxStep, 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond vecAbsoluteTolerance, vecRelativeTolerance); 184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double integrate(final FirstOrderDifferentialEquations equations, 189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double t0, final double[] y0, 190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double t, final double[] y) 191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws DerivativeException, IntegratorException { 192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int n = y0.length; 194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond sanityChecks(equations, t0, y0, t, y); 195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setEquations(equations); 196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond resetEvaluations(); 197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean forward = t > t0; 198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // initialize working arrays 200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (y != y0) { 201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(y0, 0, y, 0, n); 202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] yDot = new double[n]; 204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up an interpolator sharing the integrator arrays 206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final NordsieckStepInterpolator interpolator = new NordsieckStepInterpolator(); 207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(y, forward); 208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // set up integration control objects 210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (StepHandler handler : stepHandlers) { 211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond handler.reset(); 212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond setStateInitialized(false); 214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // compute the initial Nordsieck vector using the configured starter integrator 216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond start(t0, y, t); 217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); 218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepStart); 219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int lastRow = nordsieck.getRowDimension() - 1; 220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // reuse the step that was chosen by the starter integrator 222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double hNew = stepSize; 223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // main integration loop 226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond isLastStep = false; 227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond do { 228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double error = 10; 230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond while (error >= 1.0) { 231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepSize = hNew; 233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // evaluate error using the last term of the Taylor expansion 235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond error = 0; 236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < mainSetDimension; ++i) { 237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double yScale = FastMath.abs(y[i]); 238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double tol = (vecAbsoluteTolerance == null) ? 239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : 240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond (vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * yScale); 241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double ratio = nordsieck.getEntry(lastRow, i) / tol; 242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond error += ratio * ratio; 243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond error = FastMath.sqrt(error / mainSetDimension); 245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (error >= 1.0) { 247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // reject the step and attempt to reduce error by stepsize control 248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double factor = computeStepGrowShrinkFactor(error); 249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = filterStep(stepSize * factor, forward, false); 250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // predict a first estimate of the state at step end 256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double stepEnd = stepStart + stepSize; 257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.shift(); 258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.setInterpolatedTime(stepEnd); 259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond System.arraycopy(interpolator.getInterpolatedState(), 0, y, 0, y0.length); 260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // evaluate the derivative 262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond computeDerivatives(stepEnd, y, yDot); 263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // update Nordsieck vector 265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] predictedScaled = new double[y0.length]; 266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < y0.length; ++j) { 267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond predictedScaled[j] = stepSize * yDot[j]; 268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final Array2DRowRealMatrix nordsieckTmp = updateHighOrderDerivativesPhase1(nordsieck); 270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond updateHighOrderDerivativesPhase2(scaled, predictedScaled, nordsieckTmp); 271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepEnd, stepSize, predictedScaled, nordsieckTmp); 272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // discrete events handling 274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepEnd); 275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond stepStart = acceptStep(interpolator, y, yDot, t); 276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond scaled = predictedScaled; 277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond nordsieck = nordsieckTmp; 278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepEnd, stepSize, scaled, nordsieck); 279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (!isLastStep) { 281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // prepare next step 283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.storeTime(stepStart); 284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (resetOccurred) { 286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // some events handler has triggered changes that 287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // invalidate the derivatives, we need to restart from scratch 288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond start(stepStart, y, t); 289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); 290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // stepsize control for next step 293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double factor = computeStepGrowShrinkFactor(error); 294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double scaledH = stepSize * factor; 295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double nextT = stepStart + scaledH; 296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); 297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = filterStep(scaledH, forward, nextIsLast); 298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double filteredNextT = stepStart + hNew; 300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t); 301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (filteredNextIsLast) { 302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond hNew = t - stepStart; 303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond interpolator.rescale(hNew); 306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } while (!isLastStep); 310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double stopTime = stepStart; 312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond resetInternalState(); 313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return stopTime; 314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 318