Power Module: Convert to native MATLAB code#164
Power Module: Convert to native MATLAB code#164simmsa wants to merge 14 commits intoMHKiT-Software:developfrom
Conversation
Adds: * Rigorous input data checks * Voltage imbalance check
Add `custom_hilbert_transform` for use when the Signal Processing Toolbox is not available
…er_module_to_native_matlab
|
@rpauly18 and @hivanov-nrel , this PR is ready for review. I'm keeping this as a draft for now because I may need to make other updates to the tests for some other PR's and merge those into this PR. Happy to answer any questions. |
| end | ||
|
|
||
| % Validate time vectors match (if both are structures) | ||
| if isstruct(voltage) && isstruct(current) |
There was a problem hiding this comment.
Shouldn't the time vectors match regardless of whether they come from a structure or matrix?
There was a problem hiding this comment.
Yes, good catch. This check can be removed.
| if grid_freq == 60 | ||
| max_freq = 3060; % Exactly 51st harmonic (60 * 51) | ||
| elseif grid_freq == 50 | ||
| max_freq = 2570; % 51.4th harmonic (intentional extension) |
There was a problem hiding this comment.
Good question, I am not entirely sure. This is from the python implementation: https://github.com/MHKiT-Software/MHKiT-Python/blob/68fe393855d4f8c71f2c6f15feada8464a2bc99e/mhkit/power/quality.py#L109. I need to look into the 61000-4-7 standard.
There was a problem hiding this comment.
@rpauly18, do you have any insight here. The intent of the harmonics code seems to be to calculate the input values to downstream harmonics quantities of interest calculations including total harmonic distortion:
Ref: IEC 61000-4-7 Edition 2.1 2009-10 Equation 4
In the code section referenced here max_freq should be 2550. Does this seem correct to you?
Should we also add an argument so the user can set
There was a problem hiding this comment.
I am looking into this, but I am having a hard time following the history of this function on the Python side to figure out why this was developed this way. This function was originally different. The 51 Hz thing is very weird. The commit history on this function is also weird and inconsistent.
| if any(abs(time_diff - dt_mean) > dt_tolerance) | ||
| warning('MHKiT:instantaneous_frequency: Non-uniform time spacing detected, using local time differences'); | ||
| end |
There was a problem hiding this comment.
Is this warning necessary? The function will always use the local time difference rather than the dt_mean
| % Calculate power for each phase | ||
| if line_to_line | ||
| % For line-to-line measurements, apply sqrt(3) correction | ||
| power_per_phase = abs_current .* (abs_voltage * sqrt(3)); |
There was a problem hiding this comment.
alternatively, you could send the data to dc_power(), take the gross power output and multiply the power factor and line_to_line factor
There was a problem hiding this comment.
I think this is a good suggestion, but might be confusing to a user (Why are we using the dc power to calculate ac_power), even though the equations are the same. The python implementation does not call dc_power: https://github.com/MHKiT-Software/MHKiT-Python/blob/68fe393855d4f8c71f2c6f15feada8464a2bc99e/mhkit/power/characteristics.py#L247, so maybe it makes the most sense to use the same pattern here.
There was a problem hiding this comment.
@simmsa The python code calls dc_power() a few lines above: https://github.com/MHKiT-Software/MHKiT-Python/blob/68fe393855d4f8c71f2c6f15feada8464a2bc99e/mhkit/power/characteristics.py#L239. The current implementation is fine as is though
This is redundant based on the checks performed before this check.
This PR removes the python calls to MHKiT-Python and rewrites the MHKiT-Python functionality in native MATLAB code:
mhkit/power/characteristics/ac_three_phase.mmhkit/power/characteristics/dc_power.mmhkit/power/characteristics/instantaneous_frequency.mmhkit/power/quality/calc_electrical_angle.m(Already native matlab, unchanged)mhkit/power/quality/calc_flicker_coefficient.m(Already native matlab, unchanged)mhkit/power/quality/calc_fundamental_freq.m(Already native matlab, unchanged)mhkit/power/quality/calc_ideal_voltage.m(Already native matlab, unchanged)mhkit/power/quality/calc_Rfic_Lfic.m(Already native matlab, unchanged)mhkit/power/quality/calc_shortterm_flicker_severity.m(Already native matlab, unchanged)mhkit/power/quality/calc_simulated_voltage.m(Already native matlab, unchanged)mhkit/power/quality/flicker_ufic_workflow.m(Already native matlab, unchanged)mhkit/power/quality/gen_test_data.m(Already native matlab, unchanged)mhkit/power/quality/harmonic_subgroups.mmhkit/power/quality/harmonics.mmhkit/power/quality/interharmonics.mmhkit/power/quality/total_harmonic_current_distortion.m