diff --git a/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java b/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java index 7be46371c..0b1f9d6bc 100644 --- a/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java +++ b/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java @@ -154,6 +154,58 @@ public abstract class AdaptiveStepsizeIntegrator switchesHandler.add(function, maxCheckInterval, convergence); } + /** Perform some sanity checks on the integration parameters. + * @param equations differential equations set + * @param t0 start time + * @param y0 state vector at t0 + * @param t target time for the integration + * @param y placeholder where to put the state vector + * @exception IntegratorException if some inconsistency is detected + */ + protected void sanityChecks(FirstOrderDifferentialEquations equations, + double t0, double[] y0, double t, double[] y) + throws IntegratorException { + if (equations.getDimension() != y0.length) { + throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," + + " initial state vector has dimension {1}", + new String[] { + Integer.toString(equations.getDimension()), + Integer.toString(y0.length) + }); + } + if (equations.getDimension() != y.length) { + throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," + + " final state vector has dimension {1}", + new String[] { + Integer.toString(equations.getDimension()), + Integer.toString(y.length) + }); + } + if ((vecAbsoluteTolerance != null) && (vecAbsoluteTolerance.length != y0.length)) { + throw new IntegratorException("dimensions mismatch: state vector has dimension {0}," + + " absolute tolerance vector has dimension {1}", + new String[] { + Integer.toString(y0.length), + Integer.toString(vecAbsoluteTolerance.length) + }); + } + if ((vecRelativeTolerance != null) && (vecRelativeTolerance.length != y0.length)) { + throw new IntegratorException("dimensions mismatch: state vector has dimension {0}," + + " relative tolerance vector has dimension {1}", + new String[] { + Integer.toString(y0.length), + Integer.toString(vecRelativeTolerance.length) + }); + } + if (Math.abs(t - t0) <= 1.0e-12 * Math.max(Math.abs(t0), Math.abs(t))) { + throw new IntegratorException("too small integration interval: length = {0}", + new String[] { + Double.toString(Math.abs(t - t0)) + }); + } + + } + /** Initialize the integration step. * @param equations differential equations set * @param forward forward integration indicator diff --git a/src/java/org/apache/commons/math/ode/GraggBulirschStoerIntegrator.java b/src/java/org/apache/commons/math/ode/GraggBulirschStoerIntegrator.java index 8f7433673..8bfadaf8f 100644 --- a/src/java/org/apache/commons/math/ode/GraggBulirschStoerIntegrator.java +++ b/src/java/org/apache/commons/math/ode/GraggBulirschStoerIntegrator.java @@ -508,23 +508,7 @@ public class GraggBulirschStoerIntegrator double t0, double[] y0, double t, double[] y) throws DerivativeException, IntegratorException { - // sanity check - if (equations.getDimension() != y0.length) { - throw new IntegratorException("dimensions mismatch: " - + "ODE problem has dimension {0}" - + ", state vector has dimension {1}", - new String[] { - Integer.toString(equations.getDimension()), - Integer.toString(y0.length) - }); - } - if (Math.abs(t - t0) <= 1.0e-12 * Math.max(Math.abs(t0), Math.abs(t))) { - throw new IntegratorException("too small integration interval: length = {0}", - new String[] { - Double.toString(Math.abs(t - t0)) - }); - } - + sanityChecks(equations, t0, y0, t, y); boolean forward = (t > t0); // create some internal working arrays diff --git a/src/java/org/apache/commons/math/ode/RungeKuttaFehlbergIntegrator.java b/src/java/org/apache/commons/math/ode/RungeKuttaFehlbergIntegrator.java index f62e6ad85..a6b867eb0 100644 --- a/src/java/org/apache/commons/math/ode/RungeKuttaFehlbergIntegrator.java +++ b/src/java/org/apache/commons/math/ode/RungeKuttaFehlbergIntegrator.java @@ -162,22 +162,7 @@ public abstract class RungeKuttaFehlbergIntegrator double t, double[] y) throws DerivativeException, IntegratorException { - // sanity check - if (equations.getDimension() != y0.length) { - throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," - + " state vector has dimension {1}", - new String[] { - Integer.toString(equations.getDimension()), - Integer.toString(y0.length) - }); - } - if (Math.abs(t - t0) <= 1.0e-12 * Math.max(Math.abs(t0), Math.abs(t))) { - throw new IntegratorException("too small integration interval: length = {0}", - new String[] { - Double.toString(Math.abs(t - t0)) - }); - } - + sanityChecks(equations, t0, y0, t, y); boolean forward = (t > t0); // create some internal working arrays diff --git a/src/java/org/apache/commons/math/ode/RungeKuttaIntegrator.java b/src/java/org/apache/commons/math/ode/RungeKuttaIntegrator.java index 50876a07a..dff1097f3 100644 --- a/src/java/org/apache/commons/math/ode/RungeKuttaIntegrator.java +++ b/src/java/org/apache/commons/math/ode/RungeKuttaIntegrator.java @@ -114,27 +114,47 @@ public abstract class RungeKuttaIntegrator switchesHandler.add(function, maxCheckInterval, convergence); } - public void integrate(FirstOrderDifferentialEquations equations, - double t0, double[] y0, - double t, double[] y) - throws DerivativeException, IntegratorException { - - // sanity check + /** Perform some sanity checks on the integration parameters. + * @param equations differential equations set + * @param t0 start time + * @param y0 state vector at t0 + * @param t target time for the integration + * @param y placeholder where to put the state vector + * @exception IntegratorException if some inconsistency is detected + */ + private void sanityChecks(FirstOrderDifferentialEquations equations, + double t0, double[] y0, double t, double[] y) + throws IntegratorException { if (equations.getDimension() != y0.length) { throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," - + " state vector has dimension {1}", + + " initial state vector has dimension {1}", new String[] { Integer.toString(equations.getDimension()), Integer.toString(y0.length) }); } + if (equations.getDimension() != y.length) { + throw new IntegratorException("dimensions mismatch: ODE problem has dimension {0}," + + " final state vector has dimension {1}", + new String[] { + Integer.toString(equations.getDimension()), + Integer.toString(y.length) + }); + } if (Math.abs(t - t0) <= 1.0e-12 * Math.max(Math.abs(t0), Math.abs(t))) { throw new IntegratorException("too small integration interval: length = {0}", new String[] { Double.toString(Math.abs(t - t0)) }); - } - + } + } + + public void integrate(FirstOrderDifferentialEquations equations, + double t0, double[] y0, + double t, double[] y) + throws DerivativeException, IntegratorException { + + sanityChecks(equations, t0, y0, t, y); boolean forward = (t > t0); // create some internal working arrays