View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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    *      http://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  package org.hipparchus.ode.events;
18  
19  import org.hipparchus.analysis.UnivariateFunction;
20  import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
21  import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
22  import org.hipparchus.ode.ODEIntegrator;
23  import org.hipparchus.ode.ODEState;
24  import org.hipparchus.ode.ODEStateAndDerivative;
25  import org.hipparchus.ode.OrdinaryDifferentialEquation;
26  import org.hipparchus.ode.nonstiff.DormandPrince853Integrator;
27  import org.hipparchus.util.FastMath;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  
32  /** Tests for variable check interval.
33   */
34  public class VariableCheckInterval implements OrdinaryDifferentialEquation {
35  
36      @Test
37      void testFixedInterval() {
38          double tZero = 7.0;
39          double width = 0.25;
40          doTest(tZero, width, (s, isForward) -> width / 25, 710);
41      }
42  
43      @Test
44      void testWidthAwareInterval() {
45          double tZero = 7.0;
46          double width = 0.25;
47          doTest(tZero, width,
48                 (s, isForward) -> {
49                     if (s.getTime() < tZero - 0.5 * width) {
50                         return tZero - 0.25 * width - s.getTime();
51                     } else if (s.getTime() > tZero + 0.5 * width) {
52                         return s.getTime() - (tZero + 0.25 * width);
53                     } else {
54                         return width / 25;
55                     }
56                 },
57                 21);
58      }
59  
60      private void doTest(final double tZero, final double width, final AdaptableInterval checkInterval,
61                          final int expectedCalls) {
62          double e = 1e-15;
63          ODEIntegrator integrator = new DormandPrince853Integrator(e, 100.0, 1e-7, 1e-7);
64          Event evt = new Event(checkInterval, e, 999, tZero, width);
65          integrator.addEventDetector(evt);
66          double t = 0.0;
67          double tEnd = 9.75;
68          double[] y = {0.0, 0.0};
69          final ODEStateAndDerivative finalState =
70                          integrator.integrate(this, new ODEState(t, y), tEnd);
71          t = finalState.getTime();
72          assertEquals(tZero, finalState.getTime(), e);
73          assertEquals(expectedCalls, evt.count);
74       }
75  
76      /** {@inheritDoc} */
77      public int getDimension() {
78          return 2;
79      }
80  
81      /** {@inheritDoc} */
82      public double[] computeDerivatives(double t, double[] y) {
83          return new double[] { 1.0, 2.0 };
84      }
85  
86      /** State events for this unit test. */
87      private class Event implements ODEEventDetector {
88  
89          private final AdaptableInterval             maxCheck;
90          private final int                           maxIter;
91          private final BracketingNthOrderBrentSolver solver;
92          private final double                        tZero;
93          private final double                        width;
94          private       int                           count;
95  
96          /** Constructor for the {@link Event} class.
97           * @param maxCheck maximum checking interval
98           * @param threshold convergence threshold (s)
99           * @param maxIter maximum number of iterations in the event time search
100          * @param tZero time of zero crossing event
101          * @param width width of variation interval
102          */
103         public Event(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
104                      final double tZero, final double width) {
105             this.maxCheck  = maxCheck;
106             this.maxIter   = maxIter;
107             this.solver    = new BracketingNthOrderBrentSolver(0, threshold, 0, 5);
108             this.tZero     = tZero;
109             this.width     = width;
110         }
111 
112         public AdaptableInterval getMaxCheckInterval() {
113             return maxCheck;
114         }
115 
116         public int getMaxIterationCount() {
117             return maxIter;
118         }
119 
120         public BracketedUnivariateSolver<UnivariateFunction> getSolver() {
121             return solver;
122         }
123 
124         public ODEEventHandler getHandler() {
125             return (state, detector, increasing) -> Action.STOP;
126         }
127 
128         /** {@inheritDoc} */
129         public double g(final ODEStateAndDerivative s) {
130             ++count;
131             final double t = s.getTime();
132             return FastMath.max(-0.5 * width, FastMath.min(0.5 * width, t - tZero));
133         }
134 
135         /** {@inheritDoc} */
136         public void init(final ODEStateAndDerivative initialState, final double finalTime) {
137             count = 0;
138         }
139 
140     }
141 
142 }