Switch

class pathsim.blocks.switch.Switch(switch_state=None)[source]

Bases: Block

Switch block that selects between its inputs.

Example

The block is initialized like this:

#default None -> no passthrough
s1 = Switch()

#selecting port 2 as passthrough
s2 = Switch(2)

#change the state of the switch to port 3
s2.select(3)

Sets block output depending on self.switch_state like this:

switch_state == None -> outputs[0] = 0

switch_state == 0 -> outputs[0] = inputs[0]

switch_state == 1 -> outputs[0] = inputs[1]

switch_state == 2 -> outputs[0] = inputs[2]

...
Parameters:

switch_state (int, None) – state of the switch

input_port_labels = None
output_port_labels = {'out': 0}
select(switch_state=0)[source]

This method is unique to the Switch block and intended to be used from outside the simulation level for selecting the input ports for the switch state.

This can be achieved for example with the event management system and its callback/action functions.

Parameters:

switch_state (int, None) – switch state / input port selection

to_checkpoint(prefix, recordings=False)[source]

Serialize block state for checkpointing.

Parameters:
  • prefix (str) – key prefix for NPZ arrays (assigned by simulation)

  • recordings (bool) – include recording data (for Scope blocks)

Returns:

  • json_data (dict) – JSON-serializable metadata

  • npz_data (dict) – numpy arrays keyed by path

load_checkpoint(prefix, json_data, npz)[source]

Restore block state from checkpoint.

Parameters:
  • prefix (str) – key prefix for NPZ arrays (assigned by simulation)

  • json_data (dict) – block metadata from checkpoint JSON

  • npz (dict-like) – numpy arrays from checkpoint NPZ

update(t)[source]

Update switch output depending on inputs and switch state.

Parameters:

t (float) – evaluation time