From 18e191214dfc34f71d8beb35501c88cee0df123d Mon Sep 17 00:00:00 2001 From: Thomas Lambert <t.lambert@uliege.be> Date: Thu, 25 May 2023 15:10:57 +0200 Subject: [PATCH] chore(Rotare): Adjust autosave to Result.save --- src/classes/@Result/Result.m | 8 +++- src/classes/@Result/save.m | 91 ++++++++++++++++++++++++++++++++++++ src/rotare.m | 18 +++---- src/utils/saveresults.m | 77 ------------------------------ 4 files changed, 107 insertions(+), 87 deletions(-) create mode 100644 src/classes/@Result/save.m delete mode 100644 src/utils/saveresults.m diff --git a/src/classes/@Result/Result.m b/src/classes/@Result/Result.m index f24772c..7a58485 100644 --- a/src/classes/@Result/Result.m +++ b/src/classes/@Result/Result.m @@ -14,9 +14,11 @@ classdef Result < handle % indvel - Results obtained with the indVel solver % stahlhut - Results obtained with the stalhlut solver % operPts - Table with the operating points for each individual result + % ModExt - Modelling enstension options (losses, etc) % % % Result methods: + % save - Save the Result object to file % % Result constructor: % Result = Result() creates an empty object. @@ -45,7 +47,8 @@ classdef Result < handle indfact (1, :) OperRotor % Results obtained with the indFact solver indvel (1, :) OperRotor % Results obtained with the indVel 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 methods @@ -57,5 +60,8 @@ classdef Result < handle % Currently empty on purpose. Results are to be assigned manually. end + % Other methods + save(self, SaveOpts) % Save the results to file + end end diff --git a/src/classes/@Result/save.m b/src/classes/@Result/save.m new file mode 100644 index 0000000..6b55fcf --- /dev/null +++ b/src/classes/@Result/save.m @@ -0,0 +1,91 @@ +function save(self, SaveOpts) + % SAVE Method to save the Result object to a MAT-file. + % This method saves the Result object in a MAT-file, following the user's the user's + % configured rules for the file naming and the saving directory. + % ----- + % + % Syntax: + % Result.save() Prompt the user for a file name and directory and then save it. + % + % Result.save(SaveOpts) Save the result object and name it following the options passed in + % SaveOpts. + % + % Inputs: + % SaveOpts : (optional) Options for the naming of the saved file. Sturcture with fields + % - overwrite (bool) Overwrite previous result if filename is the same + % - dir (char) Directory where the results are saved + % - filename (char) Base file name of the saved result + % + % Outputs: + % MAT-file with the saved Result object. + % + % See also: rotare, Result, template. + % + % <a href="https://gitlab.uliege.be/rotare/documentation">Complete documentation (online)</a> + + % ---------------------------------------------------------------------------------------------- + % (c) Copyright 2022-2023 University of Liege + % Author: Thomas Lambert <t.lambert@uliege.be> + % ULiege - Aeroelasticity and Experimental Aerodynamics + % MIT License + % Repo: https://gitlab.uliege.be/rotare/rotare + % Docs: https://gitlab.uliege.be/rotare/documentation + % Issues: https://gitlab.uliege.be/rotare/rotare/-/issues + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + % Defaults and constants + DEF.OVERWRITE = false; + DEF.FILENAME = 'RotareResult.mat'; + + if nargin < 2 + % 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 + tempFile.Results = self; % So it can be saved properly + save([SaveOpts.dir, filename, '.mat'], '-struct', 'tempFile'); + +end + +function filename = formatfilename(SaveOpts) + % FORMATFILENAME Format the filename using user configuration preferences. + + TIME_FORMAT = 'YYYYmmddTHHMM'; + + % Ensure user did not put an extension already + filename = removeextension(SaveOpts.filename); + + % Prevent overwriting file + if ~SaveOpts.overwrite + % First preprend a timestamp at the front of the filename + timestamp = datestr(datetime('now'), TIME_FORMAT); + filename = [timestamp, '-', filename]; + + % If not enough, add a _v1 at the end of the filename + baseFilename = filename; + i = 0; + while isfile([SaveOpts.dir, filename, '.mat']) + i = i + 1; + filename = [baseFilename, '_v', num2str(i)]; + 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 diff --git a/src/rotare.m b/src/rotare.m index 969e442..57290c8 100644 --- a/src/rotare.m +++ b/src/rotare.m @@ -152,17 +152,17 @@ function [Results] = rotare(configFile) % Output result in an easily accessible form 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 + + % 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 ============================================== % ============================================================================================== diff --git a/src/utils/saveresults.m b/src/utils/saveresults.m deleted file mode 100644 index 2eedd3a..0000000 --- a/src/utils/saveresults.m +++ /dev/null @@ -1,77 +0,0 @@ -function saveresults(Struct, Save, Mod) - % SAVERESULTS Save the fields of a strucure in a MAT-file, according to user's config. - % This function saves the fields of the input structure 'Struct' as individual variables, - % following the user's configuration defined rules for the file naming. - % ----- - % - % Syntax: - % saveresults(Struct, Save, Mod) Saves each field of the input Struct as individual variables - % in a MAT-File, following the configuration options described in Sim.Save and Mod - % (see configs/template.m). - % - % Inputs: - % Struct : Struct whose fileds will be saved - % Save : Save options (see Sim.Save structure) - % Mod : Model configuration (see Mod structure) - % - % Outputs: - % MAT-file with the Struct fields as variables. - % - % See also: rotare, template. - % - % <a href="https://gitlab.uliege.be/rotare/documentation">Complete documentation (online)</a> - - % ---------------------------------------------------------------------------------------------- - % (c) Copyright 2022-2023 University of Liege - % Author: Thomas Lambert <t.lambert@uliege.be> - % ULiege - Aeroelasticity and Experimental Aerodynamics - % MIT License - % Repo: https://gitlab.uliege.be/rotare/rotare - % Docs: https://gitlab.uliege.be/rotare/documentation - % Issues: https://gitlab.uliege.be/rotare/rotare/-/issues - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - % Create directory for the results if it does not exist - if ~isfolder(Save.dir) - mkdir(Save.dir); - end - - % Generate proper name based on user inputs - filename = formatfilename(Save, Mod); - - % Save to file - save([Save.dir, filename, '.mat'], '-struct', 'Struct'); - -end - -function filename = formatfilename(Save, Mod) - % FORMATFILENAME Format the filename using user configuration preferences. - - filename = Save.filename; - - % Ensure user did not put an extension already - [path, file, ~] = fileparts(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 - if Save.prependTime - timestamp = datestr(datetime('now'), Save.timeFormat); - filename = [timestamp, '-', filename]; - end - - % Prevent overwriting file - if ~Save.overwrite - baseFilename = filename; - i = 0; - while isfile([Save.dir, filename, '.mat']) - i = i + 1; - filename = [baseFilename, '_v', num2str(i)]; - end - end - -end -- GitLab