|
|
|
12 Units
Ptolemy II supports units systems, which are
built on top of the expression language. Units systems allow parameter
values to be expressed with units, such as "1.0 * cm", which is
equal to "0.01 * meters". These are expressed this way (with the *
for multiplication) because "cm" and "meters" are actually
variables that become in scope when a units system icon is dragged in
to a model. A few simple units systems are provided (mainly as
examples) in the utilities library.
A model using one of the simple provided units systems is shown in
figure 8 This unit system is called
BasicUnits; the units it defines can
be examined by double clicking on its icon, or by invoking Configure,
as shown in figure 9. In that figure, we
see that "meters", "meter", and "m" are defined, and are all
synonymous. Moreover, "cm" is defined, and given value
"0.01*meters", and "in", "inch" and "inches" are defined, all
with value "2.54*cm".
Figure 8: Example of a model that includes a unit system.
Figure 9: Units defined in a units system can be examined by invoking Configure on its icon.
In the example in figure 8, a constant with value
"1.0 * meter" is
fed into a Scale actor with scale factor equal to "2.0/ms". This
produces a result with dimensions of length over time. If we feed this
result directly into a Display actor, then it is displayed as "2000.0
meters/seconds", as shown in figure 10, top display. The canonical
units for length are meters, and for time are seconds.
Figure 10: Result of running the model in figure 8
In figure 8, we also take the result and feed it to
the InUnitsOf actor, which divides its input by its
argument, and checks to make sure that the result is unitless. This
tells us that 2 meters/ms is equal to about 78,740 inches/second.
The InUnitsOf actor can be used to ensure that
numbers are interpreted correctly in a model, which can be effective
in catching certain kinds of critical errors. For example, if in
figure 8, we had entered "seconds/inch" instead
of "inches/second" in the InUnitsOf actor, we would have gotten the
exception in figure 11 instead of the execution
in figure 10.
Figure 11: Example of an exception resulting from a units mismatch.
Units systems are built entirely on the expression language
infrastructure in Ptolemy II. The units system icons actually
represent instances of scope-extending attributes, which are
attributes whose parameters are in scope as if those parameters were
directly contained by the container of the scopeextending
attribute. That is, scope-extending attributes can define a collection
of variables and constants that can be manipulated as a unit. In
version 2.0 of Ptolemy II, two fairly extensive units systems are
provided, CGSUnitBase and
ElectronicUnitBase. Nonetheless, these are intended as examples only, and can no doubt be significantly improved and extended.