---
url: /dyad/stable/manual/language/values-and-equations.md
description: >-
  Declare parameters and variables, then express their behavior with equations,
  initial conditions, arrays, and structural choices in Dyad.
---

# Parameters, Variables, and Equations {#Parameters,-Variables,-and-Equations}

A component declares typed parameters and variables, then relates them through equations. This page follows that order: choose types, configure parameters, declare unknowns, and describe their behavior.

## Types, units, and attributes {#Types,-units,-and-attributes}

A declaration puts the type after `::`: `variable position::Real` declares a real-valued unknown. Use `Real` for continuous physical quantities, `Integer` for integer values, `Boolean` for `true` or `false`, and `String` for text.

A named type attaches units and other attributes to a base type. A library can define types for quantities it uses repeatedly:

```dyad
type Length = Real(units="m")
type Speed = Real(units="m/s")
```

A component can then declare `variable position::Length` or `variable velocity::Speed`. Attributes can also appear directly on a declaration:

```dyad
variable position::Real(units="m", min=0, max=10, guess=1)
```

| Attribute    | Purpose                                                                                                               |
|:------------ |:--------------------------------------------------------------------------------------------------------------------- |
| `units`      | Records the physical unit.                                                                                            |
| `quantity`   | Names the physical quantity the value represents, as a string.                                                        |
| `min`, `max` | Record bounds for downstream tools.                                                                                   |
| `guess`      | Supplies a starting point for [initialization](/manual/language/values-and-equations#Initial-conditions-and-guesses). |

`quantity` labels what a value represents beyond its units. Two concentrations can share the unit `mmol/L` while describing different species:

```dyad
type GlucoseConcentration = Real(units="mmol/L", quantity="GlucoseConcentration")
type LactateConcentration = Real(units="mmol/L", quantity="LactateConcentration")
```

Like `units`, `quantity` is declarative. In Modelica, connecting two connector fields with different non-empty quantities is an error; Dyad does not currently compare quantities when checking a connection or an equation.

The `position` bounds record an intended range of 0 to 10 m. They do not stop motion at 10 m or clip a computed position to that range. Express that behavior through equations or an appropriate library component.

Units are currently declarative. A `units` attribute documents what a value means, and the generated reference documentation lists it beside each parameter and variable. Dyad does not check that the units in an equation or a connection are consistent: an equation that sets a length equal to a time compiles without a diagnostic. No compiler or analysis option turns on unit checking. By default, the generated ModelingToolkit model does not carry the unit attributes either. The compiler has an internal option that adds unit metadata to parameters, but not to variables, and it does not make Dyad check units. Unit checking is planned.

Until then, keeping units consistent is up to the model author. Use one unit system throughout a model, and write any conversion factor explicitly in the equation that needs it.

## Parameters and variables {#Choose-the-kind-of-quantity}

### Parameters and defaults {#Parameters-and-defaults}

A parameter holds a model setting that stays fixed during a simulation. A declaration can supply a default:

```dyad
parameter tau::Real = 2
```

An instance modification chooses a different value. For example, `CoolingBody(tau=10)` sets the time constant of the component from the [Language Guide](../syntax.md) to 10. Each instance has its own parameter values. A `final parameter` fixes a setting against modification by an enclosing model.

Use [`apply`](loading-parameters.md) to load a set of instance parameters from a TOML or JSON file.

### Variables {#Variables}

A variable declares an unknown that the equations determine:

```dyad
variable x::Real
```

A differential equation describes how a state changes over time; an algebraic equation relates quantities at each time. The following examples use both.

## Write equations in a relations block {#Write-equations}

The `relations` keyword introduces the equations and connections of a component. In this complete component, an initial equation fixes the starting state and a differential equation describes exponential decay:

```dyad
component Decay
  parameter tau::Real = 2
  variable x::Real
relations
  initial x = 1
  der(x) = -x / tau
end
```

`der(x)` is the derivative of `x` with respect to simulation time. For positive `tau`, the state decays toward zero. `initial x = 1` supplies the starting condition.

An equation constrains both sides to have equal values. Its position in the `relations` block does not define an execution order. You can express a force law as `force = mass * der(velocity)` and let the solver determine the unknowns together with the rest of the model.

Algebraic equations describe quantities without their own differential state:

```dyad
component DecayWithEnergy
  parameter tau::Real = 2
  variable x::Real
  variable energy::Real
relations
  initial x = 1
  der(x) = -x / tau
  energy = x^2 / 2
end
```

Here `energy` follows from `x` through the algebraic equation. At initialization, `x = 1` already implies `energy = 0.5`; adding `initial energy = 0` would contradict those equations.

## Initial conditions and guesses {#Initial-conditions-and-guesses}

Initial equations constrain the starting state. Supply independent physical conditions and let the remaining values follow from the equations: adding an initial equation for every algebraic quantity can overconstrain initialization. A guess gives the initialization solver a starting point for its numerical search.

This excerpt gives the pressure solver an initial guess and fixes the initial temperature:

```dyad
variable pressure::Real(guess=100000)
variable temperature::Real
relations
  initial temperature = 300
```

`guess=100000` does not require the solved pressure to equal 100000. `initial temperature = 300` does require the initial temperature to equal 300. Choose guesses near a physically meaningful solution when nonlinear equations admit several roots or are difficult to initialize.

Clocked states use initial values at past ticks. Their initialization is explained in [Discrete-Time Modeling](discrete-time.md).

## Expressions and time {#Expressions-and-time}

Expressions appear in defaults, equations, function arguments, and structural choices. Common forms include arithmetic (`+`, `-`, `*`, `/`, `^`), comparisons (`<`, `>=`, `==`, `!=`), and boolean combinations (`and`, `or`, `!`). The [Syntax Reference](syntax-reference.md) lists supported operators and forms.

`time` gives the current continuous simulation time. This complete component produces a piecewise constant signal:

```dyad
component StepValue
  parameter start::Real = 1
  variable y::Real
relations
  y = time < start ? 0 : 1
end
```

`StepValue()` produces `y = 0` before time 1 and `y = 1` from time 1 onward. The ternary expression `condition ? a : b` selects a value. Both branches should describe compatible quantities. A discontinuous expression alone does not specify a sampled controller; use clocks to express behavior that executes at discrete ticks.

Literals include integers (`10`), reals (`3.14`), scientific notation (`1.5e-3`), booleans (`true`, `false`), and quoted strings (`"steel"`). Numeric engineering suffixes include `10k` for 10000; these scale a number and do not assign units.

## Arrays and repeated equations {#Arrays-and-repeated-equations}

Array dimensions follow the type, and indices start at one. A `structural parameter` can set a dimension because its value is known during model construction. Changing it requires constructing and symbolically processing the model again; the equations or unknowns may change.

```dyad
component DecayArray
  structural parameter N::Integer = 3
  parameter tau::Real = 2
  variable x::Real[N]
relations
  for i in 1:N
    initial x[i] = 1
    der(x[i]) = -x[i] / tau
  end
end
```

`DecayArray()` has three states, each with its own differential equation and initial condition. `DecayArray(N=5)` creates five states; changing `tau` changes their decay rate without changing their number.

Array literal declarations use commas. Matrix literals contain a list of rows:

```dyad
parameter weights::Real[3] = [1, 2, 3]
parameter A::Real[2, 2] = [[1, 2], [3, 4]]
```

Use `A[1, 2]` to access the first row's second element. A comprehension such as `[Cell() for i in 1:N]` constructs an array of components; see [Arrays of components](/manual/components#Arrays-of-components).

## Structural choices {#Structural-choices}

A relation `if` chooses equations using a condition known during model construction:

```dyad
component ConfigurableDecay
  structural parameter active::Boolean = true
  parameter tau::Real = 2
  variable x::Real
relations
  initial x = 1
  if active
    der(x) = -x / tau
  else
    der(x) = 0
  end
end
```

`ConfigurableDecay()` decays from 1 toward zero. `ConfigurableDecay(active=false)` keeps `x = 1`, because its selected equation is `der(x) = 0`. The `active` setting selects that equation during construction and stays fixed during simulation. Use a value expression such as `condition ? a : b` when a quantity changes according to a condition during the simulation.

Enum-valued structural parameters provide named alternatives for initialization policies and model variants. `switch` selects the corresponding relations. See the [Syntax Reference](syntax-reference.md) for enum constructors, payloads, and cases.

## Libraries and Julia functions {#Libraries-and-Julia-functions}

Import library definitions or Julia functions when a model needs behavior supplied by another package. A component library's `Project.toml` declares its dependencies. Dyad source can refer to a dependency's definitions by qualified name or import names with `using`. This example assumes `Mechanical` is a dependency:

```dyad
using Mechanical: Flange, Spring
```

A Julia function imported from another package needs a named function type. If `MyLibrary` provides `myfunc(x, y)`, its import can take this form:

```dyad
type BinaryOp = func(::Real, ::Real)::Real
using MyLibrary: myfunc::BinaryOp
```

`BinaryOp` declares the argument types and return type of `myfunc`. A function's ability to participate in an equation also depends on its compatibility with symbolic arguments and differentiation.

The `Native` type carries Julia values, such as the medium data used in [Media and Fluids](media-and-fluids.md).

Julia definitions in a library's own `dyad/definitions.jl` are available to its Dyad sources. See [Julia-Based Component Libraries](../advanced/juliacomponentlibraries.md) for that integration and [Syntax Reference](syntax-reference.md) for import forms.
