Skip to contents

get_unit(x) returns the unit of an expression from the unit metadata of its variables, and signals a ValidationError when the units are inconsistent (terms of a sum with different units, a dimensional argument of exp, ...). Units are strings such as "m/s^2", "kJ" or "ms", built from SI base and derived units with SI prefixes, products, quotients and powers. As in ModelingToolkit, units must agree exactly: "kJ" and "J" are different units.

Usage

get_unit(x)

validate(x, info = "")

Arguments

x

An expression, an equation, a list of them or a System.

info

A prefix for the warning messages.

Value

get_unit() a unit object; validate() a logical.

Details

validate(x) checks an equation, a list of equations, an expression or a system: it warns about each inconsistency and returns FALSE, or returns TRUE. Terms that are identically zero are exempt, so 0 ~ P validates. System() runs these checks when a variable has a unit, unless the checks flags exclude CheckUnits.

Examples

tm <- independent_variable("tm"); tm <- setmetadata(tm, "unit", "ms")
tau <- Parameter("tau", unit = "ms")
E <- Variable("E", tm, unit = "kJ"); P <- Variable("P", tm, unit = "MW")
D <- Differential(tm)
get_unit(E / tau)
#> 1e+06 m^2 kg s^-3 
validate(list(D(E) ~ P - E / tau, 0 ~ P))
#> [1] TRUE