Apply clang-format to source tree
[alexxy/gromacs.git] / src / api / cpp / include / gmxapi / system.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,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.
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 #ifndef GMXAPI_SYSTEM_H
36 #define GMXAPI_SYSTEM_H
37 /*! \file
38  * \brief Declare container for molecular systems
39  *
40  * \ingroup gmxapi
41  */
42 #include <memory>
43 #include <string>
44
45 #include "gmxapi/gmxapi.h"
46 #include "gmxapi/status.h"
47
48 namespace gmxapi
49 {
50
51 // Forward declarations.
52 class Context; // full specification in gmxapi/context.h
53 class Session; // full specification in gmxapi/session.h
54
55 /*!
56  * \brief Deprecated: A wrapper for gmx::Mdrunner
57  *
58  * Container for molecular model and simulation parameters.
59  *
60  * \internal
61  * It was not intended as such, but gmxapi::System has ended up as basically a
62  * wrapper for the gmx::Mdrunner simulator object and serves as the aspect of a
63  * gmxapi Session that performs simulation work. As such, this class does not fit the
64  * current gmxapi paradigm and will be removed, reworked, or renamed soon.
65  *
66  * # Protocol
67  *
68  * As of gmxapi 0.0.6, a simulation is configured and launched as follows.
69  *
70  * 1. Caller gets a System handle with gmxapi::fromTprFile().
71  * 2. Caller optionally attaches additional MD Modules with
72  *    getSpec()->addModule(std::shared_ptr<gmxapi::MDModule> module).
73  *    See gmxapi::MDHolder
74  * 3. Caller gets a runnable object by passing a Context to System::launch()
75  *
76  * During launch() configured gmxapi::MDModules are attached to the simulator, which is
77  * then run by calling run() on the object returned by launch().
78  *
79  * \ingroup gmxapi
80  */
81 class System final
82 {
83 public:
84     /*! \brief Private implementation class.
85      *
86      * System::Impl does not have a public interface and is only exposed in opaque pointers.
87      */
88     class Impl;
89
90     /*! \brief No copy.
91      *
92      * The semantics of copying a System are ambiguous, so disallow implicit
93      * copy.
94      * \{
95      * \internal
96      * Some sort of prototype or clone idiom could be useful, but
97      * needs to explicitly identify any expensive operations.
98      */
99     System(const System&) = delete;
100     System& operator=(const System&) = delete;
101     /*! \} */
102
103     /*! \brief Allow move.
104      *
105      * \{
106      */
107     System(System&& /*unused*/) noexcept;
108     System& operator=(System&& /*unused*/) noexcept;
109     /*! \} */
110
111     /*!
112      * \brief Create by taking ownership of an implementation object.
113      *
114      * \param implementation
115      */
116     explicit System(std::unique_ptr<Impl> implementation);
117
118     /*! \cond internal
119      *  Destructor defined later to allow unique_ptr members of partially-defined types.
120      */
121     ~System();
122     /*! \endcond */
123
124     /*!
125      * \brief Configure the computing environment for the specified workflow.
126      *
127      * \return Ownership of a ready-to-run workflow or nullptr if there were errors.
128      *
129      * If errors occur, they will be stored in the context object. If run without
130      * an argument, launch() uses the current context of the System object. If a
131      * context argument is given, the system and its configured workflow are
132      * translated to the provided context and launched.
133      *
134      * \param context (optional) execution context in which to launch.
135      *
136      * \note The Session object does not "own" the Context, but must be able
137      * to extend the lifetime of the Context in which it is running.
138      *
139      * \todo Policy: does System then track the (potentially remote) context or should
140      * it be considered to have "forked", and the new session object retrieved from
141      * the session handle if needed?
142      *
143      * \cond internal
144      * # Protocol
145      *
146      * The current implementation of System::launch() performs the following actions.
147      *
148      * When launch() is called, a new gmxapi::Session is created by passing a
149      * gmxapi::Workflow to context->launch().
150      * The Workflow basically just contains the TPR filename.
151      * 1. A new Mdrunner is created from the information in the gmxapi::Workflow
152      * 2. A new Session is created using the ContextImpl and the runner
153      *
154      * Then, for each module available through getSpec()->getModules(),
155      * the session and module are passed to gmxapi::setSessionRestraint().
156      * 1. A gmx::IRestraintPotential is retrieved from the module.
157      * 2. A unique, named SessionResources is created for the module and attached to the SessionImpl.
158      *     1. The module is added as a signaller to the session SignalManager
159      *        with getSignalManager()->addSignaller(module->name())
160      *     2. The SessionResources is passed to IRestraintPotential::bindSession().
161      *        Currently, the only thing the restraint could do at this point is to
162      *        save a copy of the pointer and later pass it to gmxapi::getMdrunnerSignal().
163      *     3. The restraint is passed to gmx::Mdrunner::addPullPotential(),
164      *        which adds the restraint to the global gmx::restraint::Manager,
165      *        which then needs to be `clear()`ed after the runner completes.
166      *
167      * Shared ownership of the Session is returned to the caller of launch().
168      *
169      * \endcond
170      */
171     std::shared_ptr<Session> launch(const std::shared_ptr<Context>& context);
172
173 private:
174     /*!
175      * \brief Opaque pointer to implementation.
176      */
177     std::unique_ptr<Impl> impl_;
178 };
179
180
181 /*! \brief Defines an MD workflow from a TPR file.
182  *
183  * The TPR file has sufficient information to fully specify an MD run, though
184  * various parameters are implicit until the work is launched. The TPR filename
185  * provided must refer to identical TPR files at the API client and at the
186  * master rank of the execution host.
187  *
188  * \param filename Filesystem path of TPR file.
189  * \returns gmxapi::System object with the specified workflow.
190  * \ingroup gmxapi
191  */
192 System fromTprFile(const std::string& filename);
193
194 } // end namespace gmxapi
195
196 #endif // include guard