blob: 3ed14b357d0c7ff7b18385be91a93071e9b79dbe [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.legacy.ode.nonstiff;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
import org.apache.commons.math4.legacy.exception.NoBracketingException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
import org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations;
import org.apache.commons.math4.legacy.ode.FirstOrderIntegrator;
import org.apache.commons.math4.legacy.ode.TestProblem1;
import org.apache.commons.math4.legacy.ode.TestProblem3;
import org.apache.commons.math4.legacy.ode.TestProblem4;
import org.apache.commons.math4.legacy.ode.TestProblem5;
import org.apache.commons.math4.legacy.ode.TestProblemHandler;
import org.apache.commons.math4.legacy.ode.events.EventHandler;
import org.apache.commons.math4.core.jdkmath.JdkMath;
import org.junit.Assert;
import org.junit.Test;
public class HighamHall54IntegratorTest {
@Test
public void testWrongDerivative()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
HighamHall54Integrator integrator =
new HighamHall54Integrator(0.0, 1.0, 1.0e-10, 1.0e-10);
FirstOrderDifferentialEquations equations =
new FirstOrderDifferentialEquations() {
@Override
public void computeDerivatives(double t, double[] y, double[] dot) {
if (t < -0.5) {
throw new LocalException();
} else {
throw new RuntimeException("oops");
}
}
@Override
public int getDimension() {
return 1;
}
};
try {
integrator.integrate(equations, -1.0, new double[1], 0.0, new double[1]);
Assert.fail("an exception should have been thrown");
} catch(LocalException de) {
// expected behavior
}
try {
integrator.integrate(equations, 0.0, new double[1], 1.0, new double[1]);
Assert.fail("an exception should have been thrown");
} catch(RuntimeException de) {
// expected behavior
}
}
@Test(expected=NumberIsTooSmallException.class)
public void testMinStep()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
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");
}
@Test
public void testIncreasingTolerance()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
int previousCalls = Integer.MAX_VALUE;
for (int i = -12; i < -2; ++i) {
TestProblem1 pb = new TestProblem1();
double minStep = 0;
double maxStep = pb.getFinalTime() - pb.getInitialTime();
double scalAbsoluteTolerance = JdkMath.pow(10.0, i);
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()]);
// the 1.3 factor is only valid for this test
// and has been obtained from trial and error
// there is no general relation between local and global errors
Assert.assertTrue(handler.getMaximalValueError() < (1.3 * scalAbsoluteTolerance));
Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
int calls = pb.getCalls();
Assert.assertEquals(integ.getEvaluations(), calls);
Assert.assertTrue(calls <= previousCalls);
previousCalls = calls;
}
}
@Test
public void testBackward()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
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()]);
Assert.assertTrue(handler.getLastError() < 5.0e-7);
Assert.assertTrue(handler.getMaximalValueError() < 5.0e-7);
Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
Assert.assertEquals("Higham-Hall 5(4)", integ.getName());
}
@Test
public void testEvents()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
TestProblem4 pb = new TestProblem4();
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);
EventHandler[] functions = pb.getEventsHandlers();
double convergence = 1.0e-8 * maxStep;
for (int l = 0; l < functions.length; ++l) {
integ.addEventHandler(functions[l],
Double.POSITIVE_INFINITY, convergence, 1000);
}
Assert.assertEquals(functions.length, integ.getEventHandlers().size());
integ.integrate(pb,
pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[pb.getDimension()]);
Assert.assertTrue(handler.getMaximalValueError() < 1.0e-7);
Assert.assertEquals(0, handler.getMaximalTimeError(), convergence);
Assert.assertEquals(12.0, handler.getLastTime(), convergence);
integ.clearEventHandlers();
Assert.assertEquals(0, integ.getEventHandlers().size());
}
@Test(expected=LocalException.class)
public void testEventsErrors()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
final TestProblem1 pb = new TestProblem1();
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.addEventHandler(new EventHandler() {
@Override
public void init(double t0, double[] y0, double t) {
}
@Override
public Action eventOccurred(double t, double[] y, boolean increasing) {
return Action.CONTINUE;
}
@Override
public double g(double t, double[] y) {
double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2;
double offset = t - middle;
if (offset > 0) {
throw new LocalException();
}
return offset;
}
@Override
public void resetState(double t, double[] y) {
}
}, Double.POSITIVE_INFINITY, 1.0e-8 * maxStep, 1000);
integ.integrate(pb,
pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[pb.getDimension()]);
}
private static class LocalException extends RuntimeException {
private static final long serialVersionUID = 3041292643919807960L;
}
@Test
public void testEventsNoConvergence()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
final TestProblem1 pb = new TestProblem1();
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.addEventHandler(new EventHandler() {
@Override
public void init(double t0, double[] y0, double t) {
}
@Override
public Action eventOccurred(double t, double[] y, boolean increasing) {
return Action.CONTINUE;
}
@Override
public double g(double t, double[] y) {
double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2;
double offset = t - middle;
return (offset > 0) ? (offset + 0.5) : (offset - 0.5);
}
@Override
public void resetState(double t, double[] y) {
}
}, Double.POSITIVE_INFINITY, 1.0e-8 * maxStep, 3);
try {
integ.integrate(pb,
pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[pb.getDimension()]);
Assert.fail("an exception should have been thrown");
} catch (TooManyEvaluationsException tmee) {
// Expected.
}
}
@Test
public void testSanityChecks()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
final TestProblem3 pb = new TestProblem3(0.9);
double minStep = 0;
double maxStep = pb.getFinalTime() - pb.getInitialTime();
try {
FirstOrderIntegrator integ =
new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]);
integ.integrate(pb, pb.getInitialTime(), new double[6],
pb.getFinalTime(), new double[pb.getDimension()]);
Assert.fail("an exception should have been thrown");
} catch (DimensionMismatchException ie) {
// expected behavior
}
try {
FirstOrderIntegrator integ =
new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]);
integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[6]);
Assert.fail("an exception should have been thrown");
} catch (DimensionMismatchException ie) {
// expected behavior
}
try {
FirstOrderIntegrator integ =
new HighamHall54Integrator(minStep, maxStep, new double[2], new double[4]);
integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[pb.getDimension()]);
Assert.fail("an exception should have been thrown");
} catch (DimensionMismatchException ie) {
// expected behavior
}
try {
FirstOrderIntegrator integ =
new HighamHall54Integrator(minStep, maxStep, new double[4], new double[2]);
integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
pb.getFinalTime(), new double[pb.getDimension()]);
Assert.fail("an exception should have been thrown");
} catch (DimensionMismatchException ie) {
// expected behavior
}
try {
FirstOrderIntegrator integ =
new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]);
integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
pb.getInitialTime(), new double[pb.getDimension()]);
Assert.fail("an exception should have been thrown");
} catch (NumberIsTooSmallException ie) {
// expected behavior
}
}
@Test
public void testKepler()
throws DimensionMismatchException, NumberIsTooSmallException,
MaxCountExceededException, NoBracketingException {
final TestProblem3 pb = new TestProblem3(0.9);
double minStep = 0;
double maxStep = pb.getFinalTime() - pb.getInitialTime();
double[] vecAbsoluteTolerance = { 1.0e-8, 1.0e-8, 1.0e-10, 1.0e-10 };
double[] vecRelativeTolerance = { 1.0e-10, 1.0e-10, 1.0e-8, 1.0e-8 };
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.assertEquals(0.0, handler.getMaximalValueError(), 1.5e-4);
Assert.assertEquals("Higham-Hall 5(4)", integ.getName());
}
}