Skip to content

Commit

Permalink
add uuid to prevent generated file conflict when running tests in par…
Browse files Browse the repository at this point in the history
…allel
  • Loading branch information
rebeccamccabe committed Nov 10, 2024
1 parent 165fcec commit 810393e
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ codegen/
octave-workspace

# auto-generated optimization functions
mdocean/optimization/generated
mdocean/optimization/generated*

# html report files
html/
Expand Down
6 changes: 1 addition & 5 deletions mdocean/inputs/var_bounds.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@
'pos_power','spar_damping','spar_height_up','spar_height_down','LCOE_max',...
'irrelevant_max_force','water_deep_enough'};

% modify nominal control inputs to so power and force matches actual
[F_max_nom, B_p_nom, w_n_nom] = deal(1e6);%find_nominal_inputs(b, false);
b.F_max_nom = F_max_nom;
b.B_p_nom = B_p_nom;
b.w_n_nom = w_n_nom;
b.filename_uuid = ''; % string to append to generated filenames to prevent parallel overlap

b.X_noms = [b.D_f_nom b.D_s_ratio_nom b.h_f_ratio_nom b.T_s_ratio_nom b.F_max_nom b.B_p_nom b.w_n_nom]';

Expand Down
2 changes: 1 addition & 1 deletion mdocean/optimization/gradient_optim.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
error('x0 input struct has wrong size')
end

[X_opt_raw,obj_opt,flag,output,lambda,grad,hess,problem] = run_solver(prob, obj_names{which_obj}, x0, opts);
[X_opt_raw,obj_opt,flag,output,lambda,grad,hess,problem] = run_solver(prob, obj_names{which_obj}, x0, opts, b.filename_uuid);
probs{i} = problem;

% D_f D_s_ratio h_f_ratio T_s_ratio F_max B_p w_n]
Expand Down
3 changes: 2 additions & 1 deletion mdocean/optimization/multiobjective/pareto_search.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function [x,fval] = pareto_search()
function [x,fval] = pareto_search(filename_uuid)
p = parameters();
b = var_bounds();
b.filename_uuid = filename_uuid;
x0 = b.X_start_struct;

%% Calculate seed points for the pareto front
Expand Down
4 changes: 2 additions & 2 deletions mdocean/optimization/run_solver.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function [X_opt,obj_opt,flag,output,lambda,grad,hess,problem] = run_solver(prob, obj, x0, opts)
function [X_opt,obj_opt,flag,output,lambda,grad,hess,problem] = run_solver(prob, obj, x0, opts, filename_uuid)
solver_based = true;
% create folder for generated objectives if it doesn't already exist
if solver_based
generated_folder = 'optimization/generated';
generated_folder = ['optimization/generated' filename_uuid];
if ~exist(generated_folder,'dir')
mkdir(generated_folder)
addpath(generated_folder)
Expand Down
4 changes: 4 additions & 0 deletions mdocean/optimization/sensitivities/gradient_mult_x0.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
function [] = gradient_mult_x0(filename_uuid)
clear;clc;close all

p = parameters();
b = var_bounds();
b.filename_uuid = filename_uuid;

num_runs = 100;
objs = Inf(num_runs,2);
Expand Down Expand Up @@ -55,3 +57,5 @@
percent_optimal = sum(optimal) / num_runs
percent_optimal_given_converged = sum(optimal_and_converged) ./ sum(converged)
percent_optimal_given_kkt = sum(optimal_and_kkt) ./ sum(kkt)

end
3 changes: 2 additions & 1 deletion mdocean/optimization/sensitivities/location_sensitivity.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function [tab] = location_sensitivity()
function [tab] = location_sensitivity(filename_uuid)

p = parameters();
b = var_bounds();
b.filename_uuid = filename_uuid;

