Skip to content

Commit e951514

Browse files
committed
Added integration weights for Fourier discretization
1 parent f916e4a commit e951514

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

pySDC/helpers/spectral_helper.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ def get_integration_matrix(self, lbnd=0):
384384
return S
385385

386386
def get_integration_weights(self):
387+
"""Weights for integration across entire domain"""
387388
n = self.xp.arange(self.N, dtype=float)
388389

389390
weights = (-1) ** n + 1
@@ -821,6 +822,12 @@ def get_integration_matrix(self, p=1):
821822
k[0] = 1j * self.L
822823
return self.linalg.matrix_power(self.sparse_lib.diags(1 / (1j * k)), p)
823824

825+
def get_integration_weights(self):
826+
"""Weights for integration across entire domain"""
827+
weights = self.xp.zeros(self.N)
828+
weights[0] = self.L / self.N
829+
return weights
830+
824831
def get_plan(self, u, forward, *args, **kwargs):
825832
if self.fft_lib.__name__ == 'mpi4py_fft.fftw':
826833
if 'axes' in kwargs.keys():

pySDC/tests/test_helpers/test_spectral_helper_1d_fft.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ def test_integration_matrix(N, plot=False):
112112
assert np.allclose(expect, Du)
113113

114114

115+
@pytest.mark.base
116+
@pytest.mark.parametrize('x0', [-1, 0])
117+
@pytest.mark.parametrize('x1', [0.789, 1])
118+
@pytest.mark.parametrize('N', [32, 45])
119+
def test_integral_whole_interval(x0, x1, N):
120+
import numpy as np
121+
from pySDC.helpers.spectral_helper import FFTHelper
122+
from qmat.lagrange import LagrangeApproximation
123+
124+
helper = FFTHelper(N, x0=x0, x1=x1)
125+
x = helper.get_1dgrid()
126+
127+
u = np.zeros_like(x)
128+
129+
num_coef = N // 2 - 1
130+
coeffs = np.random.random((2, N))
131+
u += coeffs[0, 0]
132+
for i in range(1, num_coef + 1):
133+
u += coeffs[0, i] * np.sin(2 * np.pi * i * x / helper.L)
134+
u += coeffs[1, i] * np.cos(2 * np.pi * i * x / helper.L)
135+
136+
u_hat = helper.transform(u)
137+
138+
weights = helper.get_integration_weights()
139+
integral = weights @ u_hat
140+
integral_ref = coeffs[0, 0] * helper.L
141+
142+
assert np.isclose(integral, integral_ref, atol=1e-7), abs(integral_ref - integral)
143+
144+
115145
@pytest.mark.base
116146
@pytest.mark.parametrize('N', [4, 32])
117147
@pytest.mark.parametrize('v', [0, 4.78])
@@ -141,4 +171,5 @@ def test_tau_method(N, v):
141171
# test_tau_method(6, 1)
142172
# test_transform(True)
143173
# test_transform(False)
144-
test_transform_cupy(4)
174+
# test_transform_cupy(4)
175+
test_integral_whole_interval(0, 2, 90)

0 commit comments

Comments
 (0)