rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
twophase_flow.m
1 function outfile = twophase_flow(steps, params)
2  if nargin == 0
3  steps = [0, 1];
4  end
5  if nargin <= 1
6  params = [];
7  end
8 
9  if ~isfield(params, 'id')
10  params.id = '';
11  end
12 
13  if ~isfield(params, 'size')
14  params.size = 0.2;
15  end
16  if ~isfield(params, 'mode')
17  params.mode = 'normal';
18  end
19 
20  mkdir(fullfile(rbmatlabresult, 'TwoPhaseFlow'));
21  outfile_prefix = fullfile(rbmatlabresult, 'TwoPhaseFlow', params.id, 'Script');
22  imsavepath = fullfile(getenv('BASEDIR'), 'images', 'TwoPhaseFlow');
23  [dummy, sha] = system('git log --no-color --format=''%H'' HEAD | head -1');
24  outfile_elems = {'params', 'sha'};
25 
26  for si = 1:length(steps)
27 
28  step = steps(si);
29 
30  switch(step)
31  case 0
32  descr = twophase_descr(params);
33 
34  dmodel = gen_detailed_model(descr);
35 
36  model_data = gen_model_data(dmodel);
37 
38  bg_descr.rb_problem_type = 'TwoPhaseFlow';
39  bg_descr.train_sample_mode = 'single';
40  bg_descr.pca = true;
41 
42  rmodel = gen_reduced_model(dmodel, bg_descr);
43 
44  outfile_elems = [ outfile_elems, {'descr', 'dmodel', 'model_data', 'rmodel'}];
45  outfile = [outfile_prefix, 'Step0'];
46  save(outfile, outfile_elems{:});
47  case 1
48  oldoutfile = [outfile_prefix, 'Step0'];
49  load(oldoutfile);
50  outfile_elems = unique([outfile_elems, matfile_fields(oldoutfile)]);
51 
52  tic;
53  sim_data = detailed_simulation(dmodel, model_data);
54  t = toc;
55  sim_data.tictoc = t;
56 
57  outfile_elems = [ outfile_elems, {'sim_data'}];
58  outfile = [outfile_prefix, 'Step1'];
59  save(outfile, outfile_elems{:});
60 
61  case 2
62  if exist([outfile_prefix, 'Step1.mat'], 'file')
63  oldoutfile = [outfile_prefix, 'Step1'];
64  else
65  oldoutfile = [outfile_prefix, 'Step0'];
66  end
67  load(oldoutfile);
68  outfile_elems = unique([outfile_elems, matfile_fields(oldoutfile)]);
69  if exist([outfile_prefix, 'Step1'], 'file')
70  load([outfile_prefix, 'Step1']);
71  end
72 
73  detailed_data = gen_detailed_data(rmodel, model_data);
74 
75  outfile_elems = [ outfile_elems, {'detailed_data'}];
76  outfile = [outfile_prefix, 'Step2'];
77  save(outfile, outfile_elems{:});
78  case 3
79  oldoutfile = [outfile_prefix, 'Step2'];
80  load(oldoutfile);
81  outfile_elems = unique([outfile_elems, matfile_fields(oldoutfile)]);
82  reduced_data = gen_reduced_data(rmodel, detailed_data);
83  tic;
84  rb_sim_data = rb_simulation(rmodel, reduced_data);
85  t = toc;
86  rb_sim_data.tictoc = t;
87 
88  tic;
89  rb_sim_data = rb_reconstruction(rmodel, detailed_data, rb_sim_data);
90  t = toc;
91  rb_sim_data.rtictoc = t;
92 
93  plot_sim_data(dmodel, model_data, rb_sim_data, []);
94 
95  if exist('sim_data', 'var')
96  l2_errs = dmodel.l2_error_sequence_complete(rb_sim_data, sim_data, detailed_data.model_data);
97  linfty_errs = dmodel.linfty_error_sequence_complete(rb_sim_data, sim_data, detailed_data.model_data);
98 
99  figure;
100  subplot(1,2,1); plot_error_sequence(dmodel, l2_errs); title('L^2-error');
101  subplot(1,2,2); plot_error_sequence(dmodel, linfty_errs); title('L^{\infty}-error');
102 
103  data = [0:rmodel.descr.dt:rmodel.descr.T; ...
104  l2_errs.S; linfty_errs.S; ...
105  l2_errs.U; linfty_errs.U; ...
106  l2_errs.P; linfty_errs.P];
107  datafile = fullfile(imsavepath, 'errors_over_time.dat');
108 
109  print_datatable(datafile, {'t', 'sat_l2', 'sat_linfty', 'vel_l2', 'vel_linfty', 'prs_l2', 'prs_linfty'}, data);
110 
111  tmpcell = cellfun(@(X) rb_sim_data.(X) - sim_data.(X), {'S', 'U', 'P'}, 'UniformOutput', false);
112  diff_data_rb=cell2struct(tmpcell, {'S', 'U', 'P'}, 2);
113  plot_sim_data(dmodel, model_data, diff_data_rb, []);
114 
115  datafile = fullfile(imsavepath, 'basis_gen.dat');
116  N = reduced_data.N;
117  Ns = get_by_description(N, 'saturation');
118  Nu = get_by_description(N, 'velocity');
119  Np = get_by_description(N, 'pressure');
120  M = reduced_data.M;
121  Ms = get_by_description(M, 'L_saturation');
122  Mu = get_by_description(M, 'L_velocity');
123  l2_errs.tot = l2_errs.S + l2_errs.U + l2_errs.P;
124  linfty_errs.tot = linfty_errs.S + linfty_errs.U + linfty_errs.P;
125  data = [ Ns, Nu, Np, Ms, Mu, ...
126  model_data.grid.nelements, ...
127  max(l2_errs.S), max(linfty_errs.S), ...
128  max(l2_errs.U), max(linfty_errs.U), ...
129  max(l2_errs.P), max(linfty_errs.P), ...
130  max(l2_errs.tot), max(linfty_errs.tot), ...
131  sim_data.tictoc, rb_sim_data.rtictoc]';
132 
133  print_datatable(datafile, {'Ns', 'Nu', 'Np', 'Ms', 'Mu', 'H', 'sat_l2', 'sat_linfty', 'vel_l2', 'vel_linfty', 'prs_l2', 'prs_linfty', 'l2', 'linfty', 'time', 'rtime'}, data);
134  end
135 
136  outfile_elems = [ outfile_elems, {'reduced_data', 'rb_sim_data'}];
137  outfile = [outfile_prefix, 'Step3'];
138  save(outfile, outfile_elems{:});
139 
140  end
141  end
142 
143 end
144 
145 function fnames = matfile_fields(matfile)
146  temp = whos('-file', matfile);
147  fnames = arrayfun(@(X) X.name, temp, 'UniformOutput', false)';
148 end