1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.commons.math4.legacy.ode.events;
18  
19  import org.junit.Assert;
20  
21  import java.util.Arrays;
22  
23  import org.apache.commons.math4.legacy.analysis.solvers.PegasusSolver;
24  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
25  import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
26  import org.apache.commons.math4.legacy.exception.NoBracketingException;
27  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
28  import org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations;
29  import org.apache.commons.math4.legacy.ode.FirstOrderIntegrator;
30  import org.apache.commons.math4.legacy.ode.nonstiff.DormandPrince853Integrator;
31  import org.apache.commons.math4.legacy.ode.nonstiff.GraggBulirschStoerIntegrator;
32  import org.junit.Test;
33  
34  public class ReappearingEventTest {
35      @Test
36      public void testDormandPrince()
37          throws DimensionMismatchException, NumberIsTooSmallException,
38                 MaxCountExceededException, NoBracketingException {
39          double tEnd = test(1);
40          Assert.assertEquals(10.0, tEnd, 1e-7);
41      }
42  
43      @Test
44      public void testGragg()
45          throws DimensionMismatchException, NumberIsTooSmallException,
46                 MaxCountExceededException, NoBracketingException {
47          double tEnd = test(2);
48          Assert.assertEquals(10.0, tEnd, 1e-7);
49      }
50  
51      public double test(int integratorType)
52          throws DimensionMismatchException, NumberIsTooSmallException,
53                 MaxCountExceededException, NoBracketingException {
54          double e = 1e-15;
55          FirstOrderIntegrator integrator;
56          integrator = (integratorType == 1)
57                       ? new DormandPrince853Integrator(e, 100.0, 1e-7, 1e-7)
58                       : new GraggBulirschStoerIntegrator(e, 100.0, 1e-7, 1e-7);
59          PegasusSolver rootSolver = new PegasusSolver(e, e);
60          integrator.addEventHandler(new Event(), 0.1, e, 1000, rootSolver);
61          double t0 = 6.0;
62          double tEnd = 10.0;
63          double[] y = {2.0, 2.0, 2.0, 4.0, 2.0, 7.0, 15.0};
64          return integrator.integrate(new Ode(), t0, y, tEnd, y);
65      }
66  
67      private static final class Ode implements FirstOrderDifferentialEquations {
68          @Override
69          public int getDimension() {
70              return 7;
71          }
72  
73          @Override
74          public void computeDerivatives(double t, double[] y, double[] yDot) {
75              Arrays.fill(yDot, 1.0);
76          }
77      }
78  
79      
80      protected static class Event implements EventHandler {
81  
82          @Override
83          public void init(double t0, double[] y0, double t) {
84          }
85  
86          @Override
87          public double g(double t, double[] y) {
88              return y[6] - 15.0;
89          }
90  
91          @Override
92          public Action eventOccurred(double t, double[] y, boolean increasing) {
93              return Action.STOP;
94          }
95  
96          @Override
97          public void resetState(double t, double[] y) {
98              
99          }
100     }
101 }