fbf1bdd3beceb36903f2158ccf2f3907f23370cd
[alexxy/gromacs.git] / src / gromacs / mdrunutility / mdmodules.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018, 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 #include "gmxpre.h"
36
37 #include "mdmodules.h"
38
39 #include <memory>
40
41 #include "gromacs/applied-forces/electricfield.h"
42 #include "gromacs/compat/make_unique.h"
43 #include "gromacs/mdtypes/iforceprovider.h"
44 #include "gromacs/mdtypes/imdmodule.h"
45 #include "gromacs/mdtypes/imdoutputprovider.h"
46 #include "gromacs/mdtypes/imdpoptionprovider.h"
47 #include "gromacs/mdtypes/inputrec.h"
48 #include "gromacs/options/options.h"
49 #include "gromacs/options/optionsection.h"
50 #include "gromacs/options/treesupport.h"
51 #include "gromacs/utility/keyvaluetree.h"
52 #include "gromacs/utility/keyvaluetreebuilder.h"
53 #include "gromacs/utility/keyvaluetreetransform.h"
54 #include "gromacs/utility/smalloc.h"
55
56 namespace gmx
57 {
58
59 class MDModules::Impl : public IMDOutputProvider
60 {
61     public:
62
63         Impl()
64             : field_(createElectricFieldModule())
65         {
66         }
67
68         void makeModuleOptions(Options *options)
69         {
70             // Create a section for applied-forces modules
71             auto appliedForcesOptions = options->addSection(OptionSection("applied-forces"));
72             field_->mdpOptionProvider()->initMdpOptions(&appliedForcesOptions);
73             // In future, other sections would also go here.
74         }
75
76         // From IMDOutputProvider
77         void initOutput(FILE *fplog, int nfile, const t_filenm fnm[],
78                         bool bAppendFiles, const gmx_output_env_t *oenv) override
79         {
80             field_->outputProvider()->initOutput(fplog, nfile, fnm, bAppendFiles, oenv);
81         }
82         void finishOutput() override
83         {
84             field_->outputProvider()->finishOutput();
85         }
86
87         std::unique_ptr<IMDModule>      field_;
88         std::unique_ptr<ForceProviders> forceProviders_;
89
90         /*! \brief List of registered MDModules
91          *
92          * Note that MDModules::Impl owns this container, but it is only used by
93          * the MDModules::initForceProviders() function. To be consistent with
94          * IMDModule's vision, as indicated by its docs, we should
95          * \todo update IMDModule docs to allow nullptr return values
96          * \todo check for nullptr returned by IMDModule methods.
97          * \todo include field_ in modules_
98          */
99         std::vector< std::shared_ptr<IMDModule> > modules_;
100 };
101
102 MDModules::MDModules() : impl_(new Impl)
103 {
104 }
105
106 MDModules::~MDModules()
107 {
108 }
109
110 void MDModules::initMdpTransform(IKeyValueTreeTransformRules *rules)
111 {
112     auto appliedForcesScope = rules->scopedTransform("/applied-forces");
113     impl_->field_->mdpOptionProvider()->initMdpTransform(appliedForcesScope.rules());
114 }
115
116 void MDModules::buildMdpOutput(KeyValueTreeObjectBuilder *builder)
117 {
118     impl_->field_->mdpOptionProvider()->buildMdpOutput(builder);
119 }
120
121 void MDModules::assignOptionsToModules(const KeyValueTreeObject  &params,
122                                        IKeyValueTreeErrorHandler *errorHandler)
123 {
124     Options moduleOptions;
125     impl_->makeModuleOptions(&moduleOptions);
126     // The actual output is in the data fields of the modules that
127     // were set up in the module options.
128     assignOptionsFromKeyValueTree(&moduleOptions, params, errorHandler);
129 }
130
131 void MDModules::adjustInputrecBasedOnModules(t_inputrec *ir)
132 {
133     Options moduleOptions;
134     impl_->makeModuleOptions(&moduleOptions);
135
136     checkForUnknownOptionsInKeyValueTree(*ir->params, moduleOptions);
137
138     std::unique_ptr<KeyValueTreeObject> params(
139             new KeyValueTreeObject(
140                     adjustKeyValueTreeFromOptions(*ir->params, moduleOptions)));
141     delete ir->params;
142     ir->params = params.release();
143 }
144
145 IMDOutputProvider *MDModules::outputProvider()
146 {
147     return impl_.get();
148 }
149
150 ForceProviders *MDModules::initForceProviders()
151 {
152     GMX_RELEASE_ASSERT(impl_->forceProviders_ == nullptr,
153                        "Force providers initialized multiple times");
154     impl_->forceProviders_ = compat::make_unique<ForceProviders>();
155     impl_->field_->initForceProviders(impl_->forceProviders_.get());
156     for (auto && module : impl_->modules_)
157     {
158         module->initForceProviders(impl_->forceProviders_.get());
159     }
160     return impl_->forceProviders_.get();
161 }
162
163 void MDModules::add(std::shared_ptr<gmx::IMDModule> module)
164 {
165     impl_->modules_.emplace_back(std::move(module));
166 }
167
168 } // namespace gmx