@Test
 public void testStopEvent() throws OrekitException {
   final AbsoluteDate stopDate = initDate.shiftedBy(1000);
   CheckingHandler<DateDetector> checking = new CheckingHandler<DateDetector>(Action.STOP);
   propagator.addEventDetector(new DateDetector(stopDate).withHandler(checking));
   Assert.assertEquals(1, propagator.getEventsDetectors().size());
   checking.assertEvent(false);
   final SpacecraftState finalState = propagator.propagate(initDate.shiftedBy(3200));
   checking.assertEvent(true);
   Assert.assertEquals(0, finalState.getDate().durationFrom(stopDate), 1.0e-10);
   propagator.clearEventsDetectors();
   Assert.assertEquals(0, propagator.getEventsDetectors().size());
 }
 @Test
 public void testResetStateEvent() throws OrekitException {
   final AbsoluteDate resetDate = initDate.shiftedBy(1000);
   CheckingHandler<DateDetector> checking =
       new CheckingHandler<DateDetector>(Action.RESET_STATE) {
         public SpacecraftState resetState(DateDetector detector, SpacecraftState oldState) {
           return new SpacecraftState(
               oldState.getOrbit(), oldState.getAttitude(), oldState.getMass() - 200.0);
         }
       };
   propagator.addEventDetector(new DateDetector(resetDate).withHandler(checking));
   checking.assertEvent(false);
   final SpacecraftState finalState = propagator.propagate(initDate.shiftedBy(3200));
   checking.assertEvent(true);
   Assert.assertEquals(initialState.getMass() - 200, finalState.getMass(), 1.0e-10);
 }
 @Test
 public void testContinueEvent() throws OrekitException {
   final AbsoluteDate resetDate = initDate.shiftedBy(1000);
   CheckingHandler<DateDetector> checking = new CheckingHandler<DateDetector>(Action.CONTINUE);
   propagator.addEventDetector(new DateDetector(resetDate).withHandler(checking));
   final double dt = 3200;
   checking.assertEvent(false);
   final SpacecraftState finalState = propagator.propagate(initDate.shiftedBy(dt));
   checking.assertEvent(true);
   final double n =
       FastMath.sqrt(initialState.getMu() / initialState.getA()) / initialState.getA();
   Assert.assertEquals(initialState.getA(), finalState.getA(), 1.0e-10);
   Assert.assertEquals(initialState.getEquinoctialEx(), finalState.getEquinoctialEx(), 1.0e-10);
   Assert.assertEquals(initialState.getEquinoctialEy(), finalState.getEquinoctialEy(), 1.0e-10);
   Assert.assertEquals(initialState.getHx(), finalState.getHx(), 1.0e-10);
   Assert.assertEquals(initialState.getHy(), finalState.getHy(), 1.0e-10);
   Assert.assertEquals(initialState.getLM() + n * dt, finalState.getLM(), 6.0e-10);
 }
  @Test
  public void testResetAdditionalStateEvent() throws OrekitException {
    propagator.addAdditionalEquations(
        new AdditionalEquations() {

          public String getName() {
            return "linear";
          }

          public double[] computeDerivatives(SpacecraftState s, double[] pDot) {
            pDot[0] = 1.0;
            return null;
          }
        });
    propagator.setInitialState(propagator.getInitialState().addAdditionalState("linear", 1.5));

    CheckingHandler<AdditionalStateLinearDetector> checking =
        new CheckingHandler<AdditionalStateLinearDetector>(Action.RESET_STATE) {
          public SpacecraftState resetState(
              AdditionalStateLinearDetector detector, SpacecraftState oldState)
              throws OrekitException {
            return oldState.addAdditionalState(
                "linear", oldState.getAdditionalState("linear")[0] * 2);
          }
        };

    propagator.addEventDetector(
        new AdditionalStateLinearDetector(10.0, 1.0e-8).withHandler(checking));

    final double dt = 3200;
    checking.assertEvent(false);
    final SpacecraftState finalState = propagator.propagate(initDate.shiftedBy(dt));
    checking.assertEvent(true);
    Assert.assertEquals(dt + 4.5, finalState.getAdditionalState("linear")[0], 1.0e-8);
    Assert.assertEquals(dt, finalState.getDate().durationFrom(initDate), 1.0e-8);
  }
  @Test
  public void testAdditionalStateEvent() throws OrekitException {
    propagator.addAdditionalEquations(
        new AdditionalEquations() {

          public String getName() {
            return "linear";
          }

          public double[] computeDerivatives(SpacecraftState s, double[] pDot) {
            pDot[0] = 1.0;
            return new double[7];
          }
        });
    try {
      propagator.addAdditionalEquations(
          new AdditionalEquations() {

            public String getName() {
              return "linear";
            }

            public double[] computeDerivatives(SpacecraftState s, double[] pDot) {
              pDot[0] = 1.0;
              return new double[7];
            }
          });
      Assert.fail("an exception should have been thrown");
    } catch (OrekitException oe) {
      Assert.assertEquals(oe.getSpecifier(), OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE);
    }
    try {
      propagator.addAdditionalStateProvider(
          new AdditionalStateProvider() {
            public String getName() {
              return "linear";
            }

            public double[] getAdditionalState(SpacecraftState state) {
              return null;
            }
          });
      Assert.fail("an exception should have been thrown");
    } catch (OrekitException oe) {
      Assert.assertEquals(oe.getSpecifier(), OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE);
    }
    propagator.addAdditionalStateProvider(
        new AdditionalStateProvider() {
          public String getName() {
            return "constant";
          }

          public double[] getAdditionalState(SpacecraftState state) {
            return new double[] {1.0};
          }
        });
    Assert.assertTrue(propagator.isAdditionalStateManaged("linear"));
    Assert.assertTrue(propagator.isAdditionalStateManaged("constant"));
    Assert.assertFalse(propagator.isAdditionalStateManaged("non-managed"));
    Assert.assertEquals(2, propagator.getManagedAdditionalStates().length);
    propagator.setInitialState(propagator.getInitialState().addAdditionalState("linear", 1.5));

    CheckingHandler<AdditionalStateLinearDetector> checking =
        new CheckingHandler<AdditionalStateLinearDetector>(Action.STOP);
    propagator.addEventDetector(
        new AdditionalStateLinearDetector(10.0, 1.0e-8).withHandler(checking));

    final double dt = 3200;
    checking.assertEvent(false);
    final SpacecraftState finalState = propagator.propagate(initDate.shiftedBy(dt));
    checking.assertEvent(true);
    Assert.assertEquals(3.0, finalState.getAdditionalState("linear")[0], 1.0e-8);
    Assert.assertEquals(1.5, finalState.getDate().durationFrom(initDate), 1.0e-8);
  }