Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included facultative h5 in rc_prior #541

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions tools/cmdata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)

project(cmdata VERSION 0.1
DESCRIPTION "A programm to calculate contact data from gromacs trajectories for multi-eGO"
LANGUAGES CXX)
LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17)
set(CMDATA cmdata)
Expand All @@ -25,10 +25,16 @@ endif()

# find gromacs
find_package(GROMACS REQUIRED NAMES gromacs gromacs_mpi gromacs_d gromacs_mpi_d HINTS "$ENV{GROMACS_DIR}")
find_package(HDF5 REQUIRED COMPONENTS C CXX)
find_package(HDF5 COMPONENTS C CXX)
if(HDF5_FOUND)
message(STATUS "HDF5 found, enabling HDF5 support")
add_definitions(-DUSE_HDF5)
target_include_directories(${CMDATA} PRIVATE ${HDF5_INCLUDE_DIRS})
target_link_libraries(${CMDATA} PRIVATE ${HDF5_CXX_LIBRARIES})
endif()

# include source and header files
target_include_directories(${CMDATA} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src ${HDF5_INCLUDE_DIRS})
target_include_directories(${CMDATA} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

include(FetchContent)
SET(FETCHCONTENT_QUIET OFF)
Expand All @@ -51,7 +57,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(xdrfile)

# link libraries
target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs xdrfile popt ${HDF5_CXX_LIBRARIES})
target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs xdrfile popt)

set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
install(TARGETS ${CMDATA} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
Expand Down
9 changes: 8 additions & 1 deletion tools/cmdata/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ int main(int argc, const char** argv)
{"mode", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &p_mode, 0, "Mode of operation", "STRING"},
{"weights", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &p_weights_path, 0, "Weights file", "FILE"},
{"no_pbc", '\0', POPT_ARG_NONE | POPT_ARGFLAG_OPTIONAL, &p_nopbc, 0, "Ignore pbcs", 0},
#ifdef USE_HDF5
{"h5", '\0', POPT_ARG_NONE | POPT_ARGFLAG_OPTIONAL, &p_h5 , 0, "Write output in HDF5 format", 0},
#endif
POPT_TABLEEND
};

Expand All @@ -73,7 +75,9 @@ int main(int argc, const char** argv)
if ( p_weights_path != NULL ) weights_path = std::string(p_weights_path);
if ( p_out_prefix != NULL ) out_prefix = std::string(p_out_prefix);
if ( p_nopbc != NULL ) nopbc = true;
#ifdef USE_HDF5
if ( p_h5 != NULL ) h5 = true;
#endif

// check if paths are valid
if ( !std::filesystem::exists(std::filesystem::path(traj_path)) )
Expand Down Expand Up @@ -155,7 +159,10 @@ int main(int argc, const char** argv)

cmdata::CMData cmdata(
top_path, traj_path, cutoff, mol_cutoff, nskip, num_threads, mol_threads, dt,
mode, weights_path, nopbc, t_begin, t_end, h5
mode, weights_path, nopbc, t_begin, t_end
#ifdef USE_HDF5
, h5
#endif
);
cmdata.run();
cmdata.process_data();
Expand Down
36 changes: 28 additions & 8 deletions tools/cmdata/src/cmdata/cmdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ class CMData
// mode selection, booleans and functions
std::string mode_;
bool intra_ = false, same_ = false, cross_ = false;
#ifdef USE_HDF5
bool h5_ = false;
#endif

