mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-29 06:35:48 -04:00
Convert HDF5 write_string to C++
This commit is contained in:
parent
aa5a5cf7db
commit
2760bce679
4 changed files with 56 additions and 95 deletions
|
|
@ -164,6 +164,15 @@ module hdf5_interface
|
|||
integer(C_LONG_LONG), intent(in) :: buffer(*)
|
||||
logical(C_BOOL), intent(in) :: indep
|
||||
end subroutine write_llong_c
|
||||
|
||||
subroutine write_string_c(group_id, name, buffer, indep) &
|
||||
bind(C, name='write_string')
|
||||
import HID_T, HSIZE_T, C_CHAR, C_BOOL
|
||||
integer(HID_T), value :: group_id
|
||||
character(kind=C_CHAR), intent(in) :: name(*)
|
||||
character(kind=C_CHAR), intent(in) :: buffer(*)
|
||||
logical(C_BOOL), intent(in) :: indep
|
||||
end subroutine write_string_c
|
||||
end interface
|
||||
|
||||
contains
|
||||
|
|
@ -888,58 +897,12 @@ contains
|
|||
character(*), intent(in), target :: buffer ! read data to here
|
||||
logical, intent(in), optional :: indep ! independent I/O
|
||||
|
||||
integer :: hdf5_err
|
||||
integer :: data_xfer_mode
|
||||
#ifdef PHDF5
|
||||
integer(HID_T) :: plist ! property list
|
||||
#endif
|
||||
integer(HID_T) :: dset ! data set handle
|
||||
integer(HID_T) :: dspace ! data or file space handle
|
||||
integer(HID_T) :: filetype
|
||||
integer(SIZE_T) :: i, n
|
||||
character(kind=C_CHAR), allocatable, target :: temp_buffer(:)
|
||||
type(c_ptr) :: f_ptr
|
||||
logical(C_BOOL) :: indep_
|
||||
|
||||
! Set up collective vs. independent I/O
|
||||
data_xfer_mode = H5FD_MPIO_COLLECTIVE_F
|
||||
if (present(indep)) then
|
||||
if (indep) data_xfer_mode = H5FD_MPIO_INDEPENDENT_F
|
||||
end if
|
||||
indep_ = .false.
|
||||
if (present(indep)) indep_ = indep
|
||||
|
||||
! Create datatype for HDF5 file based on C char
|
||||
n = len_trim(buffer)
|
||||
if (n > 0) then
|
||||
call h5tcopy_f(H5T_C_S1, filetype, hdf5_err)
|
||||
call h5tset_size_f(filetype, n, hdf5_err)
|
||||
|
||||
! Create dataspace/dataset
|
||||
call h5screate_f(H5S_SCALAR_F, dspace, hdf5_err)
|
||||
call h5dcreate_f(group_id, trim(name), filetype, dspace, dset, hdf5_err)
|
||||
|
||||
! Copy string to temporary buffer
|
||||
allocate(temp_buffer(n))
|
||||
do i = 1, n
|
||||
temp_buffer(i) = buffer(i:i)
|
||||
end do
|
||||
|
||||
! Get pointer to start of string
|
||||
f_ptr = c_loc(temp_buffer(1))
|
||||
|
||||
if (using_mpio_device(group_id)) then
|
||||
#ifdef PHDF5
|
||||
call h5pcreate_f(H5P_DATASET_XFER_F, plist, hdf5_err)
|
||||
call h5pset_dxpl_mpio_f(plist, data_xfer_mode, hdf5_err)
|
||||
call h5dwrite_f(dset, filetype, f_ptr, hdf5_err, xfer_prp=plist)
|
||||
call h5pclose_f(plist, hdf5_err)
|
||||
#endif
|
||||
else
|
||||
call h5dwrite_f(dset, filetype, f_ptr, hdf5_err)
|
||||
end if
|
||||
|
||||
call h5dclose_f(dset, hdf5_err)
|
||||
call h5sclose_f(dspace, hdf5_err)
|
||||
call h5tclose_f(filetype, hdf5_err)
|
||||
end if
|
||||
call write_string_c(group_id, to_c_string(name), to_c_string(buffer), indep_)
|
||||
end subroutine write_string
|
||||
|
||||
!===============================================================================
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "hdf5_hl.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
|
|
@ -160,7 +161,7 @@ open_group(hid_t group_id, const char* name)
|
|||
|
||||
|
||||
void
|
||||
read_array(hid_t obj_id, const char* name, hid_t mem_type_id,
|
||||
read_data(hid_t obj_id, const char* name, hid_t mem_type_id,
|
||||
void* buffer, bool indep)
|
||||
{
|
||||
hid_t dset = obj_id;
|
||||
|
|
@ -190,26 +191,26 @@ read_array(hid_t obj_id, const char* name, hid_t mem_type_id,
|
|||
void
|
||||
read_double(hid_t obj_id, const char* name, double* buffer, bool indep)
|
||||
{
|
||||
read_array(obj_id, name, H5T_NATIVE_DOUBLE, buffer, indep);
|
||||
read_data(obj_id, name, H5T_NATIVE_DOUBLE, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
read_int(hid_t obj_id, const char* name, int* buffer, bool indep)
|
||||
{
|
||||
read_array(obj_id, name, H5T_NATIVE_INT, buffer, indep);
|
||||
read_data(obj_id, name, H5T_NATIVE_INT, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
read_llong(hid_t obj_id, const char* name, long long* buffer, bool indep)
|
||||
{
|
||||
read_array(obj_id, name, H5T_NATIVE_LLONG, buffer, indep);
|
||||
read_data(obj_id, name, H5T_NATIVE_LLONG, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
write_array(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
write_data(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
hid_t mem_type_id, const void* buffer, bool indep)
|
||||
{
|
||||
// If array is given, create a simple dataspace. Otherwise, create a scalar
|
||||
|
|
@ -251,7 +252,7 @@ void
|
|||
write_double(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
const double* buffer, bool indep)
|
||||
{
|
||||
write_array(group_id, ndim, dims, name, H5T_NATIVE_DOUBLE, buffer, indep);
|
||||
write_data(group_id, ndim, dims, name, H5T_NATIVE_DOUBLE, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -259,7 +260,7 @@ void
|
|||
write_int(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
const int* buffer, bool indep)
|
||||
{
|
||||
write_array(group_id, ndim, dims, name, H5T_NATIVE_INT, buffer, indep);
|
||||
write_data(group_id, ndim, dims, name, H5T_NATIVE_INT, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -267,34 +268,31 @@ void
|
|||
write_llong(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
const long long* buffer, bool indep)
|
||||
{
|
||||
write_array(group_id, ndim, dims, name, H5T_NATIVE_LLONG, buffer, indep);
|
||||
write_data(group_id, ndim, dims, name, H5T_NATIVE_LLONG, buffer, indep);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
write_string(hid_t group_id, char const *name, char const *buffer)
|
||||
write_string(hid_t group_id, char const* name, const char* buffer, bool indep)
|
||||
{
|
||||
size_t buffer_len = strlen(buffer);
|
||||
hid_t datatype = H5Tcopy(H5T_C_S1);
|
||||
H5Tset_size(datatype, buffer_len);
|
||||
if (buffer_len > 0) {
|
||||
// Set up appropriate datatype for a fixed-length string
|
||||
hid_t datatype = H5Tcopy(H5T_C_S1);
|
||||
H5Tset_size(datatype, buffer_len);
|
||||
|
||||
hid_t dataspace = H5Screate(H5S_SCALAR);
|
||||
write_data(group_id, 0, nullptr, name, datatype, buffer, indep);
|
||||
|
||||
hid_t dataset = H5Dcreate(group_id, name, datatype, dataspace,
|
||||
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
|
||||
H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
|
||||
|
||||
H5Tclose(datatype);
|
||||
H5Sclose(dataspace);
|
||||
H5Dclose(dataset);
|
||||
// Free resources
|
||||
H5Tclose(datatype);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
write_string(hid_t group_id, char const *name, const std::string &buffer)
|
||||
write_string(hid_t group_id, char const* name, const std::string& buffer, bool indep)
|
||||
{
|
||||
write_string(group_id, name, buffer.c_str());
|
||||
write_string(group_id, name, buffer.c_str(), indep);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,8 +41,8 @@ write_double_1D(hid_t group_id, char const *name,
|
|||
H5Dclose(dataset);
|
||||
}
|
||||
|
||||
void read_array(hid_t obj_id, const char* name, double* buffer,
|
||||
hid_t mem_type_id, bool indep);
|
||||
void read_data(hid_t obj_id, const char* name, double* buffer,
|
||||
hid_t mem_type_id, bool indep);
|
||||
extern "C" void read_double(hid_t obj_id, const char* name, double* buffer,
|
||||
bool indep);
|
||||
extern "C" void read_int(hid_t obj_id, const char* name, int* buffer,
|
||||
|
|
@ -50,8 +50,8 @@ extern "C" void read_int(hid_t obj_id, const char* name, int* buffer,
|
|||
extern "C" void read_llong(hid_t obj_id, const char* name, long long* buffer,
|
||||
bool indep);
|
||||
|
||||
void write_array(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
hid_t mem_type_id, const void* buffer, bool indep);
|
||||
void write_data(hid_t group_id, int ndim, const hsize_t* dims, const char* name,
|
||||
hid_t mem_type_id, const void* buffer, bool indep);
|
||||
extern "C" void write_double(hid_t group_id, int ndim, const hsize_t* dims,
|
||||
const char* name, const double* buffer, bool indep);
|
||||
extern "C" void write_int(hid_t group_id, int ndim, const hsize_t* dims,
|
||||
|
|
@ -59,8 +59,8 @@ extern "C" void write_int(hid_t group_id, int ndim, const hsize_t* dims,
|
|||
extern "C" void write_llong(hid_t group_id, int ndim, const hsize_t* dims,
|
||||
const char* name, const long long* buffer, bool indep);
|
||||
|
||||
void write_string(hid_t group_id, char const *name, char const *buffer);
|
||||
void write_string(hid_t group_id, char const *name, const std::string &buffer);
|
||||
extern "C" void write_string(hid_t group_id, char const *name, char const *buffer, bool indep);
|
||||
void write_string(hid_t group_id, char const *name, const std::string &buffer, bool indep);
|
||||
|
||||
} // namespace openmc
|
||||
#endif //HDF5_INTERFACE_H
|
||||
|
|
|
|||
|
|
@ -202,21 +202,21 @@ Surface::to_hdf5(hid_t group_id) const
|
|||
|
||||
switch(bc) {
|
||||
case BC_TRANSMIT :
|
||||
write_string(surf_group, "boundary_type", "transmission");
|
||||
write_string(surf_group, "boundary_type", "transmission", false);
|
||||
break;
|
||||
case BC_VACUUM :
|
||||
write_string(surf_group, "boundary_type", "vacuum");
|
||||
write_string(surf_group, "boundary_type", "vacuum", false);
|
||||
break;
|
||||
case BC_REFLECT :
|
||||
write_string(surf_group, "boundary_type", "reflective");
|
||||
write_string(surf_group, "boundary_type", "reflective", false);
|
||||
break;
|
||||
case BC_PERIODIC :
|
||||
write_string(surf_group, "boundary_type", "periodic");
|
||||
write_string(surf_group, "boundary_type", "periodic", false);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!name.empty()) {
|
||||
write_string(surf_group, "name", name);
|
||||
write_string(surf_group, "name", name, false);
|
||||
}
|
||||
|
||||
to_hdf5_inner(surf_group);
|
||||
|
|
@ -297,7 +297,7 @@ inline void SurfaceXPlane::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceXPlane::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "x-plane");
|
||||
write_string(group_id, "type", "x-plane", false);
|
||||
std::array<double, 1> coeffs {{x0}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -362,7 +362,7 @@ inline void SurfaceYPlane::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceYPlane::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "y-plane");
|
||||
write_string(group_id, "type", "y-plane", false);
|
||||
std::array<double, 1> coeffs {{y0}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -428,7 +428,7 @@ inline void SurfaceZPlane::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "z-plane");
|
||||
write_string(group_id, "type", "z-plane", false);
|
||||
std::array<double, 1> coeffs {{z0}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -489,7 +489,7 @@ SurfacePlane::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "plane");
|
||||
write_string(group_id, "type", "plane", false);
|
||||
std::array<double, 4> coeffs {{A, B, C, D}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -621,7 +621,7 @@ inline void SurfaceXCylinder::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "x-cylinder");
|
||||
write_string(group_id, "type", "x-cylinder", false);
|
||||
std::array<double, 3> coeffs {{y0, z0, r}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -655,7 +655,7 @@ inline void SurfaceYCylinder::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "y-cylinder");
|
||||
write_string(group_id, "type", "y-cylinder", false);
|
||||
std::array<double, 3> coeffs {{x0, z0, r}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -689,7 +689,7 @@ inline void SurfaceZCylinder::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceZCylinder::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "z-cylinder");
|
||||
write_string(group_id, "type", "z-cylinder", false);
|
||||
std::array<double, 3> coeffs {{x0, y0, r}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -760,7 +760,7 @@ inline void SurfaceSphere::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "sphere");
|
||||
write_string(group_id, "type", "sphere", false);
|
||||
std::array<double, 4> coeffs {{x0, y0, z0, r}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -877,7 +877,7 @@ inline void SurfaceXCone::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "x-cone");
|
||||
write_string(group_id, "type", "x-cone", false);
|
||||
std::array<double, 4> coeffs {{x0, y0, z0, r_sq}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -911,7 +911,7 @@ inline void SurfaceYCone::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "y-cone");
|
||||
write_string(group_id, "type", "y-cone", false);
|
||||
std::array<double, 4> coeffs {{x0, y0, z0, r_sq}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -945,7 +945,7 @@ inline void SurfaceZCone::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "z-cone");
|
||||
write_string(group_id, "type", "z-cone", false);
|
||||
std::array<double, 4> coeffs {{x0, y0, z0, r_sq}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
@ -1039,7 +1039,7 @@ SurfaceQuadric::normal(const double xyz[3], double uvw[3]) const
|
|||
|
||||
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
|
||||
{
|
||||
write_string(group_id, "type", "quadric");
|
||||
write_string(group_id, "type", "quadric", false);
|
||||
std::array<double, 10> coeffs {{A, B, C, D, E, F, G, H, J, K}};
|
||||
write_double_1D(group_id, "coefficients", coeffs);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue