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