-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeneralMPIPhysics_Demo.m
More file actions
410 lines (319 loc) · 12 KB
/
Copy pathGeneralMPIPhysics_Demo.m
File metadata and controls
410 lines (319 loc) · 12 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
function [] = GeneralMPIPhysics_Demo(SaveVid,varargin)
%% Function Inputs/ help
% SaveVid is a binary option, if you want to save a video (1) or not (0)
% For Varargin:
% - FileName: is the file name that will be saved
% - VidFormat: either "gif" or "mp4"
% - MoveSample: Binary 1/0 if you want the sample to move
% - SoundOn: Binary 1/0 if you want it to make a beep when it finishes
%%
addpath([pwd,'\Called Functions'])
VidFormat='mp4'; %Default. WIll change if user enters something else
filename = [date(),'MPI_Physics_Vid.mp4']; %Default value. Will change if user enters something else when starting function
MoveSample = 0; %Default
NumVarArgsIn = size(varargin,2);
if mod(NumVarArgsIn,2)>0
error('Number of arguments inputted must be even')
elseif NumVarArgsIn==0
filename = [date(),'MPI_Physics_Vid.mp4'];
end
varargin = reshape(varargin,2,NumVarArgsIn/2);
for i = 1:NumVarArgsIn/2
if strcmpi(varargin(1,i),'FileName')
filename = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'VidFormat')
VidFormat = cell2mat(varargin(2,i));
VidFormat(VidFormat=='.')=[]; %Making sure there is no period in the format. So ".GIF" goes to "GIF", Just for consistency
if not(strcmpi(VidFormat,'mp4')|strcmpi(VidFormat,'gif'))
error('Video format must be mp4 or gif')
end
end
if strcmpi(varargin(1,i),'MoveSample') %Selecting MoveSample as 1(Yes) or 0(No) moves the sample along the axis to show how the signal changes with biasing
MoveSample = cell2mat(varargin(2,i));
else
end
if strcmpi(varargin(1,i),'SoundOn')
SoundOn = cell2mat(varargin(2,i));
end
if strcmpi(varargin(1,i),'MorseIn')
MorseIn = cell2mat(varargin(2,i));
end
end
filename_HasExt = sum(filename=='.')>0;
if filename_HasExt==1
ExtensionStart = find(filename=='.');
VidFormat = filename(ExtensionStart+1:end);
else
filename = [filename,'.',VidFormat];
end
SavePath = uigetdir();
filename = [SavePath,'\',filename];
isGIF = strcmpi(VidFormat,'gif');
isMP4 = strcmpi(VidFormat,'mp4');
fDrive = 25e3;%Hz
Fs= 2e6; %Hz If this is too low, you start to get discretization artifacts in the figures. Keep above ~50x fDrive
x = -.025:.001:.025;%FOV meters
XZero_Ind = find(x==0);
if exist('MoveSample','var')
if MoveSample==1
X_EndIndex=length(x);
else
X_EndIndex = XZero_Ind;
end
else %The default if you do not select motion or no is to not move
X_EndIndex = XZero_Ind;
end
Grad = 2; %Gradient in Tesla per meter
BfflFunc = @(x) Grad.*x;
Bffl = BfflFunc(x); %B field of the FFL
t = 0:1/Fs:3e-3-(1/Fs);
t_Up = 0:1/(Fs*4):3e-3-(1/Fs);
dt = 1/Fs;
Count = 0;
GIF_Count = 1;
% for XPos_Ind = XZero_Ind:1:length(x)
for XPos_Ind = XZero_Ind:X_EndIndex
Count = Count +1;
XPos = x(XPos_Ind);
B = Grad*x(end)/1.5*cos(2*pi*fDrive*t);
B_Big = 2*Grad*x(end)*cos(2*pi*fDrive*t_Up);
B_Bias = B+ Grad*XPos;
M = Langevin(B,1);
M_Big = Langevin(B_Big,1);
M_Bias = Langevin(B_Bias,1);
Signal = diff(M);
FT_Sig = fft(Signal(1:end-1));
L_FT = length(FT_Sig);
P1_FT = abs(FT_Sig(1:L_FT/2+1));
f = Fs*(0:L_FT/2)/L_FT;
%%
if Count==1
h= figure('Position',[100 100 1300 850]);
CitationBox = annotation('textbox',[.0,.23,.1,.025],'String',{'Adapted from Gleich and Weizenecker, 2005 Fig. 1'},'FitBoxToText','on');
AnimationFromBox = annotation('textbox',[.0,.23,.2,.025],'String','Animation from OS-MPI.GitHub.io','FitBoxToText','off');
drawnow
AnimationFromBox.Position(1) = 1-AnimationFromBox.Position(3);
AnimationFromBox.Position(2) = CitationBox.Position(2);
AnimationFromBox.Position(4) = CitationBox.Position(4);
P1 = subplot(2,3,1);
P1.Position(2) = P1.Position(2)+.1;
P1.Position(4) = P1.Position(4)-.1;
P2 = subplot(2,3,2);
P2.Position(2) = P2.Position(2)+.1;
P2.Position(4) = P2.Position(4)-.1;
P3 = subplot(2,3,3);
P3.Position(2) = P3.Position(2)+.1;
P3.Position(4) = P3.Position(4)-.1;
P4 = subplot(2,3,4);
P4.Position(2) = P4.Position(2)+.2;
P4.Position(4) = P4.Position(4)-.1;
P6 = subplot(2,3,6);
P6.Position(2) = P6.Position(2)+.2;
P6.Position(4) = P6.Position(4)-.1;
TMP_Ax = axes('Position',[0 0 1 .25]);
rectangle('Position',[0 0 1 .125],'FaceColor',[.85 .85 .85])
TMP_Ax.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
TMP_Ax.YColor = 'None';
TMP_Ax.XColor = 'None';
AxGap14 = axes('Position',[P1.Position(1) P4.Position(2)+P4.Position(4) P4.Position(3) P1.Position(2)-(P4.Position(2)+P4.Position(4))]);
Ax14Over = axes('Position',P1.Position);
AxGap12 = axes('Position',[P1.Position(1)+P1.Position(3) P1.Position(2) P2.Position(1)-(P1.Position(1)+P1.Position(3)) P1.Position(4)]);
Ax12Over = axes('Position',P1.Position);
FFPAx = axes('Position',[P1.Position(1) .05 (P6.Position(1)+P6.Position(3)-P1.Position(1)) .125]);
end
BPick = B_Bias;
MPick = M_Bias;
Signal = diff(MPick);
FT_Sig = fft(Signal(1:end-1));
L_FT = length(FT_Sig);
P1_FT = abs(FT_Sig(1:L_FT/2+1));
if Count==1
P1_FT_Orig = P1_FT;
end
f = Fs*(0:L_FT/2)/L_FT;
if mod(Count,2)==0
StartI = round(Fs/fDrive/2)+1;
EndI = round(Fs/fDrive/2)*2;
else
StartI = 1;
EndI = round(Fs/fDrive/2);
if MoveSample==0
EndI = round(Fs/fDrive);
end
end
for ii = StartI:EndI
if ii==StartI && Count==1
axes(P1)
PlotMBLine = plot(B_Big(1:400),M_Big(1:400));
hold on
Dot = plot(BPick(ii),MPick(ii),'bo');
hold off
xlim([min(B_Big) max(B_Big)])
xlabel('External Magnetic Field')
title('Particle Magnetization Curve')
ylabel('Magnetization')
else
set(PlotMBLine,'XData',B_Big(1:400))
set(PlotMBLine,'YData',M_Big(1:400))
set(Dot,'XData',BPick(ii))
set(Dot,'YData',MPick(ii))
end
if ii==StartI && Count==1
axes(P4)
FieldOverTime = plot(BPick(1+ii:200+ii),t(1+ii:200+ii));
ylim([ t(1+ii) t(200+ii)])
xlim([min(B_Big) max(B_Big)])
axis ij
ylabel('Time (seconds)')
xlabel('External Magnetic Field')
else
set(FieldOverTime,'XData',BPick(1+ii:200+ii))
set(FieldOverTime,'YData',t(1+ii:200+ii))
set(P4,'ylim',([ t(1+ii) t(200+ii)]))
set(P4,'xlim',[min(B_Big) max(B_Big)])
end
if ii==StartI&& Count==1
axes(P2)
MagOverTime = plot(t(1+ii:200+ii),MPick(1+ii:200+ii));
xlim([t(1+ii) t(200+ii)])
ylim(P1.YLim)
xlabel('Time (seconds)')
title('Magnetization over time')
else
set(MagOverTime,'YData',MPick(1+ii:200+ii))
set(MagOverTime,'XData',t(1+ii:200+ii))
set(P2,'xlim',([ t(1+ii) t(200+ii)]))
end
if ii==StartI&& Count==1
axes(P3)
SigOverTime = plot(t(1+ii:200+ii),Signal(1+ii:200+ii));
xlim([t(1+ii) t(200+ii)])
ylim([-0.5 0.5])
xlabel('Time (seconds)')
ylabel('dM/dt')
title('Signal over time')
else
set(SigOverTime,'YData',Signal(1+ii:200+ii))
set(SigOverTime,'XData',t(1+ii:200+ii))
set(P3,'xlim',([ t(1+ii) t(200+ii)]))
end
if ii==StartI&& Count==1
axes(P6)
FTPlot = plot(f/fDrive,P1_FT);
ylim([0 max(P1_FT_Orig)])
xlabel('Frequencies (f/f_{Drive})')
ylabel('Magnitude')
else
set(FTPlot,'YData',P1_FT)
end
axes(FFPAx)
imagesc(repmat(abs(Bffl+B(ii)),3,1))
caxis([0 max(abs(B))*1.25])
if ii==1 && Count==1
CBR = colorbar;
CBR.Label.String = {'External Field'; 'Magnitude'};
CBR.Label.FontWeight = 'Bold';
CBR.Location = 'westoutside';
end
hold on
plot(XPos_Ind,2,'ko','LineWidth',4)
% plot(XPos_Ind,2,'bo','LineWidth',1)
set(gca,'XTick',[1:5:length(x)], 'YTick', [])
FFPAx.XTickLabel = {x(1:5:length(x))};
title('B-Field along 1D Line')
xlabel('Position (meters)')
Lgd = legend('Particle location');
Lgd.Location = 'northeastoutside';
if ii==StartI&& Count==1
axes(AxGap14)
Axes14GapPlot = plot([BPick(ii) BPick(ii)],[0 1],'b--');
AxGap14.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
AxGap14.YColor = 'None';
AxGap14.XColor = 'None';
xlim(P1.XLim)
ylim([0 1])
else
set(Axes14GapPlot,'XData',[BPick(ii) BPick(ii)])
end
if ii==StartI && Count==1
axes(Ax14Over)
Axes14OverFigPlot = plot([BPick(ii) BPick(ii)],[P1.YLim(1) MPick(ii) ],'b--');
Ax14Over.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
Ax14Over.YColor = 'None';
Ax14Over.XColor = 'None';
xlim(P1.XLim)
ylim(P1.YLim)
else
set(Axes14OverFigPlot,'XData',[BPick(ii) BPick(ii)])
set(Axes14OverFigPlot,'YData',[P1.YLim(1) MPick(ii) ])
end
if ii==StartI && Count==1
axes(AxGap12)
Axes12GapPlot = plot([0 1],[MPick(ii) MPick(ii)],'b--');
AxGap12.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
AxGap12.YColor = 'None';
AxGap12.XColor = 'None';
ylim(P1.YLim)
xlim([0 1])
else
set(Axes12GapPlot,'YData',[MPick(ii) MPick(ii)])
end
if ii==StartI && Count==1
axes(Ax12Over)
Axes12OverPlot = plot([BPick(ii) P1.XLim(2) ],[MPick(ii) MPick(ii) ],'b--');
Ax12Over.Color = 'None';
set(gca,'XTick',[], 'YTick', [])
Ax12Over.YColor = 'None';
Ax12Over.XColor = 'None';
xlim(P1.XLim)
ylim(P1.YLim)
else
set(Axes12OverPlot,'XData',[BPick(ii) P1.XLim(2) ])
set(Axes12OverPlot,'YData',[MPick(ii) MPick(ii) ])
end
drawnow
% pause(.001)
if SaveVid==1
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
end
if isGIF && SaveVid==1
if GIF_Count == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',1/32);
else
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',1/32);
end
end
disp([num2str(ii),',' num2str(GIF_Count)])
frame_1(GIF_Count) = getframe(gcf);
clear tmp
GIF_Count = GIF_Count+1;
end
% sound(sawtooth(2*pi*.3e3*(0:1/10e3:1)).*sin(2*pi*.3e3*(0:1/10e3:1)),10e3) %Obnoxious beeping to alert me it is done
end
if isMP4 && SaveVid==1
v = VideoWriter(filename,'MPEG-4');
open(v)
writeVideo(v,frame_1)
close(v)
end
if exist('SoundOn','var')
if SoundOn==1
if not(exist('MorseIn','var'))
MorseBeep('-- .-.- ..')
else
if MorseIn==1
MorseBeep(MorseIn)
else
MorseBeep('-- .-.- ..')
end
end
end
end
end