Example #1
0
def test_spaces(space, a_blocks, L_blocks):
    for i in range(space.num_sub_spaces()):
        L_form = _create_dolfin_form(L_blocks[i])
        for j in range(space.num_sub_spaces()):
            if i == j:  ## TEMPORARY : Do not conider non-diagonal blocks for now
                a_form = _create_dolfin_form(
                    a_blocks[i * space.num_sub_spaces() + j])
                assert a_form.function_space(0) == L_form.function_space(0)
Example #2
0
 def __init__(self, block_form, block_function_space, form_compiler_parameters=None):
     # Store UFL form
     self._block_form = block_form
     # Store block function space
     assert len(block_function_space) == 1
     self._block_function_space = block_function_space
     # Replace UFL form by Dolfin form before passing it to the constructor
     # (note that we assume that block_form has been already preprocessed,
     #  so we can assume that nested blocks have been unrolled and zero
     #  placeholders have been replaced by zero forms)
     N = len(block_form)
     replaced_block_form = empty((N, ), dtype=object)
     for I in range(N):
         replaced_block_form[I] = block_replace_zero(block_form, (I, ), block_function_space)
         assert isinstance(replaced_block_form[I], Form) or _is_zero(replaced_block_form[I])
         if isinstance(replaced_block_form[I], Form):
             replaced_block_form[I] = _create_dolfin_form(
                 form=replaced_block_form[I],
                 form_compiler_parameters=form_compiler_parameters
             )
         elif _is_zero(replaced_block_form[I]):
             assert isinstance(replaced_block_form[I], cpp_Form)
         else:
             raise TypeError("Invalid form")
     BlockForm1_Base.__init__(self, replaced_block_form.tolist(), [block_function_space_.cpp_object() for block_function_space_ in block_function_space])
     # Store size for len and shape method
     self.N = N
Example #3
0
 def __init__(self, block_form, block_function_space, form_compiler_parameters=None):
     # Store UFL form
     self._block_form = block_form
     # Store block function space
     assert len(block_function_space) == 2
     self._block_function_space = block_function_space
     # Replace UFL form by Dolfin form before passing it to the constructor
     # (note that we assume that block_form has been already preprocessed,
     #  so we can assume that nested blocks have been unrolled and zero
     #  placeholders have been replaced by zero forms)
     N = len(block_form)
     M = len(block_form[0])
     assert all([len(block_form_I) == M for block_form_I in block_form])
     replaced_block_form = empty((N, M), dtype=object)
     for I in range(N):
         for J in range(M):
             if isinstance(block_form[I, J], Form) and has_exact_type(block_form[I, J], CoefficientDerivative):
                 block_form[I, J] = expand_derivatives(block_form[I, J])
             replaced_block_form[I, J] = block_replace_zero(block_form, (I, J), block_function_space)
             assert isinstance(replaced_block_form[I, J], Form) or _is_zero(replaced_block_form[I, J])
             if isinstance(replaced_block_form[I, J], Form):
                 replaced_block_form[I, J] = _create_dolfin_form(
                     form=replaced_block_form[I, J],
                     form_compiler_parameters=form_compiler_parameters
                 )
             elif _is_zero(replaced_block_form[I, J]):
                 assert isinstance(replaced_block_form[I, J], cpp_Form)
             else:
                 raise TypeError("Invalid form")
     BlockForm2_Base.__init__(self, replaced_block_form.tolist(), [block_function_space_.cpp_object() for block_function_space_ in block_function_space])
     # Store sizes for shape method
     self.N = N
     self.M = M
Example #4
0
 def __init__(self,
              block_form,
              block_function_space,
              form_compiler_parameters=None):
     # Store UFL form
     self._block_form = block_form
     # Store block function space
     self._block_function_space = block_function_space
     # Replace UFL form by Dolfin form before passing it to the constructor
     # (note that we assume that block_form has been already preprocessed,
     #  so we can assume that nested blocks have been unrolled and zero
     #  placeholders have been replaced by zero forms)
     N = len(block_form)
     M = len(block_form[0])
     assert all([len(block_form_I) is M for block_form_I in block_form])
     replaced_block_form = empty((N, M), dtype=object)
     for I in range(N):
         for J in range(M):
             replaced_block_form[I, J] = _create_dolfin_form(
                 form=block_form[I, J],
                 form_compiler_parameters=form_compiler_parameters)
     BlockForm2_Base.__init__(self, replaced_block_form.tolist(), [
         block_function_space_.cpp_object()
         for block_function_space_ in block_function_space
     ])
     # Store sizes for shape method
     self.N = N
     self.M = M
Example #5
0
 def decorated_assemble(*args, **kwargs):
     tensor = kwargs.get('tensor')
     if isinstance(tensor, (VectorView, MatrixView)):
         form = _create_dolfin_form(args[0], kwargs.pop("form_compiler_parameters", None))
         spaces = form.function_spaces
         for i in range(len(spaces)):
             dim = spaces[i].dofmap().max_cell_dimension()
             tensor.resize_work_array(i, dim)
     else:
         form = args[0]
     return assemble_function(form, *args[1:], **kwargs)
Example #6
0
def assemble_local(form, cell):
    """Assemble the given form on the given cell and return the
    corresponding local tensor.

    *Arguments*
        Depending on the input form, which may be a functional, linear
        form or bilinear form, a scalar value, a vector or a matrix is
        returned.

    The ``form`` can be either a UFL Form or a DOLFIN Form. The return
    value will be a Python float, or an appropriately sized numpy
    ndarray.

    To understand the connection between the returned local cell
    tensor and the global tensor the mapping can be found as normal
    from a function space as V.dofmap().cell_dofs(cell.index()).

    If you pass a UFL form it will be compiled with
    ``dolfin.Form(form)``. This operation is collective. If you do not
    run assemble_local on all MPI processes simultaneously you should
    pre-compile the form first on all ranks.

    """
    # Create dolfin Form object referencing all data needed by
    # assembler
    dolfin_form = _create_dolfin_form(form)

    assert dolfin_form.rank() in (0, 1, 2)
    tensor = cpp.fem.assemble_local(dolfin_form, cell)

    # Convert to float for scalars and matrix for bilinear forms
    if dolfin_form.rank() == 0:
        tensor = tensor[0]
    elif dolfin_form.rank() == 2:
        test_dofmap = dolfin_form.function_space(0).dofmap()
        test_dofs = test_dofmap.cell_dofs(cell.index())
        N = len(test_dofs)
        tensor = tensor.reshape((N, -1))

    # Return value
    return tensor
Example #7
0
def test_spaces(space, a_blocks, L_blocks):
    for i in range(space.num_sub_spaces()):
        L_form = _create_dolfin_form(L_blocks[i])
        a_form = _create_dolfin_form(a_blocks[i*space.num_sub_spaces() + i])
        assert a_form.function_spaces[0] == L_form.function_spaces[0]