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  import java.io.ByteArrayOutputStream;
23  import java.io.ByteArrayInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.ObjectInputStream;
26  import java.io.IOException;
27  
28  import org.apache.commons.math.ode.ContinuousOutputModel;
29  import org.apache.commons.math.ode.DerivativeException;
30  import org.apache.commons.math.ode.EulerIntegrator;
31  import org.apache.commons.math.ode.EulerStepInterpolator;
32  import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
33  import org.apache.commons.math.ode.IntegratorException;
34  
35  public class EulerStepInterpolatorTest
36    extends TestCase {
37  
38    public EulerStepInterpolatorTest(String name) {
39      super(name);
40    }
41  
42    public void testNoReset() {
43  
44      double[]   y    =   { 0.0, 1.0, -2.0 };
45      double[][] yDot = { { 1.0, 2.0, -2.0 } };
46      EulerStepInterpolator interpolator = new EulerStepInterpolator();
47      interpolator.reinitialize(new DummyEquations(), y, yDot, true);
48      interpolator.storeTime(0);
49      interpolator.shift();
50      interpolator.storeTime(1);
51  
52      double[] result = interpolator.getInterpolatedState();
53      for (int i = 0; i < result.length; ++i) {
54        assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
55      }
56  
57    }
58  
59    public void testInterpolationAtBounds()
60      throws DerivativeException {
61  
62      double   t0 = 0;
63      double[] y0 = {0.0, 1.0, -2.0};
64  
65      double[] y = (double[]) y0.clone();
66      double[][] yDot = { new double[y0.length] };
67      EulerStepInterpolator interpolator = new EulerStepInterpolator();
68      interpolator.reinitialize(new DummyEquations(), y, yDot, true);
69      interpolator.storeTime(t0);
70  
71      double dt = 1.0;
72      y[0] =  1.0;
73      y[1] =  3.0;
74      y[2] = -4.0;
75      yDot[0][0] = (y[0] - y0[0]) / dt;
76      yDot[0][1] = (y[1] - y0[1]) / dt;
77      yDot[0][2] = (y[2] - y0[2]) / dt;
78      interpolator.shift();
79      interpolator.storeTime(t0 + dt);
80  
81      interpolator.setInterpolatedTime(interpolator.getPreviousTime());
82      double[] result = interpolator.getInterpolatedState();
83      for (int i = 0; i < result.length; ++i) {
84        assertTrue(Math.abs(result[i] - y0[i]) < 1.0e-10);
85      }
86  
87      interpolator.setInterpolatedTime(interpolator.getCurrentTime());
88      result = interpolator.getInterpolatedState();
89      for (int i = 0; i < result.length; ++i) {
90        assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
91      }
92  
93    }
94  
95    public void testInterpolationInside()
96      throws DerivativeException {
97  
98      double[]   y    =   { 1.0, 3.0, -4.0 };
99      double[][] yDot = { { 1.0, 2.0, -2.0 } };
100     EulerStepInterpolator interpolator = new EulerStepInterpolator();
101     interpolator.reinitialize(new DummyEquations(), y, yDot, true);
102     interpolator.storeTime(0);
103     interpolator.shift();
104     interpolator.storeTime(1);
105 
106     interpolator.setInterpolatedTime(0.1);
107     double[] result = interpolator.getInterpolatedState();
108     assertTrue(Math.abs(result[0] - 0.1) < 1.0e-10);
109     assertTrue(Math.abs(result[1] - 1.2) < 1.0e-10);
110     assertTrue(Math.abs(result[2] + 2.2) < 1.0e-10);
111 
112     interpolator.setInterpolatedTime(0.5);
113     result = interpolator.getInterpolatedState();
114     assertTrue(Math.abs(result[0] - 0.5) < 1.0e-10);
115     assertTrue(Math.abs(result[1] - 2.0) < 1.0e-10);
116     assertTrue(Math.abs(result[2] + 3.0) < 1.0e-10);
117 
118   }
119 
120   public void testSerialization()
121     throws DerivativeException, IntegratorException,
122            IOException, ClassNotFoundException {
123 
124     TestProblem1 pb = new TestProblem1();
125     double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
126     EulerIntegrator integ = new EulerIntegrator(step);
127     integ.setStepHandler(new ContinuousOutputModel());
128     integ.integrate(pb,
129                     pb.getInitialTime(), pb.getInitialState(),
130                     pb.getFinalTime(), new double[pb.getDimension()]);
131 
132     ByteArrayOutputStream bos = new ByteArrayOutputStream();
133     ObjectOutputStream    oos = new ObjectOutputStream(bos);
134     oos.writeObject(integ.getStepHandler());
135 
136     assertTrue(bos.size () > 82000);
137     assertTrue(bos.size () < 83000);
138 
139     ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
140     ObjectInputStream     ois = new ObjectInputStream(bis);
141     ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();
142 
143     Random random = new Random(347588535632l);
144     double maxError = 0.0;
145     for (int i = 0; i < 1000; ++i) {
146       double r = random.nextDouble();
147       double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
148       cm.setInterpolatedTime(time);
149       double[] interpolatedY = cm.getInterpolatedState ();
150       double[] theoreticalY  = pb.computeTheoreticalState(time);
151       double dx = interpolatedY[0] - theoreticalY[0];
152       double dy = interpolatedY[1] - theoreticalY[1];
153       double error = dx * dx + dy * dy;
154       if (error > maxError) {
155         maxError = error;
156       }
157     }
158 
159     assertTrue(maxError < 0.001);
160 
161   }
162 
163   private static class DummyEquations
164     implements FirstOrderDifferentialEquations {
165     public int getDimension() {
166       return 0;
167     }
168     public void computeDerivatives(double t, double[] y, double[] yDot) {
169     }
170   }
171 
172   public static Test suite() {
173     return new TestSuite(EulerStepInterpolatorTest.class);
174   }
175 
176 }