View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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      /** State events for this unit test. */
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          /** Constructor for the {@link Event} class.
90           * @param maxCheck maximum checking interval, must be strictly positive (s)
91           * @param threshold convergence threshold (s)
92           * @param maxIter maximum number of iterations in the event time search
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 }