2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, 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 Bindings for Context class.
39 * \author M. Eric Irrgang <ericirrgang@gmail.com>
41 * \ingroup module_python
45 #include "gmxapi/context.h"
46 #include "gmxapi/exceptions.h"
47 #include "pycontext.h"
56 namespace py = pybind11;
60 * \brief Update a parameter structure for a simulation execution context.
62 * \param mdargs [OUT] Container for parameters made available to the MD library context.
63 * \param params Python dictionary mapping parameter names to argument values.
65 * Note that the current library infrastructure does not provide a way for the
66 * simulation machinery to express human-readable parameter names with rich
67 * descriptions, so a few of the most necessary mdrun command line parameters
68 * are hard coded here. Ref. https://gitlab.com/gromacs/gromacs/-/issues/2877
70 * For reference and default values, see
71 * http://manual.gromacs.org/current/onlinehelp/gmx-mdrun.html#options
73 static void setMDArgs(std::vector<std::string>* mdargs, const py::dict& params)
76 if (params.contains("grid"))
78 if (py::len(params["grid"]) == 0)
80 throw gmxapi::UsageError("Grid argument must describe domain decomposition grid.");
82 std::vector<std::string> vals;
83 auto iterator = py::iter(params["grid"]);
84 while (iterator != py::iterator::sentinel())
86 vals.emplace_back(py::cast<std::string>(py::str(iterator)));
89 mdargs->emplace_back("-dd");
90 for (auto&& val : vals)
92 mdargs->emplace_back(val);
95 if (params.contains("pme_ranks"))
97 auto val = py::cast<std::string>(py::str(params["pme_ranks"]));
98 mdargs->emplace_back("-npme");
99 mdargs->emplace_back(val);
101 if (params.contains("threads"))
103 auto val = py::cast<std::string>(py::str(params["threads"]));
104 mdargs->emplace_back("-nt");
105 mdargs->emplace_back(val);
107 if (params.contains("tmpi"))
109 auto val = py::cast<std::string>(py::str(params["tmpi"]));
110 mdargs->emplace_back("-ntmpi");
111 mdargs->emplace_back(val);
113 if (params.contains("threads_per_rank"))
115 auto val = py::cast<std::string>(py::str(params["threads_per_rank"]));
116 mdargs->emplace_back("-ntomp");
117 mdargs->emplace_back(val);
119 if (params.contains("pme_threads_per_rank"))
121 auto val = py::cast<std::string>(py::str(params["threads_per_pme_rank"]));
122 mdargs->emplace_back("-ntomp_pme");
123 mdargs->emplace_back(val);
125 if (params.contains("steps"))
127 auto val = py::cast<std::string>(py::str(params["steps"]));
128 mdargs->emplace_back("-nsteps");
129 mdargs->emplace_back(val);
131 if (params.contains("max_hours"))
133 auto val = py::cast<std::string>(py::str(params["max_hours"]));
134 mdargs->emplace_back("-maxh");
135 mdargs->emplace_back(val);
137 if (params.contains("append_output"))
141 if (!params["append_output"].cast<bool>())
143 mdargs->emplace_back("-noappend");
146 catch (const py::cast_error& e)
148 // Couldn't cast to bool for some reason.
149 // Convert to gmxapi exception (not implemented)
150 // ref. https://github.com/kassonlab/gmxapi/issues/125
156 void export_context(py::module& m)
158 // Add argument type before it is used for more sensible automatic bindings behavior.
159 py::class_<MDArgs, std::unique_ptr<MDArgs>> mdargs(m, "MDArgs");
160 mdargs.def(py::init(), "Create an empty MDArgs object.");
162 [](MDArgs* self, const py::dict& params) { setMDArgs(self, params); },
163 "Assign parameters in MDArgs from Python dict.");
165 // Export execution context class
166 py::class_<PyContext, std::shared_ptr<PyContext>> context(m, "Context");
167 context.def(py::init(), "Create a default execution context.");
168 context.def("setMDArgs", &PyContext::setMDArgs, "Set MD runtime parameters.");
170 context.def("add_mdmodule", &PyContext::addMDModule, "Add an MD plugin for the simulation.");
173 } // namespace detail
175 } // end namespace gmxpy