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.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.Assert;
29  import org.junit.Before;
30  import org.junit.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.ObservableSatellite;
35  import org.orekit.estimation.measurements.ObservedMeasurement;
36  import org.orekit.estimation.measurements.PV;
37  import org.orekit.estimation.measurements.modifiers.Bias;
38  import org.orekit.orbits.OrbitType;
39  import org.orekit.orbits.PositionAngle;
40  import org.orekit.propagation.Propagator;
41  import org.orekit.propagation.SpacecraftState;
42  import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
43  import org.orekit.time.AbsoluteDate;
44  import org.orekit.time.BurstSelector;
45  import org.orekit.time.TimeScalesFactory;
46  
47  public class PVBuilderTest {
48  
49      private static final double SIGMA_P = 10.0;
50      private static final double SIGMA_V =  0.01;
51      private static final double BIAS_P  =  5.0;
52      private static final double BIAS_V  = -0.003;
53  
54      private MeasurementBuilder<PV> getBuilder(final RandomGenerator random, final ObservableSatellite satellite) {
55          final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] {
56              SIGMA_P * SIGMA_P, SIGMA_P * SIGMA_P, SIGMA_P * SIGMA_P,
57              SIGMA_V * SIGMA_V, SIGMA_V * SIGMA_V, SIGMA_V * SIGMA_V,
58          });
59          MeasurementBuilder<PV> pvb =
60                          new PVBuilder(random == null ? null : new CorrelatedRandomVectorGenerator(covariance,
61                                                                                                    1.0e-10,
62                                                                                                    new GaussianRandomGenerator(random)),
63                                        SIGMA_P, SIGMA_V, 1.0, satellite);
64          pvb.addModifier(new Bias<>(new String[] { "pxBias", "pyBias", "pzBias", "vxBias", "vyBias", "vzBias" },
65                          new double[] { BIAS_P, BIAS_P, BIAS_P, BIAS_V, BIAS_V, BIAS_V },
66                          new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 },
67                          new double[] { Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY,
68                                         Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY },
69                          new double[] { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY,
70                          Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }));
71          return pvb;
72      }
73  
74      @Test
75      public void testForward() {
76          doTest(0x292b6e87436fe4c7l, 0.0, 1.2, 3.7 * SIGMA_P, 3.3 * SIGMA_V);
77      }
78  
79      @Test
80      public void testBackward() {
81          doTest(0x2f3285aa70b83c47l, 0.0, -1.0, 3.1 * SIGMA_P, 3.3 * SIGMA_V);
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 toleranceP, double toleranceV) {
89          Generator    generator    = new Generator();
90          final int    maxBurstSize = 10;
91          final double highRateStep = 5.0;
92          final double burstPeriod  = 300.0;
93  
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 ContinuousScheduler<>(getBuilder(new Well19937a(seed), satellite),
99                                                           new BurstSelector(maxBurstSize, highRateStep, burstPeriod,
100                                                                            TimeScalesFactory.getUTC())));
101         final double period = context.initialOrbit.getKeplerianPeriod();
102         AbsoluteDate t0     = context.initialOrbit.getDate().shiftedBy(startPeriod * period);
103         AbsoluteDate t1     = context.initialOrbit.getDate().shiftedBy(endPeriod   * period);
104         SortedSet<ObservedMeasurement<?>> measurements = generator.generate(t0, t1);
105         Propagator propagator = buildPropagator();
106         double maxErrorP = 0;
107         double maxErrorV = 0;
108         AbsoluteDate previous = null;
109         AbsoluteDate tInf = t0.compareTo(t1) < 0 ? t0 : t1;
110         AbsoluteDate tSup = t0.compareTo(t1) < 0 ? t1 : t0;
111         int count = 0;
112         for (ObservedMeasurement<?> measurement : measurements) {
113             AbsoluteDate date = measurement.getDate();
114             double[] m = measurement.getObservedValue();
115             Assert.assertTrue(date.compareTo(tInf) >= 0);
116             Assert.assertTrue(date.compareTo(tSup) <= 0);
117             if (previous != null) {
118                 // measurements are always chronological, even with backward propagation,
119                 // due to the SortedSet (which is intended for combining several
120                 // measurements types with different builders and schedulers)
121                 final double expected = (count % maxBurstSize == 0) ?
122                                         burstPeriod - (maxBurstSize - 1) * highRateStep :
123                                         highRateStep;
124                 Assert.assertEquals(expected, date.durationFrom(previous), 1.0e-10 * expected);
125             }
126             previous = date;
127             ++count;
128             SpacecraftState state = propagator.propagate(date);
129             double[] e = measurement.estimate(0, 0, new SpacecraftState[] { state }).getEstimatedValue();
130             for (int i = 0; i < 3; ++i) {
131                 maxErrorP = FastMath.max(maxErrorP, FastMath.abs(e[i] - m[i]));
132             }
133             for (int i = 3; i < m.length; ++i) {
134                 maxErrorV = FastMath.max(maxErrorV, FastMath.abs(e[i] - m[i]));
135             }
136         }
137         Assert.assertEquals(0.0, maxErrorP, toleranceP);
138         Assert.assertEquals(0.0, maxErrorV, toleranceV);
139      }
140 
141      @Before
142      public void setUp() {
143          context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
144 
145          propagatorBuilder = context.createBuilder(OrbitType.KEPLERIAN, PositionAngle.TRUE, true,
146                                                    1.0e-6, 300.0, 0.001, Force.POTENTIAL,
147                                                    Force.THIRD_BODY_SUN, Force.THIRD_BODY_MOON);
148      }
149 
150      Context context;
151      NumericalPropagatorBuilder propagatorBuilder;
152 
153 }