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
% 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
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.
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:
% 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).
% 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:
% Struct : Struct whose fileds will be saved
% Save : Save options (see Sim.Save structure)
% Mod : Model configuration (see Mod structure)
% 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 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>
......@@ -31,47 +33,59 @@ function saveresults(Struct, Save, Mod)
% 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
% Defaults and constants
DEF.OVERWRITE = false;
DEF.FILENAME = 'RotareResult.mat';
% Generate proper name based on user inputs
filename = formatfilename(Save, Mod);
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
save([Save.dir, filename, '.mat'], '-struct', 'Struct');
tempFile.Results = self; % So it can be saved properly
save([SaveOpts.dir, filename, '.mat'], '-struct', 'tempFile');
end
function filename = formatfilename(Save, Mod)
function filename = formatfilename(SaveOpts)
% FORMATFILENAME Format the filename using user configuration preferences.
filename = Save.filename;
TIME_FORMAT = 'YYYYmmddTHHMM';
% 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
filename = removeextension(SaveOpts.filename);
% Prepend the file name with a timestamp
if Save.prependTime
timestamp = datestr(datetime('now'), Save.timeFormat);
% 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];
end
% Prevent overwriting file
if ~Save.overwrite
% If not enough, add a _v1 at the end of the filename
baseFilename = filename;
i = 0;
while isfile([Save.dir, filename, '.mat'])
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
......@@ -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 ==============================================
% ==============================================================================================
......
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