38f3cb0d4c2c4e379fced7ced966b5130abb4fda
[alexxy/gromacs.git] / src / gromacs / applied_forces / tests / densityfitting.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  * Tests for functionality of the density fitting module.
38  *
39  * \author Christian Blau <blau@kth.se>
40  * \ingroup module_applied_forces
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/applied_forces/densityfitting.h"
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/gmxlib/network.h"
49 #include "gromacs/math/paddedvector.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/mdtypes/enerdata.h"
52 #include "gromacs/mdtypes/forceoutput.h"
53 #include "gromacs/mdtypes/iforceprovider.h"
54 #include "gromacs/mdtypes/imdmodule.h"
55 #include "gromacs/mdtypes/imdpoptionprovider.h"
56 #include "gromacs/mdtypes/mdatom.h"
57 #include "gromacs/options/options.h"
58 #include "gromacs/options/treesupport.h"
59 #include "gromacs/utility/keyvaluetreebuilder.h"
60 #include "gromacs/utility/keyvaluetreetransform.h"
61 #include "gromacs/utility/mdmodulenotification.h"
62 #include "gromacs/utility/real.h"
63 #include "gromacs/utility/smalloc.h"
64 #include "gromacs/utility/stringcompare.h"
65
66 #include "testutils/testasserts.h"
67 #include "testutils/testfilemanager.h"
68 #include "testutils/testmatchers.h"
69
70 namespace gmx
71 {
72
73 namespace
74 {
75
76 class DensityFittingTest : public ::testing::Test
77 {
78 public:
79     void addMdpOptionDensityFittingActive()
80     {
81         mdpValueBuilder_.rootObject().addValue("density-guided-simulation-active",
82                                                std::string("yes"));
83     }
84
85     void addMdpOptionReferenceDensity()
86     {
87         mdpValueBuilder_.rootObject().addValue(
88                 "density-guided-simulation-reference-density-filename",
89                 std::string(test::TestFileManager::getInputFilePath("ellipsoid-density.mrc")));
90     }
91
92     //! build an mdp options tree that sets the options for the density fitting module
93     void makeDensityFittingModuleWithSetOptions()
94     {
95         KeyValueTreeObject mdpOptionsTree = mdpValueBuilder_.build();
96
97         densityFittingModule_ = DensityFittingModuleInfo::create(&notifier_);
98
99         // set up options
100         Options densityFittingModuleOptions;
101         densityFittingModule_->mdpOptionProvider()->initMdpOptions(&densityFittingModuleOptions);
102
103         // Add rules to transform mdp inputs to densityFittingModule data
104         KeyValueTreeTransformer transform;
105         transform.rules()->addRule().keyMatchType("/", StringCompareType::CaseAndDashInsensitive);
106         densityFittingModule_->mdpOptionProvider()->initMdpTransform(transform.rules());
107
108         // Execute the transform on the mdpValues
109         auto transformedMdpValues = transform.transform(mdpOptionsTree, nullptr);
110         assignOptionsFromKeyValueTree(&densityFittingModuleOptions, transformedMdpValues.object(), nullptr);
111     }
112
113     void intializeForceProviders()
114     {
115         densityFittingModule_->initForceProviders(&densityFittingForces_);
116     }
117
118 protected:
119     KeyValueTreeBuilder        mdpValueBuilder_;
120     MdModulesNotifier          notifier_;
121     ForceProviders             densityFittingForces_;
122     std::unique_ptr<IMDModule> densityFittingModule_;
123 };
124
125 TEST_F(DensityFittingTest, ForceProviderLackingInputThrows)
126 {
127     // Prepare MDP inputs
128     addMdpOptionDensityFittingActive();
129
130     makeDensityFittingModuleWithSetOptions();
131
132     // Build the force provider, once all input data is gathered
133     EXPECT_ANY_THROW(intializeForceProviders());
134 }
135
136 TEST_F(DensityFittingTest, SingleAtom)
137 {
138     // Prepare MDP inputs
139     addMdpOptionDensityFittingActive();
140     addMdpOptionReferenceDensity();
141
142     makeDensityFittingModuleWithSetOptions();
143
144     EXPECT_ANY_THROW(intializeForceProviders());
145 }
146
147 } // namespace
148
149 } // namespace gmx