---
url: /dyad/dev/manual/language/connectors.md
description: >-
  Model physical conservation, causal signals, stream transport, and shared
  network choices with Dyad connectors.
---

# Connectors and Networks {#Connectors-and-Networks}

A connector defines how a component interacts with its surroundings. `connect(...)` joins compatible connectors and generates the equations implied by their field qualifiers. Use physical connectors for conserved quantities such as current, and signal connectors for commands and measurements.

## Physical ports and conservation {#Physical-ports-and-conservation}

An electrical pin carries voltage and current:
Download as a Dyad projectlanguage_connectors.zipOpen in Dyad Studio›

```dyad
connector Pin
  potential v::Real
  flow i::Real
end
```

Connecting pins makes their voltages equal and generates current conservation. Current is positive into a component. Keep this convention at every component boundary; the compiler accounts for hierarchy when connecting an external port to an internal part.

## A complete network {#A-complete-network}

The following definitions form a small resistive circuit. They share the `Pin` definition above. The viewers show code beside the diagram when space allows; on narrower screens, use the **Diagram** and **Code** tabs.

```dyad
component Resistor
  parameter R::Real = 10
  p = Pin()
  n = Pin()
relations
  p.v - n.v = R * p.i
  p.i + n.i = 0
end

component VoltageSource
  parameter V::Real = 5
  p = Pin()
  n = Pin()
relations
  p.v - n.v = V
  p.i + n.i = 0
end

component Ground
  p = Pin()
relations
  p.v = 0
end
```

In the viewer's code, `ResistiveCircuit` assembles the three parts with two `connect` calls. `connect(source.p, load.p)` joins the positive pins; `connect(source.n, load.n, ground.p)` joins the negative pins and ground. Select a part in the diagram to inspect its parameters.&#xA;

```dyad
connector Pin
  potential v::Real
  flow i::Real
metadata {"Dyad": {"icons": {"default": "dyad://Dyad/PosElectricalPort.svg"}}}
end

component Resistor
  parameter R::Real = 10
  p = Pin() {"Dyad": {"placement": {"icon": {"x1": -50, "y1": 450, "x2": 50, "y2": 550}}}}
  n = Pin() {"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 1050, "y2": 550}}}}
relations
  p.v - n.v = R * p.i
  p.i + n.i = 0
metadata {"Dyad": {"icons": {"default": "dyad://ElectricalComponents/Resistor.svg"}, "labels": [{"label": "$(instance)", "x": 500, "y": 1200, "layer": "icon"}]}}
end

component VoltageSource
  parameter V::Real = 5
  p = Pin() {"Dyad": {"placement": {"icon": {"x1": -50, "y1": 450, "x2": 50, "y2": 550}}}}
  n = Pin() {"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 1050, "y2": 550}}}}
relations
  p.v - n.v = V
  p.i + n.i = 0
metadata {"Dyad": {"icons": {"default": "dyad://ElectricalComponents/Voltage.svg"}, "labels": [{"label": "$(instance)", "x": 500, "y": 1200, "layer": "icon"}]}}
end

component Ground
  p = Pin() {"Dyad": {"placement": {"icon": {"x1": 450, "y1": -50, "x2": 550, "y2": 50}}}}
relations
  p.v = 0
metadata {"Dyad": {"icons": {"default": "dyad://ElectricalComponents/Ground.svg"}, "labels": [{"label": "$(instance)", "x": 500, "y": 1200, "layer": "icon"}]}}
end

component ResistiveCircuit
  source = VoltageSource() {"Dyad": {"placement": {"diagram": {"x1": 350, "y1": 600, "x2": 550, "y2": 800}}}}
  load = Resistor() {"Dyad": {"placement": {"diagram": {"x1": 350, "y1": 200, "x2": 550, "y2": 400}}}}
  ground = Ground() {"Dyad": {"placement": {"diagram": {"x1": 650, "y1": 850, "x2": 750, "y2": 950}}}}
relations
  connect(source.p, load.p) {"Dyad": {"edges": [{"S": 1, "M": [{"x": 200, "y": 700}, {"x": 200, "y": 300}], "E": 2}]}}
  connect(source.n, load.n, ground.p) {"Dyad": {"junctions": [{"x": 700, "y": 700}], "edges": [{"S": 1, "M": [], "E": -1}, {"S": 2, "M": [{"x": 700, "y": 300}], "E": -1}, {"S": 3, "M": [], "E": -1}]}}
end
```

