Implementation of QMMM, QMMMSimulationParameterSetup and QMMMOutputProvider classes
[alexxy/gromacs.git] / src / gromacs / applied_forces / qmmm / tests / qmmm.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2021, 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 QMMM module.
38  *
39  * \author Dmitry Morozov <dmitry.morozov@jyu.fi>
40  * \author Christian Blau <blau@kth.se>
41  * \ingroup module_applied_forces
42  */
43 #include "gmxpre.h"
44
45 #include "gromacs/applied_forces/qmmm/qmmm.h"
46
47 #include <gtest/gtest.h>
48
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/math/paddedvector.h"
51 #include "gromacs/math/vec.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/mdmodulesnotifiers.h"
63 #include "gromacs/utility/real.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/stringcompare.h"
66
67 #include "testutils/testasserts.h"
68 #include "testutils/testfilemanager.h"
69 #include "testutils/testmatchers.h"
70
71 namespace gmx
72 {
73
74 namespace
75 {
76
77 class QMMMTest : public ::testing::Test
78 {
79 public:
80     void addMdpOptionQMMMActive()
81     {
82         mdpValueBuilder_.rootObject().addValue("qmmm-cp2k-active", std::string("yes"));
83     }
84
85     //! build an mdp options tree that sets the options for the qmmm module
86     void makeQMMMModuleWithSetOptions()
87     {
88         KeyValueTreeObject mdpOptionsTree = mdpValueBuilder_.build();
89
90         QMMMModule_ = QMMMModuleInfo::create();
91
92         // set up options
93         Options QMMMModuleOptions;
94         QMMMModule_->mdpOptionProvider()->initMdpOptions(&QMMMModuleOptions);
95
96         // Add rules to transform mdp inputs to QMMMModule data
97         KeyValueTreeTransformer transform;
98         transform.rules()->addRule().keyMatchType("/", StringCompareType::CaseAndDashInsensitive);
99         QMMMModule_->mdpOptionProvider()->initMdpTransform(transform.rules());
100
101         // Execute the transform on the mdpValues
102         auto transformedMdpValues = transform.transform(mdpOptionsTree, nullptr);
103         assignOptionsFromKeyValueTree(&QMMMModuleOptions, transformedMdpValues.object(), nullptr);
104     }
105
106     void intializeForceProviders() { QMMMModule_->initForceProviders(&QMMMForces_); }
107
108 protected:
109     KeyValueTreeBuilder        mdpValueBuilder_;
110     ForceProviders             QMMMForces_;
111     std::unique_ptr<IMDModule> QMMMModule_;
112 };
113
114 TEST_F(QMMMTest, ForceProviderLackingInputThrows)
115 {
116     // Prepare MDP inputs
117     addMdpOptionQMMMActive();
118
119     // Initialize with default options
120     makeQMMMModuleWithSetOptions();
121
122     // Build the force provider, once all input data is gathered
123     EXPECT_ANY_THROW(intializeForceProviders());
124 }
125
126 } // namespace
127
128 } // namespace gmx