updatetmz.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /* updatetmz.c: Functions to update the fields. Depending on the type
  17. * of grid, the fields can be treated as either one-.or two-dimensional. */
  18. #include "gridtmz.h"
  19. #include "grid1dez.h"
  20. #include "updatetmz.h"
  21. #include <stdio.h>
  22. // update 1D magnetic field
  23. void updateH1d(Grid1D *g) {
  24. int mm;
  25. for(mm = 0; mm < g->sizeX - 1; mm++) {
  26. g->hy[mm] = g->hyUpdateHcoeff[mm] * g->hy[mm]
  27. + g->hyUpdateEcoeff[mm] * (g->ez[mm+1]
  28. - g->ez[mm]);
  29. }
  30. return;
  31. }
  32. // update magnetic field
  33. void updateH2d(Grid *g) {
  34. int mm, nn;
  35. for(mm = 0; mm < g->sizeX; mm++) {
  36. for(nn = 0; nn < g->sizeY - 1; nn++) {
  37. g->hx[mm][nn] = g->hxUpdateHcoeff[mm][nn] * g->hx[mm][nn]
  38. - g->hxUpdateEcoeff[mm][nn] * (g->ez[mm][nn+1]
  39. - g->ez[mm][nn]);
  40. }
  41. }
  42. for(mm = 0; mm < g->sizeX - 1; mm++) {
  43. for(nn = 0; nn < g->sizeY; nn++) {
  44. g->hy[mm][nn] = g->hyUpdateHcoeff[mm][nn] * g->hy[mm][nn]
  45. + g->hyUpdateEcoeff[mm][nn] * (g->ez[mm+1][nn]
  46. - g->ez[mm][nn]);
  47. }
  48. }
  49. return;
  50. }
  51. // update 1D electric field
  52. void updateE1d(Grid1D *g) {
  53. int mm;
  54. for(mm = 1; mm < g->sizeX - 1; mm++) {
  55. g->ez[mm] = g->ezUpdateEcoeff[mm] * g->ez[mm]
  56. + g->ezUpdateHcoeff[mm] * (g->hy[mm] - g->hy[mm-1]);
  57. }
  58. return;
  59. }
  60. // update electric field
  61. void updateE2d(Grid *g) {
  62. int mm, nn;
  63. for(mm = 1; mm < g->sizeX - 1; mm++) {
  64. for(nn = 1; nn < g->sizeY - 1; nn++) {
  65. g->ez[mm][nn] = g->ezUpdateEcoeff[mm][nn] * g->ez[mm][nn]
  66. + g->ezUpdateHcoeff[mm][nn] * (
  67. (g->hy[mm][nn] - g->hy[mm-1][nn])
  68. - (g->hx[mm][nn] - g->hx[mm][nn-1])
  69. );
  70. }
  71. }
  72. return;
  73. }