private FlightDataBranch simulateLoop() { // Initialize the simulation currentStepper = flightStepper; currentStatus = currentStepper.initialize(currentStatus); // Get originating position (in case listener has modified launch position) Coordinate origin = currentStatus.getRocketPosition(); Coordinate originVelocity = currentStatus.getRocketVelocity(); try { // Start the simulation while (handleEvents()) { // Take the step double oldAlt = currentStatus.getRocketPosition().z; if (SimulationListenerHelper.firePreStep(currentStatus)) { // Step at most to the next event double maxStepTime = Double.MAX_VALUE; FlightEvent nextEvent = currentStatus.getEventQueue().peek(); if (nextEvent != null) { maxStepTime = MathUtil.max(nextEvent.getTime() - currentStatus.getSimulationTime(), 0.001); } log.trace( "BasicEventSimulationEngine: Taking simulation step at t=" + currentStatus.getSimulationTime()); currentStepper.step(currentStatus, maxStepTime); } SimulationListenerHelper.firePostStep(currentStatus); // Check for NaN values in the simulation status checkNaN(); // Add altitude event addEvent( new FlightEvent( FlightEvent.Type.ALTITUDE, currentStatus.getSimulationTime(), currentStatus.getConfiguration().getRocket(), new Pair<Double, Double>(oldAlt, currentStatus.getRocketPosition().z))); if (currentStatus.getRocketPosition().z > currentStatus.getMaxAlt()) { currentStatus.setMaxAlt(currentStatus.getRocketPosition().z); } // Position relative to start location Coordinate relativePosition = currentStatus.getRocketPosition().sub(origin); // Add appropriate events if (!currentStatus.isLiftoff()) { // Avoid sinking into ground before liftoff if (relativePosition.z < 0) { currentStatus.setRocketPosition(origin); currentStatus.setRocketVelocity(originVelocity); } // Detect lift-off if (relativePosition.z > 0.02) { addEvent(new FlightEvent(FlightEvent.Type.LIFTOFF, currentStatus.getSimulationTime())); } } else { // Check ground hit after liftoff if (currentStatus.getRocketPosition().z < 0) { currentStatus.setRocketPosition(currentStatus.getRocketPosition().setZ(0)); addEvent( new FlightEvent(FlightEvent.Type.GROUND_HIT, currentStatus.getSimulationTime())); addEvent( new FlightEvent( FlightEvent.Type.SIMULATION_END, currentStatus.getSimulationTime())); } } // Check for launch guide clearance if (!currentStatus.isLaunchRodCleared() && relativePosition.length() > currentStatus.getSimulationConditions().getLaunchRodLength()) { addEvent( new FlightEvent(FlightEvent.Type.LAUNCHROD, currentStatus.getSimulationTime(), null)); } // Check for apogee if (!currentStatus.isApogeeReached() && currentStatus.getRocketPosition().z < currentStatus.getMaxAlt() - 0.01) { currentStatus.setMaxAltTime(currentStatus.getSimulationTime()); addEvent( new FlightEvent( FlightEvent.Type.APOGEE, currentStatus.getSimulationTime(), currentStatus.getConfiguration().getRocket())); } // //@Obsolete // //@Redundant // // Check for burnt out motors // for( MotorClusterState state : currentStatus.getActiveMotors()){ // if ( state.isSpent()){ // addEvent(new FlightEvent(FlightEvent.Type.BURNOUT, // currentStatus.getSimulationTime(), // (RocketComponent) state.getMount(), state)); // } // } // Check for Tumbling // Conditions for transision are: // apogee reached (if sustainer stage) // and is not already tumbling // and not stable (cg > cp) // and aoa > AOA_TUMBLE_CONDITION threshold // and thrust < THRUST_TUMBLE_CONDITION threshold if (!currentStatus.isTumbling()) { final double t = currentStatus.getFlightData().getLast(FlightDataType.TYPE_THRUST_FORCE); final double cp = currentStatus.getFlightData().getLast(FlightDataType.TYPE_CP_LOCATION); final double cg = currentStatus.getFlightData().getLast(FlightDataType.TYPE_CG_LOCATION); final double aoa = currentStatus.getFlightData().getLast(FlightDataType.TYPE_AOA); final boolean wantToTumble = (cg > cp && aoa > AOA_TUMBLE_CONDITION); if (wantToTumble) { final boolean tooMuchThrust = t > THRUST_TUMBLE_CONDITION; // final boolean isSustainer = status.getConfiguration().isStageActive(0); final boolean isApogee = currentStatus.isApogeeReached(); if (tooMuchThrust) { currentStatus.getWarnings().add(Warning.TUMBLE_UNDER_THRUST); } else if (isApogee) { addEvent(new FlightEvent(FlightEvent.Type.TUMBLE, currentStatus.getSimulationTime())); currentStatus.setTumbling(true); } } } } } catch (SimulationException e) { SimulationListenerHelper.fireEndSimulation(currentStatus, e); // Add FlightEvent for Abort. currentStatus .getFlightData() .addEvent( new FlightEvent( FlightEvent.Type.EXCEPTION, currentStatus.getSimulationTime(), currentStatus.getConfiguration().getRocket(), e.getLocalizedMessage())); currentStatus.getWarnings().add(e.getLocalizedMessage()); } return currentStatus.getFlightData(); }