AnyLogic
Expand
Font size

Train

A train is a sequence of one or several rail cars coupled with each other that can move in the rail yard and is controlled by Rail Library and Process Modeling Library flowchart blocks. Trains, and not the rail cars are the main agents you are dealing with when creating the rail models. If a rail car is a part of a train, it is fully controlled by the train, so it will not accept most of low-level commands such as, for example, setSpeed() or couple().

Trains are created by TrainSource blocks and must be disposed by TrainDispose blocks. A train can be split into two trains by TrainDecouple and two trains can be combined into one by TrainCouple. Trains move in the rail yard under control of TrainMoveTo blocks. Trains can go through any Process Modeling Library blocks like Delay, Seize, Release, etc.

The cars in the train are ordered and there is always the first car and the last car (which are the same in case the train contains only one car). The orientation of cars at the time of the train creation is the same as of the train, but later on it can change as a result of coupling/decoupling. The length of the train (the length of the track portion occupied by the train) is equal to the sum of all car lengths.

The Rail Library does not have a notion of locomotive, or of any other car type: all rail cars are considered to be equal. Any train can move at any speed, or be coupled/decoupled at any side.

The train has cruise speed, acceleration, and deceleration properties. They are initially set up by TrainSource but can later on be changed via the train API. While the train is moving you can change its speed instantly or by applying acceleration and deceleration.

Train 3D animation

To create a custom train type

  1. Open the Rail Library palette and drag the Train Type element onto the agent type diagram, e.g. Main.

  2. The New agent wizard dialog window will open. Specify the Agent type name. Click Next to proceed to the next step.

  3. Here you can define the parameters of the train type. Click < add new...> and specify the parameter name, type, and default value. To remove a parameter from the list, click the button . You can always add more parameters onto the train diagram after you finish creating it.

  4. Click Finish. AnyLogic will automatically open new train type diagram with the defined parameters.

  5. Now you can use the new train type in the Rail Library blocks parameters, for instance, in TrainSource properties:

Train type properties

General

Name — The name of the train type.

Since AnyLogic generates Java class for each train type, you should follow Java naming guidelines and start the name with an uppercase letter.

Ignore — If selected, the train type is excluded from the model.

Parameters preview

Parameters preview properties are available only when this train type has any parameters. Here you can rearrange parameters of this train. Trains of this type will list parameters in their property view in this well-defined order.

Agent actions

On startup — Code to be executed after all agents throughout the whole model are constructed, connected, and initialized, and before anything else is done. This is a place for some additional initialization of the train. The order of execution of On startup code of different agents is not guaranteed. We recommend to perform train initialization not here, but in the On exit action property of the TrainSource block that generates trains in your model.

On destroy — Code to be executed on destroying train of this type. This parameter is derived from agent type and is rarely used for trains. For details refer to the Agent article.

On arrival to target destination — This parameter is derived from agent type and is not applied to the movement defined with a Rail Library flowchart.

On before step — This parameter is derived from agent type and is rarely used for trains. For details refer to the Agent article.

On step — This parameter is derived from agent type and is rarely used for trains. For details refer to the Agent article.

Agent in flowcharts

Use in flowcharts as — Here you choose the role for agents of this type in the flowcharts. To enable using these agents as trains in the blocks of the Rail Library, leave the Train option selected. This way the agent of this type obtains train-specific functions: getCruiseSpeed(), getOrientation(), getTargetTrack(), etc.

On enter flowchart block — Here you can write the code to be executed when this train enters the flowchart.

On exit flowchart block — Here you can write the code to be executed when this train exits the flowchart.

On seize resource — Here you can write the code to be executed when this train seizes the resource while being in the flowchart.

On release resource — Here you can write the code to be executed when this train releases the resource while being in the flowchart.

Dimensions and movement

The parameters in this section are derived from agent type and are rarely used for trains. For details refer to the Agent article. Define the initial speed, cruise speed, acceleration, deceleration, and length of the train's rail cars in the Train and cars properties section of the TrainSource block that generates trains in your model.

