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    *      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  package org.hipparchus.ode;
19  
20  import org.hipparchus.analysis.UnivariateFunction;
21  import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
22  import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
23  import org.hipparchus.ode.events.Action;
24  import org.hipparchus.ode.events.AdaptableInterval;
25  import org.hipparchus.ode.events.ODEEventDetector;
26  import org.hipparchus.ode.events.ODEEventHandler;
27  import org.hipparchus.ode.nonstiff.EulerIntegrator;
28  import org.hipparchus.ode.sampling.ODEStateInterpolator;
29  import org.hipparchus.ode.sampling.ODEStepHandler;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  class AbstractIntegratorTest {
36  
37      @Test
38      void testIntegrateWithResetDerivativesAndEventDetector() {
39          // GIVEN
40          final double finalTime = 1.;
41          final EulerIntegrator integrator = new EulerIntegrator(finalTime);
42          final TestDetector detector = new TestDetector(0.5, Action.RESET_DERIVATIVES);
43          integrator.addEventDetector(detector);
44          final TestProblem1 testProblem = new TestProblem1();
45          final ODEState initialState = new ODEState(0., new double[2]);
46          // WHEN
47          integrator.integrate(testProblem, initialState, finalTime);
48          // THEN
49          assertTrue(detector.resetted);
50      }
51  
52  
53      @Test
54      void testUpdateStepIsCalledOncePerStepWhileHandleStepIsCalledAtEachEvent() {
55          // GIVEN
56          final double finalTime = 3.;
57          final EulerIntegrator integrator = new EulerIntegrator(finalTime);
58          integrator.addStepHandler(new UpdateStepTestHandler());
59          integrator.addEventDetector(new TestDetector(0.5, Action.CONTINUE));
60          integrator.addEventDetector(new TestDetector(0.6, Action.CONTINUE));
61          final TestProblem1 testProblem = new TestProblem1();
62          final ODEState initialState = new ODEState(0., new double[2]);
63          // WHEN
64          integrator.integrate(testProblem, initialState, finalTime);
65          // THEN
66          assertEquals(1, ((UpdateStepTestHandler) integrator.getStepHandlers().get(0)).getUpdateStepCounter());
67          assertEquals(3, ((UpdateStepTestHandler) integrator.getStepHandlers().get(0)).getHandlerStepCounter());
68      }
69  
70      private static class TestDetector implements ODEEventDetector {
71          boolean resetted = false;
72          double rootTime;
73          Action action;
74  
75          public TestDetector(double rootTime, Action action) {
76              this.rootTime = rootTime;
77              this.action = action;
78          }
79  
80          @Override
81          public void reset(ODEStateAndDerivative intermediateState, double finalTime) {
82              ODEEventDetector.super.reset(intermediateState, finalTime);
83              resetted = true;
84          }
85  
86          @Override
87          public AdaptableInterval getMaxCheckInterval() {
88              return AdaptableInterval.of(1);
89          }
90  
91          @Override
92          public int getMaxIterationCount() {
93              return 10;
94          }
95  
96          @Override
97          public BracketedUnivariateSolver<UnivariateFunction> getSolver() {
98              return new BracketingNthOrderBrentSolver();
99          }
100 
101         @Override
102         public ODEEventHandler getHandler() {
103             return (state, detector, increasing) -> action;
104         }
105 
106         @Override
107         public double g(ODEStateAndDerivative state) {
108             return state.getTime() - rootTime;
109         }
110     }
111 
112     
113     private static class UpdateStepTestHandler implements ODEStepHandler {
114 
115         private int handlerStepCounter = 0;
116         private int updateStepCounter = 0;
117 
118         @Override
119         public void handleStep(ODEStateInterpolator interpolator) {
120             handlerStepCounter++;
121         }
122 
123         @Override
124         public void updateOnStep(ODEStateInterpolator interpolator) {
125             updateStepCounter++;
126         }
127 
128         public int getHandlerStepCounter() {
129             return handlerStepCounter;
130         }
131 
132         public int getUpdateStepCounter() {
133             return updateStepCounter;
134         }
135     }
136 }