app Package

app Package

Main package of the software. It contains the Program class which is the core application controller.

class ActionMap[source]

Bases: object

add(key, value, repeat=False)[source]
call(key)[source]
get(key, default=None)[source]
get_function(key)[source]
has_key(key)[source]
is_repeat(key)[source]
class Program[source]

Bases: object

Main class of ARS.

To run a custom simulation, create a subclass. It must contain an implementation of the ‘create_sim_objects’ method which will be called during the simulation creation.

To use it, only two statements are necessary:

  • create an object of this class
    >>> sim_program = ProgramSubclass()
    
  • call its ‘start’ method
    >>> sim_program.start()
    

Constructor. Defines some attributes and calls some initialization methods to:

  • set the basic mapping of key to action,
  • create the visualization window according to class constants,
  • create the simulation.
BACKGROUND_COLOR = (1, 1, 1)
CAMERA_POSITION = (10, 8, 10)
FLOOR_BOX_SIZE = (10, 0.01, 10)
FPS = 50
STEPS_PER_FRAME = 50
WINDOW_POSITION = (0, 0)
WINDOW_SIZE = (1024, 768)
WINDOW_TITLE = 'ARS simulation'
WINDOW_ZOOM = 1.0
create_screenshot_recorder(base_filename, periodically=False)[source]

Create a screenshot (of the frames displayed in the graphics window) recorder.

Each image will be written to a numbered file according to base_filename. By default it will create an image each time record_frame() is called. If periodically is True then screenshots will be saved in sequence. The time period between each frame is determined according to FPS.

create_sim_objects()[source]

This method must be overriden (at least once in the inheritance tree) by the subclass that will instatiated to run the simulator.

It shall contain statements calling its ‘sim’ attribute’s methods for adding objects (e.g. add_sphere).

For example:

>>> self.sim.add_sphere(0.5, (1,10,1), density=1)
create_simulation(add_axes=True, add_floor=True)[source]

Creates an empty simulation and:

  1. adds basic simulation objects (add_basic_simulation_objects()),

  2. (if add_axes is True) adds axes to the visualization at the

    coordinates-system origin,

  3. (if add_floor is True) adds a floor with a defined normal

    vector and some visualization parameters,

  4. calls create_sim_objects() (which must be implemented by

    subclasses),

  5. gets the actors representing the simulation objects and adds them to

    the graphics adapter.

finalize()[source]

Finalize the program, deleting or releasing all associated resources.

Currently, the following is done:

A finalized program file cannot be used for further simulations.

Note

This method may be called more than once without error.

on_action_selection(key)[source]

Method called after an actions is selected by pressing a key.

on_pre_frame()[source]

This method will be called before each visualization frame is created. It is meant to be, optionally, implemented by subclasses.

on_pre_step()[source]

This method will be called before each integration step of the simulation. It is meant to be, optionally, implemented by subclasses.

record_frame()[source]

Record a frame using a screenshot recorder.

If frames are meant to be written periodically, a new one will be recorded only if enough time has elapsed, otherwise it will return False. The filename index will be time / period.

If frames are not meant to be written periodically, then index equals simulator’s frame number.

reset_simulation()[source]

Resets the simulation by resetting the graphics adapter and creating a new simulation.

set_key_2_action_mapping()[source]

Creates an Action map, assigns it to key_press_functions and then adds some (key, function tuples.

start()[source]

Starts (indirectly) the simulation handled by this class by starting the visualization window. If it is closed, the simulation ends. It will restart if do_create_window has been previously set to True.