org.apache.commons.math.ode.IntegratorException Java Examples

The following examples show how to use org.apache.commons.math.ode.IntegratorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DormandPrince853IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testMinStep() {

    try {
      TestProblem1 pb = new TestProblem1();
      double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime());
      double maxStep = pb.getFinalTime() - pb.getInitialTime();
      double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 };
      double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 };

      FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep,
                                                                  vecAbsoluteTolerance,
                                                                  vecRelativeTolerance);
      TestProblemHandler handler = new TestProblemHandler(pb, integ);
      integ.addStepHandler(handler);
      integ.integrate(pb,
                      pb.getInitialTime(), pb.getInitialState(),
                      pb.getFinalTime(), new double[pb.getDimension()]);
      fail("an exception should have been thrown");
    } catch(DerivativeException de) {
      fail("wrong exception caught");
    } catch(IntegratorException ie) {
    }

  }
 
Example #2
Source File: DormandPrince853IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testKepler()
  throws DerivativeException, IntegratorException {

  final TestProblem3 pb  = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;

  FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep,
                                                              scalAbsoluteTolerance,
                                                              scalRelativeTolerance);
  integ.addStepHandler(new KeplerHandler(pb));
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertEquals(integ.getEvaluations(), pb.getCalls());
  assertTrue(pb.getCalls() < 3300);

}
 
Example #3
Source File: FirstOrderIntegratorWithJacobiansTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFinalResult() throws IntegratorException, DerivativeException {
    FirstOrderIntegrator integ =
        new DormandPrince54Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
    double[] y = new double[] { 0.0, 1.0 };
    Circle circle = new Circle(y, 1.0, 1.0, 0.1);
    double[][] dydy0 = new double[2][2];
    double[][] dydp  = new double[2][3];
    double t = 18 * Math.PI;
    FirstOrderIntegratorWithJacobians extInt =
        new FirstOrderIntegratorWithJacobians(integ, circle);
    extInt.integrate(0, y, circle.exactDyDp(0), t, y, dydy0, dydp);
    for (int i = 0; i < y.length; ++i) {
        Assert.assertEquals(circle.exactY(t)[i], y[i], 1.0e-10);
    }
    for (int i = 0; i < dydy0.length; ++i) {
        for (int j = 0; j < dydy0[i].length; ++j) {
            Assert.assertEquals(circle.exactDyDy0(t)[i][j], dydy0[i][j], 1.0e-10);
        }
    }
    for (int i = 0; i < dydp.length; ++i) {
        for (int j = 0; j < dydp[i].length; ++j) {
            Assert.assertEquals(circle.exactDyDp(t)[i][j], dydp[i][j], 1.0e-8);
        }
    }
}
 
Example #4
Source File: GraggBulirschStoerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testVariableSteps()
  throws MathUserException, IntegratorException {

  final TestProblem3 pb = new TestProblem3(0.9);
  double minStep        = 0;
  double maxStep        = pb.getFinalTime() - pb.getInitialTime();
  double absTolerance   = 1.0e-8;
  double relTolerance   = 1.0e-8;
  FirstOrderIntegrator integ =
    new GraggBulirschStoerIntegrator(minStep, maxStep,
                                     absTolerance, relTolerance);
  integ.addStepHandler(new VariableStepHandler());
  double stopTime = integ.integrate(pb,
                                    pb.getInitialTime(), pb.getInitialState(),
                                    pb.getFinalTime(), new double[pb.getDimension()]);
  Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10);
  Assert.assertEquals("Gragg-Bulirsch-Stoer", integ.getName());
}
 
Example #5
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSmallStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;

  FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() < 2.0e-13);
  assertTrue(handler.getMaximalValueError() < 4.0e-12);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
  assertEquals("classical Runge-Kutta", integ.getName());
}
 
Example #6
Source File: DormandPrince853IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testMinStep() {

    try {
      TestProblem1 pb = new TestProblem1();
      double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime());
      double maxStep = pb.getFinalTime() - pb.getInitialTime();
      double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 };
      double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 };

      FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep,
                                                                  vecAbsoluteTolerance,
                                                                  vecRelativeTolerance);
      TestProblemHandler handler = new TestProblemHandler(pb, integ);
      integ.addStepHandler(handler);
      integ.integrate(pb,
                      pb.getInitialTime(), pb.getInitialState(),
                      pb.getFinalTime(), new double[pb.getDimension()]);
      fail("an exception should have been thrown");
    } catch(DerivativeException de) {
      fail("wrong exception caught");
    } catch(IntegratorException ie) {
    }

  }
 
