1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode.sampling;
19
20 import org.apache.commons.math.ode.DerivativeException;
21 import org.apache.commons.math.ode.FirstOrderIntegrator;
22 import org.apache.commons.math.ode.IntegratorException;
23 import org.apache.commons.math.ode.TestProblem3;
24 import org.apache.commons.math.ode.nonstiff.DormandPrince54Integrator;
25 import org.apache.commons.math.ode.sampling.FixedStepHandler;
26 import org.apache.commons.math.ode.sampling.StepNormalizer;
27
28 import junit.framework.*;
29
30 public class StepNormalizerTest
31 extends TestCase {
32
33 public StepNormalizerTest(String name) {
34 super(name);
35 pb = null;
36 integ = null;
37 }
38
39 public void testBoundaries()
40 throws DerivativeException, IntegratorException {
41 double range = pb.getFinalTime() - pb.getInitialTime();
42 setLastSeen(false);
43 integ.addStepHandler(new StepNormalizer(range / 10.0,
44 new FixedStepHandler() {
45 private static final long serialVersionUID = 1650337364641626444L;
46 private boolean firstCall = true;
47 public void handleStep(double t,
48 double[] y,
49 double[] yDot,
50 boolean isLast) {
51 if (firstCall) {
52 checkValue(t, pb.getInitialTime());
53 firstCall = false;
54 }
55 if (isLast) {
56 setLastSeen(true);
57 checkValue(t, pb.getFinalTime());
58 }
59 }
60 }));
61 integ.integrate(pb,
62 pb.getInitialTime(), pb.getInitialState(),
63 pb.getFinalTime(), new double[pb.getDimension()]);
64 assertTrue(lastSeen);
65 }
66
67 public void testBeforeEnd()
68 throws DerivativeException, IntegratorException {
69 final double range = pb.getFinalTime() - pb.getInitialTime();
70 setLastSeen(false);
71 integ.addStepHandler(new StepNormalizer(range / 10.5,
72 new FixedStepHandler() {
73 private static final long serialVersionUID = 2228457391561277298L;
74 public void handleStep(double t,
75 double[] y,
76 double[] yDot,
77 boolean isLast) {
78 if (isLast) {
79 setLastSeen(true);
80 checkValue(t,
81 pb.getFinalTime() - range / 21.0);
82 }
83 }
84 }));
85 integ.integrate(pb,
86 pb.getInitialTime(), pb.getInitialState(),
87 pb.getFinalTime(), new double[pb.getDimension()]);
88 assertTrue(lastSeen);
89 }
90
91 public void checkValue(double value, double reference) {
92 assertTrue(Math.abs(value - reference) < 1.0e-10);
93 }
94
95 public void setLastSeen(boolean lastSeen) {
96 this.lastSeen = lastSeen;
97 }
98
99 public static Test suite() {
100 return new TestSuite(StepNormalizerTest.class);
101 }
102
103 @Override
104 public void setUp() {
105 pb = new TestProblem3(0.9);
106 double minStep = 0;
107 double maxStep = pb.getFinalTime() - pb.getInitialTime();
108 integ = new DormandPrince54Integrator(minStep, maxStep, 10.e-8, 1.0e-8);
109 lastSeen = false;
110 }
111
112 @Override
113 public void tearDown() {
114 pb = null;
115 integ = null;
116 }
117
118 TestProblem3 pb;
119 FirstOrderIntegrator integ;
120 boolean lastSeen;
121
122 }