diff --git a/tests/test_waves.py b/tests/test_waves.py index 42d17303..08f83f98 100644 --- a/tests/test_waves.py +++ b/tests/test_waves.py @@ -148,129 +148,129 @@ def test_only_one_value(self, elevation, freq): # check that now the entire array is zero. assert np.allclose(elev0.values, 0.0+0.0j) - -class TestLongCrestedWave: - """Test function :python:`waves.long_crested_wave`.""" - - @pytest.fixture(scope="class") - def ndbc_omnidirectional(self,): - """Omnidirectional spectrum from NDBC data interpolated at - desired frequencies. - """ - f1 = 0.02 - time = '2020-01-01T01:40:00.000000000' - nfreq = 24 - freq = wot.frequency(f1, nfreq, False) - dir = os.path.join(os.path.dirname(__file__), 'data', 'ndbc') - spec = ws.read_ndbc_ascii(os.path.join(dir, '41013w2020.txt')) - return spec.sel(time=time).interp(freq=freq) - - @pytest.fixture(scope="class") - def nfreq(self, ndbc_omnidirectional): - """Number of wave frequencies.""" - return len(ndbc_omnidirectional.freq) - - @pytest.fixture(scope="class") - def ndir(self, ndbc_omnidirectional): - """Number of wave directions.""" - return len(ndbc_omnidirectional.dir) - - @pytest.fixture(scope="class") - def direction(self, ndbc_omnidirectional, ndir): - """Wave direction.""" - return ndbc_omnidirectional.dir.values[np.random.randint(0, ndir)] - - @pytest.fixture(scope="class") - def nrealizations(self): - """Number of wave realizations.""" - return 2 - - @pytest.fixture(scope="class") - def elevation(self, ndbc_omnidirectional, direction, nrealizations): - """Complex sea state elevation amplitude [m] indexed by - frequency and direction.""" - elev = wot.waves.long_crested_wave( - ndbc_omnidirectional.efth, nrealizations, direction) - return elev - - @pytest.fixture(scope="class") - def pm_f1(self,): - """Fundamental frequency for the Pierson-Moskowitz spectrum.""" - return 0.05 - - @pytest.fixture(scope="class") - def pm_nfreq(self,): - """Number of frequencies for the Pierson-Moskowitz spectrum.""" - return 100 - - @pytest.fixture(scope="class") - def pm_hs(self,): - """Significant wave height for Pierson-Moskowitz spectrum.""" - return 5.0 - - @pytest.fixture(scope="class") - def pm_spectrum(self, pm_f1, pm_nfreq, pm_hs): - """Pierson-Moskowitz spectrum.""" - Tp = 1.2 - Hs = pm_hs - - def spectrum_func(f): - return wot.waves.pierson_moskowitz_spectrum(f, fp=1/Tp, hs=Hs) - spectrum_name = f"Pierson-Moskowitz ({Tp}s, {Hs}m)" - - efth_xr = wot.waves.omnidirectional_spectrum( - pm_f1, pm_nfreq, spectrum_func, spectrum_name - ) - return efth_xr - - def test_coordinates(self, elevation): - """Test that the elevation dataArray has the correct - coordinates. - """ - coordinates = ['wave_direction', 'omega', 'freq', 'realization'] - for icoord in coordinates: - assert icoord in elevation.coords, f'missing coordinate: {icoord}' - - def test_shape(self, elevation, nfreq, ndir, nrealizations): - """Test that the elevation dataArray has the correct shape.""" - assert np.squeeze(elevation.values).shape == (nfreq, nrealizations) - - def test_type(self, elevation): - """Test that the elevation dataArray has the correct type.""" - assert np.iscomplexobj(elevation) - - def test_direction(self, elevation, direction): - """Test that the wave direction is correct.""" - dir_out = elevation.wave_direction.values.item() - assert np.isclose(dir_out, wot.degrees_to_radians(direction)) - - def test_realizations(self, elevation): - """Test that the number of realizations is correct.""" - realization_out = elevation.realization.values - assert (realization_out == [0,1]).all() - - def test_spectrum(self, pm_spectrum, pm_hs): - """Test that the constructed spectrum has the expected Hs.""" - efth = ws.SpecArray(pm_spectrum) - assert np.isclose(pm_hs, efth.hs().values) - - def test_time_series(self, pm_spectrum, pm_f1, pm_nfreq): - """Test that the created time series has the desired spectrum.""" - # create time-series - direction = 0.0 - nrealizations = 1 - wave = wot.waves.long_crested_wave(pm_spectrum, nrealizations, direction) - wave_ts = wot.fd_to_td(wave.sel(realization=0).values, pm_f1, pm_nfreq, False) - # calculate the spectrum from the time-series - t = wot.time(pm_f1, pm_nfreq) - fs = 1/t[1] - nnft = len(t) - [_, S_data] = signal.welch( - wave_ts.squeeze(), fs=fs, window='boxcar', nperseg=nnft, nfft=nnft, - noverlap=0 - ) - # check it is equal to the original spectrum - assert np.allclose(S_data[1:-1], pm_spectrum.values.squeeze()[:-1]) +# TODO - remove +# class TestLongCrestedWave: +# """Test function :python:`waves.long_crested_wave`.""" + +# @pytest.fixture(scope="class") +# def ndbc_omnidirectional(self,): +# """Omnidirectional spectrum from NDBC data interpolated at +# desired frequencies. +# """ +# f1 = 0.02 +# time = '2020-01-01T01:40:00.000000000' +# nfreq = 24 +# freq = wot.frequency(f1, nfreq, False) +# dir = os.path.join(os.path.dirname(__file__), 'data', 'ndbc') +# spec = ws.read_ndbc_ascii(os.path.join(dir, '41013w2020.txt')) +# return spec.sel(time=time).interp(freq=freq) + +# @pytest.fixture(scope="class") +# def nfreq(self, ndbc_omnidirectional): +# """Number of wave frequencies.""" +# return len(ndbc_omnidirectional.freq) + +# @pytest.fixture(scope="class") +# def ndir(self, ndbc_omnidirectional): +# """Number of wave directions.""" +# return len(ndbc_omnidirectional.dir) + +# @pytest.fixture(scope="class") +# def direction(self, ndbc_omnidirectional, ndir): +# """Wave direction.""" +# return ndbc_omnidirectional.dir.values[np.random.randint(0, ndir)] + +# @pytest.fixture(scope="class") +# def nrealizations(self): +# """Number of wave realizations.""" +# return 2 + +# @pytest.fixture(scope="class") +# def elevation(self, ndbc_omnidirectional, direction, nrealizations): +# """Complex sea state elevation amplitude [m] indexed by +# frequency and direction.""" +# elev = wot.waves.long_crested_wave( +# ndbc_omnidirectional.efth, nrealizations, direction) +# return elev + +# @pytest.fixture(scope="class") +# def pm_f1(self,): +# """Fundamental frequency for the Pierson-Moskowitz spectrum.""" +# return 0.05 + +# @pytest.fixture(scope="class") +# def pm_nfreq(self,): +# """Number of frequencies for the Pierson-Moskowitz spectrum.""" +# return 100 + +# @pytest.fixture(scope="class") +# def pm_hs(self,): +# """Significant wave height for Pierson-Moskowitz spectrum.""" +# return 5.0 + +# @pytest.fixture(scope="class") +# def pm_spectrum(self, pm_f1, pm_nfreq, pm_hs): +# """Pierson-Moskowitz spectrum.""" +# Tp = 1.2 +# Hs = pm_hs + +# def spectrum_func(f): +# return wot.waves.pierson_moskowitz_spectrum(f, fp=1/Tp, hs=Hs) +# spectrum_name = f"Pierson-Moskowitz ({Tp}s, {Hs}m)" + +# efth_xr = wot.waves.omnidirectional_spectrum( +# pm_f1, pm_nfreq, spectrum_func, spectrum_name +# ) +# return efth_xr + +# def test_coordinates(self, elevation): +# """Test that the elevation dataArray has the correct +# coordinates. +# """ +# coordinates = ['wave_direction', 'omega', 'freq', 'realization'] +# for icoord in coordinates: +# assert icoord in elevation.coords, f'missing coordinate: {icoord}' + +# def test_shape(self, elevation, nfreq, ndir, nrealizations): +# """Test that the elevation dataArray has the correct shape.""" +# assert np.squeeze(elevation.values).shape == (nfreq, nrealizations) + +# def test_type(self, elevation): +# """Test that the elevation dataArray has the correct type.""" +# assert np.iscomplexobj(elevation) + +# def test_direction(self, elevation, direction): +# """Test that the wave direction is correct.""" +# dir_out = elevation.wave_direction.values.item() +# assert np.isclose(dir_out, wot.degrees_to_radians(direction)) + +# def test_realizations(self, elevation): +# """Test that the number of realizations is correct.""" +# realization_out = elevation.realization.values +# assert (realization_out == [0,1]).all() + +# def test_spectrum(self, pm_spectrum, pm_hs): +# """Test that the constructed spectrum has the expected Hs.""" +# efth = ws.SpecArray(pm_spectrum) +# assert np.isclose(pm_hs, efth.hs().values) + +# def test_time_series(self, pm_spectrum, pm_f1, pm_nfreq): +# """Test that the created time series has the desired spectrum.""" +# # create time-series +# direction = 0.0 +# nrealizations = 1 +# wave = wot.waves.long_crested_wave(pm_spectrum, nrealizations, direction) +# wave_ts = wot.fd_to_td(wave.sel(realization=0).values, pm_f1, pm_nfreq, False) +# # calculate the spectrum from the time-series +# t = wot.time(pm_f1, pm_nfreq) +# fs = 1/t[1] +# nnft = len(t) +# [_, S_data] = signal.welch( +# wave_ts.squeeze(), fs=fs, window='boxcar', nperseg=nnft, nfft=nnft, +# noverlap=0 +# ) +# # check it is equal to the original spectrum +# assert np.allclose(S_data[1:-1], pm_spectrum.values.squeeze()[:-1]) class TestIrregularWave: diff --git a/wecopttool/waves.py b/wecopttool/waves.py index 5335e8ff..560c38ee 100644 --- a/wecopttool/waves.py +++ b/wecopttool/waves.py @@ -109,7 +109,7 @@ def elevation_fd( if amplitudes is None: amplitudes = np.zeros([nfreq, ndirections, nrealizations]) else: - assert amplitudes.shape == (nfreq, ndirections, nrealizations) + assert amplitudes.shape == (nfreq, ndirections, 1) if phases is None: phases = random_phase([nfreq, ndirections, nrealizations], seed) @@ -118,6 +118,7 @@ def elevation_fd( assert phases.shape == (nfreq, ndirections, nrealizations) camplitude = amplitudes * np.exp(1j*phases) + camplitude.shape == (nfreq, ndirections, nrealizations) attr = {} if attr is None else attr attrs = {'units': 'm', 'long_name': 'Wave elevation'} | attr