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);
378 if (m_comm.rank() == 0) {
379 std::filesystem::remove(m_backup_path);
385template <std::
size_t rank>
struct slice_info {};
387template <>
struct slice_info<3> {
388 static auto extent(hsize_t n_part_diff) {
389 return Vector3s{1ul, n_part_diff, 0ul};
391 static constexpr auto count(std::size_t local_n_part) {
392 return Vector3s{1ul, local_n_part, 3ul};
394 static auto offset(hsize_t n_time_steps, hsize_t prefix) {
395 return Vector3s{n_time_steps, prefix, 0ul};
397 template <
typename T>
398 static boost::multi_array<T, 3> reshape(std::vector<T>
const &v1d,
401 boost::multi_array<T, 3> data(boost::extents[0][0][0]);
404 auto const rows = count[1];
405 auto const cols = count[2];
407 boost::multi_array<T, 3> data(
408 boost::extents[1][
static_cast<long>(rows)][
static_cast<long>(cols)]);
410 for (std::size_t i = 0; i < rows; ++i) {
411 for (std::size_t j = 0; j < cols; ++j) {
412 data[0][i][j] = v1d[cols * i + j];
420template <>
struct slice_info<2> {
421 static auto extent(hsize_t n_part_diff) {
return Vector2s{1ul, n_part_diff}; }
422 static constexpr auto count(std::size_t local_n) {
425 static auto offset(hsize_t n_time_steps, hsize_t prefix) {
426 return Vector2s{n_time_steps, prefix};
428 template <
typename T>
429 static boost::multi_array<T, 2> reshape(std::vector<T>
const &v1d,
432 boost::multi_array<T, 2> data(boost::extents[0][0]);
435 auto const cols = count[1];
437 boost::multi_array<T, 2> data(boost::extents[1][
static_cast<long>(cols)]);
439 for (std::size_t i = 0; i < cols; ++i) {
447template <
typename T>
struct get_buffer_traits {};
450 requires std::is_arithmetic_v<T>
451struct get_buffer_traits<T> {
453 constexpr static std::size_t dim = 1ul;
456template <
typename T, std::
size_t N>
457 requires std::is_arithmetic_v<T>
458struct get_buffer_traits<
Utils::Vector<T, N>> {
460 constexpr static std::size_t dim = N;
463template <
typename Functor>
class ParticleDataSerializer {
464 using RetVal = std::decay_t<std::invoke_result_t<Functor, Particle const &>>;
467 template <
typename T>
468 requires std::is_arithmetic_v<T>
469 void serialize(
auto &buffer, T
const &value)
const {
470 buffer.emplace_back(value);
473 template <
typename T, std::
size_t N>
475 buffer.insert(buffer.end(), value.
cbegin(), value.
cend());
479 explicit ParticleDataSerializer(Functor lambda) : m_getter{lambda} {}
482 auto constexpr value_dim = get_buffer_traits<RetVal>::dim;
483 std::vector<typename get_buffer_traits<RetVal>::type> buffer{};
492template <
typename Functor>
auto make_serializer(Functor lambda) {
493 return ParticleDataSerializer<Functor>{lambda};
495template <
typename RetVal>
496auto make_serializer(RetVal (
Particle::*getter)() const) {
497 auto kernel = [getter](
Particle const &p) -> RetVal {
return (p.*getter)(); };
498 return ParticleDataSerializer<
decltype(kernel)>{std::move(kernel)};
503template <std::
size_t dim,
typename Serializer>
506 HighFive::DataSet &dataset,
507 Serializer serializer) {
508 auto const n_part_local =
static_cast<hsize_t
>(particles.
size());
509 auto const old_extents = dataset.getSpace().getDimensions();
510 auto const extent_n_part =
511 std::max(n_part_global,
static_cast<hsize_t
>(old_extents[1])) -
513 extend_dataset(dataset, detail::slice_info<dim>::extent(extent_n_part));
514 auto const count = detail::slice_info<dim>::count(n_part_local);
515 auto const offset = detail::slice_info<dim>::offset(old_extents[0], prefix);
516 HighFive::DataType dtype = dataset.getDataType();
517 auto buffer = serializer(particles);
518 write_dataset(detail::slice_info<dim>::reshape(buffer, count), dataset,
523 auto const extents = dataset.getSpace().getDimensions();
525 Vector2s const offset{extents[0], 0ul};
528 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
533 HighFive::DataSet &dataset) {
534 auto const extents = dataset.getSpace().getDimensions();
536 Vector2s const offset{extents[0], 0ul};
538 auto const data = std::vector<double>{lebc.
pos_offset};
539 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
544 HighFive::DataSet &dataset) {
546 auto const extents = dataset.getSpace().getDimensions();
548 Vector2s const offset{extents[0], 0ul};
550 auto const data = std::vector<int>{shear_direction};
551 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
556 HighFive::DataSet &dataset) {
558 auto const extents = dataset.getSpace().getDimensions();
560 Vector2s const offset{extents[0], 0ul};
562 auto const data = std::vector<int>{shear_plane_normal};
563 write_dataset(detail::slice_info<2>::reshape(data, count), dataset, offset,
569 auto &datasets = m_datasets;
571 write_box(box_geo, datasets.at(
"/particles/atoms/box/edges/value"));
576 datasets.at(
"/particles/atoms/lees_edwards/offset/value"));
580 datasets.at(
"/particles/atoms/lees_edwards/direction/value"));
584 datasets.at(
"/particles/atoms/lees_edwards/normal/value"));
588 static_assert(
sizeof(hsize_t) == 8ul);
589 auto const n_part_local =
static_cast<hsize_t
>(particles.
size());
591 BOOST_MPI_CHECK_RESULT(
592 MPI_Exscan, (&n_part_local, &prefix, 1, MPI_UINT64_T, MPI_SUM, m_comm));
593 auto const n_part_global =
594 boost::mpi::all_reduce(m_comm, n_part_local, std::plus<hsize_t>());
596 write_td_particle_property<2>(prefix, n_part_global, particles,
597 datasets.at(
"/particles/atoms/id/value"),
601 auto const time_ext =
602 datasets.at(
"/particles/atoms/id/time").getSpace().getDimensions()[0];
604 datasets.at(
"/particles/atoms/id/time"),
Vector1s{1},
606 auto const step_ext =
607 datasets.at(
"/particles/atoms/id/step").getSpace().getDimensions()[0];
609 datasets.at(
"/particles/atoms/id/step"),
Vector1s{1},
614 write_td_particle_property<2>(prefix, n_part_global, particles,
615 datasets.at(
"/particles/atoms/species/value"),
619 write_td_particle_property<2>(prefix, n_part_global, particles,
620 datasets.at(
"/particles/atoms/mass/value"),
624 write_td_particle_property<3>(
625 prefix, n_part_global, particles,
626 datasets.at(
"/particles/atoms/position/value"),
627 detail::make_serializer([&](
Particle const &p) {
628 return box_geo.folded_position(p.pos());
632 write_td_particle_property<3>(
633 prefix, n_part_global, particles,
634 datasets.at(
"/particles/atoms/image/value"),
635 detail::make_serializer([&](
Particle const &p) {
636 return box_geo.folded_image_box(p.pos(), p.image_box());
640 write_td_particle_property<3>(
641 prefix, n_part_global, particles,
642 datasets.at(
"/particles/atoms/velocity/value"),
646 write_td_particle_property<3>(prefix, n_part_global, particles,
647 datasets.at(
"/particles/atoms/force/value"),
651 write_td_particle_property<2>(prefix, n_part_global, particles,
652 datasets.at(
"/particles/atoms/charge/value"),
656 write_connectivity(particles);
660void File::write_connectivity(
const ParticleRange &particles) {
662 for (
auto const &p : particles) {
663 auto nbonds_local =
static_cast<decltype(bond)::index
>(bond.shape()[1]);
664 for (
auto const b : p.bonds()) {
665 auto const &partner_ids = b.partner_ids();
666 if (partner_ids.size() == 1u) {
667 bond.resize(boost::extents[1][nbonds_local + 1][2]);
668 bond[0][nbonds_local][0] = p.id();
669 bond[0][nbonds_local][1] = partner_ids[0];
675 auto const n_bonds_local =
static_cast<int>(bond.shape()[1]);
676 auto &datasets = m_datasets;
677 int prefix_bonds = 0;
678 BOOST_MPI_CHECK_RESULT(
679 MPI_Exscan, (&n_bonds_local, &prefix_bonds, 1, MPI_INT, MPI_SUM, m_comm));
680 auto const n_bonds_total =
681 boost::mpi::all_reduce(m_comm, n_bonds_local, std::plus<int>());
683 datasets.at(
"/connectivity/atoms/value").getSpace().getDimensions();
684 Vector3s offset_bonds = {extents[0],
static_cast<std::size_t
>(prefix_bonds),
686 Vector3s count_bonds = {1,
static_cast<std::size_t
>(n_bonds_local), 2};
687 auto const n_bond_diff = std::max(
static_cast<hsize_t
>(n_bonds_total),
688 static_cast<hsize_t
>(extents[1])) -
690 Vector3s change_extent_bonds = {1,
static_cast<std::size_t
>(n_bond_diff), 0};
691 write_dataset(bond, datasets.at(
"/connectivity/atoms/value"),
692 change_extent_bonds, offset_bonds, count_bonds);
698 auto const view = std::views::elements<0>(
fields_map);
699 return {view.begin(), view.end()};
702File::File(std::filesystem::path file_path, std::filesystem::path script_path,
703 std::vector<std::string>
const &output_fields, std::string mass_unit,
704 std::string length_unit, std::string time_unit,
705 std::string force_unit, std::string velocity_unit,
706 std::string charge_unit,
int chunk_size)
707 : m_file_path(
std::move(file_path)), m_backup_path(m_file_path),
708 m_script_path(
std::move(script_path)),
709 m_absolute_script_path(
710 m_script_path.empty()
711 ?
std::filesystem::path()
712 :
std::filesystem::weakly_canonical(m_script_path)),
713 m_mass_unit(
std::move(mass_unit)), m_length_unit(
std::move(length_unit)),
714 m_time_unit(
std::move(time_unit)), m_force_unit(
std::move(force_unit)),
715 m_velocity_unit(
std::move(velocity_unit)),
716 m_charge_unit(
std::move(charge_unit)), m_chunk_size(chunk_size),
719 m_h5md_specification(m_fields) {
721 throw std::domain_error(
"Parameter 'chunk_size' must be > 0");
723 m_backup_path +=
".bak";