-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessRB.m
278 lines (234 loc) · 6.48 KB
/
accessRB.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
% this is a file to look closer to the RB components that were created in
% createRB.m and compare truth/RB/DEIM solutions
clear all, clc
% units: geo or none
unit = 'geo';
%% paths/directories
% path to LaMEM executable
lamem = '"/home/chris/software/LaMEM/bin/opt/LaMEM"';
% add path to LaMEM matlab directory
addpath('/home/chris/software/LaMEM/matlab')
% path to src folder
addpath('/home/chris/Desktop/MA/RB_Stokes/reduced_basis_generation/src');
% LaMEM input file
input = '"Subduction2D.dat"';
% setup function file
setup = 'setup2D';
% name of folder to store RB components
folder = '../RB_runs/m6_30_40_max';
%% ======== length of the domain in x,y and z direction ===================
coordx = 700;
coordy = 20;
coordz = 700;
nel_x = 32;
nel_y = 2;
nel_z = 32;
% example that lies in parameter space for RB problem
par1 = 32.456;
par2 = 39.9;
par = [par1,par2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_p = nel_x*nel_y*nel_z;
n_vel = ((nel_x+1)*nel_y*nel_z)+(nel_x*(nel_y+1)*nel_z)+(nel_x*nel_y*(nel_z+1));
n_tot = n_vel + n_p;
%% scaling factor to have velocities in cm/yr
length = 1000;
time = 1e11;
cm_yr = 0.01/(3600*24*365.25);
if (unit == 'geo')
fac = length/time/cm_yr; % for geo units
else
fac = 1; % if nondimensional
end
%% load components
cd(folder);
load('B.mat');
load('eta_DEIM.mat');
load('M.mat');
load('M_DEIM.mat');
load('res_max.mat');
load('rho_DEIM.mat');
load('rhs_bl.mat');
load('rhs_bl_DEIM.mat');
cd ('/home/chris/Desktop/MA/RB_Stokes/reduced_basis_generation/');
%% ================= check solutions ======================================
% markers
feval(setup,par);
% run simulation
[t1,t2] = system([lamem,' -ParamFile ', input]);
[t1,t2] = system([lamem,' -ParamFile ', input,' -only_matrix']);
%read data
A = PetscBinaryRead('Matrices/Ass_A.bin');
Ml = PetscBinaryRead('Matrices/Ass_M.bin');
rhs = -PetscBinaryRead('Matrices/rhs.bin');
rho = PetscBinaryRead('Matrices/rho.bin');
eta = PetscBinaryRead('Matrices/eta.bin');
sol_lamem = PetscBinaryRead('Matrices/sol.bin');
% interpolate rho values
rho_i = interpol_rho(nel_x, nel_y, nel_z, rho);
if (unit == 'geo')
rho_i = rho_i * 1e19;
end
rho_i_deim = rho_i;
rho_i = nonzeros(rho_i);
%% truth solution from LaMEM
u_tl = sol_lamem;
u_tl(1:n_vel) = u_tl(1:n_vel)*fac;
%% truth solution with direct solver
J = A-Ml;
tic
u_t = J\rhs;
toc
u_t(1:n_vel) = u_t(1:n_vel)*fac;
%% RB solution without DEIM
% a few conts
m = size(B(1,:),2);
eta = [eta; 1];
N_K = size(M(1,1,:),3);
K = sparse(m,m);
N_R = size(rhs_bl(1,:),2);
f = sparse(m,1);
% assemble matrix with precomputed matrices
disp('direct solve with reduced basis by assembling the matrix with precomputed matrices:');
% assemble K
for i = 1:N_K
K = K + (eta(i)*M(:,:,i));
end
% assemble rhs
for i = 1:N_R
f = f + (rho_i(i)*rhs_bl(:,i));
end
% solve RB system
tic
alpha = K\f;
u_RB = B * alpha;
toc
u_RB(1:n_vel) = u_RB(1:n_vel)*fac;
%% RB solution with DEIM
eta = PetscBinaryRead('Matrices/eta.bin');
% assemble matrix with precomputed DEIM matrices
disp('direct solve with reduced basis with DEIM:');
m = size(B(1,:),2);
N_K = size(M_DEIM(1,1,:),3);
Kd = sparse(m,m);
ct_eta = eta_DEIM*eta;
ct_eta = [ct_eta; 1];
N_R = size(rhs_bl_DEIM(1,:),2);
fd = sparse(m,1);
ct_rho = rho_DEIM*rho_i_deim;
for i = 1:N_K
Kd = Kd + (ct_eta(i)*M_DEIM(:,:,i));
end
for i = 1:N_R
fd = fd + (ct_rho(i)*rhs_bl_DEIM(:,i));
end
tic
alphad = Kd\fd;
u_DEIM = B * alphad;
toc
u_DEIM(1:n_vel) = u_DEIM(1:n_vel)*fac;
%% calculate differences - overall
tr_RB = abs(u_t)- abs(u_RB);
tr_D = abs(u_t)- abs(u_DEIM);
RB_D = abs(u_RB)- abs(u_DEIM);
max_trRB = max(tr_RB);
max_tD = max(tr_D);
max_RBD = max(RB_D);
%% calculate differences - velocity
v_t = u_t(1:n_vel);
v_RB = u_RB(1:n_vel);
v_D = u_DEIM(1:n_vel);
v_tr_RB = abs(v_t)- abs(v_RB);
v_tr_D = abs(v_t)- abs(v_D);
v_RB_D = abs(v_RB)- abs(v_D);
maxv_trRB = max(v_tr_RB);
maxv_tD = max(v_tr_D);
maxv_RBD = max(v_RB_D);
%% ========= plot velocities =====================================================
%
% plotting y velocity
figure(1)
% thruth solution
subplot(2,3,1)
sgtitle('z - velocity in xz plane');
[V3d_t] = arrange_vel (nel_x, nel_y, nel_z, coordx, coordy, coordz, v_t,'z','xz');
x = linspace(0,coordx,nel_x);
y = linspace(0,coordz,nel_z+1);
[X,Y] = meshgrid(x,y);
pcolor(X,Y,V3d_t(:,:,2).'); c = colorbar;
c.Label.String = 'v_z [cm/yr]';
set(gca,'XTickLabel',[]);
shading interp;
title('thruth solution');
%xlabel('x');
ylabel('z [km]');
% RB velocity solution
subplot(2,3,2)
[V3d_RB] = arrange_vel (nel_x, nel_y, nel_z, coordx, coordy, coordz, v_RB,'z','xz');
x = linspace(0,coordx,nel_x);
y = linspace(0,coordz,nel_z+1);
[X,Y] = meshgrid(x,y);
pcolor(X,Y,V3d_RB(:,:,2).'); c = colorbar;
c.Label.String = 'v_z [cm/yr]';
set(gca,'XTickLabel',[]);
set(gca,'YTickLabel',[]);
shading interp;
title('RB solution');
%xlabel('x');
%ylabel('z');
% DEIM solution
subplot(2,3,3)
[V3d_DEIM] = arrange_vel (nel_x, nel_y, nel_z, coordx, coordy, coordz, v_D,'z','xz');
x = linspace(0,coordx,nel_x);
y = linspace(0,coordz,nel_z+1);
[X,Y] = meshgrid(x,y);
pcolor(X,Y,V3d_DEIM(:,:,2).'); c = colorbar;
c.Label.String = 'v_z [cm/yr]';
set(gca,'XTickLabel',[]);
set(gca,'YTickLabel',[]);
shading interp;
title('RB with DEIM');
%xlabel('x');
%ylabel('z');
% difference truth/RB
subplot(2,3,4)
diff_3D = V3d_t-V3d_RB;
%diff_3D = diff_3D/max(max(V_3d_t(:,:,14)));
[X,Y] = meshgrid(x,y);
pcolor(X,Y,diff_3D(:,:,2).'); c = colorbar;
c.Label.String = ' dv_z [cm/yr]';
shading interp;
title('difference btw thruth & RB');
xlabel('x [km]');
ylabel('z [km]');
% difference truth/DEIM
subplot(2,3,5)
diff_3D = V3d_t-V3d_DEIM;
%diff_3D = diff_3D/max(max(V_3d_t(:,:,14)));
[X,Y] = meshgrid(x,y);
pcolor(X,Y,diff_3D(:,:,2).'); c = colorbar;
c.Label.String = ' dv_z [cm/yr]';
set(gca,'YTickLabel',[]);
shading interp;
title('difference btw thruth & DEIM');
xlabel('x [km]');
%ylabel('z');
% difference RB/DEIM
subplot(2,3,6)
diff_3D = V3d_RB-V3d_DEIM;
%diff_3D = diff_3D/max(max(V_3d_t(:,:,14)));
[X,Y] = meshgrid(x,y);
pcolor(X,Y,diff_3D(:,:,2).'); c = colorbar;
c.Label.String = ' dv_z [cm/yr]';
set(gca,'YTickLabel',[]);
shading interp;
title('difference btw RB & DEIM');
xlabel('x [km]');
%ylabel('z');
%% plot residual
%% plot max residual after adding a basis function
figure(2)
semilogy(res_max,'*-');
grid on;
ylabel('max residual');
xlabel('number of basis functions');