001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.ode.nonstiff;
019
020 import static org.junit.Assert.assertTrue;
021
022 import java.io.ByteArrayInputStream;
023 import java.io.ByteArrayOutputStream;
024 import java.io.IOException;
025 import java.io.ObjectInputStream;
026 import java.io.ObjectOutputStream;
027 import java.util.Random;
028
029 import org.apache.commons.math.ode.ContinuousOutputModel;
030 import org.apache.commons.math.ode.DerivativeException;
031 import org.apache.commons.math.ode.IntegratorException;
032 import org.apache.commons.math.ode.TestProblem3;
033 import org.apache.commons.math.ode.sampling.StepHandler;
034 import org.apache.commons.math.ode.sampling.StepInterpolatorTestUtils;
035 import org.junit.Test;
036
037 public class ClassicalRungeKuttaStepInterpolatorTest {
038
039 @Test
040 public void derivativesConsistency()
041 throws DerivativeException, IntegratorException {
042 TestProblem3 pb = new TestProblem3();
043 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
044 ClassicalRungeKuttaIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
045 StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10);
046 }
047
048 @Test
049 public void serialization()
050 throws DerivativeException, IntegratorException,
051 IOException, ClassNotFoundException {
052
053 TestProblem3 pb = new TestProblem3(0.9);
054 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
055 ClassicalRungeKuttaIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
056 integ.addStepHandler(new ContinuousOutputModel());
057 integ.integrate(pb,
058 pb.getInitialTime(), pb.getInitialState(),
059 pb.getFinalTime(), new double[pb.getDimension()]);
060
061 ByteArrayOutputStream bos = new ByteArrayOutputStream();
062 ObjectOutputStream oos = new ObjectOutputStream(bos);
063 for (StepHandler handler : integ.getStepHandlers()) {
064 oos.writeObject(handler);
065 }
066
067 assertTrue(bos.size () > 700000);
068 assertTrue(bos.size () < 701000);
069
070 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
071 ObjectInputStream ois = new ObjectInputStream(bis);
072 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
073
074 Random random = new Random(347588535632l);
075 double maxError = 0.0;
076 for (int i = 0; i < 1000; ++i) {
077 double r = random.nextDouble();
078 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
079 cm.setInterpolatedTime(time);
080 double[] interpolatedY = cm.getInterpolatedState ();
081 double[] theoreticalY = pb.computeTheoreticalState(time);
082 double dx = interpolatedY[0] - theoreticalY[0];
083 double dy = interpolatedY[1] - theoreticalY[1];
084 double error = dx * dx + dy * dy;
085 if (error > maxError) {
086 maxError = error;
087 }
088 }
089
090 assertTrue(maxError > 0.005);
091
092 }
093
094 }