Space and network

The parameters in this section are derived from agent type and are rarely used for trains. For details refer to the Agent article.

Advanced Java

The parameters in this section are derived from agent type and are rarely used for trains. For details refer to the Agent article.

Advanced

The parameters in this section are derived from agent type and are rarely used for trains. For details refer to the Agent article.

Functions

Rail cars
Function Description
int size() Returns the number of cars in the train.
double getLength() Returns the length of the train (the sum of lengths of all cars) in meters.
double getLength(LengthUnits units) Returns the length of the train (the sum of lengths of all cars) in specified length units.

units — a constant defining the length units
boolean isEmpty() Tests if the train is empty, i.e. contains no cars. Returns true if empty, false otherwise.
Agent getFirst() Returns the first car in the train, or null if the train is empty.
Agent getLast() Returns the last car in the train, or null if the train is empty.
Agent getHeadCar() Returns the car that is at the head of a moving train (either first or last), null if the train is not moving.
Agent getTailCar() Returns the car that is at the tail of the moving train (either first or last), null if the train is not moving.
Agent getCar(int i) Returns a car with a given index. the first car index is 0, the last car — size()-1.

i — the index
Speed
Function Description
double getSpeed() Returns the speed of the train in meters/second. If the train is empty, returns 0.
double getSpeed(SpeedUnits units) Returns the speed of the train in specified speed units. If the train is empty, returns 0.

units — a constant defining the speed units
double getCruiseSpeed() Returns the cruise speed of the train in meters/second.
double getCruiseSpeed(SpeedUnits units) Returns the cruise speed of the train in specified speed units.

units — a constant defining the speed units
void setSpeed(double v) Sets the new speed of the train. It applies immediately even if the train is moving. If the train is not moving, just remembers the speed but does not start the train. While the train is moving, speed cannot be set to 0. Discards any acceleration/deceleration with the exception of the final deceleration before arrival to target and the deceleration that occurs when the pauseTrain() function is called.

v — the new speed in meters / second
void setSpeed(double v, SpeedUnits units) Sets the new speed of the train in the specified speed units. It applies immediately even if the train is moving. If the train is not moving, just remembers the speed but does not start the train. While the train is moving, speed cannot be set to 0. Discards any acceleration/deceleration with the exception of the final deceleration before arrival to target and the deceleration that occurs when the pauseTrain() function is called.

v — the new speed value in the specified units
units — a constant defining the speed units
void setCruiseSpeed(double speed) Sets the cruise speed of the train (must be >0) in meters per second.

speed — the new cruise speed of the train in meters/second
void setCruiseSpeed(double speed, SpeedUnits units) Sets the new cruise speed of the train (must be >0) in specified speed units.

speed — the new cruise speed of the train in specified speed units
units — a constant defining the speed units
Acceleration and deceleration
Function Description
double getAcceleration() Returns the acceleration of the train in meters / sec2.
double getAcceleration(AccelerationUnits units) Returns the acceleration of the train in specified acceleration units.

units — a constant defining the acceleration units
double getDeceleration() Returns the deceleration of the train in meters / sec2.
double getDeceleration(AccelerationUnits units) Returns the deceleration of the train in specified acceleration units.

units — a constant defining the acceleration units
void setAcceleration(double acceleration) Sets the acceleration of the train in meters / sec2 (must be >).

acceleration — the new acceleration of the train in meters / sec2
void setAcceleration(double acceleration, AccelerationUnits units) Sets the acceleration of the train in specified units (must be >0).

acceleration — the new acceleration of the train in specified acceleration units
units — a constant defining the acceleration units
void setDeceleration(double deceleration) Sets the deceleration of the train in meters / sec2 (must be >0).

deceleration — the new deceleration of the train in meters / sec2
void setDeceleration(double deceleration, AccelerationUnits units) Sets the deceleration of the train in specified units (must be >0).

