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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.math.ode;
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;
28
import org.apache.commons.math.ode.ContinuousOutputModel;
29
import org.apache.commons.math.ode.DerivativeException;
30
import org.apache.commons.math.ode.GillIntegrator;
31
import org.apache.commons.math.ode.IntegratorException;
33
public class GillStepInterpolatorTest
36
public GillStepInterpolatorTest(String name) {
40
public void testSerialization()
41
throws DerivativeException, IntegratorException,
42
IOException, ClassNotFoundException {
44
TestProblem3 pb = new TestProblem3(0.9);
45
double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
46
GillIntegrator integ = new GillIntegrator(step);
47
integ.setStepHandler(new ContinuousOutputModel());
49
pb.getInitialTime(), pb.getInitialState(),
50
pb.getFinalTime(), new double[pb.getDimension()]);
52
ByteArrayOutputStream bos = new ByteArrayOutputStream();
53
ObjectOutputStream oos = new ObjectOutputStream(bos);
54
oos.writeObject(integ.getStepHandler());
56
assertTrue(bos.size () > 700000);
57
assertTrue(bos.size () < 701000);
59
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
60
ObjectInputStream ois = new ObjectInputStream(bis);
61
ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
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) {
79
assertTrue(maxError < 0.003);
83
public static Test suite() {
84
return new TestSuite(GillStepInterpolatorTest.class);