Initial commit
[debian/openrocket] / src / net / sf / openrocket / simulation / listeners / StopSimulationListener.java
1 package net.sf.openrocket.simulation.listeners;
2
3 import java.util.Collection;
4 import java.util.Collections;
5
6 import net.sf.openrocket.simulation.FlightEvent;
7 import net.sf.openrocket.simulation.SimulationStatus;
8
9
10 public class StopSimulationListener extends AbstractSimulationListener {
11
12         private final int REPORT = 500;
13         
14         private final double stopTime;
15         private final int stopStep;
16
17         private int step = 0;
18
19         private long startTime = -1;
20         private long time = -1;
21         
22         public StopSimulationListener(double t, int n) {
23                 stopTime = t;
24                 stopStep = n;
25         }
26         
27         
28         @Override
29         public Collection<FlightEvent> handleEvent(FlightEvent event,
30                         SimulationStatus status) {
31
32                 if (event.getType() == FlightEvent.Type.LAUNCH) {
33                         System.out.println("Simulation starting.");
34                         time = System.nanoTime();
35                         startTime = System.nanoTime();
36                 }
37                 
38                 return null;
39         }
40
41         @Override
42         public Collection<FlightEvent> stepTaken(SimulationStatus status) {
43                 step ++;
44                 if ((step%REPORT) == 0) {
45                         long t = System.nanoTime();
46                         
47                         System.out.printf("Step %4d, time=%.3f, took %d us/step (avg. %d us/step)\n",
48                                         step,status.time,(t-time)/1000/REPORT,(t-startTime)/1000/step);
49                         time = t;
50                 }
51                 if (status.time >= stopTime || step >= stopStep) {
52                         System.out.printf("Stopping simulation, step=%d time=%.3f\n",step,status.time);
53                         return Collections.singleton(new FlightEvent(FlightEvent.Type.SIMULATION_END,
54                                         status.time, null));
55                 }
56                 return null;
57         }
58
59 }