2493d52c2adbab4ca2ef39c67b12133ff44f6b24
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / tests / extract_cluster.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 "extract-cluster" trajectory analysis module.
38  *
39  * \author Paul Bauer <paul.bauer.q@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/trajectoryanalysis/modules/extract_cluster.h"
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/utility/path.h"
49 #include "gromacs/utility/stringutil.h"
50
51 #include "testutils/cmdlinetest.h"
52 #include "testutils/filematchers.h"
53 #include "testutils/refdata.h"
54 #include "testutils/testfilemanager.h"
55 #include "testutils/textblockmatchers.h"
56
57 #include "moduletest.h"
58
59 namespace gmx
60 {
61
62 namespace test
63 {
64
65 namespace
66 {
67
68 /********************************************************************
69  * Tests for gmx::analysismodules::ExtractCluster.
70  */
71
72 //! Helper struct to combine filename, path and FileMatcher
73 struct ManualOutputFile
74 {
75     //! Generated file name.
76     std::string filename;
77     //! Full generated file path.
78     std::string fullFilepath;
79     //! Corresponding file matcher to compare to reference.
80     FileMatcherPointer matcher;
81 };
82
83 //! Test fixture for the convert-trj analysis module.
84 class ExtractClusterModuleTest : public AbstractTrajectoryAnalysisModuleTestFixture
85 {
86 protected:
87     TrajectoryAnalysisModulePointer createModule() override
88     {
89         return analysismodules::ExtractClusterInfo::create();
90     }
91
92 public:
93     //! Constructor
94     ExtractClusterModuleTest();
95
96     //! Compare generated files to expected ones.
97     void compareFiles();
98
99 private:
100     //! Array of names and paths for generated files.
101     std::array<ManualOutputFile, 8> generatedFiles;
102 };
103
104 ExtractClusterModuleTest::ExtractClusterModuleTest()
105 {
106     std::string outputFilepath = gmx::Path::getWorkingDirectory();
107
108     fileManager().setOutputTempDirectory(outputFilepath);
109     // Those are for cleaning up the files generated during testing.
110     fileManager().getTemporaryFilePath("csizew.xpm");
111     fileManager().getTemporaryFilePath("csize.xpm");
112
113     setTrajectory("extract_cluster.trr");
114     setInputFile("-clusters", "extract_cluster.ndx");
115
116     int fileNumber = 1;
117     for (auto& generatedFile : generatedFiles)
118     {
119         generatedFile.filename = gmx::Path::concatenateBeforeExtension(
120                 "test.g96", gmx::formatString("_Cluster_000%d", fileNumber));
121         generatedFile.matcher      = TextFileMatch(ExactTextMatch()).createFileMatcher();
122         generatedFile.fullFilepath = fileManager().getTemporaryFilePath(generatedFile.filename);
123         fileNumber++;
124     }
125 }
126
127 void ExtractClusterModuleTest::compareFiles()
128 {
129     TestReferenceChecker outputChecker(rootChecker().checkCompound("ClusterOutputFiles", "Files"));
130     for (const auto& file : generatedFiles)
131     {
132         TestReferenceChecker manualFileChecker(outputChecker.checkCompound("File", file.filename.c_str()));
133         file.matcher->checkFile(file.fullFilepath, &manualFileChecker);
134     }
135 }
136
137 TEST_F(ExtractClusterModuleTest, WorksWithAllAtoms)
138 {
139     std::string       realFileName = TestFileManager::getTestSpecificFileName("test.g96");
140     const char* const cmdline[]    = { "extract-cluster", "-o", realFileName.c_str() };
141
142     runTest(CommandLine(cmdline));
143     compareFiles();
144 }
145
146 TEST_F(ExtractClusterModuleTest, WorksWithAtomSubset)
147 {
148     std::string       realFileName = TestFileManager::getTestSpecificFileName("test.g96");
149     const char* const cmdline[]    = { "extract-cluster", "-o", realFileName.c_str(), "-select",
150                                     "atomnr 1 2" };
151
152     runTest(CommandLine(cmdline));
153     compareFiles();
154 }
155
156 } // namespace
157 } // namespace test
158 } // namespace gmx