Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sqrt option to diagonal method of stencil/block matrices #434

Merged
merged 14 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
- name: Set default MPI and HDF5 C compilers on Ubuntu
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install --reinstall openmpi-bin libhdf5-openmpi-dev

- name: Install non-Python dependencies on macOS
Expand Down
9 changes: 7 additions & 2 deletions psydac/linalg/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,18 @@ def __sub__(self, M):
#--------------------------------------
# New properties/methods
#--------------------------------------
def diagonal(self, *, inverse = False, out = None):
def diagonal(self, *, inverse = False, sqrt = False, out = None):
"""Get the coefficients on the main diagonal as another BlockLinearOperator object.

Parameters
----------
inverse : bool
If True, get the inverse of the diagonal. (Default: False).
max-models marked this conversation as resolved.
Show resolved Hide resolved
Can be combined with sqrt to get the inverse square root.

sqrt : bool
If True, get the square root of the diagonal. (Default: False).
Can be combined with inverse to get the inverse square root.

out : BlockLinearOperator
If provided, write the diagonal entries into this matrix. (Default: None).
Expand Down Expand Up @@ -815,7 +820,7 @@ def diagonal(self, *, inverse = False, out = None):
# Store the diagonal (or its inverse) into `out`
for i, j in self.nonzero_block_indices:
if i == j:
out[i, i] = self[i, i].diagonal(inverse = inverse, out = out[i, i])
out[i, i] = self[i, i].diagonal(inverse = inverse, sqrt = sqrt, out = out[i, i])

return out

Expand Down
10 changes: 9 additions & 1 deletion psydac/linalg/stencil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,14 +1372,19 @@ def _exchange_assembly_data_serial(self):
self._data[idx_to] += self._data[idx_from]

# ...
def diagonal(self, *, inverse = False, out = None):
def diagonal(self, *, inverse = False, sqrt = False, out = None):
"""
Get the coefficients on the main diagonal as a StencilDiagonalMatrix object.

Parameters
----------
inverse : bool
If True, get the inverse of the diagonal. (Default: False).
max-models marked this conversation as resolved.
Show resolved Hide resolved
Can be combined with sqrt to get the inverse square root.

sqrt : bool
If True, get the square root of the diagonal. (Default: False).
Can be combined with inverse to get the inverse square root.

out : StencilDiagonalMatrix
If provided, write the diagonal entries into this matrix. (Default: None).
Expand Down Expand Up @@ -1418,6 +1423,9 @@ def diagonal(self, *, inverse = False, out = None):
else:
data = diag.copy()

if sqrt:
np.sqrt(data, out=data)

# If needed create a new StencilDiagonalMatrix object
if out is None:
out = StencilDiagonalMatrix(V, W, data)
Expand Down
9 changes: 8 additions & 1 deletion psydac/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ def test_in_place_operations(n1, n2, p1, p2, P1=False, P2=False):
@pytest.mark.parametrize('n2', n2array)
@pytest.mark.parametrize('p1', p1array)
@pytest.mark.parametrize('p2', p2array)
@pytest.mark.parametrize('sqrt', [False, True])

def test_inverse_transpose_interaction(n1, n2, p1, p2, P1=False, P2=False):
def test_inverse_transpose_interaction(n1, n2, p1, p2, sqrt, P1=False, P2=False):
max-models marked this conversation as resolved.
Show resolved Hide resolved

# 1. Initiate square LOs: S (V->V, StencilMatrix), S1 (W->W, StencilMatrix)
#Initiate BlockLO: B (VxW -> VxW) and a BlockVector u element of VxW
Expand Down Expand Up @@ -618,6 +619,12 @@ def test_inverse_transpose_interaction(n1, n2, p1, p2, P1=False, P2=False):
### -1,T & T,-1 --- -1,T,T --- -1,T,-1 --- T,-1,-1 --- T,-1,T (the combinations I test)
###

# Square root test
scaled_matrix = B * np.random.random() # Ensure the diagonal elements != 1
diagonal_values = scaled_matrix.diagonal(sqrt=False).toarray()
sqrt_diagonal_values = scaled_matrix.diagonal(sqrt=True).toarray()
assert np.allclose(sqrt_diagonal_values, np.sqrt(diagonal_values))
max-models marked this conversation as resolved.
Show resolved Hide resolved

tol = 1e-5
C = inverse(B, 'cg', tol=tol)
P = B.diagonal(inverse=True)
Expand Down