def sum(self, e, a, b): n_a = len(a) if isinstance(a, dict) else 0 n_b = len(b) if isinstance(b, dict) else 0 if n_a > 0 and n_b > 0: c = {} keys = set(a.keys()) | set(b.keys()) for k in keys: av = a.get(k) bv = b.get(k) if av is None: # Case: Only b contains a term with test function component k c[k] = bv elif bv is None: # Case: Only a contains a term with test function component k c[k] = av else: # Case: Both a and b contains a term with test function component k c[k] = av + bv return c elif n_a or n_b: ufl_error( "Cannot add Argument-dependent expression with non-Argument-dependent expression." ) else: return e
def _argument(self, component, expr): if self._arg is None: self._arg = expr elif self._arg is not expr: ufl_error( "Expecting only one Argument in this algorithm implementation." ) return {component: self._one}
def operator(self, e, *ops): if e.ufl_shape != (): ufl_error("Nonscalar operator {}.".format(str(e))) if any(isinstance(op, dict) for op in ops): ufl_error( "Handler for operator {} assumes no Arguments among operands.". format(e._ufl_handler_name_)) return e
def division(self, e, a, b): if isinstance(b, dict): ufl_error("Cannot divide by Arguments.") if isinstance(a, dict): c = {} for k, v in a.items(): c[k] = v / b return c else: return e
def division(self, e, a, b): if isinstance(b, dict): ufl_error("Cannot divide by Arguments.") if isinstance(a, dict): c = {} for k,v in a.items(): c[k] = v / b return c else: return e
def indexed(self, e): if e.ufl_shape != (): ufl_error("Nonscalar indexed {}.".format(str(e))) v, i = e.ufl_operands if v._ufl_typecode_ == Argument._ufl_typecode_: if len(i) != 1 or not isinstance(i[0], FixedIndex): ufl_error("Expecting only vector valued Arguments in this algorithm implementation.") return self._argument(int(i[0]), v) return e
def indexed(self, e): if e.ufl_shape != (): ufl_error("Nonscalar indexed {}.".format(str(e))) v, i = e.ufl_operands if v._ufl_typecode_ == Argument._ufl_typecode_: if len(i) != 1 or not isinstance(i[0], FixedIndex): ufl_error( "Expecting only vector valued Arguments in this algorithm implementation." ) return self._argument(int(i[0]), v) return e
def product(self, e, a, b): a_is_dict = isinstance(a, dict) b_is_dict = isinstance(b, dict) if a_is_dict and b_is_dict: ufl_error("Expecting only one Argument in this algorithm. Products of Arguments are not allowed.") elif a_is_dict: c = {} for k,v in a.items(): c[k] = v*b return c elif b_is_dict: c = {} for k,v in b.items(): c[k] = v*a return c else: return e
def product(self, e, a, b): a_is_dict = isinstance(a, dict) b_is_dict = isinstance(b, dict) if a_is_dict and b_is_dict: ufl_error( "Expecting only one Argument in this algorithm. Products of Arguments are not allowed." ) elif a_is_dict: c = {} for k, v in a.items(): c[k] = v * b return c elif b_is_dict: c = {} for k, v in b.items(): c[k] = v * a return c else: return e
def sum(self, e, a, b): n_a = len(a) if isinstance(a, dict) else 0 n_b = len(b) if isinstance(b, dict) else 0 if n_a > 0 and n_b > 0: c = {} keys = set(a.keys()) | set(b.keys()) for k in keys: av = a.get(k) bv = b.get(k) if av is None: # Case: Only b contains a term with test function component k c[k] = bv elif bv is None: # Case: Only a contains a term with test function component k c[k] = av else: # Case: Both a and b contains a term with test function component k c[k] = av + bv return c elif n_a or n_b: ufl_error("Cannot add Argument-dependent expression with non-Argument-dependent expression.") else: return e
def terminal(self, t): if t.ufl_shape != (): ufl_error("Nonscalar terminal {}.".format(str(t))) return t
def argument(self, e): if e.ufl_shape != (): ufl_error("Nonscalar argument {}.".format(str(e))) return self._argument(0, e)
def operator(self, e, *ops): if e.ufl_shape != (): ufl_error("Nonscalar operator {}.".format(str(e))) if any(isinstance(op, dict) for op in ops): ufl_error("Handler for operator {} assumes no Arguments among operands.".format(e._ufl_handler_name_)) return e
def _argument(self, component, expr): if self._arg is None: self._arg = expr elif self._arg is not expr: ufl_error("Expecting only one Argument in this algorithm implementation.") return { component: self._one }