-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_utils.cu
173 lines (124 loc) · 3.98 KB
/
io_utils.cu
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "globals.h"
void get_r(int, float*);
void unstack(int, int*);
void init_binary_output(void);
void write_grid_data(const char* lbl, float* dat) {
int i, j, * nn;
nn = new int[Dim];
FILE* otp;
float* r = new float [Dim];
otp = fopen(lbl, "w");
for (i = 0; i < M; i++) {
get_r(i, r);
unstack(i, nn);
for (j = 0; j < Dim; j++)
fprintf(otp, "%f ", r[j]);
fprintf(otp, "%1.8e \n", dat[i]);
if (Dim == 2 && nn[0] == Nx[0] - 1)
fprintf(otp, "\n");
}
fclose(otp);
}
void write_kspace_cudaComplex(const char* lbl, cufftComplex* kdt) {
int i, j, nn[3];
FILE* otp;
float kv[3], k2;
otp = fopen(lbl, "w");
for (i = 1; i < M; i++) {
unstack(i, nn);
k2 = get_k(i, kv, Dim);
for (j = 0; j < Dim; j++)
fprintf(otp, "%f ", kv[j]);
float cpx_abs = sqrtf(kdt[i].x * kdt[i].x + kdt[i].y * kdt[i].y);
fprintf(otp, "%1.5e %1.5e %1.5e %1.5e\n", cpx_abs, sqrtf(k2),
kdt[i].x, kdt[i].y);
if (Dim == 2 && nn[0] == Nx[0] - 1)
fprintf(otp, "\n");
}
fclose(otp);
}
void write_kspace_data(const char* lbl, complex<float> * kdt) {
int i, j, nn[3];
FILE* otp;
float kv[3], k2;
otp = fopen(lbl, "w");
for (i = 1; i < M; i++) {
unstack(i, nn);
k2 = get_k(i, kv, Dim);
for (j = 0; j < Dim; j++)
fprintf(otp, "%f ", kv[j]);
fprintf(otp, "%1.5e %1.5e %1.5e %1.5e\n", abs(kdt[i]), sqrt(k2),
real(kdt[i]), imag(kdt[i]));
if (Dim == 2 && nn[0] == Nx[0] - 1)
fprintf(otp, "\n");
}
fclose(otp);
}
void init_binary_output() {
FILE* otp;
otp = fopen("grid_densities.bin", "wb");
if (otp == NULL)
die("failed to open grid_densities.bin");
fwrite(&Dim, sizeof(int), 1, otp);
fwrite(Nx, sizeof(int), Dim, otp);
fwrite(L, sizeof(float), Dim, otp);
fwrite(&ntypes, sizeof(int), 1, otp);
fclose(otp);
otp = fopen("positions.bin", "wb");
if (otp == NULL)
die("Failed to open positions.bin");
fwrite(&ns, sizeof(int), 1, otp);
fwrite(&Dim, sizeof(int), 1, otp);
fwrite(L, sizeof(float), 3, otp);
fwrite(tp, sizeof(int), ns, otp);
fwrite(molecID, sizeof(int), ns, otp);
fwrite(&do_charges, sizeof(int), 1, otp);
if ( do_charges == 1 )
fwrite(charges, sizeof(float), ns, otp);
fclose(otp);
}
void write_binary() {
FILE* otp;
otp = fopen("grid_densities.bin", "ab");
if (otp == NULL)
die("Failed to append to grid_densities.bin");
fwrite(all_rho, sizeof(float), M * ntypes, otp);
fclose(otp);
otp = fopen("positions.bin", "ab");
if (otp == NULL)
die("Failed to append to positions.bin");
fwrite(h_ns_float, sizeof(float), ns*Dim, otp);
fclose(otp);
}
void write_struc_fac() {
// Declare Local Variables
FILE* otp;
int i, j, k, nn[3];
float kv[3], k2;
double temp;
char label [20];
for (i = 0; i < ntypes; i++) {
// Open output file
sprintf(label, "sk%d.dat", i);
//sprintf(label, "sk%d_%d.dat", i,step);
otp = fopen(label, "w");
if (otp == NULL)
die("Failed to write to sk.dat"); //Check to see that output file actually opened
//fprintf(otp, "Printing type: %d\n", i);
// SKIP j=o to skip 0 0 0 point
for (j = 1; j < M; j++) {
/// Get kspace coordinates
unstack(j, nn);
k2 = get_k(j, kv, Dim);
/// Print kspace coordinates
for (k = 0; k < Dim; k++) {
fprintf(otp, "%f ", kv[k]);
}
/// calculate the avg (sum) over total calcuations
temp = avg_sk[i][j] / n_avg_calc;
/// Write data points. Calculations based on binToSk script on git
fprintf(otp, "%1.5e %1.5e %1.5e %1.5e\n", abs(temp), sqrt(k2), real(temp), imag(temp));
}
fclose(otp);
}
}