Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion mhkit/tests/wave/test_resource_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -356,6 +376,6 @@ def test_plot_monthly_cumulative_distribution(self):

self.assertTrue(isfile(filename))


if __name__ == '__main__':
unittest.main()
28 changes: 28 additions & 0 deletions mhkit/wave/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 to the 23rd ITTC", Proceedings 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)
Expand Down