3f956431e5b6048e07e6d1a0039d7053a46ab31e
[alexxy/gromacs.git] / src / gromacs / mdrun / mdmodules.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,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 /*! \libinternal \file
36  * \brief
37  * Declares gmx::MDModules.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_mdrun
42  */
43 #ifndef GMX_MDRUN_MDMODULES_H
44 #define GMX_MDRUN_MDMODULES_H
45
46 #include "gromacs/utility/classhelpers.h"
47
48
49 struct t_inputrec;
50
51 namespace gmx
52 {
53
54 class ForceProviders;
55 class KeyValueTreeObjectBuilder;
56 class KeyValueTreeObject;
57 class IKeyValueTreeErrorHandler;
58 class IKeyValueTreeTransformRules;
59 class IMDModule;
60 class IMDOutputProvider;
61 struct MdModulesNotifier;
62
63 /*! \libinternal \brief
64  * Manages the collection of all modules used for mdrun.
65  *
66  * This class acts as a central place for constructing modules for mdrun
67  * and wiring up dependencies between them.  This class should be the only
68  * place that contains the full list of modules, although in the future, some
69  * code (e.g., in tools) may benefit from the ability to only create one or a
70  * few modules and use them.
71  *
72  * The general idea is that each module takes care of its own data rather than
73  * mdrun having to know about all the details of each type of force calculation.
74  * Initially this is applied for simple things like electric field calculations
75  * but later more complex forces will be supported too.
76  *
77  * Currently, where the set of modules needs to be accessed, either a pointer
78  * to MDModules is passed around, or an instance of IMDOutputProvider or
79  * ForceProviders returned from MDModules.  These objects returned from
80  * MDModules call the corresponding methods in the relevant modules.
81  * In the future, some additional logic may need to be introduced at
82  * the call sites that can also influence the signature of the methods,
83  * similar to what ForceProviders already does for force computation.
84  *
85  * The assignOptionsToModules() and adjustInputrecBasedOnModules() methods of
86  * this class also take responsibility for wiring up the options (and their
87  * defaults) for each module.
88  *
89  * \inlibraryapi
90  * \ingroup module_mdrunutility
91  */
92 class MDModules
93 {
94 public:
95     MDModules();
96     ~MDModules();
97
98     /*! \brief
99      * Initializes a transform from mdp values to sectioned options.
100      *
101      * \see IMdpOptionProvider::initMdpTransform()
102      *
103      * Initializes the combined transform from all modules.
104      */
105     void initMdpTransform(IKeyValueTreeTransformRules* rules);
106
107     /*! \brief Initializes a builder of flat mdp-style key-value pairs
108      * suitable for output.
109      *
110      * If used as input to initMdpTransform(), the key-value pairs
111      * resulting from this function would leave the module
112      * settings unchanged.
113      *
114      * Once the transition from mdp to key-value input is
115      * complete, this method will probably not exist.
116      */
117     void buildMdpOutput(KeyValueTreeObjectBuilder* builder);
118
119     /*! \brief
120      * Sets input parameters from `params` for each module.
121      *
122      * \param[in]  params  Contains keys and values from user
123      *     input (and defaults) to configure modules that have
124      *     registered options with those keys.
125      * \param[out] errorHandler  Called to report errors.
126      */
127     void assignOptionsToModules(const KeyValueTreeObject& params, IKeyValueTreeErrorHandler* errorHandler);
128
129     /*! \brief
130      * Normalizes inputrec parameters to match current code version.
131      *
132      * This orders the parameters in `ir->param` to match the current code
133      * and adds any missing defaults.  It also throws an error if the
134      * inputrec contains parameters that are not recognized by any module.
135      */
136     void adjustInputrecBasedOnModules(t_inputrec* ir);
137
138     /*! \brief
139      * Returns an interface for initializing and finalizing output for modules.
140      */
141     IMDOutputProvider* outputProvider();
142     /*! \brief
143      * Returns an object for computing forces from the modules.
144      */
145     ForceProviders* initForceProviders();
146
147     /*!
148      * \brief Add a module to the container.
149      *
150      * An object may be added by a client to the bound MD Modules at run time.
151      * Both the client and the MDModules object may need to extend the life
152      * of the provided object. However, the MDModules container guarantees
153      * to extend the life of a provided object for as long as its consumers
154      * may attempt to use its the interfaces accessible through IMDModule
155      * methods.
156      *
157      * \param module implements some sort of modular functionality for MD.
158      *
159      * \note: There is not yet a way to add a IMDModule object between
160      * creation of the MDModules container and the execution of the various
161      * initialization protocols it supports.
162      *
163      * \internal
164      * Adding a module at an arbitrary point in the MDModules life breaks
165      * some assumptions in the protocol of the other member functions. If
166      * MDModules should not change after some point, we should move this
167      * to a builder class.
168      */
169     void add(std::shared_ptr<IMDModule> module);
170
171     /*! \brief Return a handle to the callbacks.
172      */
173     const MdModulesNotifier& notifier();
174
175 private:
176     class Impl;
177
178     PrivateImplPointer<Impl> impl_;
179 };
180
181 } // namespace gmx
182
183 #endif