-
Notifications
You must be signed in to change notification settings - Fork 3
/
ascii2bin.c
225 lines (221 loc) · 6.29 KB
/
ascii2bin.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
/* $Header$
* $Log$
* Revision 1.4 2013/04/02 22:45:29 trq
* Initialize gas_particles, dark_particles, and star_particles.
*
* Revision 1.3 2006/07/19 00:10:06 trq
* Fixed up conversion programs to match changes to tipsydefs.h
*
* Revision 1.2 1996/07/26 20:09:35 trq
* Switched to accept output from genesis with the new star forming
* algorithm.
*
* Revision 1.1.1.1 1995/01/20 20:02:46 trq
* Initial revision
*
* Revision 1.1 93/09/14 09:51:22 trq
* Initial revision
*
* Revision 2.2 91/06/02 12:21:32 trq
* Added star stuff from Neal
*
* Revision 2.1 1991/04/18 18:11:35 trq
* Added checks for malloc(0).
*
*/
#include <stdio.h>
#include <malloc.h>
#include "tipsydefs.h"
int
main()
{
int ndim ;
int nbodies ;
int ngas ;
int ndark ;
int nstar ;
int count ;
struct gas_particle *gp, *lastgp, *gas_particles = NULL;
struct dark_particle *dp, *lastdp, *dark_particles = NULL;
struct star_particle *sp, *lastsp, *star_particles = NULL;
forever {
count=fscanf(stdin, "%d%*[, \t\n]%d%*[, \t\n]%d"
,&header.nbodies, &header.nsph, &header.nstar) ;
if ( (count == EOF) || (count==0) ){
break ;
}
fscanf(stdin,"%d",&header.ndim) ;
fscanf(stdin,"%lf",&header.time) ;
ndim=header.ndim;
nbodies=header.nbodies;
ngas=header.nsph;
nstar = header.nstar ;
ndark = header.ndark = nbodies - nstar - ngas ;
if(gas_particles != NULL) free(gas_particles);
if(ngas != 0) {
gas_particles =
(struct gas_particle *) malloc(ngas*sizeof(*gas_particles));
if(gas_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for gas particles, master>\n") ;
return -1;
}
}
else
gas_particles = NULL;
if(dark_particles != NULL) free(dark_particles);
if(ndark != 0) {
dark_particles =
(struct dark_particle *) malloc(ndark*sizeof(*dark_particles));
if(dark_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for dark particles, master>\n") ;
return -1;
}
}
else
dark_particles = NULL;
if(star_particles != NULL) free(star_particles);
if(nstar != 0) {
star_particles =
(struct star_particle *)malloc(nstar*sizeof(*star_particles));
if(star_particles == NULL) {
fprintf(stderr,
"<sorry, no memory for star particles, master>\n") ;
return -1;
}
}
else
star_particles = NULL;
lastgp = gas_particles + ngas ;
lastdp = dark_particles + ndark ;
lastsp = star_particles + nstar ;
for(gp=gas_particles; gp < lastgp ; gp++){
fscanf(stdin,"%f%*[, \t\n]",&gp->mass);
}
for(dp=dark_particles; dp < lastdp; dp++){
fscanf(stdin,"%f%*[, \t\n]",&dp->mass);
}
for(sp=star_particles; sp < lastsp; sp++){
fscanf(stdin,"%f%*[, \t\n]",&sp->mass);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->pos[0]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
fscanf(stdin,"%f%*[, \t\n]",&dp->pos[0]);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->pos[0]);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->pos[1]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
fscanf(stdin,"%f%*[, \t\n]",&dp->pos[1]);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->pos[1]);
}
if (ndim == 3){
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->pos[2]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
fscanf(stdin,"%f%*[, \t\n]",&dp->pos[2]);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->pos[2]);
}
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->vel[0]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
fscanf(stdin,"%f%*[, \t\n]",&dp->vel[0]);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->vel[0]);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->vel[1]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
fscanf(stdin,"%f%*[, \t\n]",&dp->vel[1]);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->vel[1]);
}
if (ndim == 3){
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->vel[2]);
}
for(dp=dark_particles; dp < lastdp ; dp++) {
count = fscanf(stdin,"%f%*[, \t\n]",&dp->vel[2]);
if ( (count == EOF) || (count==0) )
{
fprintf(stderr, "error: short read on vz\n");
return -1;
}
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->vel[2]);
}
}
for(dp=dark_particles; dp < lastdp ; dp++) {
count = fscanf(stdin,"%f%*[, \t\n]",&dp->eps);
if ( (count == EOF) || (count==0) )
{
fprintf(stderr, "error: short read on epsilons\n");
return -1;
}
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->eps);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->rho);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->temp);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->hsmooth);
}
for(gp=gas_particles; gp < lastgp ; gp++) {
fscanf(stdin,"%f%*[, \t\n]",&gp->metals);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->metals);
}
for(sp=star_particles; sp < lastsp ; sp++) {
fscanf(stdin,"%f%*[, \t\n]",&sp->tform);
}
for(gp=gas_particles; gp < lastgp ; gp++){
count = fscanf(stdin,"%f%*[, \t\n]",&gp->phi);
if ( (count == EOF) || (count==0) )
return -1;
}
for(dp=dark_particles; dp < lastdp; dp++){
count = fscanf(stdin,"%f%*[, \t\n]",&dp->phi);
if ( (count == EOF) || (count==0) )
{
fprintf(stderr, "error: short read on potentials\n");
return -1;
}
}
for(sp=star_particles; sp < lastsp; sp++){
count = fscanf(stdin,"%f%*[, \t\n]",&sp->phi);
if ( (count == EOF) || (count==0) )
return -1;
}
fwrite((char *)&header,sizeof(header),1,stdout) ;
fwrite((char *)gas_particles,sizeof(struct gas_particle),ngas,stdout) ;
fwrite((char *)dark_particles,sizeof(struct dark_particle),
ndark,stdout) ;
fwrite((char *)star_particles,sizeof(struct star_particle),
nstar,stdout) ;
fprintf(stderr, "read time %f\n",header.time) ;
}
return 0;
}