---
url: /dyad/stable/manual/components.md
description: >-
  Build reusable Dyad components through composition, partial interfaces,
  specialization, and arrays.
---

# Components and Reuse {#Components-and-Reuse}

A component groups parameters, variables, connectors, and equations into a reusable model. A small component might express one constitutive law; a subsystem might contain dozens of connected parts. Both use the same `component` declaration.

## Define a complete behavior {#Define-a-complete-behavior}

This model relaxes toward `target` with time constant `tau`:
Download as a Dyad projectlanguage_components.zipOpen in Dyad Studio›

```dyad
component Relaxation
  parameter tau::Real = 1
  parameter target::Real = 0
  variable x::Real
relations
  initial x = 1
  der(x) = (target - x) / tau
end
```

In `Relaxation()`, `x` starts at 1 and approaches the default `target` of 0. `Relaxation(target=2)` starts at the same value but approaches 2. The equation determines `x` throughout the simulation; the parameters set its target and time constant. A simulation of this model requires `tau > 0` for the intended relaxation behavior.

## Compose a subsystem {#Compose-a-subsystem}

A charging circuit combines a voltage source, resistor, capacitor, and ground. The source charges the capacitor through the resistor; `resistance * capacitance` sets the charging time constant.&#xA;

```dyad
component RCCharging
  parameter supply_voltage::Real = 5
  parameter resistance::Resistance = 1000
  parameter capacitance::Capacitance = 100e-6
  variable stored_energy::Real
  dc = BlockComponents.Sources.Constant(k=supply_voltage) {"Dyad": {"placement": {"diagram": {"x1": 80, "y1": 100, "x2": 280, "y2": 300}}}}
  source = ElectricalComponents.Analog.Sources.VoltageSource() {"Dyad": {"placement": {"diagram": {"x1": 400, "y1": 100, "x2": 600, "y2": 300}}}}
  resistor = ElectricalComponents.Analog.Basic.Resistor(R=resistance) {"Dyad": {"placement": {"diagram": {"x1": 400, "y1": 400, "x2": 600, "y2": 600}}}}
  capacitor = ElectricalComponents.Analog.Basic.Capacitor(C=capacitance) {"Dyad": {"placement": {"diagram": {"x1": 700, "y1": 400, "x2": 900, "y2": 600}}}}
  ground = ElectricalComponents.Analog.Basic.Ground() {"Dyad": {"placement": {"diagram": {"x1": 750, "y1": 700, "x2": 950, "y2": 900}}}}
relations
  connect(dc.y, source.V) {"Dyad": {"edges": [{"S": 1, "M": [{"x": 330, "y": 200}, {"x": 330, "y": 75}, {"x": 500, "y": 75}], "E": 2}]}}
  connect(source.p, resistor.p) {"Dyad": {"edges": [{"S": 1, "M": [{"x": 350, "y": 200}, {"x": 350, "y": 500}], "E": 2}]}}
  connect(resistor.n, capacitor.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
  connect(capacitor.n, source.n, ground.g) {"Dyad": {"junctions": [{"x": 950, "y": 650}], "edges": [{"S": 1, "M": [{"x": 950, "y": 500}], "E": -1}, {"S": 2, "M": [{"x": 950, "y": 200}], "E": -1}, {"S": 3, "M": [{"x": 850, "y": 650}], "E": -1}]}}
  initial capacitor.v = 0
  stored_energy = capacitor.C * capacitor.v^2 / 2
end
```

The constant block supplies the voltage source's input. The electrical connections complete a loop through the resistor and capacitor, with ground fixing the return node at zero volts.

| Part of the definition                            | What it does in this circuit                                                                                                         |
|:------------------------------------------------- |:------------------------------------------------------------------------------------------------------------------------------------ |
| `resistor = …Resistor(R=resistance)`              | Creates a resistor subcomponent; the parameter override `R=resistance` sets the child's `R` parameter to the circuit's `resistance`. |
| `connect(source.p, resistor.p)`                   | Joins the source and resistor terminals into one electrical node.                                                                    |
| `initial capacitor.v = 0`                         | Starts the capacitor uncharged.                                                                                                      |
| `stored_energy = capacitor.C * capacitor.v^2 / 2` | Defines the capacitor's stored energy in joules.                                                                                     |

Each subcomponent contributes its own equations. The connections impose equal voltage and current balance at each electrical node. The circuit adds an initial condition and an equation for `stored_energy`; `capacitor.v` and `capacitor.C` refer to the contained capacitor's voltage and capacitance. See [Connectors and Networks](language/connectors.md) for connection semantics.

