1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.ode;
19  
20  import org.apache.commons.math.ode.DerivativeException;
21  import org.apache.commons.math.ode.DormandPrince54Integrator;
22  import org.apache.commons.math.ode.FirstOrderIntegrator;
23  import org.apache.commons.math.ode.FixedStepHandler;
24  import org.apache.commons.math.ode.IntegratorException;
25  import org.apache.commons.math.ode.StepNormalizer;
26  
27  import junit.framework.*;
28  
29  public class StepNormalizerTest
30    extends TestCase {
31  
32    public StepNormalizerTest(String name) {
33      super(name);
34      pb    = null;
35      integ = null;
36    }
37  
38    public void testBoundaries()
39      throws DerivativeException, IntegratorException {
40      double range = pb.getFinalTime() - pb.getInitialTime();
41      setLastSeen(false);
42      integ.setStepHandler(new StepNormalizer(range / 10.0,
43                                         new FixedStepHandler() {
44                                           private boolean firstCall = true;
45                                           public void handleStep(double t,
46                                                                  double[] y,
47                                                                  boolean isLast) {
48                                             if (firstCall) {
49                                               checkValue(t, pb.getInitialTime());
50                                               firstCall = false;
51                                             }
52                                             if (isLast) {
53                                               setLastSeen(true);
54                                               checkValue(t, pb.getFinalTime());
55                                             }
56                                           }
57                                         }));
58      integ.integrate(pb,
59                      pb.getInitialTime(), pb.getInitialState(),
60                      pb.getFinalTime(), new double[pb.getDimension()]);
61      assertTrue(lastSeen);
62    }
63  
64    public void testBeforeEnd()
65      throws DerivativeException, IntegratorException {
66      final double range = pb.getFinalTime() - pb.getInitialTime();
67      setLastSeen(false);
68      integ.setStepHandler(new StepNormalizer(range / 10.5,
69                                         new FixedStepHandler() {
70                                           public void handleStep(double t,
71                                                                  double[] y,
72                                                                  boolean isLast) {
73                                             if (isLast) {
74                                               setLastSeen(true);
75                                               checkValue(t,
76                                                          pb.getFinalTime() - range / 21.0);
77                                             }
78                                           }
79                                         }));
80      integ.integrate(pb,
81                      pb.getInitialTime(), pb.getInitialState(),
82                      pb.getFinalTime(), new double[pb.getDimension()]);
83      assertTrue(lastSeen);
84    }
85  
86    public void checkValue(double value, double reference) {
87      assertTrue(Math.abs(value - reference) < 1.0e-10);
88    }
89  
90    public void setLastSeen(boolean lastSeen) {
91      this.lastSeen = lastSeen;
92    }
93  
94    public static Test suite() {
95      return new TestSuite(StepNormalizerTest.class);
96    }
97  
98    public void setUp() {
99      pb = new TestProblem3(0.9);
100     double minStep = 0;
101     double maxStep = pb.getFinalTime() - pb.getInitialTime();
102     integ = new DormandPrince54Integrator(minStep, maxStep, 10.e-8, 1.0e-8);
103     lastSeen = false;
104   }
105 
106   public void tearDown() {
107     pb    = null;
108     integ = null;
109   }
110 
111   TestProblem3 pb;
112   FirstOrderIntegrator integ;
113   boolean lastSeen;
114 
115 }