Skip to content

phyelds.simulator

phyelds.simulator

A simple simulator for aggregate computing systems in phyelds. This simulator allows you to create nodes, set their positions and data, and define their neighborhoods. It also provides a way to schedule events and run the simulation.

Node

A class to represent a node in the simulation.

update(new_position=None, new_data=None)

Update node position and/or data

get_neighbors()

Get neighboring nodes from the environment

__eq__(other)

Define equality based on node id

__hash__()

Hash based on node id

Environment

A class to represent the environment in which nodes exist. It manages the nodes and their neighborhoods. It also provides a way to set the neighborhood function.

add_data_for_all_nodes(data)

Add data to all nodes in the environment

node_list()

Return a list of all nodes in the environment

add_node(node)

Add a node to the environment

remove_node(node_id)

Remove a node from the environment

set_neighborhood_function(func)

Set the function that determines neighborhoods

get_neighbors(node)

Get neighbors for a node using the neighborhood function

no_neighbors(node, all_node) staticmethod

Default neighborhood function (no neighbors)

Event

A class to represent an event in the simulation. An event has a time, an action (function), and optional arguments.

execute()

Execute the event's action

__lt__(other)

For priority queue ordering

Monitor

Bases: ABC

An abstract base class for monitors that can observe the simulation.

on_start()

Called when the simulation starts

on_finish()

Called when the simulation finishes

update()

Called after each event execution

Simulator

A class to represent the simulator for a simple aggregate computing system.

schedule_event(time_delta, action, *args, **kwargs)

Schedule an event to occur after time_delta

run(until_time=None)

Run the simulation until the specified time or until no more events

continue_run(until_time=None)

Continue running the simulation until the specified time or until no more events

add_monitor(monitor)

Add a monitor to the simulator

stop()

Stop the simulation

cancel_event(event)

Cancel a scheduled event

reset()

Reset the simulator

create_node(position, data=None, node_id=None)

Helper method to create and add a node to the environment

phyelds.simulator.deployments

This module contains various functions to generate different types of node distributions in a simulation environment. These functions can be used to create specific patterns or random distributions of nodes for testing and simulation purposes.

grid_generation(simulator, width, height, spacing)

Generate a grid of nodes in the simulator's environment.

deformed_lattice(simulator, width, height, spacing, deformation_factor)

Generate a deformed lattice of nodes in the simulator's environment.

random_walk(simulator, num_steps, step_size)

Perform a random walk and create nodes at each step.

random_in_circle(simulator, num_nodes, radius)

Generate nodes randomly distributed within a circle.

phyelds.simulator.neighborhood

Neighborhood functions for the phyelds simulator. A neighborhood function is simply a function that takes a node and a list of all nodes, and returns a list of neighbors.

radius_neighborhood(radius)

Create a neighborhood function that includes nodes within a certain radius

k_nearest_neighbors(k)

Create a neighborhood function that includes the k nearest nodes

full_neighborhood(node, environment)

Include all nodes as neighbors

phyelds.simulator.runner

This module contains the event to run the aggregate program in the simulator

SimulatorNodeContext

Bases: NodeContext, ABC

A class to represent the context of a node in the simulator. This class is used to pass the node's data and messages to the aggregate program.

from_node(node) staticmethod

Create a SimulatorNodeContext from a Node.

Parameters:

Name Type Description Default
node Node

The node to create the context from.

required

Returns:

Type Description

The SimulatorNodeContext.

aggregate_program_runner(simulator, time_delta, node, program, **kwargs)

Run the program for a node.

schedule_program_for_all(simulator, start, frequency, program, **kwargs)

Schedule the program for all nodes in the simulator.

Parameters:

Name Type Description Default
simulator Simulator

The simulator to schedule the program for.

required
start float

The time to start the program.

required
frequency float

The frequency to run the program.

required
program callable

The program to run.

required

phyelds.simulator.actions

Sent of actions related to node movements and interactions in the simulator

move_with_velocity(simulator, delta_time, node, velocity)

Move the node with a given velocity.

gaussian_movement(simulator, node, mean, stddev)

Move a node according to a Gaussian distribution.

phyelds.simulator.effects

This module contains the effects that can be applied to the simulation rendering.

A class to represent a link between two nodes. The link is represented as a tuple of two nodes. Note! Link(node1, node2) == Link(node2, node1)

Effect

Bases: BaseModel, ABC

Abstract base class for effects.

apply(ax, environment) abstractmethod

Apply the effect to the matplotlib axis.

DrawEdges

Bases: Effect

Draw edges between nodes.

apply(ax, environment)

Draw edges between nodes.

DrawNodes

Bases: Effect

Draw nodes.

With color_from alone, node.data[color_from] is used as a matplotlib color. With cmap too, it is read as a number and mapped onto that colormap between vmin and vmax (by default, the finite values on screen); nodes whose value is missing, infinite or not a number are drawn in missing_color.

apply(ax, environment)

Draw nodes.

DrawIDs

Bases: Effect

Draw node IDs.

apply(ax, environment)

Draw node IDs.

DrawValues

Bases: Effect

Draw the value node.data[value_from] next to each node. Floats are rounded to precision decimals, infinity is shown as ∞ and nodes without a value are skipped. offset is the vertical distance between a node and its label, in data units.

apply(ax, environment)

Draw node values.

RenderMode

Bases: str, Enum

Enum for render modes.

RenderConfig

Bases: BaseModel

Configuration for the RenderMonitor.

save(path)

Save the configuration to a file.

load(path) classmethod

Load the configuration from a file.

validate_effect(v)

Validate the effect type.

phyelds.simulator.render

Render the nodes in the simulator's environment.

RenderMonitor

Bases: Monitor

Render the nodes in the simulator's environment.

make_writer(config)

Create the movie writer matching the extension of config.save_as.

Args: config: the render configuration.

Returns: A PillowWriter for .gif files, an FFMpegWriter otherwise.

phyelds.simulator.exporter

Exporter module for saving simulation metrics to CSV during simulation.

ExporterConfig dataclass

Configuration for CSV exporting of simulation metrics.

csv_exporter(simulator, time_delta, config, **kwargs)

Exporter module for saving simulation metrics to CSV.

aggregate_values(values, precision, aggregator)

Aggregates a list of values using the given method and rounds to the specified precision.

init_dataframe(config, file_path)

Initializes an empty DataFrame with appropriate columns based on config.