1   /* Copyright 2022-2025 Romain Serra
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.models.earth.atmosphere;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.analysis.differentiation.Gradient;
22  import org.hipparchus.analysis.differentiation.GradientField;
23  import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
24  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
25  import org.hipparchus.geometry.euclidean.threed.Vector3D;
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.frames.Frame;
31  import org.orekit.frames.FramesFactory;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.time.FieldAbsoluteDate;
34  import org.orekit.utils.IERSConventions;
35  
36  class AtmosphereTest {
37  
38      @BeforeEach
39      public void setUp() {
40          Utils.setDataRoot("regular-data");
41      }
42  
43      @Test
44      void testFieldGetVelocity() {
45          // GIVEN
46          final GradientField field = GradientField.getField(1);
47          final FieldAbsoluteDate<Gradient> fieldDate = FieldAbsoluteDate.getArbitraryEpoch(field);
48          final FieldVector3D<Gradient> fieldPosition = FieldVector3D.getMinusI(field).scalarMultiply(1e3);
49          final Frame frame = FramesFactory.getGCRF();
50          final TestAtmosphere testAtmosphere = new TestAtmosphere();
51          // WHEN
52          final FieldVector3D<Gradient> fieldVelocity = testAtmosphere.getVelocity(fieldDate, fieldPosition, frame);
53          // THEN
54          final Vector3D actualVelocity = fieldVelocity.toVector3D();
55          final Vector3D expectedVelocity = testAtmosphere.getVelocity(fieldDate.toAbsoluteDate(),
56                  fieldPosition.toVector3D(), frame);
57          final double tolerance = 1e-10;
58          Assertions.assertEquals(expectedVelocity.getX(), actualVelocity.getX(), tolerance);
59          Assertions.assertEquals(expectedVelocity.getY(), actualVelocity.getY(), tolerance);
60          Assertions.assertEquals(expectedVelocity.getZ(), actualVelocity.getZ(), tolerance);
61      }
62  
63      @Test
64      void testGetVelocityNonConstantFieldAbsoluteDate() {
65          // GIVEN
66          final UnivariateDerivative1 variable = new UnivariateDerivative1(0., 1.);
67          final Field<UnivariateDerivative1> field = variable.getField();
68          final FieldAbsoluteDate<UnivariateDerivative1> fieldDate = FieldAbsoluteDate.getArbitraryEpoch(field)
69                  .shiftedBy(variable);
70          final FieldVector3D<UnivariateDerivative1> fieldPosition = FieldVector3D.getMinusI(field).scalarMultiply(1e3);
71          final Frame frame = FramesFactory.getGCRF();
72          final TestAtmosphere testAtmosphere = new TestAtmosphere();
73          // WHEN
74          final FieldVector3D<UnivariateDerivative1> fieldVelocity = testAtmosphere.getVelocity(fieldDate, fieldPosition, frame);
75          // THEN
76          Assertions.assertNotEquals(0., fieldVelocity.getNorm().getFirstDerivative(), 0.0);
77      }
78  
79      private static class TestAtmosphere implements Atmosphere {
80  
81          @Override
82          public Frame getFrame() {
83              return FramesFactory.getITRF(IERSConventions.IERS_2003, false);
84          }
85  
86          @Override
87          public double getDensity(AbsoluteDate date, Vector3D position, Frame frame) {
88              return 1.;
89          }
90  
91          @Override
92          public <T extends CalculusFieldElement<T>> T getDensity(FieldAbsoluteDate<T> date, FieldVector3D<T> position, Frame frame) {
93              final CalculusFieldElement<T> zero = date.getField().getZero();
94              return zero.newInstance(getDensity(date.toAbsoluteDate(), position.toVector3D(), frame));
95          }
96      }
97  
98  }