Skip to content

Cookbook ​

Short answers to "how do I ...?". Each recipe shows the code and its real output; the reference has the details.

Attach a unit to a number ​

f90
a = qreal64(magnitude=2._R8P, unit=metre, name='width')  ! the constructor, optionally named
b = 2._R8P * metre                                       ! a number times a unit
c = 2 * SI%qunit('metre')                                ! a number times a unit quantity of a system
$ cookbook_attach
width: 2.0 m
2.0 m
2.0 m

The constructor also names the quantity; number * unit works with any integer or real kind; qunit takes the unit from a units system.

f90
print '(A)', g%stringify()
print '(A)', g%stringify(format='(F7.5)')
print '(A)', g%stringify(compact_reals=.true.)
print '(A)', g%stringify(compact_reals=.true., with_dimensions=.true.)
print '(A)', g%stringify(compact_reals=.true., with_aliases=.true.)
print '(A)', g%stringify(compact_reals=.true., with_name=.true.)
$ cookbook_print
+0.98066499999999994E+001 m.s-2
9.80665 m.s-2
+9.80665 m.s-2
+9.80665 m.s-2 [length.time-2]
+9.80665 m = metre.s-2 (acc)
gravity: +9.80665 m.s-2 {metre/second2}

with_aliases prints the aliases of the symbols and the main alias, with_name the name of the quantity and of its unit. format takes any Fortran edit descriptor for reals.

Sum lengths given in different units ​

f90
swim = 3.8_R8P * SI%unit('km')
bike = 112._R8P * SI%unit('mi')
run  = 26.2_R8P * SI%unit('mi')
total = swim%to(SI%unit('m')) + bike%to(SI%unit('m')) + run%to(SI%unit('m'))
print '(A)', 'Ironman: '//total%stringify(format='(F8.1)')
$ cookbook_sum
Ironman: 226211.3 m

Convert first, then sum: a sum of different units stops the program.

Convert temperatures ​

f90
kelvin     = uom64('K [temperature]')
celsius    = uom64('degC< = 273.15 + K> [temperature]')
fahrenheit = uom64('degF< = 255.37222222222223 + 0.5555555555555556 * K> [temperature]')

body       = 98.6_R8P * fahrenheit
in_celsius = body%to(celsius)       ! through K, the alias the two units share
in_kelvin  = body%to(kelvin)
print '(A)', body%stringify(format='(F4.1)')//' = '//in_celsius%stringify(format='(F5.2)')//' = '//&
             in_kelvin%stringify(format='(F6.2)')
$ cookbook_temperature
98.6 degF = 37.00 degC = 310.15 K

Fahrenheit and Celsius are both defined towards the kelvin, with an offset and a factor: FURY converts between them through K.

Compute with the SI system ​

The kinetic energy of a car, from a speed in km/h:

f90
mass   = 1200._R8P * SI%unit('kg')
speed    = 100._R8P * SI%unit('km') / (1._R8P * SI%unit('hour'))
speed_si = speed%to(SI%unit('metre.second-1'))
energy   = 0.5_R8P * mass * speed_si**2
in_joule = energy%to(SI%unit('joule'))
print '(A)', 'speed : '//speed%stringify(format='(F5.1)')//' = '//speed_si%stringify(format='(F6.3)')
print '(A)', 'energy: '//in_joule%stringify(format='(F8.1)', with_aliases=.true.)
$ cookbook_energy
speed : 100.0 km.hour-1 = 27.778 m.s-1
energy: 462963.0 kg.m2.s-2 (J)

km.hour-1 is converted reference by reference, the kilometres into metres and the hours into seconds; the energy, kg.m2.s-2, is converted into the joule to print its main alias.

Powers and roots ​

f90
side   = 4._R8P * metre
area   = side ** 2
volume = side ** 3
root   = area ** 0.5_R8P
print '(A)', area%stringify(format='(F4.1)', with_dimensions=.true.)
print '(A)', volume%stringify(format='(F4.1)', with_dimensions=.true.)
print '(A)', root%stringify(format='(F3.1)', with_dimensions=.true.)
$ cookbook_powers
16.0 m2 [length2]
64.0 m3 [length3]
4.0 m [length]

A real exponent is allowed when the resulting exponents of the symbols are integers.

Convert a compound quantity ​

FURY converts a unit symbol by symbol, and does not expand an alias inside a product: an impulse in lbf.s is not converted into kg.m.s-1 directly. Convert the force, then multiply:

f90
impulse    = 10._R8P * (pound_force * second)
force      = impulse / (1._R8P * second)     ! back to a force, lbf
impulse_si = force%to(newton) * (1._R8P * second)
print '(A)', impulse%stringify(format='(F4.1)')//' = '//impulse_si%stringify(format='(F6.3)')
$ cookbook_impulse
10.0 lbf.s = 44.482 kg.m.s-1

