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