huygens.m 1.5 KB

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