Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSCFModel_Rail.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-2026 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/****************************************************************************/
19// <description missing>
20/****************************************************************************/
21#include <config.h>
22
23#include <iostream>
29#include <microsim/MSVehicle.h>
31#include "MSCFModel_Rail.h"
32
33// ===========================================================================
34// trainParams method definitions
35// ===========================================================================
36
37double
40 return (resCoef_quadratic * speed * speed + resCoef_linear * speed + resCoef_constant); // kN
41 } else {
43 }
44}
45
46
47double
49 if (maxPower != INVALID_DOUBLE) {
50 return MIN2(maxPower / speed, maxTraction); // kN
51 } else {
52 return LinearApproxHelpers::getInterpolatedValue(traction, speed); // kN
53 }
54}
55
56
57// ===========================================================================
58// RailVehicleVariables method definitions
59// ===========================================================================
60void
63 out.writeAttr(SUMO_ATTR_ID, "Rail");
64 std::ostringstream internals;
65 internals << odometerAngles.size() << " ";
66 for (auto item : odometerAngles) {
67 internals << item.first << " " << item.second << " ";
68 }
69 out.writeAttr(SUMO_ATTR_STATE, internals.str());
70 out.closeTag();
71}
72
73
74void
76 bool ok = true;
77 const std::string cfmID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
78 if (cfmID != "Rail") {
79 throw ProcessError(TLF("incompatible carFollowModel '%' when loading state for Rail", cfmID));
80 }
81 std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
82 int odometerAnglesSize;
83 bis >> odometerAnglesSize;
84 for (int i = 0; i < odometerAnglesSize; i++) {
85 double o;
86 double a;
87 bis >> o;
88 bis >> a;
89 odometerAngles.push_back(std::make_pair(o, a));
90 }
91}
92
93
94
95double
97 const double odo = veh->getOdometer();
98 // add new data point
99 if ((odometerAngles.empty() || odometerAngles.back().first != odo) && veh->hasDeparted()) {
100 odometerAngles.push_back(std::make_pair(odo, veh->getAngle()));
101 // clean up old data points beyond integration distance
102 while (odometerAngles.size() > 2) {
103 double distCleaned = odometerAngles.back().first - odometerAngles[1].first;
104 if (distCleaned >= curveIntegration) {
105 odometerAngles.erase(odometerAngles.begin());
106 } else {
107 break;
108 }
109 }
110 }
111 if (odometerAngles.size() > 1) {
112 const double dist = odometerAngles.back().first - odometerAngles.front().first;
113 const double angleDiff = GeomHelper::angleDiff(odometerAngles.back().second, odometerAngles.front().second);
114 return angleDiff == 0
115 ? std::numeric_limits<double>::max()
116 : dist / fabs(angleDiff);
117 } else {
118 return veh->getCurveRadius();
119 }
120}
121
122
123
124// ===========================================================================
125// method definitions
126// ===========================================================================
127
128
130 MSCFModel(vtype) {
131 const std::string trainType = vtype->getParameter().getCFParamString(SUMO_ATTR_TRAIN_TYPE, "NGT400");
132 if (trainType.compare("RB425") == 0) {
134 } else if (trainType.compare("RB628") == 0) {
136 } else if (trainType.compare("NGT400") == 0) {
138 } else if (trainType.compare("NGT400_16") == 0) {
140 } else if (trainType.compare("ICE1") == 0) {
142 } else if (trainType.compare("REDosto7") == 0) {
144 } else if (trainType.compare("Freight") == 0) {
146 } else if (trainType.compare("ICE3") == 0) {
148 } else if (trainType.compare("MireoPlusB") == 0) {
150 } else if (trainType.compare("MireoPlusH") == 0) {
152 } else if (trainType.compare("custom") == 0) {
154 } else {
155 WRITE_ERRORF(TL("Unknown train type: %. Exiting!"), trainType);
156 throw ProcessError();
157 }
158 // override with user values
159 if (vtype->wasSet(VTYPEPARS_MAXSPEED_SET)) {
160 myTrainParams.vmax = vtype->getMaxSpeed();
161 }
162 if (vtype->wasSet(VTYPEPARS_LENGTH_SET)) {
163 myTrainParams.length = vtype->getLength();
164 }
169 // update type parameters so they are shown correctly in the gui (if defaults from trainType are used)
170 const_cast<MSVehicleType*>(vtype)->setMaxSpeed(myTrainParams.vmax);
171 const_cast<MSVehicleType*>(vtype)->setLength(myTrainParams.length);
172 if (!vtype->wasSet(VTYPEPARS_MASS_SET)) {
173 // tons to kg
174 const_cast<MSVehicleType*>(vtype)->setMass(myTrainParams.weight * 1000);
175 }
176
177 // init tabular curves
180
181 // init parametric curves
187 // curve resistance parameters
195
197 throw ProcessError(TLF("Undefined maxPower for vType '%'.", vtype->getID()));
199 throw ProcessError(TLF("Undefined maxTraction for vType '%'.", vtype->getID()));
200 }
202 WRITE_WARNING(TLF("Ignoring tractionTable because maxPower and maxTraction are set for vType '%'.", vtype->getID()));
203 }
204 const bool hasSomeResCoef = (myTrainParams.resCoef_constant != INVALID_DOUBLE
207 const bool hasAllResCoef = (myTrainParams.resCoef_constant != INVALID_DOUBLE
210 if (hasSomeResCoef && !hasAllResCoef) {
211 throw ProcessError(TLF("Some undefined resistance coefficients for vType '%' (requires resCoef_constant, resCoef_linear and resCoef_quadratic)", vtype->getID()));
212 }
214 WRITE_WARNING(TLF("Ignoring resistanceTable because resistance coefficients are set for vType '%'.", vtype->getID()));
215 }
216
218 throw ProcessError(TLF("Either tractionTable or maxPower must be defined for vType '%' with Rail model type '%'.", vtype->getID(), trainType));
219 }
221 throw ProcessError(TLF("Either resistanceTable or resCoef_constant must be defined for vType '%' with Rail model type '%'.", vtype->getID(), trainType));
222 }
223}
224
225
227
228
229double MSCFModel_Rail::followSpeed(const MSVehicle* const veh, double speed, double gap,
230 double /* predSpeed */, double /* predMaxDecel*/, const MSVehicle* const /*pred*/, const CalcReason /*usage*/) const {
231
232 // followSpeed module is used for the simulation of moving block operations. The safety gap is chosen similar to the existing german
233 // system CIR-ELKE (based on LZB). Other implementations of moving block systems may differ, but for now no appropriate parameter
234 // can be set (would be per lane, not per train) -> hard-coded
235
236 // @note: default train minGap of 5 is already subtracted from gap
237 if (speed >= 30 / 3.6) {
238 // safety distance for higher speeds (>= 30 km/h)
239 gap = MAX2(0.0, gap + veh->getVehicleType().getMinGap() - 50);
240 }
241
242 const double vsafe = maximumSafeStopSpeed(gap, myDecel, speed, false, TS, false); // absolute breaking distance
243 const double vmin = minNextSpeed(speed, veh);
244 const double vmax = maxNextSpeed(speed, veh);
245
247 return MIN2(vsafe, vmax);
248 } else {
249 // ballistic
250 // XXX: the euler variant can break as strong as it wishes immediately! The ballistic cannot, refs. #2575.
251 return MAX2(MIN2(vsafe, vmax), vmin);
252 }
253}
254
255
256int
260
261
264 return new MSCFModel_Rail(vtype);
265}
266
267double
269 return getWeight(veh) * myTrainParams.mf;
270}
271
272double
273MSCFModel_Rail::getWeight(const MSVehicle* const veh) const {
274 // kg to tons
275 return veh->getVehicleType().getMass() / 1000;
276}
277
278double
282 assert(vars != nullptr);
283 const double r = vars->getIntegratedRadius(veh, myTrainParams.curveIntegration);
284 if (r == std::numeric_limits<double>::max()) {
285 return 0;
286 } else if (r >= myTrainParams.roeckl_sharp_radius) {
288 } else if (r > myTrainParams.roeckl_offset_sharp) {
290 } else {
291 WRITE_WARNINGF("Cannot compute curve resistance for vehicle '%' with radius % at time %",
292 veh->getID(), r, time2string(SIMSTEP));
293 return 0;
294 }
295 }
296 return 0;
297}
298
299
300double MSCFModel_Rail::maxNextSpeed(double speed, const MSVehicle* const veh) const {
301
302 if (speed >= myTrainParams.vmax) {
303 return myTrainParams.vmax;
304 }
305
306 double targetSpeed = myTrainParams.vmax;
307
308 double res = myTrainParams.getResistance(speed); // kN
309
310 double slope = veh->getSlope();
311 double gr = getWeight(veh) * GRAVITY * sin(DEG2RAD(slope)); //kN
312 double cr = getWeight(veh) * getCurveResistance(veh); //kN
313
314 double totalRes = res + gr + cr; //kN
315
316 double trac = myTrainParams.getTraction(speed); // kN
317 double a;
318 if (speed < targetSpeed) {
319 a = (trac - totalRes) / getRotWeight(veh); //kN/t == N/kg
320 } else {
321 a = 0.;
322 if (totalRes > trac) {
323 a = (trac - totalRes) / getRotWeight(veh); //kN/t == N/kg
324 }
325 }
326 double maxNextSpeed = speed + ACCEL2SPEED(a);
327
328// std::cout << veh->getID() << " speed: " << (speed*3.6) << std::endl;
329
331}
332
333
334double MSCFModel_Rail::minNextSpeed(double speed, const MSVehicle* const veh) const {
335
336 const double slope = veh->getSlope();
337 const double gr = getWeight(veh) * GRAVITY * sin(DEG2RAD(slope)); //kN
338 const double cr = getWeight(veh) * getCurveResistance(veh);
339 const double res = myTrainParams.getResistance(speed); // kN
340 const double totalRes = res + gr + cr; //kN
341 const double a = myTrainParams.decl + totalRes / getRotWeight(veh);
342 const double vMin = speed - ACCEL2SPEED(a);
344 return MAX2(vMin, 0.);
345 } else {
346 // NOTE: ballistic update allows for negative speeds to indicate a stop within the next timestep
347 return vMin;
348 }
349
350}
351
352
353double
354MSCFModel_Rail::minNextSpeedEmergency(double speed, const MSVehicle* const veh) const {
355 return minNextSpeed(speed, veh);
356}
357
358
359//void
360//MSCFModel_Rail::initVehicleVariables(const MSVehicle *const veh, MSCFModel_Rail::VehicleVariables *pVariables) const {
361//
362// pVariables->setInitialized();
363//
364//}
365
366
367double MSCFModel_Rail::getSpeedAfterMaxDecel(double /* speed */) const {
368
369// //TODO: slope not known here
370// double gr = 0; //trainParams.weight * GRAVITY * edge.grade
371//
372// double a = 0;//trainParams.decl - gr/trainParams.rotWeight;
373//
374// return speed + a * DELTA_T / 1000.;
375 WRITE_ERROR("function call not allowed for rail model. Exiting!");
376 throw ProcessError();
377}
378
379
380double MSCFModel_Rail::finalizeSpeed(MSVehicle* const veh, double vPos) const {
381 return MSCFModel::finalizeSpeed(veh, vPos);
382}
383
384
385double MSCFModel_Rail::freeSpeed(const MSVehicle* const /* veh */, double /* speed */, double dist, double targetSpeed,
386 const bool onInsertion, const CalcReason /*usage*/) const {
387
388// MSCFModel_Rail::VehicleVariables *vars = (MSCFModel_Rail::VehicleVariables *) veh->getCarFollowVariables();
389// if (vars->isNotYetInitialized()) {
390// initVehicleVariables(veh, vars);
391// }
392
393 //TODO: signals, coasting, ...
394
396 // adapt speed to succeeding lane, no reaction time is involved
397 // when breaking for y steps the following distance g is covered
398 // (drive with v in the final step)
399 // g = (y^2 + y) * 0.5 * b + y * v
400 // y = ((((sqrt((b + 2.0*v)*(b + 2.0*v) + 8.0*b*g)) - b)*0.5 - v)/b)
401 const double v = SPEED2DIST(targetSpeed);
402 if (dist < v) {
403 return targetSpeed;
404 }
405 const double b = ACCEL2DIST(myDecel);
406 const double y = MAX2(0.0, ((sqrt((b + 2.0 * v) * (b + 2.0 * v) + 8.0 * b * dist) - b) * 0.5 - v) / b);
407 const double yFull = floor(y);
408 const double exactGap = (yFull * yFull + yFull) * 0.5 * b + yFull * v + (y > yFull ? v : 0.0);
409 const double fullSpeedGain = (yFull + (onInsertion ? 1. : 0.)) * ACCEL2SPEED(myTrainParams.decl);
410 return DIST2SPEED(MAX2(0.0, dist - exactGap) / (yFull + 1)) + fullSpeedGain + targetSpeed;
411 } else {
412 WRITE_ERROR(TL("Anything else than semi implicit euler update is not yet implemented. Exiting!"));
413 throw ProcessError();
414 }
415}
416
417
418double MSCFModel_Rail::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
419 return MIN2(maximumSafeStopSpeed(gap, decel, speed, false, TS, false), maxNextSpeed(speed, veh));
420}
#define DEG2RAD(x)
Definition GeomHelper.h:35
#define GRAVITY
Definition GeomHelper.h:37
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
#define SPEED2DIST(x)
Definition SUMOTime.h:48
#define SIMSTEP
Definition SUMOTime.h:64
#define ACCEL2SPEED(x)
Definition SUMOTime.h:54
#define TS
Definition SUMOTime.h:45
#define DIST2SPEED(x)
Definition SUMOTime.h:50
#define ACCEL2DIST(x)
Definition SUMOTime.h:52
const long long int VTYPEPARS_MAXSPEED_SET
const long long int VTYPEPARS_MASS_SET
const long long int VTYPEPARS_LENGTH_SET
@ SUMO_TAG_CF_RAIL
@ SUMO_TAG_CFM_VARIABLES
@ SUMO_ATTR_RESISTANCE_COEFFICIENT_CONSTANT
@ SUMO_ATTR_ROECKL_OFFSET_SHARP
@ SUMO_ATTR_CURVE_INTEGRATION
@ SUMO_ATTR_RESISTANCE_TABLE
@ SUMO_ATTR_ROECKL_NUMERATOR_SHARP
@ SUMO_ATTR_TRAIN_TYPE
@ SUMO_ATTR_CURVE_RESISTANCE
@ SUMO_ATTR_ROECKL_SHARP_RADIUS
@ SUMO_ATTR_MASSFACTOR
@ SUMO_ATTR_MAXTRACTION
@ SUMO_ATTR_ROECKL_NUMERATOR
@ SUMO_ATTR_MAXPOWER
@ SUMO_ATTR_RESISTANCE_COEFFICIENT_QUADRATIC
@ SUMO_ATTR_DECEL
@ SUMO_ATTR_EMERGENCYDECEL
@ SUMO_ATTR_ROECKL_OFFSET
@ SUMO_ATTR_RESISTANCE_COEFFICIENT_LINEAR
@ SUMO_ATTR_ID
@ SUMO_ATTR_TRACTION_TABLE
@ SUMO_ATTR_STATE
The state of a link.
const double INVALID_DOUBLE
invalid double
Definition StdDefs.h:68
T MIN2(T a, T b)
Definition StdDefs.h:80
T MAX2(T a, T b)
Definition StdDefs.h:86
static double angleDiff(const double angle1, const double angle2)
Returns the difference of the second angle to the first angle in radiants.
static double getInterpolatedValue(const LinearApproxMap &map, double axisValue)
Get interpolated value.
double getOdometer() const
Returns the distance that was already driven by this vehicle.
bool hasDeparted() const
Returns whether this vehicle has already departed.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the vehicle variables from the given description.
double getIntegratedRadius(const MSVehicle *veh, double curveIntegration)
void saveState(OutputDevice &out, const MSCFModel &cfm) const
Saves the vehicle variables.
TrainParams initICE3Params() const
TrainParams initNGT400_16Params() const
virtual ~MSCFModel_Rail()
TrainParams initREDosto7Params() const
virtual double minNextSpeedEmergency(double speed, const MSVehicle *const veh=0) const
Returns the minimum speed after emergency braking, given the current speed (depends on the numerical ...
TrainParams initCustomParams() const
TrainParams initICE1Params() const
MSCFModel_Rail(const MSVehicleType *vtype)
Constructor.
double getCurveResistance(const MSVehicle *veh) const
TrainParams initMireoPlusB2TParams() const
double freeSpeed(const MSVehicle *const veh, double speed, double seen, double maxSpeed, const bool onInsertion, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed without a leader.
TrainParams initRB628Params() const
virtual MSCFModel * duplicate(const MSVehicleType *vtype) const
Duplicates the car-following model.
virtual int getModelID() const
Returns the model's ID; the XML-Tag number is used.
double getSpeedAfterMaxDecel(double v) const
Returns the velocity after maximum deceleration.
TrainParams myTrainParams
virtual double minNextSpeed(double speed, const MSVehicle *const veh) const
Returns the minimum speed given the current speed (depends on the numerical update scheme and its ste...
double getWeight(const MSVehicle *const veh) const
double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's follow speed (no dawdling)
double getRotWeight(const MSVehicle *const veh) const
TrainParams initRB425Params() const
TrainParams initFreightParams() const
virtual double maxNextSpeed(double speed, const MSVehicle *const veh) const
Returns the maximum speed given the current speed.
TrainParams initNGT400Params() const
TrainParams initMireoPlusH2TParams() const
double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
double stopSpeed(const MSVehicle *const veh, const double speed, double gap, double decel, const CalcReason usage=CalcReason::CURRENT) const
Computes the vehicle's safe speed for approaching a non-moving obstacle (no dawdling)
The car-following model abstraction.
Definition MSCFModel.h:59
virtual void setEmergencyDecel(double decel)
Sets a new value for maximal physically possible deceleration [m/s^2].
Definition MSCFModel.h:588
virtual double finalizeSpeed(MSVehicle *const veh, double vPos) const
Applies interaction with stops and lane changing model influences. Called at most once per simulation...
virtual void setMaxDecel(double decel)
Sets a new value for maximal comfortable deceleration [m/s^2].
Definition MSCFModel.h:580
CalcReason
What the return value of stop/follow/free-Speed is used for.
Definition MSCFModel.h:95
double myDecel
The vehicle's maximum deceleration [m/s^2].
Definition MSCFModel.h:762
double maximumSafeStopSpeed(double gap, double decel, double currentSpeed, bool onInsertion=false, double headway=-1, bool relaxEmergency=true) const
Returns the maximum next velocity for stopping within gap.
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
double getCurveRadius() const
Returns the vehicle's current curve radius in m.
double getSlope() const
Returns the slope of the road at vehicle's position in degrees.
double getAngle() const
Returns the vehicle's direction in radians.
Definition MSVehicle.h:735
MSCFModel::VehicleVariables * getCarFollowVariables() const
Returns the vehicle's car following model variables.
Definition MSVehicle.h:994
The car-following model and parameter.
double getMaxSpeed() const
Get vehicle's (technical) maximum speed [m/s].
const std::string & getID() const
Returns the name of the vehicle type.
double getMinGap() const
Get the free space in front of vehicles of this class.
bool wasSet(long long int what) const
Returns whether the given parameter was set.
double getLength() const
Get vehicle's length [m].
double getMass() const
Get this vehicle type's mass.
const SUMOVTypeParameter & getParameter() const
const std::string & getID() const
Returns the id.
Definition Named.h:73
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
LinearApproxHelpers::LinearApproxMap getCFProfile(const SumoXMLAttr attr, const LinearApproxHelpers::LinearApproxMap &defaultProfile) const
Returns the named value from the map, or the default if it is not contained there.
double getCFParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
std::string getCFParamString(const SumoXMLAttr attr, const std::string defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
LinearApproxHelpers::LinearApproxMap traction
LinearApproxHelpers::LinearApproxMap resistance
double getTraction(double speed) const
double getResistance(double speed) const