Skip to content

Commit ee2ce6f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into merge_refactor
2 parents 6acf76a + 1879845 commit ee2ce6f

18 files changed

+2278
-21
lines changed

pySDC/helpers/spectral_helper.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,16 +1090,22 @@ def index(self, name):
10901090
else:
10911091
raise NotImplementedError(f'Don\'t know how to compute index for {type(name)=}')
10921092

1093-
def get_empty_operator_matrix(self):
1093+
def get_empty_operator_matrix(self, diag=False):
10941094
"""
10951095
Return a matrix of operators to be filled with the connections between the solution components.
10961096
1097+
Args:
1098+
diag (bool): Whether operator is block-diagonal
1099+
10971100
Returns:
10981101
list containing sparse zeros
10991102
"""
11001103
S = len(self.components)
11011104
O = self.get_Id() * 0
1102-
return [[O for _ in range(S)] for _ in range(S)]
1105+
if diag:
1106+
return [O for _ in range(S)]
1107+
else:
1108+
return [[O for _ in range(S)] for _ in range(S)]
11031109

11041110
def get_BC(self, axis, kind, line=-1, scalar=False, **kwargs):
11051111
"""
@@ -1369,7 +1375,7 @@ def put_BCs_in_rhs(self, rhs):
13691375

13701376
return rhs
13711377

1372-
def add_equation_lhs(self, A, equation, relations):
1378+
def add_equation_lhs(self, A, equation, relations, diag=False):
13731379
"""
13741380
Add the left hand part (that you want to solve implicitly) of an equation to a list of lists of sparse matrices
13751381
that you will convert to an operator later.
@@ -1404,11 +1410,16 @@ def add_equation_lhs(self, A, equation, relations):
14041410
A (list of lists of sparse matrices): The operator to be
14051411
equation (str): The equation of the component you want this in
14061412
relations: (dict): Relations between quantities
1413+
diag (bool): Whether operator is block-diagonal
14071414
"""
14081415
for k, v in relations.items():
1409-
A[self.index(equation)][self.index(k)] = v
1416+
if diag:
1417+
assert k == equation, 'You are trying to put a non-diagonal equation into a diagonal operator'
1418+
A[self.index(equation)] = v
1419+
else:
1420+
A[self.index(equation)][self.index(k)] = v
14101421

1411-
def convert_operator_matrix_to_operator(self, M):
1422+
def convert_operator_matrix_to_operator(self, M, diag=False):
14121423
"""
14131424
Promote the list of lists of sparse matrices to a single sparse matrix that can be used as linear operator.
14141425
See documentation of `SpectralHelper.add_equation_lhs` for an example.
@@ -1420,9 +1431,14 @@ def convert_operator_matrix_to_operator(self, M):
14201431
sparse linear operator
14211432
"""
14221433
if len(self.components) == 1:
1423-
return M[0][0]
1434+
if diag:
1435+
return M[0]
1436+
else:
1437+
return M[0][0]
1438+
elif diag:
1439+
return self.sparse_lib.block_diag(M, format='csc')
14241440
else:
1425-
return self.sparse_lib.bmat(M, format='csc')
1441+
return self.sparse_lib.block_array(M, format='csc')
14261442

14271443
def get_wavenumbers(self):
14281444
"""

pySDC/implementations/problem_classes/RayleighBenard.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,12 @@ def eval_f(self, u, *args, **kwargs):
213213

214214
# start by computing derivatives
215215
if not hasattr(self, '_Dx_expanded') or not hasattr(self, '_Dz_expanded'):
216-
self._Dx_expanded = self._setup_operator({'u': {'u': Dx}, 'v': {'v': Dx}, 'T': {'T': Dx}, 'p': {}})
217-
self._Dz_expanded = self._setup_operator({'u': {'u': Dz}, 'v': {'v': Dz}, 'T': {'T': Dz}, 'p': {}})
216+
self._Dx_expanded = self._setup_operator(
217+
{'u': {'u': Dx}, 'v': {'v': Dx}, 'T': {'T': Dx}, 'p': {}}, diag=True
218+
)
219+
self._Dz_expanded = self._setup_operator(
220+
{'u': {'u': Dz}, 'v': {'v': Dz}, 'T': {'T': Dz}, 'p': {}}, diag=True
221+
)
218222
Dx_u_hat = (self._Dx_expanded @ u_hat.flatten()).reshape(u_hat.shape)
219223
Dz_u_hat = (self._Dz_expanded @ u_hat.flatten()).reshape(u_hat.shape)
220224

pySDC/implementations/problem_classes/generic_spectral.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,21 @@ def __getattr__(self, name):
136136
"""
137137
return getattr(self.spectral, name)
138138

139-
def _setup_operator(self, LHS):
139+
def _setup_operator(self, LHS, diag=False):
140140
"""
141141
Setup a sparse linear operator by adding relationships. See documentation for ``GenericSpectralLinear.setup_L`` to learn more.
142142
143143
Args:
144144
LHS (dict): Equations to be added to the operator
145+
diag (bool): Whether operator is block-diagonal
145146
146147
Returns:
147148
sparse linear operator
148149
"""
149-
operator = self.spectral.get_empty_operator_matrix()
150+
operator = self.spectral.get_empty_operator_matrix(diag=diag)
150151
for line, equation in LHS.items():
151-
self.spectral.add_equation_lhs(operator, line, equation)
152-
return self.spectral.convert_operator_matrix_to_operator(operator)
152+
self.spectral.add_equation_lhs(operator, line, equation, diag=diag)
153+
return self.spectral.convert_operator_matrix_to_operator(operator, diag=diag)
153154

154155
def setup_L(self, LHS):
155156
"""
@@ -171,13 +172,13 @@ def setup_L(self, LHS):
171172
"""
172173
self.L = self._setup_operator(LHS)
173174

174-
def setup_M(self, LHS):
175+
def setup_M(self, LHS, diag=True):
175176
'''
176177
Setup mass matrix, see documentation of ``GenericSpectralLinear.setup_L``.
177178
'''
178179
diff_index = list(LHS.keys())
179180
self.diff_mask = [me in diff_index for me in self.components]
180-
self.M = self._setup_operator(LHS)
181+
self.M = self._setup_operator(LHS, diag=diag)
181182

182183
def setup_preconditioner(self, Dirichlet_recombination=True, left_preconditioner=True):
183184
"""
@@ -192,7 +193,7 @@ def setup_preconditioner(self, Dirichlet_recombination=True, left_preconditioner
192193

193194
Id = sp.eye(N)
194195
Pl_lhs = {comp: {comp: Id} for comp in self.components}
195-
self.Pl = self._setup_operator(Pl_lhs)
196+
self.Pl = self._setup_operator(Pl_lhs, diag=True)
196197

197198
if left_preconditioner:
198199
# reverse Kronecker product
@@ -214,7 +215,7 @@ def setup_preconditioner(self, Dirichlet_recombination=True, left_preconditioner
214215
_Pr = Id
215216

216217
Pr_lhs = {comp: {comp: _Pr} for comp in self.components}
217-
self.Pr = self._setup_operator(Pr_lhs) @ self.Pl.T
218+
self.Pr = self._setup_operator(Pr_lhs, diag=True) @ self.Pl.T
218219

219220
def solve_system(self, rhs, dt, u0=None, *args, skip_itransform=False, **kwargs):
220221
"""

0 commit comments

Comments
 (0)