## Expose settings intentionally {#Expose-settings-intentionally}

The circuit's public parameters configure its internal parts. For example, `RCCharging(resistance=2000)` sets `resistor.R` to 2000 Ω while retaining the default 5 V supply and 100 μF capacitance. This doubles the charging time constant from 0.1 s to 0.2 s.

Use ordinary parameters for numerical settings and structural parameters for choices that change a component's shape. A `final parameter` expresses a setting that derived definitions and instances must preserve. See [Parameters, Variables, and Equations](language/values-and-equations.md) for these declarations.

## Arrays of components {#Arrays-of-components}

A comprehension constructs repeated instances. A structural parameter controls how many instances exist:

```dyad
component RelaxationBank
  structural parameter N::Integer = 3
  cells = [Relaxation(tau=i) for i in 1:N]
end
```

`RelaxationBank()` creates three cells with `tau` values of 1, 2, and 3. Each state starts at 1 and approaches 0; the third cell relaxes most slowly. `cells[1].x` names the first state. A relation loop can connect neighboring elements or relate their variables; see [Arrays and repeated equations](/manual/language/values-and-equations#Arrays-and-repeated-equations). Choose `N` during model construction and construct the model again when changing it.

## Inheritance {#syntax-component-inheritance}

Choose composition when the existing component is a part of your model, and inheritance when the new component specializes its definition.

| Modeling choice | Use it when                                                    | Resulting name |
|:--------------- |:-------------------------------------------------------------- |:-------------- |
| `part = Base()` | The base is a contained part of an assembly                    | `part.x`       |
| `extends Base`  | The new definition specializes the base interface and behavior | `x`            |

Inheritance specializes a common definition. `extends` brings the base component's declarations and relations into the derived component's namespace:

```dyad
component SlowRelaxation
  extends Relaxation(tau=5)
end
```

`SlowRelaxation()` starts with `x = 1` and approaches zero with `tau = 5`. The inherited state is directly available as `x`, with the initialization and differential equation from `Relaxation`. Extending a complete component works well for a named preset; adding an incompatible equation for an inherited state would overconstrain it.

### Partial components {#syntax-partial-components}

A partial component defines declarations and equations shared by a family of components. Derived components supply the remaining behavior.

For an electrical family, the shared definition can contain two ports and a current convention. The example below uses the `potential` and `flow` fields introduced in [Connectors and Networks](language/connectors.md). `TwoPin` defines the interface and current balance; `LinearResistor` adds Ohm's law.

```dyad
partial component TwoPin
  p = Pin() {^p}
  n = Pin() {^n}
  variable v::Real
  variable i::Real
relations
  v = p.v - n.v
  i = p.i
  p.i + n.i = 0
metadata {
  "_links": {
    "p": {
      "Dyad": {
        "placement": {"icon": {"iconName": "pos", "x1": -50, "y1": 450, "x2": 50, "y2": 550}}
      }
    },
    "n": {
      "Dyad": {
        "placement": {"icon": {"iconName": "neg", "x1": 950, "y1": 450, "x2": 1050, "y2": 550}}
      }
    }
  }
}
end

component LinearResistor
  extends TwoPin
  parameter R::Real = 1
relations
  v = R * i
end
```

`TwoPin` deliberately leaves the relation between voltage and current open. A resistor closes it with `v = R * i`; another derived component can supply a storage law. Instantiate the completed component in a network that supplies its electrical boundary conditions.

The built-in `Pin` supplies the `pos` and `neg` electrical icons. The port metadata places them on opposite edges using the placement from ElectricalComponents' `TwoPin` interface. Derived components inherit these placements.

This pattern gives library parts consistent port names, sign conventions, and shared definitions. See the [RLC Circuit tutorial](/tutorials/creating-components#tutorial-rlc) for an assembled circuit.

## Make a component easy to use {#Make-a-component-easy-to-use}

Before sharing a component, check that its users can:

* Configure it through documented public parameters with sensible defaults.

* Connect its ports using documented physical meanings and sign conventions.

* Understand its initialization requirements.

* Run a complete example with the boundary conditions it needs.

For a component library, continue with [Julia-Based Component Libraries](advanced/juliacomponentlibraries.md). Give components their own icons with [Supplying your own SVG icons](/manual/language/metadata#Supplying-your-own-SVGs), and place their ports using [Graphical Models and Metadata](language/metadata.md).
