6. The Simulation Unit¶
The following section describes the unit management provide by Gridsim.
Module author: Gillian Basso<gillian.basso@hevs.ch>
Code author: Gillian Basso<gillian.basso@hevs.ch>
This module provides a unit management based on pint (https://pypi.python.org/pypi/Pint/).
6.1. How to use gridsim.units¶
The unit in Gridsim are used for ‘user interface’ functions such as
gridsim.simulation.Simulator.run(), but for improves performance Gridsim
converts all units in SI base unit and uses them as number (int, float, complex).
Therefore some function parameters are float instead of unit especially when
extending Gridsim core (see The Simulation Core).
It provides an object call units which is a wrapper to all useful features
of pint, thus it should used as follow:
>>> from gridsim.unit import units
>>> area = 1.2*units.meter * 2.5*units.meter
>>> print area
3.0 meter ** 2
>>> print units.value(area)
3.0
>>> print units.dimension(area)
[length] ** 2
>>> print units.unit(area)
meter ** 2
As it is possible to create a measurement by crossing a number value by the unit:
>>> size1 = 1.2*units.meter
it is also possible to create a measurement in an oriented-object way:
>>> size2 = units(1.2, units.meter)
and:
>>> size1 == size2
True
Warning
The second method to create a measurement MUST be used to define
non standard temperature and SHALL be converted in standard unit
before sending to the simulator unless
a pint.unit.OffsetUnitCalculusError will be raised:
celsius = units(20, units.degC) # define temperature in degree celsius
room = sim.thermal.add(ThermalProcess.room('room',
50*units.meter*units.meter, # define square metre
2.5*units.metre, # define metre
units.convert(celsius, units.kelvin))) # convert celsius to kelvin for the simulation
Also, there is a delta counterpart to specify temperature differences. Absolute units have no delta counterpart. For example, the change in celsius is equal to the change in kelvin, but not in fahrenheit (as the scaling factor is different):
>>> hysteresis = 2.4*units.delta_degC
>>> print(hysteresis.to(units.kelvin))
2.4 kelvin
>>> print(hysteresis.to(units.delta_degF))
4.32 delta_degF
-
gridsim.unit.to_si(unit)¶ Returns the SI base unit of the given
unit:>>> mass = 1000*units.gram >>> print units.to_si(mass) 1.0 kilogram >>> print units.to_si(units.kilometre) 1000.0 meter
Parameters: - value (int, float) – the value of the measurement
- unit (see Information of existing units) – the unit of the measurement
-
gridsim.unit.units(value, unit)¶ Creates a measurement of
valuetimesunitEquivalent to:value*unit
Parameters: - value (int, float) – the value of the measurement
- unit (see Information of existing units) – the unit of the measurement
-
units.value(measurement)¶ Returns the value of the
measurementwithout any conversion:>>> size = 1.2*units.meter >>> print units.value(size) 1.2 >>> size = 34*units.kilometer >>> print units.value(size) 34
Returns: the value of the measurement Return type: float
-
units.unit(measurement)¶ Returns the unit of the measurement:
>>> size = 1*units.meter >>> print units.unit(size) meter >>> size = 1*units.kilometer >>> print units.unit(size) kilometer
Returns: the unit of the measurement Return type: str
-
units.dimension(measurement)¶ Returns the unit of the measurement:
>>> size = 1*units.meter >>> print units.dimension(size) [length] >>> size = 1*units.kilometer >>> print units.dimension(size) [length]
Returns: the unit of the measurement Return type: str
-
units.convert(measurement, unit)¶ Converts the
measurementto the unit if possible else raisespint.unit.DimensionalityErrorexception:>>> print units.convert(1*units.metre, units.centimetre) 100.0 centimeter >>> print units.convert(1*units.metre, units.litre) [...] pint.unit.DimensionalityError: Cannot convert from 'meter' ([length]) to 'liter' ([length] ** 3)
Returns: the converted measurement Return type: unit
6.2. Information of existing units¶
Here is a summary of all units that could be used to create a measurement.
This content is based on data of file default_en.txt of pint package.
6.2.1. References¶
The data in square brackets [] are the dimension returned by
units.dimension().
meter = [length] = m = metresecond = [time] = s = secampere = [current] = A = ampcandela = [luminosity] = cd = candlekilogram = [mass] = kgmole = [substance] = molkelvin = [temperature]; offset: 0 = K = degKradian = [] = radbit = []count = []
6.2.2. Prefixes¶
6.2.2.1. Decimal prefixes¶
yocto- = 1e-24 = y-zepto- = 1e-21 = z-atto- = 1e-18 = a-femto- = 1e-15 = f-pico- = 1e-12 = p-nano- = 1e-9 = n-micro- = 1e-6 = u-milli- = 1e-3 = m-centi- = 1e-2 = c-deci- = 1e-1 = d-deca- = 1e+1 = da-hecto- = 1e2 = h-kilo- = 1e3 = k-mega- = 1e6 = M-giga- = 1e9 = G-tera- = 1e12 = T-peta- = 1e15 = P-exa- = 1e18 = E-zetta- = 1e21 = Z-yotta- = 1e24 = Y-
Example:
>>> print units.convert(1*units.yottagram, units.gram)
1e+24 gram
>>> print units.convert(1*units.Yg, units.g)
1e+24 gram
6.2.2.2. Binary_prefixes¶
kibi- = 2**10 = Ki-mebi- = 2**20 = Mi-gibi- = 2**30 = Gi-tebi- = 2**40 = Ti-pebi- = 2**50 = Pi-exbi- = 2**60 = Ei-zebi- = 2**70 = Zi-yobi- = 2**80 = Yi-
Example:
>>> print units.convert(1*units.kibibit, units.bit)
1024.0 bit
>>> print units.convert(1*units.Kibit, units.bit)
1024.0 bit
6.2.3. Other units¶
6.2.3.1. Gridsim units¶
Gridsim defines its own units to simplify coding as these special units is used often times.
heat_capacity = joule/(kilogram*kelvin)mass_density = kilogram/(metre*metre*metre)thermal_conductivity = watt/(kelvin*metre)
6.2.3.2. Acceleration¶
[acceleration] = [length] / [time] ** 2
6.2.3.3. Angle¶
turn = 2 * pi * radian = revolution = cycle = circledegree = pi / 180 * radian = deg = arcdeg = arcdegree = angular_degreearcminute = arcdeg / 60 = arcmin = arc_minute = angular_minutearcsecond = arcmin / 60 = arcsec = arc_second = angular_secondsteradian = radian ** 2 = sr
6.2.3.4. Area¶
[area] = [length] ** 2are = 100 * m**2barn = 1e-28 * m ** 2 = bcmil = 5.067075e-10 * m ** 2 = circular_milsdarcy = 9.869233e-13 * m ** 2acre = 4046.8564224 * m ** 2 = international_acreUS_survey_acre = 160 * rod ** 2
6.2.3.5. Electromagnetism¶
esu = 1 * erg**0.5 * centimeter**0.5 = statcoulombs = statC = franklin = Fresu_per_second = 1 * esu / second = statampereampere_turn = 1 * Agilbert = 10 / (4 * pi ) * ampere_turncoulomb = ampere * second = Cvolt = joule / coulomb = Vfarad = coulomb / volt = Fohm = volt / amperesiemens = ampere / volt = S = mhoweber = volt * second = Wbtesla = weber / meter ** 2 = Thenry = weber / ampere = Helementary_charge = 1.602176487e-19 * coulomb = echemical_faraday = 9.64957e4 * coulombphysical_faraday = 9.65219e4 * coulombfaraday = 96485.3399 * coulomb = C12_faradaygamma = 1e-9 * teslagauss = 1e-4 * teslamaxwell = 1e-8 * weber = mxoersted = 1000 / (4 * pi) * A / m = Oestatfarad = 1.112650e-12 * farad = statF = stFstathenry = 8.987554e11 * henry = statH = stHstatmho = 1.112650e-12 * siemens = statS = stSstatohm = 8.987554e11 * ohmstatvolt = 2.997925e2 * volt = statV = stVunit_pole = 1.256637e-7 * weber
6.2.3.6. Energy¶
[energy] = [force] * [length]joule = newton * meter = Jerg = dyne * centimeterbtu = 1.05505585262e3 * joule = Btu = BTU = british_thermal_uniteV = 1.60217653e-19 * J = electron_voltthm = 100000 * BTU = therm = EC_thermcal = 4.184 * joule = calorie = thermochemical_calorieinternational_steam_table_calorie = 4.1868 * jouleton_TNT = 4.184e9 * joule = tTNTUS_therm = 1.054804e8 * joulewatt_hour = watt * hour = Wh = watthourE_h = 4.35974394e-18 * joule = hartree = hartree_energy
6.2.3.7. Force¶
[force] = [mass] * [acceleration]newton = kilogram * meter / second ** 2 = Ndyne = gram * centimeter / second ** 2 = dynforce_kilogram = g_0 * kilogram = kgf = kilogram_force = pondforce_gram = g_0 * gram = gf = gram_forceforce_ounce = g_0 * ounce = ozf = ounce_forceforce_pound = g_0 * lb = lbf = pound_forceforce_ton = 2000 * force_pound = ton_forcepoundal = lb * feet / second ** 2 = pdlkip = 1000*lbf
6.2.3.8. Frequency¶
[frequency] = 1 / [time]hertz = 1 / second = Hz = rpsrevolutions_per_minute = revolution / minute = rpmcounts_per_second = count / second = cps
6.2.3.9. Information¶
byte = 8 * bit = Bo = octetbaud = bit / second = Bd = bps
6.2.3.10. Length¶
angstrom = 1e-10 * meterinch = 2.54 * centimeter = in = international_inch = inches = international_inchesfoot = 12 * inch = ft = international_foot = feet = international_feetmile = 5280 * foot = mi = international_mileyard = 3 * feet = yd = international_yardmil = inch / 1000 = thouparsec = 3.08568025e16 * meter = pclight_year = speed_of_light * julian_year = ly = lightyearastronomical_unit = 149597870691 * meter = aunautical_mile = 1.852e3 * meter = nmiprinters_point = 127 * millimeter / 360 = pointprinters_pica = 12 * printers_point = picaUS_survey_foot = 1200 * meter / 3937US_survey_yard = 3 * US_survey_footUS_survey_mile = 5280 * US_survey_foot = US_statute_milerod = 16.5 * US_survey_foot = pole = perchfurlong = 660 * US_survey_footfathom = 6 * US_survey_footchain = 66 * US_survey_footbarleycorn = inch / 3arpentlin = 191.835 * feetkayser = 1 / centimeter = wavenumber
6.2.3.11. Mass¶
dram = oz / 16 = dr = avoirdupois_dramounce = 28.349523125 * gram = oz = avoirdupois_ouncepound = 0.45359237 * kilogram = lb = avoirdupois_poundstone = 14 * lb = stcarat = 200 * milligramgrain = 64.79891 * milligram = grlong_hundredweight = 112 * lbshort_hundredweight = 100 * lbmetric_ton = 1000 * kilogram = t = tonnepennyweight = 24 * gram = dwtslug = 14.59390 * kilogramtroy_ounce = 480 * gram = toz = apounce = apothecary_ouncetroy_pound = 12 * toz = tlb = appound = apothecary_pounddrachm = 60 * gram = apdram = apothecary_dramatomic_mass_unit = 1.660538782e-27 * kilogram = u = amu = dalton = Dascruple = 20 * grambag = 94 * lbton = 2000 * lb = short_ton
6.2.3.12. Textile¶
denier = gram / (9000 * meter)tex = gram/ (1000 * meter)dtex = decitex
6.2.3.13. Power¶
[power] = [energy] / [time]watt = joule / second = W = volt_ampere = VAhorsepower = 33000 * ft * lbf / min = hp = UK_horsepower = British_horsepowerboiler_horsepower = 33475 * btu / hourmetric_horsepower = 75 * force_kilogram * meter / secondelectric_horsepower = 746 * watthydraulic_horsepower = 550 * feet * lbf / secondrefrigeration_ton = 12000 * btu / hour = ton_of_refrigeration
6.2.3.14. Pressure¶
[pressure] = [force] / [area]Hg = gravity * 13.59510 * gram / centimeter ** 3 = mercury = conventional_mercurymercury_60F = gravity * 13.5568 * gram / centimeter ** 3H2O = gravity * 1000 * kilogram / meter ** 3 = h2o = water = conventional_waterwater_4C = gravity * 999.972 * kilogram / meter ** 3 = water_39Fwater_60F = gravity * 999.001 * kilogram / m ** 3pascal = newton / meter ** 2 = Pabar = 100000 * pascalatmosphere = 101325 * pascal = atm = standard_atmospheretechnical_atmosphere = kilogram * gravity / centimeter ** 2 = attorr = atm / 760psi = pound * gravity / inch ** 2 = pound_force_per_square_inchksi = kip / inch ** 2 = kip_per_square_inchbarye = 0.1 * newton / meter ** 2 = barie = barad = barrie = baryd = BammHg = millimeter * Hg = mm_Hg = millimeter_Hg = millimeter_Hg_0CcmHg = centimeter * Hg = cm_Hg = centimeter_HginHg = inch * Hg = in_Hg = inch_Hg = inch_Hg_32Finch_Hg_60F = inch * mercury_60Finch_H2O_39F = inch * water_39Finch_H2O_60F = inch * water_60FfootH2O = ft * watercmH2O = centimeter * waterfoot_H2O = ft * water = ftH2Ostandard_liter_per_minute = 1.68875 * Pa * m ** 3 / s = slpm = slm
6.2.3.15. Radiation¶
Bq = Hz = becquerelcurie = 3.7e10 * Bq = Cirutherford = 1e6*Bq = rd = RdGy = joule / kilogram = gray = Sv = sievertrem = 1e-2 * sievertrads = 1e-2 * grayroentgen = 2.58e-4 * coulomb / kilogram
6.2.3.16. Temperature¶
degC = kelvin; offset: 273.15 = celsiusdegR = 5 / 9 * kelvin; offset: 0 = rankinedegF = 5 / 9 * kelvin; offset: 255.372222 = fahrenheit
Warning
As temperature can have an offset, it could be necessary to units.convert()
degree to kelvin to avoid errors.
6.2.3.17. Delta temperature¶
There is a delta counterpart to specify temperature differences. Absolute units have no delta counterpart. For example, the change in celsius is equal to the change in kelvin, but not in fahrenheit (as the scaling factor is different).
delta_degCdelta_degFkelvinrankine
6.2.3.18. Time¶
minute = 60 * second = minhour = 60 * minute = hrday = 24 * hourweek = 7 * dayfortnight = 2 * weekyear = 31556925.9747 * secondmonth = year/12shake = 1e-8 * secondsidereal_day = day / 1.00273790935079524sidereal_hour = sidereal_day/24sidereal_minute=sidereal_hour/60sidereal_second =sidereal_minute/60sidereal_year = 366.25636042 * sidereal_daysidereal_month = 27.321661 * sidereal_daytropical_month = 27.321661 * daysynodic_month = 29.530589 * day = lunar_monthcommon_year = 365 * dayleap_year = 366 * dayjulian_year = 365.25 * daygregorian_year = 365.2425 * daymillenium = 1000 * year = millenia = milenia = mileniumeon = 1e9 * yearwork_year = 2056 * hourwork_month = work_year/12
6.2.3.19. Velocity¶
[speed] = [length] / [time]knot = nautical_mile / hour = kt = knot_international = international_knot = nautical_miles_per_hourmph = mile / hour = MPHkph = kilometer / hour = KPH
6.2.3.20. Viscosity¶
[viscosity] = [pressure] * [time]poise = 1e-1 * Pa * second = Pstokes = 1e-4 * meter ** 2 / second = Strhe = 10 / (Pa * s)
6.2.3.21. Volume¶
[volume] = [length] ** 3liter = 1e-3 * m ** 3 = l = L = litrecc = centimeter ** 3 = cubic_centimeterstere = meter ** 3gross_register_ton = 100 * foot ** 3 = register_ton = GRTacre_foot = acre * foot = acre_feetboard_foot = foot ** 2 * inch = FBMbushel = 2150.42 * inch ** 3 = bu = US_busheldry_gallon = bushel / 8 = US_dry_gallondry_quart = dry_gallon / 4 = US_dry_quartdry_pint = dry_quart / 2 = US_dry_pintgallon = 231 * inch ** 3 = liquid_gallon = US_liquid_gallonquart = gallon / 4 = liquid_quart = US_liquid_quartpint = quart / 2 = pt = liquid_pint = US_liquid_pintcup = pint / 2 = liquid_cup = US_liquid_cupgill = cup / 2 = liquid_gill = US_liquid_gillfluid_ounce = gill / 4 = floz = US_fluid_ounce = US_liquid_ounceimperial_bushel = 36.36872 * liter = UK_bushelimperial_gallon = imperial_bushel / 8 = UK_gallonimperial_quart = imperial_gallon / 4 = UK_quartimperial_pint = imperial_quart / 2 = UK_pintimperial_cup = imperial_pint / 2 = UK_cupimperial_gill = imperial_cup / 2 = UK_gillimperial_floz = imperial_gill / 5 = UK_fluid_ounce = imperial_fluid_ouncebarrel = 42 * gallon = bbltablespoon = floz / 2 = tbsp = Tbsp = Tblsp = tblsp = tbs = Tblteaspoon = tablespoon / 3 = tsppeck = bushel / 4 = pkfluid_dram = floz / 8 = fldr = fluidramfirkin = barrel / 4