Source code for firedrake.utils

# Some generic python utilities not really specific to our work.
from decorator import decorator
from pyop2.utils import cached_property  # noqa: F401
from pyop2.datatypes import ScalarType, as_cstr
from firedrake_configuration import get_config

_current_uid = 0

ScalarType_c = as_cstr(ScalarType)

complex_mode = get_config()["options"].get("complex", False)

def _new_uid():
    global _current_uid
    _current_uid += 1
    return _current_uid

def _init():
    """Cause :func:`pyop2.init` to be called in case the user has not done it
    for themselves. The result of this is that the user need only call
    :func:`pyop2.init` if she wants to set a non-default option, for example
    to switch the debug or log level."""
    from pyop2 import op2
    from firedrake.parameters import parameters
    if not op2.initialised():

[docs]def unique_name(name, nameset): """Return name if name is not in nameset, or a deterministic uniquified name if name is in nameset. The new name is inserted into nameset to prevent further name clashes.""" if name not in nameset: nameset.add(name) return name idx = 0 while True: newname = "%s_%d" % (name, idx) if newname in nameset: idx += 1 else: nameset.add(name) return newname
[docs]def known_pyop2_safe(f): """Decorator to mark a function as being PyOP2 type-safe. This switches the current PyOP2 type checking mode to the value given by the parameter "type_check_safe_par_loops", and restores it after the function completes.""" from firedrake.parameters import parameters def wrapper(f, *args, **kwargs): opts = parameters["pyop2_options"] check = opts["type_check"] safe = parameters["type_check_safe_par_loops"] if check == safe: return f(*args, **kwargs) opts["type_check"] = safe try: return f(*args, **kwargs) finally: opts["type_check"] = check return decorator(wrapper, f)