1   /* Copyright 2002-2019 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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.estimation.measurements.modifiers;
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.hipparchus.geometry.euclidean.threed.FieldRotation;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.orekit.attitudes.AttitudeProvider;
28  import org.orekit.attitudes.FieldAttitude;
29  import org.orekit.models.earth.DiscreteTroposphericModel;
30  import org.orekit.orbits.FieldCartesianOrbit;
31  import org.orekit.orbits.FieldOrbit;
32  import org.orekit.propagation.FieldSpacecraftState;
33  import org.orekit.propagation.SpacecraftState;
34  import org.orekit.utils.FieldAngularCoordinates;
35  import org.orekit.utils.FieldPVCoordinates;
36  import org.orekit.utils.ParameterDriver;
37  import org.orekit.utils.TimeStampedFieldAngularCoordinates;
38  import org.orekit.utils.TimeStampedFieldPVCoordinates;
39  
40  /** Converter for states and parameters arrays.
41   * @author Luc Maisonobe
42   */
43  public class TroposphericDSConverter {
44  
45      /** Dimension of the state. */
46      private final int freeStateParameters;
47  
48      /** States with various number of additional parameters for tropospheric models. */
49      private final List<FieldSpacecraftState<DerivativeStructure>> dsStates;
50  
51      /** Simple constructor.
52       * @param state regular state
53       * @param freeStateParameters number of free parameters, either 3 (position) or 6 (position-velocity)
54       * @param provider provider to use if attitude needs to be recomputed
55       */
56      public TroposphericDSConverter(final SpacecraftState state, final int freeStateParameters, final AttitudeProvider provider) {
57  
58          this.freeStateParameters = freeStateParameters;
59  
60          // prepare derivation variables, position, optionally velocity
61          final DSFactory factory = new DSFactory(freeStateParameters, 1);
62  
63          // position always has derivatives
64          final Vector3D pos = state.getPVCoordinates().getPosition();
65          final FieldVector3D<DerivativeStructure> posDS = new FieldVector3D<>(factory.variable(0, pos.getX()),
66                                                                               factory.variable(1, pos.getY()),
67                                                                               factory.variable(2, pos.getZ()));
68  
69          // velocity may have derivatives or not
70          final Vector3D vel = state.getPVCoordinates().getVelocity();
71          final FieldVector3D<DerivativeStructure> velDS;
72          if (freeStateParameters > 3) {
73              velDS = new FieldVector3D<>(factory.variable(3, vel.getX()),
74                                          factory.variable(4, vel.getY()),
75                                          factory.variable(5, vel.getZ()));
76          } else {
77              velDS = new FieldVector3D<>(factory.constant(vel.getX()),
78                                          factory.constant(vel.getY()),
79                                          factory.constant(vel.getZ()));
80          }
81  
82          // acceleration never has derivatives
83          final Vector3D acc = state.getPVCoordinates().getAcceleration();
84          final FieldVector3D<DerivativeStructure> accDS = new FieldVector3D<>(factory.constant(acc.getX()),
85                                                                               factory.constant(acc.getY()),
86                                                                               factory.constant(acc.getZ()));
87  
88          // mass never has derivatives
89          final DerivativeStructure dsM = factory.constant(state.getMass());
90  
91          final FieldOrbit<DerivativeStructure> dsOrbit =
92                          new FieldCartesianOrbit<>(new TimeStampedFieldPVCoordinates<>(state.getDate(), posDS, velDS, accDS),
93                                                    state.getFrame(), state.getMu());
94  
95          final FieldAttitude<DerivativeStructure> dsAttitude;
96          if (freeStateParameters > 3) {
97              // compute attitude partial derivatives with respect to position/velocity
98              dsAttitude = provider.getAttitude(dsOrbit, dsOrbit.getDate(), dsOrbit.getFrame());
99          } else {
100             // force model does not depend on attitude, don't bother recomputing it
101             dsAttitude = new FieldAttitude<>(factory.getDerivativeField(), state.getAttitude());
102         }
103 
104         // initialize the list with the state having 0 formce model parameters
105         dsStates = new ArrayList<>();
106         dsStates.add(new FieldSpacecraftState<>(dsOrbit, dsAttitude, dsM));
107 
108     }
109 
110     /** Get the number of free state parameters.
111      * @return number of free state parameters
112      */
113     public int getFreeStateParameters() {
114         return freeStateParameters;
115     }
116 
117     /** Get the state with the number of parameters consistent with tropospheric model.
118      * @param tropoModel tropospheric model
119      * @return state with the number of parameters consistent with tropospheric model
120      */
121     public FieldSpacecraftState<DerivativeStructure> getState(final DiscreteTroposphericModel tropoModel) {
122 
123         // count the required number of parameters
124         int nbParams = 0;
125         for (final ParameterDriver driver : tropoModel.getParametersDrivers()) {
126             if (driver.isSelected()) {
127                 ++nbParams;
128             }
129         }
130 
131         // fill in intermediate slots
132         while (dsStates.size() < nbParams + 1) {
133             dsStates.add(null);
134         }
135 
136         if (dsStates.get(nbParams) == null) {
137             // it is the first time we need this number of parameters
138             // we need to create the state
139             final DSFactory factory = new DSFactory(freeStateParameters + nbParams, 1);
140             final FieldSpacecraftState<DerivativeStructure> s0 = dsStates.get(0);
141 
142             // orbit
143             final FieldPVCoordinates<DerivativeStructure> pv0 = s0.getPVCoordinates();
144             final FieldOrbit<DerivativeStructure> dsOrbit =
145                             new FieldCartesianOrbit<>(new TimeStampedFieldPVCoordinates<>(s0.getDate().toAbsoluteDate(),
146                                                                                           extend(pv0.getPosition(),     factory),
147                                                                                           extend(pv0.getVelocity(),     factory),
148                                                                                           extend(pv0.getAcceleration(), factory)),
149                                                       s0.getFrame(), s0.getMu());
150 
151             // attitude
152             final FieldAngularCoordinates<DerivativeStructure> ac0 = s0.getAttitude().getOrientation();
153             final FieldAttitude<DerivativeStructure> dsAttitude =
154                             new FieldAttitude<>(s0.getAttitude().getReferenceFrame(),
155                                                 new TimeStampedFieldAngularCoordinates<>(dsOrbit.getDate(),
156                                                                                          extend(ac0.getRotation(), factory),
157                                                                                          extend(ac0.getRotationRate(), factory),
158                                                                                          extend(ac0.getRotationAcceleration(), factory)));
159 
160             // mass
161             final DerivativeStructure dsM = extend(s0.getMass(), factory);
162 
163             dsStates.set(nbParams, new FieldSpacecraftState<>(dsOrbit, dsAttitude, dsM));
164 
165         }
166 
167         return dsStates.get(nbParams);
168 
169     }
170 
171     /** Add zero derivatives.
172      * @param original original scalar
173      * @param factory factory for the extended derivatives
174      * @return extended scalar
175      */
176     private DerivativeStructure extend(final DerivativeStructure original, final DSFactory factory) {
177         final double[] originalDerivatives = original.getAllDerivatives();
178         final double[] extendedDerivatives = new double[factory.getCompiler().getSize()];
179         System.arraycopy(originalDerivatives, 0, extendedDerivatives, 0, originalDerivatives.length);
180         return factory.build(extendedDerivatives);
181     }
182 
183     /** Add zero derivatives.
184      * @param original original vector
185      * @param factory factory for the extended derivatives
186      * @return extended vector
187      */
188     private FieldVector3D<DerivativeStructure> extend(final FieldVector3D<DerivativeStructure> original, final DSFactory factory) {
189         return new FieldVector3D<>(extend(original.getX(), factory),
190                         extend(original.getY(), factory),
191                         extend(original.getZ(), factory));
192     }
193 
194     /** Add zero derivatives.
195      * @param original original rotation
196      * @param factory factory for the extended derivatives
197      * @return extended rotation
198      */
199     private FieldRotation<DerivativeStructure> extend(final FieldRotation<DerivativeStructure> original, final DSFactory factory) {
200         return new FieldRotation<>(extend(original.getQ0(), factory),
201                         extend(original.getQ1(), factory),
202                         extend(original.getQ2(), factory),
203                         extend(original.getQ3(), factory),
204                         false);
205     }
206 
207     /** Get the tropospheric model parameters.
208      * @param state state as returned by {@link #getState(DiscreteTroposphericModel)}
209      * @param tropoModel tropospheric model associated with the parameters
210      * @return tropospheric model parameters
211      */
212     public DerivativeStructure[] getParameters(final FieldSpacecraftState<DerivativeStructure> state,
213                                                final DiscreteTroposphericModel tropoModel) {
214         final DSFactory factory = state.getMass().getFactory();
215         final List<ParameterDriver> drivers = tropoModel.getParametersDrivers();
216         final DerivativeStructure[] parameters = new DerivativeStructure[drivers.size()];
217         int index = freeStateParameters;
218         for (int i = 0; i < drivers.size(); ++i) {
219             parameters[i] = drivers.get(i).isSelected() ?
220                             factory.variable(index++, drivers.get(i).getValue()) :
221                             factory.constant(drivers.get(i).getValue());
222         }
223         return parameters;
224     }
225 
226 }