1 package net.sf.openrocket.simulation.listeners.example;
3 import net.sf.openrocket.simulation.FlightEvent;
4 import net.sf.openrocket.simulation.SimulationStatus;
5 import net.sf.openrocket.simulation.exception.SimulationException;
6 import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
9 * A simulation listener that stops the simulation after a specified number of steps or
10 * after a specified abount of simulation time.
12 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14 public class StopSimulationListener extends AbstractSimulationListener {
16 private final int REPORT = 500;
18 private final double stopTime;
19 private final int stopStep;
23 private long startTime = -1;
24 private long time = -1;
26 public StopSimulationListener(double t, int n) {
32 public boolean handleFlightEvent(SimulationStatus status, FlightEvent event) {
34 if (event.getType() == FlightEvent.Type.LAUNCH) {
35 System.out.println("Simulation starting.");
36 time = System.nanoTime();
37 startTime = System.nanoTime();
45 public void postStep(SimulationStatus status) throws SimulationException {
47 if ((step % REPORT) == 0) {
48 long t = System.nanoTime();
50 System.out.printf("Step %4d, time=%.3f, took %d us/step (avg. %d us/step)\n",
51 step, status.getSimulationTime(), (t - time) / 1000 / REPORT, (t - startTime) / 1000 / step);
54 if (status.getSimulationTime() >= stopTime || step >= stopStep) {
55 System.out.printf("Stopping simulation, step=%d time=%.3f\n", step, status.getSimulationTime());
56 status.getEventQueue().add(new FlightEvent(FlightEvent.Type.SIMULATION_END,
57 status.getSimulationTime(), null));