-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
428 lines (322 loc) · 14 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function main runs the sequence of codes needed to create (train) and
% test the surrogate ion energy-angle distribution (IEAD) models as well as
% to compute the sensitivity indices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Discussion
% ----------
% Tasmanian directory:
% - This directory is where the tasmanian sparse grids containing the
% moments and IEAD surrogate models are stored.
% - The main function uses this directory to check whether a
% surrogate model already exists.
% Data:
% The data needs to be stored in the directories defined under the
% section "Data Directories". The data contains:
% (1) .log files with information about the different cases
% (each case represents a different combination of physical
% parameters). These files should be placed directly in the
% chosen directories.
% (2) particle data which should be placed within a subdirectory
% "data/" inside the chosen directories.
% Functions:
% The functions are as follows:
% - "MomentsSurrogate": Creates the moments surrogate models.
% - "IEADstart_data_computation": Computes (training or testing)
% data in the form of IEAD and IEAD* (IEAD in the transformed
% coordinates).
% - "IEADstarSurrogate": Creates the IEAD surrogate models.
% - "IEADstarSurrogateError": Computes IEAD surrogate (training
% or testing) errors.
% - "plot_IEADstar_model_vs_data": Plots comparisons between the
% IEAD surrogate models and the (training or testing) IEAD data.
% - "GSA_Si": Computes sensitivity indices.
% - "GSA_Moments_2D": Computes angle and energy moments profiles
% (mean and standard deviation) using the 2D IEAD surrogate models
% (only for 2D).
% Note:
% The various inputs for the functions are as follows:
% - sglevel: (training) data sparse grid level: "7", "10", or "13"
% - dim: "2" (for 2D) or "4" (for 4D)
% - Directory: "Directory_train" for training data
% "Directory_test" for testing data
% - flag_plot: flag to produce plots
% - data_type: 'train' for training and 'test' for testing
% - N: number of samples
% - Npoints: number of samples per dimension
% Input:
% The code runs by default for sglevel = 7 & dim = 2.
% These values are hardcoded but can be modified under "Input".
% Author: Pablo Seleson
% ------
% Last Modified: March 10, 2022
% -------------
function main
clear all
close all
clc
% ====================================================================
% Input
% ====================================================================
% Parameter space dimension
dim = 2;
% Data sparse grid level
sglevel = 7;
% Flag to plot
flag_plot = 1;
% Check dimension
if dim ~= 2 && dim ~= 4
error('dim should be 2 or 4.')
end
% Sparse grid level
if sglevel ~= 7 && sglevel ~= 10 && sglevel ~= 13
error('sglevel should be 7, 10, or 13.')
end
fprintf(' ======================================================== \n')
fprintf(' %gD hPIC IEAD Surrogate Model \n',dim)
fprintf(' Sparse Grid Level: %g \n',sglevel)
fprintf(' ======================================================== \n\n')
% ====================================================================
% Tasmanian Directory
% ====================================================================
Directory_tsg = '';
% ====================================================================
% Data Directories
% ====================================================================
% Training data directory
if dim == 2
% Directory of data for 2D case
Directory_train = '';
else
% Directory of data for 4D case
Directory_train = '';
end
% Testing data directory
if dim == 2
% Directory of data for 2D case
Directory_test = '';
else
% Directory of data for 4D case
Directory_test = '';
end
% ====================================================================
% Create data folders
% ====================================================================
if dim == 2
if ~exist('IEAD_data_2D', 'dir')
mkdir('IEAD_data_2D')
end
else
if ~exist('IEAD_data_4D', 'dir')
mkdir('IEAD_data_4D')
end
end
% ====================================================================
% Create moments surrogate model
% ====================================================================
fprintf(' ---------------- Moments surrogate model --------------- \n\n')
% Check if moments surrogate model already exists
Moments_surrogate_filename = [Directory_tsg 'LS_' num2str(dim) 'D_Grid_Moments_level_' num2str(sglevel) '_FileG'];
% Initialize default answer
str = 'N';
if isfile(Moments_surrogate_filename)
% File exists
fprintf('Moments surrogate model for %gD level %g exists: \n',dim,sglevel)
prompt = 'Build the moments surrogate model again? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
end
if strcmp( str, 'Y' ) || ~isfile(Moments_surrogate_filename)
tic
fprintf('Building moments surrogate model ... \n\n')
% Run function
MomentsSurrogate(sglevel,dim,Directory_train,flag_plot)
fprintf('Moments surrogate model built: time = %f (sec) \n\n', toc);
end
% ====================================================================
% Create IEAD training data
% ====================================================================
fprintf(' ---------------- IEAD training data --------------- \n\n')
% Data type
data_type = 'train';
% Check if IEAD training data already exists
IEAD_traindata_filename = ['IEAD_data_' num2str(dim) 'D/Level_' num2str(sglevel) '_' data_type '.mat'];
% Initialize default answer
str = 'N';
if isfile(IEAD_traindata_filename)
% File exists
fprintf('IEAD training data for %gD level %g exists: \n',dim,sglevel)
prompt = 'Create IEAD training data again? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
end
if strcmp( str, 'Y' ) || ~isfile(IEAD_traindata_filename)
tic
fprintf('Generating IEAD training data ... \n\n')
% Run function
IEADstart_data_computation(sglevel,dim,Directory_train,data_type)
fprintf('\nIEAD training data generated: time = %f (sec) \n\n', toc);
end
% ====================================================================
% Generate IEAD surrogate model
% ====================================================================
fprintf(' ---------------- IEAD surrogate model --------------- \n\n')
% Check if IEAD* surrogate model already exists
IEADstar_surrogate_filename = [Directory_tsg 'LS_' num2str(dim) 'D_Grid_IEADstar_level_' num2str(sglevel) '_FileG'];
% Initialize default answer
str = 'N';
if isfile(IEADstar_surrogate_filename)
% File exists
fprintf('IEAD surrogate model for %gD level %g exists: \n',dim,sglevel)
prompt = 'Build the IEAD surrogate model again? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
end
if strcmp( str, 'Y' ) || ~isfile(IEADstar_surrogate_filename)
tic
fprintf('Building IEAD surrogate model ... \n\n')
% Run function
IEADstarSurrogate(sglevel,dim)
fprintf('IEAD surrogate model built: time = %f (sec) \n\n', toc);
end
% ====================================================================
% Compute and plot IEAD training error
% ====================================================================
fprintf(' ---------------- IEAD training error --------------- \n\n')
prompt = 'Would you like to compute the IEAD training error? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
tic
fprintf('Computing IEAD training error ... \n\n')
% Run function
data_type = 'train';
IEADstarSurrogateError(sglevel,dim,data_type)
fprintf('IEAD training error computed: time = %f (sec) \n\n', toc);
end
% ===================================================================
% Compare IEAD model with the training data
% ===================================================================
fprintf(' ------------ IEAD model vs. (training) data ----------- \n\n')
prompt = 'Would you like to compare the IEAD model with the (training) data in the transformed coordinates? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
data_type = 'train';
% IEAD*
plot_IEADstar_model_vs_data(sglevel,dim,data_type)
end
prompt = 'Would you like to compare the IEAD model with the (training) data in the original coordinates? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
data_type = 'train';
% IEAD
plot_IEAD_model_vs_data(sglevel,dim,data_type)
end
% ===================================================================
% Create IEAD testing data
% ===================================================================
fprintf(' ---------------- IEAD testing data --------------- \n\n')
% Data type
data_type = 'test';
% Check if IEAD testing data already exists
IEAD_testdata_filename = ['IEAD_data_' num2str(dim) 'D/Level_' num2str(sglevel) '_' data_type '.mat'];
% Initialize default answer
str = 'N';
if isfile(IEAD_testdata_filename)
% File exists
fprintf('IEAD testing data for %gD level %g exists: \n',dim,sglevel)
prompt = 'Create IEAD testing data again? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
end
if strcmp( str, 'Y' ) || ~isfile(IEAD_testdata_filename)
tic
fprintf('Generating IEAD testing data ... \n\n')
% Run function
IEADstart_data_computation(sglevel,dim,Directory_test,data_type)
fprintf('\nIEAD testing data generated: time = %f (sec) \n\n', toc);
end
% ====================================================================
% Compute and plot IEAD testing error
% ====================================================================
fprintf(' ---------------- IEAD testing error --------------- \n\n')
prompt = 'Would you like to compute the IEAD testing error? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
tic
fprintf('Computing IEAD testing error ... \n\n')
% Run function
data_type = 'test';
IEADstarSurrogateError(sglevel,dim,data_type)
fprintf('IEAD testing error computed: time = %f (sec) \n\n', toc);
end
% ====================================================================
% Compare IEAD model with the testing data
% ====================================================================
fprintf(' ------------ IEAD model vs. (testing) data ----------- \n\n')
prompt = 'Would you like to compare the IEAD model with the (testing) data in the transformed coordinates? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
data_type = 'test';
% IEAD*
plot_IEADstar_model_vs_data(sglevel,dim,data_type)
end
prompt = 'Would you like to compare the IEAD model with the (testing) data in the original coordinates? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
data_type = 'test';
% IEAD
plot_IEAD_model_vs_data(sglevel,dim,data_type)
end
% ===================================================================
% Compute sensitivity indices
% ===================================================================
fprintf(' --------------- Sensitivity indices --------------- \n\n')
prompt = 'Would you like to compute sensitivity indices? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
prompt = 'Enter number of samples N (default 50000): ';
str = input(prompt,'s');
fprintf('\n')
% Number of samples
N = str2num(str);
tic
fprintf('Computing IEAD sensitivity indices ... \n\n')
% Run function
GSA_Si(sglevel,dim,N)
fprintf('\nSensitivity indices computed: time = %f (sec) \n\n', toc);
end
% ===================================================================
% Compute angle and energy moments profiles
% ===================================================================
if dim == 2
fprintf(' --------------- Angle and energy moments --------------- \n\n')
prompt = 'Would you like to compute angle and energy moments profiles? [Y/N]: ';
str = input(prompt,'s');
fprintf('\n')
if strcmp( str, 'Y' )
close all
prompt = 'Enter number of samples per dimension Npoints (default 250): ';
str = input(prompt,'s');
fprintf('\n')
% Number of samples per dimension in 2D
Npoints = str2num(str);
tic
fprintf('Computing angle and energy moments ... \n\n')
% Run function
GSA_Moments_2D(sglevel, Npoints)
fprintf('\nAngle and energy moments computed: time = %f (sec) \n\n', toc);
end
end
end