def register_unit(cls, unit: UnitType) -> None: """ Registers the handed over unit in the set of the predefined units. :param unit: a single unit type. """ if unit.get_name() is not cls.name2unit.keys(): cls.name2unit[unit.get_name()] = unit
def handle_unit(unit_type): """ Handles a handed over unit by creating the corresponding unit-type, storing it in the list of predefined units, creating a type symbol and returning it. :param unit_type: a single sympy unit symbol :type unit_type: Symbol (sympy) :return: a new type symbol :rtype: type_symbol """ # first ensure that it does not already exists, if not create it and register it in the set of predefined units # first clean up the unit of not required components, here it is the 1.0 in front of the unit # e.g., 1.0 * 1 / ms. This step is not mandatory for correctness, but makes reporting easier if isinstance(unit_type, units.Quantity) and unit_type.value == 1.0: to_process = unit_type.unit else: to_process = unit_type if str(to_process) not in PredefinedUnits.get_units().keys(): unit_type_t = UnitType(name=str(to_process), unit=to_process) PredefinedUnits.register_unit(unit_type_t) # now create the corresponding type symbol if it does not exists if PredefinedTypes.get_type(str(to_process)) is None: type_symbol = UnitTypeSymbol( unit=PredefinedUnits.get_unit(str(to_process))) PredefinedTypes.register_type(type_symbol) return PredefinedTypes.get_type(name=str(to_process))
def register_unit(cls, unit): """ Registers a new astropy unit into the system :param unit: an astropy Unit object :type unit: astropy.units.core.Unit """ unit_type = UnitType(str(unit), unit) PredefinedUnits.register_unit(unit_type) type_symbol = UnitTypeSymbol(unit=unit_type) cls.register_type(type_symbol) return
def register_unit(cls, unit): """ Registers a new sympy unit into the system :param unit: a sympy unit. :type unit: SympyUnit """ unit_type = UnitType(str(unit), unit) PredefinedUnits.register_unit(unit_type) type_symbol = UnitTypeSymbol(unit=unit_type) cls.register_type(type_symbol) return
def register_units(cls): """ Registers all units in astropy.units (more specifically, from the si, cgs and astrophys submodules) as predefined units into NESTML. """ # first store all base units and the derived units without the prefix in a list cls.name2unit = {} for unit_str in dir(u.si) + dir(u.cgs) + dir(u.astrophys): try: unit = eval("u." + unit_str) # grab the unit object except BaseException: unit = None if issubclass(type(unit), u.core.UnitBase): for unit_name in unit.names: temp_unit = UnitType(name=str(unit_name), unit=unit) cls.name2unit[str(unit_name)] = temp_unit
def visit_ode_equation(self, node): """ Checks the coco. :param node: A single ode equation. :type node: ast_ode_equation """ variable_name = node.get_lhs().get_name() variable_symbol = node.get_lhs().get_scope().resolve_to_symbol(variable_name, SymbolKind.VARIABLE) variable_type = variable_symbol.type_symbol from pynestml.utils.unit_type import UnitType from pynestml.symbols.unit_type_symbol import UnitTypeSymbol inv_diff_order_unit_type = UnitType(name="inv_diff_order_unit_type_" + variable_name + "'" * node.get_lhs().get_differential_order(), unit=1 / units.s**node.get_lhs().get_differential_order()) inv_diff_order_unit_type_symbol = UnitTypeSymbol(inv_diff_order_unit_type) lhs_type = variable_type * inv_diff_order_unit_type_symbol rhs_type = node.get_rhs().type if not rhs_type.is_castable_to(lhs_type): code, message = Messages.get_ode_needs_consistent_units(variable_name, node.get_lhs().get_differential_order(), lhs_type, rhs_type) Logger.log_message(error_position=node.get_source_position(), code=code, message=message, log_level=LoggingLevel.ERROR)
def test_unit_type(self): ms_unit = UnitType(name=str(units.ms), unit=units.ms) uts = UnitTypeSymbol(unit=ms_unit) result = convert(uts) self.assertEqual(result, 'double')
def register_units(cls): """ Registers all predefined units into th system. """ # first store all base units and the derived units without the prefix in a list cls.name2unit = {} cls.prefixless_units = list() cls.prefixes = list() # first construct the prefix for prefix in u.si_prefixes: cls.prefixes.append((prefix[0][0], prefix[1][0])) # now construct the prefix units cls.prefixless_units.append(('m', 'meter')) cls.prefixless_units.append(('g', 'gram')) cls.prefixless_units.append(('s', 'second')) cls.prefixless_units.append(('A', 'ampere')) cls.prefixless_units.append(('K', 'Kelvin')) cls.prefixless_units.append(('mol', 'mole')) cls.prefixless_units.append(('cd', 'candela')) cls.prefixless_units.append(('rad', 'radian')) cls.prefixless_units.append(('st', 'steradian')) cls.prefixless_units.append(('Hz', 'hertz')) cls.prefixless_units.append(('N', 'newton')) cls.prefixless_units.append(('Pa', 'Pascal')) cls.prefixless_units.append(('J', 'Joule')) cls.prefixless_units.append(('W', 'watt')) cls.prefixless_units.append(('C', 'coulomb')) cls.prefixless_units.append(('V', 'Volt')) cls.prefixless_units.append(('F', 'farad')) cls.prefixless_units.append(('Ohm', 'Ohm')) cls.prefixless_units.append(('S', 'Siemens')) cls.prefixless_units.append(('Wb', 'Weber')) cls.prefixless_units.append(('T', 'Tesla')) cls.prefixless_units.append(('H', 'Henry')) cls.prefixless_units.append(('lx', 'lux')) cls.prefixless_units.append(('lm', 'lumen')) # then generate all combinations with all prefixes for unit in cls.prefixless_units: for prefix in cls.prefixes: temp = eval(str('u.' + prefix[1] + unit[1])) temp_unit = UnitType(name=str(prefix[0] + unit[0]), unit=temp) cls.name2unit[str(prefix[0] + unit[0])] = temp_unit # add also without the prefix, e.g., s for seconds temp_unit = UnitType(name=str(unit[0]), unit=eval(str('u.' + unit[1]))) cls.name2unit[str(unit[0])] = temp_unit # additionally four units are not directly defined, we define them by hand, Bq,Gy,Sv,kat bq = u.def_unit(['Bq', 'Becquerel'], 1 / u.s) gy = u.def_unit(['Gy', 'Gray'], (u.meter**2) / (u.s**2)) sv = u.def_unit(['Sv', 'Sievert'], (u.meter**2) / (u.s**2)) kat = u.def_unit(['kat', 'Katal'], u.mol / u.s) for prefix in cls.prefixes: cls.name2unit[str(prefix[0] + str(bq.name))] = UnitType( name=str(prefix[0] + str(bq.name)), unit=bq) cls.name2unit[str(prefix[0] + str(gy.name))] = UnitType( name=str(prefix[0] + str(gy.name)), unit=gy) cls.name2unit[str(prefix[0] + str(sv.name))] = UnitType( name=str(prefix[0] + str(sv.name)), unit=sv) cls.name2unit[str(prefix[0] + str(kat.name))] = UnitType( name=str(prefix[0] + str(kat.name)), unit=kat) return