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.assertEquals; 021 import static org.junit.Assert.assertTrue; 022 023 import java.io.ByteArrayInputStream; 024 import java.io.ByteArrayOutputStream; 025 import java.io.IOException; 026 import java.io.ObjectInputStream; 027 import java.io.ObjectOutputStream; 028 import java.util.Random; 029 030 import org.apache.commons.math.ode.ContinuousOutputModel; 031 import org.apache.commons.math.ode.DerivativeException; 032 import org.apache.commons.math.ode.IntegratorException; 033 import org.apache.commons.math.ode.TestProblem3; 034 import org.apache.commons.math.ode.sampling.StepHandler; 035 import org.apache.commons.math.ode.sampling.StepInterpolator; 036 import org.apache.commons.math.ode.sampling.StepInterpolatorTestUtils; 037 import org.junit.Test; 038 039 public class DormandPrince853StepInterpolatorTest { 040 041 @Test 042 public void derivativesConsistency() 043 throws DerivativeException, IntegratorException { 044 TestProblem3 pb = new TestProblem3(0.1); 045 double minStep = 0; 046 double maxStep = pb.getFinalTime() - pb.getInitialTime(); 047 double scalAbsoluteTolerance = 1.0e-8; 048 double scalRelativeTolerance = scalAbsoluteTolerance; 049 DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, 050 scalAbsoluteTolerance, 051 scalRelativeTolerance); 052 StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); 053 } 054 055 @Test 056 public void serialization() 057 throws DerivativeException, IntegratorException, 058 IOException, ClassNotFoundException { 059 060 TestProblem3 pb = new TestProblem3(0.9); 061 double minStep = 0; 062 double maxStep = pb.getFinalTime() - pb.getInitialTime(); 063 double scalAbsoluteTolerance = 1.0e-8; 064 double scalRelativeTolerance = scalAbsoluteTolerance; 065 DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, 066 scalAbsoluteTolerance, 067 scalRelativeTolerance); 068 integ.addStepHandler(new ContinuousOutputModel()); 069 integ.integrate(pb, 070 pb.getInitialTime(), pb.getInitialState(), 071 pb.getFinalTime(), new double[pb.getDimension()]); 072 073 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 074 ObjectOutputStream oos = new ObjectOutputStream(bos); 075 for (StepHandler handler : integ.getStepHandlers()) { 076 oos.writeObject(handler); 077 } 078 079 assertTrue(bos.size () > 86000); 080 assertTrue(bos.size () < 87000); 081 082 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 083 ObjectInputStream ois = new ObjectInputStream(bis); 084 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); 085 086 Random random = new Random(347588535632l); 087 double maxError = 0.0; 088 for (int i = 0; i < 1000; ++i) { 089 double r = random.nextDouble(); 090 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); 091 cm.setInterpolatedTime(time); 092 double[] interpolatedY = cm.getInterpolatedState (); 093 double[] theoreticalY = pb.computeTheoreticalState(time); 094 double dx = interpolatedY[0] - theoreticalY[0]; 095 double dy = interpolatedY[1] - theoreticalY[1]; 096 double error = dx * dx + dy * dy; 097 if (error > maxError) { 098 maxError = error; 099 } 100 } 101 102 assertTrue(maxError < 2.4e-10); 103 104 } 105 106 @Test 107 public void checklone() 108 throws DerivativeException, IntegratorException { 109 TestProblem3 pb = new TestProblem3(0.9); 110 double minStep = 0; 111 double maxStep = pb.getFinalTime() - pb.getInitialTime(); 112 double scalAbsoluteTolerance = 1.0e-8; 113 double scalRelativeTolerance = scalAbsoluteTolerance; 114 DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, 115 scalAbsoluteTolerance, 116 scalRelativeTolerance); 117 integ.addStepHandler(new StepHandler() { 118 public void handleStep(StepInterpolator interpolator, boolean isLast) 119 throws DerivativeException { 120 StepInterpolator cloned = interpolator.copy(); 121 double tA = cloned.getPreviousTime(); 122 double tB = cloned.getCurrentTime(); 123 double halfStep = Math.abs(tB - tA) / 2; 124 assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12); 125 assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12); 126 for (int i = 0; i < 10; ++i) { 127 double t = (i * tB + (9 - i) * tA) / 9; 128 interpolator.setInterpolatedTime(t); 129 assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10)); 130 cloned.setInterpolatedTime(t); 131 assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12); 132 double[] referenceState = interpolator.getInterpolatedState(); 133 double[] cloneState = cloned.getInterpolatedState(); 134 for (int j = 0; j < referenceState.length; ++j) { 135 assertEquals(referenceState[j], cloneState[j], 1.0e-12); 136 } 137 } 138 } 139 public boolean requiresDenseOutput() { 140 return true; 141 } 142 public void reset() { 143 } 144 }); 145 integ.integrate(pb, 146 pb.getInitialTime(), pb.getInitialState(), 147 pb.getFinalTime(), new double[pb.getDimension()]); 148 149 } 150 151 }