Skip to content

9. A units system of yours ​

The SI system is one system_abstract; you can build your own, with the units, prefixes and constants of your domain. A system extends system_abstract64 (or 32, 128) and implements initialize:

f90
type, extends(system_abstract64) :: system_aviation
  contains
    procedure, pass(self) :: initialize
endtype system_aviation

initialize frees the system, then adds its units and prefixes from strings, in the grammar of the previous chapters, and its constants as quantities:

f90
subroutine initialize(self, acronym)
class(system_aviation), intent(inout)        :: self
character(*),           intent(in), optional :: acronym

call self%free
self%acronym = 'AVIATION' ; if (present(acronym)) self%acronym = acronym
call self%add_unit('m = metre [length] {metre}')
call self%add_unit('s = second [time] {second}')
call self%add_unit('ft< = 0.3048 * m = foot> [length] {foot}')
call self%add_unit('NM = 1852 * m [length] {nautical_mile}')
call self%add_unit('h = 3600 * s = hour [time] {hour}')
call self%add_prefix('1.e3 * k = 1.e3 * kilo')
call self%add_constant(qreal64(magnitude=35000._R8P, unit=self%unit('ft'), name='cruise_altitude'))
endsubroutine initialize

A prefix lists its symbol first and its name last (k = kilo): the prefixed units take the symbol in their symbol (km) and the name in their name (kilometre).

Using it ​

f90
call aviation%initialize
print '(A)', aviation%list_units(with_dimensions=.true., with_aliases=.true., compact_reals=.true.)
altitude  = aviation%const('cruise_altitude')
converted = altitude%to(aviation%unit('km'))
print '(A)', altitude%stringify(format='(F7.1)')//' = '//converted%stringify(format='(F6.3)')
$ tutorial_9
  001. : m = metre [length]
  002. : s = second [time]
  003. : ft = +0.3048 * m = foot [length]
  004. : NM = +1852.0 * m [length]
  005. : h = +3600.0 * s = hour [time]
35000.0 ft = 10.668 km

list_units lists the units of the system. The cruise altitude, in feet, is converted into kilometres: ft and km share no formula, but both are defined through m, and FURY converts through it.

What you learned

A system extends system_abstract and implements initialize with add_unit, add_prefix, add_constant; units are converted through the aliases they share. Reference: Units systems.

The tutorial ends here. The cookbook has short recipes for everyday tasks, and the reference has the details of everything you met.

Released under GPL v3, BSD 2-Clause, BSD 3-Clause or MIT, at your choice.