Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/SUMOTime.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file SUMOTime.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @author Mirko Barthauer
19
/// @date Fri, 29.04.2005
20
///
21
// Variables, methods, and tools for internal time representation
22
/****************************************************************************/
23
#pragma once
24
#include <config.h>
25
#include <limits>
26
#include <string>
27
#include "UtilExceptions.h"
28
29
30
// ===========================================================================
31
// type definitions
32
// ===========================================================================
33
typedef long long int SUMOTime;
34
#define SUMOTime_MAX (std::numeric_limits<SUMOTime>::max() - 1000)
35
#define SUMOTime_MIN std::numeric_limits<SUMOTime>::min()
36
#define SUMOTime_MAX_PERIOD (SUMOTime_MAX - SUMOTime_MAX % DELTA_T)
37
38
// the step length in ms
39
extern SUMOTime DELTA_T;
40
41
// the step length in seconds as double
42
#define TS (static_cast<double>(DELTA_T)/1000.)
43
44
// x*deltaT
45
#define SPEED2DIST(x) ((x)*TS)
46
// x/deltaT
47
#define DIST2SPEED(x) ((x)/TS)
48
// x*deltaT*deltaT
49
#define ACCEL2DIST(x) ((x)*TS*TS)
50
// x*deltaT
51
#define ACCEL2SPEED(x) ((x)*TS)
52
// x*deltaT
53
#define SPEED2ACCEL(x) ((x)/TS)
54
55
#define STEPS2TIME(x) (static_cast<double>(x)/1000.)
56
// static cast to long long int truncates so we must pad away from 0 for correct rounding
57
#define TIME2STEPS(x) (static_cast<SUMOTime>((x) * 1000. + ((x) >= 0 ? 0.5 : -0.5)))
58
#define STEPFLOOR(x) (static_cast<SUMOTime>((x)/DELTA_T)*DELTA_T)
59
#define STEPS2MS(x) (x)
60
61
#define SIMSTEP MSNet::getInstance()->getCurrentTimeStep()
62
#define SIMTIME STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep())
63
64
// ===========================================================================
65
// method declarations
66
// ===========================================================================
67
68
/// @brief convert string to SUMOTime
69
SUMOTime string2time(const std::string& r);
70
71
/// @brief check if the given string is a valid time
72
bool isTime(const std::string& r);
73
74
/// @brief convert SUMOTime to string (independently of global format setting)
75
std::string time2string(SUMOTime t, bool humanReadable);
76
77
/// @brief convert SUMOTime to string (using the global format setting)
78
std::string time2string(SUMOTime t);
79
80
/// @brief convert ms to string for log output
81
std::string elapsedMs2string(long long int t);
82
83
/// @brief check if given SUMOTime is multiple of the step length
84
bool checkStepLengthMultiple(const SUMOTime t, const std::string& error = "", SUMOTime deltaT = DELTA_T, SUMOTime begin = 0);
85
86
/// @brief check the valid SUMOTime range of double input and throw an error if out of bounds
87
void checkTimeBounds(const double time);
88
89