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.nonstiff;
19  
20  import junit.framework.*;
21  
22  import org.apache.commons.math.ode.DerivativeException;
23  import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
24  import org.apache.commons.math.ode.FirstOrderIntegrator;
25  import org.apache.commons.math.ode.IntegratorException;
26  import org.apache.commons.math.ode.TestProblem1;
27  import org.apache.commons.math.ode.TestProblem5;
28  import org.apache.commons.math.ode.TestProblemAbstract;
29  import org.apache.commons.math.ode.TestProblemFactory;
30  import org.apache.commons.math.ode.TestProblemHandler;
31  import org.apache.commons.math.ode.events.EventHandler;
32  import org.apache.commons.math.ode.nonstiff.EulerIntegrator;
33  import org.apache.commons.math.ode.sampling.StepHandler;
34  import org.apache.commons.math.ode.sampling.StepInterpolator;
35  
36  public class EulerIntegratorTest
37    extends TestCase {
38  
39    public EulerIntegratorTest(String name) {
40      super(name);
41    }
42  
43    public void testDimensionCheck() {
44      try  {
45        TestProblem1 pb = new TestProblem1();
46        new EulerIntegrator(0.01).integrate(pb,
47                                            0.0, new double[pb.getDimension()+10],
48                                            1.0, new double[pb.getDimension()+10]);
49          fail("an exception should have been thrown");
50      } catch(DerivativeException de) {
51        fail("wrong exception caught");
52      } catch(IntegratorException ie) {
53      }
54    }
55    
56    public void testDecreasingSteps()
57      throws DerivativeException, IntegratorException {
58  
59      TestProblemAbstract[] problems = TestProblemFactory.getProblems();
60      for (int k = 0; k < problems.length; ++k) {
61  
62        double previousError = Double.NaN;
63        for (int i = 4; i < 10; ++i) {
64  
65          TestProblemAbstract pb  = problems[k].copy();
66          double step = (pb.getFinalTime() - pb.getInitialTime())
67            * Math.pow(2.0, -i);
68  
69          FirstOrderIntegrator integ = new EulerIntegrator(step);
70          TestProblemHandler handler = new TestProblemHandler(pb, integ);
71          integ.addStepHandler(handler);
72          EventHandler[] functions = pb.getEventsHandlers();
73          for (int l = 0; l < functions.length; ++l) {
74            integ.addEventHandler(functions[l],
75                                       Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000);
76          }
77          double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
78                                            pb.getFinalTime(), new double[pb.getDimension()]);
79          if (functions.length == 0) {
80              assertEquals(pb.getFinalTime(), stopTime, 1.0e-10);
81          }
82  
83          double error = handler.getMaximalValueError();
84          if (i > 4) {
85            assertTrue(error < Math.abs(previousError));
86          }
87          previousError = error;
88          assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
89  
90        }
91  
92      }
93  
94    }
95  
96    public void testSmallStep()
97      throws DerivativeException, IntegratorException {
98  
99      TestProblem1 pb  = new TestProblem1();
100     double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
101 
102     FirstOrderIntegrator integ = new EulerIntegrator(step);
103     TestProblemHandler handler = new TestProblemHandler(pb, integ);
104     integ.addStepHandler(handler);
105     integ.integrate(pb,
106                     pb.getInitialTime(), pb.getInitialState(),
107                     pb.getFinalTime(), new double[pb.getDimension()]);
108 
109    assertTrue(handler.getLastError() < 2.0e-4);
110    assertTrue(handler.getMaximalValueError() < 1.0e-3);
111    assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
112    assertEquals("Euler", integ.getName());
113 
114   }
115 
116   public void testBigStep()
117     throws DerivativeException, IntegratorException {
118 
119     TestProblem1 pb  = new TestProblem1();
120     double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;
121 
122     FirstOrderIntegrator integ = new EulerIntegrator(step);
123     TestProblemHandler handler = new TestProblemHandler(pb, integ);
124     integ.addStepHandler(handler);
125     integ.integrate(pb,
126                     pb.getInitialTime(), pb.getInitialState(),
127                     pb.getFinalTime(), new double[pb.getDimension()]);
128 
129     assertTrue(handler.getLastError() > 0.01);
130     assertTrue(handler.getMaximalValueError() > 0.2);
131     assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
132 
133   }
134 
135   public void testBackward()
136       throws DerivativeException, IntegratorException {
137 
138       TestProblem5 pb = new TestProblem5();
139       double step = Math.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;
140 
141       FirstOrderIntegrator integ = new EulerIntegrator(step);
142       TestProblemHandler handler = new TestProblemHandler(pb, integ);
143       integ.addStepHandler(handler);
144       integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
145                       pb.getFinalTime(), new double[pb.getDimension()]);
146 
147       assertTrue(handler.getLastError() < 0.45);
148       assertTrue(handler.getMaximalValueError() < 0.45);
149       assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
150       assertEquals("Euler", integ.getName());
151   }
152 
153   public void testStepSize()
154     throws DerivativeException, IntegratorException {
155       final double step = 1.23456;
156       FirstOrderIntegrator integ = new EulerIntegrator(step);
157       integ.addStepHandler(new StepHandler() {
158         public void handleStep(StepInterpolator interpolator, boolean isLast) {
159             if (! isLast) {
160                 assertEquals(step,
161                              interpolator.getCurrentTime() - interpolator.getPreviousTime(),
162                              1.0e-12);
163             }
164         }
165         public boolean requiresDenseOutput() {
166             return false;
167         }
168         public void reset() {
169         }          
170       });
171       integ.integrate(new FirstOrderDifferentialEquations() {
172                           private static final long serialVersionUID = 0L;
173                           public void computeDerivatives(double t, double[] y, double[] dot) {
174                               dot[0] = 1.0;
175                           }
176                           public int getDimension() {
177                               return 1;
178                           }
179                       }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
180   }
181 
182   public static Test suite() {
183     return new TestSuite(EulerIntegratorTest.class);
184   }
185 
186 }