1   /* Copyright 2002-2022 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.estimation.measurements.generation;
18  
19  import java.util.SortedSet;
20  
21  import org.hipparchus.linear.MatrixUtils;
22  import org.hipparchus.linear.RealMatrix;
23  import org.hipparchus.random.CorrelatedRandomVectorGenerator;
24  import org.hipparchus.random.GaussianRandomGenerator;
25  import org.hipparchus.random.RandomGenerator;
26  import org.hipparchus.random.Well19937a;
27  import org.hipparchus.util.FastMath;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.orekit.estimation.Context;
32  import org.orekit.estimation.EstimationTestUtils;
33  import org.orekit.estimation.Force;
34  import org.orekit.estimation.measurements.ObservableSatellite;
35  import org.orekit.estimation.measurements.ObservedMeasurement;
36  import org.orekit.estimation.measurements.gnss.InterSatellitesPhase;
37  import org.orekit.estimation.measurements.modifiers.Bias;
38  import org.orekit.gnss.Frequency;
39  import org.orekit.orbits.KeplerianOrbit;
40  import org.orekit.orbits.Orbit;
41  import org.orekit.orbits.OrbitType;
42  import org.orekit.orbits.PositionAngle;
43  import org.orekit.propagation.Propagator;
44  import org.orekit.propagation.SpacecraftState;
45  import org.orekit.propagation.analytical.KeplerianPropagator;
46  import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
47  import org.orekit.propagation.events.InterSatDirectViewDetector;
48  import org.orekit.time.AbsoluteDate;
49  import org.orekit.time.FixedStepSelector;
50  import org.orekit.time.TimeScalesFactory;
51  import org.orekit.utils.PVCoordinates;
52  
53  public class InterSatellitesPhaseBuilderTest {
54  
55      private static final double SIGMA =  0.5;
56      private static final double BIAS  = -0.01;
57      private static final double WAVELENGTH = Frequency.G01.getWavelength();
58  
59      private MeasurementBuilder<InterSatellitesPhase> getBuilder(final RandomGenerator random,
60                                                                  final ObservableSatellite receiver,
61                                                                  final ObservableSatellite remote) {
62          final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] { SIGMA * SIGMA });
63          MeasurementBuilder<InterSatellitesPhase> isrb =
64                          new InterSatellitesPhaseBuilder(random == null ? null : new CorrelatedRandomVectorGenerator(covariance,
65                                                                                                                      1.0e-10,
66                                                                                                                      new GaussianRandomGenerator(random)),
67                                                          receiver, remote, WAVELENGTH, SIGMA, 1.0);
68          isrb.addModifier(new Bias<>(new String[] { "bias" },
69                           new double[] { BIAS },
70                           new double[] { 1.0 },
71                           new double[] { Double.NEGATIVE_INFINITY },
72                           new double[] { Double.POSITIVE_INFINITY }));
73          return isrb;
74      }
75  
76      @Test
77      public void testForward() {
78          doTest(0xc82a56322345dc25l, 0.0, 1.2, 2.8 * SIGMA);
79      }
80  
81      @Test
82      public void testBackward() {
83          doTest(0x95c10149c4891232l, 0.0, -1.0, 2.6 * SIGMA);
84      }
85  
86      private Propagator buildPropagator() {
87          return EstimationTestUtils.createPropagator(context.initialOrbit, propagatorBuilder);
88      }
89  
90      private void doTest(long seed, double startPeriod, double endPeriod, double tolerance) {
91          Generator generator = new Generator();
92          generator.addPropagator(buildPropagator()); // dummy first propagator
93          generator.addPropagator(buildPropagator()); // dummy second propagator
94          ObservableSatellite receiver = generator.addPropagator(buildPropagator()); // useful third propagator
95          generator.addPropagator(buildPropagator()); // dummy fourth propagator
96          final Orbit o1 = context.initialOrbit;
97          // for the second satellite, we simply reverse velocity
98          final Orbit o2 = new KeplerianOrbit(new PVCoordinates(o1.getPVCoordinates().getPosition(),
99                                                                o1.getPVCoordinates().getVelocity().negate()),
100                                             o1.getFrame(), o1.getDate(), o1.getMu());
101         ObservableSatellite remote = generator.addPropagator(new KeplerianPropagator(o2)); // useful sixth propagator
102         final double step = 60.0;
103 
104         // beware that in order to avoid deadlocks, the secondary PV coordinates provider
105         // in InterSatDirectViewDetector must be *different* from the second propagator
106         // added to generator above! The reason is the event detector will be bound
107         // to the first propagator, so it cannot also refer to the second one at the same time
108         // this is the reason why we create a *new* KeplerianPropagator below
109         generator.addScheduler(new EventBasedScheduler<>(getBuilder(new Well19937a(seed), receiver, remote),
110                                                          new FixedStepSelector(step, TimeScalesFactory.getUTC()),
111                                                          generator.getPropagator(receiver),
112                                                          new InterSatDirectViewDetector(context.earth, new KeplerianPropagator(o2)),
113                                                          SignSemantic.FEASIBLE_MEASUREMENT_WHEN_POSITIVE));
114 
115         final double period = o1.getKeplerianPeriod();
116         AbsoluteDate t0     = o1.getDate().shiftedBy(startPeriod * period);
117         AbsoluteDate t1     = o1.getDate().shiftedBy(endPeriod   * period);
118         SortedSet<ObservedMeasurement<?>> measurements = generator.generate(t0, t1);
119 
120         // and yet another set of propagators for reference
121         Propagator propagator1 = buildPropagator();
122         Propagator propagator2 = new KeplerianPropagator(o2);
123 
124         double maxError = 0;
125         AbsoluteDate previous = null;
126         AbsoluteDate tInf = t0.compareTo(t1) < 0 ? t0 : t1;
127         AbsoluteDate tSup = t0.compareTo(t1) < 0 ? t1 : t0;
128         for (ObservedMeasurement<?> measurement : measurements) {
129             AbsoluteDate date = measurement.getDate();
130             double[] m = measurement.getObservedValue();
131             Assert.assertTrue(date.compareTo(tInf) >= 0);
132             Assert.assertTrue(date.compareTo(tSup) <= 0);
133             if (previous != null) {
134                 // measurements are always chronological, even with backward propagation,
135                 // due to the SortedSet (which is intended for combining several
136                 // measurements types with different builders and schedulers)
137                 Assert.assertTrue(date.durationFrom(previous) >= 0.999999 * step);
138             }
139             previous = date;
140             double[] e = measurement.estimate(0, 0,
141                                               new SpacecraftState[] {
142                                                   propagator1.propagate(date),
143                                                   propagator2.propagate(date)
144                                               }).getEstimatedValue();
145             for (int i = 0; i < m.length; ++i) {
146                 maxError = FastMath.max(maxError, FastMath.abs(e[i] - m[i]));
147             }
148         }
149         Assert.assertEquals(0.0, maxError, tolerance);
150      }
151 
152      @Before
153      public void setUp() {
154          context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
155 
156          propagatorBuilder = context.createBuilder(OrbitType.KEPLERIAN, PositionAngle.TRUE, true,
157                                                    1.0e-6, 300.0, 0.001, Force.POTENTIAL,
158                                                    Force.THIRD_BODY_SUN, Force.THIRD_BODY_MOON);
159      }
160 
161      Context context;
162      NumericalPropagatorBuilder propagatorBuilder;
163 
164 }