1   /* Copyright 2002-2020 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.semianalytical.dsst;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.analysis.differentiation.DSFactory;
23  import org.hipparchus.analysis.differentiation.DerivativeStructure;
24  import org.orekit.attitudes.AttitudeProvider;
25  import org.orekit.attitudes.FieldAttitude;
26  import org.orekit.orbits.FieldEquinoctialOrbit;
27  import org.orekit.orbits.FieldOrbit;
28  import org.orekit.orbits.PositionAngle;
29  import org.orekit.propagation.FieldSpacecraftState;
30  import org.orekit.propagation.SpacecraftState;
31  import org.orekit.propagation.integration.AbstractDSConverter;
32  import org.orekit.propagation.semianalytical.dsst.forces.DSSTForceModel;
33  import org.orekit.time.AbsoluteDate;
34  import org.orekit.time.FieldAbsoluteDate;
35  import org.orekit.utils.FieldAngularCoordinates;
36  import org.orekit.utils.ParameterDriver;
37  import org.orekit.utils.TimeStampedFieldAngularCoordinates;
38  
39  /** Converter for states and parameters arrays.
40   * @author Luc Maisonobe
41   * @author Bryan Cazabonne
42   * @since 10.0
43   * @deprecated as of 10.2, replace by {@link DSSTGradientConverter}
44   */
45  @Deprecated
46  class DSSTDSConverter extends AbstractDSConverter {
47  
48      /** Fixed dimension of the state. */
49      private static final int FREE_STATE_PARAMETERS = 6;
50  
51      /** States with various number of additional parameters for force models. */
52      private final List<FieldSpacecraftState<DerivativeStructure>> dsStates;
53  
54      /** Simple constructor.
55       * @param state regular state
56       * @param provider provider to use if attitude needs to be recomputed
57       */
58      DSSTDSConverter(final SpacecraftState state, final AttitudeProvider provider) {
59  
60          super(FREE_STATE_PARAMETERS);
61  
62          // prepare derivation variables
63          final DSFactory factory = new DSFactory(FREE_STATE_PARAMETERS, 1);
64  
65          // equinoctial parameters always has derivatives
66          final DerivativeStructure sma  = factory.variable(0, state.getA());
67          final DerivativeStructure ex   = factory.variable(1, state.getEquinoctialEx());
68          final DerivativeStructure ey   = factory.variable(2, state.getEquinoctialEy());
69          final DerivativeStructure hx   = factory.variable(3, state.getHx());
70          final DerivativeStructure hy   = factory.variable(4, state.getHy());
71          final DerivativeStructure l    = factory.variable(5, state.getLM());
72  
73          final DerivativeStructure dsMu = factory.constant(state.getMu());
74  
75          // date
76          final AbsoluteDate date = state.getDate();
77          final FieldAbsoluteDate<DerivativeStructure> dateField = new FieldAbsoluteDate<>(sma.getField(), date);
78  
79          // mass never has derivatives
80          final DerivativeStructure dsM = factory.constant(state.getMass());
81  
82          final FieldOrbit<DerivativeStructure> dsOrbit =
83                          new FieldEquinoctialOrbit<>(sma, ex, ey, hx, hy, l,
84                                                      PositionAngle.MEAN,
85                                                      state.getFrame(),
86                                                      dateField,
87                                                      dsMu);
88  
89          final FieldAttitude<DerivativeStructure> dsAttitude;
90          // compute attitude partial derivatives
91          dsAttitude = provider.getAttitude(dsOrbit, dsOrbit.getDate(), dsOrbit.getFrame());
92  
93          // initialize the list with the state having 0 force model parameters
94          dsStates = new ArrayList<>();
95          dsStates.add(new FieldSpacecraftState<>(dsOrbit, dsAttitude, dsM));
96  
97      }
98  
99      /** Get the state with the number of parameters consistent with force model.
100      * @param forceModel force model
101      * @return state with the number of parameters consistent with force model
102      */
103     public FieldSpacecraftState<DerivativeStructure> getState(final DSSTForceModel forceModel) {
104 
105         // count the required number of parameters
106         int nbParams = 0;
107         for (final ParameterDriver driver : forceModel.getParametersDrivers()) {
108             if (driver.isSelected()) {
109                 ++nbParams;
110             }
111         }
112 
113         // fill in intermediate slots
114         while (dsStates.size() < nbParams + 1) {
115             dsStates.add(null);
116         }
117 
118         if (dsStates.get(nbParams) == null) {
119             // it is the first time we need this number of parameters
120             // we need to create the state
121             final DSFactory factory = new DSFactory(FREE_STATE_PARAMETERS + nbParams, 1);
122             final FieldSpacecraftState<DerivativeStructure> s0 = dsStates.get(0);
123 
124             final FieldAbsoluteDate<DerivativeStructure> date = new FieldAbsoluteDate<>(extend(s0.getA(), factory).getField(),
125                                                                                         s0.getDate().toAbsoluteDate());
126             // orbit
127             final FieldOrbit<DerivativeStructure> dsOrbit =
128                             new FieldEquinoctialOrbit<>(extend(s0.getA(),             factory),
129                                                         extend(s0.getEquinoctialEx(), factory),
130                                                         extend(s0.getEquinoctialEy(), factory),
131                                                         extend(s0.getHx(),            factory),
132                                                         extend(s0.getHy(),            factory),
133                                                         extend(s0.getLM(),            factory),
134                                                         PositionAngle.MEAN,
135                                                         s0.getFrame(), date,
136                                                         extend(s0.getMu(),            factory));
137 
138             // attitude
139             final FieldAngularCoordinates<DerivativeStructure> ac0 = s0.getAttitude().getOrientation();
140             final FieldAttitude<DerivativeStructure> dsAttitude =
141                             new FieldAttitude<>(s0.getAttitude().getReferenceFrame(),
142                                                 new TimeStampedFieldAngularCoordinates<>(dsOrbit.getDate(),
143                                                                                          extend(ac0.getRotation(),             factory),
144                                                                                          extend(ac0.getRotationRate(),         factory),
145                                                                                          extend(ac0.getRotationAcceleration(), factory)));
146 
147             // mass
148             final DerivativeStructure dsM = extend(s0.getMass(), factory);
149 
150             dsStates.set(nbParams, new FieldSpacecraftState<>(dsOrbit, dsAttitude, dsM));
151 
152         }
153 
154         return dsStates.get(nbParams);
155 
156     }
157 
158     /** Get the force model parameters.
159      * @param state state as returned by {@link #getState(DSSTForceModel)}
160      * @param forceModel force model associated with the parameters
161      * @return force model parameters
162      * @since 9.0
163      */
164     public DerivativeStructure[] getParameters(final FieldSpacecraftState<DerivativeStructure> state,
165                                                final DSSTForceModel forceModel) {
166         final DSFactory factory = state.getA().getFactory();
167         final ParameterDriver[] drivers = forceModel.getParametersDrivers();
168         final DerivativeStructure[] parameters = new DerivativeStructure[drivers.length];
169         int index = FREE_STATE_PARAMETERS;
170         for (int i = 0; i < drivers.length; ++i) {
171             parameters[i] = drivers[i].isSelected() ?
172                             factory.variable(index++, drivers[i].getValue()) :
173                             factory.constant(drivers[i].getValue());
174         }
175         return parameters;
176     }
177 
178 }