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.nonstiff;
20
import static org.junit.Assert.assertTrue;
22
import java.util.Random;
23
import java.io.ByteArrayOutputStream;
24
import java.io.ByteArrayInputStream;
25
import java.io.ObjectOutputStream;
26
import java.io.ObjectInputStream;
27
import java.io.IOException;
29
import org.apache.commons.math.ode.ContinuousOutputModel;
30
import org.apache.commons.math.ode.DerivativeException;
31
import org.apache.commons.math.ode.IntegratorException;
32
import org.apache.commons.math.ode.TestProblem3;
33
import org.apache.commons.math.ode.nonstiff.GillIntegrator;
34
import org.apache.commons.math.ode.sampling.StepHandler;
35
import org.apache.commons.math.ode.sampling.StepInterpolatorTestUtils;
36
import org.junit.Test;
38
public class GillStepInterpolatorTest {
41
public void testDerivativesConsistency()
42
throws DerivativeException, IntegratorException {
43
TestProblem3 pb = new TestProblem3();
44
double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
45
GillIntegrator integ = new GillIntegrator(step);
46
StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10);
50
public void serialization()
51
throws DerivativeException, IntegratorException,
52
IOException, ClassNotFoundException {
54
TestProblem3 pb = new TestProblem3(0.9);
55
double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
56
GillIntegrator integ = new GillIntegrator(step);
57
integ.addStepHandler(new ContinuousOutputModel());
59
pb.getInitialTime(), pb.getInitialState(),
60
pb.getFinalTime(), new double[pb.getDimension()]);
62
ByteArrayOutputStream bos = new ByteArrayOutputStream();
63
ObjectOutputStream oos = new ObjectOutputStream(bos);
64
for (StepHandler handler : integ.getStepHandlers()) {
65
oos.writeObject(handler);
68
assertTrue(bos.size () > 700000);
69
assertTrue(bos.size () < 701000);
71
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
72
ObjectInputStream ois = new ObjectInputStream(bis);
73
ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
75
Random random = new Random(347588535632l);
76
double maxError = 0.0;
77
for (int i = 0; i < 1000; ++i) {
78
double r = random.nextDouble();
79
double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
80
cm.setInterpolatedTime(time);
81
double[] interpolatedY = cm.getInterpolatedState ();
82
double[] theoreticalY = pb.computeTheoreticalState(time);
83
double dx = interpolatedY[0] - theoreticalY[0];
84
double dy = interpolatedY[1] - theoreticalY[1];
85
double error = dx * dx + dy * dy;
86
if (error > maxError) {
91
assertTrue(maxError < 0.003);