main.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This code simulates the steady-state electromagnetic waves in a cross-shaped
  2. // 2D waveguide made of a perfect conductor.
  3. #include "sparselizardbase.h"
  4. using namespace mathop;
  5. void sparselizard(void)
  6. {
  7. // The domain regions as defined in 'waveguide3D.geo':
  8. int left = 1, skin = 2, wholedomain = 3;
  9. mesh mymesh("waveguide2D.msh");
  10. // Edge shape functions 'hcurl' for the electric field E.
  11. // Fields x and y are the x and y coordinate fields.
  12. field E("hcurl"), x("x"), y("y");
  13. // Use interpolation order 2 on the whole domain:
  14. E.setorder(wholedomain, 2);
  15. // The cutoff frequency for a 0.2 m width is freq = 0.75 GHz in theory.
  16. // With this code and a fine enough mesh you will get the same value.
  17. double freq = 0.9e9, c = 3e8, pi = 3.14159, k = 2*pi*freq/c;
  18. // The waveguide is a perfect conductor. We thus force all
  19. // tangential components of E to 0 on the waveguide skin.
  20. E.setconstraint(skin);
  21. // We force an electric field in the y direction on region 'left'
  22. // that is 0 on the exterior of 'left' and one sine period inside.
  23. E.setconstraint(left, sin(y/0.1*pi)* array3x1(0,1,0));
  24. formulation maxwell;
  25. // This is the weak formulation for electromagnetic waves:
  26. maxwell += integral(wholedomain, -curl(dof(E))*curl(tf(E)) + k*k*dof(E)*tf(E));
  27. maxwell.generate();
  28. vec solE = solve(maxwell.A(), maxwell.b());
  29. E.setdata(wholedomain, solE);
  30. // Save the electric field E and magnetic field H with an order 2 interpolation:
  31. curl(E).write(wholedomain, "H.pos", 2);
  32. E.write(wholedomain, "E.pos", 2);
  33. // Code validation line. Can be removed.
  34. std::cout << (solE.norm() < 0.6826 && solE.norm() > 0.6825);
  35. }
  36. int main(void)
  37. {
  38. SlepcInitialize(0,{},0,0);
  39. sparselizard();
  40. SlepcFinalize();
  41. return 0;
  42. }