Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / tests / moduletest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
5  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in moduletest.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_trajectoryanalysis
42  */
43 #include "gmxpre.h"
44
45 #include "moduletest.h"
46
47 #include <map>
48 #include <string>
49 #include <vector>
50
51 #include "gromacs/commandline/cmdlineoptionsmodule.h"
52 #include "gromacs/trajectoryanalysis/analysismodule.h"
53 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
54
55 #include "gromacs/analysisdata/tests/datatest.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/refdata.h"
58 #include "testutils/testasserts.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64
65 /********************************************************************
66  * AbstractTrajectoryAnalysisModuleTestFixture::Impl
67  */
68
69 class AbstractTrajectoryAnalysisModuleTestFixture::Impl
70 {
71 public:
72     struct DatasetInfo
73     {
74         DatasetInfo() : bCheck(true), tolerance(defaultRealTolerance()) {}
75
76         bool                   bCheck;
77         FloatingPointTolerance tolerance;
78     };
79
80     typedef std::map<std::string, DatasetInfo> DatasetList;
81
82     explicit Impl(AbstractTrajectoryAnalysisModuleTestFixture* parent);
83
84     TrajectoryAnalysisModule& module();
85     void                      ensureModuleCreated();
86     bool                      hasCheckedDatasets() const;
87
88     AbstractTrajectoryAnalysisModuleTestFixture& parent_;
89     TrajectoryAnalysisModulePointer              module_;
90     DatasetList                                  datasets_;
91     bool                                         bDatasetsIncluded_;
92 };
93
94 AbstractTrajectoryAnalysisModuleTestFixture::Impl::Impl(AbstractTrajectoryAnalysisModuleTestFixture* parent) :
95     parent_(*parent), bDatasetsIncluded_(false)
96 {
97 }
98
99 TrajectoryAnalysisModule& AbstractTrajectoryAnalysisModuleTestFixture::Impl::module()
100 {
101     ensureModuleCreated();
102     return *module_;
103 }
104
105 void AbstractTrajectoryAnalysisModuleTestFixture::Impl::ensureModuleCreated()
106 {
107     if (module_.get() == nullptr)
108     {
109         module_ = parent_.createModule();
110         const std::vector<std::string>& datasetNames(module_->datasetNames());
111         datasets_.clear();
112         std::vector<std::string>::const_iterator i;
113         for (i = datasetNames.begin(); i != datasetNames.end(); ++i)
114         {
115             datasets_[*i] = DatasetInfo();
116         }
117     }
118 }
119
120 bool AbstractTrajectoryAnalysisModuleTestFixture::Impl::hasCheckedDatasets() const
121 {
122     DatasetList::const_iterator dataset;
123     for (dataset = datasets_.begin(); dataset != datasets_.end(); ++dataset)
124     {
125         if (dataset->second.bCheck)
126         {
127             return true;
128         }
129     }
130     return false;
131 }
132
133 /********************************************************************
134  * AbstractTrajectoryAnalysisModuleTestFixture
135  */
136
137 AbstractTrajectoryAnalysisModuleTestFixture::AbstractTrajectoryAnalysisModuleTestFixture() :
138     impl_(new Impl(this))
139 {
140 }
141
142 AbstractTrajectoryAnalysisModuleTestFixture::~AbstractTrajectoryAnalysisModuleTestFixture() {}
143
144 void AbstractTrajectoryAnalysisModuleTestFixture::setTopology(const char* filename)
145 {
146     setInputFile("-s", filename);
147 }
148
149 void AbstractTrajectoryAnalysisModuleTestFixture::setTrajectory(const char* filename)
150 {
151     setInputFile("-f", filename);
152 }
153
154 void AbstractTrajectoryAnalysisModuleTestFixture::includeDataset(const char* name)
155 {
156     impl_->ensureModuleCreated();
157     if (!impl_->bDatasetsIncluded_)
158     {
159         Impl::DatasetList::iterator i;
160         for (i = impl_->datasets_.begin(); i != impl_->datasets_.end(); ++i)
161         {
162             i->second.bCheck = false;
163         }
164     }
165     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
166     const bool                  bFound  = (dataset != impl_->datasets_.end());
167     GMX_RELEASE_ASSERT(bFound, "Attempted to include a non-existent dataset");
168     dataset->second.bCheck = true;
169 }
170
171 void AbstractTrajectoryAnalysisModuleTestFixture::excludeDataset(const char* name)
172 {
173     impl_->ensureModuleCreated();
174     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
175     const bool                  bFound  = (dataset != impl_->datasets_.end());
176     GMX_RELEASE_ASSERT(bFound, "Attempted to exclude a non-existent dataset");
177     dataset->second.bCheck = false;
178 }
179
180 void AbstractTrajectoryAnalysisModuleTestFixture::setDatasetTolerance(const char* name,
181                                                                       const FloatingPointTolerance& tolerance)
182 {
183     impl_->ensureModuleCreated();
184     Impl::DatasetList::iterator dataset = impl_->datasets_.find(name);
185     const bool                  bFound  = (dataset != impl_->datasets_.end());
186     GMX_RELEASE_ASSERT(bFound, "Attempted to set a tolerance for a non-existent dataset");
187     dataset->second.tolerance = tolerance;
188 }
189
190 void AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine& args)
191 {
192     TrajectoryAnalysisModule& module  = impl_->module();
193     CommandLine&              cmdline = commandLine();
194     cmdline.merge(args);
195
196     TestReferenceChecker rootChecker(this->rootChecker());
197     rootChecker.checkString(args.toString(), "CommandLine");
198
199     if (impl_->hasCheckedDatasets())
200     {
201         TestReferenceChecker dataChecker(rootChecker.checkCompound("OutputData", "Data"));
202         Impl::DatasetList::const_iterator dataset;
203         for (dataset = impl_->datasets_.begin(); dataset != impl_->datasets_.end(); ++dataset)
204         {
205             if (dataset->second.bCheck)
206             {
207                 const char* const     name = dataset->first.c_str();
208                 AbstractAnalysisData& data = module.datasetFromName(name);
209                 AnalysisDataTestFixture::addReferenceCheckerModule(
210                         dataChecker, name, &data, dataset->second.tolerance);
211             }
212         }
213     }
214
215     ICommandLineOptionsModulePointer runner(
216             TrajectoryAnalysisCommandLineRunner::createModule(std::move(impl_->module_)));
217     int rc = 0;
218     EXPECT_NO_THROW_GMX(rc = CommandLineTestHelper::runModuleDirect(std::move(runner), &cmdline));
219     EXPECT_EQ(0, rc);
220
221     checkOutputFiles();
222 }
223
224 } // namespace test
225 } // namespace gmx