-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrun_pred_sim.m
154 lines (134 loc) · 4.03 KB
/
run_pred_sim.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
function [varargout] = run_pred_sim(S,osim_path)
% --------------------------------------------------------------------------
% run_pred_sim
% This functions calls the subfunction for each step in the simulation
% workflow.
%
% INPUT:
% - S -
% * setting structure S
%
% - osim_path -
% * path to the OpenSim model file (.osim)
%
%
% OUTPUT:
% - savename (optional) -
% * results of the simulation will be saved in a file with this name
%
% Original author: Lars D'Hondt
% Original date: March-October/2022
%
% Last edit by:
% Last edit date:
% --------------------------------------------------------------------------
addpath([S.misc.main_path '\VariousFunctions'])
addpath([S.misc.main_path '\WearableDevices'])
% Settings that are not specified get their default value
S = getDefaultSettings(S,osim_path);
% Add CasADi to the path
if ~isempty(S.solver.CasADi_path)
addpath(genpath(S.solver.CasADi_path));
end
% Make sure folder to save results exists
OutFolder = S.misc.save_folder;
if ~isfolder(OutFolder)
mkdir(OutFolder);
end
if S.post_process.load_prev_opti_vars
% load settings and model_info when only running post-processing
Outname = fullfile(S.misc.save_folder,[S.misc.result_filename '.mat']);
load(Outname,'R','model_info');
S = R.S;
S.post_process.load_prev_opti_vars = 1;
osim_path = model_info.osim_path;
S = getDefaultSettings(S, osim_path); % to fill in any missing settings
R.S = S;
elseif S.post_process.rerun
% load settings and model_info when only running post-processing
Outname = fullfile(S.misc.save_folder,[S.misc.result_filename '.mat']);
load(Outname,'R','model_info');
S = R.S;
S.post_process.rerun = 1;
osim_path = model_info.osim_path;
S = getDefaultSettings(S, osim_path); % to fill in any missing settings
R.S = S;
elseif isempty(S.misc.result_filename)
if strcmp(S.misc.savename,'structured')
% use a structured savename
if S.solver.run_as_batch_job
result_filename = [S.subject.name '_job' num2str(S.solver.job_id)];
else
cond = 1;
ct = 1;
while cond
result_filename = [S.subject.name '_v' num2str(ct)];
if ~isfile(fullfile(OutFolder,[result_filename '.mat']))
cond = 0;
end
ct = ct+1;
end
end
S.misc.result_filename = result_filename;
elseif strcmp(S.misc.savename,'datetime')
% use system date and time
S.misc.result_filename = [S.subject.name '_' datestr(datetime,30)];
end
end
if nargout == 1
varargout{1} = S.misc.result_filename;
end
%% Start diary
t00 = tic;
Outname = fullfile(S.misc.save_folder,[S.misc.result_filename '_log.txt']);
diary(Outname);
disp(' ')
disp(['Subject name: ' S.subject.name])
disp(['OpenSim model: ' osim_path])
disp(' ')
disp(' ')
%% PreProcessing
addpath([S.misc.main_path '\PreProcessing'])
disp('Start PreProcessing...')
disp(' ')
t0 = tic;
[S,model_info] = PreProcessing(S,osim_path);
disp(' ')
disp(['...PreProcessing done. Time elapsed ' num2str(toc(t0),'%.2f') ' s'])
disp(' ')
disp(' ')
%% Creating casadi functions
addpath([S.misc.main_path '\CasadiFunctions'])
addpath([S.misc.main_path '\ModelComponents'])
disp('Start creating CasADi functions...')
disp(' ')
t0 = tic;
[f_casadi] = createCasadiFunctions(S,model_info);
disp(' ')
disp(['...CasADi functions created. Time elapsed ' num2str(toc(t0),'%.2f') ' s'])
disp(' ')
disp(' ')
%% Formulating OCP
addpath([S.misc.main_path '\OCP'])
if ~S.post_process.rerun
OCP_formulation(S,model_info,f_casadi);
disp(' ')
disp(' ')
end
%% PostProcessing
addpath([S.misc.main_path '\PostProcessing'])
disp('Start PostProcessing...')
disp(' ')
t0 = tic;
PostProcessing(S,model_info,f_casadi);
disp(' ')
disp(['...PostProcessing done. Time elapsed ' num2str(toc(t0),'%.2f') ' s'])
disp(' ')
disp(' ')
%% Conclude diary
disp(['Total time elapsed ' num2str(toc(t00),'%.2f') ' s'])
disp(' ')
disp(['Diary saved as ' Outname])
disp(' ')
disp(' ')
diary off