files = {'Humboldt_California_Wave Resource _SAM CSV.csv',...
'PacWave-North_Oregon_Wave-Resource.csv',...
Expand Down
4 changes: 3 additions & 1 deletion mdocean/optimization/sensitivities/param_sweep.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
function [] = param_sweep()
function [] = param_sweep(filename_uuid)

% Brute force parameter sensitivity sweep (reoptimize for each param value)
%% Setup
clear;clc;%close all
b = var_bounds();
[p,T] = parameters();
b.filename_uuid = filename_uuid;

param_names = T.name_pretty(T.sweep); % list of parameters to sweep
params = T.name(T.sweep);
Expand All @@ -30,6 +31,7 @@
%% Run optimization
for i=1:length(params)
p = parameters();
p.filename_uuid = filename_uuid;
var_nom = p.(params{i});
for j=1:length(ratios)
p.(params{i}) = ratios(j) * var_nom;
Expand Down
17 changes: 11 additions & 6 deletions mdocean/plots/all_figures.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
% Generate all figures used in the paper
function [success_criterion,num_figs,num_tabs,fig_names,tab_names] = all_figures( which_figs, which_tabs )
function [success_criterion,num_figs,num_tabs,fig_names,tab_names] = all_figures( which_figs, which_tabs, filename_uuid )

if nargin<3
filename_uuid = ''; % required argument for anything running gradient_optim in parallel,
% since prob2struct needs unique filenames for code generation
end

num_figs = 10;
num_tabs = 7;
Expand Down Expand Up @@ -46,21 +51,21 @@
fig_names{6} = 'Fig. 6: pareto front';
fig_names{7} = 'Fig. 7: design heuristics';
if any(which_figs == 6 | which_figs == 7)
pareto_search();
%pareto_search(filename_uuid);
pareto_curve_heuristics()
end

%% figure 8 - parameter sensitivities
fig_names{8} = 'Fig. 8: parameter sensitivities';
if any(which_figs == 8)
param_sweep()
%param_sweep(filename_uuid)
end

%% figure 9, 10 - overlaid geometry, probability CDF
fig_names{9} = 'Fig. 9: overlaid geometry';
fig_names{10} = 'Fig. 10: probability CDF';
if any(which_figs == 9 | which_figs == 10 | which_tabs == 5)
tab_5 = compare();
tab_5 = compare(filename_uuid);
end

%% table 1 - design variables table
Expand Down Expand Up @@ -103,7 +108,7 @@
%% table 6 - optimal DVs for 4 locations
tab_names{6} = 'Tab. 6: optimal DVs for 4 locations';
if any(which_tabs == 6)
tab = location_sensitivity();
tab = location_sensitivity(filename_uuid);
display(tab);
location_flags = tab(strcmp(tab.Row,'flag'),:).Variables;
success_criterion(end+1) = {location_flags};
Expand All @@ -112,7 +117,7 @@
%% paragraph 4.2 - convergence for different x0s
tab_names{7} = 'Tab. 7: convergence for different x0s';
if any(which_tabs == 7)
%gradient_mult_x0()
%gradient_mult_x0(filename_uuid)
end

end
3 changes: 2 additions & 1 deletion mdocean/plots/compare.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function [DV_table] = compare()
function [DV_table] = compare(filename_uuid)

p = parameters();
b = var_bounds();
b.filename_uuid = filename_uuid;
x0_input = b.X_start_struct;

Xs_opt = gradient_optim(x0_input,p,b);
Expand Down
2 changes: 1 addition & 1 deletion tests/run_tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
runner.addPlugin(p2);
runner.addPlugin(p3);

results = runner.run(suite);
results = runner.runInParallel(suite);

%open([cov_dir '/coverageReport/index.html'])
open([test_dir '/testreport.pdf'])
Expand Down
9 changes: 8 additions & 1 deletion tests/test.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
failed
simulated
actual
uuid
end

% inputs for tests, including passing tolerances
Expand Down Expand Up @@ -53,12 +54,18 @@ function runNominalValidation(testCase)
testCase.simulated = sim;
testCase.actual = act;
end

function generateUUID(testCase)
% generate unique identifier for each parallel worker
% required to prevent file overalps for generated code
testCase.uuid = parallel.pool.Constant(@() char(matlab.lang.internal.uuid()));
end
end

% Test methods
methods(Test, ParameterCombination='sequential')
function allFiguresRun(testCase, which_figs, which_tabs)
success_criterion = all_figures(which_figs,which_tabs);
success_criterion = all_figures(which_figs,which_tabs,testCase.uuid.Value);
if ~isempty(success_criterion)
for i=1:length(success_criterion)
testCase.verifyGreaterThan(success_criterion{i},0);
Expand Down

0 comments on commit 810393e

Please sign in to comment.