Skip to content

Commit 5099de8

Browse files
Neumann BCs in Chebychev and ultraspherical bases (#587)
* Implemented Neumann BCs in Chebychev base * Fix typo
1 parent 5e67e05 commit 5099de8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pySDC/helpers/spectral_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ def get_BC(self, kind, **kwargs):
535535
return self.get_integ_BC_row(**kwargs)
536536
elif kind.lower() == 'dirichlet':
537537
return self.get_Dirichlet_BC_row(**kwargs)
538+
elif kind.lower() == 'neumann':
539+
return self.get_Neumann_BC_row(**kwargs)
538540
else:
539541
return super().get_BC(kind)
540542

@@ -574,6 +576,27 @@ def get_Dirichlet_BC_row(self, x):
574576
else:
575577
raise NotImplementedError(f'Don\'t know how to generate Dirichlet BC\'s at {x=}!')
576578

579+
def get_Neumann_BC_row(self, x):
580+
"""
581+
Get a row for generating Neumann BCs at x with T polynomials.
582+
583+
Args:
584+
x (float): Position of the boundary condition
585+
586+
Returns:
587+
self.xp.ndarray: Row to put into a matrix
588+
"""
589+
n = self.xp.arange(self.N, dtype='D')
590+
nn = n**2
591+
if x == -1:
592+
me = nn
593+
me[1:] *= (-1) ** n[:-1]
594+
return me
595+
elif x == 1:
596+
return nn
597+
else:
598+
raise NotImplementedError(f'Don\'t know how to generate Neumann BC\'s at {x=}!')
599+
577600
def get_Dirichlet_recombination_matrix(self):
578601
'''
579602
Get matrix for Dirichlet recombination, which changes the basis to have sparse boundary conditions.

pySDC/tests/test_helpers/test_spectral_helper_1d_chebychev.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,5 +419,25 @@ def test_tau_method2D_diffusion(nz, nx, bc_val, plotting=False):
419419
), f'Solution is incorrectly transformed back to real space at x={x[i]}'
420420

421421

422-
if __name__ == '__main__':
423-
test_transform_cupy()
422+
@pytest.mark.base
423+
@pytest.mark.parametrize('N', [4, 7, 32])
424+
@pytest.mark.parametrize('x', [-1, 1])
425+
@pytest.mark.parametrize('v', [2, 3])
426+
def test_Neumann_BCs(N, x, v):
427+
from pySDC.helpers.spectral_helper import ChebychevHelper
428+
import numpy as np
429+
430+
helper = ChebychevHelper(N)
431+
432+
BC = helper.get_BC('Neumann', x=x)
433+
434+
grid = helper.get_1dgrid()
435+
u = grid**v
436+
437+
u_hat = helper.transform(u)
438+
439+
value_at_BC = np.sum(u_hat * BC, axis=0)
440+
if v % 2 == 1:
441+
assert np.isclose(value_at_BC, v)
442+
else:
443+
assert np.isclose(value_at_BC, x * v)

0 commit comments

Comments
 (0)