Example #7
Source File: FirstOrderConverterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testDecreasingSteps()
  throws DerivativeException, IntegratorException {

  double previousError = Double.NaN;
  for (int i = 0; i < 10; ++i) {

    double step  = FastMath.pow(2.0, -(i + 1));
    double error = integrateWithSpecifiedStep(4.0, 0.0, 1.0, step)
                 - FastMath.sin(4.0);
    if (i > 0) {
      assertTrue(FastMath.abs(error) < FastMath.abs(previousError));
    }
    previousError = error;

  }
}
 
Example #8
Source File: AdaptiveStepsizeIntegrator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 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
 */
@Override
protected void sanityChecks(final FirstOrderDifferentialEquations equations,
                            final double t0, final double[] y0,
                            final double t, final double[] y)
    throws IntegratorException {

    super.sanityChecks(equations, t0, y0, t, y);

    if ((vecAbsoluteTolerance != null) && (vecAbsoluteTolerance.length != y0.length)) {
        throw new IntegratorException(
                "dimensions mismatch: state vector has dimension {0}," +
                " absolute tolerance vector has dimension {1}",
                y0.length, 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}",
                y0.length, vecRelativeTolerance.length);
    }

}
 
Example #9
Source File: StepNormalizerOutputTestBase.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The actual step normalizer output test code, shared by all the unit
 * tests.
 *
 * @param mode the step normalizer mode to use
 * @param bounds the step normalizer bounds setting to use
 * @param expected the expected output (normalized time points)
 * @param reverse whether to reverse the integration direction
 */
private void doTest(StepNormalizerMode mode, StepNormalizerBounds bounds,
                    double[] expected, boolean reverse)
    throws MathUserException, IntegratorException
{
    // Forward test.
    FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(
                                                    1e-8, 1.0, 1e-5, 1e-5);
    integ.addStepHandler(new StepNormalizer(0.5, this, mode, bounds));
    double[] y   = {0.0};
    double start = reverse ? getEnd()   : getStart();
    double end   = reverse ? getStart() : getEnd();
    output       = new ArrayList<Double>();
    integ.integrate(this, start, y, end, y);
    double[] actual = new double[output.size()];
    for(int i = 0; i < actual.length; i++) {
        actual[i] = output.get(i);
    }
    assertArrayEquals(expected, actual, 1e-5);
}
 
Example #10
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBigStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb  = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;

  FirstOrderIntegrator integ = new MidpointIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() > 0.01);
  assertTrue(handler.getMaximalValueError() > 0.05);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);

}
 
Example #11
Source File: GraggBulirschStoerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testVariableSteps()
  throws DerivativeException, IntegratorException {

  final TestProblem3 pb = new TestProblem3(0.9);
  double minStep        = 0;
  double maxStep        = pb.getFinalTime() - pb.getInitialTime();
  double absTolerance   = 1.0e-8;
  double relTolerance   = 1.0e-8;
  FirstOrderIntegrator integ =
    new GraggBulirschStoerIntegrator(minStep, maxStep,
                                     absTolerance, relTolerance);
  integ.addStepHandler(new VariableStepHandler());
  double stopTime = integ.integrate(pb,
                                    pb.getInitialTime(), pb.getInitialState(),
                                    pb.getFinalTime(), new double[pb.getDimension()]);
  assertEquals(pb.getFinalTime(), stopTime, 1.0e-10);
  assertEquals("Gragg-Bulirsch-Stoer", integ.getName());
}
 
Example #12
Source File: FirstOrderConverterTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testDecreasingSteps()
  throws DerivativeException, IntegratorException {
    
  double previousError = Double.NaN;
  for (int i = 0; i < 10; ++i) {

    double step  = Math.pow(2.0, -(i + 1));
    double error = integrateWithSpecifiedStep(4.0, 0.0, 1.0, step)
                 - Math.sin(4.0);
    if (i > 0) {
      assertTrue(Math.abs(error) < Math.abs(previousError));
    }
    previousError = error;
    
  }
}
 
Example #13
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBackward()
    throws DerivativeException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double step = Math.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;

    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    assertTrue(handler.getLastError() < 6.0e-4);
    assertTrue(handler.getMaximalValueError() < 6.0e-4);
    assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    assertEquals("midpoint", integ.getName());
}
 
Example #14
Source File: EulerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBigStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb  = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;

  FirstOrderIntegrator integ = new EulerIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() > 0.01);
  assertTrue(handler.getMaximalValueError() > 0.2);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);

}
 
Example #15
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBigStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;

  FirstOrderIntegrator integ = new GillIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() > 0.0004);
  assertTrue(handler.getMaximalValueError() > 0.005);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);

}
 
