Avoid signed-overflow warning with gcc 7
[gromacs.git] / src / gromacs / mdrunutility / mdmodules.cpp
blob2ee50c8928047a6021bcae1d4601a3ac9a6c82f0
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, 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.
35 #include "gmxpre.h"
37 #include "mdmodules.h"
39 #include <memory>
41 #include "gromacs/applied-forces/electricfield.h"
42 #include "gromacs/mdtypes/iforceprovider.h"
43 #include "gromacs/mdtypes/imdmodule.h"
44 #include "gromacs/mdtypes/imdoutputprovider.h"
45 #include "gromacs/mdtypes/imdpoptionprovider.h"
46 #include "gromacs/mdtypes/inputrec.h"
47 #include "gromacs/options/options.h"
48 #include "gromacs/options/optionsection.h"
49 #include "gromacs/options/treesupport.h"
50 #include "gromacs/utility/keyvaluetree.h"
51 #include "gromacs/utility/keyvaluetreebuilder.h"
52 #include "gromacs/utility/keyvaluetreetransform.h"
53 #include "gromacs/utility/smalloc.h"
55 namespace gmx
58 class MDModules::Impl : public IMDOutputProvider
60 public:
62 Impl()
63 : field_(createElectricFieldModule())
67 void makeModuleOptions(Options *options)
69 // Create a section for applied-forces modules
70 auto appliedForcesOptions = options->addSection(OptionSection("applied-forces"));
71 field_->mdpOptionProvider()->initMdpOptions(&appliedForcesOptions);
72 // In future, other sections would also go here.
75 // From IMDOutputProvider
76 virtual void initOutput(FILE *fplog, int nfile, const t_filenm fnm[],
77 bool bAppendFiles, const gmx_output_env_t *oenv)
79 field_->outputProvider()->initOutput(fplog, nfile, fnm, bAppendFiles, oenv);
81 virtual void finishOutput()
83 field_->outputProvider()->finishOutput();
86 std::unique_ptr<IMDModule> field_;
87 std::unique_ptr<ForceProviders> forceProviders_;
90 MDModules::MDModules() : impl_(new Impl)
94 MDModules::~MDModules()
98 void MDModules::initMdpTransform(IKeyValueTreeTransformRules *rules)
100 auto appliedForcesScope = rules->scopedTransform("/applied-forces");
101 impl_->field_->mdpOptionProvider()->initMdpTransform(appliedForcesScope.rules());
104 void MDModules::buildMdpOutput(KeyValueTreeObjectBuilder *builder)
106 impl_->field_->mdpOptionProvider()->buildMdpOutput(builder);
109 void MDModules::assignOptionsToModules(const KeyValueTreeObject &params,
110 IKeyValueTreeErrorHandler *errorHandler)
112 Options moduleOptions;
113 impl_->makeModuleOptions(&moduleOptions);
114 // The actual output is in the data fields of the modules that
115 // were set up in the module options.
116 assignOptionsFromKeyValueTree(&moduleOptions, params, errorHandler);
119 void MDModules::adjustInputrecBasedOnModules(t_inputrec *ir)
121 Options moduleOptions;
122 impl_->makeModuleOptions(&moduleOptions);
124 checkForUnknownOptionsInKeyValueTree(*ir->params, moduleOptions);
126 std::unique_ptr<KeyValueTreeObject> params(
127 new KeyValueTreeObject(
128 adjustKeyValueTreeFromOptions(*ir->params, moduleOptions)));
129 delete ir->params;
130 ir->params = params.release();
133 IMDOutputProvider *MDModules::outputProvider()
135 return impl_.get();
138 ForceProviders *MDModules::initForceProviders()
140 GMX_RELEASE_ASSERT(impl_->forceProviders_ == nullptr,
141 "Force providers initialized multiple times");
142 impl_->forceProviders_.reset(new ForceProviders);
143 impl_->field_->initForceProviders(impl_->forceProviders_.get());
144 return impl_->forceProviders_.get();
147 } // namespace gmx