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

chore(Rotare): Adjust autosave to Result.save

parent e464bbe5
No related branches found
No related tags found
No related merge requests found
...@@ -14,9 +14,11 @@ classdef Result < handle ...@@ -14,9 +14,11 @@ classdef Result < handle
% indvel - Results obtained with the indVel solver % indvel - Results obtained with the indVel solver
% stahlhut - Results obtained with the stalhlut solver % stahlhut - Results obtained with the stalhlut solver
% operPts - Table with the operating points for each individual result % operPts - Table with the operating points for each individual result
% ModExt - Modelling enstension options (losses, etc)
% %
% %
% Result methods: % Result methods:
% save - Save the Result object to file
% %
% Result constructor: % Result constructor:
% Result = Result() creates an empty object. % Result = Result() creates an empty object.
...@@ -45,7 +47,8 @@ classdef Result < handle ...@@ -45,7 +47,8 @@ classdef Result < handle
indfact (1, :) OperRotor % Results obtained with the indFact solver indfact (1, :) OperRotor % Results obtained with the indFact solver
indvel (1, :) OperRotor % Results obtained with the indVel solver indvel (1, :) OperRotor % Results obtained with the indVel solver
stahlhut (1, :) OperRotor % Results obtained with the stalhlut solver stahlhut (1, :) OperRotor % Results obtained with the stalhlut solver
operPts (:, 4) table % Table of operating points for each individual result operPts (:, 4) table % Table of operating points for each individual result
ModExt (1, 1) struct % Modelling enstension options (losses, etc)
end end
methods methods
...@@ -57,5 +60,8 @@ classdef Result < handle ...@@ -57,5 +60,8 @@ classdef Result < handle
% Currently empty on purpose. Results are to be assigned manually. % Currently empty on purpose. Results are to be assigned manually.
end end
% Other methods
save(self, SaveOpts) % Save the results to file
end end
end end
function saveresults(Struct, Save, Mod) function save(self, SaveOpts)
% SAVERESULTS Save the fields of a strucure in a MAT-file, according to user's config. % SAVE Method to save the Result object to a MAT-file.
% This function saves the fields of the input structure 'Struct' as individual variables, % This method saves the Result object in a MAT-file, following the user's the user's
% following the user's configuration defined rules for the file naming. % configured rules for the file naming and the saving directory.
% ----- % -----
% %
% Syntax: % Syntax:
% saveresults(Struct, Save, Mod) Saves each field of the input Struct as individual variables % Result.save() Prompt the user for a file name and directory and then save it.
% in a MAT-File, following the configuration options described in Sim.Save and Mod %
% (see configs/template.m). % Result.save(SaveOpts) Save the result object and name it following the options passed in
% SaveOpts.
% %
% Inputs: % Inputs:
% Struct : Struct whose fileds will be saved % SaveOpts : (optional) Options for the naming of the saved file. Sturcture with fields
% Save : Save options (see Sim.Save structure) % - overwrite (bool) Overwrite previous result if filename is the same
% Mod : Model configuration (see Mod structure) % - dir (char) Directory where the results are saved
% - filename (char) Base file name of the saved result
% %
% Outputs: % Outputs:
% MAT-file with the Struct fields as variables. % MAT-file with the saved Result object.
% %
% See also: rotare, template. % See also: rotare, Result, template.
% %
% <a href="https://gitlab.uliege.be/rotare/documentation">Complete documentation (online)</a> % <a href="https://gitlab.uliege.be/rotare/documentation">Complete documentation (online)</a>
...@@ -31,47 +33,59 @@ function saveresults(Struct, Save, Mod) ...@@ -31,47 +33,59 @@ function saveresults(Struct, Save, Mod)
% Issues: https://gitlab.uliege.be/rotare/rotare/-/issues % Issues: https://gitlab.uliege.be/rotare/rotare/-/issues
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create directory for the results if it does not exist % Defaults and constants
if ~isfolder(Save.dir) DEF.OVERWRITE = false;
mkdir(Save.dir); DEF.FILENAME = 'RotareResult.mat';
end
% Generate proper name based on user inputs if nargin < 2
filename = formatfilename(Save, Mod); % Prompt user for filename
[filename, SaveOpts.dir] = uiputfile(DEF.FILENAME, '*.mat');
filename = removeextension(filename); % Remove ext as it is added during save()
else
% Create directory for the results if it does not exist
if ~isfolder(SaveOpts.dir)
mkdir(SaveOpts.dir);
end
% Generate proper name based on user inputs
filename = formatfilename(SaveOpts);
end
% Save to file % Save to file
save([Save.dir, filename, '.mat'], '-struct', 'Struct'); tempFile.Results = self; % So it can be saved properly
save([SaveOpts.dir, filename, '.mat'], '-struct', 'tempFile');
end end
function filename = formatfilename(Save, Mod) function filename = formatfilename(SaveOpts)
% FORMATFILENAME Format the filename using user configuration preferences. % FORMATFILENAME Format the filename using user configuration preferences.
filename = Save.filename; TIME_FORMAT = 'YYYYmmddTHHMM';
% Ensure user did not put an extension already % Ensure user did not put an extension already
[path, file, ~] = fileparts(filename); filename = removeextension(SaveOpts.filename);
filename = fullfile(path, file);
% Append the solver name and loss model to the file name
if Save.appendInfo
filename = [filename, '_', Mod.solver, '_', Mod.Ext.losses];
end
% Prepend the file name with a timestamp % Prevent overwriting file
if Save.prependTime if ~SaveOpts.overwrite
timestamp = datestr(datetime('now'), Save.timeFormat); % First preprend a timestamp at the front of the filename
timestamp = datestr(datetime('now'), TIME_FORMAT);
filename = [timestamp, '-', filename]; filename = [timestamp, '-', filename];
end
% Prevent overwriting file % If not enough, add a _v1 at the end of the filename
if ~Save.overwrite
baseFilename = filename; baseFilename = filename;
i = 0; i = 0;
while isfile([Save.dir, filename, '.mat']) while isfile([SaveOpts.dir, filename, '.mat'])
i = i + 1; i = i + 1;
filename = [baseFilename, '_v', num2str(i)]; filename = [baseFilename, '_v', num2str(i)];
end end
end end
end end
function filename = removeextension(filename)
% REMOVEEXTENSION Remove extension from a filename if there was one
[path, file, ~] = fileparts(filename);
filename = fullfile(path, file);
end
...@@ -152,17 +152,17 @@ function [Results] = rotare(configFile) ...@@ -152,17 +152,17 @@ function [Results] = rotare(configFile)
% Output result in an easily accessible form % Output result in an easily accessible form
Results.(Mod.solver) = OpRot; Results.(Mod.solver) = OpRot;
Results.operPts = array2table(operPoints, ...
'VariableNames', {'Alt', 'V_ax', 'RPM', 'Coll'});
% Save solution to MAT-file for future reusability
if Sim.Save.autosave
TmpStruct.OpRot = OpRot;
saveresults(TmpStruct, Sim.Save, Mod);
clear TmpStruct;
end
end end
% Add context to the Results and save if needed
Results.operPts = array2table(operPoints, ...
'VariableNames', {'Alt', 'V_ax', 'RPM', 'Coll'});
Results.ModExt = Mod.Ext;
if Sim.Save.autosave
Results.save(Sim.Save);
end
% ============================================================================================== % ==============================================================================================
% ====================================== Analysis ============================================== % ====================================== Analysis ==============================================
% ============================================================================================== % ==============================================================================================
......
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