2 * This file is part of the GROMACS molecular simulation package.
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.
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.
37 * Implements force provider for density fitting
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_applied_forces
44 #include "densityfittingoptions.h"
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"
61 /*! \brief Helper to declare mdp transform rules.
63 * Enforces uniform mdp options that are always prepended with the correct
64 * string for the densityfitting mdp options.
66 * \tparam ToType type to be transformed to
67 * \tparam TransformWithFunctionType type of transformation function to be used
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
74 template <class ToType, class TransformWithFunctionType>
75 void densityfittingMdpTransformFromString(IKeyValueTreeTransformRules * rules,
76 TransformWithFunctionType transformationFunction,
77 const std::string &optionTag)
80 .from<std::string>("/" + DensityFittingModuleInfo::name_ + "-" + optionTag)
81 .to<ToType>("/" + DensityFittingModuleInfo::name_ +"/" + optionTag)
82 .transformWith(transformationFunction);
84 /*! \brief Helper to declare mdp output.
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.
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
96 template <class OptionType>
97 void addDensityFittingMdpOutputValue(KeyValueTreeObjectBuilder *builder,
98 const OptionType &option,
99 const std::string &optionTag)
101 builder->addValue<OptionType>(DensityFittingModuleInfo::name_ + "-" + optionTag,
107 void DensityFittingOptions::initMdpTransform(IKeyValueTreeTransformRules * rules)
109 const auto &stringIdentityTransform = [](std::string s){
112 densityfittingMdpTransformFromString<bool>(rules, &fromStdString<bool>, c_activeTag_);
113 densityfittingMdpTransformFromString<std::string>(rules, stringIdentityTransform, c_groupTag_);
116 void DensityFittingOptions::buildMdpOutput(KeyValueTreeObjectBuilder *builder) const
118 addDensityFittingMdpOutputValue(builder, parameters_.active_, c_activeTag_);
119 if (parameters_.active_)
121 addDensityFittingMdpOutputValue(builder, groupString_, c_groupTag_);
125 void DensityFittingOptions::initMdpOptions(IOptionsContainerWithSections *options)
127 auto section = options->addSection(OptionSection(DensityFittingModuleInfo::name_.c_str()));
129 section.addOption(BooleanOption(c_activeTag_.c_str()).store(¶meters_.active_));
130 section.addOption(StringOption(c_groupTag_.c_str()).store(&groupString_));
133 bool DensityFittingOptions::active() const
135 return parameters_.active_;
138 const DensityFittingParameters &DensityFittingOptions::buildParameters()
143 void DensityFittingOptions::setFitGroupIndices(const IndexGroupsAndNames &indexGroupsAndNames)
145 if (!parameters_.active_)
149 parameters_.indices_ = indexGroupsAndNames.indices(groupString_);
152 void DensityFittingOptions::writeInternalParametersToKvt(KeyValueTreeObjectBuilder treeBuilder)
154 auto groupIndexAdder = treeBuilder.addUniformArray<index>(DensityFittingModuleInfo::name_ + "-" + c_groupTag_);
155 for (const auto &indexValue : parameters_.indices_)
157 groupIndexAdder.addValue(indexValue);
161 void DensityFittingOptions::readInternalParametersFromKvt(const KeyValueTreeObject &tree)
163 if (!parameters_.active_)
168 if (!tree.keyExists(DensityFittingModuleInfo::name_ + "-" + c_groupTag_))
170 GMX_THROW(InconsistentInputError(
171 "Cannot find atom index vector required for density guided simulation."));
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>(); });