-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.c
113 lines (95 loc) · 3.59 KB
/
output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "grid.h"
#include "list.h"
#include "vector.h"
#include "tree.h"
// only way I could see to make output_one_point work with tree_apply_function
static FILE *pfile = NULL;
static inline void testPFile(FILE *pfile, char *fname) {
if (pfile == NULL) {
printf("Error! Could not create/open file: %s\n", fname);
exit(1); // must include stdlib.h
}
}
// print x,y,z coord of the point followed by |E|,|B| at the point
// extra_args: pass a FILE* to the file where the point should be written
bool output_one_point(grid_point *point, double x, double y, double z) {
double E = vec3_norm(point->E);
double B = vec3_norm(point->B);
fprintf(pfile, "%lg,%lg,%lg,%lg,%lg\n", x, y, z, E, B);
return false;
}
// the base_grid has the trees which hold the points and particles to print
// suffix is the suffix used in naming the output files
void output_grid_impl(int itNum, int numFiles, tree ****base_grid, const char suffix[]) {
int suffix_len = strlen(suffix);
// the # of chars needed to represent the biggest iteration # as a string. all iter #s will be padded to this value
int width_iter_num = round_i(log10(numFiles)+1);
// the file name. reused for all files so it needs to be large enough to hold the longest name. +1 at end holds string terminator
char fname[3+width_iter_num+11+suffix_len+1];
// FILE *pfile;
static bool shouldWriteHeader = true;
// create header file only once
if (shouldWriteHeader) {
shouldWriteHeader = false;
// set name to header and open
sprintf(fname, "params.%s", suffix);
pfile = fopen(fname, "w");
// test to ensure that the file was successfully created
testPFile(pfile, fname);
fprintf(pfile, "params.data\n");
fprintf(pfile, "nx=%d,ny=%d,nz=%d\n", nx, ny, nz);
fprintf(pfile, "dx=%lg,dy=%lg,dz=%lg\n", dx, dy, dz);
fprintf(pfile, "numFiles=%d\n", numFiles);
fprintf(pfile, "pid=%d\n", pid);
fclose(pfile);
}
// GRID OUTPUT
// set file name to grid file and open
sprintf(fname, "%d_%d_grid.%s", pid, itNum, suffix);
pfile = fopen(fname, "w");
// test to ensure that the file was actually created and exists:
testPFile(pfile, fname);
int i, j, k;
// double magE, magB;
// print |E|, |B| for each grid point
fprintf(pfile, "x, y, z, |E|, |B|\n");
for(i = imin+1; i < imax-1; ++i) {
for(j = jmin+1; j < jmax-1; ++j) {
for(k = kmin+1; k < kmax-1; ++k) {
if (base_grid[i][j][k] != NULL) {
// tell each cell to print all of the points inside it for which it is responsible
// TODO: make sure we aren't double outputting (via diff procs) or missing cells at the end of the grid
tree_apply_fcn(base_grid[i][j][k], &output_one_point);
}
}
}
}
fclose(pfile);
// PARTICLE OUTPUT
// set file name to particle file and open
sprintf(fname, "%d_%d_particles.%s", pid, itNum, suffix);
pfile = fopen(fname, "w");
// test to ensure that the file was actually created and exists:
testPFile(pfile, fname);
fprintf(pfile, "x, y, z, |p|\n");
// print # of particles as a header
//fprintf(pfile, "%d\n", list_length(particles));
for (i = 0; i < nx; ++i) {
for (j = 0; j < ny; ++j) {
for (k = 0; k < nz; ++k) {
// print x,y,z,|p| for each particle in the cell
List *particles = base_grid[i][j][k]->particles;
list_reset_iter(particles);
while(list_has_next(particles)) {
particle *ptc = (particle*) list_get_next(particles);
fprintf(pfile, "%lg,%lg,%lg,%lg\n", (ptc->pos).x, (ptc->pos).y, (ptc->pos).z, vec3_norm(ptc->p));
}
}
}
}
fclose(pfile);
}