-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnapascii.c
233 lines (206 loc) · 5.45 KB
/
snapascii.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* SNAPASCII.C: convert binary N-body file to ASCII format.
* "I did it my way" -- F. Sinatra.
*/
#include "stdinc.h"
#include "getparam.h"
#include "vectmath.h"
#include "filestruct.h"
#include "bodytags.h"
#ifndef TIPSY
# define PURPOSE ";Convert binary N-body file to ascii"
# define RFORMAT "%21.14E"
#else
# define PURPOSE ";Convert binary N-body file to tipsy"
# define RFORMAT "%14.7E"
#endif
string defv[] = { PURPOSE,
"in=???", ";Input file name",
"out=???", ";Output file name",
"options=" MassTag "," PosTag "," VelTag,
";Others are " PhiTag "," AuxTag "," KeyTag,
"iformat= %d", ";Output format for integers",
"rformat= " RFORMAT, ";Output format for floating-point",
"VERSION=2.3", ";Josh Barnes 11 June 1998",
NULL,
};
local string options, iformat, rformat;
local stream instr, outstr;
local void snapascii(void);
local void out_header(int, real);
local void out_idata(int *, int);
local void out_rdata(real *, int);
local void out_vdata(real *, int);
local void out_phase(real *, int);
local void out_int(int);
local void out_3int(int, int, int);
local void out_real(real);
local void out_vect(vector);
int main(int argc, string argv[])
{
initparam(argv, defv);
instr = stropen(getparam("in"), "r");
get_history(instr);
outstr = stropen(getparam("out"), "w");
options = getparam("options");
iformat = getparam("iformat");
rformat = getparam("rformat");
snapascii();
fflush(outstr);
return (0);
}
local void snapascii(void)
{
int nbody, *ibuf;
real tsnap, *rbuf, *vbuf;
while (get_tag_ok(instr, SnapShotTag)) { /* loop reading data frames */
get_set(instr, SnapShotTag);
get_set(instr, ParametersTag);
if (get_tag_ok(instr, TimeTag))
get_data(instr, TimeTag, RealType, &tsnap, 0);
else
tsnap = 0.0;
get_data(instr, NBodyTag, IntType, &nbody, 0);
get_tes(instr, ParametersTag);
out_header(nbody, tsnap);
get_set(instr, ParticlesTag);
if (scanopt(options, MassTag)) { /* mass data to convert? */
rbuf = (real *) allocate(nbody * sizeof(real));
get_data(instr, MassTag, RealType, rbuf, nbody, 0);
out_rdata(rbuf, nbody);
free(rbuf);
}
if (scanopt(options, PosTag)) { /* position data? */
vbuf = (real *) allocate(nbody * NDIM * sizeof(real));
get_data(instr, PosTag, RealType, vbuf, nbody, NDIM, 0);
out_vdata(vbuf, nbody);
free(vbuf);
}
if (scanopt(options, VelTag)) { /* velocity data? */
vbuf = (real *) allocate(nbody * NDIM * sizeof(real));
get_data(instr, VelTag, RealType, vbuf, nbody, NDIM, 0);
out_vdata(vbuf, nbody);
free(vbuf);
}
if (scanopt(options, PhaseTag)) { /* phase-space data? */
vbuf = (real *) allocate(nbody * 2 * NDIM * sizeof(real));
get_data(instr, PhaseTag, RealType, vbuf, nbody, 2, NDIM, 0);
out_vdata(vbuf, nbody);
free(vbuf);
}
if (scanopt(options, PhiTag)) { /* potential data? */
rbuf = (real *) allocate(nbody * sizeof(real));
get_data(instr, PhiTag, RealType, rbuf, nbody, 0);
out_rdata(rbuf, nbody);
free(rbuf);
}
if (scanopt(options, AuxTag)) { /* auxiliary data? */
rbuf = (real *) allocate(nbody * sizeof(real));
get_data(instr, AuxTag, RealType, rbuf, nbody, 0);
out_rdata(rbuf, nbody);
free(rbuf);
}
if (scanopt(options, KeyTag)) { /* key data? */
ibuf = (int *) allocate(nbody * sizeof(int));
get_data(instr, KeyTag, IntType, ibuf, nbody, 0);
out_idata(ibuf, nbody);
free(ibuf);
}
get_tes(instr, ParticlesTag);
get_tes(instr, SnapShotTag);
}
}
local void out_header(int nbody, real tsnap)
{
eprintf("[%s: writing %d bodies at time %f]\n", getargv0(), nbody, tsnap);
#ifndef TIPSY
out_int(nbody);
#else
out_3int(nbody, 0, 0);
#endif
out_int(NDIM);
out_real(tsnap);
}
local void out_idata(int *ip, int nbody)
{
int i;
for (i = 0; i < nbody; i++) {
out_int(*ip);
ip++;
}
}
local void out_rdata(real *rp, int nbody)
{
int i;
for (i = 0; i < nbody; i++) {
out_real(*rp);
rp++;
}
}
local void out_vdata(real *cptr, int nbody)
{
real *cp;
int i, k;
#ifndef TIPSY
cp = cptr;
for (i = 0; i < nbody; i++) {
out_vect(cp);
cp += NDIM;
}
#else
for (k = 0; k < NDIM; k++) {
cp = cptr + k;
for (i = 0; i < nbody; i++) {
out_real(*cp);
cp += NDIM;
}
}
#endif
}
local void out_phase(real *cptr, int nbody)
{
real *cp;
int k, i;
#ifndef TIPSY
for (k = 0; k <= 1; k++) {
cp = cptr + NDIM * k;
for (i = 0; i < nbody; i++) {
out_vect(cp);
cp += 2 * NDIM;
}
}
#else
for (k = 0; k < 6; k++) {
cp = cptr + k;
for (i = 0; i < nbody; i++) {
out_real(*cp);
cp += 2 * NDIM;
}
}
#endif
}
#define MAXBUF 256
local void out_int(int ival)
{
char fmtbuf[MAXBUF];
sprintf(fmtbuf, "%s\n", iformat);
fprintf(outstr, fmtbuf, ival);
}
local void out_3int(int i1, int i2, int i3)
{
char fmtbuf[MAXBUF];
sprintf(fmtbuf, "%s%s%s\n", iformat, iformat, iformat);
fprintf(outstr, fmtbuf, i1, i2, i3);
}
local void out_real(real rval)
{
char fmtbuf[MAXBUF];
sprintf(fmtbuf, "%s\n", rformat);
fprintf(outstr, fmtbuf, rval);
}
local void out_vect(vector vec)
{
char fmtbuf[MAXBUF];
sprintf(fmtbuf, "%s%s%s\n", rformat, rformat, rformat);
fprintf(outstr, fmtbuf, vec[0], vec[1], vec[2]);
}