private void checkNaN() throws SimulationException {
    double d = 0;
    boolean b = false;
    d += status.getSimulationTime();
    d += status.getPreviousTimeStep();
    b |= status.getRocketPosition().isNaN();
    b |= status.getRocketVelocity().isNaN();
    b |= status.getRocketOrientationQuaternion().isNaN();
    b |= status.getRocketRotationVelocity().isNaN();
    d += status.getEffectiveLaunchRodLength();

    if (Double.isNaN(d) || b) {
      log.error(
          "Simulation resulted in NaN value:"
              + " simulationTime="
              + status.getSimulationTime()
              + " previousTimeStep="
              + status.getPreviousTimeStep()
              + " rocketPosition="
              + status.getRocketPosition()
              + " rocketVelocity="
              + status.getRocketVelocity()
              + " rocketOrientationQuaternion="
              + status.getRocketOrientationQuaternion()
              + " rocketRotationVelocity="
              + status.getRocketRotationVelocity()
              + " effectiveLaunchRodLength="
              + status.getEffectiveLaunchRodLength());
      throw new SimulationException(
          "Simulation resulted in not-a-number (NaN) value, please report a bug.");
    }
  }
  @Override
  public void optimize(Point initial, OptimizationController control) throws OptimizationException {

    if (initial.dim() != 1) {
      throw new IllegalArgumentException(
          "Only single-dimensional optimization supported, dim=" + initial.dim());
    }

    log.info("Starting golden section search for optimization");

    Point guessAC = null;
    Point guessBD = null;

    try {
      boolean guessedAC;

      Point previous = p(0);
      double previousValue = Double.NaN;
      current = previous;
      double currentValue = Double.NaN;

      /*
       * Initialize the points + computation.
       */
      Point a = p(0);
      Point d = p(1.0);
      Point b = section1(a, d);
      Point c = section2(a, d);

      functionExecutor.compute(a);
      functionExecutor.compute(d);
      functionExecutor.compute(b);
      functionExecutor.compute(c);

      // Wait for points a and d, which normally are already precomputed
      functionExecutor.waitFor(a);
      functionExecutor.waitFor(d);

      boolean continueOptimization = true;
      while (continueOptimization) {

        /*
         * Get values at A & D for guessing.
         * These are pre-calculated except during the first step.
         */
        double fa, fd;
        fa = functionExecutor.getValue(a);
        fd = functionExecutor.getValue(d);

        /*
         * Start calculating possible two next points.  The order of evaluation
         * is selected based on the function values at A and D.
         */
        guessAC = section1(a, c);
        guessBD = section2(b, d);
        if (Double.isNaN(fd) || fa < fd) {
          guessedAC = true;
          functionExecutor.compute(guessAC);
          functionExecutor.compute(guessBD);
        } else {
          guessedAC = false;
          functionExecutor.compute(guessBD);
          functionExecutor.compute(guessAC);
        }

        /*
         * Get values at B and C.
         */
        double fb, fc;
        functionExecutor.waitFor(b);
        functionExecutor.waitFor(c);
        fb = functionExecutor.getValue(b);
        fc = functionExecutor.getValue(c);

        double min = MathUtil.min(fa, fb, fc, fd);
        if (Double.isNaN(min)) {
          throw new OptimizationException("Unable to compute initial function values");
        }

        /*
         * Update previous and current values for step control.
         */
        previousValue = currentValue;
        currentValue = min;
        previous = current;
        if (min == fa) {
          current = a;
        } else if (min == fb) {
          current = b;
        } else if (min == fc) {
          current = c;
        } else {
          current = d;
        }

        /*
         * Select next positions.  These are already being calculated in the background
         * as guessAC and guessBD.
         */
        if (min == fa || min == fb) {
          d = c;
          c = b;
          b = guessAC;
          functionExecutor.abort(guessBD);
          guessBD = null;
          log.debug("Selecting A-C region, a=" + a.get(0) + " c=" + c.get(0));
          if (guessedAC) {
            guessSuccess++;
          } else {
            guessFailure++;
          }
        } else {
          a = b;
          b = c;
          c = guessBD;
          functionExecutor.abort(guessAC);
          guessAC = null;
          log.debug("Selecting B-D region, b=" + b.get(0) + " d=" + d.get(0));
          if (!guessedAC) {
            guessSuccess++;
          } else {
            guessFailure++;
          }
        }

        /*
         * Check optimization control.
         */
        continueOptimization =
            control.stepTaken(previous, previousValue, current, currentValue, c.get(0) - a.get(0));

        if (Thread.interrupted()) {
          throw new InterruptedException();
        }
      }

    } catch (InterruptedException e) {
      log.info("Optimization was interrupted with InterruptedException");
    }

    if (guessAC != null) {
      functionExecutor.abort(guessAC);
    }
    if (guessBD != null) {
      functionExecutor.abort(guessBD);
    }

    log.info(
        "Finishing optimization at point " + getOptimumPoint() + " value " + getOptimumValue());
    log.info("Optimization statistics: " + getStatistics());
  }
  @Override
  public FlightData simulate(SimulationConditions simulationConditions) throws SimulationException {
    Set<MotorId> motorBurntOut = new HashSet<MotorId>();

    // Set up flight data
    FlightData flightData = new FlightData();

    // Set up rocket configuration
    Configuration configuration = setupConfiguration(simulationConditions);
    MotorInstanceConfiguration motorConfiguration = setupMotorConfiguration(configuration);
    if (motorConfiguration.getMotorIDs().isEmpty()) {
      throw new MotorIgnitionException("No motors defined in the simulation.");
    }

    // Initialize the simulation
    currentStepper = flightStepper;
    status = initialStatus(configuration, motorConfiguration, simulationConditions, flightData);
    status = currentStepper.initialize(status);

    SimulationListenerHelper.fireStartSimulation(status);
    // Get originating position (in case listener has modified launch position)
    Coordinate origin = status.getRocketPosition();
    Coordinate originVelocity = status.getRocketVelocity();

    try {
      double maxAlt = Double.NEGATIVE_INFINITY;

      // Start the simulation
      while (handleEvents()) {

        // Take the step
        double oldAlt = status.getRocketPosition().z;

        if (SimulationListenerHelper.firePreStep(status)) {
          // Step at most to the next event
          double maxStepTime = Double.MAX_VALUE;
          FlightEvent nextEvent = status.getEventQueue().peek();
          if (nextEvent != null) {
            maxStepTime = MathUtil.max(nextEvent.getTime() - status.getSimulationTime(), 0.001);
          }
          log.verbose(
              "BasicEventSimulationEngine: Taking simulation step at t="
                  + status.getSimulationTime());
          currentStepper.step(status, maxStepTime);
        }
        SimulationListenerHelper.firePostStep(status);

        // Calculate values for custom expressions
        FlightDataBranch data = status.getFlightData();
        ArrayList<CustomExpression> allExpressions =
            status.getSimulationConditions().getSimulation().getCustomExpressions();
        for (CustomExpression expression : allExpressions) {
          data.setValue(expression.getType(), expression.evaluate(status));
        }

        // Check for NaN values in the simulation status
        checkNaN();

        // Add altitude event
        addEvent(
            new FlightEvent(
                FlightEvent.Type.ALTITUDE,
                status.getSimulationTime(),
                status.getConfiguration().getRocket(),
                new Pair<Double, Double>(oldAlt, status.getRocketPosition().z)));

        if (status.getRocketPosition().z > maxAlt) {
          maxAlt = status.getRocketPosition().z;
        }

        // Position relative to start location
        Coordinate relativePosition = status.getRocketPosition().sub(origin);

        // Add appropriate events
        if (!status.isLiftoff()) {

          // Avoid sinking into ground before liftoff
          if (relativePosition.z < 0) {
            status.setRocketPosition(origin);
            status.setRocketVelocity(originVelocity);
          }
          // Detect lift-off
          if (relativePosition.z > 0.02) {
            addEvent(new FlightEvent(FlightEvent.Type.LIFTOFF, status.getSimulationTime()));
          }

        } else {

          // Check ground hit after liftoff
          if (status.getRocketPosition().z < 0) {
            status.setRocketPosition(status.getRocketPosition().setZ(0));
            addEvent(new FlightEvent(FlightEvent.Type.GROUND_HIT, status.getSimulationTime()));
            addEvent(new FlightEvent(FlightEvent.Type.SIMULATION_END, status.getSimulationTime()));
          }
        }

        // Check for launch guide clearance
        if (!status.isLaunchRodCleared()
            && relativePosition.length() > status.getSimulationConditions().getLaunchRodLength()) {
          addEvent(new FlightEvent(FlightEvent.Type.LAUNCHROD, status.getSimulationTime(), null));
        }

        // Check for apogee
        if (!status.isApogeeReached() && status.getRocketPosition().z < maxAlt - 0.01) {
          addEvent(
              new FlightEvent(
                  FlightEvent.Type.APOGEE,
                  status.getSimulationTime(),
                  status.getConfiguration().getRocket()));
        }

        // Check for burnt out motors
        for (MotorId motorId : status.getMotorConfiguration().getMotorIDs()) {
          MotorInstance motor = status.getMotorConfiguration().getMotorInstance(motorId);
          if (!motor.isActive() && motorBurntOut.add(motorId)) {
            addEvent(
                new FlightEvent(
                    FlightEvent.Type.BURNOUT,
                    status.getSimulationTime(),
                    (RocketComponent) status.getMotorConfiguration().getMotorMount(motorId),
                    motorId));
          }
        }
      }

    } catch (SimulationException e) {
      SimulationListenerHelper.fireEndSimulation(status, e);
      throw e;
    }

    SimulationListenerHelper.fireEndSimulation(status, null);

    flightData.addBranch(status.getFlightData());

    if (!flightData.getWarningSet().isEmpty()) {
      log.info("Warnings at the end of simulation:  " + flightData.getWarningSet());
    }

    // TODO: HIGH: Simulate branches
    return flightData;
  }
  /**
   * Handles events occurring during the flight from the event queue. Each event that has occurred
   * before or at the current simulation time is processed. Suitable events are also added to the
   * flight data.
   */
  private boolean handleEvents() throws SimulationException {
    boolean ret = true;
    FlightEvent event;

    for (event = nextEvent(); event != null; event = nextEvent()) {

      // Ignore events for components that are no longer attached to the rocket
      if (event.getSource() != null
          && event.getSource().getParent() != null
          && !status.getConfiguration().isStageActive(event.getSource().getStageNumber())) {
        continue;
      }

      // Call simulation listeners, allow aborting event handling
      if (!SimulationListenerHelper.fireHandleFlightEvent(status, event)) {
        continue;
      }

      if (event.getType() != FlightEvent.Type.ALTITUDE) {
        log.verbose("BasicEventSimulationEngine:  Handling event " + event);
      }

      if (event.getType() == FlightEvent.Type.IGNITION) {
        MotorMount mount = (MotorMount) event.getSource();
        MotorId motorId = (MotorId) event.getData();
        MotorInstance instance = status.getMotorConfiguration().getMotorInstance(motorId);
        if (!SimulationListenerHelper.fireMotorIgnition(status, motorId, mount, instance)) {
          continue;
        }
      }

      if (event.getType() == FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT) {
        RecoveryDevice device = (RecoveryDevice) event.getSource();
        if (!SimulationListenerHelper.fireRecoveryDeviceDeployment(status, device)) {
          continue;
        }
      }

      // Check for motor ignition events, add ignition events to queue
      for (MotorId id : status.getMotorConfiguration().getMotorIDs()) {
        MotorMount mount = status.getMotorConfiguration().getMotorMount(id);
        RocketComponent component = (RocketComponent) mount;

        if (mount.getIgnitionEvent().isActivationEvent(event, component)) {
          addEvent(
              new FlightEvent(
                  FlightEvent.Type.IGNITION,
                  status.getSimulationTime() + mount.getIgnitionDelay(),
                  component,
                  id));
        }
      }

      // Check for stage separation event
      for (int stageNo : status.getConfiguration().getActiveStages()) {
        if (stageNo == 0) continue;

        Stage stage = (Stage) status.getConfiguration().getRocket().getChild(stageNo);
        if (stage.getSeparationEvent().isSeparationEvent(event, stage)) {
          addEvent(
              new FlightEvent(
                  FlightEvent.Type.STAGE_SEPARATION,
                  event.getTime() + stage.getSeparationDelay(),
                  stage));
        }
      }

      // Check for recovery device deployment, add events to queue
      Iterator<RocketComponent> rci = status.getConfiguration().iterator();
      while (rci.hasNext()) {
        RocketComponent c = rci.next();
        if (!(c instanceof RecoveryDevice)) continue;
        if (((RecoveryDevice) c).getDeployEvent().isActivationEvent(event, c)) {
          // Delay event by at least 1ms to allow stage separation to occur first
          addEvent(
              new FlightEvent(
                  FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT,
                  event.getTime() + Math.max(0.001, ((RecoveryDevice) c).getDeployDelay()),
                  c));
        }
      }

      // Handle event
      switch (event.getType()) {
        case LAUNCH:
          {
            status.getFlightData().addEvent(event);
            break;
          }

        case IGNITION:
          {
            // Ignite the motor
            MotorMount mount = (MotorMount) event.getSource();
            RocketComponent component = (RocketComponent) mount;
            MotorId motorId = (MotorId) event.getData();
            MotorInstanceConfiguration config = status.getMotorConfiguration();
            config.setMotorIgnitionTime(motorId, event.getTime());
            status.setMotorIgnited(true);
            status.getFlightData().addEvent(event);

            break;
          }

        case LIFTOFF:
          {
            // Mark lift-off as occurred
            status.setLiftoff(true);
            status.getFlightData().addEvent(event);
            break;
          }

        case LAUNCHROD:
          {
            // Mark launch rod as cleared
            status.setLaunchRodCleared(true);
            status.getFlightData().addEvent(event);
            break;
          }

        case BURNOUT:
          {
            // If motor burnout occurs without lift-off, abort
            if (!status.isLiftoff()) {
              throw new SimulationLaunchException("Motor burnout without liftoff.");
            }
            // Add ejection charge event
            String id = status.getConfiguration().getMotorConfigurationID();
            MotorMount mount = (MotorMount) event.getSource();
            double delay = mount.getMotorDelay(id);
            if (delay != Motor.PLUGGED) {
              addEvent(
                  new FlightEvent(
                      FlightEvent.Type.EJECTION_CHARGE,
                      status.getSimulationTime() + delay,
                      event.getSource(),
                      event.getData()));
            }
            status.getFlightData().addEvent(event);
            break;
          }

        case EJECTION_CHARGE:
          {
            status.getFlightData().addEvent(event);
            break;
          }

        case STAGE_SEPARATION:
          {
            // TODO: HIGH: Store lower stages to be simulated later
            RocketComponent stage = event.getSource();
            int n = stage.getStageNumber();
            status.getConfiguration().setToStage(n - 1);
            status.getFlightData().addEvent(event);
            break;
          }

        case APOGEE:
          // Mark apogee as reached
          status.setApogeeReached(true);
          status.getFlightData().addEvent(event);
          break;

        case RECOVERY_DEVICE_DEPLOYMENT:
          RocketComponent c = event.getSource();
          int n = c.getStageNumber();
          // Ignore event if stage not active
          if (status.getConfiguration().isStageActive(n)) {
            // TODO: HIGH: Check stage activeness for other events as well?

            // Check whether any motor in the active stages is active anymore
            for (MotorId motorId : status.getMotorConfiguration().getMotorIDs()) {
              int stage =
                  ((RocketComponent) status.getMotorConfiguration().getMotorMount(motorId))
                      .getStageNumber();
              if (!status.getConfiguration().isStageActive(stage)) continue;
              if (!status.getMotorConfiguration().getMotorInstance(motorId).isActive()) continue;
              status.getWarnings().add(Warning.RECOVERY_DEPLOYMENT_WHILE_BURNING);
            }

            // Check for launch rod
            if (!status.isLaunchRodCleared()) {
              status
                  .getWarnings()
                  .add(
                      Warning.fromString(
                          "Recovery device device deployed while on " + "the launch guide."));
            }

            // Check current velocity
            if (status.getRocketVelocity().length() > 20) {
              // TODO: LOW: Custom warning.
              status
                  .getWarnings()
                  .add(
                      Warning.fromString(
                          "Recovery device deployment at high "
                              + "speed ("
                              + UnitGroup.UNITS_VELOCITY.toStringUnit(
                                  status.getRocketVelocity().length())
                              + ")."));
            }

            status.setLiftoff(true);
            status.getDeployedRecoveryDevices().add((RecoveryDevice) c);

            this.currentStepper = this.landingStepper;
            this.status = currentStepper.initialize(status);

            status.getFlightData().addEvent(event);
          }
          break;

        case GROUND_HIT:
          status.getFlightData().addEvent(event);
          break;

        case SIMULATION_END:
          ret = false;
          status.getFlightData().addEvent(event);
          break;

        case ALTITUDE:
          break;
      }
    }

    // If no motor has ignited, abort
    if (!status.isMotorIgnited()) {
      throw new MotorIgnitionException("No motors ignited.");
    }

    return ret;
  }