-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtotipnat.c
135 lines (104 loc) · 2.63 KB
/
totipnat.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
#include <stdio.h>
#include "tipsydefs.h"
#include <rpc/types.h>
#include <rpc/xdr.h>
static XDR xdrs;
int xdr_header()
{
int pad;
if(xdr_double(&xdrs, &header.time) != TRUE)
return 0;
if(xdr_int(&xdrs, &header.nbodies) != TRUE)
return 0;
if(xdr_int(&xdrs, &header.ndim) != TRUE)
return 0;
if(xdr_int(&xdrs, &header.nsph) != TRUE)
return 0;
if(xdr_int(&xdrs, &header.ndark) != TRUE)
return 0;
if(xdr_int(&xdrs, &header.nstar) != TRUE)
return 0;
if(xdr_int(&xdrs, &pad) != TRUE)
return 0;
return 1;
}
void xdr_gas()
{
if(sizeof(Real) == sizeof(float))
{
xdr_vector(&xdrs,(char *)gas_particles,
header.nsph*(sizeof(*gas_particles)/sizeof(Real)),
sizeof(Real), xdr_float);
}
}
void xdr_dark()
{
if(sizeof(Real) == sizeof(float))
{
xdr_vector(&xdrs,(char *)dark_particles,
header.ndark*(sizeof(*dark_particles)/sizeof(Real)),
sizeof(Real), xdr_float);
}
}
void xdr_star()
{
if(sizeof(Real) == sizeof(float))
{
xdr_vector(&xdrs,(char *)star_particles,
header.nstar*(sizeof(*star_particles)/sizeof(Real)),
sizeof(Real), xdr_float);
}
}
main()
{
xdrstdio_create(&xdrs, stdin, XDR_DECODE);
forever {
if(xdr_header() != 1)
break;
if(gas_particles != NULL) free(gas_particles);
if(header.nsph != 0) {
gas_particles = (struct gas_particle *)
malloc(header.nsph*sizeof(*gas_particles));
if(gas_particles == NULL) {
printf("<sorry, no memory for gas particles, master>\n") ;
return ;
}
}
else
gas_particles = NULL;
if(dark_particles != NULL) free(dark_particles);
if(header.ndark != 0) {
dark_particles = (struct dark_particle *)
malloc(header.ndark*sizeof(*dark_particles));
if(dark_particles == NULL) {
printf("<sorry, no memory for dark particles, master>\n") ;
return ;
}
}
else
dark_particles = NULL;
if(star_particles != NULL) free(star_particles);
if(header.nstar != 0) {
star_particles = (struct star_particle *)
malloc(header.nstar*sizeof(*star_particles));
if(star_particles == NULL) {
printf("<sorry, no memory for star particles, master>\n") ;
return ;
}
}
else
star_particles = NULL;
xdr_gas();
xdr_dark();
xdr_star();
fwrite((char *)&header,sizeof(header),1,stdout) ;
fwrite((char *)gas_particles,sizeof(struct gas_particle),
header.nsph, stdout) ;
fwrite((char *)dark_particles,sizeof(struct dark_particle),
header.ndark,stdout) ;
fwrite((char *)star_particles,sizeof(struct star_particle),
header.nstar, stdout) ;
fprintf(stderr, "read time %lf\n",header.time) ;
}
xdr_destroy(&xdrs);
}