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