Skip to content

Commit 20deee4

Browse files
simmsassolson
authored andcommitted
Improve surface_elevation internal method selection (#340)
* Test: Determine method using input frequency index * Feat: Use sum of sines if ifft is not computable This change allows `surface_elevation` to return a result if the user inputs a spectrum with a frequency index that does not have a zero frequency. If the non zero frequency index condition is found when the method is `ifft` we warn the user and change the method to `sum_of_sines` * Fix: Use previously found frequency index S.index may not exist for some input datasets, but f[0] does and we should use the value of f[0] here. * Test: Warn when using ifft with a non zero frequency * Lint
1 parent 4cdbe03 commit 20deee4

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

mhkit/tests/wave/test_resource_spectrum.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pandas as pd
99
import numpy as np
1010
import unittest
11+
import pytest
1112
import os
1213

1314

@@ -158,6 +159,36 @@ def test_ifft_sum_of_sines(self):
158159

159160
assert_allclose(eta_ifft, eta_sos)
160161

162+
def test_surface_elevation_uses_sum_of_sines_when_input_frequency_index_does_not_have_zero(
163+
self,
164+
):
165+
f = np.linspace(1 / 30, 1 / 2, 32)
166+
S = wave.resource.jonswap_spectrum(f, self.Tp, self.Hs)
167+
168+
eta_default = wave.resource.surface_elevation(S, self.t, seed=1)
169+
eta_sos = wave.resource.surface_elevation(
170+
S, self.t, seed=1, method="sum_of_sines"
171+
)
172+
173+
assert_allclose(eta_default, eta_sos)
174+
175+
def test_surface_elevation_warn_user_if_zero_frequency_not_defined_and_using_ifft(
176+
self,
177+
):
178+
f = np.linspace(1 / 30, 1 / 2, 32)
179+
S = wave.resource.jonswap_spectrum(f, self.Tp, self.Hs)
180+
181+
with pytest.warns(UserWarning):
182+
wave.resource.surface_elevation(S, self.t, seed=1, method="ifft")
183+
184+
def test_surface_elevation_uses_ifft_when_input_frequency_index_has_zero(self):
185+
S = wave.resource.jonswap_spectrum(self.f, self.Tp, self.Hs)
186+
187+
eta_default = wave.resource.surface_elevation(S, self.t, seed=1)
188+
eta_ifft = wave.resource.surface_elevation(S, self.t, seed=1, method="ifft")
189+
190+
assert_allclose(eta_default, eta_ifft)
191+
161192
def test_plot_spectrum(self):
162193
filename = abspath(join(plotdir, "wave_plot_spectrum.png"))
163194
if isfile(filename):

mhkit/wave/resource.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from scipy.optimize import fsolve as _fsolve
24
from scipy import signal as _signal
35
import pandas as pd
@@ -335,9 +337,10 @@ def surface_elevation(
335337

336338
if method == "ifft":
337339
if not f[0] == 0:
338-
raise ValueError(
339-
f"ifft method must have zero frequency defined. Lowest frequency is: {S.index.values[0]}"
340+
warnings.warn(
341+
f"ifft method must have zero frequency defined. Lowest frequency is: {f[0].values}. Setting method to less efficient `sum_of_sines` method."
340342
)
343+
method = "sum_of_sines"
341344

342345
if frequency_bins is None:
343346
delta_f = f.values[1] - f.values[0]

0 commit comments

Comments
 (0)