| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- clear all; close all; clc;
- c = 2.998e8; % m/s
- Lx = 5e-6; % m (5 um)
- Ly = 5e-6;
- Ny = 300;
- Nx = 300;
- 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)*2;
- Nt = floor(tmax/dt);
- % light source
- lambda = 500e-9;
- k = 2*pi/lambda;
- omega = c*k;
- sources_num = 100;
- source_position_x = floor(linspace(1, Nx, sources_num));
- source_position_y = ones(1, sources_num) * 2;
- % 30deg adjusted phase
- deflection_angle = deg2rad(30);
- source_phase = (k*sin(deflection_angle)) * linspace(0, Lx, sources_num);
- % speed of light matrix
- % We're defining the top half to have a slightly lower speed of light
- top_index = 1.5;
- bottom_index = 1;
- solm = [ones(Ny/2, Nx) / top_index * c; ones(Ny/2, Nx)/bottom_index*c];
- %initialize fields
- u = zeros(Ny, Nx);
- u_prev = zeros(Ny, Nx);
- %plot initial
- figure;
- hold on;
- colorbar;
- zlim([-1.5, 1.5]);
- clim([-1.5, 1.5]);
- view(3);
- hsurf = surf(X, Y, u, 'EdgeColor', 'none');
- figure;
- hold on;
- grid off;
- axis off;
- xlim([1, Nx]);
- ylim([1, Ny]);
- hplot = imagesc(u); colorbar; clim([-1.5, 1.5]);
- plot([0 Nx], [Ny/2, Ny/2], 'b-', 'LineWidth', 5);
- axis xy;
- for n = 1:Nt
- for i = 1:length(source_position_x)
- source = sin(omega * n * dt+source_phase(i));
- u(source_position_y(i), source_position_x(i)) = source;
- end
- u_prev_prev = u_prev;
- u_prev = u;
- u = 2*u_prev - u_prev_prev + 4*(solm*dt).^2.*del2(u_prev, dx, dy);
- u = absorbing_bc(u, u_prev, c, dt, dx, dy);
- if mod(n, 2) == 0
- current_time_str = num2str(n*dt*10e15, '%.2f');
- title(['Time: ', current_time_str, ' fs']);
- set(hsurf, 'ZData', u, 'CData', u);
- set(hplot, 'CData', u);
- drawnow;
- endif
- pause(0.005);
- end
|