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.GroundStation;
34  import org.orekit.estimation.measurements.ObservableSatellite;
35  import org.orekit.estimation.measurements.TurnAroundRange;
36  import org.orekit.estimation.measurements.modifiers.Bias;
37  import org.orekit.orbits.OrbitType;
38  import org.orekit.orbits.PositionAngleType;
39  import org.orekit.propagation.Propagator;
40  import org.orekit.propagation.SpacecraftState;
41  import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
42  import org.orekit.propagation.events.BooleanDetector;
43  import org.orekit.propagation.events.ElevationDetector;
44  import org.orekit.time.AbsoluteDate;
45  import org.orekit.time.FixedStepSelector;
46  import org.orekit.time.TimeScalesFactory;
47  
48  import java.util.Map;
49  import java.util.SortedSet;
50  
51  public class TurnAroundRangeBuilderTest {
52  
53      private static final double SIGMA = 10.0;
54      private static final double BIAS  = -7.0;
55  
56      private MeasurementBuilder<TurnAroundRange> getBuilder(final RandomGenerator random,
57                                                             final GroundStation primary,
58                                                             final GroundStation secondary,
59                                                             final ObservableSatellite satellite) {
60          final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] { SIGMA * SIGMA });
61          MeasurementBuilder<TurnAroundRange> rb =
62                          new TurnAroundRangeBuilder(random == null ? null : new CorrelatedRandomVectorGenerator(covariance,
63                                                                                                                 1.0e-10,
64                                                                                                                 new GaussianRandomGenerator(random)),
65                                                     primary, secondary, SIGMA, 1.0, satellite);
66          rb.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 rb;
72      }
73  
74      @Test
75      public void testForward() {
76          doTest(0xf50c0ce7c8c1dab2L, 0.0, 1.2, 3.2 * SIGMA);
77      }
78  
79      @Test
80      public void testBackward() {
81          doTest(0x453a681440d01832L, 0.0, -1.0, 2.6 * 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          final double step = 60.0;
91          final Map.Entry<GroundStation, GroundStation> entry = context.TARstations.entrySet().iterator().next();
92          final GroundStation primary = entry.getKey();
93          final GroundStation secondary  = entry.getValue();
94          generator.addPropagator(buildPropagator()); // dummy first propagator
95          generator.addPropagator(buildPropagator()); // dummy second propagator
96          ObservableSatellite satellite = generator.addPropagator(buildPropagator()); // useful third propagator
97          generator.addPropagator(buildPropagator()); // dummy fourth propagator
98          generator.addScheduler(new EventBasedScheduler<>(getBuilder(new Well19937a(seed), primary, secondary, satellite),
99                                                           new FixedStepSelector(step, TimeScalesFactory.getUTC()),
100                                                          generator.getPropagator(satellite),
101                                                          BooleanDetector.andCombine(EstimationTestUtils.getElevationDetector(primary.getBaseFrame(),
102                                                                                                                              FastMath.toRadians(5.0)),
103                                                                                     EstimationTestUtils.getElevationDetector(secondary.getBaseFrame(),
104                                                                                                                              FastMath.toRadians(5.0))),
105                                                          SignSemantic.FEASIBLE_MEASUREMENT_WHEN_POSITIVE));
106         final GatheringSubscriber gatherer = new GatheringSubscriber();
107         generator.addSubscriber(gatherer);
108         final double period = context.initialOrbit.getKeplerianPeriod();
109         AbsoluteDate t0     = context.initialOrbit.getDate().shiftedBy(startPeriod * period);
110         AbsoluteDate t1     = context.initialOrbit.getDate().shiftedBy(endPeriod   * period);
111         generator.generate(t0, t1);
112         SortedSet<EstimatedMeasurementBase<?>> measurements = gatherer.getGeneratedMeasurements();
113         Propagator propagator = buildPropagator();
114         double maxError = 0;
115         AbsoluteDate previous = null;
116         AbsoluteDate tInf = t0.isBefore(t1) ? t0 : t1;
117         AbsoluteDate tSup = t0.isBefore(t1) ? t1 : t0;
118         for (EstimatedMeasurementBase<?> measurement : measurements) {
119             AbsoluteDate date = measurement.getDate();
120             double[] m = measurement.getObservedValue();
121             Assertions.assertTrue(date.compareTo(tInf) >= 0);
122             Assertions.assertTrue(date.compareTo(tSup) <= 0);
123             if (previous != null) {
124                 if (t0.isBefore(t1)) {
125                     // measurements are expected to be chronological
126                     Assertions.assertTrue(date.durationFrom(previous) >= 0.999999 * step);
127                 } else {
128                     // measurements are expected to be reverse chronological
129                     Assertions.assertTrue(previous.durationFrom(date) >= 0.999999 * step);
130                 }
131             }
132             previous = date;
133             SpacecraftState state = propagator.propagate(date);
134             double[] e = measurement.
135                 getObservedMeasurement().
136                 estimateWithoutDerivatives(new SpacecraftState[] { state }).
137                 getEstimatedValue();
138             for (int i = 0; i < m.length; ++i) {
139                 maxError = FastMath.max(maxError, FastMath.abs(e[i] - m[i]));
140             }
141         }
142         Assertions.assertEquals(0.0, maxError, tolerance);
143      }
144 
145      @BeforeEach
146      public void setUp() {
147          context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
148 
149          propagatorBuilder = context.createBuilder(OrbitType.KEPLERIAN, PositionAngleType.TRUE, true,
150                                                    1.0e-6, 300.0, 0.001, Force.POTENTIAL,
151                                                    Force.THIRD_BODY_SUN, Force.THIRD_BODY_MOON);
152      }
153 
154      Context context;
155      NumericalPropagatorBuilder propagatorBuilder;
156 
157 }