SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / fileio / mrcdensitymapheader.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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  * Implement mrc/ccp4-file metadata.
38  *
39  * \author Christian Blau <blau@kth.se>
40  *
41  * \ingroup module_fileio
42  */
43 #include "gmxpre.h"
44
45 #include "mrcdensitymapheader.h"
46
47 #include <algorithm>
48
49 #include "gromacs/utility/exceptions.h"
50
51 namespace gmx
52 {
53
54 namespace
55 {
56
57 //! Returns true if any of the argument values is smaller than zero.
58 template<typename Container>
59 bool anySmallerZero(Container values)
60 {
61     return std::any_of(std::begin(values), std::end(values), [](auto v) { return v < 0; });
62 }
63
64 //! Returns true if any of the argument values is larger than a given boundary value.
65 template<typename Container>
66 bool anyLargerThanValue(Container values, typename Container::value_type boundaryValue)
67 {
68     return std::any_of(std::begin(values), std::end(values), [boundaryValue](auto v) {
69         return v > boundaryValue;
70     });
71 }
72
73 } // namespace
74
75 size_t numberOfExpectedDataItems(const MrcDensityMapHeader& header)
76 {
77     if (anySmallerZero(header.numColumnRowSection_))
78     {
79         GMX_THROW(
80                 InternalError("Cannot determine data size, because the mrc "
81                               "density map header is invalid (Negative number "
82                               "describing data extent in at least one dimension)."));
83     }
84
85     return header.numColumnRowSection_[XX] * header.numColumnRowSection_[YY]
86            * header.numColumnRowSection_[ZZ];
87 }
88
89 TranslateAndScale getCoordinateTransformationToLattice(const MrcDensityMapHeader& header)
90 {
91     constexpr real c_AAtoNmConversion = 0.1;
92
93     RVec       scale = { header.extent_[XX] / (header.cellLength_[XX] * c_AAtoNmConversion),
94                    header.extent_[YY] / (header.cellLength_[YY] * c_AAtoNmConversion),
95                    header.extent_[ZZ] / (header.cellLength_[ZZ] * c_AAtoNmConversion) };
96     const RVec emdbOrigin{ header.userDefinedFloat_[12],
97                            header.userDefinedFloat_[13],
98                            header.userDefinedFloat_[14] };
99     RVec       translation;
100     if (emdbOrigin[XX] == 0. && emdbOrigin[YY] == 0. && emdbOrigin[ZZ] == 0.)
101     {
102         translation = RVec{ -header.columnRowSectionStart_[XX] / scale[XX],
103                             -header.columnRowSectionStart_[YY] / scale[YY],
104                             -header.columnRowSectionStart_[ZZ] / scale[ZZ] };
105     }
106     else
107     {
108         translation = { -emdbOrigin[XX] * c_AAtoNmConversion,
109                         -emdbOrigin[YY] * c_AAtoNmConversion,
110                         -emdbOrigin[ZZ] * c_AAtoNmConversion };
111     }
112     return { scale, translation };
113 }
114
115 dynamicExtents3D getDynamicExtents3D(const MrcDensityMapHeader& header)
116 {
117     return { header.numColumnRowSection_[ZZ],
118              header.numColumnRowSection_[YY],
119              header.numColumnRowSection_[XX] };
120 };
121
122 bool mrcHeaderIsSane(const MrcDensityMapHeader& header)
123 {
124     // Make sure all numbers of columns, row sections, extents and cell angles
125     // are positive
126     if (anySmallerZero(header.numColumnRowSection_) || anySmallerZero(header.cellAngles_)
127         || anySmallerZero(header.extent_))
128     {
129         return false;
130     }
131
132     // The maximum integer number in an mrc header to be considered sane
133     constexpr std::int32_t c_maxIntegerNumber = 100'000;
134     if (anyLargerThanValue(header.numColumnRowSection_, c_maxIntegerNumber)
135         || anyLargerThanValue(header.extent_, c_maxIntegerNumber))
136     {
137         return false;
138     }
139
140     constexpr std::int32_t c_maxCellAngle = 360;
141     if (anyLargerThanValue(header.cellAngles_, c_maxCellAngle))
142     {
143         return false; //NOLINT(readability-simplify-boolean-expr)
144     }
145
146     return true;
147 }
148
149 } // namespace gmx