-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuntitled2.m
More file actions
258 lines (230 loc) · 7.29 KB
/
untitled2.m
File metadata and controls
258 lines (230 loc) · 7.29 KB
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
clear;clc;close all;
expDataPrep
t = tmatrix(:,1);
R = Rmatrix(:,1);
Rdot = Rdotmatrix(:,1);
animate_strainrate_live(R, Rdot, t, ...
'UseND', true, 'NDHalfWidth', 1.5, ...
'Nx', 600, 'Speedup', 5, ...
'LockColorbar', false, ...
'TimeScale', 1, 'TimeUnit', 'ND', 'PaceScale', 1);
function animate_strainrate_live(R, Rdot, t, varargin)
% animate_strainrate_live (LaTeX everywhere)
% eps_eq(r,t) = 2*sqrt(3)*|R(t)^2 * Rdot(t)| / r^3 for r >= R(t)
%
% New (time handling):
% TimeScale, TimeUnit, TimeOffset, PaceScale (display vs pacing decoupled)
% ---------- options ----------
p = inputParser;
p.addParameter('UseND', true);
p.addParameter('NDHalfWidth', 1.5);
p.addParameter('SpanInRmax', 8);
p.addParameter('Nx', 500);
p.addParameter('Speedup', 10);
p.addParameter('SaveAs', '');
p.addParameter('UseLog', true);
p.addParameter('LogFloor', 1e-20);
p.addParameter('LockColorbar', true);
p.addParameter('FixedCLim', []);
p.addParameter('GlobalPercentiles', [5 99.5]);
% explicit time handling
p.addParameter('TimeScale', 1); % DISPLAY: t_display = t*TimeScale + TimeOffset
p.addParameter('TimeUnit', ''); % e.g., 's', '\mu s', 'ND' (LaTeX-ready or plain)
p.addParameter('TimeOffset', 0);
p.addParameter('PaceScale', 1); % PACING: pause uses Δt * PaceScale / Speedup
p.parse(varargin{:});
UseND = p.Results.UseND;
NDL = p.Results.NDHalfWidth;
span = p.Results.SpanInRmax;
Nx = p.Results.Nx;
spd = max(p.Results.Speedup, eps);
base = p.Results.SaveAs;
UseLog = p.Results.UseLog;
LogFloor = p.Results.LogFloor;
LockCB = p.Results.LockColorbar;
FixedCL = p.Results.FixedCLim;
globPr = p.Results.GlobalPercentiles;
TimeScale = p.Results.TimeScale;
TimeUnit = p.Results.TimeUnit;
TimeOffset = p.Results.TimeOffset;
PaceScale = p.Results.PaceScale;
% ---------- data prep ----------
R = R(:).';
Rdot = Rdot(:).';
t = t(:).';
% field definition
eps_eq_fun = @(Rp,Rdp,r) 2*sqrt(3)*abs(Rp.^2 .* Rdp) ./ (r.^3 + eps);
% choose domain (ND or physical)
if UseND
L = NDL;
x = linspace(-L, L, Nx);
y = x;
toUnits = 1;
xLabel = '$x$ (ND)';
yLabel = '$y$ (ND)';
else
Rmax = max(R);
L = span*Rmax; % meters
x = linspace(-L, L, Nx);
y = x;
toUnits = 1e6; % display in micrometers
xLabel = '$x~(\mu\mathrm{m})$';
yLabel = '$y~(\mu\mathrm{m})$';
end
[XX,YY] = meshgrid(x, y);
RR = hypot(XX, YY);
% ---------- one-time global CLim if locking and not provided ----------
if LockCB && isempty(FixedCL)
valsAll = [];
nR = 800;
for j = 1:numel(t)
Rp = R(j);
Rdp = Rdot(j);
rmin = max(Rp*1.001, L/Nx); % just outside bubble
if rmin >= L, continue; end
r = logspace(log10(max(rmin,1e-12)), log10(L), nR);
Eline = eps_eq_fun(Rp, Rdp, r);
v = UseLog * log10(max(Eline, LogFloor)) + (~UseLog) * Eline;
v(~isfinite(v)) = [];
valsAll = [valsAll, v]; %#ok<AGROW>
end
if isempty(valsAll)
FixedCL = UseLog * [-10 -9] + (~UseLog) * [0 1];
else
pr = prctile(valsAll, globPr);
if ~(isfinite(pr(1)) && isfinite(pr(2)) && pr(2) > pr(1))
mu = mean(valsAll, 'omitnan');
sd = std(valsAll, 0, 'omitnan');
if ~isfinite(sd) || sd == 0, sd = 0.25; end
pr = [mu-2*sd, mu+2*sd];
if pr(2) <= pr(1), pr(2) = pr(1) + 1e-3; end
end
FixedCL = pr;
end
end
% ---------- figure & handles ----------
fig = figure('Color','w','Units','pixels','Position',[100 100 760 760]);
ax = axes(fig);
colormap(ax, parula);
% LaTeX everywhere
set(ax, 'TickLabelInterpreter','latex');
img = imagesc(ax, x*toUnits, y*toUnits, nan(size(RR)));
set(ax, 'YDir', 'normal'); axis(ax, 'image');
ax.XAxis.Exponent = 0; ax.YAxis.Exponent = 0;
xtickformat(ax, '%.1f'); ytickformat(ax, '%.1f');
xlabel(ax, xLabel, 'Interpreter','latex');
ylabel(ax, yLabel, 'Interpreter','latex');
xlim(ax, [-L L]*toUnits); ylim(ax, [-L L]*toUnits);
cb = colorbar(ax);
cb.TickLabelInterpreter = 'latex';
cb.Label.Interpreter = 'latex';
if UseLog
cb.Label.String = '$\log_{10}(\dot{\epsilon}_{\mathrm{eq}})$';
else
cb.Label.String = '$\dot{\epsilon}_{\mathrm{eq}}~(\mathrm{s}^{-1})$';
end
if LockCB && ~isempty(FixedCL), caxis(ax, FixedCL); end
hold(ax, 'on');
th = linspace(0, 2*pi, 400);
circ = plot(ax, nan(size(th)), nan(size(th)), 'k-', 'LineWidth', 1.1);
hold(ax, 'off');
% ---------- optional video ----------
vw = []; opened = false; framesWritten = 0;
if ~isempty(base)
prof = chooseProfile();
vw = VideoWriter([base profileExt(prof)], prof);
vw.FrameRate = 30;
end
% ---------- main loop ----------
for j = 1:numel(t)
Rp = R(j);
Rdp = Rdot(j);
% field on grid and mask inside bubble
E = eps_eq_fun(Rp, Rdp, RR);
E(RR < Rp) = NaN;
V = UseLog * log10(max(E, LogFloor)) + (~UseLog) * E;
V(~isfinite(V)) = NaN;
% update image and circle
set(img, 'CData', V, 'XData', x*toUnits, 'YData', y*toUnits);
set(circ, 'XData', (Rp*cos(th))*toUnits, 'YData', (Rp*sin(th))*toUnits);
% color limits
if LockCB && ~isempty(FixedCL)
caxis(ax, FixedCL);
else
finiteVals = V(isfinite(V));
[lo, hi] = safeCLim(finiteVals, UseLog);
caxis(ax, [lo hi]);
end
% bounds and title (LaTeX)
xlim(ax, [-L L]*toUnits);
ylim(ax, [-L L]*toUnits);
t_disp = t(j)*TimeScale + TimeOffset;
if isempty(TimeUnit)
ttlTime = sprintf('$t = %.6g$', t_disp);
else
if contains(TimeUnit, '\') % already LaTeX-coded (e.g., '\mu s')
ttlTime = sprintf('$t = %.6g\\,%s$', t_disp, TimeUnit);
else % plain text -> roman
ttlTime = sprintf('$t = %.6g\\,\\mathrm{%s}$', t_disp, TimeUnit);
end
end
if UseND
ttlDom = sprintf('(ND domain = $\\pm%.1f$)', NDL);
else
ttlDom = sprintf('(domain = $\\pm%.1f~R_{\\max}$)', span);
end
title(ax, sprintf('%s %s', ttlTime, ttlDom), 'Interpreter','latex');
drawnow;
% write frame
if ~isempty(vw)
if ~opened, open(vw); opened = true; end
writeVideo(vw, getframe(fig));
framesWritten = framesWritten + 1;
end
% pacing (real Δt, scaled only if requested)
if j < numel(t)
dt = (t(j+1) - t(j)) * PaceScale;
pause(max(dt/spd, 0));
end
end
if ~isempty(vw) && opened
close(vw);
if framesWritten == 0
warning('No video frames were written.');
end
end
end
% ---------- helpers ----------
function [lo, hi] = safeCLim(vals, UseLog)
if isempty(vals)
if UseLog, lo = -10; hi = -9; else, lo = 0; hi = 1; end
return
end
lo = prctile(vals, 5, 'all');
hi = prctile(vals, 99.5, 'all');
if ~isfinite(lo) || ~isfinite(hi) || hi <= lo
mu = mean(vals, 'omitnan');
sd = std(vals, 0, 'omitnan');
if ~isfinite(sd) || sd == 0, sd = 0.25; end
lo = mu - 2*sd;
hi = mu + 2*sd;
if ~isfinite(lo) || ~isfinite(hi) || hi <= lo
lo = mu - 1;
hi = mu + 1;
if hi <= lo, hi = lo + 1e-3; end
end
end
end
function prof = chooseProfile()
try
names = string({VideoWriter.getProfiles.Name});
if any(names == "MPEG-4"), prof = 'MPEG-4';
else, prof = 'Motion JPEG AVI';
end
catch
prof = 'Motion JPEG AVI';
end
end
function ext = profileExt(p)
if strcmp(p,'MPEG-4'), ext = '.mp4'; else, ext = '.avi'; end
end