e0ce323e2d3cc66b72b83319eac30fadb7fa56bd
[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/utility/keyvaluetreebuilder.h"
50 #include "gromacs/utility/keyvaluetreetransform.h"
51 #include "gromacs/utility/strconvert.h"
52
53 namespace gmx
54 {
55
56 namespace
57 {
58
59 /*! \brief Helper to declare mdp transform rules.
60  *
61  * Enforces uniform mdp options that are always prepended with the correct
62  * string for the densityfitting mdp options.
63  *
64  * \tparam ToType type to be transformed to
65  * \tparam TransformWithFunctionType type of transformation function to be used
66  *
67  * \param[in] rules KVT transformation rules
68  * \param[in] transformationFunction the function to transform the flat kvt tree
69  * \param[in] optionTag string tag that describes the mdp option, appended to the
70  *                      default string for the density guided simulation
71  */
72 template <class ToType, class TransformWithFunctionType>
73 void densityfittingMdpTransformFromString(IKeyValueTreeTransformRules * rules,
74                                           TransformWithFunctionType     transformationFunction,
75                                           const std::string            &optionTag)
76 {
77     rules->addRule()
78         .from<std::string>("/" + DensityFittingModuleInfo::name_ + "-" + optionTag)
79         .to<ToType>("/" + DensityFittingModuleInfo::name_ +"/" + optionTag)
80         .transformWith(transformationFunction);
81 }
82 /*! \brief Helper to declare mdp output.
83  *
84  * Enforces uniform mdp options output sting that are always prepended with the
85  * correct string for the densityfitting mdp options and is consistent with the
86  * options name and transformation.
87  *
88  * \tparam OptionType the type of the mdp option
89  * \param[in] builder the KVT builder to generate the output
90  * \param[in] option the mdp option
91  * \param[in] optionTag string tag that describes the mdp option, appended to the
92  *                      default string for the density guided simulation
93  */
94 template <class OptionType>
95 void addDensityFittingMdpOutputValue(KeyValueTreeObjectBuilder *builder,
96                                      const OptionType          &option,
97                                      const std::string         &optionTag)
98 {
99     builder->addValue<OptionType>(DensityFittingModuleInfo::name_ + "-" + optionTag,
100                                   option);
101 }
102
103 }   // namespace
104
105 void DensityFittingOptions::initMdpTransform(IKeyValueTreeTransformRules * rules)
106 {
107     densityfittingMdpTransformFromString<bool>(rules, &fromStdString<bool>, c_activeTag_);
108 }
109
110 void DensityFittingOptions::buildMdpOutput(KeyValueTreeObjectBuilder *builder) const
111 {
112     addDensityFittingMdpOutputValue(builder, parameters_.active_, c_activeTag_);
113 }
114
115 void DensityFittingOptions::initMdpOptions(IOptionsContainerWithSections *options)
116 {
117     auto section = options->addSection(OptionSection(DensityFittingModuleInfo::name_.c_str()));
118
119     section.addOption(BooleanOption(c_activeTag_.c_str()).store(&parameters_.active_));
120 }
121
122 bool DensityFittingOptions::active() const
123 {
124     return parameters_.active_;
125 }
126
127 const DensityFittingParameters &DensityFittingOptions::buildParameters()
128 {
129     return parameters_;
130 }
131
132 } // namespace gmx