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.attitudes;
18  
19  
20  import org.hipparchus.Field;
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.geometry.euclidean.threed.Rotation;
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.hipparchus.util.Decimal64Field;
25  import org.hipparchus.util.FastMath;
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.orekit.Utils;
30  import org.orekit.bodies.CelestialBodyFactory;
31  import org.orekit.frames.Frame;
32  import org.orekit.frames.FramesFactory;
33  import org.orekit.orbits.FieldOrbit;
34  import org.orekit.orbits.KeplerianOrbit;
35  import org.orekit.orbits.Orbit;
36  import org.orekit.propagation.FieldSpacecraftState;
37  import org.orekit.propagation.SpacecraftState;
38  import org.orekit.time.AbsoluteDate;
39  import org.orekit.time.DateComponents;
40  import org.orekit.time.FieldAbsoluteDate;
41  import org.orekit.time.TimeComponents;
42  import org.orekit.time.TimeScalesFactory;
43  import org.orekit.utils.PVCoordinates;
44  import org.orekit.utils.PVCoordinatesProvider;
45  
46  public class CelestialBodyPointingTest {
47  
48      @Test
49      public void testSunPointing() {
50          PVCoordinatesProvider sun = CelestialBodyFactory.getSun();
51  
52          final Frame frame = FramesFactory.getGCRF();
53          AbsoluteDate date = new AbsoluteDate(new DateComponents(1970, 01, 01),
54                                               new TimeComponents(3, 25, 45.6789),
55                                               TimeScalesFactory.getTAI());
56          AttitudeProvider sunPointing =
57              new CelestialBodyPointed(frame, sun, Vector3D.PLUS_K,
58                                       Vector3D.PLUS_I, Vector3D.PLUS_K);
59          PVCoordinates pv =
60              new PVCoordinates(new Vector3D(28812595.32120171334, 5948437.45881852374, 0.0),
61                                new Vector3D(0, 0, 3680.853673522056));
62          Orbit orbit = new KeplerianOrbit(pv, frame, date, 3.986004415e14);
63          Attitude attitude   = sunPointing.getAttitude(orbit, date, frame);
64  
65          checkField(Decimal64Field.getInstance(), sunPointing, orbit, date, frame);
66  
67          Vector3D xDirection = attitude.getRotation().applyInverseTo(Vector3D.PLUS_I);
68          Vector3D zDirection = attitude.getRotation().applyInverseTo(Vector3D.PLUS_K);
69          Assert.assertEquals(0,
70                       Vector3D.dotProduct(zDirection, Vector3D.crossProduct(xDirection, Vector3D.PLUS_K)),
71                       1.0e-15);
72  
73          // the following statement checks we take parallax into account
74          // Sun-Earth-Sat are in quadrature, with distance (Earth, Sat) == distance(Sun, Earth) / 5000
75          Assert.assertEquals(FastMath.atan(1.0 / 5000.0),
76                              Vector3D.angle(xDirection,
77                                             sun.getPVCoordinates(date, frame).getPosition()),
78                                             1.0e-15);
79  
80          double h = 0.1;
81          Attitude aMinus = sunPointing.getAttitude(orbit.shiftedBy(-h), date.shiftedBy(-h), frame);
82          Attitude a0     = sunPointing.getAttitude(orbit, date, frame);
83          Attitude aPlus  = sunPointing.getAttitude(orbit.shiftedBy(h), date.shiftedBy(h), frame);
84  
85          // check spin is consistent with attitude evolution
86          double errorAngleMinus     = Rotation.distance(aMinus.shiftedBy(h).getRotation(),
87                                                         a0.getRotation());
88          double evolutionAngleMinus = Rotation.distance(aMinus.getRotation(),
89                                                         a0.getRotation());
90          Assert.assertEquals(0.0, errorAngleMinus, 1.0e-6 * evolutionAngleMinus);
91          double errorAnglePlus      = Rotation.distance(a0.getRotation(),
92                                                         aPlus.shiftedBy(-h).getRotation());
93          double evolutionAnglePlus  = Rotation.distance(a0.getRotation(),
94                                                         aPlus.getRotation());
95          Assert.assertEquals(0.0, errorAnglePlus, 1.0e-6 * evolutionAnglePlus);
96  
97      }
98  
99      private <T extends CalculusFieldElement<T>> void checkField(final Field<T> field, final AttitudeProvider provider,
100                                                             final Orbit orbit, final AbsoluteDate date,
101                                                             final Frame frame)
102         {
103         Attitude attitudeD = provider.getAttitude(orbit, date, frame);
104         final FieldOrbit<T> orbitF = new FieldSpacecraftState<>(field, new SpacecraftState(orbit)).getOrbit();
105         final FieldAbsoluteDate<T> dateF = new FieldAbsoluteDate<>(field, date);
106         FieldAttitude<T> attitudeF = provider.getAttitude(orbitF, dateF, frame);
107         Assert.assertEquals(0.0, Rotation.distance(attitudeD.getRotation(), attitudeF.getRotation().toRotation()), 1.0e-15);
108         Assert.assertEquals(0.0, Vector3D.distance(attitudeD.getSpin(), attitudeF.getSpin().toVector3D()), 1.0e-15);
109         Assert.assertEquals(0.0, Vector3D.distance(attitudeD.getRotationAcceleration(), attitudeF.getRotationAcceleration().toVector3D()), 1.0e-15);
110     }
111 
112     @Before
113     public void setUp() {
114         Utils.setDataRoot("regular-data");
115     }
116 
117 }
118