1   /* Copyright 2002-2021 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      public void init(final FieldSpacecraftState<T> s0,
73                       final FieldAbsoluteDate<T> t) {
74          forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
75          getHandler().init(s0, t);
76      }
77  
78      /** {@inheritDoc} */
79      public abstract T g(FieldSpacecraftState<T> s);
80  
81      /** {@inheritDoc} */
82      public T getMaxCheckInterval() {
83          return maxCheck;
84      }
85  
86      /** {@inheritDoc} */
87      public int getMaxIterationCount() {
88          return maxIter;
89      }
90  
91      /** {@inheritDoc} */
92      public T getThreshold() {
93          return threshold;
94      }
95  
96      /**
97       * Setup the maximum checking interval.
98       * <p>
99       * This will override a maximum checking interval if it has been configured previously.
100      * </p>
101      * @param newMaxCheck maximum checking interval (s)
102      * @return a new detector with updated configuration (the instance is not changed)
103      * @since 6.1
104      */
105     public D withMaxCheck(final T newMaxCheck) {
106         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
107     }
108 
109     /**
110      * Setup the maximum number of iterations in the event time search.
111      * <p>
112      * This will override a number of iterations if it has been configured previously.
113      * </p>
114      * @param newMaxIter maximum number of iterations in the event time search
115      * @return a new detector with updated configuration (the instance is not changed)
116      * @since 6.1
117      */
118     public D withMaxIter(final int newMaxIter) {
119         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
120     }
121 
122     /**
123      * Setup the convergence threshold.
124      * <p>
125      * This will override a convergence threshold if it has been configured previously.
126      * </p>
127      * @param newThreshold convergence threshold (s)
128      * @return a new detector with updated configuration (the instance is not changed)
129      * @since 6.1
130      */
131     public D withThreshold(final T newThreshold) {
132         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
133     }
134 
135     /**
136      * Setup the event handler to call at event occurrences.
137      * <p>
138      * This will override a handler if it has been configured previously.
139      * </p>
140      * @param newHandler event handler to call at event occurrences
141      * @return a new detector with updated configuration (the instance is not changed)
142      * @since 6.1
143      */
144     public D withHandler(final FieldEventHandler<? super D, T> newHandler) {
145         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
146     }
147 
148     /** Get the handler.
149      * @return event handler to call at event occurrences
150      */
151     public FieldEventHandler<? super D, T> getHandler() {
152         return handler;
153     }
154 
155     /** {@inheritDoc} */
156     public Action eventOccurred(final FieldSpacecraftState<T> s, final boolean increasing) {
157         @SuppressWarnings("unchecked")
158         final Action whatNext = getHandler().eventOccurred(s, (D) this, increasing);
159         return whatNext;
160     }
161 
162     /** {@inheritDoc} */
163     public FieldSpacecraftState<T> resetState(final FieldSpacecraftState<T> oldState) {
164         @SuppressWarnings("unchecked")
165         final FieldSpacecraftState<T> newState = getHandler().resetState((D) this, oldState);
166         return newState;
167     }
168 
169     /** Build a new instance.
170      * @param newMaxCheck maximum checking interval (s)
171      * @param newThreshold convergence threshold (s)
172      * @param newMaxIter maximum number of iterations in the event time search
173      * @param newHandler event handler to call at event occurrences
174      * @return a new instance of the appropriate sub-type
175      */
176     protected abstract D create(T newMaxCheck, T newThreshold,
177                                 int newMaxIter, FieldEventHandler<? super D, T> newHandler);
178 
179     /** Check if the current propagation is forward or backward.
180      * @return true if the current propagation is forward
181      * @since 7.2
182      */
183     public boolean isForward() {
184         return forward;
185     }
186 
187 }