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 junit.framework.*;
21  
22  import org.apache.commons.math.ode.DerivativeException;
23  import org.apache.commons.math.ode.FirstOrderIntegrator;
24  import org.apache.commons.math.ode.IntegratorException;
25  import org.apache.commons.math.ode.MidpointIntegrator;
26  import org.apache.commons.math.ode.SwitchingFunction;
27  
28  public class MidpointIntegratorTest
29    extends TestCase {
30  
31    public MidpointIntegratorTest(String name) {
32      super(name);
33    }
34  
35    public void testDimensionCheck() {
36      try  {
37        TestProblem1 pb = new TestProblem1();
38        new MidpointIntegrator(0.01).integrate(pb,
39                                               0.0, new double[pb.getDimension()+10],
40                                               1.0, new double[pb.getDimension()+10]);
41          fail("an exception should have been thrown");
42      } catch(DerivativeException de) {
43        fail("wrong exception caught");
44      } catch(IntegratorException ie) {
45      }
46    }
47    
48    public void testDecreasingSteps()
49      throws DerivativeException, IntegratorException  {
50        
51      TestProblemAbstract[] problems = TestProblemFactory.getProblems();
52      for (int k = 0; k < problems.length; ++k) {
53  
54        double previousError = Double.NaN;
55        for (int i = 4; i < 10; ++i) {
56  
57          TestProblemAbstract pb = (TestProblemAbstract) problems[k].clone();
58          double step = (pb.getFinalTime() - pb.getInitialTime())
59            * Math.pow(2.0, -i);
60          FirstOrderIntegrator integ = new MidpointIntegrator(step);
61          TestProblemHandler handler = new TestProblemHandler(pb, integ);
62          integ.setStepHandler(handler);
63          SwitchingFunction[] functions = pb.getSwitchingFunctions();
64          for (int l = 0; l < functions.length; ++l) {
65            integ.addSwitchingFunction(functions[l],
66                                       Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000);
67          }
68          integ.integrate(pb,
69                          pb.getInitialTime(), pb.getInitialState(),
70                          pb.getFinalTime(), new double[pb.getDimension()]);
71  
72          double error = handler.getMaximalValueError();
73          if (i > 4) {
74            assertTrue(error < Math.abs(previousError));
75          }
76          previousError = error;
77          assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
78  
79        }
80  
81      }
82  
83    }
84  
85    public void testSmallStep()
86      throws DerivativeException, IntegratorException {
87  
88      TestProblem1 pb  = new TestProblem1();
89      double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
90  
91      FirstOrderIntegrator integ = new MidpointIntegrator(step);
92      TestProblemHandler handler = new TestProblemHandler(pb, integ);
93      integ.setStepHandler(handler);
94      integ.integrate(pb,
95                      pb.getInitialTime(), pb.getInitialState(),
96                      pb.getFinalTime(), new double[pb.getDimension()]);
97  
98      assertTrue(handler.getLastError() < 2.0e-7);
99      assertTrue(handler.getMaximalValueError() < 1.0e-6);
100     assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
101     assertEquals("midpoint", integ.getName());
102 
103   }
104 
105   public void testBigStep()
106     throws DerivativeException, IntegratorException {
107 
108     TestProblem1 pb  = new TestProblem1();
109     double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;
110 
111     FirstOrderIntegrator integ = new MidpointIntegrator(step);
112     TestProblemHandler handler = new TestProblemHandler(pb, integ);
113     integ.setStepHandler(handler);
114     integ.integrate(pb,
115                     pb.getInitialTime(), pb.getInitialState(),
116                     pb.getFinalTime(), new double[pb.getDimension()]);
117 
118     assertTrue(handler.getLastError() > 0.01);
119     assertTrue(handler.getMaximalValueError() > 0.05);
120     assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
121 
122   }
123 
124   public static Test suite() {
125     return new TestSuite(MidpointIntegratorTest.class);
126   }
127 
128 }