Skip to content

Commit f864deb

Browse files
committed
Update hdf5 output to contain the mesh information. This way h5diff detects difference mesh sizes as a solution verification fail.
1 parent 9edca70 commit f864deb

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

swm_AMReX/plotting_utils/plotfile_2_hdf5.cpp

+60-20
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,98 @@
88

99
void WriteMultiFabToHDF5(const std::string& plotfile, const std::string& hdf5file) {
1010

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];
1518

1619
amrex::MultiFab mf;
1720

1821
amrex::VisMF::Read(mf, plotfile+"/Level_0/Cell");
1922

20-
amrex::BoxArray ba = mf.boxArray();
23+
const amrex::BoxArray ba = mf.boxArray();
2124

25+
// Get the dimensions of the entire MultiFab
2226
// 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();
2428
AMREX_ASSERT(minimal_box.smallEnd(0) == 0);
2529
AMREX_ASSERT(minimal_box.smallEnd(1) == 0);
30+
const int nx = minimal_box.length(0);
31+
const int ny = minimal_box.length(1);
2632

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);
2834

2935
{
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);
3238
H5Awrite(attr_id, H5T_NATIVE_DOUBLE, &time);
3339
H5Aclose(attr_id);
3440
H5Sclose(attr_space_id);
3541
}
3642

3743
{
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);
4046
H5Awrite(attr_id, H5T_NATIVE_INT, &time_step);
4147
H5Aclose(attr_id);
4248
H5Sclose(attr_space_id);
4349
}
4450

45-
// Add an attribute to specify the data layout of the following datasets (row-major or column-major)
4651
{
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);
5171
H5Aclose(attr_id);
5272
H5Sclose(attr_space_id);
5373
}
5474

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+
}
5796

5897
// Iterate over the components of the MultiFab
5998
for (int component_idx = 0; component_idx < mf.nComp(); ++component_idx) {
6099
// 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);
63103

64104
std::vector<double> component_data(dims[0]*dims[1]);
65105

swm_AMReX/run_local.sh

+11-12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ fi
7676
# Solution Verification
7777
##############################################################################
7878
if [ "$solution_verification_method" != "none" ]; then
79+
80+
print_banner "Solution Verification"
81+
echo " "
82+
7983
if [ "$solution_verification_method" == "plotfile" ]; then
8084

8185
# Look for files that match the pattern fcompare*.ex in the directory $AMREX_HOME/Tools/Plotfile/
@@ -109,13 +113,9 @@ if [ "$solution_verification_method" != "none" ]; then
109113
#compare_exe="h5diff --use-system-epsilon" # compare to machine epsilon
110114
#compare_exe="h5diff --relative=1.0e-2" # compare to 1% relative error
111115

112-
reference_file="$SWM_AMREX_ROOT"/plt00100_reference.h5
116+
reference_file="$SWM_AMREX_ROOT"/plt00100_64_reference.h5
117+
#reference_file="$SWM_AMREX_ROOT"/plt00100_4096_reference.h5
113118

114-
## First we need to convert the plotfile to hdf5
115-
#$SWM_AMREX_ROOT/plotting_utils/convert_to_hdf5_and_plot.sh "$run_dir"
116-
117-
#file_to_compare="$run_dir"/plt00100.h5
118-
119119
## Call the function and get the path to the executable
120120
# This function sets the variable plotfile_2_hdf5_exe to the path of the executable.
121121
build_plotfile_to_hdf5_exe
@@ -127,9 +127,11 @@ if [ "$solution_verification_method" != "none" ]; then
127127
fi
128128

129129
plt_file="$run_dir"/plt00100
130-
hdf5_file="$run_dir"/plt00100.h5
130+
hdf5_file=${plt_file}.h5 # put the hdf5 file in the same directory as the plotfile and have the same base name as the plotfile, just append the .h5 extension
131131
"${plotfile_2_hdf5_exe}" infile="$plt_file" outfile="$hdf5_file"
132132

133+
echo "Converted $plt_file to $hdf5_file"
134+
133135
file_to_compare="$hdf5_file"
134136

135137
else
@@ -138,15 +140,12 @@ if [ "$solution_verification_method" != "none" ]; then
138140
exit 1
139141
fi
140142

141-
print_banner "Solution Verification"
142-
echo " "
143143
echo "Comparing $reference_file to $file_to_compare"
144-
echo " "
145144

146145
# Turn off exit on error for this one command since we want to check the return value
147146
set +e
148147

149-
$compare_exe "$reference_file" "$file_to_compare"
148+
$compare_exe $reference_file $file_to_compare
150149

151150
if [ $? -eq 0 ]; then
152151
echo -e "\nSolution Verification: PASS"
@@ -176,4 +175,4 @@ fi
176175

177176
##############################################################################
178177
# Create Movie
179-
##############################################################################
178+
##############################################################################

0 commit comments

Comments
 (0)