Skip to content

Commit

Permalink
Implemented HDF5 output
Browse files Browse the repository at this point in the history
  • Loading branch information
mattloulou committed Aug 30, 2024
1 parent 9493ae7 commit 9bc6e61
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 28 deletions.
42 changes: 22 additions & 20 deletions libNeonCore/include/Neon/core/tools/io/ioToHDF5.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Neon {
/**
* Namespace for this tool
*/
namespace ioToNanoVDBns {
namespace ioToHDF5ns {

/**
* Implicit function that defines the data stores by a user fields
Expand Down Expand Up @@ -79,8 +79,8 @@ namespace helpNs {
} // namespace helpNs

template <typename intType_ta, typename real_tt = double>
void ioToNanoVDB(const ioToNanoVDBns::UserFieldInformation<intType_ta, real_tt>& fieldData /*! User data that defines the field */,
ioToNanoVDBns::UserFieldAccessMask<real_tt, intType_ta> mask /*! Stores a mask for which indices in the field should be outputted*/,
void ioToHDF5(const ioToHDF5ns::UserFieldInformation<intType_ta, real_tt>& fieldData /*! User data that defines the field */,
ioToHDF5ns::UserFieldAccessMask<real_tt, intType_ta> mask /*! Stores a mask for which indices in the field should be outputted*/,
const std::string& filename /*! File name */,
const Neon::Integer_3d<intType_ta>& dim /*! Dimension of the field */,
[[maybe_unused]] double spacingScale = 1.0 /*! Spacing, i.e. size of a voxel */,
Expand All @@ -90,17 +90,19 @@ void ioToNanoVDB(const ioToNanoVDBns::UserFieldInformation<intType_ta, real_tt>&
{

if (fieldData.m_cardinality != 1) {
std::string msg = std::string("Too many components specified during attempt at creating hdf5 output. It currently only supports 1 component.");
NeonException exception("ioToNanoVDB");
std::string msg = std::string("Too many components specified during attempt at creating HDF5 output. It currently only supports 1 component.");
NeonException exception("ioToHDF5");
exception << msg;
NEON_THROW(exception);
}

// create the dataset
HighFive::File file(filename, HighFive::File::ReadWrite | HighFive::File::Create | HighFive::File::Truncate);
HighFive::DataSetCreateProps props;
props.add(HighFive::Chunking(std::vector<intType_ta>{chunking.x, chunking.y, chunking.z}));
HighFive::DataSet dataset = file.createDataSet<real_tt>(filename, HighFive::DataSpace({dim.x, dim.y, dim.z}), props);

// write the values to the dataset
for (int i = origin.x; i < origin.x + dim.x; ++i) {
for (int j = origin.y; j < origin.y + dim.y; ++j) {
for (int k = origin.z; k < origin.z + dim.z; ++k) {
Expand All @@ -112,32 +114,32 @@ void ioToNanoVDB(const ioToNanoVDBns::UserFieldInformation<intType_ta, real_tt>&
}

}
} // namespace ioToNanoVDBns
} // namespace ioToHDF5ns

template <typename intType_ta = int, class real_tt = double>
struct ioToNanoVDB
struct ioToHDF5
{
ioToNanoVDB(const std::string& filename /*! File name */,
ioToHDF5(const std::string& filename /*! File name */,
const Neon::Integer_3d<intType_ta>& dim /*! IoDense dimension of the field */,
const std::function<real_tt(const Neon::Integer_3d<intType_ta>&, int componentIdx)>& fun /*! Implicit defintion of the user field */,
const nComponent_t card /*! Field cardinality */,
const double scalingData = 1.0 /*! Spacing, i.e. size of a voxel */,
const Neon::Integer_3d<intType_ta>& origin = Neon::Integer_3d<intType_ta>(0, 0, 0) /*! Minimum Corner && Origin */,
const Neon::Integer_3d<intType_ta>& chunking = Neon::Integer_3d<intType_ta>(10, 10, 10) /*1 Chunking size of the output file */,
const Neon::ioToNanoVDBns::UserFieldAccessMask<real_tt, intType_ta> mask = [](const Neon::index_3d& idx){return (idx.x == idx.x) ? true: false;}) /*! Used for sparce matrices; returns true for indices that should be stored in vdb output */
const Neon::ioToHDF5ns::UserFieldAccessMask<real_tt, intType_ta> mask = [](const Neon::index_3d& idx){return (idx.x == idx.x) ? true: false;}) /*! Used for sparce matrices; returns true for indices that should be included in the output */
: m_filename(filename),
m_dim(dim),
m_scalingData(scalingData),
m_origin(origin),
m_field(ioToNanoVDBns::UserFieldInformation<intType_ta, real_tt>(fun, card)),
m_field(ioToHDF5ns::UserFieldInformation<intType_ta, real_tt>(fun, card)),
m_chunking(chunking),
m_mask(mask)
{
std::ofstream out("metadata2");
out << "dim: " << m_dim.x << " " << m_dim.y << " " << m_dim.z << std::endl;
}

virtual ~ioToNanoVDB()
virtual ~ioToHDF5()
{
}

Expand All @@ -152,8 +154,8 @@ struct ioToNanoVDB
std::string s = ss.str();
filename = m_filename + s;
}
filename = filename + ".nvdb";
ioToNanoVDBns::ioToNanoVDB<intType_ta, real_tt>(m_field,
filename = filename + ".h5";
ioToHDF5ns::ioToHDF5<intType_ta, real_tt>(m_field,
m_mask,
filename,
m_dim,
Expand All @@ -176,13 +178,13 @@ struct ioToNanoVDB


private:
std::string m_filename /*! File name */;
Neon::Integer_3d<intType_ta> m_dim /*! IoDense dimension of the field */;
double m_scalingData = 1.0 /*! Spacing, i.e. size of a voxel */;
Neon::Integer_3d<intType_ta> m_origin = Neon::Integer_3d<intType_ta>(0, 0, 0) /*! Origin */;
Neon::Integer_3d<intType_ta> m_chunking = Neon::Integer_3d<intType_ta>(10, 10, 10) /*! Chunking */;
ioToNanoVDBns::UserFieldInformation<intType_ta, real_tt> m_field /*! Field data*/;
ioToNanoVDBns::UserFieldAccessMask<real_tt, intType_ta> m_mask;
std::string m_filename /*! File name */;
Neon::Integer_3d<intType_ta> m_dim /*! IoDense dimension of the field */;
double m_scalingData = 1.0 /*! Spacing, i.e. size of a voxel */;
Neon::Integer_3d<intType_ta> m_origin = Neon::Integer_3d<intType_ta>(0, 0, 0) /*! Origin */;
Neon::Integer_3d<intType_ta> m_chunking = Neon::Integer_3d<intType_ta>(10, 10, 10) /*! Chunking */;
ioToHDF5ns::UserFieldInformation<intType_ta, real_tt> m_field /*! Field data*/;
ioToHDF5ns::UserFieldAccessMask<real_tt, intType_ta> m_mask;
int m_iteration = -1;
};

Expand Down
12 changes: 8 additions & 4 deletions libNeonDomain/include/Neon/domain/interface/FieldBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,16 @@ class FieldBase
bool isNodeSpace = false) const -> void;

template <typename NanoVDBExportType = T>
auto ioToNanoVDB(const std::string& fileName,
bool isNodeSpace = false) const -> void;
auto ioToNanoVDB(const std::string& fileName) const -> void;

template <typename NanoVDBExportType = T>
auto ioDomainToNanoVDB(const std::string& fileName,
bool isNodeSpace = false) const -> void;
auto ioDomainToNanoVDB(const std::string& fileName) const -> void;

template <typename NanoVDBExportType = T>
auto ioToHDF5(const std::string& fileName) const -> void;

template <typename NanoVDBExportType = T>
auto ioDomainToHDF5(const std::string& fileName) const -> void;


private:
Expand Down
44 changes: 40 additions & 4 deletions libNeonDomain/include/Neon/domain/interface/FieldBase_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Neon/domain/interface/FieldBase.h"
#include "Neon/domain/tools/IOGridVTK.h"
#include "Neon/core/tools/io/ioToNanoVDB.h"
#include "Neon/core/tools/io/ioToHDF5.h"

namespace Neon::domain::interface {

Expand Down Expand Up @@ -321,8 +322,7 @@ auto FieldBase<T, C>::ioToVtk(const std::string& fileName,

template <typename T, int C>
template <typename NanoVDBExportType>
auto FieldBase<T, C>::ioToNanoVDB(const std::string& fileName,
bool isNodeSpace) const -> void
auto FieldBase<T, C>::ioToNanoVDB(const std::string& fileName) const -> void
{
Neon::ioToNanoVDB<int, NanoVDBExportType> io(fileName,
this->getDimension(),
Expand All @@ -340,8 +340,7 @@ auto FieldBase<T, C>::ioToNanoVDB(const std::string& fileName,

template <typename T, int C>
template <typename NanoVDBExportType>
auto FieldBase<T, C>::ioDomainToNanoVDB(const std::string& fileName,
bool isNodeSpace) const -> void
auto FieldBase<T, C>::ioDomainToNanoVDB(const std::string& fileName) const -> void
{
Neon::ioToNanoVDB<int32_t, NanoVDBExportType> io(fileName,
this->getDimension(),
Expand All @@ -358,6 +357,43 @@ auto FieldBase<T, C>::ioDomainToNanoVDB(const std::string& fileName,
return;
}

template <typename T, int C>
template <typename HDF5ExportType>
auto FieldBase<T, C>::ioToHDF5(const std::string& fileName) const -> void
{
Neon::ioToHDF5<int, HDF5ExportType> io(fileName,
this->getDimension(),
[&](Neon::Integer_3d<int> idx, int card) -> HDF5ExportType {
return (*this)(idx, card);
},
this->getCardinality(),
1.0,
Neon::Integer_3d<int>(0, 0, 0));


io.flush();
return;
}

template <typename T, int C>
template <typename HDF5ExportType>
auto FieldBase<T, C>::ioDomainToHDF5(const std::string& fileName) const -> void
{
Neon::ioToHDF5<int32_t, HDF5ExportType> io(fileName,
this->getDimension(),
[&](const Neon::index_3d& idx, int) {
HDF5ExportType setIdx = HDF5ExportType(getBaseGridTool().getSetIdx(idx));
return setIdx;
},
1,
1.0,
Neon::Integer_3d<int>(0, 0, 0));


io.flush();
return;
}

template <typename T, int C>
auto FieldBase<T, C>::getClassName() const -> const std::string&
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class GridBaseTemplate : public GridBase
* Exporting the domain active voxel to nanoVDB
*/
auto ioDomainToNanoVDB(const std::string& fileName) const -> void;

/**
* Exporting the domain active voxel to HDF5
*/
auto ioDomainToHDF5(const std::string& fileName) const -> void;
};
} // namespace Neon::domain::interface

Expand Down
33 changes: 33 additions & 0 deletions libNeonDomain/include/Neon/domain/interface/GridBaseTemplate_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Neon/core/tools/io/ioToVTK.h"
#include "Neon/core/tools/io/ioToNanoVDB.h"
#include "Neon/core/tools/io/ioToHDF5.h"

namespace Neon::domain::interface {

Expand Down Expand Up @@ -65,4 +66,36 @@ auto GridBaseTemplate<GridT, CellT>::ioDomainToNanoVDB(const std::string& fileNa
io2.flush();
return;
}

template <typename GridT, typename CellT>
auto GridBaseTemplate<GridT, CellT>::ioDomainToHDF5(const std::string& fileName) const -> void
{
ioToHDF5<int, float> io1(fileName + "_domain",
this->getDimension(),
[&](const Neon::index_3d& idx, int) {
bool isActiveVox = isInsideDomain(idx);
return isActiveVox;
},
1,
1.0,
Neon::Integer_3d<int>(0, 0, 0));

ioToHDF5<int, float> io2(fileName + "_partition",
this->getDimension(),
[&](const Neon::index_3d& idx, int) {
const auto& cellProperties = this->getProperties(idx);
if (!cellProperties.isInside()) {
return -1;
}
auto setIdx = cellProperties.getSetIdx();
return setIdx.idx();
},
1,
1.0,
Neon::Integer_3d<int>(0, 0, 0));

io1.flush();
io2.flush();
return;
}
} // namespace Neon::domain::interface

0 comments on commit 9bc6e61

Please sign in to comment.