ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
h5md.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2025 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "config/config.hpp"
23
24#ifdef ESPRESSO_H5MD
25
26#include "h5md.hpp"
27
32
33#include <cassert>
34#include <cmath>
35#include <filesystem>
36#include <string>
37#include <vector>
38
39namespace ScriptInterface {
40namespace Writer {
41
44 {{"file_path", m_h5md, &::Writer::H5md::File::file_path},
45 {"chunk_size", m_h5md, &::Writer::H5md::File::chunk_size},
46 {"script_path", m_h5md, &::Writer::H5md::File::script_path},
47 {"fields", AutoParameter::read_only,
48 [this]() { return make_vector_of_variants(m_output_fields); }},
49 {"mass_unit", m_h5md, &::Writer::H5md::File::mass_unit},
50 {"length_unit", m_h5md, &::Writer::H5md::File::length_unit},
51 {"time_unit", m_h5md, &::Writer::H5md::File::time_unit},
52 {"force_unit", m_h5md, &::Writer::H5md::File::force_unit},
53 {"velocity_unit", m_h5md, &::Writer::H5md::File::velocity_unit},
54 {"charge_unit", m_h5md, &::Writer::H5md::File::charge_unit}});
55};
56
58 m_output_fields = get_value<std::vector<std::string>>(params, "fields");
59 m_h5md =
60 make_shared_from_args<::Writer::H5md::File, std::filesystem::path,
61 std::filesystem::path, std::vector<std::string>,
62 std::string, std::string, std::string, std::string,
63 std::string, std::string, int>(
64 params, "file_path", "script_path", "fields", "mass_unit",
65 "length_unit", "time_unit", "force_unit", "velocity_unit",
66 "charge_unit", "chunk_size");
67 // MPI communicator is needed to close parallel file handles
68 m_mpi_env_lock = ::communication_environment->get_mpi_env();
69}
70
72 m_h5md.reset();
73 assert(m_h5md.use_count() == 0u);
74 m_mpi_env_lock.reset();
75}
76
77Variant H5md::do_call_method(std::string const &name, VariantMap const &) {
78 if (name == "write") {
79 auto const &system = ::System::get_system();
80 auto const particles = system.cell_structure->local_particles();
81 auto const sim_time = system.get_sim_time();
82 auto const time_step = system.get_time_step();
83 auto const n_steps = static_cast<int>(std::round(sim_time / time_step));
84 m_h5md->write(particles, sim_time, n_steps, *system.box_geo);
85 } else if (name == "flush") {
86 m_h5md->flush();
87 } else if (name == "close") {
88 m_h5md->close();
89 } else if (name == "valid_fields") {
90 return make_vector_of_variants(m_h5md->valid_fields());
91 }
92 return {};
93}
94
95} // namespace Writer
96} // namespace ScriptInterface
97
98#endif // ESPRESSO_H5MD
void add_parameters(std::vector< AutoParameter > &&params)
std::string_view name() const
void do_construct(VariantMap const &params) override
Definition h5md.cpp:57
Variant do_call_method(const std::string &name, const VariantMap &parameters) override
Definition h5md.cpp:77
Class for writing H5MD files.
Definition h5md_core.hpp:75
auto const & chunk_size() const
Retrieve the set chunk size.
auto const & length_unit() const
Retrieve the set length unit.
auto const & time_unit() const
Retrieve the set time unit.
auto const & file_path() const
Retrieve the path to the hdf5 file.
auto const & force_unit() const
Retrieve the set force unit.
auto const & mass_unit() const
Retrieve the set mass unit.
auto const & charge_unit() const
Retrieve the set charge unit.
auto const & velocity_unit() const
Retrieve the set velocity unit.
auto const & script_path() const
Retrieve the path to the simulation script.
std::unique_ptr< CommunicationEnvironment > communication_environment
This file contains the asynchronous MPI communication.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:148
std::shared_ptr< T > make_shared_from_args(VariantMap const &vals, ArgNames &&...args)
Make a new std::shared_ptr<T> with arguments extracted from a VariantMap.
System & get_system()
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84