Beispiel #1
0
 def __mul__(self, other):
     if isinstance(other, BlockFunction):
         assert self.M == other._num_sub_spaces
         assert self._block_function_space[1] is other._block_function_space
         output_block_form = empty((self.N, ), dtype=object)
         for I in range(self.N):
             non_zero_J = list()
             for J in range(self.M):
                 if isinstance(self[I, J], Form):
                     non_zero_J.append(J)
             if len(non_zero_J) > 0:
                 output_block_form[I] = self[I, non_zero_J[0]] * other[
                     non_zero_J[0]]
                 for J in non_zero_J[1:]:
                     output_block_form[I] += self[I, J] * other[J]
             else:
                 output_block_form = 0
         return BlockForm1(output_block_form,
                           [self._block_function_space[0]])
     else:
         return NotImplemented
def block_restrict(block_input, block_function_sub_space):
    assert isinstance(
        block_input,
        (array, list, BlockDirichletBC, BlockForm1, BlockForm2, BlockFunction))
    if isinstance(block_input, (array, list, BlockForm1, BlockForm2)):
        if isinstance(block_input, (array, list)):
            (block_form, _,
             block_form_rank) = _block_form_preprocessing(block_input)
        elif isinstance(block_input, BlockForm2):
            block_form = block_input
            block_form_rank = 2
        elif isinstance(block_input, BlockForm1):
            block_form = block_input
            block_form_rank = 1
        if block_form_rank == 2:
            assert isinstance(block_function_sub_space, list)
            assert len(block_function_sub_space) == 2
            assert isinstance(block_function_sub_space[0], BlockFunctionSpace)
            assert isinstance(block_function_sub_space[1], BlockFunctionSpace)
            N_sub_space = block_function_sub_space[0].num_sub_spaces()
            M_sub_space = block_function_sub_space[1].num_sub_spaces()
            sub_block_form = zeros((N_sub_space, M_sub_space), dtype=object)
            for I_sub_space in range(N_sub_space):
                I_space = _sub_component_to_component(
                    block_function_sub_space[0], I_sub_space)
                for J_sub_space in range(M_sub_space):
                    J_space = _sub_component_to_component(
                        block_function_sub_space[1], J_sub_space)
                    sub_block_form[I_sub_space,
                                   J_sub_space] = block_form[I_space, J_space]
            return BlockForm2(sub_block_form, block_function_sub_space)
        elif block_form_rank == 1:
            assert isinstance(block_function_sub_space,
                              (BlockFunctionSpace, list))
            if isinstance(block_function_sub_space, BlockFunctionSpace):
                block_function_sub_space = [block_function_sub_space]
            assert len(block_function_sub_space) == 1
            assert isinstance(block_function_sub_space[0], BlockFunctionSpace)
            N_sub_space = block_function_sub_space[0].num_sub_spaces()
            sub_block_form = zeros((N_sub_space, ), dtype=object)
            for I_sub_space in range(N_sub_space):
                I_space = _sub_component_to_component(
                    block_function_sub_space[0], I_sub_space)
                sub_block_form[I_sub_space] = block_form[I_space]
            return BlockForm1(sub_block_form, block_function_sub_space)
    elif isinstance(block_input, BlockFunction):
        assert isinstance(block_function_sub_space, (BlockFunctionSpace, list))
        if isinstance(block_function_sub_space, list):
            assert len(block_function_sub_space) == 1
            block_function_sub_space = block_function_sub_space[0]
        assert isinstance(block_function_sub_space, BlockFunctionSpace)
        N_sub_space = block_function_sub_space.num_sub_spaces()
        sub_functions = list()
        for I_sub_space in range(N_sub_space):
            I_space = _sub_component_to_component(block_function_sub_space,
                                                  I_sub_space)
            sub_functions.append(block_input[I_space])
        return BlockFunction(block_function_sub_space, sub_functions)
    elif isinstance(block_input, BlockDirichletBC):
        assert isinstance(block_function_sub_space, (BlockFunctionSpace, list))
        if isinstance(block_function_sub_space, list):
            assert len(block_function_sub_space) == 1
            block_function_sub_space = block_function_sub_space[0]
        assert isinstance(block_function_sub_space, BlockFunctionSpace)
        N_sub_space = block_function_sub_space.num_sub_spaces()
        sub_bcs = list()
        for I_sub_space in range(N_sub_space):
            I_space = _sub_component_to_component(block_function_sub_space,
                                                  I_sub_space)
            sub_bcs.append(block_input[I_space])
        return BlockDirichletBC(sub_bcs, block_function_sub_space)
    else:
        raise AssertionError("Invalid arguments to block_restrict")