1   /* Copyright 2002-2022 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.Map;
20  import java.util.SortedSet;
21  
22  import org.hipparchus.linear.MatrixUtils;
23  import org.hipparchus.linear.RealMatrix;
24  import org.hipparchus.random.CorrelatedRandomVectorGenerator;
25  import org.hipparchus.random.GaussianRandomGenerator;
26  import org.hipparchus.random.RandomGenerator;
27  import org.hipparchus.random.Well19937a;
28  import org.hipparchus.util.FastMath;
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.orekit.estimation.Context;
33  import org.orekit.estimation.EstimationTestUtils;
34  import org.orekit.estimation.Force;
35  import org.orekit.estimation.measurements.GroundStation;
36  import org.orekit.estimation.measurements.ObservableSatellite;
37  import org.orekit.estimation.measurements.ObservedMeasurement;
38  import org.orekit.estimation.measurements.TurnAroundRange;
39  import org.orekit.estimation.measurements.modifiers.Bias;
40  import org.orekit.orbits.OrbitType;
41  import org.orekit.orbits.PositionAngle;
42  import org.orekit.propagation.Propagator;
43  import org.orekit.propagation.SpacecraftState;
44  import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
45  import org.orekit.propagation.events.BooleanDetector;
46  import org.orekit.propagation.events.ElevationDetector;
47  import org.orekit.time.AbsoluteDate;
48  import org.orekit.time.FixedStepSelector;
49  import org.orekit.time.TimeScalesFactory;
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(new ElevationDetector(primary.getBaseFrame()).
102                                                                                     withConstantElevation(FastMath.toRadians(5.0)),
103                                                                                     new ElevationDetector(secondary.getBaseFrame()).
104                                                                                     withConstantElevation(FastMath.toRadians(5.0))),
105                                                          SignSemantic.FEASIBLE_MEASUREMENT_WHEN_POSITIVE));
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         SortedSet<ObservedMeasurement<?>> measurements = generator.generate(t0, t1);
110         Propagator propagator = buildPropagator();
111         double maxError = 0;
112         AbsoluteDate previous = null;
113         AbsoluteDate tInf = t0.compareTo(t1) < 0 ? t0 : t1;
114         AbsoluteDate tSup = t0.compareTo(t1) < 0 ? t1 : t0;
115         for (ObservedMeasurement<?> measurement : measurements) {
116             AbsoluteDate date = measurement.getDate();
117             double[] m = measurement.getObservedValue();
118             Assert.assertTrue(date.compareTo(tInf) >= 0);
119             Assert.assertTrue(date.compareTo(tSup) <= 0);
120             if (previous != null) {
121                 // measurements are always chronological, even with backward propagation,
122                 // due to the SortedSet (which is intended for combining several
123                 // measurements types with different builders and schedulers)
124                 Assert.assertTrue(date.durationFrom(previous) >= 0.999999 * step);
125             }
126             previous = date;
127             SpacecraftState state = propagator.propagate(date);
128             double[] e = measurement.estimate(0, 0, new SpacecraftState[] { state }).getEstimatedValue();
129             for (int i = 0; i < m.length; ++i) {
130                 maxError = FastMath.max(maxError, FastMath.abs(e[i] - m[i]));
131             }
132         }
133         Assert.assertEquals(0.0, maxError, tolerance);
134      }
135 
136      @Before
137      public void setUp() {
138          context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
139 
140          propagatorBuilder = context.createBuilder(OrbitType.KEPLERIAN, PositionAngle.TRUE, true,
141                                                    1.0e-6, 300.0, 0.001, Force.POTENTIAL,
142                                                    Force.THIRD_BODY_SUN, Force.THIRD_BODY_MOON);
143      }
144 
145      Context context;
146      NumericalPropagatorBuilder propagatorBuilder;
147 
148 }