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.propagation.integration;
18  
19  import org.hipparchus.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.ode.nonstiff.AdaptiveStepsizeIntegrator;
21  import org.hipparchus.ode.nonstiff.DormandPrince853Integrator;
22  import org.junit.After;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.orekit.Utils;
27  import org.orekit.forces.gravity.potential.GravityFieldFactory;
28  import org.orekit.forces.gravity.potential.SHMFormatReader;
29  import org.orekit.forces.maneuvers.ImpulseManeuver;
30  import org.orekit.frames.FramesFactory;
31  import org.orekit.orbits.EquinoctialOrbit;
32  import org.orekit.orbits.Orbit;
33  import org.orekit.orbits.OrbitType;
34  import org.orekit.propagation.SpacecraftState;
35  import org.orekit.propagation.events.DateDetector;
36  import org.orekit.propagation.numerical.NumericalPropagator;
37  import org.orekit.propagation.semianalytical.dsst.DSSTPropagator;
38  import org.orekit.time.AbsoluteDate;
39  import org.orekit.utils.PVCoordinates;
40  
41  @Deprecated
42  public class AdditionalEquationsTest {
43  
44      private double          mu;
45      private AbsoluteDate    initDate;
46      private SpacecraftState initialState;
47      private double[][]      tolerance;
48  
49      /** Test for issue #401
50       *  with a numerical propagator */
51      @Test
52      public void testInitNumerical() {
53  
54          // setup
55          final double reference = 1.25;
56          final double rate      = 1.5;
57          final double dt        = 600.0;
58          Linear linear = new Linear("linear", reference, rate);
59          Assert.assertFalse(linear.wasCalled());
60  
61          // action
62          AdaptiveStepsizeIntegrator integrator = new DormandPrince853Integrator(0.001, 200, tolerance[0], tolerance[1]);
63          integrator.setInitialStepSize(60);
64          NumericalPropagator propagatorNumerical = new NumericalPropagator(integrator);
65          propagatorNumerical.setInitialState(initialState.addAdditionalState(linear.getName(), reference));
66          propagatorNumerical.addAdditionalEquations(linear);
67          SpacecraftState finalState = propagatorNumerical.propagate(initDate.shiftedBy(dt));
68  
69          // verify
70          Assert.assertTrue(linear.wasCalled());
71          Assert.assertEquals(reference + dt * rate, finalState.getAdditionalState(linear.getName())[0], 1.0e-10);
72  
73      }
74  
75      /** Test for issue #401
76       *  with a DSST propagator */
77      @Test
78      public void testInitDSST() {
79  
80          // setup
81          final double reference = 3.5;
82          final double rate      = 1.5;
83          final double dt        = 600.0;
84          Linear linear = new Linear("linear", reference, rate);
85          Assert.assertFalse(linear.wasCalled());
86  
87          // action
88          AdaptiveStepsizeIntegrator integrator = new DormandPrince853Integrator(0.001, 200, tolerance[0], tolerance[1]);
89          integrator.setInitialStepSize(60);
90          DSSTPropagator propagatorDSST = new DSSTPropagator(integrator);
91          propagatorDSST.setInitialState(initialState.addAdditionalState(linear.getName(), reference));
92          propagatorDSST.addAdditionalEquations(linear);
93          SpacecraftState finalState = propagatorDSST.propagate(initDate.shiftedBy(dt));
94  
95          // verify
96          Assert.assertTrue(linear.wasCalled());
97          Assert.assertEquals(reference + dt * rate, finalState.getAdditionalState(linear.getName())[0], 1.0e-10);
98  
99      }
100 
101     @Test
102     public void testResetState() {
103 
104         // setup
105         final double reference1 = 3.5;
106         final double rate1      = 1.5;
107         Linear linear1 = new Linear("linear-1", reference1, rate1);
108         Assert.assertFalse(linear1.wasCalled());
109         final double reference2 = 4.5;
110         final double rate2      = 1.25;
111         Linear linear2 = new Linear("linear-2", reference2, rate2);
112         Assert.assertFalse(linear2.wasCalled());
113         final double dt = 600;
114 
115         // action
116         AdaptiveStepsizeIntegrator integrator = new DormandPrince853Integrator(0.001, 200, tolerance[0], tolerance[1]);
117         integrator.setInitialStepSize(60);
118         NumericalPropagator propagatorNumerical = new NumericalPropagator(integrator);
119         propagatorNumerical.setInitialState(initialState.
120                                             addAdditionalState(linear1.getName(), reference1).
121                                             addAdditionalState(linear2.getName(), reference2));
122         propagatorNumerical.addAdditionalEquations(linear1);
123         propagatorNumerical.addAdditionalEquations(linear2);
124         propagatorNumerical.addEventDetector(new ImpulseManeuver<>(new DateDetector(initDate.shiftedBy(dt / 2.0)),
125                                                                    new Vector3D(0.1, 0.2, 0.3), 350.0));
126         SpacecraftState finalState = propagatorNumerical.propagate(initDate.shiftedBy(dt));
127 
128         // verify
129         Assert.assertTrue(linear1.wasCalled());
130         Assert.assertTrue(linear2.wasCalled());
131         Assert.assertEquals(reference1 + dt * rate1, finalState.getAdditionalState(linear1.getName())[0], 1.0e-10);
132         Assert.assertEquals(reference2 + dt * rate2, finalState.getAdditionalState(linear2.getName())[0], 1.0e-10);
133 
134     }
135 
136     @Before
137     public void setUp() {
138         Utils.setDataRoot("regular-data:potential/shm-format");
139         GravityFieldFactory.addPotentialCoefficientsReader(new SHMFormatReader("^eigen_cg03c_coef$", false));
140         mu  = GravityFieldFactory.getUnnormalizedProvider(0, 0).getMu();
141         final Vector3D position = new Vector3D(7.0e6, 1.0e6, 4.0e6);
142         final Vector3D velocity = new Vector3D(-500.0, 8000.0, 1000.0);
143         initDate = AbsoluteDate.J2000_EPOCH;
144         final Orbit orbit = new EquinoctialOrbit(new PVCoordinates(position,  velocity),
145                                                  FramesFactory.getEME2000(), initDate, mu);
146         initialState = new SpacecraftState(orbit);
147         tolerance = NumericalPropagator.tolerances(0.001, orbit, OrbitType.EQUINOCTIAL);
148     }
149 
150     @After
151     public void tearDown() {
152         initDate     = null;
153         initialState = null;
154         tolerance    = null;
155     }
156 
157     private static class Linear implements AdditionalEquations {
158 
159         private String  name;
160         private double  expectedAtInit;
161         private double  rate;
162         private boolean called;
163 
164         public Linear(final String name, final double expectedAtInit, final double rate) {
165             this.name           = name;
166             this.expectedAtInit = expectedAtInit;
167             this.rate           = rate;
168             this.called         = false;
169         }
170 
171         @Override
172         public void init(SpacecraftState initiaState, AbsoluteDate target) {
173             Assert.assertEquals(expectedAtInit, initiaState.getAdditionalState(getName())[0], 1.0e-15);
174             called = true;
175         }
176 
177         @Override
178         public double[] computeDerivatives(SpacecraftState s, double[] pDot) {
179             pDot[0] = rate;
180             return null;
181         }
182 
183         @Override
184         public String getName() {
185             return name;
186         }
187 
188         public boolean wasCalled() {
189             return called;
190         }
191 
192     }
193 
194 }