def assemble_estimator_diffusive_flux_aa(lambda_xi, lambda_xi_prime, grid, ii,
                                         block_space, lambda_hat, kappa,
                                         solution_space):
    local_subdomains, num_local_subdomains, num_global_subdomains = _get_subdomains(
        grid)
    diffusive_flux_aa_product = make_diffusive_flux_aa_product(
        grid,
        ii,
        block_space.local_space(ii),
        lambda_hat,
        lambda_u=lambda_xi,
        lambda_v=lambda_xi_prime,
        kappa=kappa,
        over_integrate=2)
    subdomain_walker = make_subdomain_walker(grid, ii)
    subdomain_walker.append(diffusive_flux_aa_product)
    subdomain_walker.walk()
    # , block_space.local_space(ii).dof_communicator,
    matrix = DuneXTMatrixOperator(diffusive_flux_aa_product.matrix(),
                                  range_id='domain_{}'.format(ii),
                                  source_id='domain_{}'.format(ii))
    df_ops = np.full((num_global_subdomains, ) * 2, None)
    df_ops[ii, ii] = matrix
    return BlockOperator(df_ops,
                         range_spaces=solution_space.subspaces,
                         source_spaces=solution_space.subspaces)
        def assemble_estimator_diffusive_flux_aa(lambda_xi, lambda_xi_prime):
            df_ops = np.full((grid.num_subdomains,) * 2, None)
            df_ops[ii, ii] = DiffusiveFluxOperatorAA(ii, ii, ii, block_op.source, grid, block_space,
                                                     global_rt_space, neighborhood_boundary_info,
                                                     lambda_hat, lambda_xi, lambda_xi_prime, kappa)

            return BlockOperator(df_ops, range_spaces=spaces, source_spaces=spaces)
Exemplo n.º 3
0
 def __init__(self, d):
     self._impl = d
     lhs_op = BlockOperator([[wrapper[d.get_local_operator(ss)] if ss == nn
                              else wrapper[d.get_coupling_operator(ss, nn)] if nn in list(d.neighbouring_subdomains(ss))
                              else None
                              for nn in np.arange(d.num_subdomains())] for ss in np.arange(d.num_subdomains())])
     rhs_op = BlockOperator.hstack([wrapper[d.get_local_functional(ss)] for ss in np.arange(d.num_subdomains())])
     operators = {'operator': lhs_op}
     functionals = {'rhs': rhs_op}
     vector_operators = {}
     self.operators = FrozenDict(operators)
     self.functionals = FrozenDict(functionals)
     self.vector_operators = FrozenDict(vector_operators)
     self.operator = operators['operator']
     self.solution_space = self.operator.source
     self.rhs = functionals['rhs']
     self.products = {k: BlockDiagonalOperator([wrapper[d.get_local_product(ss, k)]
                                                for ss in np.arange(d.num_subdomains())])
                      for k in list(d.available_products())}
     if self.products:
         for k, v in self.products.iteritems():
             setattr(self, '{}_product'.format(k), v)
             setattr(self, '{}_norm'.format(k), induced_norm(v))
     self.linear = all(op.linear for op in operators.itervalues())
     self.num_subdomains = self._impl.num_subdomains()
     self.neighboring_subdomains = [self._impl.neighbouring_subdomains(ss)
                                    for ss in np.arange(self.num_subdomains)]
     self.build_parameter_type(inherits=operators.values())
     assert self.parameter_type == self._wrapper[d.parameter_type()]
 def assemble_estimator_residual_functional():
     r1_ops = np.full((1, grid.num_subdomains,), None)
     for jj in neighborhood:
         r1_ops[0, jj] = ResidualPartFunctional(f, ii, jj, jj, block_op.source, grid, block_space,
                                                global_rt_space, neighborhood_boundary_info,
                                                lambda_hat, None, None, kappa)
     return BlockOperator(r1_ops, source_spaces=rt_spaces, name='residual_functional_{}'.format(ii))
