Skip to content

Commit 76cfa15

Browse files
Added separate function to apply Dirichlet BCs to any solution in RBC (#508)
1 parent 0518f26 commit 76cfa15

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

pySDC/implementations/problem_classes/RayleighBenard.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def __init__(
152152
)
153153
self.add_BC(component='T', equation='T', axis=1, x=-1, v=self.BCs['T_bottom'], kind='Dirichlet', line=-1)
154154
self.add_BC(component='T', equation='T', axis=1, x=1, v=self.BCs['T_top'], kind='Dirichlet', line=-2)
155-
self.add_BC(component='v', equation='v', axis=1, x=1, v=self.BCs['v_bottom'], kind='Dirichlet', line=-1)
155+
self.add_BC(component='v', equation='v', axis=1, x=1, v=self.BCs['v_top'], kind='Dirichlet', line=-1)
156156
self.add_BC(component='v', equation='v', axis=1, x=-1, v=self.BCs['v_bottom'], kind='Dirichlet', line=-2)
157157
self.remove_BC(component='v', equation='v', axis=1, x=-1, kind='Dirichlet', line=-2, scalar=True)
158158
self.add_BC(component='u', equation='u', axis=1, v=self.BCs['u_top'], x=1, kind='Dirichlet', line=-2)
@@ -257,6 +257,45 @@ def u_exact(self, t=0, noise_level=1e-3, seed=99):
257257
else:
258258
return me
259259

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