snells_law_forreal.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. clear all; close all; clc;
  2. c = 2.998e8; % m/s
  3. Lx = 5e-6; % m (5 um)
  4. Ly = 5e-6;
  5. Ny = 300;
  6. Nx = 300;
  7. x = linspace(0, Lx, Nx);
  8. y = linspace(-Ly/2, Ly/2, Ny);
  9. dx = x(2) - x(1);
  10. dy = y(2) - y(1);
  11. dt = 0.5*dx/c;
  12. [X, Y] = meshgrid(x, y);
  13. tmax = (Lx/c)*2;
  14. Nt = floor(tmax/dt);
  15. % light source
  16. lambda = 500e-9;
  17. k = 2*pi/lambda;
  18. omega = c*k;
  19. sources_num = 100;
  20. source_position_x = floor(linspace(1, Nx, sources_num));
  21. source_position_y = ones(1, sources_num) * 2;
  22. % 30deg adjusted phase
  23. deflection_angle = deg2rad(30);
  24. source_phase = (k*sin(deflection_angle)) * linspace(0, Lx, sources_num);
  25. % speed of light matrix
  26. % We're defining the top half to have a slightly lower speed of light
  27. top_index = 1.5;
  28. bottom_index = 1;
  29. solm = [ones(Ny/2, Nx) / top_index * c; ones(Ny/2, Nx)/bottom_index*c];
  30. %initialize fields
  31. u = zeros(Ny, Nx);
  32. u_prev = zeros(Ny, Nx);
  33. %plot initial
  34. figure;
  35. hold on;
  36. colorbar;
  37. zlim([-1.5, 1.5]);
  38. clim([-1.5, 1.5]);
  39. view(3);
  40. hsurf = surf(X, Y, u, 'EdgeColor', 'none');
  41. figure;
  42. hold on;
  43. grid off;
  44. axis off;
  45. xlim([1, Nx]);
  46. ylim([1, Ny]);
  47. hplot = imagesc(u); colorbar; clim([-1.5, 1.5]);
  48. plot([0 Nx], [Ny/2, Ny/2], 'b-', 'LineWidth', 5);
  49. axis xy;
  50. for n = 1:Nt
  51. for i = 1:length(source_position_x)
  52. source = sin(omega * n * dt+source_phase(i));
  53. u(source_position_y(i), source_position_x(i)) = source;
  54. end
  55. u_prev_prev = u_prev;
  56. u_prev = u;
  57. u = 2*u_prev - u_prev_prev + 4*(solm*dt).^2.*del2(u_prev, dx, dy);
  58. u = absorbing_bc(u, u_prev, c, dt, dx, dy);
  59. if mod(n, 2) == 0
  60. current_time_str = num2str(n*dt*10e15, '%.2f');
  61. title(['Time: ', current_time_str, ' fs']);
  62. set(hsurf, 'ZData', u, 'CData', u);
  63. set(hplot, 'CData', u);
  64. drawnow;
  65. endif
  66. pause(0.005);
  67. end