Example #16
Source File: GraggBulirschStoerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testKepler()
  throws DerivativeException, IntegratorException {

  final TestProblem3 pb = new TestProblem3(0.9);
  double minStep        = 0;
  double maxStep        = pb.getFinalTime() - pb.getInitialTime();
  double absTolerance   = 1.0e-6;
  double relTolerance   = 1.0e-6;

  FirstOrderIntegrator integ =
    new GraggBulirschStoerIntegrator(minStep, maxStep,
                                     absTolerance, relTolerance);
  integ.addStepHandler(new KeplerStepHandler(pb));
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertEquals(integ.getEvaluations(), pb.getCalls());
  assertTrue(pb.getCalls() < 2150);

}
 
Example #17
Source File: AdamsMoultonIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=IntegratorException.class)
public void testMinStep() throws DerivativeException, IntegratorException {

      TestProblem1 pb = new TestProblem1();
      double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime());
      double maxStep = pb.getFinalTime() - pb.getInitialTime();
      double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 };
      double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 };

      FirstOrderIntegrator integ = new AdamsMoultonIntegrator(4, minStep, maxStep,
                                                              vecAbsoluteTolerance,
                                                              vecRelativeTolerance);
      TestProblemHandler handler = new TestProblemHandler(pb, integ);
      integ.addStepHandler(handler);
      integ.integrate(pb,
                      pb.getInitialTime(), pb.getInitialState(),
                      pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #18
Source File: EulerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBackward()
    throws MathUserException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;

    FirstOrderIntegrator integ = new EulerIntegrator(step);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    Assert.assertTrue(handler.getLastError() < 0.45);
    Assert.assertTrue(handler.getMaximalValueError() < 0.45);
    Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    Assert.assertEquals("Euler", integ.getName());
}
 
Example #19
Source File: StepNormalizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBeforeEnd()
  throws DerivativeException, IntegratorException {
  final double range = pb.getFinalTime() - pb.getInitialTime();
  setLastSeen(false);
  integ.addStepHandler(new StepNormalizer(range / 10.5,
                                     new FixedStepHandler() {
                                      private static final long serialVersionUID = 2228457391561277298L;
                                      public void handleStep(double t,
                                                              double[] y,
                                                              double[] yDot,
                                                              boolean isLast) {
                                         if (isLast) {
                                           setLastSeen(true);
                                           checkValue(t,
                                                      pb.getFinalTime() - range / 21.0);
                                         }
                                       }
                                     }));
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);
  assertTrue(lastSeen);
}
 
Example #20
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSmallStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;

  FirstOrderIntegrator integ = new GillIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() < 2.0e-13);
  assertTrue(handler.getMaximalValueError() < 4.0e-12);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
  assertEquals("Gill", integ.getName());

}
 
Example #21
Source File: DormandPrince54IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBackward()
    throws DerivativeException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double minStep = 0;
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
    double scalAbsoluteTolerance = 1.0e-8;
    double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance;

    FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep,
                                                               scalAbsoluteTolerance,
                                                               scalRelativeTolerance);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    assertTrue(handler.getLastError() < 2.0e-7);
    assertTrue(handler.getMaximalValueError() < 2.0e-7);
    assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    assertEquals("Dormand-Prince 5(4)", integ.getName());
}
 
Example #22
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBackward()
  throws DerivativeException, IntegratorException {

  TestProblem5 pb = new TestProblem5();
  double step = Math.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;

  FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  assertTrue(handler.getLastError() < 5.0e-10);
  assertTrue(handler.getMaximalValueError() < 7.0e-10);
  assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
  assertEquals("classical Runge-Kutta", integ.getName());
}
 
Example #23
Source File: HighamHall54IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBackward()
    throws DerivativeException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double minStep = 0;
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
    double scalAbsoluteTolerance = 1.0e-8;
    double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance;

    FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    assertTrue(handler.getLastError() < 5.0e-7);
    assertTrue(handler.getMaximalValueError() < 5.0e-7);
    assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    assertEquals("Higham-Hall 5(4)", integ.getName());
}
 
Example #24
Source File: DormandPrince853IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testBackward()
    throws MathUserException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double minStep = 0;
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
    double scalAbsoluteTolerance = 1.0e-8;
    double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance;

    FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep,
                                                                scalAbsoluteTolerance,
                                                                scalRelativeTolerance);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    Assert.assertTrue(handler.getLastError() < 1.1e-7);
    Assert.assertTrue(handler.getMaximalValueError() < 1.1e-7);
    Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    Assert.assertEquals("Dormand-Prince 8 (5, 3)", integ.getName());
}
 
