gridtmz.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* gridtmz.c: Grid initialization function for a TMz simulation.
  17. * Here the grid is simply homogeneous free space. */
  18. #include "gridtmz.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "material.h"
  23. void gridInitUpdateCoeff(Grid *g, int choice)
  24. {
  25. if(g->type != tmZGrid) {
  26. perror("Update coefficient initialization error");
  27. fprintf(stderr, "Trying to initialize 2D coefficient on wrong type of grid.\n");
  28. exit(-1);
  29. }
  30. double imp0 = 377.0;
  31. int sizeX = g->sizeX; // x size of domain
  32. int sizeY = g->sizeY; // y size of domain
  33. double cdtds = g->cdtds; // Courant number
  34. materialInit(sizeX,sizeY,cdtds,imp0);
  35. int mm, nn;
  36. // set electric field update coefficients
  37. for(mm = 0; mm < sizeX; mm++) {
  38. for(nn = 0; nn < sizeY; nn++) {
  39. g->ezUpdateEcoeff[mm][nn] = ezCe(mm,nn,choice);
  40. g->ezUpdateHcoeff[mm][nn] = ezCh(mm,nn,choice);
  41. }
  42. }
  43. // set magnetic field update coefficients
  44. for(mm = 0; mm < sizeX; mm++) {
  45. for(nn = 0; nn < sizeY - 1; nn++) {
  46. g->hxUpdateEcoeff[mm][nn] = hxCe(mm,nn,choice);
  47. g->hxUpdateHcoeff[mm][nn] = hxCh(mm,nn,choice);
  48. }
  49. }
  50. for(mm = 0; mm < sizeX - 1; mm++) {
  51. for(nn = 0; nn < sizeY; nn++) {
  52. g->hyUpdateEcoeff[mm][nn] = hyCe(mm,nn,choice);
  53. g->hyUpdateHcoeff[mm][nn] = hyCh(mm,nn,choice);
  54. }
  55. }
  56. return;
  57. }
  58. void gridInit(Grid *g, int choice)
  59. {
  60. g->maxTime = 30000; // duration of simulation
  61. g->cdtds = 1.0 / sqrt(2.0); // Courant number
  62. gridInitUpdateCoeff(g, choice);
  63. return;
  64. }
  65. Grid *gridCreate(int sizeX, int sizeY) {
  66. // Memory for the struct itself
  67. Grid *g;
  68. g = (Grid *)calloc(1, sizeof(Grid));
  69. if(!g) {
  70. perror("gridCreate()");
  71. fprintf(stderr, "Failed to allocate memory for Grid.\n");
  72. exit(-1);
  73. }
  74. // Start allocating memory for the big arrays
  75. // tmZ grid!!! I.e. only the hx, hy and ez arrays allocated!!
  76. g->type = tmZGrid;
  77. g->sizeX = sizeX;
  78. g->sizeY = sizeY;
  79. // g->hx is actually an array storing the pointers to each y-array
  80. // This allows the use of g->hx[x][y] style notation
  81. g->hx = (double **)calloc(sizeX, sizeof(double*));
  82. g->hxCols = (double *)calloc(sizeX*(sizeY-1), sizeof(double));
  83. for(int m=0; m<sizeX; m++)
  84. g->hx[m] = g->hxCols + m*(sizeY-1);
  85. // the RHS above is a pointer pointing to m*(sizeY-1) steps
  86. // after where g->hxCols points to
  87. g->hxUpdateHcoeff = (double **)calloc(sizeX, sizeof(double*));
  88. g->hxUpdateHcoeffCols = (double *)calloc(sizeX*(sizeY-1), sizeof(double));
  89. for(int m=0; m<sizeX; m++)
  90. g->hxUpdateHcoeff[m] = g->hxUpdateHcoeffCols + m*(sizeY-1);
  91. g->hxUpdateEcoeff = (double **)calloc(sizeX, sizeof(double*));
  92. g->hxUpdateEcoeffCols = (double *)calloc(sizeX*(sizeY-1), sizeof(double));
  93. for(int m=0; m<sizeX; m++)
  94. g->hxUpdateEcoeff[m] = g->hxUpdateEcoeffCols + m*(sizeY-1);
  95. g->hy = (double **)calloc(sizeX-1, sizeof(double*));
  96. g->hyCols = (double *)calloc((sizeX-1)*sizeY, sizeof(double));
  97. for(int m=0; m<sizeX-1; m++)
  98. g->hy[m] = g->hyCols + m*sizeY;
  99. g->hyUpdateHcoeff = (double **)calloc(sizeX-1, sizeof(double*));
  100. g->hyUpdateHcoeffCols = (double *)calloc((sizeX-1)*sizeY, sizeof(double));
  101. for(int m=0; m<sizeX-1; m++)
  102. g->hyUpdateHcoeff[m] = g->hyUpdateHcoeffCols + m*sizeY;
  103. g->hyUpdateEcoeff = (double **)calloc(sizeX-1, sizeof(double*));
  104. g->hyUpdateEcoeffCols = (double *)calloc((sizeX-1)*sizeY, sizeof(double));
  105. for(int m=0; m<sizeX-1; m++)
  106. g->hyUpdateEcoeff[m] = g->hyUpdateEcoeffCols + m*sizeY;
  107. g->ez = (double **)calloc(sizeX, sizeof(double*));
  108. g->ezCols = (double *)calloc(sizeX*sizeY, sizeof(double));
  109. for(int m=0; m<sizeX; m++)
  110. g->ez[m] = g->ezCols + m*sizeY;
  111. g->ezUpdateHcoeff = (double **)calloc(sizeX, sizeof(double*));
  112. g->ezUpdateHcoeffCols = (double *)calloc(sizeX*sizeY, sizeof(double));
  113. for(int m=0; m<sizeX; m++)
  114. g->ezUpdateHcoeff[m] = g->ezUpdateHcoeffCols + m*sizeY;
  115. g->ezUpdateEcoeff = (double **)calloc(sizeX, sizeof(double*));
  116. g->ezUpdateEcoeffCols = (double *)calloc(sizeX*sizeY, sizeof(double));
  117. for(int m=0; m<sizeX; m++)
  118. g->ezUpdateEcoeff[m] = g->ezUpdateEcoeffCols + m*sizeY;
  119. if(!g->hx || !g->hxUpdateEcoeff || !g->hxUpdateHcoeff ||
  120. !g->hy || !g->hyUpdateEcoeff || !g->hyUpdateHcoeff ||
  121. !g->ez || !g->ezUpdateEcoeff || !g->ezUpdateHcoeff) {
  122. perror("gridCreate()");
  123. fprintf(stderr, "Failed to allocate memory for the arrays.\n");
  124. exit(-1);
  125. }
  126. return g;
  127. }
  128. void gridDestroy(Grid *g)
  129. {
  130. free(g->hxCols);
  131. free(g->hx);
  132. free(g->hxUpdateHcoeffCols);
  133. free(g->hxUpdateHcoeff);
  134. free(g->hxUpdateEcoeffCols);
  135. free(g->hxUpdateEcoeff);
  136. free(g->hyCols);
  137. free(g->hy);
  138. free(g->hyUpdateHcoeffCols);
  139. free(g->hyUpdateHcoeff);
  140. free(g->hyUpdateEcoeffCols);
  141. free(g->hyUpdateEcoeff);
  142. free(g->ezCols);
  143. free(g->ez);
  144. free(g->ezUpdateHcoeffCols);
  145. free(g->ezUpdateHcoeff);
  146. free(g->ezUpdateEcoeffCols);
  147. free(g->ezUpdateEcoeff);
  148. free(g);
  149. return;
  150. }