-
Notifications
You must be signed in to change notification settings - Fork 2
/
Subzero.m
380 lines (312 loc) · 14.8 KB
/
Subzero.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
close all; clear all;
paths
%% Set Flags
RIDGING=false;
FRACTURES=false;
PERIODIC=false;
PACKING = false;
WELDING = false;
CORNERS = true;
COLLISION = true;
AVERAGE = false; %average certain values in time
RAFTING = false;
KEEP_MIN = false; %Retain floes in simulation that get smaller set threshold
ifPlot = true; %Plot floe figures or not?
ifPlotStress = false; %Plot floe figures with floes shaded by stress values
ifPlotStressStrain = false; %Create plot with homogenized stress and strain values
justfrac = false;
%% Initialize model vars
dt=10; %Time step in sec
height.mean = 0.25; %mean value of thickness for initial floes
height.delta = 0; %maximum deviation about the mean thickness if a distribution is desired
nDTpack = 500; %Time streps between creation of new floes
rho_ice=920; % kg/m3
[ocean, HFo, h0]=initialize_ocean(dt,nDTpack); %Define ocean currents
%Define Atmospheric winds
winds.x = ocean.Xocn; winds.y = ocean.Yocn;
U0 = 0; V0 = 0;
winds.u=U0*ones(size(ocean.Xocn));
winds.v=V0*ones(size(ocean.Xocn));
%Define boundaries
c2_boundary=initialize_boundaries();
f=2*pi/(24*3600);
Ly = max(c2_boundary(2,:));Lx = max(c2_boundary(1,:));
min_floe_size = 4*Lx*Ly/10000; %set minimum floe size for initialization
%Initialize Floe state
target_concentration = 1; %Set target concentration for initial conditions
[Floe, Nb] = initial_concentration(c2_boundary,target_concentration,height,10,min_floe_size);
%create polyshape of any boundary floes
if Nb >0
polyboundary = union([Floe(1:Nb).poly]);
else
polyboundary = [];
end
c2_boundary_poly = polyshape(c2_boundary');
c2_border = polyshape(2*[-Lx -Lx Lx Lx; -Ly Ly Ly -Ly]'); c2_border = subtract(c2_border, c2_boundary_poly);
floebound = initialize_floe_values(c2_border, height);
if isfield(Floe,'poly')
Floe=rmfield(Floe,{'poly'});
end
min_floe_size = (4*Lx*Ly-sum(cat(1,Floe(1:Nb).area)))/20000; %set minimum floe size for simulation
%calculate elastic modulus used in floe interactions
global Modulus
Modulus = 1.5e3*(mean(sqrt(cat(1,Floe.area)))+min(sqrt(cat(1,Floe.area))));
save('Modulus.mat','Modulus');
%% Set output parameters
dhdt = 1; %atmospheric heat flux
nDTOut=150; %Output frequency (in number of time steps)
nSnapshots=50; %Total number of model snapshots to save
nDT=nDTOut*nSnapshots; %Total number of time steps
nSimp = 20; %Timesteps between simplification of floe boundaries
% Set parallel computing inputs
%nPar = 18; %Number of workers for parfor
%poolobj = gcp('nocreate'); % If no pool, do not create new one.
%if isempty(poolobj)
% parpool(nPar);
%else
% delete(poolobj);
% parpool(nPar);
%end
target_concentration=1; %Set target concentration for when creation of new elements will be run
tStart = tic;
%ocean forces need updated on less frequent time scale
doInt.flag = true;
doInt.step = 10; %Timesteps between update of ocean stresses
% specify coarse grid size
LxO= 2*max(ocean.Xo);LyO= 2*max(ocean.Yo);
Nx=10; Ny=10;%fix(Nx*LyO/LxO);
xc = min(c2_boundary(1,:)):(max(c2_boundary(1,:))-min(c2_boundary(1,:)))/Nx:max(c2_boundary(1,:));
yc = min(c2_boundary(2,:)):(max(c2_boundary(2,:))-min(c2_boundary(2,:)))/Ny:max(c2_boundary(2,:));
Xc = (xc(1:end-1)+xc(2:end))/2; Yc = -(yc(1:end-1)+yc(2:end))/2;
%initialize dissolved ice at zero
dissolvedNEW=zeros(Ny,Nx);
%Initiailize Eulearian Data
[eulerian_data] = calc_eulerian_data(Floe,Nx,Ny,Nb,c2_boundary,PERIODIC);
Vd = zeros(Ny,Nx,2);
Vdnew=zeros(Ny, Nx);
SigXX = zeros(Ny, Nx); SigYX = zeros(Ny, Nx);
SigXY = zeros(Ny, Nx); SigYY = zeros(Ny, Nx);
Eux = zeros(Ny, Nx); Evx = zeros(Ny, Nx);
Euy = zeros(Ny, Nx); Evy = zeros(Ny, Nx);
U = zeros(Ny, Nx); V = zeros(Ny, Nx);
dU = zeros(Ny, Nx); dV = zeros(Ny, Nx);
Sig = zeros(Ny, Nx); mass = zeros(Ny,Nx);
%% Calc interactions and plot initial state
Floe=Floe(logical(cat(1,Floe.alive)));
[Floe,dissolvedNEW] = floe_interactions_all(Floe, floebound, ocean, winds,c2_boundary, dt,HFo,min_floe_size,Nx,Ny,Nb, dissolvedNEW,doInt,COLLISION, PERIODIC, RIDGING, RAFTING); % find interaction points
A=cat(1,Floe.area);
Amax = max(A);
%% Initialize time and other stuff to zero
if isempty(dir('figs')); disp('Creating folder: figs'); mkdir('figs'); end
if isempty(dir('Floes')); disp('Creating folder: Floes'); mkdir('Floes'); end
if ~exist('Time','var')
Time=0;
i_step=0;
im_num=1;
fig=0;
fig2=figure('Position',[100 100 1000 500],'visible','on');
end
%% Solving for floe trajectories
tic;
while im_num<nSnapshots
if mod(i_step,10)==0
disp(' ');
toc
disp([num2str(i_step) ' timesteps comleted']);
numCollisions = calc_collisionNum(Floe);
sacked = sum(~cat(1, Floe.alive));
if sacked>0, disp(['sacked floes: ' num2str(sacked)]); end
disp(['number of collisions: ' num2str(numCollisions)]);
disp(' ');
tic
doInt.flag=true;
else
doInt.flag=false;
end
%Dynamic simplification of floe shape
if mod(i_step,nSimp)==0
FloeOld = Floe;
parfor j=1:length(FloeOld)
FloeOld(j).poly = polyshape(FloeOld(j).c_alpha'+[FloeOld(j).Xi FloeOld(j).Yi]);
end
floenew = [];
for ii = 1:length(Floe)
ParFloes(ii).floenew = [];
ParFloes(ii).kill = [];
ParFloes(ii).verts = 0;
end
if isfield(Floe,'poly')
Floe=rmfield(Floe,{'poly'});
end
parfor ii = 1:length(Floe)
floe = Floe(ii);
if length(Floe(ii).c0) > 30
[floe2,kill] = FloeSimplify(Floe(ii),0,FloeOld,polyboundary);
if isfield(floe2,'poly')
floe2=rmfield(floe2,{'poly'});
end
if isempty(kill)
ParFloes(ii).kill = [ParFloes(ii).kill kill];
end
for jj = 1:length(floe2)
if jj == 1
Floe(ii) = floe2(jj);
ParFloes(ii).verts=length(floe2(jj).c0(1,:));
else
ParFloes(ii).floenew = [ParFloes(ii).floenew floe2(jj)];
end
end
end
end
clear FloeOld
if isfield(Floe,'poly')
Floe=rmfield(Floe,{'poly'});
end
floenew =[]; kill = [];
for ii = 1:length(ParFloes)
floenew = [floenew ParFloes(ii).floenew];
kill = [kill ParFloes(ii).kill];
end
kill = unique(kill(kill>Nb));
live = cat(1,Floe.alive);
live(kill) = 0;
Floe(live==0)=[];
Floe =[Floe floenew];
end
if mod(i_step,nDTOut)==0 %plot the state after a number of timesteps
[eulerian_data] = calc_eulerian_data(Floe,Nx,Ny,Nb,c2_boundary,PERIODIC);
if ifPlot
[fig] = plot_basic(fig, Time,Floe,ocean,c2_boundary_poly,Nb,PERIODIC);
% saveas(fig,['./figs/' num2str(im_num,'%03.f') '.jpg'],'jpg');
end
%Average coarse eulerian data
if AVERAGE
SigXXa = SigXX/fix(nDTOut); SigYXa = SigYX/fix(nDTOut);
SigXYa = SigXY/fix(nDTOut); SigYYa = SigYY/fix(nDTOut);
Eux = Eux/fix(nDTOut); Evx = Evx/fix(nDTOut);
Euy = Euy/fix(nDTOut); Evy = Evy/fix(nDTOut);
U = U/fix(nDTOut); V = V/fix(nDTOut);
dU = dU/fix(nDTOut); dV = dV/fix(nDTOut);
Sig = Sig/fix(nDTOut);
mass = mass/fix(nDTOut);
else
SigXXa = squeeze(eulerian_data.stressxx); SigYXa = squeeze(eulerian_data.stressyx);
SigXYa = squeeze(eulerian_data.stressxy); SigYYa = squeeze(eulerian_data.stressyy);
Eux = squeeze(eulerian_data.strainux); Evx = squeeze(eulerian_data.strainvx);
Euy = squeeze(eulerian_data.strainuy); Evy = squeeze(eulerian_data.strainvy);
U = U+squeeze(eulerian_data.u);V = V+squeeze(eulerian_data.v);
dU = dU+squeeze(eulerian_data.du);dV = dV+squeeze(eulerian_data.dv);
Sig = Sig+squeeze(eulerian_data.stress);
end
if ifPlotStress
[fig] =plot_basic_stress(fig, Time,Floe,ocean,c2_boundary_poly,Nb);
saveas(fig,['./figs/' num2str(im_num,'%03.f') '.jpg'],'jpg');
figure(2)
plot(Xc,SigXXa,'kx','linewidth',2); title(['Time = ' num2str(Time/3600) ' hours'],'fontsize',24);
drawnow
end
if ifPlotStressStrain
fig2 = figure(fig2);
SigO = Sig;
subplot(2,4,1); imagesc(Xc,Yc,SigXXa); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$\sigma_{xx}$','interpreter','latex','fontsize',16); colorbar; caxis([-1e6 1e6])
subplot(2,4,2); imagesc(Xc,Yc,SigYXa); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$\sigma_{yx}$','interpreter','latex','fontsize',16); colorbar; caxis([-1e6 1e6])
subplot(2,4,5); imagesc(Xc,Yc,SigXYa); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$\sigma_{xy}$','interpreter','latex','fontsize',16); colorbar; caxis([-1e6 1e6])
subplot(2,4,6); imagesc(Xc,Yc,SigYYa); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$\sigma_{yy}$','interpreter','latex','fontsize',16); colorbar; caxis([-1e6 1e6])
subplot(2,4,3); imagesc(Xc,Yc,Eux); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$E_{11}$','interpreter','latex','fontsize',16); colorbar; caxis([-5e-6 5e-6])
subplot(2,4,4); imagesc(Xc,Yc,Evx); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$E_{21}$','interpreter','latex','fontsize',16); colorbar; caxis([-5e-6 5e-6])
subplot(2,4,7); imagesc(Xc,Yc,Euy); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$E_{12}$','interpreter','latex','fontsize',16); colorbar; caxis([-5e-6 5e-6])
subplot(2,4,8); imagesc(Xc,Yc,Evy); hold on; quiver(Xc,Yc,U*1e6,V*1e6,'k','autoscale','on'); set(gca,'YDir','normal','DataAspectRatio',[1 1 1]); title('$E_{22}$','interpreter','latex','fontsize',16); colorbar; caxis([-5e-6 5e-6])
saveas(fig2,['./figs/' num2str(im_num,'Stress%03.f') '.jpg'],'jpg');
end
end
%Periodic creation of new elements in open ocean
if PACKING && h0 > 0
if mod(i_step,nDTpack)==0
height.mean = h0;
height.delta = 0;
[Floe,Vd] = create_new_ice(Floe,c2_boundary,dhdt,Vd,target_concentration, height, min_floe_size, PERIODIC,3,3,Nb);
end
end
%output and reset data values
if mod(i_step,nDTOut)==0
save(['./Floes/Floe' num2str(im_num,'%07.f') '.mat'],'Floe','eulerian_data','SigXXa','SigXYa', 'SigYYa','U','dU','mass');
SigXX = zeros(Ny, Nx); SigYX = zeros(Ny, Nx);
SigXY = zeros(Ny, Nx); SigYY = zeros(Ny, Nx);
Eux = zeros(Ny, Nx); Evx = zeros(Ny, Nx);
Euy = zeros(Ny, Nx); Evy = zeros(Ny, Nx);
U = zeros(Ny, Nx); V = zeros(Ny, Nx);
dU = zeros(Ny, Nx); dV = zeros(Ny, Nx);
Sig = zeros(Ny, Nx); mass = zeros(Ny, Nx);
M = cat(1,Floe.mass);
Mtot(im_num) = sum(M)+sum(Vdnew(:));
im_num=im_num+1; %image number for saving data and coarse vars;
end
%Calculate forces and torques and intergrate forward
[Floe,dissolvedNEW] = floe_interactions_all(Floe, floebound, ocean, winds, c2_boundary, dt, HFo,min_floe_size, Nx,Ny,Nb, dissolvedNEW,doInt,COLLISION, PERIODIC, RIDGING, RAFTING);
%Average coarse eulerian data
if AVERAGE
[eulerian_data] = calc_eulerian_data(Floe,Nx,Ny,Nb,c2_boundary,PERIODIC);
SigXX = SigXX+squeeze(eulerian_data.stressxx); SigYX = SigYX+squeeze(eulerian_data.stressyx);
SigXY = SigXY+squeeze(eulerian_data.stressxy); SigYY = SigYY+squeeze(eulerian_data.stressyy);
Eux = Eux+squeeze(eulerian_data.strainux); Evx = Evx+squeeze(eulerian_data.strainvx);
Euy = Euy+squeeze(eulerian_data.strainuy); Evy = Evy+squeeze(eulerian_data.strainvy);
U = U+squeeze(eulerian_data.u);V = V+squeeze(eulerian_data.v);
dU = dU+squeeze(eulerian_data.du);dV = dV+squeeze(eulerian_data.dv);
Sig = Sig+squeeze(eulerian_data.stress);
mass = mass+squeeze(eulerian_data.Mtot);
end
% Weld floes
if WELDING && mod(i_step,25)==0 && dhdt > 0
Fweld = 150;%Welding probability coefficient
A=cat(1,Floe.area);
if max(A) > Amax
Amax = max(A);
end
if mod(i_step,5000)==0
Floe = weld(Floe,Nb,Fweld,c2_boundary,Amax/2,1,1);
elseif mod(i_step,500)==0
Floe = weld(Floe,Nb,Fweld,c2_boundary,Amax/3,2,2);
else
Floe = weld(Floe,Nb,Fweld,c2_boundary,Amax/3,3,3);
end
end
% Fracture floes based upon stress
if FRACTURES && mod(i_step,75)==0
compactness = 1;
[Floe,Princ] = fracture(Floe,Nb,min_floe_size,compactness);
end
% Remove sharp corners of interacting floes
if CORNERS && mod(i_step,10)==0
keep = rand(length(Floe),1)>0.7;
overlapArea=cat(1,Floe.OverlapArea)./cat(1,Floe.area);
keep(overlapArea>0.15) = 0;
keep(1:Nb) = ones(Nb,1); %Do not fracture boundaries
if KEEP_MIN %Do not allow floes below threshold to fracture
keep(cat(1,Floe.area)<min_floe_size)=1;
end
fracturedFloes=corners(Floe(~keep),Nb,Floe,c2_boundary);
if ~isempty(fracturedFloes)
Floe=[Floe(keep) fracturedFloes];
end
end
%Advect the dissolved mass
Area=cat(1,Floe.area);
if ~KEEP_MIN
dissolvedNEW = calc_dissolved_mass(Floe(Area<min_floe_size),Nx,Ny,c2_boundary_poly)+dissolvedNEW;
end
% Vdnew = Advect_Dissolved_Ice(Vd,coarseMean,im_num,dissolvedNEW,c2_boundary,dt);
Vdnew = Vd(:,:,1)+dissolvedNEW;
dissolvedNEW=zeros(Ny,Nx);
Vd(:,:,2) = Vd(:,:,1);
Vd(:,:,1) = Vdnew;
% Remove small floes from simulation
Area=cat(1,Floe.area);
if ~KEEP_MIN
if sum(Area<min_floe_size)>0, display(['num of small floes killed:' num2str(sum(Area<min_floe_size))]); end
Floe=Floe(Area> min_floe_size);
end
live = cat(1,Floe.alive);
Floe(live == 0) = [];
Time=Time+dt; i_step=i_step+1; %update time index
end
tEnd = toc(tStart)