HydroCODE_2D 0.1
This is a implementation of fully explict forward Euler scheme for 2-D Euler equations of motion on Eulerian coordinate
file_out_hdf5.c
浏览该文件的文档.
1
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "../include/var_struc.h"
12#include "../include/file_io.h"
13#ifdef HDF5PLOT
14#include "hdf5.h"
15
16
17/* Create the data itself in the dataset.
18 * dataset_id = H5Dcreate(loc_id (location id), const char *name (dataset name),
19 * hid_t dtype_id (data type) hid_t space_id (dataspace id),
20 * link created_property, dataset created_property, dataset accessed_property);
21 */
22/* Writes data to the dataset.
23 * herr_t write_status = H5Dwrite(dataset_id, memory data type,
24 * memory_dataspace_id (define the memory dataspace and its selection),
25 * - H5S_ALL: the file dataspace is used as the memory dataspace,
26 * the selection in file_dataspace_id is used as the memory dataspace selection.
27 * file_dataspace_id (define the file dataspace selection),
28 * - H5S_ALL: all of the datasapce in the file,
29 * defined as all of the dimensional data defined by datasapce in the dataset.
30 * conversion property of this I/O operation,
31 * const void * buf (the location of data in memory) );
32 */
33
37#define PRINT_NC(v, v_array) \
38 do { \
39 dataset_id = H5Dcreate(group_id, #v, H5T_NATIVE_FLOAT, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); \
40 status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, v_array); \
41 status = H5Dclose(dataset_id); \
42 } while (0)
43
54void file_1D_write_HDF5(const int m, const int N, const struct cell_var_stru CV,
55 double * X[], const double * cpu_time, const char * problem, double time_plot[])
56{
57 char add_out[FILENAME_MAX+40];
58 // Get the address of the output data folder of the test example.
59 example_io(problem, add_out, 0);
60
61 char file_data[FILENAME_MAX+40];
62 strcpy(file_data, add_out);
63 strcat(file_data, "FLU_VAR.h5");
64
65 double *XX = (double*)malloc(m * sizeof(double));
66 if(XX == NULL)
67 {
68 printf("NOT enough memory! plot X\n");
69 exit(5);
70 }
71
72 hid_t file_id, group_id, attr_id, dataspace_id, dataspaceA_id, dataset_id;
73 herr_t status;
74 const unsigned rank = 1;
75 const hsize_t dims[1] = {(hsize_t)m}, dimsA[1] = {1};
76 /*
77 * file_id = H5Fcreate(const char *filename,
78 * unsigned overlay_flag,
79 * - H5F_ACC_TRUNC->can overlay
80 * - H5F_ACC_EXCL ->cannot overlay, error
81 * hid_t created_property, hid_t accessed_property);
82 */
83 file_id = H5Fcreate(file_data, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
84 /* Create the dataspace information items in the metadata of the dataset.
85 * dataspace_id = H5Screate_simple(int rank (spatial dimension),
86 * const hsize_t* current_dims (number of elements per dimension),
87 * const hsize_t* max_dims, (upper limit on the number of elements per dimension)
88 * - NULL: same as current_dim.
89 * - H5S_UNLIMITED: no upper limit, but the dataset must be chunked.);
90 */
91 dataspace_id = H5Screate_simple(rank, dims, NULL);
92 dataspaceA_id = H5Screate_simple(rank, dimsA, NULL);
93
94 char group_name[14];
95 for(int k = 0; k < N; k++)
96 {
97 sprintf(group_name, "/T%d", k);
98 /*
99 * new_group_id = H5Gcreate2(group_id, absolute or relative group link name,
100 * link created_propertygroup created_property, group accessed_property);
101 */
102 group_id = H5Gcreate(file_id, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
103 // group2_id = H5Gcreate(group_id, "/MyGroup1/MyGroup2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
104 // group2_id = H5Gcreate(group_id, "./MyGroup2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
105
106 /* Create an attribute. */
107 attr_id = H5Acreate(group_id, "time_plot", H5T_NATIVE_FLOAT, dataspaceA_id, H5P_DEFAULT, H5P_DEFAULT);
108 status = H5Awrite(attr_id, H5T_NATIVE_DOUBLE, time_plot+k);
109 status = H5Aclose(attr_id);
110
111 PRINT_NC(RHO, CV.RHO[k]);
112 PRINT_NC(U, CV.U[k]);
113 PRINT_NC(P, CV.P[k]);
114 PRINT_NC(E, CV.E[k]);
115#ifdef RADIAL_BASICS
116 PRINT_NC(R, X[k]);
117#else
118 for(int j = 0; j < m; ++j)
119 XX[j] = 0.5 * (X[k][j] + X[k][j+1]);
120 PRINT_NC(X, XX);
121#endif
122
123 status = H5Gclose(group_id);
124 }
125
126 status = H5Sclose(dataspaceA_id);
127 status = H5Sclose(dataspace_id);
128 status = H5Fclose(file_id);
129
130 free(XX);
131 XX = NULL;
132 if(status)
133 return;
134 /*
135 * file_id = H5Fopen(const char *filename,
136 * unsigned read-write_flag,
137 * - H5F_ACC_RDWR read-write
138 * - H5F_ACC_RDONLY read only
139 * hid_t accessed_property);
140 */
141 // file_id = H5Fopen(file_data, H5F_ACC_RDWR, H5P_DEFAULT);
142 /*
143 * group_id = H5Gopen2(father group_id, absolute or relative group link name,
144 * group accessed_property);
145 */
146 // group_id = H5Gopen(file_id, "MyGroup1/MyGroup2", H5P_DEFAULT);
147}
148
149
162void file_2D_write_HDF5(const int n_x, const int n_y, const int N, const struct cell_var_stru CV[],
163 double ** X, double ** Y, const double * cpu_time, const char * problem, double time_plot[])
164{
165 char add_out[FILENAME_MAX+40];
166 // Get the address of the output data folder of the test example.
167 example_io(problem, add_out, 0);
168
169 char file_data[FILENAME_MAX+40];
170 strcpy(file_data, add_out);
171 strcat(file_data, "FLU_VAR.h5");
172
173 double ** XX, ** YY;
174 XX = (double **)malloc(n_x * sizeof(double *));
175 YY = (double **)malloc(n_x * sizeof(double *));
176 if(XX == NULL || YY == NULL)
177 {
178 printf("NOT enough memory! plot X or Y\n");
179 exit(5);
180 }
181 for(int j = 0; j < n_x; ++j)
182 {
183 XX[j] = (double *)malloc(n_y * sizeof(double));
184 YY[j] = (double *)malloc(n_y * sizeof(double));
185 if(XX[j] == NULL || YY[j] == NULL)
186 {
187 printf("NOT enough memory! plot X[%d] or Y[%d]\n", j, j);
188 exit(5);
189 }
190 }
191
192 hid_t file_id, group_id, attr_id, dataspace_id, dataspaceA_id, dataset_id;
193 herr_t status;
194 const unsigned rank = 2;
195 const hsize_t dims[2] = {(hsize_t)n_y, (hsize_t)n_x}, dimsA[1] = {1};
196
197 file_id = H5Fcreate(file_data, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
198 dataspace_id = H5Screate_simple(rank, dims, NULL);
199 dataspaceA_id = H5Screate_simple(rank, dimsA, NULL);
200
201 char group_name[14];
202 for(int k = 0; k < N; k++)
203 {
204 sprintf(group_name, "/T%d", k);
205 group_id = H5Gcreate(file_id, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
206
207 attr_id = H5Acreate(group_id, "time_plot", H5T_NATIVE_FLOAT, dataspaceA_id, H5P_DEFAULT, H5P_DEFAULT);
208 status = H5Awrite(attr_id, H5T_NATIVE_DOUBLE, time_plot+k);
209 status = H5Aclose(attr_id);
210
211 PRINT_NC(RHO, CV[k].RHO);
212 PRINT_NC(U, CV[k].U);
213 PRINT_NC(V, CV[k].V);
214 PRINT_NC(P, CV[k].P);
215 PRINT_NC(E, CV[k].E);
216 for(int i = 0; i < n_y; ++i)
217 for(int j = 0; j < n_x; ++j)
218 {
219 XX[j][i] = 0.25*(X[j][i] + X[j][i+1] + X[j+1][i] + X[j+1][i+1]);
220 YY[j][i] = 0.25*(Y[j][i] + Y[j][i+1] + Y[j+1][i] + Y[j+1][i+1]);
221 }
222 PRINT_NC(X, XX);
223 PRINT_NC(Y, YY);
224
225 status = H5Gclose(group_id);
226 }
227
228 status = H5Sclose(dataspaceA_id);
229 status = H5Sclose(dataspace_id);
230 status = H5Fclose(file_id);
231
232 for(int j = 0; j < n_x; ++j)
233 {
234 free(XX[j]);
235 free(YY[j]);
236 XX[j] = NULL;
237 YY[j] = NULL;
238 }
239 free(XX);
240 free(YY);
241 XX = NULL;
242 YY = NULL;
243 if(status)
244 return;
245}
246#endif
void file_1D_write_HDF5(const int m, const int N, const struct cell_var_stru CV, double *X[], const double *cpu_time, const char *problem, double time_plot[])
This function write the 1-D solution into HDF5 output '.h5' files.
Definition: file_out_hdf5.c:54
void file_2D_write_HDF5(const int n_x, const int n_y, const int N, const struct cell_var_stru CV[], double **X, double **Y, const double *cpu_time, const char *problem, double time_plot[])
This function write the 2-D solution into HDF5 output '.h5' files.
#define PRINT_NC(v, v_array)
Print out fluid variable 'v' with data array 'v_array'.
Definition: file_out_hdf5.c:37
void example_io(const char *example, char *add_mkdir, const int i_or_o)
This function produces folder path for data input or output.
Definition: io_control.c:40
pointer structure of VARiables on STRUctural computational grid CELLs.
Definition: var_struc.h:61
double ** E
specific total energy.
Definition: var_struc.h:62
double ** U
Definition: var_struc.h:63
double ** RHO
Definition: var_struc.h:63
double ** P
density, velocity components in direction x and y, pressure.
Definition: var_struc.h:63