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