-
Notifications
You must be signed in to change notification settings - Fork 54
/
add_pred_sim_to_batch.m
76 lines (70 loc) · 2.46 KB
/
add_pred_sim_to_batch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function [varargout] = add_pred_sim_to_batch(S,osim_path,varargin)
% --------------------------------------------------------------------------
% add_pred_sim_to_batch
% This functions runs the predictive simulation as a batch job. Doing so
% allows the simulation to run in the background. It is possible to run
% multiple simulations at the same time, and queue more to run when they
% are done.
% Requires the parallel computing toolbox
%
% INPUT:
% - S -
% * setting structure S
%
% - osim_path -
% * path to the OpenSim model file (.osim)
%
% - PredSim_fun (optional) -
% * handle of function that is added to the batch. By default this is
% run_pred_sim. (Advanced)
%
% OUTPUT:
% - S (optional)-
% * setting structure S
%
%
% Original author: Lars D'Hondt
% Original date: 09/May/2022
% --------------------------------------------------------------------------
if isempty(varargin)
PredSim_fun = 'run_pred_sim';
else
PredSim_fun = varargin{1};
end
% Running a function as part of batch needs all paths that are called inside
% the function
additional_paths = S.solver.batch_job_paths;
[pathOsim,~,~] = fileparts(osim_path);
additional_paths{end+1} = pathOsim;
additional_paths{end+1} = fullfile(S.misc.main_path,'DefaultSettings');
additional_paths{end+1} = fullfile(S.misc.main_path,'PreProcessing');
additional_paths{end+1} = fullfile(S.misc.main_path,'CasadiFunctions');
additional_paths{end+1} = fullfile(S.misc.main_path,'OCP');
additional_paths{end+1} = fullfile(S.misc.main_path,'Subjects');
additional_paths{end+1} = fullfile(S.misc.main_path,'VariousFunctions');
additional_paths{end+1} = fullfile(S.misc.main_path,'PostProcessing');
% Select parallel cluster
all_par_clusters = parallel.clusterProfiles;
if isfield(S.solver,'par_cluster_name') && ~isempty(S.solver.par_cluster_name)...
&& ismember(S.solver.par_cluster_name,all_par_clusters)
myCluster = parcluster(S.solver.par_cluster_name);
else
myCluster = parcluster;
end
% Adapt number of threads if needed
N_threads = myCluster.NumThreads;
if isfield(S.solver,'N_threads') && S.solver.N_threads > N_threads
S.solver.N_threads = N_threads;
end
try myCluster.Jobs(end,1);
S.solver.job_id = myCluster.Jobs(end,1).ID+1;
catch
S.solver.job_id = 1;
end
% Add job to batch
batch(myCluster,PredSim_fun,0,{S,osim_path},'CurrentFolder',S.misc.main_path,...
'AdditionalPaths',additional_paths);
% Return optional outputs
if nargout >= 1
varargout{1} = S.solver.job_id;
end