|
8 | 8 |
|
9 | 9 | void WriteMultiFabToHDF5(const std::string& plotfile, const std::string& hdf5file) {
|
10 | 10 |
|
11 |
| - amrex::PlotFileData plotfile_data(plotfile); |
12 |
| - amrex::Vector<std::string> varnames = plotfile_data.varNames(); |
13 |
| - double time = plotfile_data.time(); |
14 |
| - int time_step = plotfile_data.levelStep(0); |
| 11 | + const amrex::PlotFileData plotfile_data(plotfile); |
| 12 | + const amrex::Vector<std::string> varnames = plotfile_data.varNames(); |
| 13 | + const double time = plotfile_data.time(); |
| 14 | + const int level = 0; // Assume there is only one level, we are reading from level 0 |
| 15 | + const int time_step = plotfile_data.levelStep(level); |
| 16 | + const double dx = plotfile_data.cellSize(level)[0]; |
| 17 | + const double dy = plotfile_data.cellSize(level)[1]; |
15 | 18 |
|
16 | 19 | amrex::MultiFab mf;
|
17 | 20 |
|
18 | 21 | amrex::VisMF::Read(mf, plotfile+"/Level_0/Cell");
|
19 | 22 |
|
20 |
| - amrex::BoxArray ba = mf.boxArray(); |
| 23 | + const amrex::BoxArray ba = mf.boxArray(); |
21 | 24 |
|
| 25 | + // Get the dimensions of the entire MultiFab |
22 | 26 | // Expecting a cell centered box with low and high index bounds {0,0} to {nx-1,ny-1}
|
23 |
| - amrex::Box minimal_box = ba.minimalBox(); |
| 27 | + const amrex::Box minimal_box = ba.minimalBox(); |
24 | 28 | AMREX_ASSERT(minimal_box.smallEnd(0) == 0);
|
25 | 29 | AMREX_ASSERT(minimal_box.smallEnd(1) == 0);
|
| 30 | + const int nx = minimal_box.length(0); |
| 31 | + const int ny = minimal_box.length(1); |
26 | 32 |
|
27 |
| - hid_t file_id = H5Fcreate(hdf5file.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
| 33 | + const hid_t file_id = H5Fcreate(hdf5file.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
28 | 34 |
|
29 | 35 | {
|
30 |
| - hid_t attr_space_id = H5Screate(H5S_SCALAR); |
31 |
| - hid_t attr_id = H5Acreate(file_id, "time", H5T_NATIVE_DOUBLE, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 36 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 37 | + const hid_t attr_id = H5Acreate(file_id, "time", H5T_NATIVE_DOUBLE, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
32 | 38 | H5Awrite(attr_id, H5T_NATIVE_DOUBLE, &time);
|
33 | 39 | H5Aclose(attr_id);
|
34 | 40 | H5Sclose(attr_space_id);
|
35 | 41 | }
|
36 | 42 |
|
37 | 43 | {
|
38 |
| - hid_t attr_space_id = H5Screate(H5S_SCALAR); |
39 |
| - hid_t attr_id = H5Acreate(file_id, "time_step", H5T_NATIVE_INT, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 44 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 45 | + const hid_t attr_id = H5Acreate(file_id, "time_step", H5T_NATIVE_INT, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
40 | 46 | H5Awrite(attr_id, H5T_NATIVE_INT, &time_step);
|
41 | 47 | H5Aclose(attr_id);
|
42 | 48 | H5Sclose(attr_space_id);
|
43 | 49 | }
|
44 | 50 |
|
45 |
| - // Add an attribute to specify the data layout of the following datasets (row-major or column-major) |
46 | 51 | {
|
47 |
| - hid_t attr_space_id = H5Screate(H5S_SCALAR); |
48 |
| - hid_t attr_id = H5Acreate(file_id, "data_layout", H5T_C_S1, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
49 |
| - const char* layout = "row-major"; |
50 |
| - H5Awrite(attr_id, H5T_C_S1, layout); |
| 52 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 53 | + const hid_t attr_id = H5Acreate(file_id, "dx", H5T_NATIVE_DOUBLE, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 54 | + H5Awrite(attr_id, H5T_NATIVE_DOUBLE, &dx); |
| 55 | + H5Aclose(attr_id); |
| 56 | + H5Sclose(attr_space_id); |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 61 | + const hid_t attr_id = H5Acreate(file_id, "dy", H5T_NATIVE_DOUBLE, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 62 | + H5Awrite(attr_id, H5T_NATIVE_DOUBLE, &dy); |
| 63 | + H5Aclose(attr_id); |
| 64 | + H5Sclose(attr_space_id); |
| 65 | + } |
| 66 | + |
| 67 | + { |
| 68 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 69 | + const hid_t attr_id = H5Acreate(file_id, "nx", H5T_NATIVE_INT, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 70 | + H5Awrite(attr_id, H5T_NATIVE_INT, &nx); |
51 | 71 | H5Aclose(attr_id);
|
52 | 72 | H5Sclose(attr_space_id);
|
53 | 73 | }
|
54 | 74 |
|
55 |
| - // Get the dimensions of the MultiFab |
56 |
| - hsize_t dims[2] = {static_cast<hsize_t>(minimal_box.length(0)), static_cast<hsize_t>(minimal_box.length(1))}; |
| 75 | + { |
| 76 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 77 | + const hid_t attr_id = H5Acreate(file_id, "ny", H5T_NATIVE_INT, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 78 | + H5Awrite(attr_id, H5T_NATIVE_INT, &ny); |
| 79 | + H5Aclose(attr_id); |
| 80 | + H5Sclose(attr_space_id); |
| 81 | + } |
| 82 | + |
| 83 | + // Add an attribute to specify the data layout of the following datasets (row-major or column-major) |
| 84 | + { |
| 85 | + const char* layout = "row-major"; |
| 86 | + const hid_t str_type = H5Tcopy(H5T_C_S1); |
| 87 | + H5Tset_size(str_type, strlen(layout) + 1); // +1 for the null terminator |
| 88 | + |
| 89 | + const hid_t attr_space_id = H5Screate(H5S_SCALAR); |
| 90 | + const hid_t attr_id = H5Acreate(file_id, "data_layout", str_type, attr_space_id, H5P_DEFAULT, H5P_DEFAULT); |
| 91 | + H5Awrite(attr_id, str_type, layout); |
| 92 | + H5Aclose(attr_id); |
| 93 | + H5Sclose(attr_space_id); |
| 94 | + H5Tclose(str_type); |
| 95 | + } |
57 | 96 |
|
58 | 97 | // Iterate over the components of the MultiFab
|
59 | 98 | for (int component_idx = 0; component_idx < mf.nComp(); ++component_idx) {
|
60 | 99 | // Create a dataset for this component
|
61 |
| - hid_t dataspace_id = H5Screate_simple(2, dims, NULL); |
62 |
| - hid_t dataset_id = H5Dcreate(file_id, varnames[component_idx].c_str(), H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
| 100 | + const hsize_t dims[2] = {static_cast<hsize_t>(nx), static_cast<hsize_t>(ny)}; |
| 101 | + const hid_t dataspace_id = H5Screate_simple(2, dims, NULL); |
| 102 | + const hid_t dataset_id = H5Dcreate(file_id, varnames[component_idx].c_str(), H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
63 | 103 |
|
64 | 104 | std::vector<double> component_data(dims[0]*dims[1]);
|
65 | 105 |
|
|
0 commit comments