Thinking in fields: aggregate programs¶
Aggregate computing programs a collective instead of a single device. Rather than writing message handlers, socket loops, or state machines for individual nodes, you write one function that describes how a global distributed data structure—a computational field—evolves over space and time.
For the theoretical foundations, see the main paper on aggregate computing.
Local execution, global meaning¶
A computational field assigns a value to every device at every moment in time (for example: a field of temperatures, a field of distances from an exit, or a field of cluster IDs). No single coordinator holds the whole field; instead, it emerges from every device repeatedly running the same program and exchanging values only with its immediate neighbors.

Every aggregate function therefore has two readings:
- Local reading: what one device computes in a single step (for instance, "take the minimum of my neighbors' numbers and add 1").
- Global reading: what field the entire network converges to over time (for instance, "the shortest-path distance of every device from the nearest source").
You design by composing global fields, while Python executes the local steps on each device.
The execution model: Sense → Compute → Act¶
Every device executes the aggregate program in periodic, asynchronous rounds. In each round, a device goes through three phases:

- Sense: the runtime gathers the device's current sensor readings, the state saved at the end of its own previous round, and the most recent messages received from its current neighbors.
- Compute: the device runs the
@aggregatefunction from top to bottom. Whenever the code callsremember(...), it reads from the previous state; whenever it callsneighbors(...), it reads the neighbors' values for that exact call site and records its own value for the outbox. - Act: the runtime stores the new local state for the next round, exposes any outputs written with
store(...)to the platform (actuators, logs, metrics), and broadcasts the single outbox packet to neighboring devices.
Rounds across different devices do not need to be synchronized, and devices can move, join, or leave at any time. Because every round recomputes the field from fresh inputs, aggregate programs naturally self-heal when the network topology changes.
How space and time combine¶
All collective behaviors in phyelds come from combining just four mechanisms:
| Mechanism | Primitive | What it gives you |
|---|---|---|
| Space only | neighbors(x) |
1-hop perception: compare or aggregate values across immediate neighbors |
| Time only | remember(init) |
Local memory: counters, filters, timers, and state machines on a single node |
| Space + Time | exchange(init) |
Global propagation: information travels hop-by-hop across the whole network (gradients, broadcasts, collections, gossip) |
| Domain splitting | if / align, @process |
Network partitioning: split devices into independent sub-networks that do not mix messages |
The building blocks at a glance¶
| Construct | What it does |
|---|---|
@aggregate |
Marks a function as part of the aggregate computation and aligns its branches |
remember |
Keeps local state that survives from one round to the next |
neighbors |
Shares a value with neighbors and returns their values as a NeighborhoodField |
exchange |
Evolves state and sends (possibly distinct, per-neighbor) values in one step |
if / align |
Isolates branches into separate, non-interfering communication domains |
sense, store, local_id |
Reads device sensors and writes actuation/metric outputs (phyelds.libraries.device) |
Pragmatic and Pythonic¶
Most aggregate computing frameworks (such as ScaFi, Protelis, or FCPP) enforce a strictly functional style. phyelds is intentionally pragmatic: aggregate programs are ordinary Python functions where you can use standard loops, dictionaries, classes, and controlled mutability.
Three golden rules to keep in mind
Because Python does not enforce purity at compile time, keep these three rules in mind (see Common pitfalls for concrete examples):
- Decorate every aggregate helper with
@aggregateso its calls andif/elsebranches are properly aligned across devices. - Never call
neighborsa data-dependent number of times (e.g., inside aforloop whose length varies from device to device). - Keep device state in
remember, not in Python globals or mutated neighbor objects, because all simulated nodes share the same Python process.
To watch fields, neighbors and gradients evolve round by round, open the interactive 3D walkthrough, a full-screen page apart from these docs; then go on with the tutorial.