The ground fixes the negative pins at `0 V`. The source sets the positive pins to `5 V`, so the `10 Ω` load draws `0.5 A`. The three-port connection creates one connection set: its voltages are equal, and its currents satisfy `source.n.i + load.n.i + ground.p.i = 0`.

Physical connections express simultaneous constraints. A resistor can carry either sign of current; the connector does not impose a direction of causality on the solver.

### Every flow field needs a component equation {#Every-flow-field-needs-a-component-equation}

`connect` generates only the connection equations: the potentials in a connection set are equal, and each flow field sums to zero across the set. The equations that determine a component's own flows come from the component. `Resistor` determines `p.i` and `n.i` with `p.v - n.v = R * p.i` and `p.i + n.i = 0`.

Dyad does not assume a zero flow for a field that a component's equations leave undetermined. A component whose connector carries several flows must give an equation for each of them, including flows it does not exchange. This port carries two species:

```dyad
connector SpeciesPort
  potential c_a::Real
  potential c_b::Real
  flow n_a::Real
  flow n_b::Real
end

component ConsumeA
  parameter k::Real = 0.1
  port = SpeciesPort()
relations
  port.n_a = k * port.c_a
  port.n_b = 0
end
```

`ConsumeA` takes up species A in proportion to its concentration and exchanges no species B. Without `port.n_b = 0`, the component has one equation too few, and a model that uses it is underdetermined.

A connector that takes part in no connection is different: when the model is assembled, each of its flow fields is set to zero. An unconnected `Pin` carries no current, like an open terminal.

