Line data Source code
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 : /****************************************************************************/
14 : /// @file MSCFModel_Rail.cpp
15 : /// @author Gregor Laemmel
16 : /// @author Leander Flamm
17 : /// @date Tue, 08 Feb 2017
18 : ///
19 : // <description missing>
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include <iostream>
24 : #include <utils/common/MsgHandler.h>
25 : #include <utils/common/StringUtils.h>
26 : #include <utils/common/StringTokenizer.h>
27 : #include <utils/geom/GeomHelper.h>
28 : #include <utils/xml/SUMOSAXAttributes.h>
29 : #include <microsim/MSVehicle.h>
30 : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
31 : #include "MSCFModel_Rail.h"
32 :
33 : // ===========================================================================
34 : // trainParams method definitions
35 : // ===========================================================================
36 :
37 : double
38 1967665 : MSCFModel_Rail::TrainParams::getResistance(double speed) const {
39 1967665 : if (resCoef_constant != INVALID_DOUBLE) {
40 16857 : return (resCoef_quadratic * speed * speed + resCoef_linear * speed + resCoef_constant); // kN
41 : } else {
42 1950808 : return LinearApproxHelpers::getInterpolatedValue(resistance, speed); // kN
43 : }
44 : }
45 :
46 :
47 : double
48 1254734 : MSCFModel_Rail::TrainParams::getTraction(double speed) const {
49 1254734 : if (maxPower != INVALID_DOUBLE) {
50 5395 : return MIN2(maxPower / speed, maxTraction); // kN
51 : } else {
52 1249339 : return LinearApproxHelpers::getInterpolatedValue(traction, speed); // kN
53 : }
54 : }
55 :
56 :
57 : // ===========================================================================
58 : // RailVehicleVariables method definitions
59 : // ===========================================================================
60 : void
61 10 : MSCFModel_Rail::RailVehicleVariables::saveState(OutputDevice& out, const MSCFModel& /*cfm*/) const {
62 10 : out.openTag(SUMO_TAG_CFM_VARIABLES);
63 10 : out.writeAttr(SUMO_ATTR_ID, "Rail");
64 10 : std::ostringstream internals;
65 10 : internals << odometerAngles.size() << " ";
66 28 : for (auto item : odometerAngles) {
67 18 : internals << item.first << " " << item.second << " ";
68 : }
69 10 : out.writeAttr(SUMO_ATTR_STATE, internals.str());
70 10 : out.closeTag();
71 10 : }
72 :
73 :
74 : void
75 10 : MSCFModel_Rail::RailVehicleVariables::loadState(const SUMOSAXAttributes& attrs) {
76 10 : bool ok = true;
77 10 : const std::string cfmID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
78 10 : if (cfmID != "Rail") {
79 0 : throw ProcessError(TLF("incompatible carFollowModel '%' when loading state for Rail", cfmID));
80 : }
81 10 : std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
82 : int odometerAnglesSize;
83 10 : bis >> odometerAnglesSize;
84 28 : for (int i = 0; i < odometerAnglesSize; i++) {
85 : double o;
86 : double a;
87 : bis >> o;
88 : bis >> a;
89 18 : odometerAngles.push_back(std::make_pair(o, a));
90 : }
91 20 : }
92 :
93 :
94 :
95 : double
96 57353 : MSCFModel_Rail::RailVehicleVariables::getIntegratedRadius(const MSVehicle* veh, double curveIntegration) {
97 57353 : const double odo = veh->getOdometer();
98 : // add new data point
99 57353 : if ((odometerAngles.empty() || odometerAngles.back().first != odo) && veh->hasDeparted()) {
100 4853 : odometerAngles.push_back(std::make_pair(odo, veh->getAngle()));
101 : // clean up old data points beyond integration distance
102 9345 : while (odometerAngles.size() > 2) {
103 9279 : double distCleaned = odometerAngles.back().first - odometerAngles[1].first;
104 9279 : if (distCleaned >= curveIntegration) {
105 : odometerAngles.erase(odometerAngles.begin());
106 : } else {
107 : break;
108 : }
109 : }
110 : }
111 57353 : if (odometerAngles.size() > 1) {
112 56827 : const double dist = odometerAngles.back().first - odometerAngles.front().first;
113 56827 : const double angleDiff = GeomHelper::angleDiff(odometerAngles.back().second, odometerAngles.front().second);
114 : return angleDiff == 0
115 56827 : ? std::numeric_limits<double>::max()
116 46367 : : dist / fabs(angleDiff);
117 : } else {
118 526 : return veh->getCurveRadius();
119 : }
120 : }
121 :
122 :
123 :
124 : // ===========================================================================
125 : // method definitions
126 : // ===========================================================================
127 :
128 :
129 297 : MSCFModel_Rail::MSCFModel_Rail(const MSVehicleType* vtype) :
130 297 : MSCFModel(vtype) {
131 297 : const std::string trainType = vtype->getParameter().getCFParamString(SUMO_ATTR_TRAIN_TYPE, "NGT400");
132 297 : if (trainType.compare("RB425") == 0) {
133 40 : myTrainParams = initRB425Params();
134 257 : } else if (trainType.compare("RB628") == 0) {
135 12 : myTrainParams = initRB628Params();
136 245 : } else if (trainType.compare("NGT400") == 0) {
137 87 : myTrainParams = initNGT400Params();
138 158 : } else if (trainType.compare("NGT400_16") == 0) {
139 3 : myTrainParams = initNGT400_16Params();
140 155 : } else if (trainType.compare("ICE1") == 0) {
141 6 : myTrainParams = initICE1Params();
142 149 : } else if (trainType.compare("REDosto7") == 0) {
143 74 : myTrainParams = initREDosto7Params();
144 75 : } else if (trainType.compare("Freight") == 0) {
145 56 : myTrainParams = initFreightParams();
146 19 : } else if (trainType.compare("ICE3") == 0) {
147 4 : myTrainParams = initICE3Params();
148 15 : } else if (trainType.compare("MireoPlusB") == 0) {
149 3 : myTrainParams = initMireoPlusB2TParams();
150 12 : } else if (trainType.compare("MireoPlusH") == 0) {
151 3 : myTrainParams = initMireoPlusH2TParams();
152 9 : } else if (trainType.compare("custom") == 0) {
153 9 : myTrainParams = initCustomParams();
154 : } else {
155 0 : WRITE_ERRORF(TL("Unknown train type: %. Exiting!"), trainType);
156 0 : throw ProcessError();
157 : }
158 : // override with user values
159 297 : if (vtype->wasSet(VTYPEPARS_MAXSPEED_SET)) {
160 : myTrainParams.vmax = vtype->getMaxSpeed();
161 : }
162 297 : if (vtype->wasSet(VTYPEPARS_LENGTH_SET)) {
163 54 : myTrainParams.length = vtype->getLength();
164 : }
165 297 : myTrainParams.mf = vtype->getParameter().getCFParam(SUMO_ATTR_MASSFACTOR, myTrainParams.mf);
166 297 : myTrainParams.decl = vtype->getParameter().getCFParam(SUMO_ATTR_DECEL, myTrainParams.decl);
167 : setMaxDecel(myTrainParams.decl);
168 297 : setEmergencyDecel(vtype->getParameter().getCFParam(SUMO_ATTR_EMERGENCYDECEL, myTrainParams.decl + 0.3));
169 : // update type parameters so they are shown correctly in the gui (if defaults from trainType are used)
170 297 : const_cast<MSVehicleType*>(vtype)->setMaxSpeed(myTrainParams.vmax);
171 297 : const_cast<MSVehicleType*>(vtype)->setLength(myTrainParams.length);
172 297 : if (!vtype->wasSet(VTYPEPARS_MASS_SET)) {
173 : // tons to kg
174 273 : const_cast<MSVehicleType*>(vtype)->setMass(myTrainParams.weight * 1000);
175 : }
176 :
177 : // init tabular curves
178 297 : myTrainParams.traction = vtype->getParameter().getCFProfile(SUMO_ATTR_TRACTION_TABLE, myTrainParams.traction);
179 297 : myTrainParams.resistance = vtype->getParameter().getCFProfile(SUMO_ATTR_RESISTANCE_TABLE, myTrainParams.resistance);
180 :
181 : // init parametric curves
182 297 : myTrainParams.maxPower = vtype->getParameter().getCFParam(SUMO_ATTR_MAXPOWER, INVALID_DOUBLE);
183 297 : myTrainParams.maxTraction = vtype->getParameter().getCFParam(SUMO_ATTR_MAXTRACTION, INVALID_DOUBLE);
184 297 : myTrainParams.resCoef_constant = vtype->getParameter().getCFParam(SUMO_ATTR_RESISTANCE_COEFFICIENT_CONSTANT, INVALID_DOUBLE);
185 297 : myTrainParams.resCoef_linear = vtype->getParameter().getCFParam(SUMO_ATTR_RESISTANCE_COEFFICIENT_LINEAR, INVALID_DOUBLE);
186 297 : myTrainParams.resCoef_quadratic = vtype->getParameter().getCFParam(SUMO_ATTR_RESISTANCE_COEFFICIENT_QUADRATIC, INVALID_DOUBLE);
187 : // curve resistance parameters
188 297 : myTrainParams.curveResistance = vtype->getParameter().getCFParam(SUMO_ATTR_CURVE_RESISTANCE, myTrainParams.curveResistance);
189 297 : myTrainParams.curveIntegration = vtype->getParameter().getCFParam(SUMO_ATTR_CURVE_INTEGRATION, myTrainParams.curveIntegration);
190 297 : myTrainParams.roeckl_sharp_radius = vtype->getParameter().getCFParam(SUMO_ATTR_ROECKL_SHARP_RADIUS, myTrainParams.roeckl_sharp_radius);
191 297 : myTrainParams.roeckl_numerator = vtype->getParameter().getCFParam(SUMO_ATTR_ROECKL_NUMERATOR, myTrainParams.roeckl_numerator);
192 297 : myTrainParams.roeckl_numerator_sharp = vtype->getParameter().getCFParam(SUMO_ATTR_ROECKL_NUMERATOR_SHARP, myTrainParams.roeckl_numerator_sharp);
193 297 : myTrainParams.roeckl_offset = vtype->getParameter().getCFParam(SUMO_ATTR_ROECKL_OFFSET, myTrainParams.roeckl_offset);
194 297 : myTrainParams.roeckl_offset_sharp = vtype->getParameter().getCFParam(SUMO_ATTR_ROECKL_OFFSET_SHARP, myTrainParams.roeckl_offset_sharp);
195 :
196 297 : if (myTrainParams.maxPower != INVALID_DOUBLE && myTrainParams.maxTraction == INVALID_DOUBLE) {
197 0 : throw ProcessError(TLF("Undefined maxPower for vType '%'.", vtype->getID()));
198 297 : } else if (myTrainParams.maxPower == INVALID_DOUBLE && myTrainParams.maxTraction != INVALID_DOUBLE) {
199 0 : throw ProcessError(TLF("Undefined maxTraction for vType '%'.", vtype->getID()));
200 : }
201 306 : if (myTrainParams.maxPower != INVALID_DOUBLE && vtype->getParameter().getCFParamString(SUMO_ATTR_TRACTION_TABLE, "") != "") {
202 0 : WRITE_WARNING(TLF("Ignoring tractionTable because maxPower and maxTraction are set for vType '%'.", vtype->getID()));
203 : }
204 297 : const bool hasSomeResCoef = (myTrainParams.resCoef_constant != INVALID_DOUBLE
205 294 : || myTrainParams.resCoef_linear != INVALID_DOUBLE
206 591 : || myTrainParams.resCoef_quadratic != INVALID_DOUBLE);
207 : const bool hasAllResCoef = (myTrainParams.resCoef_constant != INVALID_DOUBLE
208 3 : && myTrainParams.resCoef_linear != INVALID_DOUBLE
209 300 : && myTrainParams.resCoef_quadratic != INVALID_DOUBLE);
210 297 : if (hasSomeResCoef && !hasAllResCoef) {
211 0 : throw ProcessError(TLF("Some undefined resistance coefficients for vType '%' (requires resCoef_constant, resCoef_linear and resCoef_quadratic)", vtype->getID()));
212 : }
213 306 : if (myTrainParams.resCoef_constant != INVALID_DOUBLE && vtype->getParameter().getCFParamString(SUMO_ATTR_RESISTANCE_TABLE, "") != "") {
214 3 : WRITE_WARNING(TLF("Ignoring resistanceTable because resistance coefficients are set for vType '%'.", vtype->getID()));
215 : }
216 :
217 297 : if (myTrainParams.traction.empty() && myTrainParams.maxPower == INVALID_DOUBLE) {
218 9 : throw ProcessError(TLF("Either tractionTable or maxPower must be defined for vType '%' with Rail model type '%'.", vtype->getID(), trainType));
219 : }
220 294 : if (myTrainParams.resistance.empty() && myTrainParams.resCoef_constant == INVALID_DOUBLE) {
221 0 : throw ProcessError(TLF("Either resistanceTable or resCoef_constant must be defined for vType '%' with Rail model type '%'.", vtype->getID(), trainType));
222 : }
223 300 : }
224 :
225 :
226 586 : MSCFModel_Rail::~MSCFModel_Rail() { }
227 :
228 :
229 109766 : double 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 109766 : if (speed >= 30 / 3.6) {
238 : // safety distance for higher speeds (>= 30 km/h)
239 73736 : gap = MAX2(0.0, gap + veh->getVehicleType().getMinGap() - 50);
240 : }
241 :
242 109766 : const double vsafe = maximumSafeStopSpeed(gap, myDecel, speed, false, TS, false); // absolute breaking distance
243 109766 : const double vmin = minNextSpeed(speed, veh);
244 109766 : const double vmax = maxNextSpeed(speed, veh);
245 :
246 109766 : if (MSGlobals::gSemiImplicitEulerUpdate) {
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 :
256 : int
257 0 : MSCFModel_Rail::getModelID() const {
258 0 : return SUMO_TAG_CF_RAIL;
259 : }
260 :
261 :
262 : MSCFModel*
263 8 : MSCFModel_Rail::duplicate(const MSVehicleType* vtype) const {
264 8 : return new MSCFModel_Rail(vtype);
265 : }
266 :
267 : double
268 1967665 : MSCFModel_Rail::getRotWeight(const MSVehicle* const veh) const {
269 1967665 : return getWeight(veh) * myTrainParams.mf;
270 : }
271 :
272 : double
273 5902995 : MSCFModel_Rail::getWeight(const MSVehicle* const veh) const {
274 : // kg to tons
275 5902995 : return veh->getVehicleType().getMass() / 1000;
276 : }
277 :
278 : double
279 1967665 : MSCFModel_Rail::getCurveResistance(const MSVehicle* veh) const {
280 1967665 : if (myTrainParams.curveResistance > 0) {
281 : RailVehicleVariables* vars = (RailVehicleVariables*)veh->getCarFollowVariables();
282 : assert(vars != nullptr);
283 57353 : const double r = vars->getIntegratedRadius(veh, myTrainParams.curveIntegration);
284 57353 : if (r == std::numeric_limits<double>::max()) {
285 : return 0;
286 46367 : } else if (r >= myTrainParams.roeckl_sharp_radius) {
287 34114 : return 0.001 * myTrainParams.curveResistance * myTrainParams.roeckl_numerator / (r - myTrainParams.roeckl_offset);
288 12253 : } else if (r > myTrainParams.roeckl_offset_sharp) {
289 10589 : return 0.001 * myTrainParams.curveResistance * myTrainParams.roeckl_numerator_sharp / (r - myTrainParams.roeckl_offset_sharp);
290 : } else {
291 4992 : WRITE_WARNINGF("Cannot compute curve resistance for vehicle '%' with radius % at time %",
292 : veh->getID(), r, time2string(SIMSTEP));
293 1664 : return 0;
294 : }
295 : }
296 : return 0;
297 : }
298 :
299 :
300 1547911 : double MSCFModel_Rail::maxNextSpeed(double speed, const MSVehicle* const veh) const {
301 :
302 1547911 : if (speed >= myTrainParams.vmax) {
303 : return myTrainParams.vmax;
304 : }
305 :
306 : double targetSpeed = myTrainParams.vmax;
307 :
308 1254734 : double res = myTrainParams.getResistance(speed); // kN
309 :
310 1254734 : double slope = veh->getSlope();
311 1254734 : double gr = getWeight(veh) * GRAVITY * sin(DEG2RAD(slope)); //kN
312 1254734 : double cr = getWeight(veh) * getCurveResistance(veh); //kN
313 :
314 1254734 : double totalRes = res + gr + cr; //kN
315 :
316 1254734 : double trac = myTrainParams.getTraction(speed); // kN
317 : double a;
318 1254734 : if (speed < targetSpeed) {
319 1254734 : a = (trac - totalRes) / getRotWeight(veh); //kN/t == N/kg
320 : } else {
321 : a = 0.;
322 0 : if (totalRes > trac) {
323 0 : a = (trac - totalRes) / getRotWeight(veh); //kN/t == N/kg
324 : }
325 : }
326 1254734 : double maxNextSpeed = speed + ACCEL2SPEED(a);
327 :
328 : // std::cout << veh->getID() << " speed: " << (speed*3.6) << std::endl;
329 :
330 1254734 : return MIN2(myTrainParams.vmax, maxNextSpeed);
331 : }
332 :
333 :
334 712931 : double MSCFModel_Rail::minNextSpeed(double speed, const MSVehicle* const veh) const {
335 :
336 712931 : const double slope = veh->getSlope();
337 712931 : const double gr = getWeight(veh) * GRAVITY * sin(DEG2RAD(slope)); //kN
338 712931 : const double cr = getWeight(veh) * getCurveResistance(veh);
339 712931 : const double res = myTrainParams.getResistance(speed); // kN
340 712931 : const double totalRes = res + gr + cr; //kN
341 712931 : const double a = myTrainParams.decl + totalRes / getRotWeight(veh);
342 712931 : const double vMin = speed - ACCEL2SPEED(a);
343 712931 : if (MSGlobals::gSemiImplicitEulerUpdate) {
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 :
353 : double
354 199857 : MSCFModel_Rail::minNextSpeedEmergency(double speed, const MSVehicle* const veh) const {
355 199857 : 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 :
367 0 : double 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 0 : WRITE_ERROR("function call not allowed for rail model. Exiting!");
376 0 : throw ProcessError();
377 : }
378 :
379 :
380 199857 : double MSCFModel_Rail::finalizeSpeed(MSVehicle* const veh, double vPos) const {
381 199857 : return MSCFModel::finalizeSpeed(veh, vPos);
382 : }
383 :
384 :
385 763722 : double 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 :
395 763722 : if (MSGlobals::gSemiImplicitEulerUpdate) {
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 763721 : const double v = SPEED2DIST(targetSpeed);
402 763721 : if (dist < v) {
403 : return targetSpeed;
404 : }
405 693516 : const double b = ACCEL2DIST(myDecel);
406 693516 : 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 693516 : const double yFull = floor(y);
408 693516 : const double exactGap = (yFull * yFull + yFull) * 0.5 * b + yFull * v + (y > yFull ? v : 0.0);
409 693516 : const double fullSpeedGain = (yFull + (onInsertion ? 1. : 0.)) * ACCEL2SPEED(myTrainParams.decl);
410 942869 : return DIST2SPEED(MAX2(0.0, dist - exactGap) / (yFull + 1)) + fullSpeedGain + targetSpeed;
411 : } else {
412 1 : WRITE_ERROR(TL("Anything else than semi implicit euler update is not yet implemented. Exiting!"));
413 1 : throw ProcessError();
414 : }
415 : }
416 :
417 :
418 797703 : double MSCFModel_Rail::stopSpeed(const MSVehicle* const veh, const double speed, double gap, double decel, const CalcReason /*usage*/) const {
419 797703 : return MIN2(maximumSafeStopSpeed(gap, decel, speed, false, TS, false), maxNextSpeed(speed, veh));
420 : }
|