AnyLogic 9
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 the cars at the time of the train creation is the same as that of the train, but it can change later as a result of coupling or decoupling. The length of the train (the length of the section of track occupied by the train) is equal to the sum of all car lengths.

The Rail Library has no notion of locomotive, or of any other car type: all rail cars are considered to be equal. Each train can move at any speed and can be coupled and decoupled at any side.

The animation of a train is actually the animation of the rail cars of the train. There are no specific visuals associated with the train.

The train has cruising speed, acceleration, and deceleration properties. These are initially set by TrainSource, but you can change them later using 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. Drag and drop the Train Source flowchart block onto the agent type diagram, for example, Main.

    AnyLogic 9: The Train Source block on the agent diagram

  2. In the block properties, in the Train and cars section, locate the New train property. Click in the edit box to open the drop-down list:

    AnyLogic 9: The New train property of the Train Source block

  3. Click Create new agent type.
  4. In the Name edit box, specify the name of the train. By default, Train is used.
    Train is an agent type, so its name should start with an uppercase letter.

    AnyLogic 9: The new agent wizard block

  5. Customize the animation of the train: by default, the Locomotive 14.1m shape is selected. Click on its name to see other options, both in the 2D and 3D palettes. Select the shape you want.
  6. Click Add parameters to specify additional parameters for the train type. On the page that appears, click Add and specify the name, type, and default value of the parameter. Repeat this for each parameter you need.
    To remove a parameter from the list, move the mouse pointer over it, then click the AnyLogic 9: The Delete icon Delete icon that appears on the right.

    AnyLogic 9: Customizing the train’s parameters

    You can add more parameters to the train diagram after you have finished creating it.
  7. Click Finish. AnyLogic will create and open the new train type diagram with the defined parameters.
  8. Now you can use the new train type in the Rail Library block parameters. It is automatically specified in the properties of the Train Source block you used to create the train.

    AnyLogic 9: The train specified in the Train Source 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

The Parameters preview properties are available only when this train type has any parameters. Here you can rearrange the 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.

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.

On step — This parameter is derived from agent type and is rarely used for trains.

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(), and so on.

On enter flowchart block — The code to be executed when this train enters the flowchart.

On exit flowchart block — The code to be executed when this train exits the flowchart.

On seize resource — The code to be executed when this train seizes the resource while being in the flowchart.

On release resource — 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.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.

Advanced Java

The parameters in this section are derived from agent type and are rarely used for trains.

Expert

The parameters in this section are derived from agent type and are rarely used for trains.

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, that is, 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 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 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 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 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?