-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5_io.c
257 lines (176 loc) · 7.16 KB
/
h5_io.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "hdf5.h"
#include <stdlib.h>
#include <string.h>
/*
_
|_| |_ | | _|_ o | _
| | _) |_| |_ | | _>
*/
void strcati(char *foobar, char *foo, char *bar) {
// Concat foo and bar and copying it foobar"
// Assume foobar is large enought"
memset(foobar, 0, strlen(foobar));
strcpy(foobar, foo);
strcat(foobar, bar);
}
void H5Dread666(hid_t file_id, char *h, char *path, hid_t H5T_USER, void *data) {
herr_t status;
hid_t dataset_id;
char str[80];
strcati(str, h, path);
printf("h5: Reading %s \n", str);
dataset_id = H5Dopen(file_id, str, H5P_DEFAULT);
status = H5Dread(dataset_id, H5T_USER, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
status = H5Dclose(dataset_id);
}
void H5Dread666_hyperslab(hid_t file_id, char *h, char *path, hsize_t *start, hsize_t *count, hid_t memspace, hid_t H5T_USER, void *data) {
char str[80];
hid_t dataset_id; /* identifiers */
hid_t dataspace_id; /* identifiers */
herr_t status;
memspace = H5Screate_simple(1, count, NULL);
strcati(str, h, path);
printf("h5: Reading %s from %i to %i \n", str, start[0], start[0]+count[0]);
dataset_id = H5Dopen(file_id, str, H5P_DEFAULT);
dataspace_id = H5Dget_space(dataset_id);
status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, start, NULL, count, NULL);
status = H5Dread(dataset_id, H5T_USER, memspace, dataspace_id, H5P_DEFAULT, data);
status = H5Dclose(dataset_id);
status = H5Sclose(dataspace_id);
}
void H5Dcreate666(hid_t file_id, char *h, char *path, hsize_t *dims, int dims_size, hid_t H5T_USER) {
herr_t status;
hid_t dataspace_id, dataset_id;
dataspace_id = H5Screate_simple(dims_size, dims, NULL);
char str[80];
strcati(str, h, path);
printf("h5: Creating %s \n", str);
dataset_id = H5Dcreate2(file_id, str, H5T_USER, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
status = H5Dclose(dataset_id);
status = H5Sclose(dataspace_id);
}
void H5Dcreate_scalar666w(hid_t file_id, char *h, char *path, hid_t H5T_USER, void *data) {
herr_t status;
hid_t dataspace_id, dataset_id;
dataspace_id = H5Screate(H5S_SCALAR);
char str[80];
strcati(str, h, path);
printf("h5: Creating and writting scalar %s \n", str);
dataset_id = H5Dcreate2(file_id, str, H5T_USER, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
status = H5Dwrite(dataset_id, H5T_USER, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
status = H5Dclose(dataset_id);
status = H5Sclose(dataspace_id);
}
void H5Dwrite666(hid_t file_id, char *h, char *path, hid_t H5T_USER, void *data) {
herr_t status;
hid_t dataset_id;
char str[80];
strcati(str, h, path);
printf("h5: Writing %s \n", str);
dataset_id = H5Dopen(file_id, str, H5P_DEFAULT);
status = H5Dwrite(dataset_id, H5T_USER, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
status = H5Dclose(dataset_id);
}
/*
_ _
|_) _ _. _| _|_ ._ _ _|_ o _ ._
| \ (/_ (_| (_| | |_| | | (_ |_ | (_) | |
*/
void get_param_h5_int_(char *h5path, int *n_bielec, int *n_hcore, int *flag) {
char *h = (flag[0] == 0) ? "mo" : "ao";
hid_t file_id;
herr_t status;
file_id = H5Fopen(h5path, H5F_ACC_RDONLY, H5P_DEFAULT);
H5Dread666(file_id, h, "/ints/two_e/n", H5T_NATIVE_INT, n_bielec);
H5Dread666(file_id, h, "/ints/one_e/hcore/n", H5T_NATIVE_INT, n_hcore);
status = H5Fclose(file_id);
}
void read_bielec_h5_(char *h5path, int32_t *bielec_idx, double *bielec_val, int *offset, int *chunk_size, int *flag) {
char *h = (flag[0] == 0) ? "mo" : "ao";
hid_t file_id; /* identifiers */
herr_t status;
hid_t memspace;
file_id = H5Fopen(h5path, H5F_ACC_RDONLY, H5P_DEFAULT);
hsize_t start[1] = {offset[0]};
hsize_t count[1] = {chunk_size[0]};
memspace = H5Screate_simple(1, count, NULL);
H5Dread666_hyperslab(file_id, h, "/ints/two_e/value", start, count, memspace, H5T_NATIVE_DOUBLE, bielec_val);
H5Dread666_hyperslab(file_id, h, "/ints/two_e/idx", start, count, memspace, H5T_NATIVE_LLONG, bielec_idx);
status = H5Fclose(file_id);
}
void read_hcore_h5_(char *h5path, int32_t *bielec_idx, double *bielec_val, int *flag) {
char *h = (flag[0] == 0) ? "mo" : "ao";
hid_t file_id; /* identifiers */
herr_t status;
file_id = H5Fopen(h5path, H5F_ACC_RDONLY, H5P_DEFAULT);
H5Dread666(file_id, h, "/ints/one_e/hcore/value", H5T_NATIVE_DOUBLE, bielec_val);
H5Dread666(file_id, h, "/ints/one_e/hcore/idx", H5T_NATIVE_LLONG, bielec_idx);
status = H5Fclose(file_id);
}
/*
_
\ / ._ o _|_ _ _|_ ._ _ _|_ o _ ._
\/\/ | | |_ (/_ | |_| | | (_ |_ | (_) | |
*/
void write_init_h5_(char *h5path, int *o_tot_num, int *bi_elec_num, int *flag, double *nr) {
herr_t status;
hid_t dataset_id;
hsize_t dims[2];
hid_t file_id = H5Fcreate(h5path, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
// =~=~=~=
// H_core
// =~=~=~=
hid_t grp_crt_plist = H5Pcreate(H5P_LINK_CREATE);
/* Set flag for intermediate group creation */
status = H5Pset_create_intermediate_group(grp_crt_plist, 1);
char *h = (flag[0] == 0) ? "mo" : "ao";
char str[80];
strcati(str, h, "/ints/one_e/hcore");
dataset_id = H5Gcreate2(file_id, str, grp_crt_plist, H5P_DEFAULT, H5P_DEFAULT);
H5Gclose(dataset_id);
dims[0] = 1;
int n = o_tot_num[0]*o_tot_num[0];
H5Dcreate_scalar666w(file_id, h, "/ints/one_e/hcore/n", H5T_NATIVE_INT, &n);
dims[0] = o_tot_num[0] * o_tot_num[0];
dims[1] = 2;
H5Dcreate666(file_id, h, "/ints/one_e/hcore/idx", dims, 2, H5T_NATIVE_INT);
H5Dcreate666(file_id, h, "/ints/one_e/hcore/value", dims, 1, H5T_NATIVE_DOUBLE);
strcati(str, h, "/ints/two_e");
dataset_id = H5Gcreate2(file_id, str, grp_crt_plist, H5P_DEFAULT, H5P_DEFAULT);
H5Gclose(dataset_id);
H5Dcreate_scalar666w(file_id, h, "/ints/two_e/n", H5T_NATIVE_INT, bi_elec_num);
dims[0] = bi_elec_num[0];
H5Dcreate666(file_id, h, "/ints/two_e/idx", dims, 1, H5T_NATIVE_LLONG);
H5Dcreate666(file_id, h, "/ints/two_e/value", dims, 1, H5T_NATIVE_DOUBLE);
dataset_id = H5Gcreate2(file_id, "nuclei", grp_crt_plist, H5P_DEFAULT, H5P_DEFAULT);
H5Gclose(dataset_id);
H5Dcreate_scalar666w(file_id, "nuclei", "/nuclear_repulsion", H5T_NATIVE_DOUBLE, nr);
status = H5Fclose(file_id);
}
void write_hcore_h5_(char *h5path, int *o_tot_num, double *moelec_val, int *flag) {
hid_t file_id; /* identifiers */
herr_t status;
int i, j, k = 0;
long *ptr = (long *)malloc(o_tot_num[0] * o_tot_num[0] * 2 * sizeof(long));
for (i = 0; i < o_tot_num[0]; i++) {
for (j = 0; j < o_tot_num[0]; j++) {
ptr[k] = i;
ptr[k + 1] = j;
k += 2;
}
}
char *h = (flag[0] == 0) ? "mo" : "ao";
file_id = H5Fopen(h5path, H5F_ACC_RDWR, H5P_DEFAULT);
H5Dwrite666(file_id, h, "/ints/one_e/hcore/idx", H5T_NATIVE_LLONG, ptr);
H5Dwrite666(file_id, h, "/ints/one_e/hcore/value", H5T_NATIVE_DOUBLE, moelec_val);
status = H5Fclose(file_id);
}
void write_bielec_h5_(char *h5path, int *bielec_idx, int *bielec_val, int *flag) {
hid_t file_id; /* identifiers */
herr_t status;
file_id = H5Fopen(h5path, H5F_ACC_RDWR, H5P_DEFAULT);
char *h = (flag[0] == 0) ? "mo" : "ao";
H5Dwrite666(file_id, h, "/ints/two_e/idx", H5T_NATIVE_LLONG, bielec_idx);
H5Dwrite666(file_id, h, "/ints/two_e/value", H5T_NATIVE_DOUBLE, bielec_val);
status = H5Fclose(file_id);
}