2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Exports TPR I/O tools during Python module initialization.
38 * Provides _gmxapi.SimulationParameters and _gmxapi.TprFile classes, as well
39 * as module functions read_tprfile, write_tprfile, copy_tprfile, and rewrite_tprfile.
41 * TprFile is a Python object that holds a gmxapicompat::TprReadHandle.
43 * SimulationParameters is the Python type for data sources providing the
44 * simulation parameters aspect of input to simulation operations.
46 * \author M. Eric Irrgang <ericirrgang@gmail.com>
47 * \ingroup module_python
50 #include "gmxapi/exceptions.h"
60 void detail::export_tprfile(pybind11::module &module)
62 namespace py = pybind11;
63 using gmxapicompat::GmxMdParams;
64 using gmxapicompat::TprReadHandle;
65 using gmxapicompat::readTprFile;
67 py::class_<GmxMdParams> mdparams(module, "SimulationParameters");
68 // We don't want Python users to create invalid params objects, so don't
69 // export a constructor until we can default initialize a valid one.
70 // mdparams.def(py::init());
71 mdparams.def("extract",
72 [](const GmxMdParams &self)
75 for (const auto &key: gmxapicompat::keys(self))
79 // TODO: More complete typing and dispatching.
80 // This only handles the two types described in the initial implementation.
81 // Less trivial types (strings, maps, arrays) warrant additional
82 // design discussion before being exposed through an interface
84 // Also reference https://redmine.gromacs.org/issues/2993
86 // We can use templates and/or tag dispatch in a more complete
87 // future implementation.
88 const auto ¶mType = gmxapicompat::mdParamToType(key);
89 if (gmxapicompat::isFloat(paramType))
91 dictionary[key.c_str()] = extractParam(self, key, double());
93 else if (gmxapicompat::isInt(paramType))
95 dictionary[key.c_str()] = extractParam(self, key, int64_t());
98 catch (const gmxapicompat::ValueError &e)
100 throw gmxapi::ProtocolError(std::string("Unknown parameter: ") + key);
105 "Get a dictionary of the parameters.");
107 // Overload a setter for each known type and None
109 [](GmxMdParams* self, const std::string &key, py::int_ value)
111 gmxapicompat::setParam(self, key, py::cast<int64_t>(value));
113 py::arg("key").none(false),
114 py::arg("value").none(false),
115 "Use a dictionary to update simulation parameters.");
117 [](GmxMdParams* self, const std::string &key, py::float_ value)
119 gmxapicompat::setParam(self, key, py::cast<double>(value));
121 py::arg("key").none(false),
122 py::arg("value").none(false),
123 "Use a dictionary to update simulation parameters.");
125 [](GmxMdParams* self, const std::string &key, py::none)
127 // unsetParam(self, key);
129 py::arg("key").none(false),
131 "Use a dictionary to update simulation parameters.");
134 py::class_<TprReadHandle> tprfile(module, "TprFile");
135 tprfile.def("params",
136 [](const TprReadHandle &self)
138 auto params = gmxapicompat::getMdParams(self);
142 module.def("read_tprfile",
145 "Get a handle to a TPR file resource for a given file name.");
147 module.def("write_tprfile",
148 [](std::string filename, const GmxMdParams ¶meterObject)
150 auto tprReadHandle = gmxapicompat::getSourceFileHandle(parameterObject);
151 auto params = gmxapicompat::getMdParams(tprReadHandle);
152 auto structure = gmxapicompat::getStructureSource(tprReadHandle);
153 auto state = gmxapicompat::getSimulationState(tprReadHandle);
154 auto topology = gmxapicompat::getTopologySource(tprReadHandle);
155 gmxapicompat::writeTprFile(filename, params, structure, state, topology);
157 py::arg("filename").none(false),
158 py::arg("parameters"),
159 "Write a new TPR file with the provided data.");
161 module.def("copy_tprfile",
162 [](const gmxapicompat::TprReadHandle &input, std::string outFile)
164 return gmxpy::copy_tprfile(input, outFile);
167 py::arg("destination"),
168 "Copy a TPR file from `source` to `destination`."
171 module.def("rewrite_tprfile",
172 [](std::string input, std::string output, double end_time)
174 return gmxpy::rewrite_tprfile(input, output, end_time);
177 py::arg("destination"),
179 "Copy a TPR file from `source` to `destination`, replacing `nsteps` with `end_time`.");
182 } // end namespace gmxpy