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.opentrafficsim.core.dsol.OTSSimulatorInterface;
9 import org.opentrafficsim.core.network.OTSNetwork;
10
11 import nl.tudelft.simulation.dsol.SimRuntimeException;
12 import nl.tudelft.simulation.language.d3.DirectedPoint;
13
14 /**
15 * GTUDUmper; create a text file with the locations, directions and speeds of all GTUs at regular intervals.
16 * <p>
17 * Copyright (c) 2019-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19 * <p>
20 * @version $Revision$, $LastChangedDate$, by $Author$, initial version July 5, 2019 <br>
21 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23 * @author <a href="http://www.transport.citg.tudelft.nl">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.getSimulatorTime();
35 String fileName = String.format("%s%08.2f.txt", fileNamePrefix, now.si);
36 PrintWriter pw = new PrintWriter(new File(fileName));
37 for (GTU gtu : this.network.getGTUs())
38 {
39 DirectedPoint 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.getRotZ()), gtu.getSpeed());
42 }
43 pw.close();
44 this.simulator.scheduleEventRel(this.interval, this, 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 OTSNetwork 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 simulator OTSSimulatorInterface; the simulator
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 OTSSimulatorInterface simulator, final Time firstDumpTime, final Duration interval,
75 final OTSNetwork network, final String fileNamePrefix) throws SimRuntimeException
76 {
77 this.simulator = simulator;
78 this.interval = interval;
79 this.network = network;
80 this.fileNamePrefix = fileNamePrefix;
81 simulator.scheduleEventAbs(firstDumpTime, this, this, "dump", new Object[] {});
82 }
83
84 }