From b56ab0a7d40f65c883fbf130ca51c0e891acef1a Mon Sep 17 00:00:00 2001 From: Mark Bruggemann Date: Thu, 8 Dec 2022 14:10:59 +0000 Subject: [PATCH 1/2] Provide function to convert from Te to Tp using ITTC approximation --- mhkit/tests/wave/test_resource_metrics.py | 22 +++++++++++++++++- mhkit/wave/resource.py | 28 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/mhkit/tests/wave/test_resource_metrics.py b/mhkit/tests/wave/test_resource_metrics.py index 1bef43f12..e927a6157 100644 --- a/mhkit/tests/wave/test_resource_metrics.py +++ b/mhkit/tests/wave/test_resource_metrics.py @@ -201,6 +201,26 @@ def test_moments(self): self.assertLess(error, 0.01) + def test_energy_period_to_peak_period(self): + # This test checks that if we perform the + # Te to Tp conversion, we create a spectrum + # (using Tp) that has the provided Te. + Hs = 2.5 + Te = np.linspace(5, 20, 10) + gamma = np.linspace(1, 7, 7) + + for g in gamma: + for T in Te: + Tp = wave.resource.energy_period_to_peak_period(T, g) + + f = np.linspace(1 / (10 * Tp), 3 / Tp, 100) + S = wave.resource.jonswap_spectrum(f, Tp, Hs, g) + + Te_calc = wave.resource.energy_period(S).values[0][0] + + error = np.abs(T - Te_calc)/Te_calc + self.assertLess(error, 0.01) + def test_metrics(self): for file_i in self.valdata2.keys(): # for each file MC, AH, CDiP @@ -356,6 +376,6 @@ def test_plot_monthly_cumulative_distribution(self): self.assertTrue(isfile(filename)) - + if __name__ == '__main__': unittest.main() diff --git a/mhkit/wave/resource.py b/mhkit/wave/resource.py index 07ecc69d1..12ef30d64 100644 --- a/mhkit/wave/resource.py +++ b/mhkit/wave/resource.py @@ -591,6 +591,34 @@ def energy_flux(S, h, deep=False, rho=1025, g=9.80665, ratio=2): return J +def energy_period_to_peak_period(Te, gamma): + """ + Convert from spectral energy period (Te) to peak period (Tp) using ITTC approximation for JONSWAP Spectrum. + + Approximation is given in "The Specialist Committee on Waves, Final Report + and Recommendations of the 23rd ITTC", Procedings of the 23rd ITTC - Volume + 2, Table A4. + + Parameters: + ---------- + Te: float or array + Spectral energy period (s) + gamma: float or int + Peak enhancement factor for JONSWAP spectrum + + Returns + ------- + Tp: float or array + Spectral peak period (s) + """ + assert isinstance(Te, (float, np.ndarray)), 'Te must be a float or a ndarray' + assert isinstance(gamma, (float, int)), 'gamma must be of type float or int' + + factor = 0.8255 + 0.03852*gamma - 0.005537*gamma**2 + 0.0003154*gamma**3 + + return Te / factor + + def wave_celerity(k, h, g=9.80665, depth_check=False, ratio=2): """ Calculates wave celerity (group velocity) From c33acb67464e323ea176458d65fcdc8eebfb78cb Mon Sep 17 00:00:00 2001 From: Adam Keester <72414466+akeeste@users.noreply.github.com> Date: Fri, 16 Dec 2022 14:36:08 -0600 Subject: [PATCH 2/2] Apply suggestions from code review --- mhkit/wave/resource.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mhkit/wave/resource.py b/mhkit/wave/resource.py index 12ef30d64..0d7df634f 100644 --- a/mhkit/wave/resource.py +++ b/mhkit/wave/resource.py @@ -596,20 +596,20 @@ def energy_period_to_peak_period(Te, gamma): Convert from spectral energy period (Te) to peak period (Tp) using ITTC approximation for JONSWAP Spectrum. Approximation is given in "The Specialist Committee on Waves, Final Report - and Recommendations of the 23rd ITTC", Procedings of the 23rd ITTC - Volume + and Recommendations to the 23rd ITTC", Proceedings of the 23rd ITTC - Volume 2, Table A4. Parameters: ---------- Te: float or array - Spectral energy period (s) + Spectral energy period [s] gamma: float or int Peak enhancement factor for JONSWAP spectrum Returns ------- Tp: float or array - Spectral peak period (s) + Spectral peak period [s] """ assert isinstance(Te, (float, np.ndarray)), 'Te must be a float or a ndarray' assert isinstance(gamma, (float, int)), 'gamma must be of type float or int'