79static std::unordered_map<std::string, H5MDOutputFields>
const fields_map = {
97 for (
auto const &field_name : fields) {
99 throw std::invalid_argument(
"Unknown field '" + field_name +
"'");
107 std::filesystem::path
const &to) {
112 auto constexpr option_fail_if_exists = std::filesystem::copy_options::none;
114 std::filesystem::copy_file(from, to, option_fail_if_exists);
115 }
catch (std::filesystem::filesystem_error
const &) {
120template <
typename extent_type>
122 extent_type
const &change_extent) {
123 auto extents = dataset.getSpace().getDimensions();
124 auto const rank = extents.size();
126 for (
auto i = 0u; i < rank; i++) {
127 extents[i] += change_extent[i];
129 dataset.resize(extents);
132template <
typename value_type,
typename extent_type>
133static void write_dataset(value_type
const &data, HighFive::DataSet &dataset,
134 extent_type
const &offset, extent_type
const &count) {
135 auto xfer_props = HighFive::DataTransferProps{};
136 xfer_props.add(HighFive::UseCollectiveIO{});
138 dataset.select(offset, count).write(data, xfer_props);
141template <
typename value_type,
typename extent_type>
142static void write_dataset(value_type
const &data, HighFive::DataSet &dataset,
143 extent_type
const &change_extent,
144 extent_type
const &offset, extent_type
const &count) {
151 std::filesystem::path
const &script_path) {
152 if (!script_path.empty()) {
153 std::ifstream scriptfile(script_path);
154 std::string buffer(std::istreambuf_iterator<char>(scriptfile),
155 std::istreambuf_iterator<char>{});
156 h5md_file.createGroup(
"/parameters");
157 auto group = h5md_file.createGroup(
"/parameters/files");
158 group.createAttribute(
"script", buffer);
163void File::init_file() {
164 auto const file_exists = std::filesystem::exists(m_file_path);
165 auto const backup_file_exists = std::filesystem::exists(m_backup_path);
177 if (m_comm.rank() == 0)
181 throw incompatible_h5mdfile();
184 if (backup_file_exists)
185 throw left_backupfile();
190void File::load_datasets() {
191 auto &datasets = m_datasets;
192 for (
auto const &ds : m_h5md_specification.get_datasets()) {
195 auto path = ds.path();
196 datasets[path] = m_h5md_file->getDataSet(path);
200void File::create_groups() {
201 for (
auto const &ds : m_h5md_specification.get_datasets()) {
202 std::stringstream ss(ds.group);
204 std::string current_path =
"/";
205 while (std::getline(ss, segment,
'/')) {
208 current_path +=
"/" + segment;
209 if (!m_h5md_file->exist(current_path)) {
210 m_h5md_file->createGroup(current_path);
216static std::vector<std::size_t>
create_dims(hsize_t rank, hsize_t data_dim) {
218 return {0ul, 0ul, data_dim};
221 return {0ul, data_dim};
230 return {max_dim, max_dim, data_dim};
233 return {max_dim, max_dim};
241 auto const chunk_size = (rank > 1ul) ? size : hsize_t{1ul};
243 return {1ul, chunk_size, data_dim};
246 return {1ul, chunk_size};
252void File::create_datasets() {
253 auto &datasets = m_datasets;
254 for (
auto const &ds : m_h5md_specification.get_datasets()) {
258 auto maxdims =
create_maxdims(ds.rank, ds.data_dim, H5S_UNLIMITED);
259 auto dataspace = HighFive::DataSpace(dims, maxdims);
260 auto const chunk_size =
static_cast<hsize_t
>(m_chunk_size);
262 HighFive::DataSetCreateProps props;
263 props.add(HighFive::Chunking(chunk));
264 auto path = ds.path();
265 if (ds.type == H5T_NATIVE_INT) {
266 datasets.emplace(path,
267 m_h5md_file->createDataSet<
int>(path, dataspace, props));
268 }
else if (ds.type == H5T_NATIVE_DOUBLE) {
270 path, m_h5md_file->createDataSet<
double>(path, dataspace, props));
275void File::load_file() {
276 HighFive::FileAccessProps fapl;
277 fapl.add(HighFive::MPIOFileAccess{m_comm, MPI_INFO_NULL});
278 fapl.add(HighFive::MPIOCollectiveMetadata{});
279 m_h5md_file = std::make_unique<HighFive::File>(
280 m_file_path.string(), HighFive::File::ReadWrite, fapl);
285 auto h5md_group = h5md_file.createGroup(
"h5md");
286 auto att = h5md_group.createAttribute<std::size_t>(
288 HighFive::DataSpace::From(std::array<std::size_t, 2>{{1ul, 1ul}}));
289 att.write(std::array<std::size_t, 2>{{1ul, 1ul}});
290 auto h5md_creator_group = h5md_group.createGroup(
"creator");
291 h5md_creator_group.createAttribute(
"name",
"ESPResSo");
292 h5md_creator_group.createAttribute(
"version", ESPRESSO_VERSION);
293 auto h5md_author_group = h5md_group.createGroup(
"author");
294 h5md_author_group.createAttribute(
"name",
"N/A");
295 auto box_path =
"/particles/atoms/box";
296 if (h5md_file.exist(box_path)) {
297 auto group = h5md_file.getGroup(box_path);
298 group.createAttribute(
"dimension", 3);
299 group.createAttribute(
"boundary",
"periodic");
303void File::write_units() {
304 auto &datasets = m_datasets;
306 datasets.at(
"/particles/atoms/mass/value")
310 datasets.at(
"/particles/atoms/charge/value")
314 datasets.at(
"/particles/atoms/position/value")
316 datasets.at(
"/particles/atoms/box/edges/value")
320 datasets.at(
"/particles/atoms/lees_edwards/offset/value")
324 datasets.at(
"/particles/atoms/velocity/value")
328 datasets.at(
"/particles/atoms/force/value")
332 datasets.at(
"/particles/atoms/id/time")
337void File::create_hard_links() {
338 std::string path_step =
"/particles/atoms/id/step";
339 std::string path_time =
"/particles/atoms/id/time";
340 for (
auto &ds : m_h5md_specification.get_datasets()) {
342 char const *from =
nullptr;
343 if (ds.name ==
"step") {
344 from = path_step.c_str();
345 }
else if (ds.name ==
"time") {
346 from = path_time.c_str();
348 assert(from !=
nullptr);
349 if (H5Lcreate_hard(m_h5md_file->getId(), from, m_h5md_file->getId(),
350 ds.path().c_str(), H5P_DEFAULT, H5P_DEFAULT) < 0) {
351 throw std::runtime_error(
"Error creating hard link for " + ds.path());
357void File::create_file() {
358 HighFive::FileAccessProps fapl;
359 fapl.add(HighFive::MPIOFileAccess{m_comm, MPI_INFO_NULL});
360 fapl.add(HighFive::MPIOCollectiveMetadata{});
361 m_h5md_file = std::make_unique<HighFive::File>(m_file_path.string(),
362 HighFive::File::Create, fapl);
372 if (m_comm.rank() == 0) {
373 std::filesystem::remove(m_backup_path);
379template <std::
size_t rank>
struct slice_info {};
381template <>
struct slice_info<3> {
382 static auto extent(hsize_t n_part_diff) {
383 return Vector3s{1ul, n_part_diff, 0ul};
385 static constexpr auto count(std::size_t local_n_part) {
386 return Vector3s{1ul, local_n_part, 3ul};
388 static auto offset(hsize_t n_time_steps, hsize_t prefix) {
389 return Vector3s{n_time_steps, prefix, 0ul};
391 template <
typename T>
392 static boost::multi_array<T, 3> reshape(std::vector<T>
const &v1d,
395 boost::multi_array<T, 3> data(boost::extents[0][0][0]);
398 auto const rows = count[1];
399 auto const cols = count[2];
401 boost::multi_array<T, 3> data(
402 boost::extents[1][
static_cast<long>(rows)][
static_cast<long>(cols)]);
404 for (std::size_t i = 0; i < rows; ++i) {
405 for (std::size_t j = 0; j < cols; ++j) {
406 data[0][i][j] = v1d[cols * i + j];
414template <>
struct slice_info<2> {
415 static auto extent(hsize_t n_part_diff) {
return Vector2s{1ul, n_part_diff}; }
416 static constexpr auto count(std::size_t local_n) {
419 static auto offset(hsize_t n_time_steps, hsize_t prefix) {
420 return Vector2s{n_time_steps, prefix};
422 template <
typename T>
423 static boost::multi_array<T, 2> reshape(std::vector<T>
const &v1d,
426 boost::multi_array<T, 2> data(boost::extents[0][0]);
429 auto const cols = count[1];
431 boost::multi_array<T, 2> data(boost::extents[1][
static_cast<long>(cols)]);
433 for (std::size_t i = 0; i < cols; ++i) {
441template <
typename T>
struct get_buffer_traits {};
444 requires std::is_arithmetic_v<T>
445struct get_buffer_traits<T> {
447 constexpr static std::size_t dim = 1ul;
450template <
typename T, std::
size_t N>
451 requires std::is_arithmetic_v<T>
452struct get_buffer_traits<
Utils::Vector<T, N>> {
454 constexpr static std::size_t dim = N;
457template <
typename Functor>
class ParticleDataSerializer {
458 using RetVal = std::decay_t<std::invoke_result_t<Functor, Particle const &>>;
461 template <
typename T>
462 requires std::is_arithmetic_v<T>
463 void serialize(
auto &buffer, T
const &value)
const {
464 buffer.emplace_back(value);
467 template <
typename T, std::
size_t N>
469 buffer.insert(buffer.end(), value.
cbegin(), value.
cend());
473 explicit ParticleDataSerializer(Functor lambda) : m_getter{lambda} {}
476 auto constexpr value_dim = get_buffer_traits<RetVal>::dim;
477 std::vector<typename get_buffer_traits<RetVal>::type> buffer{};
486template <
typename Functor>
auto make_serializer(Functor lambda) {
487 return ParticleDataSerializer<Functor>{lambda};
489template <
typename RetVal>
490auto make_serializer(RetVal (
Particle::*getter)() const) {
491 auto kernel = [getter](
Particle const &p) -> RetVal {
return (p.*getter)(); };
492 return ParticleDataSerializer<
decltype(kernel)>{std::move(kernel)};
497template <std::
size_t dim,
typename Serializer>
500 HighFive::DataSet &dataset,
501 Serializer serializer) {
502 auto const n_part_local =
static_cast<hsize_t
>(particles.
size());
503 auto const old_extents = dataset.getSpace().getDimensions();
504 auto const extent_n_part =
505 std::max(n_part_global,
static_cast<hsize_t
>(old_extents[1])) -
507 extend_dataset(dataset, detail::slice_info<dim>::extent(extent_n_part));
508 auto const count = detail::slice_info<dim>::count(n_part_local);
509 auto const offset = detail::slice_info<dim>::offset(old_extents[0], prefix);
510 HighFive::DataType dtype = dataset.getDataType();
511 auto buffer = serializer(particles);
512 write_dataset(detail::slice_info<dim>::reshape(buffer, count), dataset,
517 auto const extents = dataset.getSpace().getDimensions();
519 Vector2s const offset{extents[0], 0ul};
522 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
527 HighFive::DataSet &dataset) {
528 auto const extents = dataset.getSpace().getDimensions();
530 Vector2s const offset{extents[0], 0ul};
532 auto const data = std::vector<double>{lebc.
pos_offset};
533 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
538 HighFive::DataSet &dataset) {
540 auto const extents = dataset.getSpace().getDimensions();
542 Vector2s const offset{extents[0], 0ul};
544 auto const data = std::vector<int>{shear_direction};
545 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
550 HighFive::DataSet &dataset) {
552 auto const extents = dataset.getSpace().getDimensions();
554 Vector2s const offset{extents[0], 0ul};
556 auto const data = std::vector<int>{shear_plane_normal};
557 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
563 auto &datasets = m_datasets;
565 write_box(box_geo, datasets.at(
"/particles/atoms/box/edges/value"));
570 datasets.at(
"/particles/atoms/lees_edwards/offset/value"));
574 datasets.at(
"/particles/atoms/lees_edwards/direction/value"));
578 datasets.at(
"/particles/atoms/lees_edwards/normal/value"));
582 static_assert(
sizeof(hsize_t) == 8ul);
583 auto const n_part_local =
static_cast<hsize_t
>(particles.
size());
585 BOOST_MPI_CHECK_RESULT(
586 MPI_Exscan, (&n_part_local, &prefix, 1, MPI_UINT64_T, MPI_SUM, m_comm));
587 auto const n_part_global =
588 boost::mpi::all_reduce(m_comm, n_part_local, std::plus<hsize_t>());
590 write_td_particle_property<2>(prefix, n_part_global, particles,
591 datasets.at(
"/particles/atoms/id/value"),
595 HighFive::DataSet &dataset = datasets.at(
"/particles/atoms/id/value");
596 auto const extents = dataset.getSpace().getDimensions();
598 datasets.at(
"/particles/atoms/id/time"),
Vector1s{1},
601 datasets.at(
"/particles/atoms/id/step"),
Vector1s{1},
606 write_td_particle_property<2>(prefix, n_part_global, particles,
607 datasets.at(
"/particles/atoms/species/value"),
611 write_td_particle_property<2>(prefix, n_part_global, particles,
612 datasets.at(
"/particles/atoms/mass/value"),
616 write_td_particle_property<3>(
617 prefix, n_part_global, particles,
618 datasets.at(
"/particles/atoms/position/value"),
619 detail::make_serializer([&](
Particle const &p) {
620 return box_geo.folded_position(p.pos());
624 write_td_particle_property<3>(
625 prefix, n_part_global, particles,
626 datasets.at(
"/particles/atoms/image/value"),
627 detail::make_serializer([&](
Particle const &p) {
628 return box_geo.folded_image_box(p.pos(), p.image_box());
632 write_td_particle_property<3>(
633 prefix, n_part_global, particles,
634 datasets.at(
"/particles/atoms/velocity/value"),
638 write_td_particle_property<3>(prefix, n_part_global, particles,
639 datasets.at(
"/particles/atoms/force/value"),
643 write_td_particle_property<2>(prefix, n_part_global, particles,
644 datasets.at(
"/particles/atoms/charge/value"),
648 write_connectivity(particles);
652void File::write_connectivity(
const ParticleRange &particles) {
654 for (
auto const &p : particles) {
655 auto nbonds_local =
static_cast<decltype(bond)::index
>(bond.shape()[1]);
656 for (
auto const b : p.bonds()) {
657 auto const &partner_ids = b.partner_ids();
658 if (partner_ids.size() == 1u) {
659 bond.resize(boost::extents[1][nbonds_local + 1][2]);
660 bond[0][nbonds_local][0] = p.id();
661 bond[0][nbonds_local][1] = partner_ids[0];
667 auto const n_bonds_local =
static_cast<int>(bond.shape()[1]);
668 auto &datasets = m_datasets;
669 int prefix_bonds = 0;
670 BOOST_MPI_CHECK_RESULT(
671 MPI_Exscan, (&n_bonds_local, &prefix_bonds, 1, MPI_INT, MPI_SUM, m_comm));
672 auto const n_bonds_total =
673 boost::mpi::all_reduce(m_comm, n_bonds_local, std::plus<int>());
675 datasets.at(
"/connectivity/atoms/value").getSpace().getDimensions();
676 Vector3s offset_bonds = {extents[0],
static_cast<std::size_t
>(prefix_bonds),
678 Vector3s count_bonds = {1,
static_cast<std::size_t
>(n_bonds_local), 2};
679 auto const n_bond_diff = std::max(
static_cast<hsize_t
>(n_bonds_total),
680 static_cast<hsize_t
>(extents[1])) -
682 Vector3s change_extent_bonds = {1,
static_cast<std::size_t
>(n_bond_diff), 0};
683 write_dataset(bond, datasets.at(
"/connectivity/atoms/value"),
684 change_extent_bonds, offset_bonds, count_bonds);
690 auto const view = std::views::elements<0>(
fields_map);
691 return {view.begin(), view.end()};
694File::File(std::filesystem::path file_path, std::filesystem::path script_path,
695 std::vector<std::string>
const &output_fields, std::string mass_unit,
696 std::string length_unit, std::string time_unit,
697 std::string force_unit, std::string velocity_unit,
698 std::string charge_unit,
int chunk_size)
699 : m_file_path(
std::move(file_path)), m_backup_path(m_file_path),
700 m_script_path(
std::move(script_path)),
701 m_absolute_script_path(
702 m_script_path.empty()
703 ?
std::filesystem::path()
704 :
std::filesystem::weakly_canonical(m_script_path)),
705 m_mass_unit(
std::move(mass_unit)), m_length_unit(
std::move(length_unit)),
706 m_time_unit(
std::move(time_unit)), m_force_unit(
std::move(force_unit)),
707 m_velocity_unit(
std::move(velocity_unit)),
708 m_charge_unit(
std::move(charge_unit)), m_chunk_size(chunk_size),
711 m_h5md_specification(m_fields) {
713 throw std::domain_error(
"Parameter 'chunk_size' must be > 0");
715 m_backup_path +=
".bak";