Use [partial components](/manual/components#syntax-partial-components) to share port declarations and conservation equations across a family of parts.

## Causal signals {#Causal-signals}

Signal connectors express a producer–consumer relationship. An output supplies a value; connected inputs receive it. A signal connection equates values without a conservation sum.

```dyad
connector SignalInput
  input value::Real
end

connector SignalOutput
  output value::Real
end

component ConstantSignal
  parameter k::Real = 1
  y = SignalOutput()
relations
  y.value = k
end

component Gain
  parameter k::Real = 2
  u = SignalInput()
  y = SignalOutput()
relations
  y.value = k * u.value
end
```

The viewer's `SignalChain` component assembles these parts with `connect(source.y, gain.u)`. Follow that connection in the diagram, then select `gain` to inspect its multiplier `k`. With the default parameters, `gain.y.value = 2`.&#xA;

```dyad
connector SignalInput
  input value::Real
metadata {"Dyad": {"icons": {"default": "dyad://Dyad/RealInput.svg"}}}
end

connector SignalOutput
  output value::Real
metadata {"Dyad": {"icons": {"default": "dyad://Dyad/RealOutput.svg"}}}
end

component ConstantSignal
  parameter k::Real = 1
  y = SignalOutput() {"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 1050, "y2": 550}}}}
relations
  y.value = k
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Constant.svg"}, "labels": [{"label": "$(instance)", "x": 500, "y": 1200, "layer": "icon"}]}}
end

component Gain
  parameter k::Real = 2
  u = SignalInput() {"Dyad": {"placement": {"icon": {"x1": -50, "y1": 450, "x2": 50, "y2": 550}}}}
  y = SignalOutput() {"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 1050, "y2": 550}}}}
relations
  y.value = k * u.value
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Gain.svg"}, "labels": [{"label": "$(instance)", "x": 500, "y": 1200, "layer": "icon"}]}}
end

component SignalChain
  source = ConstantSignal() {"Dyad": {"placement": {"diagram": {"x1": 150, "y1": 350, "x2": 350, "y2": 550}}}}
  gain = Gain() {"Dyad": {"placement": {"diagram": {"x1": 650, "y1": 350, "x2": 850, "y2": 550}}}}
relations
  connect(source.y, gain.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "path": {"stroke": "#2457a7", "stroke-width": "3"}}}
end
```

Give a signal connection a single producer. A signal input is useful for commands and measurements; a physical flow field is useful for conserved transport.

The standard libraries provide signal connector types, including `Dyad.RealInput` and `Dyad.RealOutput`. For clock compatibility and sampling between continuous and discrete models, see [Discrete-Time Modeling](discrete-time.md).

## Connector field reference {#Connector-field-reference}

| Field qualifier | Connection meaning                                | Example                |
|:--------------- |:------------------------------------------------- |:---------------------- |
| `potential`     | Connected values are equal                        | Voltage, pressure      |
| `flow`          | Signed flows obey conservation                    | Current, mass flow     |
| `input`         | The component receives a signal                   | Controller measurement |
| `output`        | The component supplies a signal                   | Controller command     |
| `stream`        | A flow transports an intensive property           | Specific enthalpy      |
| `path`          | A network shares a value with one defining source | Shared configuration   |

## Stream transport {#Stream-transport}

A stream field describes the property of material leaving a component through a port. The associated flow determines transport direction. A connector containing stream fields has one flow field.

For a fluid port with `stream h_outflow`, `instream(port.h_outflow)` gives the incoming mixture seen at that port. It combines the contributions from the other connected ports according to stream semantics. This lets a component describe flow reversal using one interface.

Do not equate every connected `h_outflow` manually: outgoing fluid from different components can have different properties. Use `connect` and the stream operators to express mixing and transport. The [Media and Fluids](media-and-fluids.md) guide develops the library pattern, including the port's shared medium.

## Shared network choices {#Shared-network-choices}

A `path` field lets connected components share one definition of a configuration value. For example, a fluid circuit needs a consistent medium definition across its boundaries, pipes, and storage components. A path can carry that choice through the circuit, with exactly one defining source.

Consider a simple liquid circuit with an inlet, a resistance, and an outlet. Each fluid port declares `path cp::Real` for the liquid's specific heat capacity. The containing model defines `final path cp::Real = 4184.0`, in J/(kg·K). These relations carry that definition through the circuit:

| Relation                                       | What it joins                                    |
|:---------------------------------------------- |:------------------------------------------------ |
| `continuity(cp, inlet.port.cp)`                | The circuit's definition and the inlet port      |
| `connect(inlet.port, resistance.a)`            | The inlet port and the resistance's first port   |
| `continuity(a.cp, b.cp)` inside the resistance | The resistance's two path fields                 |
| `connect(resistance.b, outlet.port)`           | The resistance's second port and the outlet port |

Both boundaries now read `port.cp` as `4184.0` when calculating outflow enthalpy. Changing the circuit's single definition changes the value used at both boundaries. Each boundary still supplies its own temperature: the path shares a property constant, while the stream field describes transported enthalpy.

The [Media and Fluids](/manual/language/media-and-fluids#Share-configuration-along-a-path) example contains this complete circuit and its interactive diagram. It also explains the explicit structural forwarding used by the current MediaComponents and FluidComponents development sources.

## Build interfaces that compose {#Build-interfaces-that-compose}

Expose the physical ports where material or energy crosses a subsystem boundary, and signal ports where another component supplies a command or receives a measurement. Give every physical port a documented sign convention. Choose boundary components that provide the reference, source, or sink needed to make a network well posed.

Port icons and positions are stored as metadata on the connector instances. They affect the graphical representation; `connect` defines the network. See [Graphical Models and Metadata](metadata.md).