Example #25
Source File: EulerIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSmallStep()
  throws DerivativeException, IntegratorException {

  TestProblem1 pb  = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;

  FirstOrderIntegrator integ = new EulerIntegrator(step);
  TestProblemHandler handler = new TestProblemHandler(pb, integ);
  integ.addStepHandler(handler);
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

 assertTrue(handler.getLastError() < 2.0e-4);
 assertTrue(handler.getMaximalValueError() < 1.0e-3);
 assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
 assertEquals("Euler", integ.getName());

}
 
Example #26
Source File: HighamHall54IntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinStep() {

  try {
    TestProblem1 pb = new TestProblem1();
    double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime());
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
    double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 };
    double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 };

    FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            vecAbsoluteTolerance,
                                                            vecRelativeTolerance);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb,
                    pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);
    Assert.fail("an exception should have been thrown");
  } catch(MathUserException de) {
    Assert.fail("wrong exception caught");
  } catch(IntegratorException ie) {
  }

}
 
Example #27
Source File: AdamsMoultonIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=IntegratorException.class)
public void testMinStep() throws DerivativeException, IntegratorException {

      TestProblem1 pb = new TestProblem1();
      double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime());
      double maxStep = pb.getFinalTime() - pb.getInitialTime();
      double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 };
      double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 };

      FirstOrderIntegrator integ = new AdamsMoultonIntegrator(4, minStep, maxStep,
                                                              vecAbsoluteTolerance,
                                                              vecRelativeTolerance);
      TestProblemHandler handler = new TestProblemHandler(pb, integ);
      integ.addStepHandler(handler);
      integ.integrate(pb,
                      pb.getInitialTime(), pb.getInitialState(),
                      pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #28
Source File: FirstOrderIntegratorWithJacobiansTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAnalyticalDifferentiation()
    throws IntegratorException, DerivativeException {
    FirstOrderIntegrator integ =
        new DormandPrince54Integrator(1.0e-8, 100.0, 1.0e-4, 1.0e-4);
    SummaryStatistics residualsP0 = new SummaryStatistics();
    SummaryStatistics residualsP1 = new SummaryStatistics();
    for (double b = 2.88; b < 3.08; b += 0.001) {
        Brusselator brusselator = new Brusselator(b);
        brusselator.setParameter(0, b);
        double[] z = { 1.3, b };
        double[][] dZdZ0 = new double[2][2];
        double[][] dZdP  = new double[2][1];
        FirstOrderIntegratorWithJacobians extInt =
            new FirstOrderIntegratorWithJacobians(integ, brusselator);
        extInt.setMaxEvaluations(5000);
        extInt.integrate(0, z, new double[][] { { 0.0 }, { 1.0 } }, 20.0, z, dZdZ0, dZdP);
        Assert.assertEquals(5000, extInt.getMaxEvaluations());
        Assert.assertTrue(extInt.getEvaluations() > 510);
        Assert.assertTrue(extInt.getEvaluations() < 610);
        Assert.assertEquals(integ.getEvaluations(), extInt.getEvaluations());
        residualsP0.addValue(dZdP[0][0] - brusselator.dYdP0());
        residualsP1.addValue(dZdP[1][0] - brusselator.dYdP1());
    }
    Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) < 0.004);
    Assert.assertTrue(residualsP0.getStandardDeviation() < 0.0008);
    Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) < 0.005);
    Assert.assertTrue(residualsP1.getStandardDeviation() < 0.0010);
}
 
Example #29
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new GillIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }          
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #30
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testDecreasingSteps()
  throws DerivativeException, IntegratorException  {

  TestProblemAbstract[] problems = TestProblemFactory.getProblems();
  for (int k = 0; k < problems.length; ++k) {

    double previousError = Double.NaN;
    for (int i = 5; i < 10; ++i) {

      TestProblemAbstract pb = problems[k].copy();
      double step = (pb.getFinalTime() - pb.getInitialTime())
        * Math.pow(2.0, -i);

      FirstOrderIntegrator integ = new GillIntegrator(step);
      TestProblemHandler handler = new TestProblemHandler(pb, integ);
      integ.addStepHandler(handler);
      EventHandler[] functions = pb.getEventsHandlers();
      for (int l = 0; l < functions.length; ++l) {
        integ.addEventHandler(functions[l],
                                   Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000);
      }
      double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                                        pb.getFinalTime(), new double[pb.getDimension()]);
      if (functions.length == 0) {
          assertEquals(pb.getFinalTime(), stopTime, 1.0e-10);
      }

      double error = handler.getMaximalValueError();
      if (i > 5) {
        assertTrue(error < Math.abs(previousError));
      }
      previousError = error;
      assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);

    }

  }

}