StepEndEventState.java

  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. package org.hipparchus.ode.events;

  18. import org.hipparchus.ode.ODEState;
  19. import org.hipparchus.ode.ODEStateAndDerivative;
  20. import org.hipparchus.ode.sampling.ODEStateInterpolator;

  21. /** This class handles the state for one {@link ODEEventHandler
  22.  * event handler} that triggers at step end.
  23.  * @since 3.0
  24.  */
  25. public class StepEndEventState implements EventState {

  26.     /** Step end handler. */
  27.     private final ODEStepEndHandler handler;

  28.     /** Time at step end. */
  29.     private double stepEnd;

  30.     /** Integration direction. */
  31.     private boolean forward;

  32.     /** Simple constructor.
  33.      * @param handler step end handler
  34.      */
  35.     public StepEndEventState(final ODEStepEndHandler handler) {
  36.         this.handler = handler;
  37.         this.stepEnd = Double.NaN;
  38.     }

  39.     /** Get the underlying step end handler.
  40.      * @return underlying step end handler
  41.      */
  42.     public ODEStepEndHandler getHandler() {
  43.         return handler;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public void init(final ODEStateAndDerivative s0, final double t) {
  48.         forward = t >= s0.getTime();
  49.     }

  50.     /** Set the step end.
  51.      * @param stepEnd step end
  52.      */
  53.     public void setStepEnd(final double stepEnd) {
  54.         this.stepEnd = stepEnd;
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public boolean evaluateStep(final ODEStateInterpolator interpolator) {
  59.         return stepEnd == interpolator.getCurrentState().getTime();
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public double getEventTime() {
  64.         return stepEnd;
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public EventOccurrence doEvent(final ODEStateAndDerivative state) {

  69.         final Action action = handler.stepEndOccurred(state, forward);
  70.         final ODEState newState;
  71.         if (action == Action.RESET_STATE) {
  72.             newState = handler.resetState(state);
  73.         } else {
  74.             newState = state;
  75.         }

  76.         final EventOccurrence occurrence = new EventOccurrence(action, newState, stepEnd);
  77.         setStepEnd(Double.NaN);
  78.         return occurrence;

  79.     }

  80. }