-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPFC_2D_CN_SAV_PreCor.m
252 lines (211 loc) · 6.98 KB
/
MPFC_2D_CN_SAV_PreCor.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
function [phi, r0] = MPFC_2D_CN_SAV_PreCor(pde,domain,Nx,Ny,time,phi_p,option)
% Solve 2D phase field crystal equaiton
% (1) bar_beta*phi_tt + beta\phi_t = \Delta \mu
% (2) \mu = (1+\Delta)^2\phi + (\phi^3 - epsilon\phi)
% (2) \mu = (1+\Delta)^2\phi + \beta*phi + \phi*(\phi^2 - epsilon - \beta)
% Qi Li
% 01/16/2019
global dt epsilon M k2 k4 beta_bar beta alpha C0 hx hy Lx Ly sigma
if ~exist('option','var'), option = []; end
if ~isfield(option,'tol')
option.tol = 10^-14; % default tol
end
if ~isfield(option,'tolit')
option.tolit = 10^-8; % default tolit
end
if ~isfield(option,'maxit')
option.maxit = 2000; % default maxit
end
if ~isfield(option,'plotflag')
option.plotflag = 0;
end
if ~isfield(option,'saveflag')
option.saveflag = 0;
end
if ~isfield(option,'savefinal')
option.savefinal = 0;
end
if ~isfield(option,'printflag')
option.printflag = 0;
end
if ~isfield(option,'vtkflag')
option.printflag = 0;
end
if ~isfield(option,'energyflag')
option.energyflag = 0;
end
if 1 == option.energyflag
figname_mass = [pde.name,'e',num2str(pde.epsilon),'M',num2str(pde.M),'b_bar',num2str(pde.beta_bar),'b',num2str(pde.beta),'dt',num2str(time.dt),'_mass.txt'];
figname_energy = [pde.name,'e',num2str(pde.epsilon),'M',num2str(pde.M),'b_bar',num2str(pde.beta_bar),'b',num2str(pde.beta),'dt',num2str(time.dt),'_energy.txt'];
out1 = fopen(figname_mass,'w');
out2 = fopen(figname_energy,'w');
end
tol = option.tol;
tolit = option.tolit;
maxit = option.maxit;
%%
T = time.T;
t = time.t0;
dt = time.dt;
tsave = time.tsave;
dir_fig = [pde.name '/fig'];
dir_data = [pde.name '/data'];
epsilon = pde.epsilon;
M = pde.M;
alpha = pde.alpha;
beta_bar = pde.beta_bar;
beta = pde.beta;
sigma = pde.sigma;
Lx = domain.right - domain.left;
Ly = domain.top - domain.bottom;
hx = Lx/Nx;
hy = Ly/Ny;
% x = domain.left + hx*(1:Nx);
% y = domain.bottom + hy*(1:Ny);
x = domain.left + hx*(0:Nx-1);
y = domain.bottom + hy*(0:Ny-1);
[xx,yy] = meshgrid(x,y);
phi0 = pde.init(xx,yy);
psi0 = zeros(size(phi0));
nfigure =1;
[k_x,k_y,kx,ky,kxx,kyy,k2,k4] = prepare_fft2(Lx,Ly,Nx,Ny);
nplot = round((T-t)/dt);
nsave = round(tsave/dt);
C0 = (epsilon+alpha).^2*Lx*Ly;
r0 = fun_r_init(phi0);
tstart = tic;
% Initial energy
if 1 == option.energyflag
calculate_energy(out1,out2,hx,hy,t,phi0,psi0,r0,C0);
end
for nt = 1:nplot
t = t+dt;
% phi_star = 1/2*(3*phi1-phi0);
phi_star = phi_p;
% step 1
H = fun_H(phi_star);
if isfield(pde,'rhs') && isfield(pde,'exact')
rhs = dt*pde.rhs(xx,yy,t-dt/2);
else
rhs = 0;
end
g = get_rhs(phi0,psi0,r0,H)+rhs;
psiA = inv_A(lap_diff(H));
psiB = inv_A(g);
gamma = -fft2(H.*psiA);
gamma = gamma(1,1)*hx*hy;
% Step 2
Hphi = fft2(H.*psiB);
Hphi = Hphi(1,1)*hx*hy/(1+dt*M*gamma/4);
% Step 3
phi = dt*M/4*Hphi.*psiA + psiB;
%% update phi0
r0 = fun_r(phi,phi0,r0,H);
psi1 = fun_psi(phi,phi0,psi0);
phi0 = phi;
if 1 == option.energyflag
calculate_energy(out1,out2,hx,hy,t,phi,psi1,r0,C0);
end
if 0 == mod(nt,nsave)
if 1 == option.printflag
timeElapsed = toc(tstart);
fprintf('epsilon=%.3f,t=%.4f/%.f, dt=%.4e, Nx=%d, Ny=%d, timeElapsed=%f\n',epsilon,t,T,dt,Nx,Ny,timeElapsed);
end
if 1 == option.saveflag
ss = [dir_data '/phi_t=' num2str(t) '.txt'];
fid = fopen(ss, 'wt');
fprintf(fid, '%f\n', phi(:));
fclose(fid);
end
nfigure = nfigure +1;
if 1 == option.plotflag
if 1 == option.vtkflag
write_vtk_grid_values(dir_data,x,y,nt,phi0);
end
if 1 == option.saveflag
showsolution_2D(nfigure,xx,yy,phi,t,dir_fig);
else
showsolution_2D(nfigure,xx,yy,phi,t);
end
end
end
end
if 1 == option.savefinal
name= [pde.name,'e',num2str(pde.epsilon),'M',num2str(pde.M),'b_bar',num2str(pde.beta_bar),'b',num2str(pde.beta),'Nx=',num2str(Nx),'Ny=',num2str(Ny),'dt=',num2str(dt)];
filename=[name '.mat'];
save(filename,'epsilon','M','x','y','hx','hy','Nx','Ny','dt','T','phi','domain');
% showsolution_2D(nfigure,xx,yy,phi,t,'./','.fig');
end
if 1 == option.energyflag
fclose(out1);
fclose(out2);
end
end
function r = fun_psi(phi,phi0,psi1)
global dt
r = 2*(phi-phi0)/dt-psi1;
end
function r = fun_r_init(phi)
global C0 hx hy
E1 = fft2(F(phi));
r = sqrt(E1(1,1)*hx*hy + C0);
end
function r = fun_r(phi,phi0,r0,b)
global hx hy
bphi0 = fft2(b.*phi0);
bphi0 = bphi0(1,1)*hx*hy;
bphi1 = fft2(b.*phi);
bphi1 = bphi1(1,1)*hx*hy;
g1 = r0 - 1/2*bphi0;
r = 1/2*bphi1+g1;
end
function r = fun_H(phi)
global C0 hx hy
E1 = fft2(F(phi));
r = F_derivative(phi)./sqrt(E1(1,1)*hx*hy+C0);
end
function r = get_rhs(phi1,psi1,r1,b)
global beta_bar beta dt M hx hy alpha sigma Lx Ly
bphiphi = fft2(b.*phi1);
bphiphi = bphiphi(1,1)*hx*hy;
h2 = -2/dt*phi1-psi1;
gg = dt/(2*beta_bar+dt*beta)*fft2(-(beta_bar+dt*beta/2)*h2 + (beta_bar-dt*beta/2)*psi1);
gg = (gg + fft2(phi1))/2;
r = -(beta_bar+beta/2*dt)*h2 +(beta_bar-dt*beta/2)*psi1 ...
+dt*M/2*lap_diff(phi1+2*lap_diff(phi1)+lap_diff(lap_diff(phi1))) ...
+alpha/2*dt*M*lap_diff(phi1) ...
+dt*M/2*(2*r1-1/2*bphiphi).*lap_diff(b)...
-sigma*dt*M/2*phi1 + sigma*dt*M*gg(1,1)*hx*hy/(Lx*Ly);
end
function lap=lap_diff(phi)
global k2
lap=real(ifft2((k2.*fft2(phi))));
end
function r = inv_A(phi)
global dt M k2 alpha beta_bar beta sigma
r = real(ifft2(fft2(phi)./((beta_bar+dt*beta/2)*2/dt-dt*M/2*k2.*(1+k2).^2-dt*M/2*alpha.*k2+sigma*dt*M/2)));
end
function [] = calculate_energy(out1,out2,hx,hy,t,phi,psi,r,C0)
global M epsilon alpha beta_bar k2 Lx Ly sigma
energy1 = hx*hy*sum(sum( 1/4*phi.^4 - epsilon/2*phi.^2+ 1/2*phi.^2 + lap_diff(phi).*phi + 1/2*lap_diff(phi).^2 + alpha/2*phi.^2));
Fpsi = fft2(psi)./k2; Fpsi(1,1) = 0; psi_inv_lap = real(ifft2(Fpsi));
ene = -0.5/M*beta_bar*hx*hy*sum(sum(psi_inv_lap.*psi));
phi_hat = fft2(phi);
psi_OK = phi - phi_hat(1,1)*hx*hy/(Lx*Ly);
Fpsi = fft2(-psi_OK)./k2; Fpsi(1,1) = 0; psi_OK = real(ifft2(Fpsi));
psi_hat = fft2(-lap_diff(psi_OK).*psi_OK);
ene2 = 0.5*sigma*psi_hat(1,1)*hx*hy;
energy2 = energy1 + ene;
energy3 = hx*hy*sum(sum(1/2*phi.^2 + lap_diff(phi).*phi + 1/2*lap_diff(phi).^2 + alpha/2*phi.^2)) + r.^2 - C0 + ene;
mass = hx*hy*sum(sum( phi ));
fprintf(out1,'%14.6e %f \n',t,mass);
fprintf(out2,'%14.6e %f %f %f \n',t,energy1+ene2,energy2+ene2,energy3+ene2);
end
function r = F_derivative(phi0)
global alpha epsilon
r = phi0.*(phi0.^2 - epsilon - alpha);
end
function r = F(phi)
global alpha epsilon
r = 1/4*phi.^4 - epsilon/2*phi.^2 - alpha/2*phi.^2;
end