~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/ode/ContinuousOutputModelTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
import java.util.Random;
 
22
 
 
23
import org.apache.commons.math.ode.ContinuousOutputModel;
 
24
import org.apache.commons.math.ode.DerivativeException;
 
25
import org.apache.commons.math.ode.FirstOrderIntegrator;
 
26
import org.apache.commons.math.ode.IntegratorException;
 
27
import org.apache.commons.math.ode.nonstiff.DormandPrince54Integrator;
 
28
import org.apache.commons.math.ode.nonstiff.DormandPrince853Integrator;
 
29
import org.apache.commons.math.ode.sampling.DummyStepInterpolator;
 
30
import org.apache.commons.math.ode.sampling.StepInterpolator;
 
31
 
 
32
public class ContinuousOutputModelTest
 
33
  extends TestCase {
 
34
 
 
35
  public ContinuousOutputModelTest(String name) {
 
36
    super(name);
 
37
    pb    = null;
 
38
    integ = null;
 
39
  }
 
40
 
 
41
  public void testBoundaries()
 
42
    throws DerivativeException, IntegratorException {
 
43
    integ.addStepHandler(new ContinuousOutputModel());
 
44
    integ.integrate(pb,
 
45
                    pb.getInitialTime(), pb.getInitialState(),
 
46
                    pb.getFinalTime(), new double[pb.getDimension()]);
 
47
    ContinuousOutputModel cm = (ContinuousOutputModel) integ.getStepHandlers().iterator().next();
 
48
    cm.setInterpolatedTime(2.0 * pb.getInitialTime() - pb.getFinalTime());
 
49
    cm.setInterpolatedTime(2.0 * pb.getFinalTime() - pb.getInitialTime());
 
50
    cm.setInterpolatedTime(0.5 * (pb.getFinalTime() + pb.getInitialTime()));
 
51
  }
 
52
 
 
53
  public void testRandomAccess()
 
54
    throws DerivativeException, IntegratorException {
 
55
 
 
56
    ContinuousOutputModel cm = new ContinuousOutputModel();
 
57
    integ.addStepHandler(cm);
 
58
    integ.integrate(pb,
 
59
                    pb.getInitialTime(), pb.getInitialState(),
 
60
                    pb.getFinalTime(), new double[pb.getDimension()]);
 
61
 
 
62
    Random random = new Random(347588535632l);
 
63
    double maxError = 0.0;
 
64
    for (int i = 0; i < 1000; ++i) {
 
65
      double r = random.nextDouble();
 
66
      double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
 
67
      cm.setInterpolatedTime(time);
 
68
      double[] interpolatedY = cm.getInterpolatedState ();
 
69
      double[] theoreticalY  = pb.computeTheoreticalState(time);
 
70
      double dx = interpolatedY[0] - theoreticalY[0];
 
71
      double dy = interpolatedY[1] - theoreticalY[1];
 
72
      double error = dx * dx + dy * dy;
 
73
      if (error > maxError) {
 
74
        maxError = error;
 
75
      }
 
76
    }
 
77
 
 
78
    assertTrue(maxError < 1.0e-9);
 
79
 
 
80
  }
 
81
 
 
82
  public void testModelsMerging()
 
83
    throws DerivativeException, IntegratorException {
 
84
 
 
85
      // theoretical solution: y[0] = cos(t), y[1] = sin(t)
 
86
      FirstOrderDifferentialEquations problem =
 
87
          new FirstOrderDifferentialEquations() {
 
88
              private static final long serialVersionUID = 2472449657345878299L;
 
89
              public void computeDerivatives(double t, double[] y, double[] dot)
 
90
                  throws DerivativeException {
 
91
                  dot[0] = -y[1];
 
92
                  dot[1] =  y[0];
 
93
              }
 
94
              public int getDimension() {
 
95
                  return 2;
 
96
              }
 
97
          };
 
98
 
 
99
      // integrate backward from &pi; to 0;
 
100
      ContinuousOutputModel cm1 = new ContinuousOutputModel();
 
101
      FirstOrderIntegrator integ1 =
 
102
          new DormandPrince853Integrator(0, 1.0, 1.0e-8, 1.0e-8);
 
103
      integ1.addStepHandler(cm1);
 
104
      integ1.integrate(problem, Math.PI, new double[] { -1.0, 0.0 },
 
105
                       0, new double[2]);
 
106
 
 
107
      // integrate backward from 2&pi; to &pi;
 
108
      ContinuousOutputModel cm2 = new ContinuousOutputModel();
 
109
      FirstOrderIntegrator integ2 =
 
110
          new DormandPrince853Integrator(0, 0.1, 1.0e-12, 1.0e-12);
 
111
      integ2.addStepHandler(cm2);
 
112
      integ2.integrate(problem, 2.0 * Math.PI, new double[] { 1.0, 0.0 },
 
113
                       Math.PI, new double[2]);
 
114
 
 
115
      // merge the two half circles
 
116
      ContinuousOutputModel cm = new ContinuousOutputModel();
 
117
      cm.append(cm2);
 
118
      cm.append(new ContinuousOutputModel());
 
119
      cm.append(cm1);
 
120
 
 
121
      // check circle
 
122
      assertEquals(2.0 * Math.PI, cm.getInitialTime(), 1.0e-12);
 
123
      assertEquals(0, cm.getFinalTime(), 1.0e-12);
 
124
      assertEquals(cm.getFinalTime(), cm.getInterpolatedTime(), 1.0e-12);
 
125
      for (double t = 0; t < 2.0 * Math.PI; t += 0.1) {
 
126
          cm.setInterpolatedTime(t);
 
127
          double[] y = cm.getInterpolatedState();
 
128
          assertEquals(Math.cos(t), y[0], 1.0e-7);
 
129
          assertEquals(Math.sin(t), y[1], 1.0e-7);
 
130
      }
 
131
      
 
132
  }
 
133
 
 
134
  public void testErrorConditions()
 
135
    throws DerivativeException {
 
136
 
 
137
      ContinuousOutputModel cm = new ContinuousOutputModel();
 
138
      cm.handleStep(buildInterpolator(0, new double[] { 0.0, 1.0, -2.0 }, 1), true);
 
139
      
 
140
      // dimension mismatch
 
141
      assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0 }, 2.0));
 
