Skip to content

Commit e1999ef

Browse files
committed
Merge remote-tracking branch 'origin/master' into neuralpint
2 parents 3b49d5f + da7a177 commit e1999ef

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

pySDC/implementations/convergence_controller_classes/check_convergence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def check_convergence(S, self=None):
7373

7474
# get residual and check against prescribed tolerance (plus check number of iterations)
7575
iter_converged = S.status.iter >= S.params.maxiter
76-
res_converged = L.status.residual <= L.params.restol
76+
res_converged = L.status.residual <= L.params.restol and (S.status.iter > 0 or L.status.sweep > 0)
7777
e_tol_converged = (
7878
L.status.increment < L.params.e_tol if (L.params.get('e_tol') and L.status.get('increment')) else False
7979
)

pySDC/implementations/problem_classes/RayleighBenard.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(
156156
)
157157
self.add_BC(component='T', equation='T', axis=1, x=-1, v=self.BCs['T_bottom'], kind='Dirichlet', line=-1)
158158
self.add_BC(component='T', equation='T', axis=1, x=1, v=self.BCs['T_top'], kind='Dirichlet', line=-2)
159-
self.add_BC(component='v', equation='v', axis=1, x=1, v=self.BCs['v_bottom'], kind='Dirichlet', line=-1)
159+
self.add_BC(component='v', equation='v', axis=1, x=1, v=self.BCs['v_top'], kind='Dirichlet', line=-1)
160160
self.add_BC(component='v', equation='v', axis=1, x=-1, v=self.BCs['v_bottom'], kind='Dirichlet', line=-2)
161161
self.remove_BC(component='v', equation='v', axis=1, x=-1, kind='Dirichlet', line=-2, scalar=True)
162162
self.add_BC(component='u', equation='u', axis=1, v=self.BCs['u_top'], x=1, kind='Dirichlet', line=-2)
@@ -264,6 +264,45 @@ def u_exact(self, t=0, noise_level=1e-3, seed=99):
264264
else:
265265
return me
266266

267+
def apply_BCs(self, sol):
268+
"""
269+
Enforce the Dirichlet BCs at the top and bottom for arbitrary solution.
270+
The function modifies the last two modes of u, v, and T in order to achieve this.
271+
Note that the pressure is not modified here and the Nyquist mode is not altered either.
272+
273+
Args:
274+
sol: Some solution that does not need to enforce boundary conditions
275+
276+
Returns:
277+
Modified version of the solution that satisfies Dirichlet BCs.
278+
"""
279+
ultraspherical = self.spectral.axes[-1]
280+
281+
if self.spectral_space:
282+
sol_half_hat = self.itransform(sol, axes=(-2,))
283+
else:
284+
sol_half_hat = self.transform(sol, axes=(-1,))
285+
286+
BC_bottom = ultraspherical.get_BC(x=-1, kind='dirichlet')
287+
BC_top = ultraspherical.get_BC(x=1, kind='dirichlet')
288+
289+
M = np.array([BC_top[-2:], BC_bottom[-2:]])
290+
M_I = np.linalg.inv(M)
291+
rhs = np.empty((2, self.nx), dtype=complex)
292+
for component in ['u', 'v', 'T']:
293+
i = self.index(component)
294+
rhs[0] = self.BCs[f'{component}_top'] - self.xp.sum(sol_half_hat[i, :, :-2] * BC_top[:-2], axis=1)
295+
rhs[1] = self.BCs[f'{component}_bottom'] - self.xp.sum(sol_half_hat[i, :, :-2] * BC_bottom[:-2], axis=1)
296+
297+
BC_vals = M_I @ rhs
298+
299+
sol_half_hat[i, :, -2:] = BC_vals.T
300+
301+
if self.spectral_space:
302+
return self.transform(sol_half_hat, axes=(-2,))
303+
else:
304+
return self.itransform(sol_half_hat, axes=(-1,))
305+
267306
def get_fig(self): # pragma: no cover
268307
"""
269308
Get a figure suitable to plot the solution of this problem

pySDC/tests/test_problems/test_RayleighBenard.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,36 @@ def test_Nyquist_mode_elimination():
286286
assert np.allclose(u[:, Nyquist_mode_index, :], 0)
287287

288288

289+
@pytest.mark.mpi4py
290+
def test_apply_BCs():
291+
from pySDC.implementations.problem_classes.RayleighBenard import RayleighBenard
292+
import numpy as np
293+
294+
BCs = {
295+
'u_top': np.random.rand(),
296+
'u_bottom': np.random.rand(),
297+
'v_top': np.random.rand(),
298+
'v_bottom': np.random.rand(),
299+
'T_top': np.random.rand(),
300+
'T_bottom': np.random.rand(),
301+
}
302+
P = RayleighBenard(nx=5, nz=2**2, BCs=BCs)
303+
304+
u_in = P.u_init
305+
u_in[...] = np.random.rand(*u_in.shape)
306+
u_in_hat = P.transform(u_in)
307+
308+
u_hat = P.apply_BCs(u_in_hat)
309+
u = P.itransform(u_hat)
310+
311+
P.check_BCs(u)
312+
313+
289314
if __name__ == '__main__':
290315
# test_eval_f(2**0, 2**2, 'z', True)
291316
# test_Poisson_problem(1, 'T')
292-
test_Poisson_problem_v()
317+
# test_Poisson_problem_v()
318+
test_apply_BCs()
293319
# test_Nusselt_numbers(1)
294320
# test_buoyancy_computation()
295321
# test_viscous_dissipation()

0 commit comments

Comments
 (0)