// function types
using ftype_intra_ = cmdata::ftypes::function_traits<decltype(&cmdata::density::intra_mol_routine)>;
Expand Down Expand Up @@ -235,12 +237,18 @@ class CMData
public:
CMData(
const std::string &top_path, const std::string &traj_path,
float cutoff, float mol_cutoff, int nskip, int num_threads, int num_mol_threads,
double cutoff, double mol_cutoff, int nskip, int num_threads, int num_mol_threads,
int dt, const std::string &mode, const std::string &weights_path,
bool no_pbc, float t_begin, float t_end, bool h5
bool no_pbc, float t_begin, float t_end
#ifdef USE_HDF5
, bool h5
#endif
) : cutoff_(cutoff), mol_cutoff_(mol_cutoff), nskip_(nskip), num_threads_(num_threads), num_mol_threads_(num_mol_threads),
mode_(mode), weights_path_(weights_path),
no_pbc_(no_pbc), dt_(dt), t_begin_(t_begin), t_end_(t_end), h5_(h5)
no_pbc_(no_pbc), dt_(dt), t_begin_(t_begin), t_end_(t_end)
#ifdef USE_HDF5
,h5_(h5)
#endif
{
bool bTop_;
matrix boxtop_;
Expand Down Expand Up @@ -756,27 +764,39 @@ class CMData
void write_output( const std::string &output_prefix )
{
std::cout << "Writing data... " << std::endl;
using ftype_write_intra = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_intra_HDF5)>;
using ftype_write_inter_same = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_inter_same_HDF5)>;
using ftype_write_inter_cross = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_inter_cross_HDF5)>;
using ftype_write_intra = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_intra)>;
using ftype_write_inter_same = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_inter_same)>;
using ftype_write_inter_cross = cmdata::ftypes::function_traits<decltype(&cmdata::io::f_write_inter_cross)>;
std::function<ftype_write_intra::signature> write_intra = cmdata::ftypes::do_nothing<ftype_write_intra>();
std::function<ftype_write_inter_same::signature> write_inter_same = cmdata::ftypes::do_nothing<ftype_write_inter_same>();
std::function<ftype_write_inter_cross::signature> write_inter_cross = cmdata::ftypes::do_nothing<ftype_write_inter_cross>();

if (intra_)
{
#ifdef USE_HDF5
if(h5_) write_intra = cmdata::io::f_write_intra_HDF5;
else write_intra = cmdata::io::f_write_intra;
else write_intra = cmdata::io::f_write_intra;
#else
write_intra = cmdata::io::f_write_intra;
#endif
}
if (same_)
{
#ifdef USE_HDF5
if(h5_) write_inter_same = cmdata::io::f_write_inter_same_HDF5;
else write_inter_same = cmdata::io::f_write_inter_same;
#else
write_inter_same = cmdata::io::f_write_inter_same;
#endif
}
if (cross_)
{
#ifdef USE_HDF5
if(h5_) write_inter_cross = cmdata::io::f_write_inter_cross_HDF5;
else write_inter_cross = cmdata::io::f_write_inter_cross;
else write_inter_cross = cmdata::io::f_write_inter_cross;
#else
write_inter_cross = cmdata::io::f_write_inter_cross;
#endif
}

for (std::size_t i = 0; i < natmol2_.size(); i++)
Expand Down
38 changes: 35 additions & 3 deletions tools/cmdata/src/cmdata/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <gromacs/trajectoryanalysis/topologyinformation.h>
#include <gromacs/fileio/tpxio.h>

#ifdef USE_HDF5
#include <H5Cpp.h>
#endif

#include <filesystem>
#include <string>
Expand All @@ -18,7 +20,9 @@

#define COUT_FLOAT_PREC6 std::fixed << std::setprecision(6)

#ifdef USE_HDF5
using namespace H5;
#endif