Exemplo n.º 5
0
def test_vstack():
    np.random.seed(0)
    A = np.random.randn(2, 3)
    B = np.random.randn(4, 3)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = BlockOperator.vstack((Aop, Bop))
    assert Cop.source.dim == 3
    assert Cop.range.dim == 6
 def assemble_estimator_noconformity():
     nc_ops = np.full((grid.num_subdomains,) * 2, None)
     for jj in neighborhood:
         for kk in neighborhood:
             nc_ops[jj, kk] = NonconformityOperator(ii, jj, kk, block_op.source, grid, block_space,
                                                    global_rt_space, neighborhood_boundary_info,
                                                    lambda_bar, None, None, kappa)
     return BlockOperator(nc_ops, range_spaces=oi_op.range.subspaces, source_spaces=oi_op.range.subspaces,
                          name='nonconformity_{}'.format(ii))
Exemplo n.º 7
0
def test_vstack():
    np.random.seed(0)
    A = np.random.randn(2, 3)
    B = np.random.randn(4, 3)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = BlockOperator.vstack((Aop, Bop))
    assert Cop.source.dim == 3
    assert Cop.range.dim == 6
        def assemble_estimator_residual():
            r2_ops = np.full((grid.num_subdomains,) * 2, None)
            for jj in neighborhood:
                for kk in neighborhood:
                    r2_ops[jj, kk] = ResidualPartOperator(ii, jj, kk, block_op.source, grid, block_space,
                                                          global_rt_space, neighborhood_boundary_info,
                                                          lambda_hat, None, None, kappa)

            return BlockOperator(r2_ops, range_spaces=rt_spaces, source_spaces=rt_spaces, name='residual_{}'.format(ii))
        def assemble_estimator_diffusive_flux_bb():
            df_ops = np.full((grid.num_subdomains,) * 2, None)
            for jj in neighborhood:
                for kk in neighborhood:
                    df_ops[jj, kk] = DiffusiveFluxOperatorBB(ii, jj, kk, block_op.source, grid, block_space,
                                                             global_rt_space, neighborhood_boundary_info,
                                                             lambda_hat, None, None, kappa)

            return BlockOperator(df_ops, range_spaces=rt_spaces, source_spaces=rt_spaces,
                                 name='diffusive_flux_bb_{}'.format(ii))
Exemplo n.º 10
0
def test_to_matrix_BlockOperator():
    np.random.seed(0)
    A11 = np.random.randn(2, 2)
    A12 = np.random.randn(2, 3)
    A21 = np.random.randn(3, 2)
    A22 = np.random.randn(3, 3)
    B = np.asarray(np.bmat([[A11, A12], [A21, A22]]))

    A11op = NumpyMatrixOperator(A11)
    A12op = NumpyMatrixOperator(A12)
    A21op = NumpyMatrixOperator(A21)
    A22op = NumpyMatrixOperator(A22)
    Bop = BlockOperator([[A11op, A12op], [A21op, A22op]])
    assert_type_and_allclose(B, Bop, 'dense')

    A11op = NumpyMatrixOperator(sps.csc_matrix(A11))
    A12op = NumpyMatrixOperator(A12)
    A21op = NumpyMatrixOperator(A21)
    A22op = NumpyMatrixOperator(A22)
    Bop = BlockOperator([[A11op, A12op], [A21op, A22op]])
    assert_type_and_allclose(B, Bop, 'sparse')
