1 /* Copyright 2002-2022 CS GROUP
2 * Licensed to CS GROUP (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS 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 * http://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.orekit.propagation.events;
18
19 import org.hipparchus.CalculusFieldElement;
20 import org.hipparchus.ode.events.Action;
21 import org.orekit.propagation.FieldSpacecraftState;
22 import org.orekit.propagation.events.handlers.FieldEventHandler;
23 import org.orekit.time.FieldAbsoluteDate;
24
25 /** Common parts shared by several orbital events finders.
26 * @see org.orekit.propagation.Propagator#addEventDetector(EventDetector)
27 * @author Luc Maisonobe
28 */
29 public abstract class FieldAbstractDetector<D extends FieldEventDetector<T>,
30 T extends CalculusFieldElement<T>> implements FieldEventDetector<T> {
31
32 /** Default maximum checking interval (s). */
33 public static final double DEFAULT_MAXCHECK = 600;
34
35 /** Default convergence threshold (s). */
36 public static final double DEFAULT_THRESHOLD = 1.e-6;
37
38 /** Default cmaximum number of iterations in the event time search. */
39 public static final int DEFAULT_MAX_ITER = 100;
40
41 /** Max check interval. */
42 private final T maxCheck;
43
44 /** Convergence threshold. */
45 private final T threshold;
46
47 /** Maximum number of iterations in the event time search. */
48 private final int maxIter;
49
50 /** Default handler for event overrides. */
51 private final FieldEventHandler<? super D, T> handler;
52
53 /** Propagation direction. */
54 private boolean forward;
55
56 /** Build a new instance.
57 * @param maxCheck maximum checking interval (s)
58 * @param threshold convergence threshold (s)
59 * @param maxIter maximum number of iterations in the event time search
60 * @param handler event handler to call at event occurrences
61 */
62 protected FieldAbstractDetector(final T maxCheck, final T threshold, final int maxIter,
63 final FieldEventHandler<? super D, T> handler) {
64 this.maxCheck = maxCheck;
65 this.threshold = threshold;
66 this.maxIter = maxIter;
67 this.handler = handler;
68 this.forward = true;
69 }
70
71 /** {@inheritDoc} */
72 @SuppressWarnings("unchecked")
73 public void init(final FieldSpacecraftState<T> s0,
74 final FieldAbsoluteDate<T> t) {
75 forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
76 getHandler().init(s0, t, (D) this);
77 }
78
79 /** {@inheritDoc} */
80 public abstract T g(FieldSpacecraftState<T> s);
81
82 /** {@inheritDoc} */
83 public T getMaxCheckInterval() {
84 return maxCheck;
85 }
86
87 /** {@inheritDoc} */
88 public int getMaxIterationCount() {
89 return maxIter;
90 }
91
92 /** {@inheritDoc} */
93 public T getThreshold() {
94 return threshold;
95 }
96
97 /**
98 * Setup the maximum checking interval.
99 * <p>
100 * This will override a maximum checking interval if it has been configured previously.
101 * </p>
102 * @param newMaxCheck maximum checking interval (s)
103 * @return a new detector with updated configuration (the instance is not changed)
104 * @since 6.1
105 */
106 public D withMaxCheck(final T newMaxCheck) {
107 return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
108 }
109
110 /**
111 * Setup the maximum number of iterations in the event time search.
112 * <p>
113 * This will override a number of iterations if it has been configured previously.
114 * </p>
115 * @param newMaxIter maximum number of iterations in the event time search
116 * @return a new detector with updated configuration (the instance is not changed)
117 * @since 6.1
118 */
119 public D withMaxIter(final int newMaxIter) {
120 return create(getMaxCheckInterval(), getThreshold(), newMaxIter, getHandler());
121 }
122
123 /**
124 * Setup the convergence threshold.
125 * <p>
126 * This will override a convergence threshold if it has been configured previously.
127 * </p>
128 * @param newThreshold convergence threshold (s)
129 * @return a new detector with updated configuration (the instance is not changed)
130 * @since 6.1
131 */
132 public D withThreshold(final T newThreshold) {
133 return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(), getHandler());
134 }
135
136 /**
137 * Setup the event handler to call at event occurrences.
138 * <p>
139 * This will override a handler if it has been configured previously.
140 * </p>
141 * @param newHandler event handler to call at event occurrences
142 * @return a new detector with updated configuration (the instance is not changed)
143 * @since 6.1
144 */
145 public D withHandler(final FieldEventHandler<? super D, T> newHandler) {
146 return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
147 }
148
149 /** Get the handler.
150 * @return event handler to call at event occurrences
151 */
152 public FieldEventHandler<? super D, T> getHandler() {
153 return handler;
154 }
155
156 /** {@inheritDoc} */
157 public Action eventOccurred(final FieldSpacecraftState<T> s, final boolean increasing) {
158 @SuppressWarnings("unchecked")
159 final Action whatNext = getHandler().eventOccurred(s, (D) this, increasing);
160 return whatNext;
161 }
162
163 /** {@inheritDoc} */
164 public FieldSpacecraftState<T> resetState(final FieldSpacecraftState<T> oldState) {
165 @SuppressWarnings("unchecked")
166 final FieldSpacecraftState<T> newState = getHandler().resetState((D) this, oldState);
167 return newState;
168 }
169
170 /** Build a new instance.
171 * @param newMaxCheck maximum checking interval (s)
172 * @param newThreshold convergence threshold (s)
173 * @param newMaxIter maximum number of iterations in the event time search
174 * @param newHandler event handler to call at event occurrences
175 * @return a new instance of the appropriate sub-type
176 */
177 protected abstract D create(T newMaxCheck, T newThreshold,
178 int newMaxIter, FieldEventHandler<? super D, T> newHandler);
179
180 /** Check if the current propagation is forward or backward.
181 * @return true if the current propagation is forward
182 * @since 7.2
183 */
184 public boolean isForward() {
185 return forward;
186 }
187
188 }