tmzdemo2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. 2D FDTD simulator
  3. Copyright (C) 2019 Emilia Blåsten
  4. This program is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Affero General Public License
  6. as published by the Free Software Foundation, either version 3 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public
  13. License along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. */
  16. /* tmzdemo2.c: Program to perform a TMz simulation where the field is
  17. * introduced via a TFSF boundary and the grid is terminated with a
  18. * second order ABC. */
  19. #include <stdio.h>
  20. #include <stdlib.h> //for calloc
  21. #include "ezinc.h"
  22. #include "plotWindow.h"
  23. #include "plot2d.h"
  24. #include "tfsftmz.h"
  25. #include "updatetmz.h"
  26. #include "gridtmz.h"
  27. #include "abctmz.h"
  28. #include "source.h"
  29. int main()
  30. {
  31. int WIDTH = 640;
  32. int HEIGHT = 480;
  33. plotWindow *window1 = createPlotWindow(0,100,WIDTH, HEIGHT, "Total Ez-field");
  34. plotWindow *window2 = createPlotWindow(WIDTH+5,100,WIDTH, HEIGHT, "Incident Ez-field");
  35. plotWindow *window3 = createPlotWindow(2*WIDTH+10,100,WIDTH, HEIGHT, "Scattered Ez-field");
  36. // allocate memory for grids
  37. Grid *gTotal, *gIncid;
  38. gTotal = gridCreate(WIDTH, HEIGHT);
  39. gIncid = gridCreate(WIDTH, HEIGHT);
  40. // Initialize field values and parameters
  41. gridInit(gTotal, 3);
  42. gridInit(gIncid, -1);
  43. // initialize ABC
  44. Abc *abTot, *abInc;
  45. abTot = abcCreate(gTotal->sizeX, gTotal->sizeY);
  46. abInc = abcCreate(gTotal->sizeX, gTotal->sizeY);
  47. abcInit(abTot, gTotal);
  48. abcInit(abInc, gIncid);
  49. // initialize TFSF
  50. Tfsf *tfTot, *tfInc;
  51. tfTot = tfsfCreate(gTotal->sizeX);
  52. tfInc = tfsfCreate(gIncid->sizeX);
  53. tfsfInit(tfTot, gTotal);
  54. tfsfInit(tfInc, gIncid);
  55. // do time stepping
  56. for( gTotal->time = 0; gTotal->time < gTotal->maxTime; gTotal->time++) {
  57. // update magnetic fields
  58. updateH2d(gTotal);
  59. updateH2d(gIncid);
  60. // apply TFSF boundary
  61. tfsfUpdate(tfTot, gTotal);
  62. tfsfUpdate(tfInc, gIncid);
  63. // update electric fields
  64. updateE2d(gTotal);
  65. updateE2d(gIncid);
  66. // add source term
  67. addSrc(gTotal);
  68. // apply ABC
  69. abc(abTot, gTotal);
  70. abc(abInc, gIncid);
  71. // draw the current Ez field
  72. plot2d(gTotal->ez, WIDTH, HEIGHT, window1);
  73. plot2d(gIncid->ez, WIDTH, HEIGHT, window2);
  74. plot2dLinComb(1, gTotal->ez, -1, gIncid->ez, WIDTH, HEIGHT, window3);
  75. // check to see if we should stop
  76. if(wantToStopPlot()) break;
  77. }
  78. // close the windows and free their memory
  79. destroyPlotWindow(window1);
  80. destroyPlotWindow(window2);
  81. destroyPlotWindow(window3);
  82. // free memory and safely shut SDL down
  83. quitAllPlotting();
  84. // free memory used by TFSF boundary
  85. tfsfDestroy(tfTot);
  86. tfsfDestroy(tfInc);
  87. // free memory used by ABC boundary
  88. abcDestroy(abTot);
  89. abcDestroy(abInc);
  90. // free memory allocated for Grid
  91. gridDestroy(gTotal);
  92. gridDestroy(gIncid);
  93. return 0;
  94. }