FieldStepEndEventState.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.CalculusFieldElement;
  19. import org.hipparchus.ode.FieldODEState;
  20. import org.hipparchus.ode.FieldODEStateAndDerivative;
  21. import org.hipparchus.ode.sampling.FieldODEStateInterpolator;

  22. /** This class handles the state for one {@link ODEEventHandler
  23.  * event handler} that triggers at step end.
  24.  * @since 3.0
  25.  * @param <T> the type of the field elements
  26.  */
  27. public class FieldStepEndEventState<T extends CalculusFieldElement<T>> implements FieldEventState<T> {

  28.     /** Step end handler. */
  29.     private final FieldODEStepEndHandler<T> handler;

  30.     /** Time at step end. */
  31.     private T stepEnd;

  32.     /** Integration direction. */
  33.     private boolean forward;

  34.     /** Simple constructor.
  35.      * @param handler step end handler
  36.      */
  37.     public FieldStepEndEventState(final FieldODEStepEndHandler<T> handler) {
  38.         this.handler = handler;
  39.         this.stepEnd = null;
  40.     }

  41.     /** Get the underlying step end handler.
  42.      * @return underlying step end handler
  43.      */
  44.     public FieldODEStepEndHandler<T> getHandler() {
  45.         return handler;
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public void init(final FieldODEStateAndDerivative<T> s0, final T t) {
  50.         forward = t.subtract(s0.getTime()).getReal() >= 0;
  51.     }

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

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public boolean evaluateStep(final FieldODEStateInterpolator<T> interpolator) {
  61.         return stepEnd != null && stepEnd.subtract(interpolator.getCurrentState().getTime()).isZero();
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public T getEventTime() {
  66.         return stepEnd;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public FieldEventOccurrence<T> doEvent(final FieldODEStateAndDerivative<T> state) {

  71.         final Action action = handler.stepEndOccurred(state, forward);
  72.         final FieldODEState<T> newState;
  73.         if (action == Action.RESET_STATE) {
  74.             newState = handler.resetState(state);
  75.         } else {
  76.             newState = state;
  77.         }

  78.         final FieldEventOccurrence<T> occurrence = new FieldEventOccurrence<>(action, newState, stepEnd);
  79.         setStepEnd(null);
  80.         return occurrence;

  81.     }

  82. }