فهرست منبع

Batshit crazy double slit simulation (barely works)

Pjotir 1 هفته پیش
والد
کامیت
495ea88130
2فایلهای تغییر یافته به همراه114 افزوده شده و 4 حذف شده
  1. 8 4
      classwork/absorbing_bc.m
  2. 106 0
      classwork/double_slit.m

+ 8 - 4
classwork/absorbing_bc.m

@@ -1,6 +1,10 @@
 function u_next = absorbing_bc(u_next, u, c, dt, dx, dy)
-    u_next(:, 1) = u(:, 2) + ((c*dt-dx)/(c*dt+dx))*(u_next(:, 2)-u(:,1));
-    u_next(:, end) = u(:, end-1) + ((c*dt-dx)/(c*dt+dx))*(u_next(:, end-1)-u(:,end));
-    u_next(1, :) = u(2, :) + ((c*dt-dy)/(c*dt+dy))*(u_next(2, :)-u(1,:));
-    u_next(end, :) = u(end-1, :) + ((c*dt-dy)/(c*dt+dy))*(u_next(end-1, :)-u(end,:));
+    % 1st-order Mur absorbing boundary conditions
+    cx = (c*dt - dx) / (c*dt + dx);
+    cy = (c*dt - dy) / (c*dt + dy);
+
+    u_next(:, 1)   = u(:, 2)     + cx * (u_next(:, 2)     - u(:, 1));
+    u_next(:, end) = u(:, end-1) + cx * (u_next(:, end-1) - u(:, end));
+    u_next(1, :)   = u(2, :)     + cy * (u_next(2, :)     - u(1, :));
+    u_next(end, :) = u(end-1, :) + cy * (u_next(end-1, :) - u(end, :));
 end

+ 106 - 0
classwork/double_slit.m

@@ -0,0 +1,106 @@
+clear all; close all; clc;
+
+c = 2.998e8; % m/s
+Lx = 6e-6;   % m (6 um)
+Ly = 5e-6;
+Nx = 360;
+Ny = 360;
+x = linspace(0, Lx, Nx);
+y = linspace(-Ly/2, Ly/2, Ny);
+dx = x(2) - x(1);
+dy = y(2) - y(1);
+dt = 0.5*dx/c;
+[X, Y] = meshgrid(x, y);
+tmax = (Lx/c)*3;
+Nt = floor(tmax/dt);
+
+% light source
+lambda = 500e-9;
+k = 2*pi/lambda;
+omega = c*k;
+
+source_width = 1*Ly;
+source_mask = zeros(Ny, 1);
+source_index = find(abs(y)<=source_width/2);
+source_mask(source_index) = 1;
+
+% initialize fields
+u = zeros(Ny, Nx);
+u_prev = zeros(Nx, Ny);
+
+% setting up slit
+barrier_index_x = floor(Nx/3);
+slit_width = lambda;
+
+% double slit
+slit_seperation = 2*lambda;
+barrier_index_top = find(abs(y-slit_seperation/2) > slit_width/2);
+barrier_index_bottom = find(abs(y+slit_seperation/2) > slit_width/2);
+barrier_index_y = intersect(barrier_index_top, barrier_index_bottom);
+
+% Accumulation vector
+I = zeros(Ny, 1);
+
+%plot initial
+fig1 = figure;
+hold on;
+shading interp;
+colorbar;
+zlim([-4, 4]);
+clim([-4, 4]);
+xlabel('x');
+ylabel('y');
+zlabel('z');
+view(37.5, 30);
+hsurf = surf(X, Y, u, 'EdgeColor', 'none');
+
+% Also the accumulation plot
+fig2 = figure;
+hold on;
+aplot = plot(y, I');
+xlim([-Lx/2 Lx/2]);
+ylim([0, 50]);
+grid on;
+
+% single slit plot
+% wallZ = 0;
+% plot3([x(barrier_index_x), x(barrier_index_x)], [y(1), y(floor(length(y)/2))-slit_width/2], [wallZ, wallZ], 'r-', 'LineWidth', 3);
+% plot3([x(barrier_index_x), x(barrier_index_x)], [y(floor(length(y)/2))+slit_width/2, y(end)], [wallZ, wallZ], 'r-', 'LineWidth', 3);
+
+
+for n = 1:Nt
+    u_prev_prev = u_prev;
+    u_prev = u;
+
+    % 2D wave equation udpate
+    u = 2*u_prev - u_prev_prev + 4*(c*dt)^2*del2(u_prev, dx, dy);
+
+    % Apply boundary conditions
+    u = absorbing_bc(u, u_prev, c, dt, dx, dy);
+
+    % Soft source on left edge (prevents reflected waves from trapping/exploding)
+    if n < Nt
+        source = 0.5 * sin(omega * n * dt);
+        u(:, 1) = u(:, 1) + source .* source_mask;
+    end
+
+    % Zeroing out waveform at barrier
+    u(barrier_index_y, barrier_index_x) = 0;
+
+    % Accumulate waveform at the edge
+    I = I + u(:, end).^2;
+
+    if mod(n, 2) == 0
+        current_time_str = num2str(n*dt*1e15, '%.2f');
+        title(['Time: ', current_time_str, ' fs']);
+        set(hsurf, 'ZData', u, 'CData', u);
+        set(aplot, 'ydata', I');
+
+        % Scale fig2 y axis if intensity exceeds current limit
+        if max(I) > ylim(gca)*[0;1]
+            ylim(gca, [0, max(I)*1.2]);
+        endif
+
+        drawnow;
+    endif
+end