Beispiel #1
0
def insert_sparse_to_csr(mtx1, mtx2, irs, ics):
    """
    Insert a sparse matrix `mtx2` into a CSR sparse matrix `mtx1` at
    rows `irs` and columns `ics`. The submatrix `mtx1[irs,ics]` must
    already be preallocated and have the same structure as `mtx2`.
    """
    import sfepy.fem.extmods.fem as fem

    if isinstance(irs, slice):
        irs = nm.arange(irs.start, irs.stop, irs.step, dtype=nm.int32)

    if isinstance(ics, slice):
        ics = nm.arange(ics.start, ics.stop, ics.step, dtype=nm.int32)

    n_row, n_col = mtx1.shape

    assert_((irs.min() >= 0) and (irs.max() < n_row))
    assert_((ics.min() >= 0) and (ics.max() < n_col))

    aux = mtx2.tocoo()
    data = nm.ascontiguousarray(aux.data[:,None,None,None])
    rows = irs[aux.row[:,None]]
    cols = ics[aux.col[:,None]]

    iels = nm.arange(rows.shape[0], dtype=nm.int32)
    fem.assemble_matrix(mtx1.data, mtx1.indptr, mtx1.indices, data,
                        iels, 1.0, rows, cols)
Beispiel #2
0
    def assemble_to(self, asm_obj, val, iels, mode="vector", diff_var=None):
        import sfepy.fem.extmods.fem as fem

        vvar = self.get_virtual_variable()
        dc_type = self.get_dof_conn_type()

        if mode == "vector":
            for ii, (ig, _iels) in enumerate(iels):
                vec_in_els = val[ii]
                dc = vvar.get_dof_conn(dc_type, ig, active=True)
                assert_(vec_in_els.shape[2] == dc.shape[1])

                if asm_obj.dtype == nm.float64:
                    fem.assemble_vector(asm_obj, vec_in_els, _iels, 1.0, dc)

                else:
                    assert_(asm_obj.dtype == nm.complex128)
                    fem.assemble_vector_complex(
                        asm_obj.real, asm_obj.imag, vec_in_els.real, vec_in_els.imag, _iels, 1.0, 0.0, dc
                    )

        elif mode == "matrix":
            svar = diff_var
            tmd = (asm_obj.data, asm_obj.indptr, asm_obj.indices)
            for ii, (ig, _iels) in enumerate(iels):
                mtx_in_els = val[ii]
                rdc = vvar.get_dof_conn(dc_type, ig, active=True)

                is_trace = self.arg_traces[svar.name]
                ## print dc_type, ig, is_trace
                cdc = svar.get_dof_conn(dc_type, ig, active=True, is_trace=is_trace)
                if is_trace:
                    # check correct dofs order in the mirror region
                    rgnt = vvar.get_global_node_tab(dc_type, ig)
                    cgnt = svar.get_global_node_tab(dc_type, ig, is_trace=is_trace)
                    cdc = reorder_dofs_on_mirror(cdc, cgnt, rgnt)

                ## print svar.name, cdc.shape
                assert_(mtx_in_els.shape[2:] == (rdc.shape[1], cdc.shape[1]))

                sign = 1.0
                if self.arg_derivatives[svar.name]:
                    sign *= 1.0 / self.dt

                if asm_obj.dtype == nm.float64:
                    fem.assemble_matrix(tmd[0], tmd[1], tmd[2], mtx_in_els, _iels, sign, rdc, cdc)

                else:
                    assert_(asm_obj.dtype == nm.complex128)
                    fem.assemble_matrix_complex(
                        tmd[0].real,
                        tmd[0].imag,
                        tmd[1],
                        tmd[2],
                        mtx_in_els.real,
                        mtx_in_els.imag,
                        _iels,
                        sign,
                        0.0,
                        rdc,
                        cdc,
                    )

        else:
            raise ValueError("unknown assembling mode! (%s)" % mode)