grid1dez.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /* grid1dez.c: Initialization function for the 1D auxiliary grid used
  17. * by the TFSF function to calculate the incident field. */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h> //for calloc
  21. #include "grid1dez.h"
  22. #define NLOSS 20 // number of lossy cells at the end of 1D grid
  23. #define MAX_LOSS 0.35 // maximum loss factor in lossy layer
  24. void gridInit1d(Grid1D *g, int time, int maxTime, double cdtds) {
  25. double imp0 = 377.0, depthInLayer, lossFactor;
  26. g->time = time;
  27. g->maxTime = maxTime;
  28. g->cdtds = cdtds;
  29. int mm;
  30. // set the electric- and magnetic-field update coefficients
  31. for(mm = 0; mm < g->sizeX - 1; mm++) {
  32. if( mm < g->sizeX - 1 - NLOSS) {
  33. g->ezUpdateEcoeff[mm] = 1.0;
  34. g->ezUpdateHcoeff[mm] = g->cdtds * imp0;
  35. g->hyUpdateHcoeff[mm] = 1.0;
  36. g->hyUpdateEcoeff[mm] = g->cdtds / imp0;
  37. } else {
  38. depthInLayer = mm - (g->sizeX - 1 - NLOSS) + 0.5;
  39. lossFactor = MAX_LOSS * pow(depthInLayer / NLOSS, 2);
  40. g->ezUpdateEcoeff[mm] = (1.0 - lossFactor) / (1.0 + lossFactor);
  41. g->ezUpdateHcoeff[mm] = g->cdtds * imp0 / (1.0 + lossFactor);
  42. depthInLayer += 0.5;
  43. lossFactor = MAX_LOSS * pow(depthInLayer / NLOSS, 2);
  44. g->hyUpdateHcoeff[mm] = (1.0 - lossFactor) / (1.0 + lossFactor);
  45. g->hyUpdateEcoeff[mm] = g->cdtds / imp0 / (1.0 + lossFactor);
  46. }
  47. }
  48. return;
  49. }
  50. Grid1D *gridCreate1d(int sizeX) {
  51. Grid1D *g;
  52. g = (Grid1D *)calloc(1, sizeof(Grid1D));
  53. if(!g) {
  54. perror("gridCreate1d()");
  55. fprintf(stderr, "Failed to allocate memory for Grid1D.\n");
  56. exit(-1);
  57. }
  58. g->sizeX = sizeX + NLOSS; // size of domain
  59. // Allocate memory for the arrays
  60. g->hy = (double *)calloc(g->sizeX-1, sizeof(double));
  61. g->hyUpdateEcoeff = (double *)calloc(g->sizeX-1, sizeof(double));
  62. g->hyUpdateHcoeff = (double *)calloc(g->sizeX-1, sizeof(double));
  63. g->ez = (double *)calloc(g->sizeX, sizeof(double));
  64. g->ezUpdateEcoeff = (double *)calloc(g->sizeX, sizeof(double));
  65. g->ezUpdateHcoeff = (double *)calloc(g->sizeX, sizeof(double));
  66. // Check memory allocated successfully
  67. if(!g->hy || !g->hyUpdateEcoeff || !g->hyUpdateHcoeff ||
  68. !g->ez || !g->ezUpdateEcoeff || !g->ezUpdateHcoeff) {
  69. perror("gridCreate1d()");
  70. fprintf(stderr, "Failed to allocate memory to Grid1D arrays.\n");
  71. exit(-1);
  72. }
  73. return g;
  74. }
  75. void gridDestroy1d(Grid1D *g) {
  76. free(g->hy);
  77. free(g->hyUpdateEcoeff);
  78. free(g->hyUpdateHcoeff);
  79. free(g->ez);
  80. free(g->ezUpdateEcoeff);
  81. free(g->ezUpdateHcoeff);
  82. free(g);
  83. return;
  84. }