예제 #1
0
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))
예제 #2
0
 def __register_units(cls):
     """
     Adds all units as predefined type symbols to the list of available types.
     """
     from pynestml.symbols.predefined_units import PredefinedUnits
     from pynestml.symbols.unit_type_symbol import UnitTypeSymbol
     units = PredefinedUnits.get_units()
     for unitName in units.keys():
         type_symbol = UnitTypeSymbol(unit=units[unitName])
         cls.name2type[unitName] = type_symbol
     return
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
    def is_conductance_based(self) -> bool:
        """
        Indicates whether this element is conductance based, based on the physical units of the spike input port. If the unit can be cast to Siemens, the function returns True, otherwise it returns False.

        :return: True if conductance based, otherwise False.
        """
        is_cond_based = self.type_symbol.is_castable_to(
            UnitTypeSymbol(unit=PredefinedUnits.get_unit("S")))
        is_curr_based = self.type_symbol.is_castable_to(
            UnitTypeSymbol(unit=PredefinedUnits.get_unit("A")))
        if is_cond_based == is_curr_based:
            code, message = Messages.get_could_not_determine_cond_based(
                type_str=self.type_symbol.print_nestml_type(), name=self.name)
            Logger.log_message(
                node=None,
                code=code,
                message=message,
                log_level=LoggingLevel.WARNING,
                error_position=ASTSourceLocation.get_added_source_position())
            return False

        return is_cond_based
 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')