Skip to content

Commit

Permalink
Improve surface_elevation internal method selection (#340)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
simmsa authored Jul 10, 2024
1 parent da22c8e commit cf877b6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
31 changes: 31 additions & 0 deletions mhkit/tests/wave/test_resource_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import numpy as np
import unittest
import pytest
import os


Expand Down Expand Up @@ -158,6 +159,36 @@ def test_ifft_sum_of_sines(self):

assert_allclose(eta_ifft, eta_sos)

def test_surface_elevation_uses_sum_of_sines_when_input_frequency_index_does_not_have_zero(
self,
):
f = np.linspace(1 / 30, 1 / 2, 32)
S = wave.resource.jonswap_spectrum(f, self.Tp, self.Hs)

eta_default = wave.resource.surface_elevation(S, self.t, seed=1)
eta_sos = wave.resource.surface_elevation(
S, self.t, seed=1, method="sum_of_sines"
)

assert_allclose(eta_default, eta_sos)

def test_surface_elevation_warn_user_if_zero_frequency_not_defined_and_using_ifft(
self,
):
f = np.linspace(1 / 30, 1 / 2, 32)
S = wave.resource.jonswap_spectrum(f, self.Tp, self.Hs)

with pytest.warns(UserWarning):
wave.resource.surface_elevation(S, self.t, seed=1, method="ifft")

def test_surface_elevation_uses_ifft_when_input_frequency_index_has_zero(self):
S = wave.resource.jonswap_spectrum(self.f, self.Tp, self.Hs)

eta_default = wave.resource.surface_elevation(S, self.t, seed=1)
eta_ifft = wave.resource.surface_elevation(S, self.t, seed=1, method="ifft")

assert_allclose(eta_default, eta_ifft)

def test_plot_spectrum(self):
filename = abspath(join(plotdir, "wave_plot_spectrum.png"))
if isfile(filename):
Expand Down
7 changes: 5 additions & 2 deletions mhkit/wave/resource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from scipy.optimize import fsolve as _fsolve
from scipy import signal as _signal
import pandas as pd
Expand Down Expand Up @@ -335,9 +337,10 @@ def surface_elevation(

if method == "ifft":
if not f[0] == 0:
raise ValueError(
f"ifft method must have zero frequency defined. Lowest frequency is: {S.index.values[0]}"
warnings.warn(
f"ifft method must have zero frequency defined. Lowest frequency is: {f[0].values}. Setting method to less efficient `sum_of_sines` method."
)
method = "sum_of_sines"

if frequency_bins is None:
delta_f = f.values[1] - f.values[0]
Expand Down

0 comments on commit cf877b6

Please sign in to comment.