deceleration — the new deceleration of the train in specified units
units — a constant defining the acceleration units
void accelerateTo(double speed) Accelerates or decelerates the train to achieve a given speed. Can only be called while the train is moving. Does not change the cruise speed. Uses the current settings for acceleration/deceleration. Deceleration to 0 (stop) cannot be done as stop is controlled by TrainMoveTo block (use decelerate before finish option). Discards any acceleration/deceleration with the exception of the final deceleration before arrival to target.

speed — the new desired speed in meters / second
void accelerateTo(double speed, SpeedUnits units) Similar to accelerateTo(double speed). The given speed is specified not in meters per second, but in specified speed units.

speed — the new desired speed in specified speed units
units — a constant defining the speed units
Movement control
Function Description
boolean isMoving() Tests if the train is currently moving. An empty train is considered as not moving. Returns true if moving, false if not.
void pauseTrain() Causes the deceleration of the train (according to the specified deceleration settings) until its movement stops altogether. The train remains in the TrainMoveTo block.
void resumeTrain() Causes the acceleration of the stopped train (according to the specified acceleration settings) until it reaches its cruise speed.
boolean getDirection() Tests if the first car of the train is moving at the head of the train. Returns true if first car moves at the head of the train, otherwise false.
boolean getOrientation(boolean front) Tests if the train has same orientation as the track where its given side is located. Returns true for the same orientation, false for opposite.

front — if true, the location of front of the train (open side of the first car), otherwise — of the back
boolean isForwardOnTrack(boolean front) Tests if the train is moving towards the end of the track on which its given side is located.

front — if true, the front of the train (open side of the first car), otherwise — the back
Route getRoute() Returns the route the train is currently following, if any. The route only exists while the train is handled by the TrainMoveTo block.
List<RailwayTrack> getAllOccupiedTracks() Returns all occupied railway tracks.
RailwayTrack getTrack(boolean front) Returns the railway track where a given side of the train is currently located.

front — if true, the location of front of the train (open side of the first car), otherwise — of the back
RailwayTrack getTargetTrack() Returns the target track of a moving train, null if target is not set.
double getOffset(boolean front) Returns the offset of a given side of the train relative to the track start point in meters. 0 is the beginning of the track.

front — if true, this is front side, otherwise — rear side
double getOffset(boolean front, LengthUnits units) Returns the offset of a given side of the train relative to the track start point in specified length units. 0 is the beginning of the track.

front — if true, this is front side, otherwise — rear side
units — a constant defining the length units
double getTargetOffset() Returns the target offset (on the target track) of a moving train in meters, assumes the target is set.
double getTargetOffset(LengthUnits units) Returns the target offset (on the target track) of a moving train in specified length units, assumes the target is set.

units — a constant defining the length units
double getDistanceToTarget() Returns distance from current position to target point in meters. Assumes the train is moving along a route (specified manually or calculated automatically) and the target is set.
double getDistanceToTarget(LengthUnits units) Returns distance from current position to target point in specified length units. Assumes the train is moving along a route (specified manually or calculated automatically) and the target is set.

units — a constant defining the length units
double distanceToTrain(Agent train, boolean targetSide) Returns the distance (in meters) in the network from the head of the train to the specified side of the target train.

train — target train
targetSide — target side of the train. If true, this is front side, otherwise — rearside
double distanceToTrain(Agent train, boolean targetSide, LengthUnits units) Returns the distance (in the specified units) in the network from the head of the train to the specified side of the target train.

train — target train
targetSide — target side of the train. If true, this is front side, otherwise — rearside
units — a constant defining the length units
double getDistanceDriven() Returns the distance driven by the train in meters since its creation or since the last call of resetDistanceDriven().
void resetDistanceDriven() Resets the distance driven by the train to zero.
Miscellaneous
Function Description
RailSettings getSettings() Returns the rail settings.
RailwayNetwork getRailYard() Returns the rail yard where the train is currently in. If the train is empty returns null.
void highlight(boolean yes) Highlights or de-highlights the train.

yes — true for highlighted status, false for regular
How can we improve this article?