Beispiel #1
0
def adjoint(form, reordered_arguments=None):

    # Call UFL directly if new arguments are provided directly
    if reordered_arguments is not None:
        return ufl.adjoint(form, reordered_arguments=reordered_arguments)

    # Extract form arguments
    arguments = form.arguments()
    if any(arg.part() is not None for arg in arguments):
        cpp.dolfin_error("formmanipulation.py",
                         "compute adjoint of form",
                         "parts not supported")
    if not (len(arguments) == 2):
        cpp.dolfin_error("formmanipulation.py",
                         "compute adjoint of form",
                         "Form is not bilinear")

    # Define new Argument(s) in the same spaces (NB: Order does not
    # matter anymore here because number is absolute)
    v_1 = Argument(arguments[1].function_space(), arguments[0].number(),
                   arguments[0].part())
    v_0 = Argument(arguments[0].function_space(), arguments[1].number(),
                   arguments[1].part())

    # Call ufl.adjoint with swapped arguments as new arguments
    return ufl.adjoint(form, reordered_arguments=(v_1, v_0))
Beispiel #2
0
def derivative(form, u, du=None, coefficient_derivatives=None):
    if du is None:
        # Get existing arguments from form and position the new one
        # with the next argument number
        form_arguments = form.arguments()
        number = max([-1] + [arg.number() for arg in form_arguments]) + 1

        # NOTE : Mixed-domains problems need to have arg.part() != None
        # if any(arg.part() is not None for arg in form_arguments):
        #     raise RuntimeError("Compute derivative of form, cannot automatically create new Argument using parts, please supply one")
        part = None

        if isinstance(u, Function):
            # u.part() is None except with mixed-domains
            part = u.part()
            V = u.function_space()
            du = Argument(V, number, part)
        elif isinstance(u, SpatialCoordinate):
            mesh = u.ufl_domain().ufl_cargo()
            element = u.ufl_domain().ufl_coordinate_element()
            V = FunctionSpace(mesh, element)
            du = Argument(V, number, part)
        elif isinstance(u, (list, tuple)) and all(
                isinstance(w, Function) for w in u):
            raise RuntimeError(
                "Taking derivative of form w.r.t. a tuple of Coefficients. Take derivative w.r.t. a single Coefficient on a mixed space instead."
            )
        else:
            raise RuntimeError(
                "Computing derivative of form w.r.t. '{}'. Supply Function as a Coefficient"
                .format(u))
    return ufl.derivative(form, u, du, coefficient_derivatives)
Beispiel #3
0
def derivative(form, u, du=None, coefficient_derivatives=None):
    if du is None:
        # Get existing arguments from form and position the new one
        # with the next argument number
        form_arguments = form.arguments()

        number = max([-1] + [arg.number() for arg in form_arguments]) + 1

        if any(arg.part() is not None for arg in form_arguments):
            cpp.dolfin_error("formmanipulation.py",
                             "compute derivative of form",
                             "Cannot automatically create new Argument using "
                             "parts, please supply one")
        part = None

        if isinstance(u, Function):
            V = u.function_space()
            du = Argument(V, number, part)
        elif isinstance(u, (list, tuple)) and all(isinstance(w, Function) for w in u):
            cpp.dolfin_error("formmanipulation.py",
                             "take derivative of form w.r.t. a tuple of Coefficients",
                             "Take derivative w.r.t. a single Coefficient on "
                             "a mixed space instead.")
        else:
            cpp.dolfin_error("formmanipulation.py",
                             "compute derivative of form w.r.t. '%s'" % u,
                             "Supply Function as a Coefficient")

    return ufl.derivative(form, u, du, coefficient_derivatives)
def derivative(form, u, du=None, coefficient_derivatives=None):
    if du is None:
        # Get existing arguments from form and position the new one
        # with the next argument number
        form_arguments = form.arguments()

        number = max([-1] + [arg.number() for arg in form_arguments]) + 1

        if any(arg.part() is not None for arg in form_arguments):
            raise RuntimeError(
                "Cannot automatically create new Argument using parts, please supply one"
            )
        part = None

        if isinstance(u, Function):
            V = u.function_space()
            du = Argument(V, number, part)
        elif isinstance(u, (list, tuple)) and all(
                isinstance(w, Function) for w in u):
            raise RuntimeError(
                "Take derivative w.r.t. a single Coefficient on a mixed space instead."
            )
        else:
            raise RuntimeError(
                "Computing derivative of form w.r.t. '{}'. Supply Function as a Coefficient"
                .format(u))

    return ufl.derivative(form, u, du, coefficient_derivatives)
Beispiel #5
0
    def __setitem__(self, index, value):
        '''self[i][j] 0 value'''
        if self.V1 is None:
            assert is_number(value) or ((Argument(
                self.V0[index],
                0), ), None) == (test_function(value), None), (Argument(
                    self.V0[index], 0), test_function(value))
            return list.__setitem__(self, index, value)

        assert is_number(value) or (Argument(
            self.V1, 0), ) == test_function(value), (Argument(self.V1, 0),
                                                     test_function(value))
        assert is_number(value) or (Argument(self.V0[index],
                                             1), ) == trial_function(value)

        return list.__setitem__(self, index, value)
Beispiel #6
0
def form_argument_replace(argument, reduced_V):
    return Argument(reduced_V[argument.number()], argument.number(),
                    argument.part())