Skip to content

Commit f916e4a

Browse files
committed
Added method for integrating across whole interval in Chebychev
discretization
1 parent 981a498 commit f916e4a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pySDC/helpers/spectral_helper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ def get_differentiation_matrix(self):
161161
def get_integration_matrix(self):
162162
raise NotImplementedError()
163163

164+
def get_integration_weights(self):
165+
"""Weights for integration across entire domain"""
166+
raise NotImplementedError()
167+
164168
def get_wavenumbers(self):
165169
"""
166170
Get the grid in spectral space
@@ -379,6 +383,15 @@ def get_integration_matrix(self, lbnd=0):
379383
raise NotImplementedError(f'This function allows to integrate only from x=0, you attempted from x={lbnd}.')
380384
return S
381385

386+
def get_integration_weights(self):
387+
n = self.xp.arange(self.N, dtype=float)
388+
389+
weights = (-1) ** n + 1
390+
weights[2:] /= 1 - (n**2)[2:]
391+
392+
weights /= 2 / self.L
393+
return weights
394+
382395
def get_differentiation_matrix(self, p=1):
383396
'''
384397
Keep in mind that the T2T differentiation matrix is dense.

pySDC/tests/test_helpers/test_spectral_helper_1d_chebychev.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,35 @@ def test_integration_matrix(N):
139139
assert np.allclose(exact.coef[:-1], du)
140140

141141

142+
@pytest.mark.base
143+
@pytest.mark.parametrize('x0', [-1, 0])
144+
@pytest.mark.parametrize('x1', [0.789, 1])
145+
@pytest.mark.parametrize('N', [4, 7])
146+
def test_integral_whole_interval(x0, x1, N):
147+
import numpy as np
148+
from pySDC.helpers.spectral_helper import ChebychevHelper
149+
from qmat.lagrange import LagrangeApproximation
150+
151+
cheby = ChebychevHelper(N, x0=x0, x1=x1)
152+
x = cheby.get_1dgrid()
153+
154+
coeffs = np.random.random(N)
155+
coeffs[-1] = 0
156+
157+
u_hat = coeffs
158+
u = cheby.itransform(u_hat)
159+
160+
weights = cheby.get_integration_weights()
161+
integral = weights @ u_hat
162+
163+
# generate a reference solution with qmat
164+
lag = LagrangeApproximation(points=x)
165+
Q = lag.getIntegrationMatrix(intervals=[(x0, x1)])
166+
integral_ref = (Q @ u)[0]
167+
168+
assert np.isclose(integral, integral_ref)
169+
170+
142171
@pytest.mark.base
143172
@pytest.mark.parametrize('N', [4])
144173
@pytest.mark.parametrize('d', [1, 2, 3])

0 commit comments

Comments
 (0)