static inline void mtopGetMolblockIndex(const gmx_mtop_t& mtop,
int globalAtomIndex,
Expand Down Expand Up @@ -176,6 +180,7 @@ std::vector<float> read_weights_file( const std::string &path )
// return mols;
// }

#ifdef USE_HDF5
void f_write_intra_HDF5(const std::string &output_prefix,
std::size_t i, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
const std::vector<std::vector<std::vector<std::vector<float>>>> &intram_mat_density
Expand All @@ -192,7 +197,11 @@ void f_write_intra_HDF5(const std::string &output_prefix,
// Create dataset creation property list with compression
DSetCreatPropList plist;
plist.setDeflate(6); // Set compression level (0-9, where 9 is maximum compression)
hsize_t chunk_dims[2] = {300, 512}; // Adjust chunk size based on your data
// hsize_t chunk_dims[2] = {300, 512}; // Adjust chunk size based on your data
hsize_t chunk_dims[2] = {
std::min(static_cast<hsize_t>(density_bins.size()), static_cast<hsize_t>(300)),
std::min(static_cast<hsize_t>(natmol2[i] + 1), static_cast<hsize_t>(512))
};
plist.setChunk(2, chunk_dims);

// Create a dataset with compression
Expand All @@ -211,6 +220,7 @@ void f_write_intra_HDF5(const std::string &output_prefix,
// Write the data to the dataset
dataset.write(flat_data.data(), PredType::NATIVE_FLOAT);
}
#endif

void f_write_intra(const std::string &output_prefix,
std::size_t i, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
Expand All @@ -232,6 +242,7 @@ void f_write_intra(const std::string &output_prefix,
fp_intra.close();
}

#ifdef USE_HDF5
void f_write_inter_same_HDF5(const std::string &output_prefix,
std::size_t i, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
const std::vector<std::vector<std::vector<std::vector<float>>>> &interm_same_mat_density,
Expand All @@ -251,7 +262,21 @@ void f_write_inter_same_HDF5(const std::string &output_prefix,
// Create dataset creation property list with compression
DSetCreatPropList plist;
plist.setDeflate(6); // Set compression level (0-9, where 9 is maximum compression)
hsize_t chunk_dims[2] = {300, 512}; // Adjust chunk size based on your data
// hsize_t chunk_dims[2] = {
// (static_cast<hsize_t>(density_bins.size()) < 300) ? 300 : static_cast<hsize_t>(density_bins.size()),
// (static_cast<hsize_t>(natmol2[i]+1) < 512) ? 512 : static_cast<hsize_t>(natmol2[i]+1)
// }; // Adjust chunk size based on your data
hsize_t chunk_dims[2] = {
std::min(static_cast<hsize_t>(density_bins.size()), static_cast<hsize_t>(300)),
std::min(static_cast<hsize_t>(natmol2[i] + 1), static_cast<hsize_t>(512))
};
std::cout << "Dataset dimensions: ("
<< density_bins.size() << ", "
<< (natmol2[i] + 1) << ")" << std::endl;
std::cout << "Chunk dimensions: ("
<< chunk_dims[0] << ", "
<< chunk_dims[1] << ")" << std::endl;

plist.setChunk(2, chunk_dims);

// Create a dataset with compression
Expand All @@ -275,6 +300,7 @@ void f_write_inter_same_HDF5(const std::string &output_prefix,
dataset.write(flat_data.data(), PredType::NATIVE_FLOAT);
dataset_c.write(flat_data_c.data(), PredType::NATIVE_FLOAT);
}
#endif

void f_write_inter_same(const std::string &output_prefix,
std::size_t i, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
Expand Down Expand Up @@ -302,6 +328,7 @@ void f_write_inter_same(const std::string &output_prefix,
fp_inter_cum.close();
}

#ifdef USE_HDF5
void f_write_inter_cross_HDF5(const std::string &output_prefix,
std::size_t i, std::size_t j, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
const std::vector<std::vector<int>> &cross_index,
Expand All @@ -322,7 +349,11 @@ void f_write_inter_cross_HDF5(const std::string &output_prefix,
// Create dataset creation property list with compression
DSetCreatPropList plist;
plist.setDeflate(6); // Set compression level (0-9, where 9 is maximum compression)
hsize_t chunk_dims[2] = {300, 512}; // Adjust chunk size based on your data
// hsize_t chunk_dims[2] = {300, 512}; // Adjust chunk size based on your data
hsize_t chunk_dims[2] = {
std::min(static_cast<hsize_t>(density_bins.size()), static_cast<hsize_t>(300)),
std::min(static_cast<hsize_t>(natmol2[i] + 1), static_cast<hsize_t>(512))
};
plist.setChunk(2, chunk_dims);

// Create a dataset with compression
Expand All @@ -346,6 +377,7 @@ void f_write_inter_cross_HDF5(const std::string &output_prefix,
dataset.write(flat_data.data(), PredType::NATIVE_FLOAT);
dataset_c.write(flat_data_c.data(), PredType::NATIVE_FLOAT);
}
#endif

void f_write_inter_cross(const std::string &output_prefix,
std::size_t i, std::size_t j, int ii, const std::vector<float> &density_bins, const std::vector<int> &natmol2,
Expand Down
Loading