Add index group option to density fitting
[alexxy/gromacs.git] / src / gromacs / applied_forces / densityfittingoptions.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \internal \file
36  * \brief
37  * Implements force provider for density fitting
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_applied_forces
41  */
42 #include "gmxpre.h"
43
44 #include "densityfittingoptions.h"
45
46 #include "gromacs/applied_forces/densityfitting.h"
47 #include "gromacs/options/basicoptions.h"
48 #include "gromacs/options/optionsection.h"
49 #include "gromacs/selection/indexutil.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/keyvaluetreebuilder.h"
52 #include "gromacs/utility/keyvaluetreetransform.h"
53 #include "gromacs/utility/strconvert.h"
54
55 namespace gmx
56 {
57
58 namespace
59 {
60
61 /*! \brief Helper to declare mdp transform rules.
62  *
63  * Enforces uniform mdp options that are always prepended with the correct
64  * string for the densityfitting mdp options.
65  *
66  * \tparam ToType type to be transformed to
67  * \tparam TransformWithFunctionType type of transformation function to be used
68  *
69  * \param[in] rules KVT transformation rules
70  * \param[in] transformationFunction the function to transform the flat kvt tree
71  * \param[in] optionTag string tag that describes the mdp option, appended to the
72  *                      default string for the density guided simulation
73  */
74 template <class ToType, class TransformWithFunctionType>
75 void densityfittingMdpTransformFromString(IKeyValueTreeTransformRules * rules,
76                                           TransformWithFunctionType     transformationFunction,
77                                           const std::string            &optionTag)
78 {
79     rules->addRule()
80         .from<std::string>("/" + DensityFittingModuleInfo::name_ + "-" + optionTag)
81         .to<ToType>("/" + DensityFittingModuleInfo::name_ +"/" + optionTag)
82         .transformWith(transformationFunction);
83 }
84 /*! \brief Helper to declare mdp output.
85  *
86  * Enforces uniform mdp options output sting that are always prepended with the
87  * correct string for the densityfitting mdp options and is consistent with the
88  * options name and transformation.
89  *
90  * \tparam OptionType the type of the mdp option
91  * \param[in] builder the KVT builder to generate the output
92  * \param[in] option the mdp option
93  * \param[in] optionTag string tag that describes the mdp option, appended to the
94  *                      default string for the density guided simulation
95  */
96 template <class OptionType>
97 void addDensityFittingMdpOutputValue(KeyValueTreeObjectBuilder *builder,
98                                      const OptionType          &option,
99                                      const std::string         &optionTag)
100 {
101     builder->addValue<OptionType>(DensityFittingModuleInfo::name_ + "-" + optionTag,
102                                   option);
103 }
104
105 }   // namespace
106
107 void DensityFittingOptions::initMdpTransform(IKeyValueTreeTransformRules * rules)
108 {
109     const auto &stringIdentityTransform = [](std::string s){
110             return s;
111         };
112     densityfittingMdpTransformFromString<bool>(rules, &fromStdString<bool>, c_activeTag_);
113     densityfittingMdpTransformFromString<std::string>(rules, stringIdentityTransform, c_groupTag_);
114 }
115
116 void DensityFittingOptions::buildMdpOutput(KeyValueTreeObjectBuilder *builder) const
117 {
118     addDensityFittingMdpOutputValue(builder, parameters_.active_, c_activeTag_);
119     if (parameters_.active_)
120     {
121         addDensityFittingMdpOutputValue(builder, groupString_, c_groupTag_);
122     }
123 }
124
125 void DensityFittingOptions::initMdpOptions(IOptionsContainerWithSections *options)
126 {
127     auto section = options->addSection(OptionSection(DensityFittingModuleInfo::name_.c_str()));
128
129     section.addOption(BooleanOption(c_activeTag_.c_str()).store(&parameters_.active_));
130     section.addOption(StringOption(c_groupTag_.c_str()).store(&groupString_));
131 }
132
133 bool DensityFittingOptions::active() const
134 {
135     return parameters_.active_;
136 }
137
138 const DensityFittingParameters &DensityFittingOptions::buildParameters()
139 {
140     return parameters_;
141 }
142
143 void DensityFittingOptions::setFitGroupIndices(const IndexGroupsAndNames &indexGroupsAndNames)
144 {
145     if (!parameters_.active_)
146     {
147         return;
148     }
149     parameters_.indices_ = indexGroupsAndNames.indices(groupString_);
150 }
151
152 void DensityFittingOptions::writeInternalParametersToKvt(KeyValueTreeObjectBuilder treeBuilder)
153 {
154     auto groupIndexAdder = treeBuilder.addUniformArray<index>(DensityFittingModuleInfo::name_ + "-" + c_groupTag_);
155     for (const auto &indexValue : parameters_.indices_)
156     {
157         groupIndexAdder.addValue(indexValue);
158     }
159 }
160
161 void DensityFittingOptions::readInternalParametersFromKvt(const KeyValueTreeObject &tree)
162 {
163     if (!parameters_.active_)
164     {
165         return;
166     }
167
168     if (!tree.keyExists(DensityFittingModuleInfo::name_ + "-" + c_groupTag_))
169     {
170         GMX_THROW(InconsistentInputError(
171                           "Cannot find atom index vector required for density guided simulation."));
172     }
173     auto kvtIndexArray = tree[DensityFittingModuleInfo::name_ + "-" + c_groupTag_].asArray().values();
174     parameters_.indices_.resize(kvtIndexArray.size());
175     std::transform(std::begin(kvtIndexArray), std::end(kvtIndexArray), std::begin(parameters_.indices_),
176                    [](const KeyValueTreeValue &val) { return val.cast<index>(); });
177 }
178
179 } // namespace gmx