A model−driven approach to automatic conversion of physical units - Cooper, J; McKeever, S (2008)
When physical quantities are used in programs they are typically represented as raw numbers, with the units in which they were measured only being given in comments, if at all. This can lead to errors from the use of dimensionally inconsistent expressions, or the comparison of two quantities of the same dimension but measured in different units, which are not discovered until run time. Any program working with the physical world has this issue, with scientific modelling being a major application. Implementors of models have the time consuming and error-prone task of adding in dynamic units checks and conversions manually. Most existing programming languages do not provide support for representing units explicitly (although extensions to some have been proposed). With the advent of domain specific modelling languages, incorporating code generation techniques, we propose checking physical units at the level of the modelling language, removing the need for such support in the underlying implementation language. We present our work in the context of one such modelling language: CellML, developed at the University of Auckland with a focus on modelling biological systems. We have developed an intuitive algorithm for performing automatic conversions between quantities measured in different units. It both requires fewer conversion operations than current approaches, and makes more sensible choices about which quantities to convert. Uniquely, by using partial evaluation techniques it is also capable of dealing robustly with quantities raised to arbitrary powers, even where the exponent is given by an expression. We demonstrate our algorithm on various examples.