示例#1
0
 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
示例#2
0
 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}
示例#3
0
 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
示例#4
0
 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
示例#5
0
 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
示例#6
0
    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
示例#7
0
    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
示例#8
0
 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
示例#9
0
 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
示例#10
0
 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
示例#11
0
 def terminal(self, t):
     if t.ufl_shape != ():
         ufl_error("Nonscalar terminal {}.".format(str(t)))
     return t
示例#12
0
 def argument(self, e):
     if e.ufl_shape != ():
         ufl_error("Nonscalar argument {}.".format(str(e)))
     return self._argument(0, e)
示例#13
0
 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
示例#14
0
 def terminal(self, t):
     if t.ufl_shape != ():
         ufl_error("Nonscalar terminal {}.".format(str(t)))
     return t
示例#15
0
 def argument(self, e):
     if e.ufl_shape != ():
         ufl_error("Nonscalar argument {}.".format(str(e)))
     return self._argument(0, e)
示例#16
0
 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 }