View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.File;
4   import java.io.PrintWriter;
5   
6   import org.djunits.value.vdouble.scalar.Duration;
7   import org.djunits.value.vdouble.scalar.Time;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
10  import org.opentrafficsim.core.network.OTSNetwork;
11  
12  import nl.tudelft.simulation.dsol.SimRuntimeException;
13  import nl.tudelft.simulation.language.d3.DirectedPoint;
14  
15  /**
16   * GTUDUmper; create a text file with the locations, directions and speeds of all GTUs at regular intervals.
17   * <p>
18   * Copyright (c) 2019-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version July 5, 2019 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class GTUDumper
27  {
28      /**
29       * Write all GTU positions in a file.
30       */
31      public void dump()
32      {
33          try
34          {
35              Time now = this.simulator.getSimulatorTime();
36              String fileName = String.format("%s%08.2f.txt", fileNamePrefix, now.si);
37              PrintWriter pw = new PrintWriter(new File(fileName));
38              for (GTU gtu : this.network.getGTUs())
39              {
40                  DirectedPoint dp = gtu.getOperationalPlan().getLocation(now);
41                  pw.format("%s position %.3f,%.3f dir=%5.1f speed %s\n", gtu.toString(), dp.x, dp.y,
42                          Math.toDegrees(dp.getRotZ()), gtu.getSpeed());
43              }
44              pw.close();
45              this.simulator.scheduleEventRel(this.interval, this, this, "dump", new Object[] {});
46          }
47          catch (Exception e)
48          {
49              e.printStackTrace();
50          }
51      }
52  
53      /** Interval time between dumps. */
54      private final Duration interval;
55  
56      /** The network with the GTUs to dump. */
57      private final OTSNetwork network;
58  
59      /** Directory and first part of the file names. */
60      private final String fileNamePrefix;
61  
62      /** The simulator. */
63      private final OTSSimulatorInterface simulator;
64  
65      /**
66       * Construct a new GTUDumper.
67       * @param firstDumpTime Time; the time of the first dump
68       * @param interval Duration; the interval until each subsequent dump
69       * @param network OTSNetwork; the network (that will contain the GTUs to dump)
70       * @param fileNamePrefix String; directory and first part if the file names; the simulation time of the dump will be
71       *            appended to the file name. The file type will be .txt
72       * @throws SimRuntimeException when scheduling the first dump time fails
73       */
74      public GTUDumper(final Time firstDumpTime, final Duration interval, final OTSNetwork network,
75              final String fileNamePrefix) throws SimRuntimeException
76      {
77          Throw.whenNull(network, "Network may not be null");
78          this.simulator = network.getSimulator();
79          Throw.whenNull(firstDumpTime, "firstDumpTime may not be null");
80          Throw.when(firstDumpTime.lt(this.simulator.getSimulatorTime()), RuntimeException.class,
81                  "firstDumptTime may not be before current simulator time");
82          Throw.whenNull(interval, "interval may not be null");
83          Throw.when(interval.le(Duration.ZERO), RuntimeException.class, "Duration must be positive");
84          Throw.whenNull(fileNamePrefix, "fileNamePrefix may not be null");
85          this.interval = interval;
86          this.network = network;
87          this.fileNamePrefix = fileNamePrefix;
88          simulator.scheduleEventAbs(firstDumpTime, this, this, "dump", new Object[] {});
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public String toString()
94      {
95          return "GTUDumper [interval=" + interval + ", network=" + network + ", fileNamePrefix=" + fileNamePrefix + "]";
96      }
97  
98  }