Compare quantities ​

f90
print '(L1)', length == 3._R8P * metre
print '(L1)', length /= other_length
print '(L1)', length%has_same_unit(other_length)
print '(L1)', length%has_same_unit(time)
$ tutorial_4 compare
T
T
T
F

== and /= compare the magnitudes and the units, has_same_unit the units only. Comparing floating point magnitudes with == has the usual pitfalls: compare abs(a%magnitude - b%magnitude) with a tolerance when the values come from computations.

Reuse a variable for another unit ​

f90
total = length
call total%unset
total = time
print '(A)', total%stringify(format='(F3.1)')
$ tutorial_4 reuse
2.0 s

Without unset, the second assignment stops the program.

Look up a unit of the SI system ​

f90
query = [character(16) :: 'm', 'meter', 'second', 'sec', 'km', 'kilometre']
do i=1, size(query)
  call metre%unset
  metre = SI%unit(trim(query(i)))
  print '(A)', query(i)//' -> '//metre%stringify(with_name=.true.)
enddo

The query matches a name, a symbol, a synonym, a main alias or a prefixed unit; a unit not found is returned undefined (is_defined() is false).

Use a physical constant ​

f90
light = SI%const('speed_of_light')
g     = SI%const('gravity')
weight = 75._R8P * SI%unit('kg') * g
print '(A)', light%stringify(compact_reals=.true., with_name=.true.)
print '(A)', 'weight: '//weight%stringify(format='(F5.1)', with_dimensions=.true.)

The Units systems page lists the constants of the SI system.

Data sizes ​

f90
kilobyte = 64 * SI%qunit('KiB')
call converted%unset
converted = kilobyte%to(SI%unit('byte'))
print '(A)', kilobyte%stringify(format='(F4.1)')//' = '//converted%stringify(format='(F7.1)')

The binary prefixes (Ki, Mi, Gi, ...) are powers of 1024, the decimal ones (k, M, G, ...) powers of 1000.

A conversion that is not a factor ​

A user-supplied converter, from dBm to mW:

f90
dBm = uom64('dBm = @user mW')
mW  = uom64('mW')
call dBm%set_alias_conversion(reference_index=1, alias_index=2, convert=converter)

power = 20._R8P * dBm
converted = power%to(mW)
print '(A)', power%stringify(format='(F4.1)')//' = '//converted%stringify(format='(F5.1)')
call power%unset
power = converted%to(dBm)
print '(A)', converted%stringify(format='(F5.1)')//' = '//power%stringify(format='(F4.1)')
$ tutorial_8
20.0 dBm = 100.0 mW
100.0 mW = 20.0 dBm

The converter itself is in chapter 8.

Mix precisions ​

f90
sum128 = third128 + third64
narrowed = third128
print '(A)', sum128%stringify()
print '(A)', narrowed%stringify()

The result of a mixed-kind operation has the kind of the more precise operand.

Catch the Mars Climate Orbiter bug ​

f90
program mco
!< The Mars Climate Orbiter mishap: an impulse in pound-force seconds summed to one in newton seconds.
use fury
implicit none
type(uom64)   :: newton, pound_force, second
type(qreal64) :: impulse_ground, impulse_spacecraft, total

newton      = uom64('kg [mass].m [length].s-2 [time-2] (N[force]) {newton}')
pound_force = uom64('lbf< = 4.4482216152605 * N> [force] {pound_force}')
second      = uom64('s [time] {second}')

impulse_spacecraft = 10._R8P * (newton * second)       ! the spacecraft expected newton seconds
impulse_ground     = 10._R8P * (pound_force * second)  ! the ground software produced pound-force seconds

print '(A)', 'spacecraft: '//impulse_spacecraft%stringify(format='(F5.1)', with_dimensions=.true.)
print '(A)', 'ground    : '//impulse_ground%stringify(format='(F5.1)', with_dimensions=.true.)
total = impulse_spacecraft + impulse_ground
print '(A)', 'total     : '//total%stringify(format='(F5.1)')
endprogram mco
$ mco
spacecraft:  10.0 kg.m.s-1 [mass.length.time-1]
ground    :  10.0 lbf.s [force.time]
error: cannot add "+10.0 kg.m.s-1 [mass.length.time-1]" and "+10.0 lbf.s [force.time]"
[exit status 1]

Stop instead of a wrong conversion ​

f90
metre  = uom64('m [length]')
second = uom64('s [time]')
q = 3._R8P * metre
converted = q%to(second)
$ tutorial_5 impossible
error: cannot convert "m [length]" to "s [time]"
[exit status 1]

Define a units system ​

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

The complete program is chapter 9.

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