1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.ode.events;
23
24 import java.util.Arrays;
25
26 import org.hipparchus.analysis.UnivariateFunction;
27 import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
28 import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.hipparchus.exception.MathIllegalStateException;
31 import org.hipparchus.ode.ODEIntegrator;
32 import org.hipparchus.ode.ODEState;
33 import org.hipparchus.ode.ODEStateAndDerivative;
34 import org.hipparchus.ode.OrdinaryDifferentialEquation;
35 import org.hipparchus.ode.nonstiff.DormandPrince853Integrator;
36 import org.hipparchus.ode.nonstiff.GraggBulirschStoerIntegrator;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40 public class ReappearingEventTest {
41
42 @Test
43 public void testDormandPrince()
44 throws MathIllegalArgumentException, MathIllegalStateException {
45 double tEnd = test(1);
46 Assert.assertEquals(10.0, tEnd, 1e-7);
47 }
48
49 @Test
50 public void testGragg()
51 throws MathIllegalArgumentException, MathIllegalStateException {
52 double tEnd = test(2);
53 Assert.assertEquals(10.0, tEnd, 1e-7);
54 }
55
56 public double test(int integratorType)
57 throws MathIllegalArgumentException, MathIllegalStateException {
58 double e = 1e-15;
59 ODEIntegrator integrator = (integratorType == 1) ?
60 new DormandPrince853Integrator(e, 100.0, 1e-7, 1e-7) :
61 new GraggBulirschStoerIntegrator(e, 100.0, 1e-7, 1e-7);
62 integrator.addEventDetector(new Event(0.1, e, 1000));
63 double t0 = 6.0;
64 double tEnd = 10.0;
65 double[] y = {2.0, 2.0, 2.0, 4.0, 2.0, 7.0, 15.0};
66 return integrator.integrate(new Ode(), new ODEState(t0, y), tEnd).getTime();
67 }
68
69 private static class Ode implements OrdinaryDifferentialEquation {
70 public int getDimension() {
71 return 7;
72 }
73
74 public double[] computeDerivatives(double t, double[] y) {
75 double[] yDot = new double[y.length];
76 Arrays.fill(yDot, 1.0);
77 return yDot;
78 }
79 }
80
81
82 protected static class Event implements ODEEventDetector {
83
84 private final AdaptableInterval maxCheck;
85 private final int maxIter;
86 private final BracketingNthOrderBrentSolver solver;
87
88
89
90
91
92
93 public Event(final double maxCheck, final double threshold, final int maxIter) {
94 this.maxCheck = s -> maxCheck;
95 this.maxIter = maxIter;
96 this.solver = new BracketingNthOrderBrentSolver(0, threshold, 0, 5);
97 }
98
99 public AdaptableInterval getMaxCheckInterval() {
100 return maxCheck;
101 }
102
103 public int getMaxIterationCount() {
104 return maxIter;
105 }
106
107 public BracketedUnivariateSolver<UnivariateFunction> getSolver() {
108 return solver;
109 }
110
111 public ODEEventHandler getHandler() {
112 return (state, detector, increasing) -> Action.STOP;
113 }
114
115 public double g(ODEStateAndDerivative s) {
116 return s.getPrimaryState()[6] - 15.0;
117 }
118
119 }
120
121 }