1   /* Copyright 2022-2025 Luc Maisonobe
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.utils;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.hipparchus.util.Binary64;
24  import org.hipparchus.util.Binary64Field;
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.Utils;
30  import org.orekit.bodies.CelestialBody;
31  import org.orekit.bodies.CelestialBodyFactory;
32  import org.orekit.frames.Frame;
33  import org.orekit.frames.FramesFactory;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.time.TimeScalesFactory;
37  
38  public class FrameAdapterTest {
39  
40      @Test
41      public void testDouble() {
42  
43          final Frame                         eme2000        = FramesFactory.getEME2000();
44          final CelestialBody                 moon           = CelestialBodyFactory.getMoon();
45          final Frame                         moonFrame      = moon.getBodyOrientedFrame();
46          final ExtendedPositionProvider moonPVProvider = new FrameAdapter(moonFrame);
47  
48          final AbsoluteDate t0 = new AbsoluteDate("2000-01-22T13:30:00", TimeScalesFactory.getUTC());
49          double maxP = 0;
50          double maxV = 0;
51          double maxA = 0;
52          for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 60.0) {
53              final AbsoluteDate t = t0.shiftedBy(dt);
54              final TimeStampedPVCoordinates pvRef     = moon.getPVCoordinates(t, eme2000);
55              final TimeStampedPVCoordinates pvAdapted = moonPVProvider.getPVCoordinates(t, eme2000);
56              maxP = FastMath.max(maxP, Vector3D.distance(pvRef.getPosition(),     pvAdapted.getPosition()));
57              maxV = FastMath.max(maxV, Vector3D.distance(pvRef.getVelocity(),     pvAdapted.getVelocity()));
58              maxA = FastMath.max(maxA, Vector3D.distance(pvRef.getAcceleration(), pvAdapted.getAcceleration()));
59          }
60          Assertions.assertEquals(0.0, maxP, 7.6e-7);
61          Assertions.assertEquals(0.0, maxV, 2.9e-12);
62          Assertions.assertEquals(0.0, maxA, 1.1e-17);
63  
64      }
65  
66      @Test
67      public void testField() {
68          doTestField(Binary64Field.getInstance());
69      }
70  
71      private <T extends CalculusFieldElement<T>> void doTestField(final Field<T> field) {
72  
73          final Frame                         eme2000        = FramesFactory.getEME2000();
74          final CelestialBody                 moon           = CelestialBodyFactory.getMoon();
75          final Frame                         moonFrame      = moon.getBodyOrientedFrame();
76          final ExtendedPositionProvider moonPVProvider = new FrameAdapter(moonFrame);
77  
78          final FieldAbsoluteDate<T> t0 = new FieldAbsoluteDate<>(field,
79                                                                  new AbsoluteDate("2000-01-22T13:30:00",
80                                                                                   TimeScalesFactory.getUTC()));
81          T maxP = field.getZero();
82          T maxV = field.getZero();
83          T maxA = field.getZero();
84          for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 60.0) {
85              final FieldAbsoluteDate<T> t = t0.shiftedBy(dt);
86              final TimeStampedFieldPVCoordinates<T> pvRef = moon.getPVCoordinates(t, eme2000);
87              final TimeStampedFieldPVCoordinates<T> pvAdapted = moonPVProvider.getPVCoordinates(t, eme2000);
88              maxP = FastMath.max(maxP, FieldVector3D.distance(pvRef.getPosition(),     pvAdapted.getPosition()));
89              maxV = FastMath.max(maxV, FieldVector3D.distance(pvRef.getVelocity(),     pvAdapted.getVelocity()));
90              maxA = FastMath.max(maxA, FieldVector3D.distance(pvRef.getAcceleration(), pvAdapted.getAcceleration()));
91          }
92          Assertions.assertEquals(0.0, maxP.getReal(), 7.6e-7);
93          Assertions.assertEquals(0.0, maxV.getReal(), 2.9e-12);
94          Assertions.assertEquals(0.0, maxA.getReal(), 1.1e-17);
95  
96      }
97  
98      @Test
99      void testGetPositionField() {
100         // GIVEN
101         final Frame eme2000 = FramesFactory.getEME2000();
102         final CelestialBody moon = CelestialBodyFactory.getMoon();
103         final Frame moonFrame = moon.getBodyOrientedFrame();
104         final FrameAdapter moonPVProvider = new FrameAdapter(moonFrame);
105         final AbsoluteDate date = AbsoluteDate.ARBITRARY_EPOCH;
106         final FieldAbsoluteDate<Binary64> fieldDate = new FieldAbsoluteDate<>(Binary64Field.getInstance(), date);
107         // WHEN
108         final FieldVector3D<Binary64> fieldPosition = moonPVProvider.getPosition(fieldDate, eme2000);
109         // THEN
110         Assertions.assertEquals(moonPVProvider.getPosition(date, eme2000), fieldPosition.toVector3D());
111     }
112 
113     @BeforeEach
114     public void setUp() {
115         Utils.setDataRoot("regular-data");
116     }
117 
118 }