1   /* Copyright 2022-2025 Romain Serra
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.forces.empirical;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
22  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23  import org.hipparchus.geometry.euclidean.threed.Rotation;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.orekit.attitudes.AttitudeProvider;
26  import org.orekit.forces.ForceModel;
27  import org.orekit.propagation.FieldSpacecraftState;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.propagation.events.EventDetector;
30  import org.orekit.propagation.events.FieldEventDetector;
31  
32  import java.util.stream.Stream;
33  
34  /**
35   * Abstract class for parametric acceleration.
36   *
37   * @since 13.0
38   * @author Romain Serra
39   */
40  public abstract class AbstractParametricAcceleration implements ForceModel {
41  
42      /** Direction of the acceleration in defining frame. */
43      private final Vector3D direction;
44  
45      /** Flag for inertial acceleration direction. */
46      private final boolean isInertial;
47  
48      /** The attitude to override, if set. */
49      private final AttitudeProvider attitudeOverride;
50  
51      protected AbstractParametricAcceleration(final Vector3D direction, final boolean isInertial,
52                                               final AttitudeProvider attitudeOverride) {
53          this.direction = direction;
54          this.isInertial = isInertial;
55          this.attitudeOverride = attitudeOverride;
56      }
57  
58      /**
59       * Getter for attitude override.
60       * @return attitude override
61       */
62      public AttitudeProvider getAttitudeOverride() {
63          return attitudeOverride;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public boolean dependsOnPositionOnly() {
69          return isInertial;
70      }
71  
72      /**
73       * Computes the acceleration's direction in the propagation frame.
74       * @param state state
75       * @return direction
76       */
77      protected Vector3D getAccelerationDirection(final SpacecraftState state) {
78          if (isInertial) {
79              // the acceleration direction is already defined in the inertial frame
80              return direction;
81          } else {
82              final Rotation rotation;
83              if (attitudeOverride == null) {
84                  // the acceleration direction is defined in spacecraft frame as set by the propagator
85                  rotation = state.getAttitude().getRotation();
86              } else {
87                  // the acceleration direction is defined in a dedicated frame
88                  rotation = attitudeOverride.getAttitudeRotation(state.isOrbitDefined() ? state.getOrbit() : state.getAbsPVA(),
89                          state.getDate(), state.getFrame());
90              }
91              return rotation.applyInverseTo(direction);
92          }
93      }
94  
95      /**
96       * Computes the acceleration's direction in the propagation frame.
97       * @param state state
98       * @param <T> field type
99       * @return direction
100      */
101     protected <T extends CalculusFieldElement<T>> FieldVector3D<T> getAccelerationDirection(final FieldSpacecraftState<T> state) {
102         if (isInertial) {
103             // the acceleration direction is already defined in the inertial frame
104             return new FieldVector3D<>(state.getDate().getField(), direction);
105         } else {
106             final FieldRotation<T> rotation;
107             if (attitudeOverride == null) {
108                 // the acceleration direction is defined in spacecraft frame as set by the propagator
109                 rotation = state.getAttitude().getRotation();
110             } else {
111                 // the acceleration direction is defined in a dedicated frame
112                 rotation = attitudeOverride.getAttitudeRotation(state.getOrbit(), state.getDate(), state.getFrame());
113             }
114             return rotation.applyInverseTo(direction);
115         }
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public Stream<EventDetector> getEventDetectors() {
121         return attitudeOverride == null ? Stream.of() : attitudeOverride.getEventDetectors();
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {
127         return attitudeOverride == null ? Stream.of() : attitudeOverride.getFieldEventDetectors(field);
128     }
129 }