How to use the pymoca.ast.Symbol.ATTRIBUTES function in pymoca

To help you get started, we’ve selected a few pymoca examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pymoca / pymoca / src / pymoca / backends / casadi / generator.py View on Github external
def _ast_symbols_to_variables(self, ast_symbols, differentiate=False):
        variables = []
        for ast_symbol in ast_symbols:
            mx_symbol = self.get_mx(ast_symbol)
            modelica_shape = mx_symbol._modelica_shape
            if mx_symbol.is_empty():
                continue
            if differentiate:
                mx_symbol = self.get_derivative(mx_symbol)
                mx_symbol._modelica_shape = modelica_shape
            python_type = self.get_python_type(ast_symbol)
            variable = Variable(mx_symbol, python_type)
            if not differentiate:
                for a in ast.Symbol.ATTRIBUTES:
                    v = self.get_mx(getattr(ast_symbol, a))
                    if v is not None:
                        if isinstance(v, ca.DM) and all(x == (None,) for x in modelica_shape):
                            # Scalar numeric type that behaves like an array.
                            # Coerce to Pyhton type to avoid interpretation
                            # issues.
                            v = python_type(v)
                        elif isinstance(v, (float, int)) and not isinstance(v, python_type):
                            # We skip booleans for now, as users likely depend
                            # on them being integer/float-like.
                            try:
                                v = python_type(v)
                            except (OverflowError, ValueError):
                                # Cannot convert NaN/infs to integer
                                pass
github pymoca / pymoca / src / pymoca / backends / casadi / model.py View on Github external
import logging
import re
import sys

from pymoca import ast

from .alias_relation import AliasRelation
from .mtensor import _MTensor
from ._options import _merge_default_options

logger = logging.getLogger("pymoca")

CASADI_COMPARISON_DEPTH = sys.maxsize
SUBSTITUTE_LOOP_LIMIT = 100
SIMPLIFICATION_LOOP_LIMIT = 50
CASADI_ATTRIBUTES = [attr for attr in ast.Symbol.ATTRIBUTES if not attr == 'unit']


class _DefaultValue(int):
    pass


class Variable:
    def __init__(self, symbol, python_type=float, aliases=None):
        if aliases is None:
            aliases = set()
        self.symbol = symbol
        self.python_type = python_type
        self.aliases = aliases

        # Default attribute values
        self.value = np.nan