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.IntegratorException;
31  import org.apache.commons.math.ode.ThreeEighthesIntegrator;
32  
33  public class ThreeEighthesStepInterpolatorTest
34    extends TestCase {
35  
36    public ThreeEighthesStepInterpolatorTest(String name) {
37      super(name);
38    }
39  
40    public void testSerialization()
41      throws DerivativeException, IntegratorException,
42             IOException, ClassNotFoundException {
43  
44      TestProblem3 pb = new TestProblem3(0.9);
45      double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
46      ThreeEighthesIntegrator integ = new ThreeEighthesIntegrator(step);
47      integ.setStepHandler(new ContinuousOutputModel());
48      integ.integrate(pb,
49                      pb.getInitialTime(), pb.getInitialState(),
50                      pb.getFinalTime(), new double[pb.getDimension()]);
51  
52      ByteArrayOutputStream bos = new ByteArrayOutputStream();
53      ObjectOutputStream    oos = new ObjectOutputStream(bos);
54      oos.writeObject(integ.getStepHandler());
55  
56      assertTrue(bos.size () > 700000);
57      assertTrue(bos.size () < 701000);
58  
59      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
60      ObjectInputStream     ois = new ObjectInputStream(bis);
61      ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();
62  
63      Random random = new Random(347588535632l);
64      double maxError = 0.0;
65      for (int i = 0; i < 1000; ++i) {
66        double r = random.nextDouble();
67        double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
68        cm.setInterpolatedTime(time);
69        double[] interpolatedY = cm.getInterpolatedState ();
70        double[] theoreticalY  = pb.computeTheoreticalState(time);
71        double dx = interpolatedY[0] - theoreticalY[0];
72        double dy = interpolatedY[1] - theoreticalY[1];
73        double error = dx * dx + dy * dy;
74        if (error > maxError) {
75          maxError = error;
76        }
77      }
78  
79      assertTrue(maxError > 0.005);
80  
81    }
82  
83    public static Test suite() {
84      return new TestSuite(ThreeEighthesStepInterpolatorTest.class);
85    }
86  
87  }