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  
18  package org.orekit.forces.maneuvers.propulsion;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.hipparchus.CalculusFieldElement;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.hipparchus.util.FastMath;
28  import org.orekit.utils.ParameterDriver;
29  
30  /** Thrust propulsion model with parameters (for estimation) represented by scale factors
31   *  on the X, Y and Z axis of the spacecraft frame.
32   * @author Maxime Journot
33   * @since 10.2
34   */
35  public class ScaledConstantThrustPropulsionModel extends AbstractConstantThrustPropulsionModel {
36  
37      /** Parameter name for the scale factor on the X component of the thrust in S/C frame. */
38      public static final String THRUSTX_SCALE_FACTOR = "TX scale factor";
39      /** Parameter name for the scale factor on the Y component of the thrust in S/C frame. */
40      public static final String THRUSTY_SCALE_FACTOR = "TY scale factor";
41      /** Parameter name for the scale factor on the Z component of the thrust in S/C frame. */
42      public static final String THRUSTZ_SCALE_FACTOR = "TZ scale factor";
43  
44      /** Thrust scaling factor.
45       * <p>
46       * We use a power of 2 to avoid numeric noise introduction
47       * in the multiplications/divisions sequences.
48       * </p>
49       */
50      private static final double THRUST_SCALE = FastMath.scalb(1.0, -5);
51  
52      /** Parameter driver for the scale factor on the X component of the thrust in S/C frame. */
53      private final ParameterDriver scaleFactorThrustXDriver;
54      /** Parameter driver for the scale factor on the Y component of the thrust in S/C frame. */
55      private final ParameterDriver scaleFactorThrustYDriver;
56      /** Parameter driver for the scale factor on the Z component of the thrust in S/C frame. */
57      private final ParameterDriver scaleFactorThrustZDriver;
58  
59      /** Constructor with min/max deviation for the scale factors.
60       * Typical usage is, for example, if you know that your propulsion system
61       * usually has an error of less than 10% then set the min/max to respectively 0.9 and 1.1.
62       * @param thrust the thrust (N)
63       * @param isp the isp (s)
64       * @param direction in spacecraft frame
65       * @param name the name of the maneuver
66       */
67      public ScaledConstantThrustPropulsionModel(final double thrust,
68                                                 final double isp,
69                                                 final Vector3D direction,
70                                                 final String name) {
71          super(thrust, isp, direction, name);
72  
73          // Build the parameter drivers, using maneuver name as prefix
74          this.scaleFactorThrustXDriver   = new ParameterDriver(name + THRUSTX_SCALE_FACTOR, 1., THRUST_SCALE,
75                                                                Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
76          this.scaleFactorThrustYDriver   = new ParameterDriver(name + THRUSTY_SCALE_FACTOR, 1., THRUST_SCALE,
77                                                                Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
78          this.scaleFactorThrustZDriver   = new ParameterDriver(name + THRUSTZ_SCALE_FACTOR, 1., THRUST_SCALE,
79                                                                Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
80      }
81  
82      /** Get the thrust vector in S/C frame from scale factors (N).
83       * @param scaleFactorX thrust vector scale factor on X axis of S/C frame
84       * @param scaleFactorY thrust vector scale factor on Y axis of S/C frame
85       * @param scaleFactorZ thrust vector scale factor on Z axis of S/C frame
86       * @return thrust vector in S/C frame
87       */
88      private Vector3D getThrustVector(final double scaleFactorX,
89                                       final double scaleFactorY,
90                                       final double scaleFactorZ) {
91          return new Vector3D(getInitialThrustVector().getX() * scaleFactorX,
92                              getInitialThrustVector().getY() * scaleFactorY,
93                              getInitialThrustVector().getZ() * scaleFactorZ);
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public Vector3D getThrustVector() {
99          return getThrustVector(scaleFactorThrustXDriver.getValue(),
100                                scaleFactorThrustYDriver.getValue(),
101                                scaleFactorThrustZDriver.getValue());
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public double getFlowRate() {
107         return getInitialFlowrate();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public List<ParameterDriver> getParametersDrivers() {
113         final List<ParameterDriver> drivers = new ArrayList<>(3);
114         drivers.add(scaleFactorThrustXDriver);
115         drivers.add(scaleFactorThrustYDriver);
116         drivers.add(scaleFactorThrustZDriver);
117         return Collections.unmodifiableList(drivers);
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public Vector3D getThrustVector(final double parameters[]) {
123         return getThrustVector(parameters[0], parameters[1], parameters[2]);
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public double getFlowRate(final double[] parameters) {
129         return getInitialFlowrate();
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public <T extends CalculusFieldElement<T>> FieldVector3D<T> getThrustVector(final T parameters[]) {
135         return new FieldVector3D<T>(parameters[0].multiply(getInitialThrustVector().getX()),
136                         parameters[1].multiply(getInitialThrustVector().getY()),
137                         parameters[2].multiply(getInitialThrustVector().getZ()));
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     public <T extends CalculusFieldElement<T>> T getFlowRate(final T[] parameters) {
143         return parameters[0].getField().getZero().add(getInitialFlowrate());
144     }
145 }