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