Skip to content

Commit 2c76f49

Browse files
authored
Merge pull request #179 from hivanov-nrel/loads_bug_fix
Loads mler bug fix
2 parents 23f8381 + ed0d4a3 commit 2c76f49

5 files changed

Lines changed: 31 additions & 41 deletions

File tree

examples/extreme_response_MLER_example.html

Lines changed: 3 additions & 8 deletions
Large diffs are not rendered by default.

examples/extreme_response_MLER_example.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
% would like to find the wave profile that will generate a heave response of 1
4747
% meter for our WEC device.
4848

49-
mler_data = mler_coefficients(RAO', js, 1);
49+
mler_data = mler_coefficients(RAO, js, 1);
5050
plot(mler_data.frequency, mler_data.conditioned_spectrum); xlabel('Frequency [Hz]'); ylabel('Conditoned wave spectrum [m^2-s]')
5151
plot(mler_data.frequency, mler_data.phase); xlabel('Frequency [Hz]'); ylabel('Phase [rad]')
5252

@@ -82,11 +82,6 @@
8282

8383
peakHeightDesired = Hs/2 * 1.9;
8484

85-
% Transpose MLER output
86-
mler_data.conditioned_spectrum = mler_data.conditioned_spectrum';
87-
mler_data.phase = mler_data.phase';
88-
mler_data.frequency = mler_data.frequency';
89-
9085
mler_norm = mler_wave_amp_normalize(peakHeightDesired, mler_data, sim, k.values);
9186

9287
%%
-97.6 KB
Binary file not shown.

mhkit/loads/extreme/mler_coefficients.m

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
%
88
% Parameters
99
% ----------
10-
% RAO : array
10+
% RAO : array (N x 1)
1111
% Response amplitude operator [-]
1212
% wave_spectrum: struct
13-
% Struct with wave spectral density [m^2/Hz] and frequency [Hz]
14-
% response_desired: int or float
15-
% Latitude longitude pairs at which to extract data.
13+
% wave_spectrum.spectrum - Spectral density [m^2/Hz] (N x 1)
14+
% wave_spectrum.frequency - Frequency [Hz] (N x 1)
15+
% response_desired: scalar
16+
% Desired response amplitude
1617
%
1718
% Returns
1819
% -------
1920
% mler : struct
20-
% containing conditioned wave spectral amplitude
21-
% coefficient [m^2-s], and phase [rad] indexed by frequency [Hz].
21+
% mler.conditioned_spectrum - Conditioned wave spectral amplitude [m^2-s] (N x 1)
22+
% mler.phase - Phase [rad] (N x 1)
23+
% mler.frequency - Frequency [Hz] (N x 1)
2224
%
2325
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2426

@@ -32,16 +34,21 @@
3234
error('ERROR: response_desired must be an int or double')
3335
end
3436

37+
% validate all inputs have same length
38+
N = length(RAO);
39+
if length(wave_spectrum.frequency) ~= N || length(wave_spectrum.spectrum) ~= N
40+
error('MHKiT:loads:mler_coefficients: RAO, frequency, and spectrum must have same length');
41+
end
42+
3543
% convert from Hz to rad/s
36-
freq = wave_spectrum.frequency * (2*pi);
37-
freq_hz = wave_spectrum.frequency;
38-
wave_spectrum = wave_spectrum.spectrum / (2*pi);
39-
dw = (2*pi - 0) / (length(freq)-1);
44+
freq_rad = wave_spectrum.frequency * (2*pi);
45+
wave_spectrum_rad = wave_spectrum.spectrum / (2*pi);
46+
dw = (2*pi - 0) / (N-1);
4047

4148
% response spectrum
42-
R.spectrum = abs(RAO).^2 .* (2*wave_spectrum);
49+
R.spectrum = abs(RAO).^2 .* (2*wave_spectrum_rad);
4350
R.type = 'response';
44-
R.frequency = freq;
51+
R.frequency = freq_rad;
4552

4653
% spectral moment calculations
4754
m0 = frequency_moment(R, 0);
@@ -50,16 +57,16 @@
5057
wBar = m1/m0;
5158

5259
% calculate coefficient_a from Quon2016 Eqn.8
53-
coeff_a_rn = abs(RAO) .* sqrt(2*wave_spectrum*dw) .* ((m2 - freq*m1) + wBar*(freq*m0 - m1)) ./ (m0*m2 - m1^2);
60+
coeff_a_rn = abs(RAO) .* sqrt(2*dw.*wave_spectrum_rad) .* ((m2 - freq_rad.*m1) + wBar.*(freq_rad.*m0 - m1)) ./ (m0*m2 - m1^2);
5461
% phase delay should be positive number
5562
phase = -unwrap(angle(RAO));
5663
% for negative values of Amp, add pi phase shift, flip sign
5764
phase(coeff_a_rn < 0) = phase(coeff_a_rn < 0) - pi;
5865
coeff_a_rn(coeff_a_rn < 0) = coeff_a_rn(coeff_a_rn < 0) * -1;
5966

6067
% calculate conditioned spectrum [m^2-s/rad]
61-
S = wave_spectrum .* coeff_a_rn.^2 .* response_desired^2;
62-
S(isnan(S)) = 0; % replace nans with zero
68+
conditioned_spectrum = wave_spectrum_rad .* coeff_a_rn.^2 .* response_desired^2;
69+
conditioned_spectrum(isnan(conditioned_spectrum)) = 0; % replace nans with zero
6370
% if the response amplitude we ask for is negative, we will add
6471
% a pi phase shift to the phase information. This is because
6572
% the sign of response_desired is lost in the squaring above.
@@ -71,8 +78,8 @@
7178
end
7279

7380
% outputs
74-
mler.conditioned_spectrum = S;
81+
mler.conditioned_spectrum = conditioned_spectrum;
7582
mler.phase = phase;
76-
mler.frequency = freq_hz;
83+
mler.frequency = wave_spectrum.frequency;
7784

7885
end

mhkit/tests/Loads_TestExtreme.m

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@
33
methods (Test)
44

55
function test_mler_coefficients(testCase)
6-
% `mler_coefficients` Line 54, 55
7-
% phase delay should be positive number
8-
% phase = -unwrap(angle(RAO));
9-
% Per @simmsa, this does not perform this conversion correctly for positive RAO values
10-
assumeFail(testCase, "Per @simmsa, ask @hivanov about setting RAO to negative values per the above comment");
11-
126
% create inputs and load validation data
137
fpath = '../../examples/data/loads/mler.csv';
148
validation = readtable(fpath);
159
wave_freq = linspace(0,1,500);
16-
js = jonswap_spectrum(wave_freq,15.1,9);
10+
js = pierson_moskowitz_spectrum(wave_freq,15.1,9);
1711
response_desired = 1;
1812
RAO = validation.RAO;
19-
RAO = RAO';
2013
% execute function
2114
mler = mler_coefficients(RAO, js, response_desired);
2215
% assertions
23-
assertEqual(testCase, mler.conditioned_spectrum, validation.Res_Spec', 'RelTol',0.005)
24-
assertEqual(testCase, mler.phase, validation.phase', 'RelTol',0.001)
16+
assertEqual(testCase, mler.conditioned_spectrum, validation.Res_Spec, 'RelTol',0.005)
17+
assertEqual(testCase, mler.phase, validation.phase, 'RelTol',0.001)
2518
end
2619

2720
function test_mler_simulation(testCase)

0 commit comments

Comments
 (0)