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.sampling; 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.TestProblem1; 033 import org.apache.commons.math.ode.TestProblem3; 034 import org.apache.commons.math.ode.nonstiff.AdamsBashforthIntegrator; 035 import org.junit.Test; 036 037 public class NordsieckStepInterpolatorTest { 038 039 @Test 040 public void derivativesConsistency() 041 throws DerivativeException, IntegratorException { 042 TestProblem3 pb = new TestProblem3(); 043 AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(4, 0.0, 1.0, 1.0e-10, 1.0e-10); 044 StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 7e-10); 045 } 046 047 @Test 048 public void serialization() 049 throws DerivativeException, IntegratorException, 050 IOException, ClassNotFoundException { 051 052 TestProblem1 pb = new TestProblem1(); 053 AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(4, 0.0, 1.0, 1.0e-10, 1.0e-10); 054 integ.addStepHandler(new ContinuousOutputModel()); 055 integ.integrate(pb, 056 pb.getInitialTime(), pb.getInitialState(), 057 pb.getFinalTime(), new double[pb.getDimension()]); 058 059 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 060 ObjectOutputStream oos = new ObjectOutputStream(bos); 061 for (StepHandler handler : integ.getStepHandlers()) { 062 oos.writeObject(handler); 063 } 064 065 assertTrue(bos.size () > 20000); 066 assertTrue(bos.size () < 25000); 067 068 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 069 ObjectInputStream ois = new ObjectInputStream(bis); 070 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); 071 072 Random random = new Random(347588535632l); 073 double maxError = 0.0; 074 for (int i = 0; i < 1000; ++i) { 075 double r = random.nextDouble(); 076 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); 077 cm.setInterpolatedTime(time); 078 double[] interpolatedY = cm.getInterpolatedState (); 079 double[] theoreticalY = pb.computeTheoreticalState(time); 080 double dx = interpolatedY[0] - theoreticalY[0]; 081 double dy = interpolatedY[1] - theoreticalY[1]; 082 double error = dx * dx + dy * dy; 083 if (error > maxError) { 084 maxError = error; 085 } 086 } 087 088 assertTrue(maxError < 1.0e-6); 089 090 } 091 092 }