2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
38 * Implements classes in moduletest.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_trajectoryanalysis
45 #include "moduletest.h"
51 #include "gromacs/commandline/cmdlineoptionsmodule.h"
52 #include "gromacs/trajectoryanalysis/analysismodule.h"
53 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
55 #include "gromacs/analysisdata/tests/datatest.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/refdata.h"
58 #include "testutils/testasserts.h"
65 /********************************************************************
66 * AbstractTrajectoryAnalysisModuleTestFixture::Impl
69 class AbstractTrajectoryAnalysisModuleTestFixture::Impl
74 DatasetInfo() : bCheck(true), tolerance(defaultRealTolerance()) {}
77 FloatingPointTolerance tolerance;
80 typedef std::map<std::string, DatasetInfo> DatasetList;
82 explicit Impl(AbstractTrajectoryAnalysisModuleTestFixture* parent);
84 TrajectoryAnalysisModule& module();
85 void ensureModuleCreated();
86 bool hasCheckedDatasets() const;
88 AbstractTrajectoryAnalysisModuleTestFixture& parent_;
89 TrajectoryAnalysisModulePointer module_;
90 DatasetList datasets_;
91 bool bDatasetsIncluded_;
94 AbstractTrajectoryAnalysisModuleTestFixture::Impl::Impl(AbstractTrajectoryAnalysisModuleTestFixture* parent) :
95 parent_(*parent), bDatasetsIncluded_(false)
99 TrajectoryAnalysisModule& AbstractTrajectoryAnalysisModuleTestFixture::Impl::module()
101 ensureModuleCreated();
105 void AbstractTrajectoryAnalysisModuleTestFixture::Impl::ensureModuleCreated()
107 if (module_.get() == nullptr)
109 module_ = parent_.createModule();
110 const std::vector<std::string>& datasetNames(module_->datasetNames());
112 std::vector<std::string>::const_iterator i;
113 for (i = datasetNames.begin(); i != datasetNames.end(); ++i)
115 datasets_[*i] = DatasetInfo();
120 bool AbstractTrajectoryAnalysisModuleTestFixture::Impl::hasCheckedDatasets() const
122 DatasetList::const_iterator dataset;
123 for (dataset = datasets_.begin(); dataset != datasets_.end(); ++dataset)
125 if (dataset->second.bCheck)
133 /********************************************************************
134 * AbstractTrajectoryAnalysisModuleTestFixture
137 AbstractTrajectoryAnalysisModuleTestFixture::AbstractTrajectoryAnalysisModuleTestFixture() :
138 impl_(new Impl(this))
142 AbstractTrajectoryAnalysisModuleTestFixture::~AbstractTrajectoryAnalysisModuleTestFixture() {}
144 void AbstractTrajectoryAnalysisModuleTestFixture::setTopology(const char* filename)
146 setInputFile("-s", filename);
149 void AbstractTrajectoryAnalysisModuleTestFixture::setTrajectory(const char* filename)
151 setInputFile("-f", filename);
154 void AbstractTrajectoryAnalysisModuleTestFixture::includeDataset(const char* name)
156 impl_->ensureModuleCreated();
157 if (!impl_->bDatasetsIncluded_)
159 Impl::DatasetList::iterator i;
160 for (i = impl_->datasets_.begin(); i != impl_->datasets_.end(); ++i)
162 i->second.bCheck = false;
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;
171 void AbstractTrajectoryAnalysisModuleTestFixture::excludeDataset(const char* name)
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;
180 void AbstractTrajectoryAnalysisModuleTestFixture::setDatasetTolerance(const char* name,
181 const FloatingPointTolerance& tolerance)
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;
190 void AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine& args)
192 TrajectoryAnalysisModule& module = impl_->module();
193 CommandLine& cmdline = commandLine();
196 TestReferenceChecker rootChecker(this->rootChecker());
197 rootChecker.checkString(args.toString(), "CommandLine");
199 if (impl_->hasCheckedDatasets())
201 TestReferenceChecker dataChecker(rootChecker.checkCompound("OutputData", "Data"));
202 Impl::DatasetList::const_iterator dataset;
203 for (dataset = impl_->datasets_.begin(); dataset != impl_->datasets_.end(); ++dataset)
205 if (dataset->second.bCheck)
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);
215 ICommandLineOptionsModulePointer runner(
216 TrajectoryAnalysisCommandLineRunner::createModule(std::move(impl_->module_)));
218 EXPECT_NO_THROW_GMX(rc = CommandLineTestHelper::runModuleDirect(std::move(runner), &cmdline));