FEniCS / FEniCS/dolfinx

Add function to remove (explicit) zero entries from a CSR matrix

Open
#2,700 3 comments 0 reactions 1 assignee Claimed by @chrisrichardson View on GitHub
enhancement
Dominant language
C++
Stars
1.2k
Forks
261
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Describe new/missing feature

Explicit zero values can be added to the entries of CSR matrix.
For some types of elements, the element tensor is not dense, but the sparsity pattern doesn't have this information.

Example using scipy:
``` python3
V = dolfinx.fem.FunctionSpace(mesh, element)
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx + ufl.inner(u, v) * ufl.dx
a = dolfinx.fem.form(a, jit_options=jit_options)
A = dolfinx.fem.assemble_matrix(a)

# Convert to scipy sparse matrix
from scipy.sparse import csr_matrix
D = csr_matrix((A.data, A.indices, A.indptr))

print(D.shape, D.nnz, D.nnz/(D.shape[0]*D.shape[1]) * 100, "%")

D.data[np.abs(D.data) < 1e-15] = 0
D.eliminate_zeros()
D.prune()

print(D.shape, D.nnz, D.nnz/(D.shape[0]*D.shape[1]) * 100, "%")
```
Output example:
```
# Before
(64, 64) 512 12.5 %
# After
(64, 64) 256 6.25 %
```

Maybe a similar function should be implemented for `MatrixCSR.h`.

### Suggestion user interface

```c++
class MatrixCSR
{
...
...

void eliminate_zeros(Scalar tol)
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.