1   /* Copyright 2022-2025 Luc Maisonobe
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.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.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.EstimatedMeasurementBase;
35  import org.orekit.estimation.measurements.ObservableSatellite;
36  import org.orekit.estimation.measurements.gnss.OneWayGNSSRange;
37  import org.orekit.estimation.measurements.modifiers.Bias;
38  import org.orekit.orbits.KeplerianOrbit;
39  import org.orekit.orbits.Orbit;
40  import org.orekit.orbits.OrbitType;
41  import org.orekit.orbits.PositionAngleType;
42  import org.orekit.propagation.Propagator;
43  import org.orekit.propagation.SpacecraftState;
44  import org.orekit.propagation.analytical.KeplerianPropagator;
45  import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
46  import org.orekit.propagation.events.InterSatDirectViewDetector;
47  import org.orekit.time.AbsoluteDate;
48  import org.orekit.time.FixedStepSelector;
49  import org.orekit.time.TimeScalesFactory;
50  import org.orekit.utils.PVCoordinates;
51  
52  public class OneWayGNSSRangeBuilderTest {
53  
54      private static final double SIGMA =  0.5;
55      private static final double BIAS  = -0.01;
56  
57      private MeasurementBuilder<OneWayGNSSRange> getBuilder(final RandomGenerator random,
58                                                             final ObservableSatellite receiver,
59                                                             final ObservableSatellite remote) {
60          final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] { SIGMA * SIGMA });
61          remote.getClockOffsetDriver().setReferenceDate(AbsoluteDate.ARBITRARY_EPOCH);
62          remote.getClockOffsetDriver().setValue(1.0e-16);
63          remote.getClockDriftDriver().setReferenceDate(AbsoluteDate.ARBITRARY_EPOCH);
64          remote.getClockDriftDriver().setValue(0);
65          remote.getClockAccelerationDriver().setReferenceDate(AbsoluteDate.ARBITRARY_EPOCH);
66          remote.getClockAccelerationDriver().setValue(0);
67          MeasurementBuilder<OneWayGNSSRange> b =
68                          new OneWayGNSSRangeBuilder(random == null ? null : new CorrelatedRandomVectorGenerator(covariance,
69                                                                                                                 1.0e-10,
70                                                                                                                 new GaussianRandomGenerator(random)),
71                                                     receiver, remote,
72                                                     SIGMA, 1.0);
73          b.addModifier(new Bias<>(new String[] { "bias" },
74                           new double[] { BIAS },
75                           new double[] { 1.0 },
76                           new double[] { Double.NEGATIVE_INFINITY },
77                           new double[] { Double.POSITIVE_INFINITY }));
78          return b;
79      }
80  
81      @Test
82      public void testForward() {
83          doTest(0x6f44484882311d49L, 0.0, 1.2, 2.9 * SIGMA);
84      }
85  
86      @Test
87      public void testBackward() {
88          doTest(0x486b1353daa9f73eL, 0.0, -1.0, 3.6 * SIGMA);
89      }
90  
91      private Propagator buildPropagator() {
92          return EstimationTestUtils.createPropagator(context.initialOrbit, propagatorBuilder);
93      }
94  
95      private void doTest(long seed, double startPeriod, double endPeriod, double tolerance) {
96          Generator generator = new Generator();
97          generator.addPropagator(buildPropagator()); // dummy first propagator
98          generator.addPropagator(buildPropagator()); // dummy second propagator
99          ObservableSatellite receiver = generator.addPropagator(buildPropagator()); // useful third propagator
100         generator.addPropagator(buildPropagator()); // dummy fourth propagator
101         final Orbit o1 = context.initialOrbit;
102         // for the second satellite, we simply reverse velocity
103         final Orbit o2 = new KeplerianOrbit(new PVCoordinates(o1.getPosition(),
104                                                               o1.getPVCoordinates().getVelocity().negate()),
105                                             o1.getFrame(), o1.getDate(), o1.getMu());
106         ObservableSatellite remote = generator.addPropagator(new KeplerianPropagator(o2)); // useful sixth propagator
107         final double step = 60.0;
108 
109         // beware that in order to avoid deadlocks, the secondary PV coordinates provider
110         // in InterSatDirectViewDetector must be *different* from the second propagator
111         // added to generator above! The reason is the event detector will be bound
112         // to the first propagator, so it cannot also refer to the second one at the same time
113         // this is the reason why we create a *new* KeplerianPropagator below
114         generator.addScheduler(new EventBasedScheduler<>(getBuilder(new Well19937a(seed), receiver, remote),
115                                                          new FixedStepSelector(step, TimeScalesFactory.getUTC()),
116                                                          generator.getPropagator(receiver),
117                                                          new InterSatDirectViewDetector(context.earth, new KeplerianPropagator(o2)),
118                                                          SignSemantic.FEASIBLE_MEASUREMENT_WHEN_POSITIVE));
119 
120         final GatheringSubscriber gatherer = new GatheringSubscriber();
121         generator.addSubscriber(gatherer);
122         final double period = o1.getKeplerianPeriod();
123         AbsoluteDate t0     = o1.getDate().shiftedBy(startPeriod * period);
124         AbsoluteDate t1     = o1.getDate().shiftedBy(endPeriod   * period);
125         generator.generate(t0, t1);
126         SortedSet<EstimatedMeasurementBase<?>> measurements = gatherer.getGeneratedMeasurements();
127 
128         // and yet another set of propagators for reference
129         Propagator propagator1 = buildPropagator();
130         Propagator propagator2 = new KeplerianPropagator(o2);
131 
132         double maxError = 0;
133         AbsoluteDate previous = null;
134         AbsoluteDate tInf = t0.isBefore(t1) ? t0 : t1;
135         AbsoluteDate tSup = t0.isBefore(t1) ? t1 : t0;
136         for (EstimatedMeasurementBase<?> measurement : measurements) {
137             AbsoluteDate date = measurement.getDate();
138             double[] m = measurement.getObservedValue();
139             Assertions.assertTrue(date.compareTo(tInf) >= 0);
140             Assertions.assertTrue(date.compareTo(tSup) <= 0);
141             if (previous != null) {
142                 if (t0.isBefore(t1)) {
143                     // measurements are expected to be chronological
144                     Assertions.assertTrue(date.durationFrom(previous) >= 0.999999 * step);
145                 } else {
146                     // measurements are expected to be reverse chronological
147                     Assertions.assertTrue(previous.durationFrom(date) >= 0.999999 * step);
148                 }
149             }
150             previous = date;
151             double[] e = measurement.
152                 getObservedMeasurement().
153                 estimateWithoutDerivatives(new SpacecraftState[] {
154                                                propagator1.propagate(date),
155                                                propagator2.propagate(date)
156                                            }).
157                 getEstimatedValue();
158             for (int i = 0; i < m.length; ++i) {
159                 maxError = FastMath.max(maxError, FastMath.abs(e[i] - m[i]));
160             }
161         }
162         Assertions.assertEquals(0.0, maxError, tolerance);
163      }
164 
165      @BeforeEach
166      public void setUp() {
167          context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
168 
169          propagatorBuilder = context.createBuilder(OrbitType.KEPLERIAN, PositionAngleType.TRUE, true,
170                                                    1.0e-6, 300.0, 0.001, Force.POTENTIAL,
171                                                    Force.THIRD_BODY_SUN, Force.THIRD_BODY_MOON);
172      }
173 
174      Context context;
175      NumericalPropagatorBuilder propagatorBuilder;
176 
177 }