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