ddf950237273d4d4bf5762e54fa63eed487c72e9
[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/real.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/stringcompare.h"
64
65 #include "testutils/testasserts.h"
66 #include "testutils/testmatchers.h"
67
68 namespace gmx
69 {
70
71 namespace
72 {
73
74 TEST(DensityFittingTest, ForceOnSingleOption)
75 {
76     auto densityFittingModule(DensityFittingModuleInfo::create());
77
78     // Prepare MDP inputs
79     KeyValueTreeBuilder      mdpValueBuilder;
80     mdpValueBuilder.rootObject().addValue("density-guided-simulation-active", std::string("yes"));
81     KeyValueTreeObject       densityFittingMdpValues = mdpValueBuilder.build();
82
83     // set up options
84     Options densityFittingModuleOptions;
85     densityFittingModule->mdpOptionProvider()->initMdpOptions(&densityFittingModuleOptions);
86
87     // Add rules to transform mdp inputs to densityFittingModule data
88     KeyValueTreeTransformer transform;
89     transform.rules()->addRule().keyMatchType("/", StringCompareType::CaseAndDashInsensitive);
90     densityFittingModule->mdpOptionProvider()->initMdpTransform(transform.rules());
91
92     // Execute the transform on the mdpValues
93     auto transformedMdpValues = transform.transform(densityFittingMdpValues, nullptr);
94     assignOptionsFromKeyValueTree(&densityFittingModuleOptions, transformedMdpValues.object(), nullptr);
95
96     // Build the force provider, once all input data is gathered
97     ForceProviders densityFittingForces;
98     densityFittingModule->initForceProviders(&densityFittingForces);
99
100     // Build a minimal simulation system.
101     t_mdatoms               mdAtoms;
102     mdAtoms.homenr = 1;
103     PaddedVector<RVec>      x             = {{0, 0, 0}};
104     matrix                  simulationBox = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
105     const double            t             = 0.0;
106     t_commrec              *cr            = init_commrec();
107     ForceProviderInput      forceProviderInput(x, mdAtoms, t, simulationBox, *cr);
108
109     // The forces that the force-provider is to update
110     PaddedVector<RVec>       f = {{0, 0, 0}};
111     ForceWithVirial          forceWithVirial(f, false);
112
113     gmx_enerdata_t           energyData(1, 0);
114     ForceProviderOutput      forceProviderOutput(&forceWithVirial, &energyData);
115
116     // update the forces
117     densityFittingForces.calculateForces(forceProviderInput, &forceProviderOutput);
118
119     // check that calculated forces match expected forces
120     std::vector<RVec> expectedForce = {{0, 0, 0}};
121     EXPECT_THAT(expectedForce, Pointwise(test::RVecEq(test::defaultFloatTolerance()),
122                                          forceProviderOutput.forceWithVirial_.force_));
123
124     // clean up C-style commrec, so this test leaks no memory
125     // \todo remove once commrec manages its own memory
126     done_commrec(cr);
127 }
128
129 } // namespace
130
131 } // namespace gmx