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 org.apache.commons.math.ode.DerivativeException; 021 import org.apache.commons.math.ode.FirstOrderIntegrator; 022 import org.apache.commons.math.ode.IntegratorException; 023 import org.apache.commons.math.ode.TestProblem3; 024 import org.apache.commons.math.ode.nonstiff.DormandPrince54Integrator; 025 import org.apache.commons.math.ode.sampling.FixedStepHandler; 026 import org.apache.commons.math.ode.sampling.StepNormalizer; 027 028 import junit.framework.*; 029 030 public class StepNormalizerTest 031 extends TestCase { 032 033 public StepNormalizerTest(String name) { 034 super(name); 035 pb = null; 036 integ = null; 037 } 038 039 public void testBoundaries() 040 throws DerivativeException, IntegratorException { 041 double range = pb.getFinalTime() - pb.getInitialTime(); 042 setLastSeen(false); 043 integ.addStepHandler(new StepNormalizer(range / 10.0, 044 new FixedStepHandler() { 045 private static final long serialVersionUID = 1650337364641626444L; 046 private boolean firstCall = true; 047 public void handleStep(double t, 048 double[] y, 049 double[] yDot, 050 boolean isLast) { 051 if (firstCall) { 052 checkValue(t, pb.getInitialTime()); 053 firstCall = false; 054 } 055 if (isLast) { 056 setLastSeen(true); 057 checkValue(t, pb.getFinalTime()); 058 } 059 } 060 })); 061 integ.integrate(pb, 062 pb.getInitialTime(), pb.getInitialState(), 063 pb.getFinalTime(), new double[pb.getDimension()]); 064 assertTrue(lastSeen); 065 } 066 067 public void testBeforeEnd() 068 throws DerivativeException, IntegratorException { 069 final double range = pb.getFinalTime() - pb.getInitialTime(); 070 setLastSeen(false); 071 integ.addStepHandler(new StepNormalizer(range / 10.5, 072 new FixedStepHandler() { 073 private static final long serialVersionUID = 2228457391561277298L; 074 public void handleStep(double t, 075 double[] y, 076 double[] yDot, 077 boolean isLast) { 078 if (isLast) { 079 setLastSeen(true); 080 checkValue(t, 081 pb.getFinalTime() - range / 21.0); 082 } 083 } 084 })); 085 integ.integrate(pb, 086 pb.getInitialTime(), pb.getInitialState(), 087 pb.getFinalTime(), new double[pb.getDimension()]); 088 assertTrue(lastSeen); 089 } 090 091 public void checkValue(double value, double reference) { 092 assertTrue(Math.abs(value - reference) < 1.0e-10); 093 } 094 095 public void setLastSeen(boolean lastSeen) { 096 this.lastSeen = lastSeen; 097 } 098 099 public static Test suite() { 100 return new TestSuite(StepNormalizerTest.class); 101 } 102 103 @Override 104 public void setUp() { 105 pb = new TestProblem3(0.9); 106 double minStep = 0; 107 double maxStep = pb.getFinalTime() - pb.getInitialTime(); 108 integ = new DormandPrince54Integrator(minStep, maxStep, 10.e-8, 1.0e-8); 109 lastSeen = false; 110 } 111 112 @Override 113 public void tearDown() { 114 pb = null; 115 integ = null; 116 } 117 118 TestProblem3 pb; 119 FirstOrderIntegrator integ; 120 boolean lastSeen; 121 122 }