📄 adaptivestepsizeintegrator.java
字号:
* @param equations differential equations set * @param forward forward integration indicator * @param order order of the method * @param scale scaling vector for the state vector * @param t0 start time * @param y0 state vector at t0 * @param yDot0 first time derivative of y0 * @param y1 work array for a state vector * @param yDot1 work array for the first time derivative of y1 * @return first integration step * @exception DerivativeException this exception is propagated to * the caller if the underlying user function triggers one */ public double initializeStep(FirstOrderDifferentialEquations equations, boolean forward, int order, double[] scale, double t0, double[] y0, double[] yDot0, double[] y1, double[] yDot1) throws DerivativeException { if (initialStep > 0) { // use the user provided value return forward ? initialStep : -initialStep; } // very rough first guess : h = 0.01 * ||y/scale|| / ||y'/scale|| // this guess will be used to perform an Euler step double ratio; double yOnScale2 = 0; double yDotOnScale2 = 0; for (int j = 0; j < y0.length; ++j) { ratio = y0[j] / scale[j]; yOnScale2 += ratio * ratio; ratio = yDot0[j] / scale[j]; yDotOnScale2 += ratio * ratio; } double h = ((yOnScale2 < 1.0e-10) || (yDotOnScale2 < 1.0e-10)) ? 1.0e-6 : (0.01 * Math.sqrt(yOnScale2 / yDotOnScale2)); if (! forward) { h = -h; } // perform an Euler step using the preceding rough guess for (int j = 0; j < y0.length; ++j) { y1[j] = y0[j] + h * yDot0[j]; } equations.computeDerivatives(t0 + h, y1, yDot1); // estimate the second derivative of the solution double yDDotOnScale = 0; for (int j = 0; j < y0.length; ++j) { ratio = (yDot1[j] - yDot0[j]) / scale[j]; yDDotOnScale += ratio * ratio; } yDDotOnScale = Math.sqrt(yDDotOnScale) / h; // step size is computed such that // h^order * max (||y'/tol||, ||y''/tol||) = 0.01 double maxInv2 = Math.max(Math.sqrt(yDotOnScale2), yDDotOnScale); double h1 = (maxInv2 < 1.0e-15) ? Math.max(1.0e-6, 0.001 * Math.abs(h)) : Math.pow(0.01 / maxInv2, 1.0 / order); h = Math.min(100.0 * Math.abs(h), h1); h = Math.max(h, 1.0e-12 * Math.abs(t0)); // avoids cancellation when computing t1 - t0 if (h < getMinStep()) { h = getMinStep(); } if (h > getMaxStep()) { h = getMaxStep(); } if (! forward) { h = -h; } return h; } /** Filter the integration step. * @param h signed step * @param acceptSmall if true, steps smaller than the minimal value * are silently increased up to this value, if false such small * steps generate an exception * @return a bounded integration step (h if no bound is reach, or a bounded value) * @exception IntegratorException if the step is too small and acceptSmall is false */ protected double filterStep(double h, boolean acceptSmall) throws IntegratorException { if (Math.abs(h) < minStep) { if (acceptSmall) { h = (h < 0) ? -minStep : minStep; } else { throw new IntegratorException("minimal step size ({0}) reached," + " integration needs {1}", new Object[] { new Double(minStep), new Double(Math.abs(h)) }); } } if (h > maxStep) { h = maxStep; } else if (h < -maxStep) { h = -maxStep; } return h; } /** Integrate the differential equations up to the given time. * <p>This method solves an Initial Value Problem (IVP).</p> * <p>Since this method stores some internal state variables made * available in its public interface during integration ({@link * #getCurrentSignedStepsize()}), it is <em>not</em> thread-safe.</p> * @param equations differential equations to integrate * @param t0 initial time * @param y0 initial value of the state vector at t0 * @param t target time for the integration * (can be set to a value smaller than <code>t0</code> for backward integration) * @param y placeholder where to put the state vector at each successful * step (and hence at the end of integration), can be the same object as y0 * @throws IntegratorException if the integrator cannot perform integration * @throws DerivativeException this exception is propagated to the caller if * the underlying user function triggers one */ public abstract void integrate (FirstOrderDifferentialEquations equations, double t0, double[] y0, double t, double[] y) throws DerivativeException, IntegratorException; /** Get the current value of the step start time t<sub>i</sub>. * <p>This method can be called during integration (typically by * the object implementing the {@link FirstOrderDifferentialEquations * differential equations} problem) if the value of the current step that * is attempted is needed.</p> * <p>The result is undefined if the method is called outside of * calls to {@link #integrate}</p> * @return current value of the step start time t<sub>i</sub> */ public double getCurrentStepStart() { return stepStart; } /** Get the current signed value of the integration stepsize. * <p>This method can be called during integration (typically by * the object implementing the {@link FirstOrderDifferentialEquations * differential equations} problem) if the signed value of the current stepsize * that is tried is needed.</p> * <p>The result is undefined if the method is called outside of * calls to {@link #integrate}</p> * @return current signed value of the stepsize */ public double getCurrentSignedStepsize() { return stepSize; } /** Reset internal state to dummy values. */ protected void resetInternalState() { stepStart = Double.NaN; stepSize = Math.sqrt(minStep * maxStep); } /** Get the minimal step. * @return minimal step */ public double getMinStep() { return minStep; } /** Get the maximal step. * @return maximal step */ public double getMaxStep() { return maxStep; } /** Minimal step. */ private double minStep; /** Maximal step. */ private double maxStep; /** User supplied initial step. */ private double initialStep; /** Allowed absolute scalar error. */ protected double scalAbsoluteTolerance; /** Allowed relative scalar error. */ protected double scalRelativeTolerance; /** Allowed absolute vectorial error. */ protected double[] vecAbsoluteTolerance; /** Allowed relative vectorial error. */ protected double[] vecRelativeTolerance; /** Step handler. */ protected StepHandler handler; /** Switching functions handler. */ protected SwitchingFunctionsHandler switchesHandler; /** Current step start time. */ protected double stepStart; /** Current stepsize. */ protected double stepSize;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -