snapshot2d.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* snapshot2d.c: Function to record the 2D field to a file. The data
  17. * is stored as binary data. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "gridtmz.h"
  21. #include "snapshot2d.h"
  22. static int temporalStride = -2, frame = 0, startTime,
  23. startNodeX, endNodeX, spatialStrideX,
  24. startNodeY, endNodeY, spatialStrideY;
  25. static char basename[80];
  26. void snapshotInit2d(Grid *g) {
  27. int choice;
  28. printf("Do you want 2D snapshots? (1=yes, 0=no) ");
  29. scanf("%d", &choice);
  30. if(choice == 0) {
  31. temporalStride = -1;
  32. return;
  33. }
  34. printf("Duration of simulation is %d steps.\n", g->maxTime);
  35. printf("Enter start time and temporal stride: ");
  36. scanf(" %d %d", &startTime, &temporalStride);
  37. printf("In x direction grid has %d total nodes"
  38. " (ranging from 0 to %d).\n", g->sizeX, g->sizeX-1);
  39. printf("Enter first node, last node, and spatial stride: ");
  40. scanf(" %d %d %d", &startNodeX, &endNodeX, &spatialStrideX);
  41. printf("In y direction grid has %d total nodes"
  42. " (ranging from 0 to %d).\n", g->sizeY, g->sizeY-1);
  43. printf("Enter first node, last node, and spatial stride: ");
  44. scanf(" %d %d %d", &startNodeY, &endNodeY, &spatialStrideY);
  45. printf("Enter the base name: ");
  46. scanf(" %s", basename);
  47. return;
  48. }
  49. void snapshot2d(Grid *g) {
  50. int mm, nn;
  51. float dim1, dim2, temp;
  52. char filename[100];
  53. FILE *out;
  54. // ensure temporal stride set to a reasonable value
  55. if(temporalStride == -1) return;
  56. if(temporalStride < -1) {
  57. fprintf(stderr,
  58. "snapshot2d: snapshotInit2d must be called before snapshot.\n"
  59. " Temporal stride must be set to positive value.\n");
  60. exit(-1);
  61. }
  62. // get snapshot is temporal conditions met
  63. if(g->time >= startTime && (g->time - startTime) % temporalStride == 0) {
  64. sprintf(filename, "%s.%d", basename, frame++);
  65. out = fopen(filename, "wb");
  66. // write dimensions to output file --
  67. // express dimensions as floats
  68. dim1 = (endNodeX - startNodeX) / spatialStrideX + 1;
  69. dim2 = (endNodeY - startNodeY) / spatialStrideY + 1;
  70. fwrite(&dim1, sizeof(float), 1, out);
  71. fwrite(&dim2, sizeof(float), 1, out);
  72. // write remaining data
  73. for(nn = endNodeY; nn >= startNodeY; nn -= spatialStrideY) {
  74. for(mm = startNodeX; mm <= endNodeX; mm += spatialStrideX) {
  75. temp = (float)g->ez[mm][nn]; //store data as a float
  76. fwrite(&temp, sizeof(float), 1, out); // write the float
  77. }
  78. }
  79. fclose(out); // close the file
  80. }
  81. return;
  82. }