Skip to content
Snippets Groups Projects
Verified Commit 34dc2698 authored by Thomas Lambert's avatar Thomas Lambert :helicopter:
Browse files

refactor(findccllinrange): improve cyclomatic complexity

parent 9622fc96
No related branches found
No related tags found
No related merge requests found
......@@ -91,7 +91,6 @@ function [alphaRange, clRange, clSlope] = linearrangefinder(alpha, cl)
IDEAL_LIFT_SLOPE = 2 * pi; % Ideal Cl curve, [rad]
TOL_DCL = 0.6; % Tolerance on the slope
TOL_DDCL = 0.01; % Tolerance on the curvature
PLOT_POLAR_IF_ERROR = true; % Plot the polar if error so user can see why it fails
% Initialization
alphaRange = zeros(2, size(cl, 2));
......@@ -100,64 +99,11 @@ function [alphaRange, clRange, clSlope] = linearrangefinder(alpha, cl)
% Loop for every polar data (every column of cl)
for i = 1:size(cl, 2)
% First and second derivatives
dalpha = diff(alpha(:, i));
dcl = diff(cl(:, i)) ./ dalpha(1);
ddcl = diff(dcl) ./ dalpha(1);
% Adapt tolerance to angle of attack
ddClTol = TOL_DDCL / dalpha(1)^2;
% Initialize loop
maxFound = false;
minFound = false;
% Initialize linear range start and end (mostly useful for debugging)
tmpRange = [1, length(alpha(:, i))];
for j = 1:(length(alpha)) - 2
% Check if current point is a valid point of the linear range
isMin = isbound(dcl(j), ddcl(j), TOL_DCL, ddClTol, IDEAL_LIFT_SLOPE);
isMax = isbound(dcl(end + 1 - j), ddcl(end + 1 - j), TOL_DCL, ddClTol, ...
IDEAL_LIFT_SLOPE);
if isMin && ~minFound
minFound = true;
tmpRange(1) = j;
end
if isMax && ~maxFound
maxFound = true;
tmpRange(end) = length(dcl) + 2 - j;
end
% Stop when both min and max were found
if minFound && maxFound
break
end
end
[tmpRange, rangeFound] = findrange(i, alpha, cl, TOL_DCL, TOL_DDCL, IDEAL_LIFT_SLOPE);
% Check issues in case linear range was not found
if ~minFound || ~maxFound
if PLOT_POLAR_IF_ERROR % Plot the polar if error so user can see why it fails
% Display issue
figure('Name', 'linearrange:Debug');
hold on;
plot(alpha(:, i), cl(:, i), '--');
plot(alpha(tmpRange(1):tmpRange(2), i), cl(tmpRange(1):tmpRange(2), i), ...
'color', 'r', 'lineWidth', 1.2);
if ~minFound
plot(alpha(tmpRange(1), i), cl(tmpRange(1), i), ...
's', 'MarkerFaceColor', 'b', 'Markersize', 12);
end
if ~maxFound
plot(alpha(tmpRange(2), i), cl(tmpRange(2), i), ...
's', 'MarkerFaceColor', 'b', 'Markersize', 12);
end
grid on;
legend('Polar', 'Attempt at linear range', 'Missing point(s)');
end
if ~rangeFound
ploterror(alpha, cl, i, tmpRange);
error('MATLAB:findcllinearrange:noRangeFound', ...
['findcllinearrange was not able to determine the linear range of the given '...
'polar.\nMaybe the polar does not have enough points, or it is malformed.\n '...
......@@ -193,3 +139,67 @@ function bool = iscurvaturezero(ddCl, tol)
% ISCURVATUREZERO Returns true if the curvature is almost zero
bool = abs(ddCl) < tol;
end
% --------------------------------------------------------------------------------------------------
function ploterror(alpha, cl, i, tmpRange)
% PLOTERROR Plots the polar in case of error
figure('Name', 'linearrange:Debug');
hold on;
plot(alpha(:, i), cl(:, i), '--');
plot(alpha(tmpRange(1):tmpRange(2), i), cl(tmpRange(1):tmpRange(2), i), ...
'color', 'r', 'lineWidth', 1.2);
plot(alpha(tmpRange(1), i), cl(tmpRange(1), i), ...
's', 'MarkerFaceColor', 'b', 'Markersize', 12);
plot(alpha(tmpRange(2), i), cl(tmpRange(2), i), ...
's', 'MarkerFaceColor', 'b', 'Markersize', 12);
grid on;
legend('Polar', 'Attempt at linear range', 'Missing point(s)');
end
% --------------------------------------------------------------------------------------------------
function [tmpRange, rangeFound] = findrange(i, alpha, cl, TOL_DCL, TOL_DDCL, IDEAL_LIFT_SLOPE)
% FINDRANGE Finds the range iteratively.
rangeFound = false;
% First and second derivatives
dalpha = diff(alpha(:, i));
dcl = diff(cl(:, i)) ./ dalpha(1);
ddcl = diff(dcl) ./ dalpha(1);
% Adapt tolerance to angle of attack
ddClTol = TOL_DDCL / dalpha(1)^2;
% Initialize linear range start and end (mostly useful for debugging)
tmpRange = [1, length(alpha(:, i))];
maxFound = false;
minFound = false;
for j = 1:(length(alpha)) - 2
% Check if current point is a valid point of the linear range
isMin = isbound(dcl(j), ddcl(j), TOL_DCL, ddClTol, IDEAL_LIFT_SLOPE);
isMax = isbound(dcl(end + 1 - j), ddcl(end + 1 - j), TOL_DCL, ddClTol, ...
IDEAL_LIFT_SLOPE);
if isMin && ~minFound
minFound = true;
tmpRange(1) = j;
end
if isMax && ~maxFound
maxFound = true;
tmpRange(end) = length(dcl) + 2 - j;
end
if minFound && maxFound
rangeFound = true;
break
end
end
end
......@@ -9,16 +9,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Start using MISS_HIT for style and code analysis.
### Changed
- Style: Adopt style from MISS_HIT
- Copyright notice: change main copyright holder to the University
- Doc: Revamp `CONTRIBUTING.md` to put emphasis on MISS_HIT and conventions
- Refactor: Rework functions to lower their complexity using mh_metrics
### Deprecated
### Removed
### Fixed
### Security
## [1.2.0] - 2022-04-10
### Added
......@@ -51,7 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Initial release
[Unreleased]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v1.0.0...master
[Unreleased]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v1.2.0...master
[1.2.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v1.1.0...v1.2.0
[1.1.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v1.0.0...v1.1.0
[1.0.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/releases/v1.0.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment