View Javadoc
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.math4.legacy.ode.sampling;
19  
20  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
21  import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
22  import org.apache.commons.math4.legacy.exception.NoBracketingException;
23  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
24  import org.apache.commons.math4.legacy.ode.FirstOrderIntegrator;
25  import org.apache.commons.math4.legacy.ode.TestProblem3;
26  import org.apache.commons.math4.legacy.ode.nonstiff.DormandPrince54Integrator;
27  import org.apache.commons.math4.core.jdkmath.JdkMath;
28  import org.junit.After;
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  
34  public class StepNormalizerTest {
35  
36    public StepNormalizerTest() {
37      pb    = null;
38      integ = null;
39    }
40  
41    @Test
42    public void testBoundaries()
43        throws DimensionMismatchException, NumberIsTooSmallException,
44               MaxCountExceededException, NoBracketingException {
45      double range = pb.getFinalTime() - pb.getInitialTime();
46      setLastSeen(false);
47      integ.addStepHandler(new StepNormalizer(range / 10.0,
48                                         new FixedStepHandler() {
49                                           private boolean firstCall = true;
50                                           @Override
51                                          public void init(double t0, double[] y0, double t) {
52                                           }
53                                           @Override
54                                          public void handleStep(double t,
55                                                                  double[] y,
56                                                                  double[] yDot,
57                                                                  boolean isLast) {
58                                             if (firstCall) {
59                                               checkValue(t, pb.getInitialTime());
60                                               firstCall = false;
61                                             }
62                                             if (isLast) {
63                                               setLastSeen(true);
64                                               checkValue(t, pb.getFinalTime());
65                                             }
66                                           }
67                                         }));
68      integ.integrate(pb,
69                      pb.getInitialTime(), pb.getInitialState(),
70                      pb.getFinalTime(), new double[pb.getDimension()]);
71      Assert.assertTrue(lastSeen);
72    }
73  
74    @Test
75    public void testBeforeEnd()
76        throws DimensionMismatchException, NumberIsTooSmallException,
77               MaxCountExceededException, NoBracketingException {
78      final double range = pb.getFinalTime() - pb.getInitialTime();
79      setLastSeen(false);
80      integ.addStepHandler(new StepNormalizer(range / 10.5,
81                                         new FixedStepHandler() {
82                                           @Override
83                                          public void init(double t0, double[] y0, double t) {
84                                           }
85                                           @Override
86                                          public void handleStep(double t,
87                                                                  double[] y,
88                                                                  double[] yDot,
89                                                                  boolean isLast) {
90                                             if (isLast) {
91                                               setLastSeen(true);
92                                               checkValue(t,
93                                                          pb.getFinalTime() - range / 21.0);
94                                             }
95                                           }
96                                         }));
97      integ.integrate(pb,
98                      pb.getInitialTime(), pb.getInitialState(),
99                      pb.getFinalTime(), new double[pb.getDimension()]);
100     Assert.assertTrue(lastSeen);
101   }
102 
103   public void checkValue(double value, double reference) {
104     Assert.assertTrue(JdkMath.abs(value - reference) < 1.0e-10);
105   }
106 
107   public void setLastSeen(boolean lastSeen) {
108     this.lastSeen = lastSeen;
109   }
110 
111   @Before
112   public void setUp() {
113     pb = new TestProblem3(0.9);
114     double minStep = 0;
115     double maxStep = pb.getFinalTime() - pb.getInitialTime();
116     integ = new DormandPrince54Integrator(minStep, maxStep, 10.e-8, 1.0e-8);
117     lastSeen = false;
118   }
119 
120   @After
121   public void tearDown() {
122     pb    = null;
123     integ = null;
124   }
125 
126   private TestProblem3 pb;
127   private FirstOrderIntegrator integ;
128   private boolean lastSeen;
129 }