-
Notifications
You must be signed in to change notification settings - Fork 4
/
Advect_and_Calculate_2DUnsteady.m
162 lines (116 loc) · 5.51 KB
/
Advect_and_Calculate_2DUnsteady.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
% Input arguments:
% tspan : Advection timesteps (e.g. tspan=linspace(t_initial,t_final,100)
% xx,yy : Initial positions for advection
% U_Interp,V_Interp : Velocity field interpolants with inputs (tspan,xx,yy)
% NCores : Number of Cores for parpool
% Output arguments:
% xt,yt : xx-component, yy-component of trajectory final position
% time_note : tspan time if a trajectory left interpolant domain
% TSE_Bar_EPS,TRA_Bar_EPS,TSE_EPS : Single trajectory metrics from
% section IV in [1] (Theorem 3)
%--------------------------------------------------------------------------
% Author: Nikolas Aksamit [email protected]
%--------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% References:
% [1] Haller, G., Aksamit, N. O., & Bartos, A. P. E. (2021). Quasi-Objective Coherent Structure Diagnostics from Single Trajectories.
% Chaos, 31, 043131-1–17. https://doi.org/10.1063/5.0044151
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Calculate extended phase space (EPS) TRA and TSE diagnostics for unsteady
%%% 2D flows.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xt,yt,time_note,TSE_Bar_EPS,TRA_Bar_EPS,TSE_EPS] = Advect_and_Calculate_2DUnsteady(tspan,xx,yy,U_Interp,V_Interp,NCores)
%%% Calculate v_0 as spatio-temporal mean of the flow in your domain of
%%% interest
v_0=mean(sqrt(U_Interp.Values.^2+V_Interp.Values.^2),'all');
%%%% v_0=0; %%%% Equivalent to steady version of TRA/TSE
if isempty(gcp)
parpool(NCores);
end
Np=numel(xx);
cpu_num = min(NCores,Np);
id = ceil( linspace(0,Np,cpu_num+1) );
% Shared variables
xt = zeros(2,length(xx));
yt = zeros(2,length(yy));
xt(1,:) = xx;
yt(1,:) = yy;
spmd(cpu_num)
tic
Range = id(labindex)+1:id(labindex+1);
x_spmd=xt(1,Range);
y_spmd=yt(1,Range);
time_note=zeros(size(x_spmd));
TSE_EPS_spmd=zeros(size(x_spmd));
TRA_EPS_spmd=zeros(size(x_spmd));
for s=1:numel(tspan)-1
%% This only records the last point
ds = tspan(s+1) - tspan(s);
[UK1,VK1] = r_prime(tspan(s),x_spmd(1,:),y_spmd(1,:),U_Interp,V_Interp);
xx = x_spmd(1,:) + 0.5 * ds * UK1;
yy = y_spmd(1,:) + 0.5 * ds * VK1;
[UK2,VK2] = r_prime(tspan(s)+ds/2,xx,yy,U_Interp,V_Interp);
xx = x_spmd(1,:) + 0.5 * ds * UK2;
yy = y_spmd(1,:) + 0.5 * ds * VK2;
[UK3,VK3] = r_prime(tspan(s)+ds/2,xx,yy,U_Interp,V_Interp);
xx = x_spmd(1,:) + ds * UK3;
yy = y_spmd(1,:) + ds * VK3;
[UK4,VK4] = r_prime(tspan(s)+ds,xx,yy,U_Interp,V_Interp);
%increment in trajectories (displacement of grid)
deltax(2,:) = ds / 6 * (UK1 + 2 * UK2 + 2 * UK3 + UK4);
deltay(2,:) = ds / 6 * (VK1 + 2 * VK2 + 2 * VK3 + VK4);
%update particle positions
x_spmd(2,:) = x_spmd(1,:) + deltax(2,:); %Make sure theta goes around the whole circle
y_spmd(2,:) = y_spmd(1,:) + deltay(2,:);
x_spmd(1,~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))=x_spmd(2,~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)));
y_spmd(1,~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))=y_spmd(2,~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)));
time_note(1,(isnan(x_spmd(2,:)) & time_note(1,:)==0) | (isnan(y_spmd(2,:)) & time_note(1,:)==0))=s;
if s==1
smooth_vx=deltax(2,:)'/ds;
smooth_vy=deltay(2,:)'/ds;
V1=[smooth_vx,smooth_vy];
Speed_sqrd(1,:)=sum(V1.^2,2);
deltax(1,:) = deltax(2,:);
deltay(1,:) = deltay(2,:);
V1_spmd=V1;
end
if s>1
smooth_vx=deltax(2,:)'/ds;
smooth_vy=deltay(2,:)'/ds;
V2=[smooth_vx,smooth_vy];
Speed_sqrd(2,:)=sum(V2.^2,2);
TSE_Inst_EPS=abs(log(sqrt(Speed_sqrd(2,:)+v_0^2)./sqrt(Speed_sqrd(1,:)+v_0^2)));
TSE_EPS_spmd(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))=TSE_EPS_spmd(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))+TSE_Inst_EPS(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)));
TRA_Inst_EPS=acos((sum(V2.*V1,2)+v_0^2)./(sqrt(sum(V2.^2,2)+v_0^2).*sqrt(sum(V1.^2,2)+v_0^2)))';
TRA_EPS_spmd(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))=TRA_EPS_spmd(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)))+TRA_Inst_EPS(~isnan(x_spmd(2,:)) & ~isnan(y_spmd(2,:)));
deltax(1,:) = deltax(2,:);
deltay(1,:) = deltay(2,:);
Speed_sqrd(1,:)=Speed_sqrd(2,:);
V1=V2;
V2_spmd=V2;
end
end
toc
end
V1 = cat(1,V1_spmd{:});
V2 = cat(1,V2_spmd{:});
TSE_EPS=1/(tspan(end)-tspan(1))*log(sqrt((sum(V2.*V2,2)+v_0.^2)./(sum(V1.*V1,2).^2+v_0.^2)));
xt = cat(2,x_spmd{:});
yt = cat(2,y_spmd{:});
time_note = cat(2,time_note{:});
TSE_Bar_EPS=cat(2,TSE_EPS_spmd{:})/(tspan(end)-tspan(1));
TRA_Bar_EPS=cat(2,TRA_EPS_spmd{:})/(tspan(end)-tspan(1));
clear x_spmd y_spmd
% disp('Advection Complete')
end
function [uk,vk] = r_prime(s,xx,yy,U_Interp,V_Interp)
Bly = 1+0*xx;
Bly(yy>max(yy(:))) = 0; %Freeze when outside domain
Bly(yy<min(yy(:))) = 0;
Bly(xx>max(xx(:))) = 0;
Bly(xx<min(xx(:))) = 0;
uk=U_Interp(double(s)*ones(size(xx)),double(xx),double(yy));
uk=uk.*Bly;
vk=V_Interp(double(s)*ones(size(xx)),double(xx),double(yy));
vk=vk.*Bly;
end