-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_nodeData.c
executable file
·204 lines (176 loc) · 6.47 KB
/
write_nodeData.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
#include <stdlib.h>
#include "hdf5.h"
#include "node.h"
int write_nodeData(char * filename, struct node ** nodeData, int nNodes, int doffset) {
hid_t file_id;
hid_t group_id;
hid_t dataspace_id;
hid_t dataset_id;
hsize_t offset = doffset;
hsize_t stride = 1;
hsize_t count = nNodes;
hid_t memspace_id = H5Screate_simple(1,&count,NULL);
// hsize_t variables for 3d quantities like position
hsize_t offset3D[2];
offset3D[0] = doffset;
hsize_t count3D[2] = {nNodes,1};
int i;
// open the hdf5 file and fill it
file_id = H5Fopen(filename , H5F_ACC_RDWR, H5P_DEFAULT);
group_id = H5Gopen(file_id, "/forestHalos");
float * buf_f = malloc(nNodes*sizeof(float));
long long int * buf_i = malloc(nNodes*sizeof(long long int));
// descendantIndex
dataset_id = H5Dopen(file_id,"/forestHalos/descendantIndex");
dataspace_id = H5Dget_space(dataset_id);
for(i=0;i<nNodes;i++) {
buf_i[i] = (*nodeData)[i].descendantIndex;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_LLONG, memspace_id, dataspace_id, H5P_DEFAULT, buf_i);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// expansionFactor
dataset_id = H5Dopen(file_id,"/forestHalos/expansionFactor");
dataspace_id = H5Dget_space (dataset_id);
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].expansionFactor;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// scaleRadius
dataset_id = H5Dopen(file_id,"/forestHalos/scaleRadius");
dataspace_id = H5Dget_space (dataset_id);
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].scaleRadius;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// hostIndex
dataset_id = H5Dopen(file_id,"/forestHalos/hostIndex");
dataspace_id = H5Dget_space(dataset_id);
for(i=0;i<nNodes;i++) {
buf_i[i] = (*nodeData)[i].hostIndex;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_LLONG, memspace_id, dataspace_id, H5P_DEFAULT, buf_i);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// nodeIndex
dataset_id = H5Dopen(file_id,"/forestHalos/nodeIndex");
dataspace_id = H5Dget_space(dataset_id);
for(i=0;i<nNodes;i++) {
buf_i[i] = (*nodeData)[i].nodeIndex;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_LLONG, memspace_id, dataspace_id, H5P_DEFAULT, buf_i);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// nodeMass
dataset_id = H5Dopen(file_id,"/forestHalos/nodeMass");
dataspace_id = H5Dget_space (dataset_id);
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].nodeMass;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// spin
dataset_id = H5Dopen(file_id,"/forestHalos/spin");
dataspace_id = H5Dget_space (dataset_id);
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].spin;
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, &offset, &stride, &count, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// position
dataset_id = H5Dopen(file_id,"/forestHalos/position");
dataspace_id = H5Dget_space (dataset_id);
// fill x
offset3D[1] = 0;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].position[0];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill y
offset3D[1] = 1;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].position[1];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill z
offset3D[1] = 2;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].position[2];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// velocity
dataset_id = H5Dopen(file_id,"/forestHalos/velocity");
dataspace_id = H5Dget_space (dataset_id);
// fill x
offset3D[1] = 0;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].velocity[0];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill y
offset3D[1] = 1;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].velocity[1];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill z
offset3D[1] = 2;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].velocity[2];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
// angularMomentum
dataset_id = H5Dopen(file_id,"/forestHalos/angularMomentum");
dataspace_id = H5Dget_space (dataset_id);
// fill x
offset3D[1] = 0;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].angularMomentum[0];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill y
offset3D[1] = 1;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].angularMomentum[1];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
// fill z
offset3D[1] = 2;
for(i=0;i<nNodes;i++) {
buf_f[i] = (*nodeData)[i].angularMomentum[2];
}
H5Sselect_hyperslab(dataspace_id,H5S_SELECT_SET, offset3D, NULL, count3D, NULL);
H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, memspace_id, dataspace_id, H5P_DEFAULT, buf_f);
H5Sclose(dataspace_id);
H5Dclose(dataset_id);
free(buf_f);
free(buf_i);
H5Gclose(group_id);
H5Fclose(file_id);
return 0;
}