2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Tests for mrc file data structure.
39 * \author Christian Blau <blau@kth.se>
40 * \ingroup module_fileio
44 #include "gromacs/fileio/mrcdensitymap.h"
48 #include <gmock/gmock.h>
49 #include <gtest/gtest.h>
51 #include "gromacs/fileio/mrcdensitymapheader.h"
52 #include "gromacs/utility/inmemoryserializer.h"
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
65 TEST(MrcDensityMap, RoundTripIsIdempotent)
67 // write header and data to serializer, store the serialized data
69 MrcDensityMapHeader header{};
70 header.numColumnRowSection_ = { 1, 1, 1 };
72 std::vector<float> data(numberOfExpectedDataItems(header));
74 MrcDensityMapOfFloatWriter mrcDensityMapWriter(header, data);
75 InMemorySerializer serializer;
76 mrcDensityMapWriter.write(&serializer);
78 const auto serializedFile = serializer.finishAndGetBuffer();
80 // read and write again
81 InMemoryDeserializer deserializer(serializedFile, false);
82 MrcDensityMapOfFloatReader mrcDensityMapReader(&deserializer);
84 MrcDensityMapOfFloatWriter writerOfDeserializedOutput(mrcDensityMapReader.header(),
85 mrcDensityMapReader.constView());
86 writerOfDeserializedOutput.write(&serializer);
88 const auto roundTripResult = serializer.finishAndGetBuffer();
90 // compare serialized data after reading and writing again to serialized data after one-time serialization
91 EXPECT_THAT(serializedFile, testing::Pointwise(testing::Eq(), roundTripResult));
94 TEST(MrcDensityMap, ThrowsFileIOErrorWhenFileNotPresent)
96 EXPECT_THROW(MrcDensityMapOfFloatFromFileReader(""), FileIOError);
99 TEST(MrcDensityMap, ReadsCoordinateTransformationFromFile)
101 RVec coordinate1(0, 0, 0);
102 RVec coordinate2(0, 0, 1);
103 RVec coordinate3(1, 0, 0);
104 RVec coordinate4(0, 1, 0);
106 MrcDensityMapOfFloatFromFileReader mrcFileReader(
107 TestFileManager::getInputFilePath("ellipsoid-density.mrc"));
108 TranslateAndScale coordinateTransformation(mrcFileReader.transformationToDensityLattice());
110 coordinateTransformation({ &coordinate1, &coordinate1 + 1 });
111 coordinateTransformation({ &coordinate2, &coordinate2 + 1 });
113 EXPECT_REAL_EQ(0, coordinate1[XX]);
114 EXPECT_REAL_EQ(-2, coordinate1[YY]);
115 EXPECT_REAL_EQ(0, coordinate1[ZZ]);
117 EXPECT_REAL_EQ(0, coordinate2[XX]);
118 EXPECT_REAL_EQ(-2, coordinate2[YY]);
119 EXPECT_REAL_EQ(1.25, coordinate2[ZZ]);
121 EXPECT_REAL_EQ(1, coordinate3[XX]);
122 EXPECT_REAL_EQ(0, coordinate3[YY]);
123 EXPECT_REAL_EQ(0, coordinate3[ZZ]);
125 EXPECT_REAL_EQ(0, coordinate4[XX]);
126 EXPECT_REAL_EQ(1, coordinate4[YY]);
127 EXPECT_REAL_EQ(0, coordinate4[ZZ]);
130 TEST(MrcDensityMap, ReadsDensityDataFromFile)
132 MrcDensityMapOfFloatFromFileReader mrcFileReader(
133 TestFileManager::getInputFilePath("ellipsoid-density.mrc"));
134 const auto densityData = mrcFileReader.densityDataCopy();
136 TestReferenceData refData;
137 TestReferenceChecker checker(refData.rootChecker());
138 checker.checkSequence(begin(densityData.asConstView()), end(densityData.asConstView()),
139 "data ellipsoid density");