123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /*
- 2D FDTD simulator
- Copyright (C) 2019 Emilia Blåsten
- This program is free software: you can redistribute it and/or
- modify it under the terms of the GNU Affero General Public License
- as published by the Free Software Foundation, either version 3 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public
- License along with this program. If not, see
- <http://www.gnu.org/licenses/>.
- */
- /* snapshot2d.c: Function to record the 2D field to a file. The data
- * is stored as binary data. */
- #include <stdio.h>
- #include <stdlib.h>
- #include "gridtmz.h"
- #include "snapshot2d.h"
- static int temporalStride = -2, frame = 0, startTime,
- startNodeX, endNodeX, spatialStrideX,
- startNodeY, endNodeY, spatialStrideY;
- static char basename[80];
- void snapshotInit2d(Grid *g) {
- int choice;
- printf("Do you want 2D snapshots? (1=yes, 0=no) ");
- scanf("%d", &choice);
- if(choice == 0) {
- temporalStride = -1;
- return;
- }
- printf("Duration of simulation is %d steps.\n", g->maxTime);
- printf("Enter start time and temporal stride: ");
- scanf(" %d %d", &startTime, &temporalStride);
- printf("In x direction grid has %d total nodes"
- " (ranging from 0 to %d).\n", g->sizeX, g->sizeX-1);
- printf("Enter first node, last node, and spatial stride: ");
- scanf(" %d %d %d", &startNodeX, &endNodeX, &spatialStrideX);
- printf("In y direction grid has %d total nodes"
- " (ranging from 0 to %d).\n", g->sizeY, g->sizeY-1);
- printf("Enter first node, last node, and spatial stride: ");
- scanf(" %d %d %d", &startNodeY, &endNodeY, &spatialStrideY);
- printf("Enter the base name: ");
- scanf(" %s", basename);
- return;
- }
- void snapshot2d(Grid *g) {
- int mm, nn;
- float dim1, dim2, temp;
- char filename[100];
- FILE *out;
- // ensure temporal stride set to a reasonable value
- if(temporalStride == -1) return;
- if(temporalStride < -1) {
- fprintf(stderr,
- "snapshot2d: snapshotInit2d must be called before snapshot.\n"
- " Temporal stride must be set to positive value.\n");
- exit(-1);
- }
- // get snapshot is temporal conditions met
- if(g->time >= startTime && (g->time - startTime) % temporalStride == 0) {
- sprintf(filename, "%s.%d", basename, frame++);
- out = fopen(filename, "wb");
- // write dimensions to output file --
- // express dimensions as floats
- dim1 = (endNodeX - startNodeX) / spatialStrideX + 1;
- dim2 = (endNodeY - startNodeY) / spatialStrideY + 1;
- fwrite(&dim1, sizeof(float), 1, out);
- fwrite(&dim2, sizeof(float), 1, out);
- // write remaining data
- for(nn = endNodeY; nn >= startNodeY; nn -= spatialStrideY) {
- for(mm = startNodeX; mm <= endNodeX; mm += spatialStrideX) {
- temp = (float)g->ez[mm][nn]; //store data as a float
- fwrite(&temp, sizeof(float), 1, out); // write the float
- }
- }
- fclose(out); // close the file
- }
- return;
- }
|