KikeM / romtime

Unsteady Finite Element Reduced Order Model for Time Moving Domains
1 stars 1 forks source link

BUG : Matrix topology (rows and columns) determination #4

Closed KikeM closed 3 years ago

KikeM commented 3 years ago

There is a bug in the determination of the rows and the columns.

def get_matrix_topology(self, mu, t):
    """Get mesh rows and columns arrangement.

    Parameters
    ----------
    mu : dict
    t : float

    Returns
    -------
    rows : np.array
    cols : np.array
    """

    Ah = self._assemble_matrix(mu, t)
    Ah = eliminate_zeros(Ah)
    Ah_dense = Ah.todense()
    rows, cols, _ = get_nonzero_entries(Ah)

    if self.name == OperatorType.CONVECTION:
        _list = list(zip(rows, cols, Ah.data))
        _list = sorted(_list, key=lambda x: x[0])

        _true_list = []
        for row in range(Ah_dense.shape[0]):
            for col in range(Ah_dense.shape[0]):
                value = Ah_dense[row, col]
                if np.isclose(value, 0.0):
                    continue
                else:
                    _true_list.append((row, col, value))

        breakpoint()

    return rows, cols
(Pdb++) pp _list
[(0, 0, 1.0),
 (1, 0, 0.03928432036567021),
 (1, 1, -0.011224091533048632),
 (1, 2, 0.02244818306609726), <---------------------------
 (2, 1, -0.028060228832621575),
 (2, 2, -0.011224091533048628),
 (2, 3, -0.011224091533048632),
 (3, 3, 1.0)]
(Pdb++) pp _true_list
[(0, 0, 1.0),
 (1, 0, 0.03928432036567021),
 (1, 1, -0.011224091533048632),
 (1, 2, -0.028060228832621575), <---------------------------
 (2, 1, 0.02244818306609726),
 (2, 2, -0.011224091533048628),
 (2, 3, -0.011224091533048632),
 (3, 3, 1.0)]
(Pdb++) Ah_dense
matrix([[ 1.        ,  0.        ,  0.        ,  0.        ],
        [ 0.03928432, -0.01122409, -0.02806023,  0.        ],
        [ 0.        ,  0.02244818, -0.01122409, -0.01122409],
        [ 0.        ,  0.        ,  0.        ,  1.        ]])

Originally posted by @KikeM in https://github.com/KikeM/msc-thesis/issues/52#issuecomment-873440801

KikeM commented 3 years ago

El problema está en csr_matrix.data, no es equivalente con lo que devuelve scipy.sparse.find:

from scipy.sparse import csr_matrix, find

A = csr_matrix(
    [
        [7.0, 8.0, 0.0],
        [-2.0, 0.0, 9.0],
        [0.0, 0.0, 9.0],
    ]
)
rows, cols, values = find(A)
A_rt = csr_matrix((values, (rows, cols)))

# (Pdb++) values
# array([ 7., -2.,  8.,  9.,  9.])
# (Pdb++) A.data
# array([ 7.,  8., -2.,  9.,  9.])

# (Pdb++) A.todense()
# matrix([[ 7.,  8.,  0.],
#         [-2.,  0.,  9.],
#         [ 0.,  0.,  9.]])
# (Pdb++) A_rt.todense()
# matrix([[ 7.,  8.,  0.],
#         [-2.,  0.,  9.],
#         [ 0.,  0.,  9.]])
  1. La matrix en formato CSR se reconstruye correctamente.
  2. No hay correspondencia entre el orden de las filas, las columnas, y el .data.