tfsftmz.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /* tfsftmz.c: Source code to implement a TFSF boundary for a TMz grid.
  17. * The incident field is assumed to propagate along the x-direction and
  18. * is calculated using an auxiliary 1D simulation. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "tfsftmz.h"
  22. #include "updatetmz.h"
  23. #include "ezinc.h"
  24. #include "grid1dez.h"
  25. #include "gridtmz.h"
  26. #include "incident.h"
  27. void tfsfInit(Tfsf *tf, Grid *g) {
  28. // initialize 1d grid
  29. gridInit1d(tf->g1, g->time, g->maxTime, g->cdtds);
  30. tf->firstX = 10;
  31. tf->firstY = 10;
  32. tf->lastX = g->sizeX-10;
  33. tf->lastY = g->sizeY-10;
  34. ezIncInit(g); // initialize source function
  35. return;
  36. }
  37. void tfsfUpdate(Tfsf *tf, Grid *g) {
  38. int mm, nn;
  39. // check if tfsfInit() has been called
  40. if( tf->firstX <= 0 || tf->firstY <=0 || tf->lastX >= g->sizeX ||
  41. tf->lastY >= g->sizeY || tf->firstX >= tf->lastX || tf->firstY >=
  42. tf->lastY ) {
  43. fprintf(stderr,
  44. "tfsfUpdate: tfsfInit must be called before tfsfUpdate.\n"
  45. " Boundary location must be set to reasonable values.\n");
  46. exit(-1);
  47. }
  48. // correct Hy along left edge
  49. mm = tf->firstX - 1;
  50. for(nn = tf->firstY; nn <= tf->lastY; nn++)
  51. g->hy[mm][nn] -= g->hyUpdateEcoeff[mm][nn] * tf->g1->ez[mm+1];
  52. // correct Hy along right edge
  53. mm = tf->lastX;
  54. for(nn = tf->firstY; nn <= tf->lastY; nn++)
  55. g->hy[mm][nn] += g->hyUpdateEcoeff[mm][nn] * tf->g1->ez[mm];
  56. // correct Hx along the bottom
  57. nn = tf->firstY - 1;
  58. for(mm = tf->firstX; mm <= tf->lastX; mm++)
  59. g->hx[mm][nn] += g->hxUpdateEcoeff[mm][nn] * tf->g1->ez[mm];
  60. // correct Hx along the top
  61. nn = tf->lastY;
  62. for(mm = tf->firstX; mm <= tf->lastX; mm++)
  63. g->hx[mm][nn] -= g->hxUpdateEcoeff[mm][nn] * tf->g1->ez[mm];
  64. updateH1d(tf->g1); // update 1D magnetic field
  65. updateE1d(tf->g1); // update 1D electric field
  66. // incident wave
  67. tf->g1->ez[0] = incidentWaveOnLeftSide(tf->g1->time, tf->g1->cdtds);
  68. tf->g1->time++;
  69. // correct Ez adjacent to TFSF boudary
  70. // correct Ez field along left edge
  71. mm = tf->firstX;
  72. for(nn = tf->firstY; nn <= tf->lastY; nn++)
  73. g->ez[mm][nn] -= g->ezUpdateHcoeff[mm][nn] * tf->g1->hy[mm-1];
  74. // correct Ez field along right edge
  75. mm = tf->lastX;
  76. for(nn = tf->firstY; nn <= tf->lastY; nn++)
  77. g->ez[mm][nn] += g->ezUpdateHcoeff[mm][nn] * tf->g1->hy[mm];
  78. // no need to correct Ez along top and bottom
  79. // since incident Hx is zero (so need to be done
  80. // if the incident field has nonzero Hx)!!
  81. return;
  82. }
  83. Tfsf *tfsfCreate(int sizeX) {
  84. Tfsf *tf;
  85. tf = (Tfsf *)calloc(1, sizeof(Tfsf));
  86. if(!tf) {
  87. perror("tfsfCreate()");
  88. fprintf(stderr, "Failed to allocate memory for Tfsf.\n");
  89. exit(-1);
  90. }
  91. // Allocate memory for 1D Grid
  92. tf->g1 = gridCreate1d(sizeX);
  93. return tf;
  94. }
  95. void tfsfDestroy(Tfsf *tf) {
  96. gridDestroy1d(tf->g1);
  97. free(tf);
  98. return;
  99. }