Exemplo n.º 11
0
def test_apply_adjoint():
    np.random.seed(0)

    A11 = np.random.randn(2, 3)
    A12 = np.random.randn(2, 4)
    A21 = np.zeros((5, 3))
    A22 = np.random.randn(5, 4)
    A = np.vstack((np.hstack((A11, A12)), np.hstack((A21, A22))))
    A11op = NumpyMatrixOperator(A11)
    A12op = NumpyMatrixOperator(A12)
    A22op = NumpyMatrixOperator(A22)
    Aop = BlockOperator(np.array([[A11op, A12op], [None, A22op]]))

    v1 = np.random.randn(2)
    v2 = np.random.randn(5)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_numpy(v1)
    v2va = NumpyVectorSpace.from_numpy(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Aop.apply_adjoint(vva)
    w = np.hstack((wva.block(0).to_numpy(), wva.block(1).to_numpy()))
    assert np.allclose(A.T.dot(v), w)
Exemplo n.º 12
0
def test_apply_transpose():
    np.random.seed(0)

    A11 = np.random.randn(2, 3)
    A12 = np.random.randn(2, 4)
    A21 = np.zeros((5, 3))
    A22 = np.random.randn(5, 4)
    A = np.vstack((np.hstack((A11, A12)),
                   np.hstack((A21, A22))))
    A11op = NumpyMatrixOperator(A11)
    A12op = NumpyMatrixOperator(A12)
    A22op = NumpyMatrixOperator(A22)
    Aop = BlockOperator(np.array([[A11op, A12op], [None, A22op]]))

    v1 = np.random.randn(2)
    v2 = np.random.randn(5)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Aop.apply_transpose(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(A.T.dot(v), w)
Exemplo n.º 13
0
 def unblock_op(op, sparse=False):
     assert op._blocks[0][0] is not None
     if isinstance(op._blocks[0][0], LincombOperator):
         coefficients = op._blocks[0][0].coefficients
         operators = [
             None for kk in np.arange(len(op._blocks[0][0].operators))
         ]
         for kk in np.arange(len(op._blocks[0][0].operators)):
             ops = [[
                 op._blocks[ii][jj].operators[kk]
                 if op._blocks[ii][jj] is not None else None
                 for jj in np.arange(op.num_source_blocks)
             ] for ii in np.arange(op.num_range_blocks)]
             operators[kk] = unblock_op(BlockOperator(ops))
         return LincombOperator(operators=operators,
                                coefficients=coefficients)
     else:
         assert all(
             all([
                 isinstance(block, NumpyMatrixOperator
                            ) if block is not None else True
                 for block in row
             ]) for row in op._blocks)
         if op.source.dim == 0 and op.range.dim == 0:
             return NumpyMatrixOperator(np.zeros((0, 0)))
         elif op.source.dim == 1:
             mat = np.concatenate([
                 op._blocks[ii][0]._matrix
                 for ii in np.arange(op.num_range_blocks)
             ],
                                  axis=1)
         elif op.range.dim == 1:
             mat = np.concatenate([
                 op._blocks[0][jj]._matrix
                 for jj in np.arange(op.num_source_blocks)
             ],
                                  axis=1)
         else:
             mat = bmat([[
                 coo_matrix(op._blocks[ii][jj]._matrix)
                 if op._blocks[ii][jj] is not None else coo_matrix(
                     (op._range_dims[ii], op._source_dims[jj]))
                 for jj in np.arange(op.num_source_blocks)
             ] for ii in np.arange(op.num_range_blocks)])
             mat = mat.toarray()
         return NumpyMatrixOperator(mat)
def discretize_lhs(lambda_func, grid, block_space, local_patterns,
                   boundary_patterns, coupling_matrices, kappa,
                   local_all_neumann_boundary_info, boundary_info,
                   coupling_patterns, solver_options):
    logger = getLogger('discretize_lhs')
    logger.debug('...')
    local_subdomains, num_local_subdomains, num_global_subdomains = _get_subdomains(
        grid)
    local_matrices = [None] * num_global_subdomains
    boundary_matrices = {}
    logger.debug('discretize lhs coupling matrices ...')
    for ii in range(num_global_subdomains):
        local_matrices[ii] = Matrix(
            block_space.local_space(ii).size(),
            block_space.local_space(ii).size(), local_patterns[ii])
        if ii in grid.boundary_subdomains():
            boundary_matrices[ii] = Matrix(
                block_space.local_space(ii).size(),
                block_space.local_space(ii).size(), boundary_patterns[ii])

    logger.debug('discretize lhs ipdg ops ...')
    for ii in range(num_global_subdomains):
        ss = block_space.local_space(ii)
        ll = local_matrices[ii]
        ipdg_operator = make_elliptic_swipdg_matrix_operator(
            lambda_func,
            kappa,
            local_all_neumann_boundary_info,
            ll,
            ss,
            over_integrate=2)
        ipdg_operator.assemble(False)

    logger.debug('discretize lhs ops ...')
    local_ipdg_coupling_operator = make_local_elliptic_swipdg_coupling_operator(
        lambda_func, kappa)

    def assemble_coupling_contributions(subdomain, neighboring_subdomain):
        coupling_assembler = block_space.coupling_assembler(
            subdomain, neighboring_subdomain)
        coupling_assembler.append(
            local_ipdg_coupling_operator,
            coupling_matrices['in_in'][(subdomain, neighboring_subdomain)],
            coupling_matrices['out_out'][(subdomain, neighboring_subdomain)],
            coupling_matrices['in_out'][(subdomain, neighboring_subdomain)],
            coupling_matrices['out_in'][(subdomain, neighboring_subdomain)])
        coupling_assembler.assemble()

    for ii in range(num_global_subdomains):
        for jj in grid.neighboring_subdomains(ii):
            if ii < jj:  # Assemble primally (visit each coupling only once).
                assemble_coupling_contributions(ii, jj)

    logger.debug('discretize lhs boundary ...')
    local_ipdg_boundary_operator = make_local_elliptic_swipdg_boundary_operator(
        lambda_func, kappa)
    apply_on_dirichlet_intersections = make_apply_on_dirichlet_intersections(
        boundary_info)

    def assemble_boundary_contributions(subdomain):
        boundary_assembler = block_space.boundary_assembler(subdomain)
        boundary_assembler.append(local_ipdg_boundary_operator,
                                  boundary_matrices[subdomain],
                                  apply_on_dirichlet_intersections)
        boundary_assembler.assemble()

    for ii in grid.boundary_subdomains():
        assemble_boundary_contributions(ii)

    logger.debug('discretize lhs global contributions ...')
    global_pattern = SparsityPatternDefault(block_space.mapper.size)
    for ii in range(num_global_subdomains):
        block_space.mapper.copy_local_to_global(local_patterns[ii], ii,
                                                global_pattern)
        if ii in grid.boundary_subdomains():
            block_space.mapper.copy_local_to_global(boundary_patterns[ii], ii,
                                                    global_pattern)
        for jj in grid.neighboring_subdomains(ii):
            if ii < jj:  # Assemble primally (visit each coupling only once).
                block_space.mapper.copy_local_to_global(
                    coupling_patterns['in_in'][(ii, jj)], ii, ii,
                    global_pattern)
                block_space.mapper.copy_local_to_global(
                    coupling_patterns['out_out'][(ii, jj)], jj, jj,
                    global_pattern)
                block_space.mapper.copy_local_to_global(
                    coupling_patterns['in_out'][(ii, jj)], ii, jj,
                    global_pattern)
                block_space.mapper.copy_local_to_global(
                    coupling_patterns['out_in'][(ii, jj)], jj, ii,
                    global_pattern)

    system_matrix = Matrix(block_space.mapper.size, block_space.mapper.size,
                           global_pattern)
    for ii in range(num_global_subdomains):
        block_space.mapper.copy_local_to_global(local_matrices[ii],
                                                local_patterns[ii], ii,
                                                system_matrix)
        if ii in grid.boundary_subdomains():
            block_space.mapper.copy_local_to_global(boundary_matrices[ii],
                                                    boundary_patterns[ii], ii,
                                                    ii, system_matrix)
        for jj in grid.neighboring_subdomains(ii):
            if ii < jj:  # Assemble primally (visit each coupling only once).
                block_space.mapper.copy_local_to_global(
                    coupling_matrices['in_in'][(ii, jj)],
                    coupling_patterns['in_in'][(ii, jj)], ii, ii,
                    system_matrix)
                block_space.mapper.copy_local_to_global(
                    coupling_matrices['out_out'][(ii, jj)],
                    coupling_patterns['out_out'][(ii, jj)], jj, jj,
                    system_matrix)
                block_space.mapper.copy_local_to_global(
                    coupling_matrices['in_out'][(ii, jj)],
                    coupling_patterns['in_out'][(ii, jj)], ii, jj,
                    system_matrix)
                block_space.mapper.copy_local_to_global(
                    coupling_matrices['out_in'][(ii, jj)],
                    coupling_patterns['out_in'][(ii, jj)], jj, ii,
                    system_matrix)
    logger.debug('discretize lhs global op ...')
    op = DuneXTMatrixOperator(system_matrix,
                              dof_communicator=block_space.dof_communicator,
                              solver_options=solver_options)
    logger.debug('discretize lhs global op done ...')
    mats = np.full((num_global_subdomains, num_global_subdomains), None)
    for ii in range(num_global_subdomains):
        ii_size = block_space.local_space(ii).size()
        for jj in range(ii, num_global_subdomains):
            jj_size = block_space.local_space(jj).size()
            if ii == jj:
                mats[ii, ii] = Matrix(ii_size, ii_size, local_patterns[ii])
            elif (ii, jj) in coupling_matrices['in_out']:
                mats[ii, jj] = Matrix(ii_size, jj_size,
                                      coupling_patterns['in_out'][(ii, jj)])
                mats[jj, ii] = Matrix(jj_size, ii_size,
                                      coupling_patterns['out_in'][(ii, jj)])

    for ii in range(num_global_subdomains):
        for jj in range(ii, num_global_subdomains):
            if ii == jj:
                mats[ii, ii].axpy(1., local_matrices[ii])
                if ii in boundary_matrices:
                    mats[ii, ii].axpy(1., boundary_matrices[ii])
            elif (ii, jj) in coupling_matrices['in_out']:
                mats[ii, ii].axpy(1., coupling_matrices['in_in'][(ii, jj)])
                mats[jj, jj].axpy(1., coupling_matrices['out_out'][(ii, jj)])
                mats[ii, jj].axpy(1., coupling_matrices['in_out'][(ii, jj)])
                mats[jj, ii].axpy(1., coupling_matrices['out_in'][(ii, jj)])

    logger.debug('discretize lhs block op ...')
    ops = np.full((num_global_subdomains, num_global_subdomains), None)
    for (ii, jj), mat in np.ndenumerate(mats):
        ops[ii, jj] = DuneXTMatrixOperator(
            mat,
            name='local_block_{}-{}'.format(ii, jj),
            source_id='domain_{}'.format(jj),
            range_id='domain_{}'.format(ii)) if mat else None

    block_op = BlockOperator(ops,
                             dof_communicator=block_space.dof_communicator,
                             name='BlockOp')
    return op, block_op
    def discretize_lhs_for_lambda(lambda_):
        local_matrices = [None]*grid.num_subdomains
        local_vectors = [None]*grid.num_subdomains
        boundary_matrices = {}
        coupling_matrices_in_in = {}
        coupling_matrices_out_out = {}
        coupling_matrices_in_out = {}
        coupling_matrices_out_in = {}
        for ii in range(grid.num_subdomains):
            local_matrices[ii] = Matrix(block_space.local_space(ii).size(),
                                        block_space.local_space(ii).size(),
                                        local_patterns[ii])
            local_vectors[ii] = Vector(block_space.local_space(ii).size())
            if ii in grid.boundary_subdomains():
                boundary_matrices[ii] = Matrix(block_space.local_space(ii).size(),
                                               block_space.local_space(ii).size(),
                                               boundary_patterns[ii])
            for jj in grid.neighboring_subdomains(ii):
                if ii < jj:  # Assemble primally (visit each coupling only once).
                    coupling_matrices_in_in[(ii, jj)] = Matrix(block_space.local_space(ii).size(),
                                                               block_space.local_space(ii).size(),
                                                               coupling_patterns_in_in[(ii, jj)])
                    coupling_matrices_out_out[(ii, jj)] = Matrix(block_space.local_space(jj).size(),
                                                                 block_space.local_space(jj).size(),
                                                                 coupling_patterns_out_out[(ii, jj)])
                    coupling_matrices_in_out[(ii, jj)] = Matrix(block_space.local_space(ii).size(),
                                                                block_space.local_space(jj).size(),
                                                                coupling_patterns_in_out[(ii, jj)])
                    coupling_matrices_out_in[(ii, jj)] = Matrix(block_space.local_space(jj).size(),
                                                                block_space.local_space(ii).size(),
                                                                coupling_patterns_out_in[(ii, jj)])

        def assemble_local_contributions(subdomain):
            ipdg_operator = make_elliptic_swipdg_matrix_operator(lambda_, kappa, local_all_neumann_boundary_info,
                                                                 local_matrices[subdomain],
                                                                 block_space.local_space(subdomain))
            l2_functional = make_l2_volume_vector_functional(f, local_vectors[subdomain],
                                                             block_space.local_space(subdomain))
            local_assembler = make_system_assembler(block_space.local_space(subdomain))
            local_assembler.append(ipdg_operator)
            local_assembler.append(l2_functional)
            local_assembler.assemble()

        for ii in range(grid.num_subdomains):
            assemble_local_contributions(ii)

        local_ipdg_coupling_operator = make_local_elliptic_swipdg_coupling_operator(lambda_, kappa)

        def assemble_coupling_contributions(subdomain, neighboring_subdomain):
            coupling_assembler = block_space.coupling_assembler(subdomain, neighboring_subdomain)
            coupling_assembler.append(local_ipdg_coupling_operator,
                                      coupling_matrices_in_in[(subdomain, neighboring_subdomain)],
                                      coupling_matrices_out_out[(subdomain, neighboring_subdomain)],
                                      coupling_matrices_in_out[(subdomain, neighboring_subdomain)],
                                      coupling_matrices_out_in[(subdomain, neighboring_subdomain)])
            coupling_assembler.assemble()

        for ii in range(grid.num_subdomains):
            for jj in grid.neighboring_subdomains(ii):
                if ii < jj:  # Assemble primally (visit each coupling only once).
                    assemble_coupling_contributions(ii, jj)

        local_ipdg_boundary_operator = make_local_elliptic_swipdg_boundary_operator(lambda_, kappa)
        apply_on_dirichlet_intersections = make_apply_on_dirichlet_intersections(boundary_info)

        def assemble_boundary_contributions(subdomain):
            boundary_assembler = block_space.boundary_assembler(subdomain)
            boundary_assembler.append(local_ipdg_boundary_operator,
                                      boundary_matrices[subdomain],
                                      apply_on_dirichlet_intersections)
            boundary_assembler.assemble()

        for ii in grid.boundary_subdomains():
            assemble_boundary_contributions(ii)

        global_pattern = SparsityPatternDefault(block_space.mapper.size)
        for ii in range(grid.num_subdomains):
            block_space.mapper.copy_local_to_global(local_patterns[ii], ii, global_pattern)
            if ii in grid.boundary_subdomains():
                block_space.mapper.copy_local_to_global(boundary_patterns[ii], ii, global_pattern)
            for jj in grid.neighboring_subdomains(ii):
                if ii < jj:  # Assemble primally (visit each coupling only once).
                    block_space.mapper.copy_local_to_global(coupling_patterns_in_in[(ii, jj)], ii, ii, global_pattern)
                    block_space.mapper.copy_local_to_global(coupling_patterns_out_out[(ii, jj)], jj, jj, global_pattern)
                    block_space.mapper.copy_local_to_global(coupling_patterns_in_out[(ii, jj)], ii, jj, global_pattern)
                    block_space.mapper.copy_local_to_global(coupling_patterns_out_in[(ii, jj)], jj, ii, global_pattern)

        system_matrix = Matrix(block_space.mapper.size, block_space.mapper.size, global_pattern)
        rhs_vector = Vector(block_space.mapper.size, 0.)
        for ii in range(grid.num_subdomains):
            block_space.mapper.copy_local_to_global(local_matrices[ii], local_patterns[ii], ii, system_matrix)
            block_space.mapper.copy_local_to_global(local_vectors[ii], ii, rhs_vector)
            if ii in grid.boundary_subdomains():
                block_space.mapper.copy_local_to_global(boundary_matrices[ii], boundary_patterns[ii], ii, ii, system_matrix)
            for jj in grid.neighboring_subdomains(ii):
                if ii < jj:  # Assemble primally (visit each coupling only once).
                    block_space.mapper.copy_local_to_global(coupling_matrices_in_in[(ii, jj)],
                                                            coupling_patterns_in_in[(ii, jj)],
                                                            ii, ii, system_matrix)
                    block_space.mapper.copy_local_to_global(coupling_matrices_out_out[(ii, jj)],
                                                            coupling_patterns_out_out[(ii, jj)],
                                                            jj, jj, system_matrix)
                    block_space.mapper.copy_local_to_global(coupling_matrices_in_out[(ii, jj)],
                                                            coupling_patterns_in_out[(ii, jj)],
                                                            ii, jj, system_matrix)
                    block_space.mapper.copy_local_to_global(coupling_matrices_out_in[(ii, jj)],
                                                            coupling_patterns_out_in[(ii, jj)],
                                                            jj, ii, system_matrix)

        op = DuneXTMatrixOperator(system_matrix)
        mats = np.full((grid.num_subdomains, grid.num_subdomains), None)
        for ii in range(grid.num_subdomains):
            for jj in range(ii, grid.num_subdomains):
                if ii == jj:
                    mats[ii, ii] = Matrix(block_space.local_space(ii).size(),
                                          block_space.local_space(ii).size(),
                                          local_patterns[ii])
                elif (ii, jj) in coupling_matrices_in_out:
                    mats[ii, jj] = Matrix(block_space.local_space(ii).size(),
                                          block_space.local_space(jj).size(),
                                          coupling_patterns_in_out[(ii, jj)])
                    mats[jj, ii] = Matrix(block_space.local_space(jj).size(),
                                          block_space.local_space(ii).size(),
                                          coupling_patterns_out_in[(ii, jj)])

        for ii in range(grid.num_subdomains):
            for jj in range(ii, grid.num_subdomains):
                if ii == jj:
                    mats[ii, ii].axpy(1.,  local_matrices[ii])
                    if ii in boundary_matrices:
                        mats[ii, ii].axpy(1.,  boundary_matrices[ii])
                elif (ii, jj) in coupling_matrices_in_out:
                    mats[ii, ii].axpy(1., coupling_matrices_in_in[(ii, jj)])
                    mats[jj, jj].axpy(1., coupling_matrices_out_out[(ii, jj)])
                    mats[ii, jj].axpy(1., coupling_matrices_in_out[(ii, jj)])
                    mats[jj, ii].axpy(1., coupling_matrices_out_in[(ii, jj)])

        ops = np.full((grid.num_subdomains, grid.num_subdomains), None)
        for (ii, jj), mat in np.ndenumerate(mats):
            ops[ii, jj] = DuneXTMatrixOperator(mat,
                                               source_id='domain_{}'.format(jj),
                                               range_id='domain_{}'.format(ii)) if mat else None
        block_op = BlockOperator(ops)

        rhs = VectorFunctional(op.range.make_array([rhs_vector]))
        rhss = []
        for ii in range(grid.num_subdomains):
            rhss.append(ops[ii, ii].range.make_array([local_vectors[ii]]))
        block_rhs = VectorFunctional(block_op.range.make_array(rhss))

        return op, block_op, rhs, block_rhs
Exemplo n.º 16
0
def misc_operator_with_arrays_and_products_factory(n):
    if n == 0:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 10, 4, 3, n)
        op = ComponentProjection(np.random.randint(0, 100, 10), U.space)
        return op, _, U, V, sp, rp
    elif n == 1:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 0, 4, 3, n)
        op = ComponentProjection([], U.space)
        return op, _, U, V, sp, rp
    elif n == 2:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 3, 4, 3, n)
        op = ComponentProjection([3, 3, 3], U.space)
        return op, _, U, V, sp, rp
    elif n == 3:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=True)
        return op, _, V, U, rp, sp
    elif n == 4:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=False)
        return op, _, V, U, rp, sp
    elif 5 <= n <= 7:
        from pymor.operators.constructions import SelectionOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(30, 30, 4, 3, n)
        op1 = NumpyMatrixOperator(np.random.random((30, 30)))
        op2 = NumpyMatrixOperator(np.random.random((30, 30)))
        op = SelectionOperator([op0, op1, op2], ProjectionParameterFunctional('x', ()), [0.3, 0.6])
        return op, op.parse_parameter((n-5)/2), V, U, rp, sp
    elif n == 8:
        from pymor.operators.block import BlockDiagonalOperator
        from pymor.vectorarrays.block import BlockVectorArray
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(20, 20, 4, 3, n+1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(30, 30, 4, 3, n+2)
        op = BlockDiagonalOperator([op0, op1, op2])
        sp = BlockDiagonalOperator([sp0, sp1, sp2])
        rp = BlockDiagonalOperator([rp0, rp1, rp2])
        U = BlockVectorArray([U0, U1, U2])
        V = BlockVectorArray([V0, V1, V2])
        return op, _, U, V, sp, rp
    elif n == 9:
        from pymor.operators.block import BlockDiagonalOperator, BlockOperator
        from pymor.vectorarrays.block import BlockVectorArray
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(20, 20, 4, 3, n+1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(20, 10, 4, 3, n+2)
        op = BlockOperator([[op0, op2],
                            [None, op1]])
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = BlockDiagonalOperator([rp0, rp1])
        U = BlockVectorArray([U0, U1])
        V = BlockVectorArray([V0, V1])
        return op, None, U, V, sp, rp
    else:
        assert False
Exemplo n.º 17
0
def misc_operator_with_arrays_and_products_factory(n):
    if n == 0:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 10, 4, 3, n)
        op = ComponentProjection(np.random.randint(0, 100, 10), U.space)
        return op, _, U, V, sp, rp
    elif n == 1:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 0, 4, 3, n)
        op = ComponentProjection([], U.space)
        return op, _, U, V, sp, rp
    elif n == 2:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 3, 4, 3, n)
        op = ComponentProjection([3, 3, 3], U.space)
        return op, _, U, V, sp, rp
    elif n == 3:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=True)
        return op, _, V, U, rp, sp
    elif n == 4:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=False)
        return op, _, V, U, rp, sp
    elif 5 <= n <= 7:
        from pymor.operators.constructions import SelectionOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            30, 30, 4, 3, n)
        op1 = NumpyMatrixOperator(np.random.random((30, 30)))
        op2 = NumpyMatrixOperator(np.random.random((30, 30)))
        op = SelectionOperator([op0, op1, op2],
                               ProjectionParameterFunctional('x'), [0.3, 0.6])
        return op, op.parameters.parse((n - 5) / 2), V, U, rp, sp
    elif n == 8:
        from pymor.operators.block import BlockDiagonalOperator
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(
            30, 30, 4, 3, n + 2)
        op = BlockDiagonalOperator([op0, op1, op2])
        sp = BlockDiagonalOperator([sp0, sp1, sp2])
        rp = BlockDiagonalOperator([rp0, rp1, rp2])
        U = op.source.make_array([U0, U1, U2])
        V = op.range.make_array([V0, V1, V2])
        return op, _, U, V, sp, rp
    elif n == 9:
        from pymor.operators.block import BlockDiagonalOperator, BlockOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0a, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op0b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op0c, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op0 = (op0a * ProjectionParameterFunctional('p', 3, 0) +
               op0b * ProjectionParameterFunctional('p', 3, 1) +
               op0c * ProjectionParameterFunctional('p', 3, 1))
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockOperator([[op0, op2], [None, op1]])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = BlockDiagonalOperator([rp0, rp1])
        U = op.source.make_array([U0, U1])
        V = op.range.make_array([V0, V1])
        return op, mu, U, V, sp, rp
    elif n == 10:
        from pymor.operators.block import BlockDiagonalOperator, BlockColumnOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockColumnOperator([op2, op1])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = sp1
        rp = BlockDiagonalOperator([rp0, rp1])
        U = U1
        V = op.range.make_array([V0, V1])
        return op, mu, U, V, sp, rp
    elif n == 11:
        from pymor.operators.block import BlockDiagonalOperator, BlockRowOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockRowOperator([op0, op2])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = rp0
        U = op.source.make_array([U0, U1])
        V = V0
        return op, mu, U, V, sp, rp
    else:
        assert False