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