| 1 |
import os, re |
| 2 |
|
| 3 |
curdir, _ = os.path.split(__file__) |
| 4 |
units_fname = os.path.normpath(os.path.join(curdir, '../../../brian2/units/allunits.py')) |
| 5 |
|
| 6 |
_siprefixes = {"y":1e-24, "z":1e-21, "a":1e-18, "f":1e-15, "p":1e-12, "n":1e-9, |
| 7 |
"u":1e-6, "m":1e-3, "c":1e-2, "d":1e-1, "":1, "da":1e1, "h":1e2, |
| 8 |
"k":1e3, "M":1e6, "G":1e9, "T":1e12, "P":1e15, "E":1e18, |
| 9 |
"Z":1e21, "Y":1e24} |
| 10 |
|
| 11 |
fundamental_units = ['metre', 'meter', 'kilogram', 'second', 'amp', 'ampere', 'kelvin', 'mole', 'mol', 'candle'] |
| 12 |
|
| 13 |
#### DERIVED UNITS, from http://physics.nist.gov/cuu/Units/units.html |
| 14 |
derived_unit_table = [ |
| 15 |
['radian', 'rad', 'get_or_create_dimension()'], |
| 16 |
['steradian', 'sr', 'get_or_create_dimension()'], |
| 17 |
['hertz', 'Hz', 'get_or_create_dimension(s= -1)'], |
| 18 |
['newton', 'N', 'get_or_create_dimension(m=1, kg=1, s=-2)'], |
| 19 |
['pascal', 'Pa', 'get_or_create_dimension(m= -1, kg=1, s=-2)'], |
| 20 |
['joule', 'J', 'get_or_create_dimension(m=2, kg=1, s=-2)'], |
| 21 |
['watt', 'W', 'get_or_create_dimension(m=2, kg=1, s=-3)'], |
| 22 |
['coulomb', 'C', 'get_or_create_dimension(s=1, A=1)'], |
| 23 |
['volt', 'V', 'get_or_create_dimension(m=2, kg=1, s=-3, A=-1)'], |
| 24 |
['farad', 'F', 'get_or_create_dimension(m= -2, kg=-1, s=4, A=2)'], |
| 25 |
['ohm', 'ohm', 'get_or_create_dimension(m=2, kg=1, s= -3, A=-2)'], |
| 26 |
['siemens', 'S', 'get_or_create_dimension(m= -2, kg=-1, s=3, A=2)'], |
| 27 |
['weber', 'Wb', 'get_or_create_dimension(m=2, kg=1, s=-2, A=-1)'], |
| 28 |
['tesla', 'T', 'get_or_create_dimension(kg=1, s=-2, A=-1)'], |
| 29 |
['henry', 'H', 'get_or_create_dimension(m=2, kg=1, s=-2, A=-2)'], |
| 30 |
['lumen', 'lm', 'get_or_create_dimension(cd=1)'], |
| 31 |
['lux', 'lx', 'get_or_create_dimension(m=-2, cd=1)'], |
| 32 |
['becquerel', 'Bq', 'get_or_create_dimension(s=-1)'], |
| 33 |
['gray', 'Gy', 'get_or_create_dimension(m=2, s=-2)'], |
| 34 |
['sievert', 'Sv', 'get_or_create_dimension(m=2, s=-2)'], |
| 35 |
['katal', 'kat', 'get_or_create_dimension(s=-1, mol=1)'], |
| 36 |
] |
| 37 |
|
| 38 |
## Generate derived unit objects and make a table of base units from these and the fundamental ones |
| 39 |
base_units = fundamental_units+['kilogramme', 'gram', 'gramme', 'molar'] |
| 40 |
derived = '' |
| 41 |
for longname, shortname, definition in derived_unit_table: |
| 42 |
derived += '{longname} = Unit.create({definition}, "{longname}", "{shortname}")\n'.format( |
| 43 |
longname=longname, shortname=shortname, definition=definition) |
| 44 |
base_units.append(longname) |
| 45 |
|
| 46 |
all_units = base_units + [] |
| 47 |
|
| 48 |
definitions = '######### SCALED BASE UNITS ###########\n' |
| 49 |
|
| 50 |
# Generate scaled units for all base units |
| 51 |
scaled_units = [] |
| 52 |
excluded_scaled_units = set() |
| 53 |
for _bu in base_units: |
| 54 |
if _bu in ['kelvin', 'kilogram', 'kilogramme']: |
| 55 |
continue # do not create "mkelvin", "kkilogram" etc. |
| 56 |
for _k in _siprefixes.keys(): |
| 57 |
if len(_k): |
| 58 |
_u = _k+_bu |
| 59 |
all_units.append(_u) |
| 60 |
definitions += '{_u} = Unit.create_scaled_unit({_bu}, "{_k}")\n'.format( |
| 61 |
_u=_u, _bu=_bu, _k=_k) |
| 62 |
if _k not in ["da", "d", "c", "h"]: |
| 63 |
scaled_units.append(_u) |
| 64 |
else: |
| 65 |
excluded_scaled_units.add(_u) |
| 66 |
|
| 67 |
# Generate 2nd and 3rd powers for all scaled base units |
| 68 |
definitions += '######### SCALED BASE UNITS TO POWERS ###########\n' |
| 69 |
powered_units = [] |
| 70 |
for bu in all_units + []: |
| 71 |
for i in [2, 3]: |
| 72 |
u = bu+str(i) |
| 73 |
definitions += '{u} = Unit.create(({bu}**{i}).dim, name="{u}", ' \ |
| 74 |
'dispname=str({bu})+"^{i}", ' \ |
| 75 |
'scale={bu}.scale*{i})\n'.format(u=u, bu=bu, i=i) |
| 76 |
all_units.append(u) |
| 77 |
if bu not in excluded_scaled_units: |
| 78 |
powered_units.append(u) |
| 79 |
|
| 80 |
additional_units = ''' |
| 81 |
# Current list from http://physics.nist.gov/cuu/Units/units.html, far from complete |
| 82 |
additional_units = [ |
| 83 |
pascal * second, newton * metre, watt / metre ** 2, joule / kelvin, |
| 84 |
joule / (kilogram * kelvin), joule / kilogram, watt / (metre * kelvin), |
| 85 |
joule / metre ** 3, volt / metre ** 3, coulomb / metre ** 3, coulomb / metre ** 2, |
| 86 |
farad / metre, henry / metre, joule / mole, joule / (mole * kelvin), |
| 87 |
coulomb / kilogram, gray / second, katal / metre ** 3, |
| 88 |
# We don't want liter/litre to be used as a standard unit for display, so we |
| 89 |
# put it here instead of in the standard units |
| 90 |
''' |
| 91 |
|
| 92 |
# Scaled units for liter (we don't want liter**2, etc., so it is not included |
| 93 |
# in the base units above; also, we don't want it to register in the standard |
| 94 |
# units, so it is not chosen instead of meter**3 for displaying volume |
| 95 |
# quantities) |
| 96 |
for _bu in ['liter', 'litre']: |
| 97 |
all_units.append(_bu) |
| 98 |
for _k in _siprefixes.keys(): |
| 99 |
_u = _k + _bu |
| 100 |
all_units.append(_u) |
| 101 |
definitions += '{_u} = Unit.create_scaled_unit({_bu}, "{_k}")\n'.format( |
| 102 |
_u=_u, _bu=_bu, _k=_k) |
| 103 |
additional_units += _u + ', ' |
| 104 |
additional_units += ']' |
| 105 |
|
| 106 |
# Add unit names to __all__ |
| 107 |
all = ''' |
| 108 |
__all__ = [ |
| 109 |
{allunits} |
| 110 |
"celsius" # Dummy object raising an error |
| 111 |
] |
| 112 |
'''.format(allunits='\n'.join(' "'+u+'",' for u in all_units)) |
| 113 |
|
| 114 |
def to_definition(name, x): |
| 115 |
return ''' |
| 116 |
{name} = [ |
| 117 |
{items} |
| 118 |
] |
| 119 |
'''.format(name=name, items='\n'.join(' '+i+',' for i in x)) |
| 120 |
|
| 121 |
# Note that for presentation purposes, the `UnitRegistry` uses the *last* |
| 122 |
# defined unit for a given scale. Therefore, we reserve the order of the units |
| 123 |
# so that the standard units come first. |
| 124 |
with open('units_template.py') as f: |
| 125 |
template = f.read() |
| 126 |
units_str = template.format( |
| 127 |
derived=derived, all=all, definitions=definitions, |
| 128 |
base_units=to_definition('base_units', base_units[::-1]), |
| 129 |
scaled_units=to_definition('scaled_units', scaled_units[::-1]), |
| 130 |
powered_units=to_definition('powered_units', powered_units[::-1]), |
| 131 |
all_units=to_definition('all_units', all_units[::-1]), |
| 132 |
additional_units=additional_units, |
| 133 |
) |
| 134 |
|
| 135 |
with open(units_fname, 'w') as f: |
| 136 |
f.write(units_str) |