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