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 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      /** State events for this unit test. */
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          /** Constructor for the {@link Event} class.
89           * @param maxCheck maximum checking interval, must be strictly positive (s)
90           * @param threshold convergence threshold (s)
91           * @param maxIter maximum number of iterations in the event time search
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 }