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.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  class AbstractIntegratorTest {
33  
34      @Test
35      void testIntegrateWithResetDerivativesAndEventDetector() {
36          // GIVEN
37          final double finalTime = 1.;
38          final EulerIntegrator integrator = new EulerIntegrator(finalTime);
39          final TestDetector detector = new TestDetector();
40          integrator.addEventDetector(detector);
41          final TestProblem1 testProblem = new TestProblem1();
42          final ODEState initialState = new ODEState(0., new double[2]);
43          // WHEN
44          integrator.integrate(testProblem, initialState, finalTime);
45          // THEN
46          assertTrue(detector.resetted);
47      }
48  
49      private static class TestDetector implements ODEEventDetector {
50          boolean resetted = false;
51  
52          @Override
53          public void reset(ODEStateAndDerivative intermediateState, double finalTime) {
54              ODEEventDetector.super.reset(intermediateState, finalTime);
55              resetted = true;
56          }
57  
58          @Override
59          public AdaptableInterval getMaxCheckInterval() {
60              return AdaptableInterval.of(1);
61          }
62  
63          @Override
64          public int getMaxIterationCount() {
65              return 10;
66          }
67  
68          @Override
69          public BracketedUnivariateSolver<UnivariateFunction> getSolver() {
70              return new BracketingNthOrderBrentSolver();
71          }
72  
73          @Override
74          public ODEEventHandler getHandler() {
75              return (state, detector, increasing) -> Action.RESET_DERIVATIVES;
76          }
77  
78          @Override
79          public double g(ODEStateAndDerivative state) {
80              return state.getTime() - 0.5;
81          }
82      }
83  
84  }