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.draw.point.OrientedPoint2d;
9   import org.djutils.exceptions.Throw;
10  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
11  import org.opentrafficsim.core.network.Network;
12  
13  import nl.tudelft.simulation.dsol.SimRuntimeException;
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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
23   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
24   */
25  public class GtuDumper
26  {
27      /**
28       * Write all GTU positions in a file.
29       */
30      public void dump()
31      {
32          try
33          {
34              Time now = this.simulator.getSimulatorAbsTime();
35              String fileName = String.format("%s%08.2f.txt", this.fileNamePrefix, now.si);
36              PrintWriter pw = new PrintWriter(new File(fileName));
37              for (Gtu gtu : this.network.getGTUs())
38              {
39                  OrientedPoint2d dp = gtu.getOperationalPlan().getLocation(now);
40                  pw.format("%s position %.3f,%.3f dir=%5.1f speed %s\n", gtu.toString(), dp.x, dp.y,
41                          Math.toDegrees(dp.getDirZ()), gtu.getSpeed());
42              }
43              pw.close();
44              this.simulator.scheduleEventRel(this.interval, this, "dump", new Object[] {});
45          }
46          catch (Exception e)
47          {
48              e.printStackTrace();
49          }
50      }
51  
52      /** Interval time between dumps. */
53      private final Duration interval;
54  
55      /** The network with the GTUs to dump. */
56      private final Network network;
57  
58      /** Directory and first part of the file names. */
59      private final String fileNamePrefix;
60  
61      /** The simulator. */
62      private final OtsSimulatorInterface simulator;
63  
64      /**
65       * Construct a new GTUDumper.
66       * @param firstDumpTime Time; the time of the first dump
67       * @param interval Duration; the interval until each subsequent dump
68       * @param network Network; the network (that will contain the GTUs to dump)
69       * @param fileNamePrefix String; directory and first part if the file names; the simulation time of the dump will be
70       *            appended to the file name. The file type will be .txt
71       * @throws SimRuntimeException when scheduling the first dump time fails
72       */
73      public GtuDumper(final Time firstDumpTime, final Duration interval, final Network network, final String fileNamePrefix)
74              throws SimRuntimeException
75      {
76          Throw.whenNull(network, "Network may not be null");
77          this.simulator = network.getSimulator();
78          Throw.whenNull(firstDumpTime, "firstDumpTime may not be null");
79          Throw.when(firstDumpTime.lt(this.simulator.getSimulatorAbsTime()), RuntimeException.class,
80                  "firstDumptTime may not be before current simulator time");
81          Throw.whenNull(interval, "interval may not be null");
82          Throw.when(interval.le(Duration.ZERO), RuntimeException.class, "Duration must be positive");
83          Throw.whenNull(fileNamePrefix, "fileNamePrefix may not be null");
84          this.interval = interval;
85          this.network = network;
86          this.fileNamePrefix = fileNamePrefix;
87          this.simulator.scheduleEventAbsTime(firstDumpTime, this, "dump", new Object[] {});
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public String toString()
93      {
94          return "GTUDumper [interval=" + this.interval + ", network=" + this.network + ", fileNamePrefix=" + this.fileNamePrefix
95                  + "]";
96      }
97  
98  }