Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / python_packaging / src / gmxapi / export_context.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \file
36  * \brief Bindings for Context class.
37  *
38  *
39  * \author M. Eric Irrgang <ericirrgang@gmail.com>
40  *
41  * \ingroup module_python
42  */
43 #include "module.h"
44
45 #include "gmxapi/context.h"
46 #include "gmxapi/exceptions.h"
47 #include "pycontext.h"
48
49
50 namespace gmxpy
51 {
52
53 namespace detail
54 {
55
56 namespace py = pybind11;
57
58
59 /*! \internal
60  * \brief Update a parameter structure for a simulation execution context.
61  *
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.
64  *
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
69  *
70  * For reference and default values, see
71  * http://manual.gromacs.org/current/onlinehelp/gmx-mdrun.html#options
72  */
73 static void setMDArgs(std::vector<std::string>* mdargs, const py::dict& params)
74 {
75     mdargs->clear();
76     if (params.contains("grid"))
77     {
78         if (py::len(params["grid"]) == 0)
79         {
80             throw gmxapi::UsageError("Grid argument must describe domain decomposition grid.");
81         }
82         std::vector<std::string> vals;
83         auto                     iterator = py::iter(params["grid"]);
84         while (iterator != py::iterator::sentinel())
85         {
86             vals.emplace_back(py::cast<std::string>(py::str(iterator)));
87             ++iterator;
88         }
89         mdargs->emplace_back("-dd");
90         for (auto&& val : vals)
91         {
92             mdargs->emplace_back(val);
93         }
94     }
95     if (params.contains("pme_ranks"))
96     {
97         auto val = py::cast<std::string>(py::str(params["pme_ranks"]));
98         mdargs->emplace_back("-npme");
99         mdargs->emplace_back(val);
100     }
101     if (params.contains("threads"))
102     {
103         auto val = py::cast<std::string>(py::str(params["threads"]));
104         mdargs->emplace_back("-nt");
105         mdargs->emplace_back(val);
106     }
107     if (params.contains("tmpi"))
108     {
109         auto val = py::cast<std::string>(py::str(params["tmpi"]));
110         mdargs->emplace_back("-ntmpi");
111         mdargs->emplace_back(val);
112     }
113     if (params.contains("threads_per_rank"))
114     {
115         auto val = py::cast<std::string>(py::str(params["threads_per_rank"]));
116         mdargs->emplace_back("-ntomp");
117         mdargs->emplace_back(val);
118     }
119     if (params.contains("pme_threads_per_rank"))
120     {
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);
124     }
125     if (params.contains("steps"))
126     {
127         auto val = py::cast<std::string>(py::str(params["steps"]));
128         mdargs->emplace_back("-nsteps");
129         mdargs->emplace_back(val);
130     }
131     if (params.contains("max_hours"))
132     {
133         auto val = py::cast<std::string>(py::str(params["max_hours"]));
134         mdargs->emplace_back("-maxh");
135         mdargs->emplace_back(val);
136     }
137     if (params.contains("append_output"))
138     {
139         try
140         {
141             if (!params["append_output"].cast<bool>())
142             {
143                 mdargs->emplace_back("-noappend");
144             }
145         }
146         catch (const py::cast_error& e)
147         {
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
151             throw;
152         }
153     }
154 }
155
156 void export_context(py::module& m)
157 {
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.");
161     mdargs.def("set",
162                [](MDArgs* self, const py::dict& params) { setMDArgs(self, params); },
163                "Assign parameters in MDArgs from Python dict.");
164
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.");
169
170     context.def("add_mdmodule", &PyContext::addMDModule, "Add an MD plugin for the simulation.");
171 }
172
173 } // namespace detail
174
175 } // end namespace gmxpy