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.events;
19  
20  import org.hipparchus.ode.ODEState;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  import org.hipparchus.ode.sampling.ODEStateInterpolator;
23  
24  /** This class handles the state for one {@link ODEEventHandler
25   * event handler} that triggers at step end.
26   * @since 3.0
27   */
28  public class StepEndEventState implements EventState {
29  
30      /** Step end handler. */
31      private final ODEStepEndHandler handler;
32  
33      /** Time at step end. */
34      private double stepEnd;
35  
36      /** Integration direction. */
37      private boolean forward;
38  
39      /** Simple constructor.
40       * @param handler step end handler
41       */
42      public StepEndEventState(final ODEStepEndHandler handler) {
43          this.handler = handler;
44          this.stepEnd = Double.NaN;
45      }
46  
47      /** Get the underlying step end handler.
48       * @return underlying step end handler
49       */
50      public ODEStepEndHandler getHandler() {
51          return handler;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public void init(final ODEStateAndDerivative s0, final double t) {
57          forward = t >= s0.getTime();
58      }
59  
60      /** Set the step end.
61       * @param stepEnd step end
62       */
63      public void setStepEnd(final double stepEnd) {
64          this.stepEnd = stepEnd;
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public boolean evaluateStep(final ODEStateInterpolator interpolator) {
70          return stepEnd == interpolator.getCurrentState().getTime();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public double getEventTime() {
76          return stepEnd;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public EventOccurrence doEvent(final ODEStateAndDerivative state) {
82  
83          final Action action = handler.stepEndOccurred(state, forward);
84          final ODEState newState;
85          if (action == Action.RESET_STATE) {
86              newState = handler.resetState(state);
87          } else {
88              newState = state;
89          }
90  
91          final EventOccurrence occurrence = new EventOccurrence(action, newState, stepEnd);
92          setStepEnd(Double.NaN);
93          return occurrence;
94  
95      }
96  
97  }