142
 
 
143
      // hole between time ranges
 
144
      assertTrue(checkAppendError(cm, 10.0, new double[] { 0.0, 1.0, -2.0 }, 20.0));
 
145
 
 
146
      // propagation direction mismatch
 
147
      assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 0.0));
 
148
 
 
149
      // no errors
 
150
      assertFalse(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 2.0));
 
151
 
 
152
  }
 
153
 
 
154
  private boolean checkAppendError(ContinuousOutputModel cm,
 
155
                                   double t0, double[] y0, double t1)
 
156
  throws DerivativeException {
 
157
      try {
 
158
          ContinuousOutputModel otherCm = new ContinuousOutputModel();
 
159
          otherCm.handleStep(buildInterpolator(t0, y0, t1), true);
 
160
          cm.append(otherCm);
 
161
      } catch(IllegalArgumentException iae) {
 
162
          //expected behavior
 
163
          return true;
 
164
      }
 
165
      return false;
 
166
  }
 
167
 
 
168
  private StepInterpolator buildInterpolator(double t0, double[] y0, double t1) {
 
169
      DummyStepInterpolator interpolator  = new DummyStepInterpolator(y0, t1 >= t0);
 
170
      interpolator.storeTime(t0);
 
171
      interpolator.shift();
 
172
      interpolator.storeTime(t1);
 
173
      return interpolator;
 
174
  }
 
175
 
 
176
  public void checkValue(double value, double reference) {
 
177
    assertTrue(Math.abs(value - reference) < 1.0e-10);
 
178
  }
 
179
 
 
180
  public static Test suite() {
 
181
    return new TestSuite(ContinuousOutputModelTest.class);
 
182
  }
 
183
 
 
184
  @Override
 
185
  public void setUp() {
 
186
    pb = new TestProblem3(0.9);
 
187
    double minStep = 0;
 
188
    double maxStep = pb.getFinalTime() - pb.getInitialTime();
 
189
    integ = new DormandPrince54Integrator(minStep, maxStep, 1.0e-8, 1.0e-8);
 
190
  }
 
191
 
 
192
  @Override
 
193
  public void tearDown() {
 
194
    pb    = null;
 
195
    integ = null;
 
196
  }
 
197
 
 
198
  TestProblem3 pb;
 
199
  FirstOrderIntegrator integ;
 
200
 
 
201
}